removed broken include
[libfirm] / ir / be / bemain.c
index 6855617..c8eaed6 100644 (file)
 #include "bitset.h"
 
 #include "irprog.h"
+#include "irgopt.h"
 #include "irgraph.h"
 #include "irdump.h"
+#include "phiclass.h"
+#include "irdom_t.h"
+#include "iredges_t.h"
+#include "irloop_t.h"
 
 #include "be_t.h"
-#include "bera_t.h"
+#include "bechordal_t.h"
 #include "benumb_t.h"
 #include "besched_t.h"
 #include "belistsched.h"
 #include "belive_t.h"
 #include "beutil.h"
 #include "bechordal.h"
-#include "bechordal.h"
-#include "bephiopt.h"
-#include "phistat.h"
+#include "bearch.h"
+#include "becopyoptmain.h"
+#include "becopystat.h"
+#include "bessadestr.h"
+#include "benode_t.h"
+#include "beirgmod.h"
+#include "bespillilp.h"
+#include "bespillbelady.h"
+
+// #include "beasm_dump_globals.h"
+// #include "beasm_asm_gnu.h"
 
-#define DUMP_ALLOCATED
-#undef DUMP_LOCALIZED
+// #include "bearch_firm.h"
+
+#undef DUMP_BEGIN
+#define DUMP_PREPARED
+#define DUMP_SCHED
+#define DUMP_SPILL
+#undef DUMP_ALLOCATED
+#undef DUMP_COPYMIN
+#undef DUMP_SSADESTR
+#undef DUMP_END
 
 #define N_PHASES 256
 
-typedef struct _be_graph_info_t {
-       bitset_t *applied_phases;
-} be_graph_info_t;
+extern const arch_isa_if_t firm_isa;
+extern const arch_isa_if_t ia32_isa;
+
+const struct {
+  const char *name;
+  const arch_isa_if_t *isa;
+} isa_name_arch_map[] = {
+  { "firm", &firm_isa },
+  { "ia32", &ia32_isa }
+};
 
-static size_t be_info_offset = 0;
+#define N_ISAS (sizeof(isa_name_arch_map) / sizeof(isa_name_arch_map[0]))
 
-#define get_irg_be_info(irg) get_irg_data(irg, be_graph_info_t, be_info_offset)
+const arch_isa_if_t *get_backend_isa_if(const char *name) {
+  int i;
 
-static int phase_ids = 1;
-static struct obstack obst;
+  for (i = 0; i < N_ISAS; i++) {
+    if (!strcmp(isa_name_arch_map[i].name, name))
+      return isa_name_arch_map[i].isa;
+  }
 
-int phase_register(phase_t *phase)
+  fprintf(stderr, "Unsupported ISA '%s'\n", name);
+  assert(0);
+  return NULL;
+}
+
+void be_init(void)
 {
-       phase->id = phase_ids;
-       return phase_ids++;
+  be_sched_init();
+  be_liveness_init();
+  be_numbering_init();
+  be_ra_chordal_init();
+  be_copy_opt_init();
+  copystat_init();
+  phi_class_init();
 }
 
-void phase_applied(const ir_graph *irg, const phase_t *phase)
+static be_main_env_t *be_init_env(be_main_env_t *env, const arch_isa_if_t *isa)
 {
-       be_graph_info_t *info = get_irg_be_info(irg);
+  obstack_init(&env->obst);
+  env->dbg = firm_dbg_register("be.main");
+  firm_dbg_set_mask(env->dbg, SET_LEVEL_1);
 
-       if(!info->applied_phases)
-               info->applied_phases = bitset_obstack_alloc(&obst, N_PHASES);
+  env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
+  arch_env_init(env->arch_env, isa);
+  env->arch_env->isa->init();
 
-       bitset_set(info->applied_phases, phase->id);
+  env->node_factory = obstack_alloc(&env->obst, sizeof(*env->node_factory));
+  be_node_factory_init(env->node_factory, isa);
+
+  /* Register the irn handler of the architecture */
+  if (isa->irn_handler)
+    arch_env_add_irn_handler(env->arch_env, isa->irn_handler);
+
+  /*
+   * Register the node handler of the back end infrastructure.
+   * This irn handler takes care of the platform independent
+   * spill, reload and perm nodes.
+   */
+  arch_env_add_irn_handler(env->arch_env, be_node_get_irn_handler(env->node_factory));
+
+  return env;
 }
 
-int phase_depends_on(const ir_graph *irg, const phase_t *phase, int n, ...)
+static be_main_session_env_t *
+be_init_session_env(be_main_session_env_t *env,
+    be_main_env_t *main_env, ir_graph *irg)
 {
-       int errors = 0;
-       int i;
-       va_list args;
+  env->main_env = main_env;
+  env->irg = irg;
 
-       if(n > 0) {
-               const be_graph_info_t *info = get_irg_be_info(irg);
-               const bitset_t *applied_phases = info->applied_phases;
+  return env;
+}
 
-               va_start(args, n);
+static void prepare_graph(be_main_session_env_t *s)
+{
+  /* set the current graph (this is important for several firm functions) */
+  current_ir_graph = s->irg;
 
-               for(i = 0; i < n; ++i) {
-                       const phase_t *dep_phase = va_arg(args, const phase_t *);
+  /* Let the isa prepare the graph. */
+  s->main_env->arch_env->isa->prepare_graph(s->irg);
 
-                       if(!applied_phases || !bitset_is_set(applied_phases, dep_phase->id)) {
-                               errors++;
-                               fprintf(stderr, "phase dependency unfulfilled: \"%s\" depends on \"%s\"\n",
-                                               phase->name, dep_phase->name);
-                       }
-               }
+  /* Normalize proj nodes. */
+  normalize_proj_nodes(s->irg);
 
-               va_end(args);
+  /* Remove critical edges */
+  remove_critical_cf_edges(s->irg);
 
-               assert(errors > 0 && "There were phase dependency errors");
-       }
+  /* Compute the dominance information. */
+  free_dom_and_peace(s->irg);
+  compute_doms(s->irg);
 
-       return errors;
-}
+  /* Compute the dominance frontiers */
+  s->dom_front = be_compute_dominance_frontiers(s->irg);
 
-void be_init(void)
-{
-       obstack_init(&obst);
-       be_info_offset = register_additional_graph_data(sizeof(be_graph_info_t));
-
-       be_sched_init();
-       be_liveness_init();
-       be_numbering_init();
-       be_ra_init();
-       be_ra_chordal_init();
-       be_phi_opt_init();
-}
+  /* Ensure, that the ir_edges are computed. */
+  edges_activate(s->irg);
 
-extern void be_ra_chordal(ir_graph *irg);
+  /* Compute loop nesting information (for weighting copies) */
+  if (get_irg_loopinfo_state(s->irg) != (loopinfo_valid & loopinfo_cf_consistent))
+    construct_cf_backedges(s->irg);
 
-static void be_main_loop(void)
+  be_check_dominance(s->irg);
+}
+
+static void be_main_loop(const arch_isa_if_t *isa)
 {
-       int i, n;
+       int i, j, n, m;
+       be_main_env_t env;
+
+       be_init_env(&env, isa);
 
+       /* For all graphs */
        for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
                ir_graph *irg = get_irp_irg(i);
+               be_main_session_env_t session;
 
-               localize_consts(irg);
-#ifdef DUMP_LOCALIZED
-               dump_consts_local(0);
-               dump_ir_block_graph(irg, "-local-const");
+               DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
+#ifdef DUMP_BEGIN
+               dump_ir_block_graph(irg, "-begin");
 #endif
-               be_numbering(irg);
-               list_sched(irg, trivial_selector, NULL);
+
+               /* Init the session. */
+               be_init_session_env(&session, &env, irg);
+
+               /* Compute some analyses and prepare the graph for backend use. */
+               prepare_graph(&session);
+
+#ifdef DUMP_PREPARED
+               dump_dominator_information(1);
+               dump_ir_block_graph(irg, "-prepared");
+               dump_dominator_information(0);
+#endif
+
+               /* Schedule the graphs. */
+               list_sched(irg, trivial_selector);
+
+// TODO: add architecture dependent schedule magic
+//             be_sched_imm(irg);
+
+#ifdef DUMP_SCHED
+               dump_ir_block_graph_sched(irg, "-sched");
+#endif
+
+               /* Verify the schedule */
+               sched_verify_irg(irg);
+
+               /* Build liveness information */
                be_liveness(irg);
-               be_ra_chordal(irg);
 
+               /* Perform the following for each register class. */
+               for(j = 0, m = isa->get_n_reg_class(); j < m; ++j) {
+                       be_chordal_env_t *chordal_env;
+                       const arch_register_class_t *cls = isa->get_reg_class(j);
+                       DBG((env.dbg, LEVEL_1, "----> Reg class: %s\n", cls->name));
+
+                       /* spilling */
+                       //be_spill_ilp(&session, cls);
+                       be_spill_belady(&session, cls);
+#ifdef DUMP_SPILL
+                       dump_ir_block_graph_sched(session.irg, "-spill");
+#endif
+                       be_liveness(irg);
+                       be_numbering(irg);
+                       be_check_pressure(&session, cls);
+
+#if 0
+                       {
+                               FILE *f;
+                               char buf[128];
+                               ir_snprintf(buf, sizeof(buf), "%F_%s-live.txt", irg, cls->name);
+                               if((f = fopen(buf, "wt")) != NULL) {
+                                       be_liveness_dump(session.irg, f);
+                                       fclose(f);
+                               }
+                       }
+#endif
+
+                       /* allocation */
+                       chordal_env = be_ra_chordal(&session, cls);
 #ifdef DUMP_ALLOCATED
-               dump_allocated_irg(irg, "");
+                       dump_allocated_irg(env.arch_env, irg, "");
+#endif
+
+                       /* copy minimization */
+                       copystat_collect_cls(chordal_env);
+                       be_copy_opt(chordal_env);
+#ifdef DUMP_COPYMIN
+                       dump_allocated_irg(env.arch_env, irg, "-copymin");
 #endif
-               //be_phi_opt(irg);
 
-               be_ra_chordal_done(irg);
-               be_numbering_done(irg);
+                       /* ssa destruction */
+                       be_ssa_destruction(chordal_env);
+                       be_ssa_destruction_check(chordal_env);
+                       be_ra_chordal_check(chordal_env);
+#ifdef DUMP_SSADESTR
+                       dump_allocated_irg(env.arch_env, irg, "-ssadestr");
+#endif
+
+                       be_ra_chordal_done(chordal_env);
+                       be_numbering_done(irg);
+               }
+#ifdef DUMP_END
+               dump_ir_block_graph_sched(session.irg, "-end");
+#endif
+               copystat_dump(irg);
        }
 }
 
+/**
+ * Backend main driver. Expects the following arguments:
+ * argc = 2
+ * argv[0] = backend isa name
+ * argv[1] = output file name
+ */
 void be_main(int argc, const char *argv[])
 {
-       be_main_loop();
+  FILE *asm_output_file;
+  const arch_isa_if_t *isa;
+
+  /* get requested isa */
+  isa = get_backend_isa_if(argv[0]);
+
+  be_main_loop(isa);
+
+  /* generate code if supported by isa */
+  if (isa->codegen) {
+    asm_output_file = fopen(argv[1], "w");
+    isa->codegen(asm_output_file);
+    fclose(asm_output_file);
+  }
 }