3 * @file firm_opt.c -- Firm-generating back end optimizations.
5 * (C) 2005-2007 Michael Beck beck@ipd.info.uni-karlsruhe.de
11 #include <libfirm/firm.h>
14 #include <libfirm/be.h>
15 #endif /* FIRM_BACKEND */
19 #include "firm_codegen.h"
20 #include "firm_cmdline.h"
21 #include "firm_timing.h"
23 #if defined(_DEBUG) || defined(FIRM_DEBUG)
24 #define DBG(x) dbg_printf x
27 #endif /* _DEBUG || FIRM_DEBUG */
30 /** dump all the graphs depending on cond */
31 #define DUMP_ALL(cond, suffix) \
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);\
40 dump_all_ir_graphs(dump_ir_block_graph, suffix); \
45 /** dump all control flow graphs depending on cond */
46 #define DUMP_ALL_CFG(cond, suffix) \
49 timer_push(TV_VCG_DUMP); \
50 dump_all_ir_graphs(dump_cfg, suffix); \
55 /** check all graphs depending on cond */
56 #define CHECK_ALL(cond) \
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); \
69 /** dump graphs irg depending on cond */
70 #define DUMP_ONE(cond, irg, suffix) \
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); \
79 dump_ir_block_graph(irg, suffix); \
84 /** dump control flow graph irg depending on cond */
85 #define DUMP_ONE_CFG(cond, irg, suffix) \
88 timer_push(TV_VCG_DUMP); \
89 dump_cfg(irg, suffix); \
94 /** check a graph irg depending on cond */
95 #define CHECK_ONE(cond, irg) \
98 timer_push(TV_VERIFY); \
99 irg_verify(irg, VRFY_ENFORCE_SSA); \
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;
111 /* entities of runtime functions */
112 ir_entity_ptr rts_entities[rts_max];
115 * factory for setting architecture dependent parameters
117 static const ir_settings_arch_dep_t *arch_factory(void)
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 */
127 32 /* Mulh allowed up to 32 bit */
130 return ad_param ? ad_param : ¶m;
134 * Map runtime functions.
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. */
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 },
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 },
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 },
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 },
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 }
217 i_record rec[sizeof(mapper)/sizeof(mapper[0])];
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;
230 lower_intrinsics(rec, n_map, /* part_block_used=*/0);
233 static int *irg_dump_no;
235 static void dump_graph_count(ir_graph *const irg, const char *const suffix)
238 snprintf(name, sizeof(name), "-%02d_%s", irg_dump_no[get_irg_idx(irg)]++, suffix);
239 DUMP_ONE(1, irg, name);
242 static void dump_graph_cfg_count(ir_graph *const irg, const char *const suffix)
245 snprintf(name, sizeof(name), "-%02d_%s", irg_dump_no[get_irg_idx(irg)]++, suffix);
246 DUMP_ONE_CFG(1, irg, name);
249 static void dump_all_count(const char *const suffix)
251 const int n_irgs = get_irp_n_irgs();
254 for (i = 0; i < n_irgs; ++i)
255 dump_graph_count(get_irp_irg(i), suffix);
258 #define DUMP_ONE_C(cond, irg, suffix) \
261 dump_graph_count((irg), (suffix)); \
265 #define DUMP_ONE_CFG_C(cond, irg, suffix) \
268 dump_graph_cfg_count((irg), (suffix)); \
272 #define DUMP_ALL_C(cond, suffix) \
275 dump_all_count((suffix)); \
280 * run all the Firm optimizations
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
285 static void do_firm_optimizations(const char *input_filename, int firm_const_exists)
291 /* FIXME: cloning might ADD new graphs. */
292 irg_dump_no = calloc(get_irp_last_idx(), sizeof(*irg_dump_no));
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);
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;
305 set_irp_memory_disambiguator_options(aa_opt);
307 timer_start(TV_ALL_OPT);
309 if (firm_opt.remove_unused) {
310 ir_entity **keep_methods;
313 /* Analysis that finds the free methods,
314 i.e. methods that are dereferenced.
315 Optimizes polymorphic calls :-). */
316 cgana(&arr_len, &keep_methods);
318 /* Remove methods that are never called. */
319 gc_irgs(arr_len, keep_methods);
324 if (! firm_opt.freestanding) {
326 DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "rts");
327 CHECK_ALL(firm_opt.check_all);
330 if (firm_opt.tail_rec) {
331 timer_push(TV_TAIL_REC);
332 opt_tail_recursion();
335 DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "tail_rec");
336 CHECK_ALL(firm_opt.check_all);
339 if (firm_opt.func_calls) {
340 timer_push(TV_REAL_FUNC_CALL);
341 optimize_funccalls(firm_const_exists, NULL);
343 DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "func_call");
344 CHECK_ALL(firm_opt.check_all);
347 if (firm_opt.do_inline) {
348 timer_push(TV_INLINE);
349 inline_leave_functions(500, 80, 30, FALSE);
351 DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "inl");
352 CHECK_ALL(firm_opt.check_all);
355 for (i = 0; i < get_irp_n_irgs(); i++) {
356 irg = current_ir_graph = get_irp_irg(i);
360 /* If SIMD optimization is on, make sure we have only 1 return */
361 if (firm_ext_grs.create_pattern || firm_ext_grs.simd_opt)
362 normalize_one_return(irg);
367 if (firm_opt.modes) {
368 /* convert all modes into integer if possible */
369 arch_mode_conversion(irg, predefs.mode_uint);
372 timer_push(TV_SCALAR_REPLACE);
373 scalar_replacement_opt(irg);
375 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "scalar");
376 CHECK_ONE(firm_opt.check_all, irg);
378 timer_push(TV_LOCAL_OPT);
379 optimize_graph_df(irg);
381 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "lopt");
382 CHECK_ONE(firm_opt.check_all, irg);
384 timer_push(TV_REASSOCIATION);
385 optimize_reassociation(irg);
387 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "reassoc");
388 CHECK_ONE(firm_opt.check_all, irg);
390 if (firm_opt.confirm) {
391 /* Confirm construction currently can only handle blocks with only one control
392 flow predecessor. Calling optimize_cf here removes Bad predecessors and help
393 the optimization of switch constructs. */
394 timer_push(TV_CF_OPT);
397 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cfopt");
398 CHECK_ONE(firm_opt.check_all, irg);
399 timer_push(TV_CONFIRM_CREATE);
400 construct_confirms(irg);
402 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "confirms");
403 CHECK_ONE(firm_opt.check_all, irg);
406 timer_push(TV_LOCAL_OPT);
407 optimize_graph_df(irg);
409 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "lopt");
410 CHECK_ONE(firm_opt.check_all, irg);
413 CHECK_ONE(firm_opt.check_all, irg);
415 if (firm_opt.code_place) {
416 timer_push(TV_CODE_PLACE);
417 set_opt_global_cse(1);
418 optimize_graph_df(irg);
420 set_opt_global_cse(0);
422 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "place");
423 CHECK_ONE(firm_opt.check_all, irg);
426 if (firm_opt.luffig) {
428 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "ldst2");
429 CHECK_ONE(firm_opt.check_all, irg);
432 timer_push(TV_CF_OPT);
435 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cfopt");
436 CHECK_ONE(firm_opt.check_all, irg);
438 /* should we really remove the Confirm here? */
439 if (firm_opt.confirm) {
440 timer_push(TV_CONFIRM_CREATE);
441 remove_confirms(irg);
445 irg_verify(irg, VRFY_ENFORCE_SSA);
446 if (firm_opt.gvn_pre) {
448 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "pre");
449 CHECK_ONE(firm_opt.check_all, irg);
450 irg_verify(irg, VRFY_ENFORCE_SSA);
453 if (firm_opt.loop_unrolling) {
454 timer_push(TV_LOOP_UNROLL);
455 optimize_loop_unrolling(irg);
457 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "loop");
458 CHECK_ONE(firm_opt.check_all, irg);
461 if (firm_opt.load_store) {
462 timer_push(TV_LOAD_STORE);
463 optimize_load_store(irg);
465 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "ldst");
466 CHECK_ONE(firm_opt.check_all, irg);
469 lower_highlevel_graph(irg, firm_opt.lower_bitfields);
471 if (firm_opt.deconv) {
472 timer_push(TV_DECONV);
475 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "deconv");
476 CHECK_ONE(firm_opt.check_all, irg);
479 if (firm_opt.cond_eval) {
480 timer_push(TV_COND_EVAL);
483 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cond_eval");
484 CHECK_ONE(firm_opt.check_all, irg);
488 compute_postdoms(irg);
489 DUMP_ONE_CFG_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "dom");
490 CHECK_ONE(firm_opt.check_all, irg);
492 construct_backedges(irg);
494 timer_push(TV_CF_OPT);
497 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cfopt");
498 CHECK_ONE(firm_opt.check_all, irg);
500 if (firm_opt.if_conversion) {
501 timer_push(TV_IF_CONV);
502 opt_if_conv(current_ir_graph, if_conv_info);
504 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "if");
505 CHECK_ONE(firm_opt.check_all, current_ir_graph);
507 timer_push(TV_LOCAL_OPT);
508 optimize_graph_df(current_ir_graph);
510 timer_push(TV_CF_OPT);
511 optimize_cf(current_ir_graph);
513 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "after_if");
514 CHECK_ONE(firm_opt.check_all, current_ir_graph);
517 if (firm_opt.bool_opt) {
519 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "bool");
520 CHECK_ONE(firm_opt.check_all, irg);
524 opt_osr(current_ir_graph, osr_flag_default /*| osr_flag_ignore_x86_shift*/);
526 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "stred");
527 CHECK_ONE(firm_opt.check_all, irg);
529 timer_push(TV_LOCAL_OPT);
530 optimize_graph_df(irg);
532 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "lopt");
533 CHECK_ONE(firm_opt.check_all, irg);
535 edges_deactivate(irg);
536 timer_push(TV_DEAD_NODE);
537 dead_node_elimination(irg);
539 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "dead");
540 CHECK_ONE(firm_opt.check_all, irg);
543 if (firm_opt.cloning) {
544 proc_cloning((float)firm_opt.clone_threshold);
545 DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "clone");
546 CHECK_ALL(firm_opt.check_all);
549 if (firm_dump.ir_graph) {
550 /* recompute backedges for nicer dumps */
551 for (i = 0; i < get_irp_n_irgs(); i++)
552 construct_cf_backedges(get_irp_irg(i));
555 if (firm_opt.remove_unused) {
556 ir_entity **keep_methods;
559 /* Analysis that finds the free methods,
560 i.e. methods that are dereferenced.
561 Optimizes polymorphic calls :-). */
562 cgana(&arr_len, &keep_methods);
564 /* Remove methods that are never called. */
565 gc_irgs(arr_len, keep_methods);
571 DUMP_ALL(firm_dump.ir_graph, "-opt");
573 /* verify optimized graphs */
574 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
575 irg_verify(get_irp_irg(i), VRFY_ENFORCE_SSA);
577 if (firm_dump.statistic & STAT_AFTER_OPT)
578 stat_dump_snapshot(input_filename, "opt");
580 timer_stop(TV_ALL_OPT);
581 } /* do_firm_optimizations */
584 * compute the size of a type (do implicit lowering)
586 * @param ty a Firm type
588 static int compute_type_size(ir_type *ty)
590 optimization_state_t state;
591 unsigned align_all = 1;
592 int n, size = 0, set = 0;
595 if (get_type_state(ty) == layout_fixed) {
596 /* do not layout already layouted types again */
600 if (is_Method_type(ty) || ty == get_glob_type()) {
601 /* no need for size calculation for method types or the global type */
605 DBG(("compute type size visiting: %s\n", get_type_name(ty)));
607 switch (get_type_tpop_code(ty)) {
610 for (i = 0, n = get_compound_n_members(ty); i < n; ++i) {
611 ir_entity *ent = get_compound_member(ty, i);
612 ir_type *ent_ty = get_entity_type(ent);
613 unsigned align, misalign;
615 /* compute member types */
616 if (! compute_type_size(ent_ty))
619 align = get_type_alignment_bytes(ent_ty);
620 align_all = align > align_all ? align : align_all;
621 misalign = (align ? size % align : 0);
622 size += (misalign ? align - misalign : 0);
624 set_entity_offset(ent, size);
625 size += get_type_size_bytes(ent_ty);
627 DBG((" member %s %s -> (size: %u, align: %u)\n",
628 get_type_name(ent_ty), get_entity_name(ent),
629 get_type_size_bytes(ent_ty), get_type_alignment_bytes(ent_ty)));
631 if (align_all > 0 && size % align_all) {
632 DBG(("align of the struct member: %u, type size: %d\n", align_all, size));
633 size += align_all - (size % align_all);
634 DBG(("correcting type-size to %d\n", size));
636 set_type_alignment_bytes(ty, align_all);
641 for (i = 0, n = get_union_n_members(ty); i < n; ++i) {
642 ir_entity *ent = get_union_member(ty, i);
644 if (! compute_type_size(get_entity_type(ent)))
646 s = get_type_size_bytes(get_entity_type(ent));
648 set_entity_offset(ent, 0);
649 size = (s > size ? s : size);
655 dims = get_array_n_dimensions(ty);
657 if (! compute_type_size(get_array_element_type(ty)))
662 save_optimization_state(&state);
664 set_opt_constant_folding(1);
666 for (i = 0; i < dims; ++i) {
667 ir_node *lower = get_array_lower_bound(ty, i);
668 ir_node *upper = get_array_upper_bound(ty, i);
669 ir_graph *rem = current_ir_graph;
670 tarval *tv_lower, *tv_upper;
671 long val_lower, val_upper;
673 current_ir_graph = get_const_code_irg();
674 local_optimize_node(lower);
675 local_optimize_node(upper);
676 current_ir_graph = rem;
678 tv_lower = computed_value(lower);
679 tv_upper = computed_value(upper);
681 if (tv_lower == tarval_bad || tv_upper == tarval_bad) {
683 * we cannot calculate the size of this array yet, it
684 * even might be unknown until the end, like argv[]
686 restore_optimization_state(&state);
690 val_upper = get_tarval_long(tv_upper);
691 val_lower = get_tarval_long(tv_lower);
692 size *= val_upper - val_lower;
694 restore_optimization_state(&state);
696 DBG(("array %s -> (elements: %d, element type size: %d)\n",
698 size, get_type_size_bytes(get_array_element_type(ty))));
699 size *= get_type_size_bytes(get_array_element_type(ty));
708 set_type_size_bytes(ty, size);
709 set_type_state(ty, layout_fixed);
712 DBG(("size: %d\n", get_type_size_bytes(ty)));
715 } /* compute_type_size */
718 * layout all types of the Firm graph
720 static void compute_type_sizes(void)
726 /* all frame types */
727 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
728 irg = get_irp_irg(i);
729 /* do not optimize away variables in debug mode */
730 if (firm_opt.debug_mode == DBG_MODE_NONE)
732 compute_type_size(get_irg_frame_type(irg));
735 /* all other types */
736 for (i = get_irp_n_types() - 1; i >= 0; --i) {
737 tp = get_irp_type(i);
738 compute_type_size(tp);
740 if (is_Method_type(tp)) {
741 tp = get_method_value_res_type(tp);
744 /* we have a value result type for this method, lower */
745 compute_type_size(tp);
749 } /* compute_type_sizes */
754 * @param input_filename the name of the (main) source file
756 static void do_firm_lowering(const char *input_filename)
760 /* do class lowering and vtbl creation */
761 // lower_classes_to_struct("vtbl", "m");
764 timer_push(TV_LOWER);
769 if (firm_opt.lower_ll) {
770 lwrdw_param_t init = {
775 def_create_intrinsic_fkt,
780 if (arch_create_intrinsic) {
781 init.create_intrinsic = arch_create_intrinsic;
782 init.ctx = create_intrinsic_ctx;
784 timer_push(TV_DW_LOWER);
786 DUMP_ALL(firm_dump.ir_graph, "-dw");
790 if (firm_dump.statistic & STAT_AFTER_LOWER)
791 stat_dump_snapshot(input_filename, "low");
793 /* verify lowered graphs */
794 timer_push(TV_VERIFY);
795 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
796 irg_verify(get_irp_irg(i), VRFY_ENFORCE_SSA);
799 DUMP_ALL(firm_dump.ir_graph, "-low");
801 if (firm_opt.enabled) {
802 timer_start(TV_ALL_OPT);
804 /* run reassociation first on all graphs BEFORE the architecture dependent optimizations
806 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
807 current_ir_graph = get_irp_irg(i);
809 timer_push(TV_REASSOCIATION);
810 optimize_reassociation(current_ir_graph);
812 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "reassoc");
813 CHECK_ONE(firm_opt.check_all, current_ir_graph);
816 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
817 current_ir_graph = get_irp_irg(i);
819 if (firm_opt.code_place)
820 set_opt_global_cse(1);
822 timer_push(TV_LOCAL_OPT);
823 optimize_graph_df(current_ir_graph);
825 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "lopt");
826 if (! firm_opt.code_place)
827 CHECK_ONE(firm_opt.check_all, current_ir_graph);
829 if (firm_opt.code_place) {
830 timer_push(TV_CODE_PLACE);
831 place_code(current_ir_graph);
832 set_opt_global_cse(0);
834 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "place");
835 CHECK_ONE(firm_opt.check_all, current_ir_graph);
838 // set_opt_global_cse(0);
839 timer_push(TV_LOAD_STORE);
840 optimize_load_store(current_ir_graph);
842 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "ldst");
843 CHECK_ONE(firm_opt.check_all, current_ir_graph);
846 timer_push(TV_LOCAL_OPT);
847 optimize_graph_df(current_ir_graph);
849 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "lopt");
851 timer_push(TV_CF_OPT);
852 optimize_cf(current_ir_graph);
854 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "cf");
855 CHECK_ONE(firm_opt.check_all, current_ir_graph);
857 if (firm_opt.if_conversion) {
858 timer_push(TV_IF_CONV);
859 opt_if_conv(current_ir_graph, if_conv_info);
861 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "if");
862 CHECK_ONE(firm_opt.check_all, current_ir_graph);
864 timer_push(TV_LOCAL_OPT);
865 optimize_graph_df(current_ir_graph);
867 timer_push(TV_CF_OPT);
868 optimize_cf(current_ir_graph);
870 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "after_if");
871 CHECK_ONE(firm_opt.check_all, current_ir_graph);
874 timer_stop(TV_ALL_OPT);
876 DUMP_ALL(firm_dump.ir_graph, "-low-opt");
880 mark_private_methods();
882 /* set the phase to low */
883 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
884 set_irg_phase_low(get_irp_irg(i));
886 /* all graphs are lowered, set the irp phase to low */
887 set_irp_phase_state(phase_low);
889 if (firm_dump.statistic & STAT_FINAL) {
890 stat_dump_snapshot(input_filename, "final");
892 } /* do_firm_lowering */
895 * Initialize for the Firm-generating back end.
897 void gen_firm_init(void)
899 firm_parameter_t params;
901 unsigned pattern = 0;
903 /* the automatic state is only set if inlining is enabled */
904 firm_opt.auto_inline = firm_opt.do_inline;
906 if (firm_dump.stat_pattern)
907 pattern |= FIRMSTAT_PATTERN_ENABLED;
909 if (firm_dump.stat_dag)
910 pattern |= FIRMSTAT_COUNT_DAG;
912 memset(¶ms, 0, sizeof(params));
913 params.size = sizeof(params);
914 params.enable_statistics = firm_dump.statistic == STAT_NONE ? 0 :
915 FIRMSTAT_ENABLED | FIRMSTAT_COUNT_STRONG_OP | FIRMSTAT_COUNT_CONSTS | pattern;
916 params.initialize_local_func = uninitialized_local_var;
917 params.cc_mask = 0; /* no regparam, cdecl */
918 params.builtin_dbg = NULL;
921 if (firm_be_opt.selection == BE_FIRM_BE) {
922 const backend_params *be_params = be_init();
924 firm_opt.lower_ll = (a_byte) be_params->do_dw_lowering;
925 params.arch_op_settings = be_params->arch_op_settings;
927 arch_create_intrinsic = be_params->arch_create_intrinsic_fkt;
928 create_intrinsic_ctx = be_params->create_intrinsic_ctx;
930 ad_param = be_params->dep_param;
931 if_conv_info = be_params->if_conv_info;
933 #endif /* FIRM_BACKEND */
936 /* Activate Graph rewriting if SIMD optimization is turned on */
937 /* This has to be done before init_firm() is called! */
938 if (firm_ext_grs.simd_opt)
943 dbg_init(NULL, NULL, dbg_snprint);
944 edges_init_dbg(firm_opt.vrfy_edges);
945 //cbackend_set_debug_retrieve(dbg_retrieve);
947 set_opt_precise_exc_context(firm_opt.precise_exc);
948 set_opt_fragile_ops(firm_opt.fragile_ops);
950 /* dynamic dispatch works currently only if whole world scenarios */
951 set_opt_dyn_meth_dispatch(0);
953 arch_dep_init(arch_factory);
955 /* do not run architecture dependent optimizations in building phase */
956 arch_dep_set_opts(arch_dep_none);
958 do_node_verification((firm_verification_t) firm_opt.vrfy);
959 if (firm_dump.filter)
960 only_dump_method_with_name(new_id_from_str(firm_dump.filter));
962 if (firm_opt.enabled) {
964 set_opt_constant_folding(firm_opt.const_folding);
965 set_opt_cse(firm_opt.cse);
966 set_opt_global_cse (0);
967 set_opt_unreachable_code(1);
968 set_opt_control_flow(firm_opt.control_flow);
969 set_opt_control_flow_weak_simplification(1);
970 set_opt_control_flow_strong_simplification(1);
975 dump_filter = getenv("FIRM_DUMP_FILTER");
977 only_dump_method_with_name(new_id_from_str(dump_filter));
979 /* do not dump entity ld names */
983 /* init the ycomp debugger extension */
984 if (firm_opt.ycomp_dbg)
985 firm_init_ycomp_debugger(firm_opt.ycomp_host, firm_opt.ycomp_port);
987 } /* gen_firm_init */
990 * Called, after the Firm generation is completed,
991 * do all optimizations and backend call here.
993 * @param out a file handle for the output, may be NULL
994 * @param input_filename the name of the (main) source file
995 * @param c_mode non-zero if "C" was compiled
996 * @param firm_const_exists non-zero, if the const attribute was used on functions
998 void gen_firm_finish(FILE *out, const char *input_filename, int c_mode, int firm_const_exists)
1002 /* the general for dumping option must be set, or the others will not work */
1004 = (a_byte) (firm_dump.ir_graph | firm_dump.all_phases | firm_dump.extbb);
1006 dump_keepalive_edges(1);
1007 dump_consts_local(1);
1008 dump_dominator_information(1);
1009 dump_loop_information(0);
1011 if (!firm_dump.edge_labels)
1012 turn_off_edge_labels();
1014 if (firm_dump.all_types) {
1017 dump_class_hierarchy(0, "");
1018 dump_class_hierarchy(1, "-with-entities");
1022 /* finalize all graphs */
1023 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1024 ir_graph *irg = get_irp_irg(i);
1026 irg_finalize_cons(irg);
1027 DUMP_ONE(firm_dump.ir_graph, irg, "");
1029 /* verify the graph */
1030 timer_push(TV_VERIFY);
1031 irg_verify(irg, VRFY_ENFORCE_SSA);
1035 timer_push(TV_VERIFY);
1039 /* all graphs are finalized, set the irp phase to high */
1040 set_irp_phase_state(phase_high);
1042 /* lower all compound call return values */
1043 lower_compound_params();
1045 /* computes the sizes of all types that are still not computed */
1046 compute_type_sizes();
1048 if (firm_dump.statistic & STAT_BEFORE_OPT) {
1049 stat_dump_snapshot(input_filename, "noopt");
1052 if (firm_opt.enabled)
1053 do_firm_optimizations(input_filename, firm_const_exists);
1055 if (firm_dump.gen_firm_asm) {
1056 timer_push(TV_FIRM_ASM);
1057 gen_Firm_assembler(input_filename);
1063 do_firm_lowering(input_filename);
1065 /* set the phase to low */
1066 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
1067 set_irg_phase_low(get_irp_irg(i));
1071 /** SIMD Optimization Extensions **/
1073 /* Pattern creation step. No code has to be generated, so
1074 exit after pattern creation */
1075 if (firm_ext_grs.create_pattern) {
1076 ext_grs_create_pattern();
1080 /* SIMD optimization step. Uses graph patterns to find
1081 rich instructions and rewrite */
1082 if (firm_ext_grs.simd_opt)
1086 /* enable architecture dependent optimizations */
1087 arch_dep_set_opts((arch_dep_opts_t)
1088 ((firm_opt.muls ? arch_dep_mul_to_shift : arch_dep_none) |
1089 (firm_opt.divs ? arch_dep_div_by_const : arch_dep_none) |
1090 (firm_opt.mods ? arch_dep_mod_by_const : arch_dep_none) ));
1093 if (firm_dump.statistic & STAT_FINAL_IR)
1094 stat_dump_snapshot(input_filename, "final-ir");
1096 /* run the code generator */
1097 if (firm_be_opt.selection != BE_NONE)
1098 do_codegen(out, input_filename);
1100 if (firm_dump.statistic & STAT_FINAL)
1101 stat_dump_snapshot(input_filename, "final");
1104 if (firm_opt.ycomp_dbg)
1105 firm_finish_ycomp_debugger();
1107 } /* gen_firm_finish */
1110 * Do very early initializations
1112 void firm_early_init(void) {
1114 /* arg: need this here for command line options */
1117 firm_init_options(NULL, 0, NULL);
1118 } /* firm_early_init */