bemain.c 25.2 KB
Newer Older
1
2
3
/**
 * Backend driver.
 * @author Sebastian Hack
Christian Würdig's avatar
Christian Würdig committed
4
5
 * @date   25.11.2004
 * @cvsid  $Id$
6
 */
Michael Beck's avatar
Michael Beck committed
7
8
9
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
10

Sebastian Hack's avatar
Sebastian Hack committed
11
#include <stdarg.h>
12
#include <stdio.h>
13

Sebastian Hack's avatar
Sebastian Hack committed
14
15
16
#ifdef WITH_LIBCORE
#include <libcore/lc_opts.h>
#include <libcore/lc_opts_enum.h>
17
#include <libcore/lc_timing.h>
18
#endif /* WITH_LIBCORE */
Sebastian Hack's avatar
Sebastian Hack committed
19

Sebastian Hack's avatar
Sebastian Hack committed
20
21
22
23
#include "obst.h"
#include "bitset.h"

#include "irprog.h"
Sebastian Hack's avatar
Sebastian Hack committed
24
#include "irgopt.h"
Sebastian Hack's avatar
Sebastian Hack committed
25
#include "irgraph.h"
Daniel Grund's avatar
Daniel Grund committed
26
#include "irdump.h"
27
#include "phiclass.h"
Sebastian Hack's avatar
Sebastian Hack committed
28
29
#include "irdom_t.h"
#include "iredges_t.h"
30
#include "irloop_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
31
#include "irtools.h"
32
#include "irvrfy.h"
33
#include "irprintf.h"
Sebastian Hack's avatar
Sebastian Hack committed
34
#include "return.h"
Christian Würdig's avatar
Christian Würdig committed
35
#include "firmstat.h"
Matthias Braun's avatar
Matthias Braun committed
36
#include "cfopt.h"
37
#include "execfreq.h"
Sebastian Hack's avatar
Sebastian Hack committed
38

39
40
41
#include "bearch.h"
#include "firm/bearch_firm.h"
#include "ia32/bearch_ia32.h"
42
43
44
#include "arm/bearch_arm.h"
#include "ppc32/bearch_ppc32.h"
#include "mips/bearch_mips.h"
45
// #include "sta/bearch_sta.h"
46

Sebastian Hack's avatar
Sebastian Hack committed
47
#include "be_t.h"
48
49
50
#include "beutil.h"
#include "benode_t.h"
#include "beirgmod.h"
Sebastian Hack's avatar
Sebastian Hack committed
51
#include "besched_t.h"
52
53
#include "belistsched.h"
#include "belive_t.h"
54
55
56
57
58
59
#include "bespillbelady.h"
#include "bera.h"
#include "beraextern.h"
#include "bechordal_t.h"
#include "beifg.h"
#include "beifg_impl.h"
Daniel Grund's avatar
Daniel Grund committed
60
#include "becopyopt.h"
61
#include "becopystat.h"
62
#include "bessadestr.h"
Sebastian Hack's avatar
Sebastian Hack committed
63
#include "beabi.h"
Christian Würdig's avatar
Christian Würdig committed
64
#include "belower.h"
Sebastian Hack's avatar
Sebastian Hack committed
65
#include "beschedmris.h"
66
#include "bestat.h"
67
#include "beverify.h"
Adam Szalkowski's avatar
Adam Szalkowski committed
68
#include "beprofile.h"
69
#include "beblocksched.h"
70
#include "be_dbgout.h"
Sebastian Hack's avatar
Sebastian Hack committed
71

72
73
74
75
#ifdef WITH_ILP
#include "beilpsched.h"
#endif /* WITH_ILP */

Sebastian Hack's avatar
Sebastian Hack committed
76
/* options visible for anyone */
Sebastian Hack's avatar
Sebastian Hack committed
77
static be_options_t be_options = {
78
	DUMP_NONE,                         /* dump flags */
79
	BE_TIME_OFF,                       /* no timing */
80
81
	0,                                 /* no opt profile */
	1,                                 /* try to omit frame pointer */
82
	0,                                 /* always stabs debugging output */
83
	BE_VRFY_WARN,                      /* verification level: warn */
84
	BE_SCHED_LIST,                     /* scheduler: list scheduler */
85
86
	"i44pc52.info.uni-karlsruhe.de",   /* ilp server */
	"cplex"                            /* ilp solver */
Sebastian Hack's avatar
Sebastian Hack committed
87
88
};

89
90
91
/* config file. */
static char config_file[256] = { 0 };

Sebastian Hack's avatar
Sebastian Hack committed
92
/* register allocator to use. */
Sebastian Hack's avatar
Sebastian Hack committed
93
static const be_ra_t *ra = &be_ra_chordal_allocator;
94

Sebastian Hack's avatar
Sebastian Hack committed
95
/* back end instruction set architecture to use */
Sebastian Hack's avatar
Sebastian Hack committed
96
static const arch_isa_if_t *isa_if = &ia32_isa_if;
Michael Beck's avatar
Michael Beck committed
97

Sebastian Hack's avatar
Sebastian Hack committed
98
#ifdef WITH_LIBCORE
Daniel Grund's avatar
Daniel Grund committed
99

Sebastian Hack's avatar
Sebastian Hack committed
100
static lc_opt_entry_t *be_grp_root = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
101

Sebastian Hack's avatar
Sebastian Hack committed
102
103
/* possible dumping options */
static const lc_opt_enum_mask_items_t dump_items[] = {
104
	{ "none",       DUMP_NONE },
Sebastian Hack's avatar
Sebastian Hack committed
105
	{ "initial",    DUMP_INITIAL },
106
	{ "abi",        DUMP_ABI    },
Sebastian Hack's avatar
Sebastian Hack committed
107
108
	{ "sched",      DUMP_SCHED  },
	{ "prepared",   DUMP_PREPARED },
109
	{ "regalloc",   DUMP_RA },
Sebastian Hack's avatar
Sebastian Hack committed
110
	{ "final",      DUMP_FINAL },
111
112
	{ "be",         DUMP_BE },
	{ "all",        2 * DUMP_BE - 1 },
Sebastian Hack's avatar
Sebastian Hack committed
113
114
115
116
117
	{ NULL,         0 }
};

/* register allocators */
static const lc_opt_enum_const_ptr_items_t ra_items[] = {
118
	{ "chordal",  &be_ra_chordal_allocator },
Daniel Grund's avatar
:wq    
Daniel Grund committed
119
	{ "external", &be_ra_external_allocator },
Sebastian Hack's avatar
Sebastian Hack committed
120
121
122
123
124
	{ NULL,      NULL }
};

/* instruction set architectures. */
static const lc_opt_enum_const_ptr_items_t isa_items[] = {
125
	{ "ia32",    &ia32_isa_if },
126
#if 0
127
	{ "sta",     &sta_isa_if },
128
	{ "arm",     &arm_isa_if },
129
	{ "ppc32",   &ppc32_isa_if },
130
	{ "mips",    &mips_isa_if },
131
#endif
Sebastian Hack's avatar
Sebastian Hack committed
132
133
	{ NULL,      NULL }
};
134

Christian Würdig's avatar
Christian Würdig committed
135
136
137
138
139
/* verify options. */
static const lc_opt_enum_int_items_t vrfy_items[] = {
	{ "off",    BE_VRFY_OFF    },
	{ "warn",   BE_VRFY_WARN   },
	{ "assert", BE_VRFY_ASSERT },
Christian Würdig's avatar
Christian Würdig committed
140
	{ NULL,     0 }
Christian Würdig's avatar
Christian Würdig committed
141
};
142

143
144
145
146
147
148
149
150
151
/* scheduling options. */
static const lc_opt_enum_int_items_t sched_items[] = {
	{ "list", BE_SCHED_LIST },
#ifdef WITH_ILP
	{ "ilp",  BE_SCHED_ILP  },
#endif /* WITH_ILP */
	{ NULL,   0 }
};

Sebastian Hack's avatar
Sebastian Hack committed
152
static lc_opt_enum_mask_var_t dump_var = {
153
	&be_options.dump_flags, dump_items
154
155
};

Sebastian Hack's avatar
Sebastian Hack committed
156
157
158
static lc_opt_enum_const_ptr_var_t ra_var = {
	(const void **) &ra, ra_items
};
159

Sebastian Hack's avatar
Sebastian Hack committed
160
161
162
static lc_opt_enum_const_ptr_var_t isa_var = {
	(const void **) &isa_if, isa_items
};
163

Christian Würdig's avatar
Christian Würdig committed
164
static lc_opt_enum_int_var_t vrfy_var = {
165
	&be_options.vrfy_option, vrfy_items
Christian Würdig's avatar
Christian Würdig committed
166
167
};

168
169
170
171
static lc_opt_enum_int_var_t sched_var = {
	&be_options.scheduler, sched_items
};

Sebastian Hack's avatar
Sebastian Hack committed
172
static const lc_opt_table_entry_t be_main_options[] = {
173
174
175
176
177
178
	LC_OPT_ENT_STR      ("config",   "read another config file containing backend options", config_file, sizeof(config_file)),
	LC_OPT_ENT_ENUM_MASK("dump",     "dump irg on several occasions",                       &dump_var),
	LC_OPT_ENT_ENUM_PTR ("ra",       "register allocator",                                  &ra_var),
	LC_OPT_ENT_ENUM_PTR ("isa",      "the instruction set architecture",                    &isa_var),
	LC_OPT_ENT_NEGBOOL  ("noomitfp", "do not omit frame pointer",                           &be_options.omit_fp),
	LC_OPT_ENT_BOOL     ("stabs",    "enable stabs debug support",                          &be_options.stabs_debug_support),
179
	LC_OPT_ENT_ENUM_PTR ("vrfy",     "verify the backend irg",                              &vrfy_var),
180
181
	LC_OPT_ENT_BOOL     ("time",     "get backend timing statistics",                       &be_options.timing),
	LC_OPT_ENT_BOOL     ("profile",  "instrument the code for execution count profiling",   &be_options.opt_profile),
182
	LC_OPT_ENT_ENUM_PTR ("sched",    "select a scheduler",                                  &sched_var),
183
	LC_OPT_ENT_STR      ("statfile", "append statistics to file statfile", &be_options.stat_file_name, sizeof(be_options.stat_file_name)),
184

Christian Würdig's avatar
Christian Würdig committed
185
#ifdef WITH_ILP
186
187
	LC_OPT_ENT_STR ("ilp.server", "the ilp server name", be_options.ilp_server, sizeof(be_options.ilp_server)),
	LC_OPT_ENT_STR ("ilp.solver", "the ilp solver name", be_options.ilp_solver, sizeof(be_options.ilp_solver)),
Christian Würdig's avatar
Christian Würdig committed
188
#endif /* WITH_ILP */
Sebastian Hack's avatar
Sebastian Hack committed
189
190
191
	{ NULL }
};

192
#endif /* WITH_LIBCORE */
Sebastian Hack's avatar
Sebastian Hack committed
193
194
195
196
197

void be_opt_register(void)
{
#ifdef WITH_LIBCORE
	int i;
Daniel Grund's avatar
:wq    
Daniel Grund committed
198
	lc_opt_entry_t *be_grp_ra;
199
200
201
	static int run_once = 0;

	if (! run_once) {
202
203
204
		run_once     = 1;
		be_grp_root  = lc_opt_get_grp(firm_opt_get_root(), "be");
		be_grp_ra    = lc_opt_get_grp(be_grp_root, "ra");
205
206
207
208
209
210
211
212
213
214
215
216
217
218

		lc_opt_add_table(be_grp_root, be_main_options);

		/* register allocator options */
		for(i = 0; ra_items[i].name != NULL; ++i) {
			const be_ra_t *ra = ra_items[i].value;
			ra->register_options(be_grp_ra);
		}

		/* register isa options */
		for(i = 0; isa_items[i].name != NULL; ++i) {
			const arch_isa_if_t *isa = isa_items[i].value;
			isa->register_options(be_grp_root);
		}
219

220
		/* list scheduler register options */
221
		list_sched_register_options(be_grp_root);
222

223
224
225
226
227
#ifdef WITH_ILP
		/* ilp scheduler register options */
		ilpsched_register_options(be_grp_root);
#endif /* WITH_ILP */

228
		be_block_schedule_register_options(be_grp_root);
229
230
231
	}
#endif /* WITH_LIBCORE */
}
Sebastian Hack's avatar
Sebastian Hack committed
232

233
234
235
/* Parse one argument. */
int be_parse_arg(const char *arg) {
#ifdef WITH_LIBCORE
Michael Beck's avatar
Michael Beck committed
236
237
238
239
	if (strcmp(arg, "help") == 0 || (arg[0] == '?' && arg[1] == '\0')) {
		lc_opt_print_help(be_grp_root, stdout);
		return -1;
	}
240
	return lc_opt_from_single_arg(be_grp_root, NULL, arg, NULL);
241
242
#else
	return 0;
243
#endif /* WITH_LIBCORE */
244
245
}

246
247
248
249
250
251
252
/** The be parameters returned by default, all off. */
const static backend_params be_params = {
	NULL,
	NULL,
	0,
	NULL,
};
Sebastian Hack's avatar
Sebastian Hack committed
253

Christian Würdig's avatar
Christian Würdig committed
254
255
256
257
258
259
260
261
262
263
/* Perform schedule verification if requested. */
static void be_sched_vrfy(ir_graph *irg, int vrfy_opt) {
	if (vrfy_opt == BE_VRFY_WARN) {
		be_verify_schedule(irg);
	}
	else if (vrfy_opt == BE_VRFY_ASSERT) {
		assert(be_verify_schedule(irg) && "Schedule verification failed.");
	}
}

264
265
/* Initialize the Firm backend. Must be run BEFORE init_firm()! */
const backend_params *be_init(void)
266
{
Sebastian Hack's avatar
Sebastian Hack committed
267
268
269
270
271
272
	be_opt_register();

	be_sched_init();
	be_copy_opt_init();
	copystat_init();
	phi_class_init();
273
274
275
276

	if (isa_if->get_params)
		return isa_if->get_params();
	return &be_params;
277
278
}

279
280
281
282
283
284
/**
 * Initializes the main environment for the backend.
 *
 * @param env          an empty environment
 * @param file_handle  the file handle where the output will be written to
 */
285
static be_main_env_t *be_init_env(be_main_env_t *env, FILE *file_handle)
Sebastian Hack's avatar
Sebastian Hack committed
286
{
Sebastian Hack's avatar
Sebastian Hack committed
287
	memset(env, 0, sizeof(*env));
Sebastian Hack's avatar
Sebastian Hack committed
288
289
	obstack_init(&env->obst);
	env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
Sebastian Hack's avatar
Sebastian Hack committed
290
	env->options  = &be_options;
291
	FIRM_DBG_REGISTER(env->dbg, "be.main");
Sebastian Hack's avatar
Sebastian Hack committed
292

Christian Würdig's avatar
Christian Würdig committed
293
	arch_env_init(env->arch_env, isa_if, file_handle, env);
Sebastian Hack's avatar
Sebastian Hack committed
294
295
296

	/* Register the irn handler of the architecture */
	if (arch_isa_get_irn_handler(env->arch_env->isa))
Sebastian Hack's avatar
Sebastian Hack committed
297
		arch_env_push_irn_handler(env->arch_env, arch_isa_get_irn_handler(env->arch_env->isa));
298

Michael Beck's avatar
Michael Beck committed
299
300
301
302
303
	/*
	 * Register the node handler of the back end infrastructure.
	 * This irn handler takes care of the platform independent
	 * spill, reload and perm nodes.
	 */
Sebastian Hack's avatar
Sebastian Hack committed
304
	arch_env_push_irn_handler(env->arch_env, &be_node_irn_handler);
Sebastian Hack's avatar
Sebastian Hack committed
305
306
	env->phi_handler = be_phi_handler_new(env->arch_env);
	arch_env_push_irn_handler(env->arch_env, env->phi_handler);
Sebastian Hack's avatar
Sebastian Hack committed
307

308
	env->db_handle = be_options.stabs_debug_support ? be_stabs_open(file_handle) : be_nulldbg_open();
Sebastian Hack's avatar
Sebastian Hack committed
309
	return env;
310
311
}

Sebastian Hack's avatar
Sebastian Hack committed
312
static void be_done_env(be_main_env_t *env)
313
{
Sebastian Hack's avatar
Sebastian Hack committed
314
	env->arch_env->isa->impl->done(env->arch_env->isa);
315
	be_dbg_close(env->db_handle);
Sebastian Hack's avatar
Sebastian Hack committed
316
	be_phi_handler_free(env->phi_handler);
Sebastian Hack's avatar
Sebastian Hack committed
317
	obstack_free(&env->obst, NULL);
Sebastian Hack's avatar
Sebastian Hack committed
318
}
Sebastian Hack's avatar
Sebastian Hack committed
319

320
321
322
323
324
325
326
327
328
/**
 * A wrapper around a firm dumper. Dumps only, if
 * flags are enabled.
 *
 * @param mask    a bitmask containing the reason what will be dumped
 * @param irg     the IR graph to dump
 * @param suffix  the suffix for the dumper
 * @param dumper  the dumper to be called
 */
Sebastian Hack's avatar
Sebastian Hack committed
329
static void dump(int mask, ir_graph *irg, const char *suffix,
330
                 void (*dumper)(ir_graph *, const char *))
Sebastian Hack's avatar
Sebastian Hack committed
331
{
332
	if(be_options.dump_flags & mask)
Christian Würdig's avatar
Christian Würdig committed
333
		be_dump(irg, suffix, dumper);
Sebastian Hack's avatar
Sebastian Hack committed
334
}
335

Michael Beck's avatar
Michael Beck committed
336
/**
337
 * Prepare a backend graph for code generation and initialize its birg
Michael Beck's avatar
Michael Beck committed
338
 */
339
static void initialize_birg(be_irg_t *birg, ir_graph *irg, be_main_env_t *env)
Sebastian Hack's avatar
Sebastian Hack committed
340
{
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
	memset(birg, 0, sizeof(*birg));
	birg->irg = irg;
	birg->main_env = env;

	edges_deactivate_kind(irg, EDGE_KIND_DEP);
	edges_activate_kind(irg, EDGE_KIND_DEP);

	DBG((env->dbg, LEVEL_2, "====> IRG: %F\n", irg));
	dump(DUMP_INITIAL, irg, "-begin", dump_ir_block_graph);

	be_stat_init_irg(env->arch_env, irg);
	be_do_stat_nodes(irg, "01 Begin");

	/* set the current graph (this is important for several firm functions) */
	current_ir_graph = irg;

Sebastian Hack's avatar
Sebastian Hack committed
357
358
	/* Normalize proj nodes. */
	normalize_proj_nodes(irg);
Sebastian Hack's avatar
Sebastian Hack committed
359

Sebastian Hack's avatar
Sebastian Hack committed
360
	/* Make just one return node. */
Sebastian Hack's avatar
Sebastian Hack committed
361
	normalize_one_return(irg);
Sebastian Hack's avatar
Sebastian Hack committed
362

Sebastian Hack's avatar
Sebastian Hack committed
363
364
	/* Remove critical edges */
	remove_critical_cf_edges(irg);
Sebastian Hack's avatar
Sebastian Hack committed
365

Sebastian Hack's avatar
Sebastian Hack committed
366
	/* Ensure, that the ir_edges are computed. */
Sebastian Hack's avatar
Sebastian Hack committed
367
	edges_assure(irg);
Sebastian Hack's avatar
Sebastian Hack committed
368

Sebastian Hack's avatar
Sebastian Hack committed
369
	/* reset the phi handler. */
370
	be_phi_handler_reset(env->phi_handler);
Sebastian Hack's avatar
Sebastian Hack committed
371
372
}

373
374
375
376
377
#ifdef WITH_LIBCORE

#define BE_TIMER_PUSH(timer)                                                        \
	if (be_options.timing == BE_TIME_ON) {                                          \
		int res = lc_timer_push(timer);                                             \
378
		if (be_options.vrfy_option == BE_VRFY_ASSERT)                               \
379
			assert(res && "Timer already on stack, cannot be pushed twice.");       \
380
		else if (be_options.vrfy_option == BE_VRFY_WARN && ! res)                   \
381
382
383
384
385
386
			fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
				lc_timer_get_name(timer));                                          \
	}
#define BE_TIMER_POP(timer)                                                                    \
	if (be_options.timing == BE_TIME_ON) {                                                     \
		lc_timer_t *tmp = lc_timer_pop();                                                      \
387
		if (be_options.vrfy_option == BE_VRFY_ASSERT)                                          \
388
			assert(tmp == timer && "Attempt to pop wrong timer.");                             \
389
		else if (be_options.vrfy_option == BE_VRFY_WARN && tmp != timer)                       \
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
			fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
				lc_timer_get_name(tmp), lc_timer_get_name(timer));                             \
		timer = tmp;                                                                           \
	}

#define BE_TIMER_ONLY(code)   do { if (be_options.timing == BE_TIME_ON) { code; } } while(0)

#else

#define BE_TIMER_PUSH(timer)
#define BE_TIMER_POP(timer)
#define BE_TIMER_ONLY(code)

#endif /* WITH_LIBCORE */


Michael Beck's avatar
Michael Beck committed
406
407
408
409
/**
 * The Firm backend main loop.
 * Do architecture specific lowering for all graphs
 * and call the architecture specific code generator.
410
411
 *
 * @param file_handle   the file handle the output will be written to
412
 * @param cup_name      name of the compilation unit
Michael Beck's avatar
Michael Beck committed
413
 */
414
static void be_main_loop(FILE *file_handle, const char *cup_name)
415
{
416
	int i;
Sebastian Hack's avatar
Sebastian Hack committed
417
	arch_isa_t *isa;
418
	be_main_env_t env;
419
420
421
	unsigned num_nodes_b = 0;
	unsigned num_nodes_a = 0;
	unsigned num_nodes_r = 0;
422
	char prof_filename[256];
Adam Szalkowski's avatar
Adam Szalkowski committed
423
	static const char suffix[] = ".prof";
424
425
	be_irg_t *birgs;
	unsigned num_birgs;
426

Michael Beck's avatar
Michael Beck committed
427
428
	be_ra_timer_t *ra_timer;

429
#ifdef WITH_LIBCORE
Christian Würdig's avatar
Christian Würdig committed
430
431
432
433
	lc_timer_t *t_abi      = NULL;
	lc_timer_t *t_codegen  = NULL;
	lc_timer_t *t_sched    = NULL;
   	lc_timer_t *t_constr   = NULL;
Matthias Braun's avatar
Matthias Braun committed
434
	lc_timer_t *t_regalloc = NULL;
Christian Würdig's avatar
Christian Würdig committed
435
436
437
438
	lc_timer_t *t_finish   = NULL;
	lc_timer_t *t_emit     = NULL;
	lc_timer_t *t_other    = NULL;
	lc_timer_t *t_verify   = NULL;
439
440
441
442
443
444
445
446
447

	if (be_options.timing == BE_TIME_ON) {
		t_abi      = lc_timer_register("beabi",    "be abi introduction");
		t_codegen  = lc_timer_register("codegen",  "codegeneration");
		t_sched    = lc_timer_register("sched",    "scheduling");
		t_constr   = lc_timer_register("constr",   "assure constraints");
		t_regalloc = lc_timer_register("regalloc", "register allocation");
		t_finish   = lc_timer_register("finish",   "graph finish");
		t_emit     = lc_timer_register("emiter",   "code emiter");
Christian Würdig's avatar
Christian Würdig committed
448
		t_verify   = lc_timer_register("verify",   "graph verification");
Michael Beck's avatar
Michael Beck committed
449
		t_other    = lc_timer_register("other",    "other");
450
	}
451
#endif /* WITH_LIBCORE */
452

453
	be_init_env(&env, file_handle);
Sebastian Hack's avatar
Sebastian Hack committed
454
455

	isa = arch_env_get_isa(env.arch_env);
456

Michael Beck's avatar
Michael Beck committed
457
458
	be_dbg_so(env.db_handle, cup_name);
	be_dbg_types(env.db_handle);
459

Christian Würdig's avatar
Christian Würdig committed
460
	/* we might need 1 birg more for instrumentation constructor */
461
	num_birgs = get_irp_n_irgs();
Christian Würdig's avatar
Christian Würdig committed
462
	birgs     = alloca(sizeof(birgs[0]) * (num_birgs + 1));
463

Christian Würdig's avatar
Christian Würdig committed
464
	/* First: initialize all birgs */
465
466
467
468
469
	for(i = 0; i < get_irp_n_irgs(); ++i) {
		ir_graph *irg = get_irp_irg(i);

		initialize_birg(&birgs[i], irg, &env);
	}
Michael Beck's avatar
Michael Beck committed
470

Christian Würdig's avatar
Christian Würdig committed
471
472
473
474
475
	/*
		Get the filename for the profiling data.
		Beware: '\0' is already included in sizeof(suffix)
	*/
	memset(prof_filename, 0, sizeof(prof_filename));
476
	strncpy(prof_filename, cup_name, sizeof(prof_filename) - sizeof(suffix));
Adam Szalkowski's avatar
Adam Szalkowski committed
477
	strcat(prof_filename, suffix);
478

Christian Würdig's avatar
Christian Würdig committed
479
480
481
482
483
	/*
		Next: Either instruments all irgs with profiling code
		or try to read in profile data for current translation unit.
	*/
	if (be_options.opt_profile) {
484
		ir_graph *prof_init_irg = be_profile_instrument(prof_filename, profile_default);
485
486
		initialize_birg(&birgs[num_birgs], prof_init_irg, &env);
		num_birgs++;
Michael Beck's avatar
Michael Beck committed
487
		set_method_img_section(get_irg_entity(prof_init_irg), section_constructors);
Christian Würdig's avatar
Christian Würdig committed
488
489
	}
	else {
490
		be_profile_read(prof_filename);
491
	}
Adam Szalkowski's avatar
Adam Szalkowski committed
492

493
	/* For all graphs */
494
495
	for (i = 0; i < num_birgs; ++i) {
		be_irg_t *birg = & birgs[i];
Christian Würdig's avatar
Christian Würdig committed
496
		ir_graph *irg  = birg->irg;
497
		optimization_state_t state;
498
		const arch_code_generator_if_t *cg_if;
499
		char irg_name[128];
Sebastian Hack's avatar
Sebastian Hack committed
500

501
502
503
		/* set the current graph (this is important for several firm functions) */
		current_ir_graph = irg;

Matthias Braun's avatar
Matthias Braun committed
504
#ifdef FIRM_STATISTICS
505
506
		if(be_stat_ev_is_active()) {
			ir_snprintf(irg_name, sizeof(irg_name), "%F", irg);
507
			be_stat_tags[STAT_TAG_CLS] = "<all>";
508
509
510
			be_stat_tags[STAT_TAG_IRG] = irg_name;
			be_stat_ev_push(be_stat_tags, STAT_TAG_LAST, be_stat_file);
		}
Matthias Braun's avatar
Matthias Braun committed
511
#endif
512

513
		/* stop and reset timers */
514
		BE_TIMER_ONLY(
515
516
517
518
519
520
521
			LC_STOP_AND_RESET_TIMER(t_abi);
			LC_STOP_AND_RESET_TIMER(t_codegen);
			LC_STOP_AND_RESET_TIMER(t_sched);
			LC_STOP_AND_RESET_TIMER(t_constr);
			LC_STOP_AND_RESET_TIMER(t_regalloc);
			LC_STOP_AND_RESET_TIMER(t_finish);
			LC_STOP_AND_RESET_TIMER(t_emit);
Christian Würdig's avatar
Christian Würdig committed
522
			LC_STOP_AND_RESET_TIMER(t_verify);
Michael Beck's avatar
Michael Beck committed
523
			LC_STOP_AND_RESET_TIMER(t_other);
524
		);
Christian Würdig's avatar
Christian Würdig committed
525
		BE_TIMER_PUSH(t_other);   /* t_other */
526

527
528
529
530
531
532
533
534
535
536
537
		/* Verify the initial graph */
		BE_TIMER_PUSH(t_verify);
		if (be_options.vrfy_option == BE_VRFY_WARN) {
			irg_verify(irg, VRFY_ENFORCE_SSA);
			be_check_dominance(irg);
		} else if (be_options.vrfy_option == BE_VRFY_ASSERT) {
			assert(irg_verify(irg, VRFY_ENFORCE_SSA) && "irg verification failed");
			assert(be_check_dominance(irg) && "Dominance verification failed");
		}
		BE_TIMER_POP(t_verify);

538
539
540
		/**
		 * Create execution frequencies from profile data or estimate some
		 */
Christian Würdig's avatar
Christian Würdig committed
541
		if (be_profile_has_data()) {
542
			birg->exec_freq = be_create_execfreqs_from_profile(irg);
543
		} else {
544
			birg->exec_freq = compute_execfreq(irg, 10);
545
		}
Christian Würdig's avatar
Christian Würdig committed
546

Michael Beck's avatar
Michael Beck committed
547
		BE_TIMER_ONLY(num_nodes_b = get_num_reachable_nodes(irg));
548

549
550
551
552
553
554
555
556
557
		/* Get the code generator interface. */
		cg_if = isa->impl->get_code_generator_if(isa);

		/* get a code generator for this graph. */
		birg->cg = cg_if->init(birg);

		/* some transformations need to be done before abi introduce */
		arch_code_generator_before_abi(birg->cg);

558
559
		/* reset the phi handler. */
		be_phi_handler_reset(env.phi_handler);
560

Sebastian Hack's avatar
Sebastian Hack committed
561
		/* implement the ABI conventions. */
Christian Würdig's avatar
Christian Würdig committed
562
		BE_TIMER_PUSH(t_abi);
563
		birg->abi = be_abi_introduce(birg);
Christian Würdig's avatar
Christian Würdig committed
564
		BE_TIMER_POP(t_abi);
Sebastian Hack's avatar
Sebastian Hack committed
565

Christian Würdig's avatar
Christian Würdig committed
566
		dump(DUMP_ABI, irg, "-abi", dump_ir_block_graph);
Christian Würdig's avatar
Christian Würdig committed
567
568
		be_do_stat_nodes(irg, "02 Abi");

569
		/* generate code */
Christian Würdig's avatar
Christian Würdig committed
570
		BE_TIMER_PUSH(t_codegen);
571
		arch_code_generator_prepare_graph(birg->cg);
Christian Würdig's avatar
Christian Würdig committed
572
		BE_TIMER_POP(t_codegen);
Sebastian Hack's avatar
Sebastian Hack committed
573

Christian Würdig's avatar
Christian Würdig committed
574
575
		be_do_stat_nodes(irg, "03 Prepare");

Sebastian Hack's avatar
Sebastian Hack committed
576
		/*
Christian Würdig's avatar
Christian Würdig committed
577
578
579
			Since the code generator made a lot of new nodes and skipped
			a lot of old ones, we should do dead node elimination here.
			Note that this requires disabling the edges here.
Christian Würdig's avatar
Christian Würdig committed
580
		*/
Sebastian Hack's avatar
Sebastian Hack committed
581
		edges_deactivate(irg);
582
		//dead_node_elimination(irg);
Sebastian Hack's avatar
Sebastian Hack committed
583
		edges_activate(irg);
584

585
586
		/* Compute loop nesting information (for weighting copies) */
		construct_cf_backedges(irg);
Sebastian Hack's avatar
Sebastian Hack committed
587
		dump(DUMP_PREPARED, irg, "-prepared", dump_ir_block_graph);
Michael Beck's avatar
Michael Beck committed
588
		BE_TIMER_ONLY(num_nodes_r = get_num_reachable_nodes(irg));
589

Christian Würdig's avatar
Christian Würdig committed
590
591
		/* let backend prepare scheduling */
		BE_TIMER_PUSH(t_codegen);
592
		arch_code_generator_before_sched(birg->cg);
Christian Würdig's avatar
Christian Würdig committed
593
		BE_TIMER_POP(t_codegen);
Christian Würdig's avatar
Christian Würdig committed
594

Christian Würdig's avatar
Christian Würdig committed
595
		/* schedule the irg */
Christian Würdig's avatar
Christian Würdig committed
596
		BE_TIMER_PUSH(t_sched);
597
598
599
600
601
602
603
604
605
		switch (be_options.scheduler) {
			default:
				fprintf(stderr, "Warning: invalid scheduler (%d) selected, falling back to list scheduler.\n", be_options.scheduler);
			case BE_SCHED_LIST:
				list_sched(birg, &be_options);
				break;
#ifdef WITH_ILP
			case BE_SCHED_ILP:
				be_ilp_sched(birg);
606
607
				//fprintf(stderr, "Warning: ILP scheduler not yet fully implemented, falling back to list scheduler.\n");
				//list_sched(birg, &be_options);
608
609
610
				break;
#endif /* WITH_ILP */
		};
Christian Würdig's avatar
Christian Würdig committed
611
		BE_TIMER_POP(t_sched);
Sebastian Hack's avatar
Sebastian Hack committed
612

Christian Würdig's avatar
Christian Würdig committed
613
		dump(DUMP_SCHED, irg, "-sched", dump_ir_block_graph_sched);
Christian Würdig's avatar
Christian Würdig committed
614

Christian Würdig's avatar
Christian Würdig committed
615
		/* check schedule */
Christian Würdig's avatar
Christian Würdig committed
616
		BE_TIMER_PUSH(t_verify);
617
		be_sched_vrfy(irg, be_options.vrfy_option);
Christian Würdig's avatar
Christian Würdig committed
618
		BE_TIMER_POP(t_verify);
619

Christian Würdig's avatar
Christian Würdig committed
620
621
		be_do_stat_nodes(irg, "04 Schedule");

Christian Würdig's avatar
Christian Würdig committed
622
		/* introduce patterns to assure constraints */
Michael Beck's avatar
Michael Beck committed
623
		BE_TIMER_PUSH(t_constr);
624
		/* we switch off optimizations here, because they might cause trouble */
625
		save_optimization_state(&state);
626
627
628
		set_optimize(0);
		set_opt_normalize(0);

629
630
		/* add Keeps for should_be_different constrained nodes  */
		/* beware: needs schedule due to usage of be_ssa_constr */
631
		assure_constraints(birg);
Christian Würdig's avatar
Christian Würdig committed
632
		BE_TIMER_POP(t_constr);
633

Christian Würdig's avatar
Christian Würdig committed
634
		dump(DUMP_SCHED, irg, "-assured", dump_ir_block_graph_sched);
Christian Würdig's avatar
Christian Würdig committed
635
636
		be_do_stat_nodes(irg, "05 Constraints");

Sebastian Hack's avatar
Sebastian Hack committed
637
		/* connect all stack modifying nodes together (see beabi.c) */
Christian Würdig's avatar
Christian Würdig committed
638
		BE_TIMER_PUSH(t_abi);
639
		be_abi_fix_stack_nodes(birg->abi, NULL);
Christian Würdig's avatar
Christian Würdig committed
640
		BE_TIMER_POP(t_abi);
Christian Würdig's avatar
Christian Würdig committed
641

Sebastian Hack's avatar
Sebastian Hack committed
642
		dump(DUMP_SCHED, irg, "-fix_stack", dump_ir_block_graph_sched);
Sebastian Hack's avatar
Sebastian Hack committed
643

Christian Würdig's avatar
Christian Würdig committed
644
		/* check schedule */
Christian Würdig's avatar
Christian Würdig committed
645
		BE_TIMER_PUSH(t_verify);
646
		be_sched_vrfy(irg, be_options.vrfy_option);
Christian Würdig's avatar
Christian Würdig committed
647
		BE_TIMER_POP(t_verify);
Sebastian Hack's avatar
Sebastian Hack committed
648

649
		/* do some statistics */
650
		be_do_stat_reg_pressure(birg);
651

Michael Beck's avatar
Michael Beck committed
652
		/* stuff needs to be done after scheduling but before register allocation */
Christian Würdig's avatar
Christian Würdig committed
653
		BE_TIMER_PUSH(t_codegen);
654
		arch_code_generator_before_ra(birg->cg);
Christian Würdig's avatar
Christian Würdig committed
655
		BE_TIMER_POP(t_codegen);
656

Matthias Braun's avatar
Matthias Braun committed
657
#ifdef FIRM_STATISTICS
658
659
		if(be_stat_ev_is_active()) {
			be_stat_ev_l("costs_before_ra",
660
					(long) be_estimate_irg_costs(irg, env.arch_env, birg->exec_freq));
661
		}
Matthias Braun's avatar
Matthias Braun committed
662
#endif
663

664
		/* Do register allocation */
665
		BE_TIMER_PUSH(t_regalloc);
666
		ra_timer = ra->allocate(birg);
667
		BE_TIMER_POP(t_regalloc);
668

Matthias Braun's avatar
Matthias Braun committed
669
#ifdef FIRM_STATISTICS
670
671
		if(be_stat_ev_is_active()) {
			be_stat_ev_l("costs_after_ra",
672
					(long) be_estimate_irg_costs(irg, env.arch_env, birg->exec_freq));
673
		}
Matthias Braun's avatar
Matthias Braun committed
674
#endif
675

Christian Würdig's avatar
Christian Würdig committed
676
		dump(DUMP_RA, irg, "-ra", dump_ir_block_graph_sched);
Christian Würdig's avatar
Christian Würdig committed
677
678
		be_do_stat_nodes(irg, "06 Register Allocation");

Christian Würdig's avatar
Christian Würdig committed
679
		/* let the code generator prepare the graph for emitter */
Michael Beck's avatar
Michael Beck committed
680
		BE_TIMER_PUSH(t_finish);
681
		arch_code_generator_after_ra(birg->cg);
Christian Würdig's avatar
Christian Würdig committed
682
		BE_TIMER_POP(t_finish);
Sebastian Hack's avatar
Sebastian Hack committed
683

Christian Würdig's avatar
Christian Würdig committed
684
685
		/* fix stack offsets */
		BE_TIMER_PUSH(t_abi);
686
687
688
		be_abi_fix_stack_nodes(birg->abi, NULL);
		be_remove_dead_nodes_from_schedule(irg);
		be_abi_fix_stack_bias(birg->abi);
Christian Würdig's avatar
Christian Würdig committed
689
		BE_TIMER_POP(t_abi);
Christian Würdig's avatar
Christian Würdig committed
690

691
		BE_TIMER_PUSH(t_finish);
692
		arch_code_generator_finish(birg->cg);
693
694
		BE_TIMER_POP(t_finish);

695
696
		dump(DUMP_FINAL, irg, "-finish", dump_ir_block_graph_sched);

697
		/* check schedule and register allocation */
Christian Würdig's avatar
Christian Würdig committed
698
		BE_TIMER_PUSH(t_verify);
699
		if (be_options.vrfy_option == BE_VRFY_WARN) {
700
701
702
703
704
			//irg_verify(irg, VRFY_ENFORCE_SSA);
			be_check_dominance(irg);
			be_verify_out_edges(irg);
			be_verify_schedule(irg);
			be_verify_register_allocation(env.arch_env, irg);
705
		}
706
		else if (be_options.vrfy_option == BE_VRFY_ASSERT) {
707
708
709
710
711
			//assert(irg_verify(irg, VRFY_ENFORCE_SSA) && "irg verification failed");
			assert(be_verify_out_edges(irg));
			assert(be_check_dominance(irg) && "Dominance verification failed");
			assert(be_verify_schedule(irg) && "Schedule verification failed");
			assert(be_verify_register_allocation(env.arch_env, irg)
712
713
			       && "register allocation verification failed");
		}
Christian Würdig's avatar
Christian Würdig committed
714
		BE_TIMER_POP(t_verify);
715

Christian Würdig's avatar
Christian Würdig committed
716
		/* emit assembler code */
Michael Beck's avatar
Michael Beck committed
717
		BE_TIMER_PUSH(t_emit);
718
		arch_code_generator_done(birg->cg);
Christian Würdig's avatar
Christian Würdig committed
719
		BE_TIMER_POP(t_emit);
Christian Würdig's avatar
Christian Würdig committed
720

721
		dump(DUMP_FINAL, irg, "-end", dump_ir_extblock_graph_sched);
Christian Würdig's avatar
Christian Würdig committed
722
723

		BE_TIMER_PUSH(t_abi);
724
		be_abi_free(birg->abi);
Christian Würdig's avatar
Christian Würdig committed
725
		BE_TIMER_POP(t_abi);
Christian Würdig's avatar
Christian Würdig committed
726

Christian Würdig's avatar
Christian Würdig committed
727
		be_do_stat_nodes(irg, "07 Final");
728
		restore_optimization_state(&state);
729

Michael Beck's avatar
Michael Beck committed
730
		BE_TIMER_ONLY(num_nodes_a = get_num_reachable_nodes(irg));
Christian Würdig's avatar
Christian Würdig committed
731
		BE_TIMER_POP(t_other);
Michael Beck's avatar
Michael Beck committed
732

733
#define LC_EMIT(timer)  \
734
735
736
737
738
	if(!be_stat_ev_is_active()) { \
		printf("%-20s: %.3lf msec\n", lc_timer_get_description(timer), (double)lc_timer_elapsed_usec(timer) / 1000.0); \
	} else { \
		be_stat_ev_l(lc_timer_get_name(timer), lc_timer_elapsed_msec(timer)); \
	}
739
#define LC_EMIT_RA(timer) \
740
741
742
743
744
	if(!be_stat_ev_is_active()) { \
		printf("\t%-20s: %.3lf msec\n", lc_timer_get_description(timer), (double)lc_timer_elapsed_usec(timer) / 1000.0); \
	} else { \
		be_stat_ev_l(lc_timer_get_name(timer), lc_timer_elapsed_msec(timer)); \
	}
745
		BE_TIMER_ONLY(
746
747
748
749
750
751
			if(!be_stat_ev_is_active()) {
				printf("==>> IRG %s <<==\n", get_entity_name(get_irg_entity(irg)));
				printf("# nodes at begin:  %u\n", num_nodes_b);
				printf("# nodes before ra: %u\n", num_nodes_r);
				printf("# nodes at end:    %u\n\n", num_nodes_a);
			}
752
753
754
755
756
			LC_EMIT(t_abi);
			LC_EMIT(t_codegen);
			LC_EMIT(t_sched);
			LC_EMIT(t_constr);
			LC_EMIT(t_regalloc);
Christian Würdig's avatar
Christian Würdig committed
757
758
759
			LC_EMIT_RA(ra_timer->t_prolog);
			LC_EMIT_RA(ra_timer->t_live);
			LC_EMIT_RA(ra_timer->t_spill);
760
			LC_EMIT_RA(ra_timer->t_spillslots);
Christian Würdig's avatar
Christian Würdig committed
761
762
763
764
765
766
767
			LC_EMIT_RA(ra_timer->t_color);
			LC_EMIT_RA(ra_timer->t_ifg);
			LC_EMIT_RA(ra_timer->t_copymin);
			LC_EMIT_RA(ra_timer->t_ssa);
			LC_EMIT_RA(ra_timer->t_epilog);
			LC_EMIT_RA(ra_timer->t_verify);
			LC_EMIT_RA(ra_timer->t_other);
768
769
			LC_EMIT(t_finish);
			LC_EMIT(t_emit);
Christian Würdig's avatar
Christian Würdig committed
770
			LC_EMIT(t_verify);
Michael Beck's avatar
Michael Beck committed
771
			LC_EMIT(t_other);
772
773
		);
#undef LC_EMIT_RA
774
#undef LC_EMIT
Michael Beck's avatar
BugFix:    
Michael Beck committed
775

776
		be_free_birg(birg);
777

Michael Beck's avatar
BugFix:    
Michael Beck committed
778
        /* switched off due to statistics (statistic module needs all irgs) */
Matthias Braun's avatar
Matthias Braun committed
779
#ifdef FIRM_STATISTICS
Michael Beck's avatar
BugFix:    
Michael Beck committed
780
		if (! stat_is_active())
Matthias Braun's avatar
Matthias Braun committed
781
#endif
Michael Beck's avatar
BugFix:    
Michael Beck committed
782
			free_ir_graph(irg);
783

784
785
786
		if(be_stat_ev_is_active()) {
			be_stat_ev_pop();
		}
787
	}
788
	be_profile_free();
Sebastian Hack's avatar
Sebastian Hack committed
789
	be_done_env(&env);
790

Christian Würdig's avatar
Christian Würdig committed
791
792
793
#undef BE_TIMER_POP
#undef BE_TIMER_PUSH
#undef BE_TIMER_ONLY
794
795
}

Michael Beck's avatar
Michael Beck committed
796
/* Main interface to the frontend. */
797
void be_main(FILE *file_handle, const char *cup_name)
798
{
799
#ifdef WITH_LIBCORE
Matthias Braun's avatar
Matthias Braun committed
800
	lc_timer_t *t = NULL;
801
802
803
804
805
806
807
808
809
810

	/* The user specified another config file to read. do that now. */
	if(strlen(config_file) > 0) {
		FILE *f;

		if((f = fopen(config_file, "rt")) != NULL) {
			lc_opt_from_file(config_file, f, NULL);
			fclose(f);
		}
	}
811
812
813
814
815
816
817
818
819
820

	if (be_options.timing == BE_TIME_ON) {
		t = lc_timer_register("bemain", "measure complete bemain loop");

		if (lc_timer_enter_high_priority()) {
			fprintf(stderr, "Warning: Could not enter high priority mode.\n");
		}

		lc_timer_reset_and_start(t);
	}
821

Matthias Braun's avatar
Matthias Braun committed
822
#ifdef FIRM_STATISTICS
823
	be_init_stat_file(be_options.stat_file_name, cup_name);
Matthias Braun's avatar
Matthias Braun committed
824
#endif
825
826
#endif /* WITH_LIBCORE */

827
	/* never build code for pseudo irgs */
Michael Beck's avatar
Michael Beck committed
828
	set_visit_pseudo_irgs(0);
829

Sebastian Hack's avatar
Sebastian Hack committed
830
 	be_node_init();
831

832
	be_main_loop(file_handle, cup_name);
833
834
835
836
837

#ifdef WITH_LIBCORE
	if (be_options.timing == BE_TIME_ON) {
		lc_timer_stop(t);
		lc_timer_leave_high_priority();
838
839
840
841
842
		if(be_stat_ev_is_active()) {
			be_stat_ev_l("backend_time", lc_timer_elapsed_msec(t));
		} else {
			printf("%-20s: %lu msec\n", "BEMAINLOOP", lc_timer_elapsed_msec(t));
		}