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);
357 for (i = 0; i < get_irp_n_irgs(); i++) {
358 irg = current_ir_graph = get_irp_irg(i);
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);
369 if (firm_opt.modes) {
370 /* convert all modes into integer if possible */
371 arch_mode_conversion(irg, predefs.mode_uint);
374 timer_push(TV_SCALAR_REPLACE);
375 scalar_replacement_opt(irg);
377 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "scalar");
378 CHECK_ONE(firm_opt.check_all, irg);
380 timer_push(TV_LOCAL_OPT);
381 optimize_graph_df(irg);
383 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "lopt");
384 CHECK_ONE(firm_opt.check_all, irg);
386 timer_push(TV_REASSOCIATION);
387 optimize_reassociation(irg);
389 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "reassoc");
390 CHECK_ONE(firm_opt.check_all, irg);
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);
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);
404 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "confirms");
405 CHECK_ONE(firm_opt.check_all, irg);
408 timer_push(TV_LOCAL_OPT);
409 optimize_graph_df(irg);
411 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "lopt");
412 CHECK_ONE(firm_opt.check_all, irg);
415 CHECK_ONE(firm_opt.check_all, irg);
417 if (firm_opt.code_place) {
418 timer_push(TV_CODE_PLACE);
419 set_opt_global_cse(1);
420 optimize_graph_df(irg);
422 set_opt_global_cse(0);
424 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "place");
425 CHECK_ONE(firm_opt.check_all, irg);
428 if (firm_opt.luffig) {
430 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "ldst2");
431 CHECK_ONE(firm_opt.check_all, irg);
434 timer_push(TV_CF_OPT);
437 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cfopt");
438 CHECK_ONE(firm_opt.check_all, irg);
440 /* should we really remove the Confirm here? */
441 if (firm_opt.confirm) {
442 timer_push(TV_CONFIRM_CREATE);
443 remove_confirms(irg);
447 irg_verify(irg, VRFY_ENFORCE_SSA);
448 if (firm_opt.gvn_pre) {
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);
455 if (firm_opt.loop_unrolling) {
456 timer_push(TV_LOOP_UNROLL);
457 optimize_loop_unrolling(irg);
459 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "loop");
460 CHECK_ONE(firm_opt.check_all, irg);
463 if (firm_opt.load_store) {
464 timer_push(TV_LOAD_STORE);
465 optimize_load_store(irg);
467 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "ldst");
468 CHECK_ONE(firm_opt.check_all, irg);
471 lower_highlevel_graph(irg, firm_opt.lower_bitfields);
473 if (firm_opt.deconv) {
474 timer_push(TV_DECONV);
477 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "deconv");
478 CHECK_ONE(firm_opt.check_all, irg);
481 if (firm_opt.cond_eval) {
482 timer_push(TV_COND_EVAL);
485 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cond_eval");
486 CHECK_ONE(firm_opt.check_all, 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);
494 construct_backedges(irg);
496 timer_push(TV_CF_OPT);
499 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cfopt");
500 CHECK_ONE(firm_opt.check_all, irg);
502 if (firm_opt.if_conversion) {
503 timer_push(TV_IF_CONV);
504 opt_if_conv(current_ir_graph, if_conv_info);
506 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "if");
507 CHECK_ONE(firm_opt.check_all, current_ir_graph);
509 timer_push(TV_LOCAL_OPT);
510 optimize_graph_df(current_ir_graph);
512 timer_push(TV_CF_OPT);
513 optimize_cf(current_ir_graph);
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);
519 if (firm_opt.bool_opt) {
521 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "bool");
522 CHECK_ONE(firm_opt.check_all, irg);
526 opt_osr(current_ir_graph, osr_flag_default /*| osr_flag_ignore_x86_shift*/);
528 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "stred");
529 CHECK_ONE(firm_opt.check_all, irg);
531 timer_push(TV_LOCAL_OPT);
532 optimize_graph_df(irg);
534 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "lopt");
535 CHECK_ONE(firm_opt.check_all, irg);
537 edges_deactivate(irg);
538 timer_push(TV_DEAD_NODE);
539 dead_node_elimination(irg);
541 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "dead");
542 CHECK_ONE(firm_opt.check_all, irg);
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);
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));
557 if (firm_opt.remove_unused) {
558 ir_entity **keep_methods;
561 /* Analysis that finds the free methods,
562 i.e. methods that are dereferenced.
563 Optimizes polymorphic calls :-). */
564 cgana(&arr_len, &keep_methods);
566 /* Remove methods that are never called. */
567 gc_irgs(arr_len, keep_methods);
573 DUMP_ALL(firm_dump.ir_graph, "-opt");
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);
579 if (firm_dump.statistic & STAT_AFTER_OPT)
580 stat_dump_snapshot(input_filename, "opt");
582 timer_stop(TV_ALL_OPT);
583 } /* do_firm_optimizations */
586 * compute the size of a type (do implicit lowering)
588 * @param ty a Firm type
590 static int compute_type_size(ir_type *ty)
592 optimization_state_t state;
593 unsigned align_all = 1;
594 int n, size = 0, set = 0;
597 if (get_type_state(ty) == layout_fixed) {
598 /* do not layout already layouted types again */
602 if (is_Method_type(ty) || ty == get_glob_type()) {
603 /* no need for size calculation for method types or the global type */
607 DBG(("compute type size visiting: %s\n", get_type_name(ty)));
609 switch (get_type_tpop_code(ty)) {
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;
617 /* compute member types */
618 if (! compute_type_size(ent_ty))
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);
626 set_entity_offset(ent, size);
627 size += get_type_size_bytes(ent_ty);
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)));
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));
638 set_type_alignment_bytes(ty, align_all);
643 for (i = 0, n = get_union_n_members(ty); i < n; ++i) {
644 ir_entity *ent = get_union_member(ty, i);
646 if (! compute_type_size(get_entity_type(ent)))
648 s = get_type_size_bytes(get_entity_type(ent));
650 set_entity_offset(ent, 0);
651 size = (s > size ? s : size);
657 dims = get_array_n_dimensions(ty);
659 if (! compute_type_size(get_array_element_type(ty)))
664 save_optimization_state(&state);
666 set_opt_constant_folding(1);
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;
675 current_ir_graph = get_const_code_irg();
676 local_optimize_node(lower);
677 local_optimize_node(upper);
678 current_ir_graph = rem;
680 tv_lower = computed_value(lower);
681 tv_upper = computed_value(upper);
683 if (tv_lower == tarval_bad || tv_upper == tarval_bad) {
685 * we cannot calculate the size of this array yet, it
686 * even might be unknown until the end, like argv[]
688 restore_optimization_state(&state);
692 val_upper = get_tarval_long(tv_upper);
693 val_lower = get_tarval_long(tv_lower);
694 size *= val_upper - val_lower;
696 restore_optimization_state(&state);
698 DBG(("array %s -> (elements: %d, element type size: %d)\n",
700 size, get_type_size_bytes(get_array_element_type(ty))));
701 size *= get_type_size_bytes(get_array_element_type(ty));
710 set_type_size_bytes(ty, size);
711 set_type_state(ty, layout_fixed);
714 DBG(("size: %d\n", get_type_size_bytes(ty)));
717 } /* compute_type_size */
720 * layout all types of the Firm graph
722 static void compute_type_sizes(void)
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)
734 compute_type_size(get_irg_frame_type(irg));
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);
742 if (is_Method_type(tp)) {
743 tp = get_method_value_res_type(tp);
746 /* we have a value result type for this method, lower */
747 compute_type_size(tp);
751 } /* compute_type_sizes */
756 * @param input_filename the name of the (main) source file
758 static void do_firm_lowering(const char *input_filename)
762 /* do class lowering and vtbl creation */
763 // lower_classes_to_struct("vtbl", "m");
766 timer_push(TV_LOWER);
771 if (firm_opt.lower_ll) {
772 lwrdw_param_t init = {
777 def_create_intrinsic_fkt,
782 if (arch_create_intrinsic) {
783 init.create_intrinsic = arch_create_intrinsic;
784 init.ctx = create_intrinsic_ctx;
786 timer_push(TV_DW_LOWER);
788 DUMP_ALL(firm_dump.ir_graph, "-dw");
792 if (firm_dump.statistic & STAT_AFTER_LOWER)
793 stat_dump_snapshot(input_filename, "low");
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);
801 DUMP_ALL(firm_dump.ir_graph, "-low");
803 if (firm_opt.enabled) {
804 timer_start(TV_ALL_OPT);
806 /* run reassociation first on all graphs BEFORE the architecture dependent optimizations
808 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
809 current_ir_graph = get_irp_irg(i);
811 timer_push(TV_REASSOCIATION);
812 optimize_reassociation(current_ir_graph);
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);
818 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
819 current_ir_graph = get_irp_irg(i);
821 if (firm_opt.code_place)
822 set_opt_global_cse(1);
824 timer_push(TV_LOCAL_OPT);
825 optimize_graph_df(current_ir_graph);
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);
831 if (firm_opt.code_place) {
832 timer_push(TV_CODE_PLACE);
833 place_code(current_ir_graph);
834 set_opt_global_cse(0);
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);
840 // set_opt_global_cse(0);
841 timer_push(TV_LOAD_STORE);
842 optimize_load_store(current_ir_graph);
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);
848 timer_push(TV_LOCAL_OPT);
849 optimize_graph_df(current_ir_graph);
851 DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, current_ir_graph, "lopt");
853 timer_push(TV_CF_OPT);
854 optimize_cf(current_ir_graph);
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);
859 if (firm_opt.if_conversion) {
860 timer_push(TV_IF_CONV);
861 opt_if_conv(current_ir_graph, if_conv_info);
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);
866 timer_push(TV_LOCAL_OPT);
867 optimize_graph_df(current_ir_graph);
869 timer_push(TV_CF_OPT);
870 optimize_cf(current_ir_graph);
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);
876 timer_stop(TV_ALL_OPT);
878 DUMP_ALL(firm_dump.ir_graph, "-low-opt");
882 mark_private_methods();
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));
888 /* all graphs are lowered, set the irp phase to low */
889 set_irp_phase_state(phase_low);
891 if (firm_dump.statistic & STAT_FINAL) {
892 stat_dump_snapshot(input_filename, "final");
894 } /* do_firm_lowering */
897 * Initialize for the Firm-generating back end.
899 void gen_firm_init(void)
901 firm_parameter_t params;
903 unsigned pattern = 0;
905 /* the automatic state is only set if inlining is enabled */
906 firm_opt.auto_inline = firm_opt.do_inline;
908 if (firm_dump.stat_pattern)
909 pattern |= FIRMSTAT_PATTERN_ENABLED;
911 if (firm_dump.stat_dag)
912 pattern |= FIRMSTAT_COUNT_DAG;
914 memset(¶ms, 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;
923 if (firm_be_opt.selection == BE_FIRM_BE) {
924 const backend_params *be_params = be_init();
926 firm_opt.lower_ll = (a_byte) be_params->do_dw_lowering;
927 params.arch_op_settings = be_params->arch_op_settings;
929 arch_create_intrinsic = be_params->arch_create_intrinsic_fkt;
930 create_intrinsic_ctx = be_params->create_intrinsic_ctx;
932 ad_param = be_params->dep_param;
933 if_conv_info = be_params->if_conv_info;
935 #endif /* FIRM_BACKEND */
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)
945 dbg_init(NULL, NULL, dbg_snprint);
946 edges_init_dbg(firm_opt.vrfy_edges);
947 //cbackend_set_debug_retrieve(dbg_retrieve);
949 set_opt_precise_exc_context(firm_opt.precise_exc);
950 set_opt_fragile_ops(firm_opt.fragile_ops);
952 /* dynamic dispatch works currently only if whole world scenarios */
953 set_opt_dyn_meth_dispatch(0);
955 arch_dep_init(arch_factory);
957 /* do not run architecture dependent optimizations in building phase */
958 arch_dep_set_opts(arch_dep_none);
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));
964 if (firm_opt.enabled) {
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);
977 dump_filter = getenv("FIRM_DUMP_FILTER");
979 only_dump_method_with_name(new_id_from_str(dump_filter));
981 /* do not dump entity ld names */
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);
989 } /* gen_firm_init */
992 * Called, after the Firm generation is completed,
993 * do all optimizations and backend call here.
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
1000 void gen_firm_finish(FILE *out, const char *input_filename, int c_mode, int firm_const_exists)
1004 /* the general for dumping option must be set, or the others will not work */
1006 = (a_byte) (firm_dump.ir_graph | firm_dump.all_phases | firm_dump.extbb);
1008 dump_keepalive_edges(1);
1009 dump_consts_local(1);
1010 dump_dominator_information(1);
1011 dump_loop_information(0);
1013 if (!firm_dump.edge_labels)
1014 turn_off_edge_labels();
1016 if (firm_dump.all_types) {
1019 dump_class_hierarchy(0, "");
1020 dump_class_hierarchy(1, "-with-entities");
1024 /* finalize all graphs */
1025 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1026 ir_graph *irg = get_irp_irg(i);
1028 irg_finalize_cons(irg);
1029 DUMP_ONE(firm_dump.ir_graph, irg, "");
1031 /* verify the graph */
1032 timer_push(TV_VERIFY);
1033 irg_verify(irg, VRFY_ENFORCE_SSA);
1037 timer_push(TV_VERIFY);
1041 /* all graphs are finalized, set the irp phase to high */
1042 set_irp_phase_state(phase_high);
1044 /* lower all compound call return values */
1045 lower_compound_params();
1047 /* computes the sizes of all types that are still not computed */
1048 compute_type_sizes();
1050 if (firm_dump.statistic & STAT_BEFORE_OPT) {
1051 stat_dump_snapshot(input_filename, "noopt");
1054 if (firm_opt.enabled)
1055 do_firm_optimizations(input_filename, firm_const_exists);
1057 if (firm_dump.gen_firm_asm) {
1058 timer_push(TV_FIRM_ASM);
1059 gen_Firm_assembler(input_filename);
1065 do_firm_lowering(input_filename);
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));
1073 /** SIMD Optimization Extensions **/
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();
1082 /* SIMD optimization step. Uses graph patterns to find
1083 rich instructions and rewrite */
1084 if (firm_ext_grs.simd_opt)
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) ));
1095 if (firm_dump.statistic & STAT_FINAL_IR)
1096 stat_dump_snapshot(input_filename, "final-ir");
1098 /* run the code generator */
1099 if (firm_be_opt.selection != BE_NONE)
1100 do_codegen(out, input_filename);
1102 if (firm_dump.statistic & STAT_FINAL)
1103 stat_dump_snapshot(input_filename, "final");
1106 if (firm_opt.ycomp_dbg)
1107 firm_finish_ycomp_debugger();
1109 } /* gen_firm_finish */
1112 * Do very early initializations
1114 void firm_early_init(void) {
1116 /* arg: need this here for command line options */
1119 firm_init_options(NULL, 0, NULL);
1120 } /* firm_early_init */