bearch_ia32.c 62.7 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

Christian Würdig's avatar
Christian Würdig committed
20
/**
Christian Würdig's avatar
Christian Würdig committed
21
22
23
24
 * @file
 * @brief       This is the main ia32 firm backend driver.
 * @author      Christian Wuerdig
 * @version     $Id$
Christian Würdig's avatar
Christian Würdig committed
25
 */
26
#ifdef HAVE_CONFIG_H
27
#include "config.h"
28
29
#endif

Matthias Braun's avatar
Matthias Braun committed
30
31
#include "lc_opts.h"
#include "lc_opts_enum.h"
32

Christian Würdig's avatar
Christian Würdig committed
33
34
#include <math.h>

Christian Würdig's avatar
Christian Würdig committed
35
#include "pseudo_irg.h"
Christoph Mallon's avatar
Christoph Mallon committed
36
#include "irarch.h"
Christian Würdig's avatar
Christian Würdig committed
37
#include "irgwalk.h"
Christian Würdig's avatar
Christian Würdig committed
38
39
#include "irprog.h"
#include "irprintf.h"
40
#include "iredges_t.h"
41
#include "ircons.h"
Christian Würdig's avatar
Christian Würdig committed
42
#include "irgmod.h"
Christian Würdig's avatar
Christian Würdig committed
43
#include "irgopt.h"
44
#include "irbitset.h"
45
#include "irgopt.h"
46
#include "irdump_grgen.h"
47
#include "pdeq.h"
48
#include "pset.h"
Christian Würdig's avatar
Christian Würdig committed
49
#include "debug.h"
50
#include "error.h"
51
#include "xmalloc.h"
Michael Beck's avatar
Michael Beck committed
52
#include "irtools.h"
Matthias Braun's avatar
Matthias Braun committed
53
#include "iroptimize.h"
Michael Beck's avatar
Michael Beck committed
54
#include "instrument.h"
Christian Würdig's avatar
Christian Würdig committed
55

56
#include "../beabi.h"
57
#include "../beirg_t.h"
58
#include "../benode_t.h"
59
#include "../belower.h"
Christian Würdig's avatar
Christian Würdig committed
60
#include "../besched_t.h"
61
#include "be.h"
Christian Würdig's avatar
Christian Würdig committed
62
#include "../be_t.h"
63
#include "../beirgmod.h"
Michael Beck's avatar
Michael Beck committed
64
#include "../be_dbgout.h"
Matthias Braun's avatar
Matthias Braun committed
65
#include "../beblocksched.h"
66
#include "../bemachine.h"
67
#include "../beilpsched.h"
68
#include "../bespillslots.h"
69
#include "../bemodule.h"
Matthias Braun's avatar
Matthias Braun committed
70
#include "../begnuas.h"
71
#include "../bestate.h"
72
#include "../beflags.h"
73

74
#include "bearch_ia32_t.h"
Christian Würdig's avatar
Christian Würdig committed
75

76
77
#include "ia32_new_nodes.h"
#include "gen_ia32_regalloc_if.h"
78
#include "gen_ia32_machine.h"
Christian Würdig's avatar
Christian Würdig committed
79
#include "ia32_transform.h"
Christian Würdig's avatar
Christian Würdig committed
80
81
#include "ia32_emitter.h"
#include "ia32_map_regs.h"
Christian Würdig's avatar
Christian Würdig committed
82
#include "ia32_optimize.h"
83
#include "ia32_x87.h"
84
#include "ia32_dbg_stat.h"
Christian Würdig's avatar
Christian Würdig committed
85
#include "ia32_finish.h"
86
#include "ia32_util.h"
87
#include "ia32_fpu.h"
88
#include "ia32_architecture.h"
89

90
91
92
93
#ifdef FIRM_GRGEN_BE
#include "ia32_pbqp_transform.h"
#endif

94
DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
Christian Würdig's avatar
Christian Würdig committed
95

Christian Würdig's avatar
Christian Würdig committed
96
97
/* TODO: ugly */
static set *cur_reg_set = NULL;
Christian Würdig's avatar
Christian Würdig committed
98

99
100
ir_mode         *mode_fpcw       = NULL;
ia32_code_gen_t *ia32_current_cg = NULL;
101

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
 * The environment for the intrinsic mapping.
 */
static ia32_intrinsic_env_t intrinsic_env = {
	NULL,    /* the isa */
	NULL,    /* the irg, these entities belong to */
	NULL,    /* entity for first div operand (move into FPU) */
	NULL,    /* entity for second div operand (move into FPU) */
	NULL,    /* entity for converts ll -> d */
	NULL,    /* entity for converts d -> ll */
	NULL,    /* entity for __divdi3 library call */
	NULL,    /* entity for __moddi3 library call */
	NULL,    /* entity for __udivdi3 library call */
	NULL,    /* entity for __umoddi3 library call */
	NULL,    /* bias value for conversion from float to unsigned 64 */
};


120
121
122
typedef ir_node *(*create_const_node_func) (dbg_info *dbg, ir_graph *irg, ir_node *block);

static INLINE ir_node *create_const(ia32_code_gen_t *cg, ir_node **place,
123
                                    create_const_node_func func,
124
                                    const arch_register_t* reg)
125
126
127
128
129
130
131
132
133
134
135
{
	ir_node *block, *res;

	if(*place != NULL)
		return *place;

	block = get_irg_start_block(cg->irg);
	res = func(NULL, cg->irg, block);
	arch_set_irn_register(cg->arch_env, res, reg);
	*place = res;

136
137
	add_irn_dep(get_irg_end(cg->irg), res);
	/* add_irn_dep(get_irg_start(cg->irg), res); */
138
139
140
141

	return res;
}

142
/* Creates the unique per irg GP NoReg node. */
143
ir_node *ia32_new_NoReg_gp(ia32_code_gen_t *cg) {
144
145
146
147
148
149
150
151
152
153
154
155
	return create_const(cg, &cg->noreg_gp, new_rd_ia32_NoReg_GP,
	                    &ia32_gp_regs[REG_GP_NOREG]);
}

ir_node *ia32_new_NoReg_vfp(ia32_code_gen_t *cg) {
	return create_const(cg, &cg->noreg_vfp, new_rd_ia32_NoReg_VFP,
	                    &ia32_vfp_regs[REG_VFP_NOREG]);
}

ir_node *ia32_new_NoReg_xmm(ia32_code_gen_t *cg) {
	return create_const(cg, &cg->noreg_xmm, new_rd_ia32_NoReg_XMM,
	                    &ia32_xmm_regs[REG_XMM_NOREG]);
156
157
}

158
159
160
ir_node *ia32_new_Unknown_gp(ia32_code_gen_t *cg) {
	return create_const(cg, &cg->unknown_gp, new_rd_ia32_Unknown_GP,
	                    &ia32_gp_regs[REG_GP_UKNWN]);
161
}
162

163
164
165
166
167
168
169
170
171
172
ir_node *ia32_new_Unknown_vfp(ia32_code_gen_t *cg) {
	return create_const(cg, &cg->unknown_vfp, new_rd_ia32_Unknown_VFP,
	                    &ia32_vfp_regs[REG_VFP_UKNWN]);
}

ir_node *ia32_new_Unknown_xmm(ia32_code_gen_t *cg) {
	return create_const(cg, &cg->unknown_xmm, new_rd_ia32_Unknown_XMM,
	                    &ia32_xmm_regs[REG_XMM_UKNWN]);
}

173
174
175
176
177
ir_node *ia32_new_Fpu_truncate(ia32_code_gen_t *cg) {
	return create_const(cg, &cg->fpu_trunc_mode, new_rd_ia32_ChangeCW,
                        &ia32_fp_cw_regs[REG_FPCW]);
}

178

179
/**
180
 * Returns the admissible noreg register node for input register pos of node irn.
181
182
 */
ir_node *ia32_get_admissible_noreg(ia32_code_gen_t *cg, ir_node *irn, int pos) {
Matthias Braun's avatar
Matthias Braun committed
183
	const arch_register_req_t *req;
184

Matthias Braun's avatar
Matthias Braun committed
185
186
187
	req = arch_get_register_req(cg->arch_env, irn, pos);
	assert(req != NULL && "Missing register requirements");
	if (req->cls == &ia32_reg_classes[CLASS_ia32_gp])
188
		return ia32_new_NoReg_gp(cg);
Matthias Braun's avatar
Matthias Braun committed
189

190
191
192
193
194
	if (ia32_cg_config.use_sse2) {
		return ia32_new_NoReg_xmm(cg);
	} else {
		return ia32_new_NoReg_vfp(cg);
	}
195
196
}

Christian Würdig's avatar
Christian Würdig committed
197
198
199
200
201
202
203
204
205
206
207
/**************************************************
 *                         _ _              _  __
 *                        | | |            (_)/ _|
 *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
 * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
 * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
 * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
 *            __/ |
 *           |___/
 **************************************************/

Christian Würdig's avatar
Christian Würdig committed
208
209
210
211
212
/**
 * Return register requirements for an ia32 node.
 * If the node returns a tuple (mode_T) then the proj's
 * will be asked for this information.
 */
213
static const arch_register_req_t *ia32_get_irn_reg_req(const ir_node *node,
214
215
													   int pos)
{
Michael Beck's avatar
Michael Beck committed
216
217
	ir_mode *mode = get_irn_mode(node);
	long    node_pos;
Matthias Braun's avatar
Matthias Braun committed
218

Michael Beck's avatar
Michael Beck committed
219
	if (mode == mode_X || is_Block(node)) {
Matthias Braun's avatar
Matthias Braun committed
220
		return arch_no_register_req;
221
222
223
	}

	if (mode == mode_T && pos < 0) {
Matthias Braun's avatar
Matthias Braun committed
224
		return arch_no_register_req;
Christian Würdig's avatar
Christian Würdig committed
225
226
	}

Michael Beck's avatar
Michael Beck committed
227
	node_pos = pos == -1 ? 0 : pos;
Matthias Braun's avatar
Matthias Braun committed
228
	if (is_Proj(node)) {
Michael Beck's avatar
Michael Beck committed
229
		if (mode == mode_M || pos >= 0) {
Matthias Braun's avatar
Matthias Braun committed
230
			return arch_no_register_req;
231
232
		}

Matthias Braun's avatar
Matthias Braun committed
233
234
		node_pos = (pos == -1) ? get_Proj_proj(node) : pos;
		node     = skip_Proj_const(node);
Christian Würdig's avatar
Christian Würdig committed
235
236
	}

Matthias Braun's avatar
Matthias Braun committed
237
238
	if (is_ia32_irn(node)) {
		const arch_register_req_t *req;
Michael Beck's avatar
Michael Beck committed
239
		if (pos >= 0)
Matthias Braun's avatar
Matthias Braun committed
240
241
242
			req = get_ia32_in_req(node, pos);
		else
			req = get_ia32_out_req(node, node_pos);
243

Matthias Braun's avatar
Matthias Braun committed
244
		assert(req != NULL);
245

Matthias Braun's avatar
Matthias Braun committed
246
		return req;
Christian Würdig's avatar
Christian Würdig committed
247
248
	}

Matthias Braun's avatar
Matthias Braun committed
249
250
251
	/* unknowns should be transformed already */
	assert(!is_Unknown(node));
	return arch_no_register_req;
Christian Würdig's avatar
Christian Würdig committed
252
}
Christian Würdig's avatar
Christian Würdig committed
253

254
static void ia32_set_irn_reg(ir_node *irn, const arch_register_t *reg)
255
{
Michael Beck's avatar
Michael Beck committed
256
	int    pos = 0;
257

258
259
260
261
	if (get_irn_mode(irn) == mode_X) {
		return;
	}

262
	if (is_Proj(irn)) {
263
		pos = get_Proj_proj(irn);
Michael Beck's avatar
Michael Beck committed
264
		irn = skip_Proj(irn);
Christian Würdig's avatar
Christian Würdig committed
265
	}
Christian Würdig's avatar
Christian Würdig committed
266

Christian Würdig's avatar
Christian Würdig committed
267
268
	if (is_ia32_irn(irn)) {
		const arch_register_t **slots;
Christian Würdig's avatar
Christian Würdig committed
269

Christian Würdig's avatar
Christian Würdig committed
270
271
		slots      = get_ia32_slots(irn);
		slots[pos] = reg;
Matthias Braun's avatar
Matthias Braun committed
272
	} else {
273
		ia32_set_firm_reg(irn, reg, cur_reg_set);
Christian Würdig's avatar
Christian Würdig committed
274
	}
Christian Würdig's avatar
Christian Würdig committed
275
276
}

277
static const arch_register_t *ia32_get_irn_reg(const ir_node *irn)
278
{
Christian Würdig's avatar
Christian Würdig committed
279
280
281
	int pos = 0;
	const arch_register_t *reg = NULL;

282
	if (is_Proj(irn)) {
283
284
285
286
287

		if (get_irn_mode(irn) == mode_X) {
			return NULL;
		}

288
		pos = get_Proj_proj(irn);
289
		irn = skip_Proj_const(irn);
Christian Würdig's avatar
Christian Würdig committed
290
291
292
	}

	if (is_ia32_irn(irn)) {
Michael Beck's avatar
Michael Beck committed
293
294
		const arch_register_t **slots;
		slots = get_ia32_slots(irn);
295
		assert(pos < get_ia32_n_res(irn));
Michael Beck's avatar
Michael Beck committed
296
		reg   = slots[pos];
Matthias Braun's avatar
Matthias Braun committed
297
	} else {
298
		reg = ia32_get_firm_reg(irn, cur_reg_set);
Christian Würdig's avatar
Christian Würdig committed
299
300
301
	}

	return reg;
Christian Würdig's avatar
Christian Würdig committed
302
303
}

304
static arch_irn_class_t ia32_classify(const ir_node *irn) {
305
306
	arch_irn_class_t classification = arch_irn_class_normal;

307
	irn = skip_Proj_const(irn);
308

Christian Würdig's avatar
Christian Würdig committed
309
	if (is_cfop(irn))
310
311
312
313
314
315
316
317
		classification |= arch_irn_class_branch;

	if (! is_ia32_irn(irn))
		return classification & ~arch_irn_class_normal;

	if (is_ia32_Ld(irn))
		classification |= arch_irn_class_load;

318
	if (is_ia32_St(irn))
319
320
		classification |= arch_irn_class_store;

321
	if (is_ia32_need_stackent(irn))
322
323
324
		classification |= arch_irn_class_reload;

	return classification;
Christian Würdig's avatar
Christian Würdig committed
325
}
Christian Würdig's avatar
Christian Würdig committed
326

327
static arch_irn_flags_t ia32_get_flags(const ir_node *irn) {
328
	arch_irn_flags_t flags = arch_irn_flags_none;
329

Christian Würdig's avatar
Christian Würdig committed
330
	if (is_Unknown(irn))
331
		return arch_irn_flags_ignore;
Christian Würdig's avatar
Christian Würdig committed
332

333
334
335
336
337
338
339
340
341
342
343
344
	if(is_Proj(irn) && mode_is_datab(get_irn_mode(irn))) {
		ir_node *pred = get_Proj_pred(irn);

		if(is_ia32_irn(pred)) {
			flags = get_ia32_out_flags(pred, get_Proj_proj(irn));
		}

		irn = pred;
	}

	if (is_ia32_irn(irn)) {
		flags |= get_ia32_flags(irn);
Christian Würdig's avatar
Christian Würdig committed
345
	}
346
347

	return flags;
Christian Würdig's avatar
Christian Würdig committed
348
349
}

350
351
352
/**
 * The IA32 ABI callback object.
 */
353
typedef struct {
354
355
356
	be_abi_call_flags_bits_t flags;  /**< The call flags. */
	const arch_env_t *aenv;          /**< The architecture environment. */
	ir_graph *irg;                   /**< The associated graph. */
357
358
} ia32_abi_env_t;

359
static ir_entity *ia32_get_frame_entity(const ir_node *irn) {
Christian Würdig's avatar
Christian Würdig committed
360
	return is_ia32_irn(irn) ? get_ia32_frame_ent(irn) : NULL;
361
362
}

363
static void ia32_set_frame_entity(ir_node *irn, ir_entity *ent) {
364
365
366
	set_ia32_frame_ent(irn, ent);
}

367
static void ia32_set_frame_offset(ir_node *irn, int bias)
368
369
370
{
	if (get_ia32_frame_ent(irn) == NULL)
		return;
371

372
	if (is_ia32_Pop(irn) || is_ia32_PopMem(irn)) {
Matthias Braun's avatar
Matthias Braun committed
373
374
		ia32_code_gen_t *cg = ia32_current_cg;
		int omit_fp = be_abi_omit_fp(cg->birg->abi);
375
376
377
378
379
380
		if (omit_fp) {
			/* Pop nodes modify the stack pointer before calculating the
			 * destination address, so fix this here
			 */
			bias -= 4;
		}
381
	}
382
	add_ia32_am_offs_int(irn, bias);
383
384
}

385
static int ia32_get_sp_bias(const ir_node *node)
386
387
388
389
{
	if (is_ia32_Push(node))
		return 4;

390
	if (is_ia32_Pop(node) || is_ia32_PopMem(node))
391
		return -4;
392
393
394
395

	return 0;
}

396
397
398
399
400
401
/**
 * Put all registers which are saved by the prologue/epilogue in a set.
 *
 * @param self  The callback object.
 * @param s     The result set.
 */
Sebastian Hack's avatar
Sebastian Hack committed
402
403
404
405
static void ia32_abi_dont_save_regs(void *self, pset *s)
{
	ia32_abi_env_t *env = self;
	if(env->flags.try_omit_fp)
406
		pset_insert_ptr(s, env->aenv->bp);
Sebastian Hack's avatar
Sebastian Hack committed
407
408
}

409
/**
Michael Beck's avatar
Michael Beck committed
410
 * Generate the routine prologue.
411
 *
412
413
 * @param self    The callback object.
 * @param mem     A pointer to the mem node. Update this if you define new memory.
Michael Beck's avatar
Michael Beck committed
414
 * @param reg_map A map mapping all callee_save/ignore/parameter registers to their defining nodes.
415
 *
416
417
418
419
 * @return        The register which shall be used as a stack frame base.
 *
 * All nodes which define registers in @p reg_map must keep @p reg_map current.
 */
Sebastian Hack's avatar
Sebastian Hack committed
420
static const arch_register_t *ia32_abi_prologue(void *self, ir_node **mem, pmap *reg_map)
Sebastian Hack's avatar
Sebastian Hack committed
421
{
422
423
424
	ia32_abi_env_t   *env      = self;
	ia32_code_gen_t  *cg       = ia32_current_cg;
	const arch_env_t *arch_env = env->aenv;
Sebastian Hack's avatar
Sebastian Hack committed
425

Christian Würdig's avatar
Christian Würdig committed
426
	if (! env->flags.try_omit_fp) {
427
428
429
430
431
432
		ir_graph *irg     =env->irg;
		ir_node  *bl      = get_irg_start_block(irg);
		ir_node  *curr_sp = be_abi_reg_map_get(reg_map, arch_env->sp);
		ir_node  *curr_bp = be_abi_reg_map_get(reg_map, arch_env->bp);
		ir_node  *noreg = ia32_new_NoReg_gp(cg);
		ir_node  *push;
Sebastian Hack's avatar
Sebastian Hack committed
433

434
435
436
		/* ALL nodes representing bp must be set to ignore. */
		be_node_set_flags(get_Proj_pred(curr_bp), BE_OUT_POS(get_Proj_proj(curr_bp)), arch_irn_flags_ignore);

437
		/* push ebp */
438
439
440
		push    = new_rd_ia32_Push(NULL, irg, bl, noreg, noreg, *mem, curr_bp, curr_sp);
		curr_sp = new_r_Proj(irg, bl, push, get_irn_mode(curr_sp), pn_ia32_Push_stack);
		*mem    = new_r_Proj(irg, bl, push, mode_M, pn_ia32_Push_M);
441
442

		/* the push must have SP out register */
443
		arch_set_irn_register(arch_env, curr_sp, arch_env->sp);
Michael Beck's avatar
Michael Beck committed
444
		set_ia32_flags(push, arch_irn_flags_ignore);
445
446

		/* move esp to ebp */
447
		curr_bp  = be_new_Copy(arch_env->bp->reg_class, irg, bl, curr_sp);
448
449
		be_set_constr_single_reg(curr_bp, BE_OUT_POS(0), arch_env->bp);
		arch_set_irn_register(arch_env, curr_bp, arch_env->bp);
Christian Würdig's avatar
Christian Würdig committed
450
		be_node_set_flags(curr_bp, BE_OUT_POS(0), arch_irn_flags_ignore);
Sebastian Hack's avatar
Sebastian Hack committed
451

452
		/* beware: the copy must be done before any other sp use */
453
		curr_sp = be_new_CopyKeep_single(arch_env->sp->reg_class, irg, bl, curr_sp, curr_bp, get_irn_mode(curr_sp));
454
455
		be_set_constr_single_reg(curr_sp, BE_OUT_POS(0), arch_env->sp);
		arch_set_irn_register(arch_env, curr_sp, arch_env->sp);
456
457
		be_node_set_flags(curr_sp, BE_OUT_POS(0), arch_irn_flags_ignore);

458
459
		be_abi_reg_map_set(reg_map, arch_env->sp, curr_sp);
		be_abi_reg_map_set(reg_map, arch_env->bp, curr_bp);
460

461
		return arch_env->bp;
Sebastian Hack's avatar
Sebastian Hack committed
462
463
	}

464
	return arch_env->sp;
Sebastian Hack's avatar
Sebastian Hack committed
465
466
}

Michael Beck's avatar
Michael Beck committed
467
468
469
/**
 * Generate the routine epilogue.
 * @param self    The callback object.
470
 * @param bl      The block for the epilog
Michael Beck's avatar
Michael Beck committed
471
472
473
474
475
476
 * @param mem     A pointer to the mem node. Update this if you define new memory.
 * @param reg_map A map mapping all callee_save/ignore/parameter registers to their defining nodes.
 * @return        The register which shall be used as a stack frame base.
 *
 * All nodes which define registers in @p reg_map must keep @p reg_map current.
 */
Sebastian Hack's avatar
Sebastian Hack committed
477
478
static void ia32_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map)
{
479
	ia32_abi_env_t   *env      = self;
480
	const arch_env_t *arch_env = env->aenv;
481
482
483
	ir_node          *curr_sp  = be_abi_reg_map_get(reg_map, arch_env->sp);
	ir_node          *curr_bp  = be_abi_reg_map_get(reg_map, arch_env->bp);
	ir_graph         *irg      = env->irg;
Sebastian Hack's avatar
Sebastian Hack committed
484

485
486
	if (env->flags.try_omit_fp) {
		/* simply remove the stack frame here */
487
		curr_sp = be_new_IncSP(arch_env->sp, irg, bl, curr_sp, BE_STACK_FRAME_SIZE_SHRINK, 0);
Sebastian Hack's avatar
Sebastian Hack committed
488
		add_irn_dep(curr_sp, *mem);
489
	} else {
490
		ir_mode *mode_bp = arch_env->bp->reg_class->mode;
Sebastian Hack's avatar
Sebastian Hack committed
491

492
		if (ia32_cg_config.use_leave) {
493
			ir_node *leave;
494

495
			/* leave */
496
			leave   = new_rd_ia32_Leave(NULL, irg, bl, curr_sp, curr_bp);
497
			set_ia32_flags(leave, arch_irn_flags_ignore);
Christoph Mallon's avatar
Christoph Mallon committed
498
499
			curr_bp = new_r_Proj(irg, bl, leave, mode_bp, pn_ia32_Leave_frame);
			curr_sp = new_r_Proj(irg, bl, leave, get_irn_mode(curr_sp), pn_ia32_Leave_stack);
500
		} else {
501
502
			ir_node *pop;

Christoph Mallon's avatar
Christoph Mallon committed
503
504
505
506
			/* the old SP is not needed anymore (kill the proj) */
			assert(is_Proj(curr_sp));
			be_kill_node(curr_sp);

507
			/* copy ebp to esp */
Christoph Mallon's avatar
Christoph Mallon committed
508
			curr_sp = be_new_Copy(&ia32_reg_classes[CLASS_ia32_gp], irg, bl, curr_bp);
509
			arch_set_irn_register(arch_env, curr_sp, arch_env->sp);
Christoph Mallon's avatar
Christoph Mallon committed
510
			be_node_set_flags(curr_sp, BE_OUT_POS(0), arch_irn_flags_ignore);
511
512

			/* pop ebp */
513
			pop     = new_rd_ia32_Pop(NULL, env->irg, bl, *mem, curr_sp);
514
			set_ia32_flags(pop, arch_irn_flags_ignore);
Christoph Mallon's avatar
Christoph Mallon committed
515
516
			curr_bp = new_r_Proj(irg, bl, pop, mode_bp, pn_ia32_Pop_res);
			curr_sp = new_r_Proj(irg, bl, pop, get_irn_mode(curr_sp), pn_ia32_Pop_stack);
517

Christoph Mallon's avatar
Christoph Mallon committed
518
			*mem = new_r_Proj(irg, bl, pop, mode_M, pn_ia32_Pop_M);
519
		}
520
521
		arch_set_irn_register(arch_env, curr_sp, arch_env->sp);
		arch_set_irn_register(arch_env, curr_bp, arch_env->bp);
Sebastian Hack's avatar
Sebastian Hack committed
522
523
	}

524
525
	be_abi_reg_map_set(reg_map, arch_env->sp, curr_sp);
	be_abi_reg_map_set(reg_map, arch_env->bp, curr_bp);
Sebastian Hack's avatar
Sebastian Hack committed
526
527
}

528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/**
 * Initialize the callback object.
 * @param call The call object.
 * @param aenv The architecture environment.
 * @param irg  The graph with the method.
 * @return     Some pointer. This pointer is passed to all other callback functions as self object.
 */
static void *ia32_abi_init(const be_abi_call_t *call, const arch_env_t *aenv, ir_graph *irg)
{
	ia32_abi_env_t *env    = xmalloc(sizeof(env[0]));
	be_abi_call_flags_t fl = be_abi_call_get_flags(call);
	env->flags = fl.bits;
	env->irg   = irg;
	env->aenv  = aenv;
	return env;
}

/**
 * Destroy the callback object.
 * @param self The callback object.
 */
static void ia32_abi_done(void *self) {
	free(self);
}

Sebastian Hack's avatar
Sebastian Hack committed
553
554
555
556
557
558
559
/**
 * Produces the type which sits between the stack args and the locals on the stack.
 * it will contain the return address and space to store the old base pointer.
 * @return The Firm type modeling the ABI between type.
 */
static ir_type *ia32_abi_get_between_type(void *self)
{
560
#define IDENT(s) new_id_from_chars(s, sizeof(s)-1)
Sebastian Hack's avatar
Sebastian Hack committed
561
562
563
564
565
	static ir_type *omit_fp_between_type = NULL;
	static ir_type *between_type         = NULL;

	ia32_abi_env_t *env = self;

566
	if (! between_type) {
567
568
569
		ir_entity *old_bp_ent;
		ir_entity *ret_addr_ent;
		ir_entity *omit_fp_ret_addr_ent;
Sebastian Hack's avatar
Sebastian Hack committed
570

571
572
		ir_type *old_bp_type   = new_type_primitive(IDENT("bp"), mode_Iu);
		ir_type *ret_addr_type = new_type_primitive(IDENT("return_addr"), mode_Iu);
Sebastian Hack's avatar
Sebastian Hack committed
573

574
575
576
		between_type           = new_type_struct(IDENT("ia32_between_type"));
		old_bp_ent             = new_entity(between_type, IDENT("old_bp"), old_bp_type);
		ret_addr_ent           = new_entity(between_type, IDENT("ret_addr"), ret_addr_type);
Sebastian Hack's avatar
Sebastian Hack committed
577

578
579
		set_entity_offset(old_bp_ent, 0);
		set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
Sebastian Hack's avatar
Sebastian Hack committed
580
		set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
581
		set_type_state(between_type, layout_fixed);
Sebastian Hack's avatar
Sebastian Hack committed
582

Christian Würdig's avatar
Christian Würdig committed
583
584
		omit_fp_between_type = new_type_struct(IDENT("ia32_between_type_omit_fp"));
		omit_fp_ret_addr_ent = new_entity(omit_fp_between_type, IDENT("ret_addr"), ret_addr_type);
Sebastian Hack's avatar
Sebastian Hack committed
585

586
		set_entity_offset(omit_fp_ret_addr_ent, 0);
Sebastian Hack's avatar
Sebastian Hack committed
587
		set_type_size_bytes(omit_fp_between_type, get_type_size_bytes(ret_addr_type));
588
		set_type_state(omit_fp_between_type, layout_fixed);
Sebastian Hack's avatar
Sebastian Hack committed
589
590
591
	}

	return env->flags.try_omit_fp ? omit_fp_between_type : between_type;
592
#undef IDENT
Sebastian Hack's avatar
Sebastian Hack committed
593
594
}

595
596
597
598
599
600
601
602
/**
 * Get the estimated cycle count for @p irn.
 *
 * @param self The this pointer.
 * @param irn  The node.
 *
 * @return     The estimated cycle count for this operation
 */
603
static int ia32_get_op_estimated_cost(const ir_node *irn)
604
{
605
	int            cost;
Christian Würdig's avatar
Christian Würdig committed
606
	ia32_op_type_t op_tp;
Adam Szalkowski's avatar
Adam Szalkowski committed
607

Christian Würdig's avatar
Christian Würdig committed
608
	if (is_Proj(irn))
609
610
611
		return 0;
	if (!is_ia32_irn(irn))
		return 0;
Adam Szalkowski's avatar
Adam Szalkowski committed
612

Christian Würdig's avatar
Christian Würdig committed
613
614
615
616
617
618
619
620
621
	assert(is_ia32_irn(irn));

	cost  = get_ia32_latency(irn);
	op_tp = get_ia32_op_type(irn);

	if (is_ia32_CopyB(irn)) {
		cost = 250;
	}
	else if (is_ia32_CopyB_i(irn)) {
Michael Beck's avatar
Michael Beck committed
622
		int size = get_ia32_copyb_size(irn);
Christian Würdig's avatar
Christian Würdig committed
623
624
625
626
627
		cost     = 20 + (int)ceil((4/3) * size);
	}
	/* in case of address mode operations add additional cycles */
	else if (op_tp == ia32_AddrModeD || op_tp == ia32_AddrModeS) {
		/*
628
629
630
			In case of stack access and access to fixed addresses add 5 cycles
			(we assume they are in cache), other memory operations cost 20
			cycles.
Christian Würdig's avatar
Christian Würdig committed
631
		*/
632
633
634
635
636
637
638
		if(is_ia32_use_frame(irn) ||
				(is_ia32_NoReg_GP(get_irn_n(irn, 0)) &&
		         is_ia32_NoReg_GP(get_irn_n(irn, 1)))) {
			cost += 5;
		} else {
			cost += 20;
		}
Christian Würdig's avatar
Christian Würdig committed
639
640
641
	}

	return cost;
642
643
}

Christian Würdig's avatar
Christian Würdig committed
644
645
646
647
648
649
650
651
652
/**
 * Returns the inverse operation if @p irn, recalculating the argument at position @p i.
 *
 * @param irn       The original operation
 * @param i         Index of the argument we want the inverse operation to yield
 * @param inverse   struct to be filled with the resulting inverse op
 * @param obstack   The obstack to use for allocation of the returned nodes array
 * @return          The inverse operation or NULL if operation invertible
 */
653
static arch_inverse_t *ia32_get_inverse(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obst) {
654
655
	ir_graph *irg;
	ir_mode  *mode;
656
	ir_mode  *irn_mode;
657
	ir_node  *block, *noreg, *nomem;
658
	dbg_info *dbg;
659
660
661
662
663
664

	/* we cannot invert non-ia32 irns */
	if (! is_ia32_irn(irn))
		return NULL;

	/* operand must always be a real operand (not base, index or mem) */
665
	if (i != n_ia32_binary_left && i != n_ia32_binary_right)
666
667
668
669
670
671
		return NULL;

	/* we don't invert address mode operations */
	if (get_ia32_op_type(irn) != ia32_Normal)
		return NULL;

672
673
674
675
676
	/* TODO: adjust for new immediates... */
	ir_fprintf(stderr, "TODO: fix get_inverse for new immediates (%+F)\n",
	           irn);
	return NULL;

677
678
	irg      = get_irn_irg(irn);
	block    = get_nodes_block(irn);
679
	mode     = get_irn_mode(irn);
680
681
682
683
	irn_mode = get_irn_mode(irn);
	noreg    = get_irn_n(irn, 0);
	nomem    = new_r_NoMem(irg);
	dbg      = get_irn_dbg_info(irn);
684
685

	/* initialize structure */
686
	inverse->nodes = obstack_alloc(obst, 2 * sizeof(inverse->nodes[0]));
687
	inverse->costs = 0;
688
	inverse->n     = 1;
689

690
	switch (get_ia32_irn_opcode(irn)) {
691
		case iro_ia32_Add:
692
#if 0
693
694
695
			if (get_ia32_immop_type(irn) == ia32_ImmConst) {
				/* we have an add with a const here */
				/* invers == add with negated const */
696
				inverse->nodes[0] = new_rd_ia32_Add(dbg, irg, block, noreg, noreg, nomem, get_irn_n(irn, i), noreg);
697
698
699
700
701
702
703
704
				inverse->costs   += 1;
				copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
				set_ia32_Immop_tarval(inverse->nodes[0], tarval_neg(get_ia32_Immop_tarval(irn)));
				set_ia32_commutative(inverse->nodes[0]);
			}
			else if (get_ia32_immop_type(irn) == ia32_ImmSymConst) {
				/* we have an add with a symconst here */
				/* invers == sub with const */
705
				inverse->nodes[0] = new_rd_ia32_Sub(dbg, irg, block, noreg, noreg, nomem, get_irn_n(irn, i), noreg);
706
				inverse->costs   += 2;
707
708
709
710
				copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
			}
			else {
				/* normal add: inverse == sub */
711
				inverse->nodes[0] = new_rd_ia32_Sub(dbg, irg, block, noreg, noreg, nomem, (ir_node*) irn, get_irn_n(irn, i ^ 1));
712
				inverse->costs   += 2;
713
			}
714
#endif
715
716
			break;
		case iro_ia32_Sub:
717
#if 0
718
719
720
			if (get_ia32_immop_type(irn) != ia32_ImmNone) {
				/* we have a sub with a const/symconst here */
				/* invers == add with this const */
721
				inverse->nodes[0] = new_rd_ia32_Add(dbg, irg, block, noreg, noreg, nomem, get_irn_n(irn, i), noreg);
722
723
724
725
726
				inverse->costs   += (get_ia32_immop_type(irn) == ia32_ImmSymConst) ? 5 : 1;
				copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
			}
			else {
				/* normal sub */
727
728
				if (i == n_ia32_binary_left) {
					inverse->nodes[0] = new_rd_ia32_Add(dbg, irg, block, noreg, noreg, nomem, (ir_node*) irn, get_irn_n(irn, 3));
729
730
				}
				else {
731
					inverse->nodes[0] = new_rd_ia32_Sub(dbg, irg, block, noreg, noreg, nomem, get_irn_n(irn, n_ia32_binary_left), (ir_node*) irn);
732
733
734
				}
				inverse->costs += 1;
			}
735
#endif
736
			break;
737
		case iro_ia32_Xor:
738
#if 0
739
740
			if (get_ia32_immop_type(irn) != ia32_ImmNone) {
				/* xor with const: inverse = xor */
741
				inverse->nodes[0] = new_rd_ia32_Xor(dbg, irg, block, noreg, noreg, nomem, get_irn_n(irn, i), noreg);
742
743
744
745
746
				inverse->costs   += (get_ia32_immop_type(irn) == ia32_ImmSymConst) ? 5 : 1;
				copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
			}
			else {
				/* normal xor */
747
				inverse->nodes[0] = new_rd_ia32_Xor(dbg, irg, block, noreg, noreg, nomem, (ir_node *) irn, get_irn_n(irn, i));
748
749
				inverse->costs   += 1;
			}
750
#endif
751
			break;
752
		case iro_ia32_Not: {
753
			inverse->nodes[0] = new_rd_ia32_Not(dbg, irg, block, (ir_node*) irn);
754
755
			inverse->costs   += 1;
			break;
756
		}
757
		case iro_ia32_Neg: {
758
			inverse->nodes[0] = new_rd_ia32_Neg(dbg, irg, block, (ir_node*) irn);
759
760
			inverse->costs   += 1;
			break;
761
		}
762
763
764
765
766
767
		default:
			/* inverse operation not supported */
			return NULL;
	}

	return inverse;
Christian Würdig's avatar
Christian Würdig committed
768
769
}

770
771
772
773
774
775
776
777
static ir_mode *get_spill_mode_mode(const ir_mode *mode)
{
	if(mode_is_float(mode))
		return mode_D;

	return mode_Iu;
}

778
779
780
/**
 * Get the mode that should be used for spilling value node
 */
781
static ir_mode *get_spill_mode(const ir_node *node)
782
783
{
	ir_mode *mode = get_irn_mode(node);
784
	return get_spill_mode_mode(mode);
785
786
787
}

/**
Michael Beck's avatar
Michael Beck committed
788
 * Checks whether an addressmode reload for a node with mode mode is compatible
789
790
791
792
793
794
795
796
797
798
799
 * with a spillslot of mode spill_mode
 */
static int ia32_is_spillmode_compatible(const ir_mode *mode, const ir_mode *spillmode)
{
	if(mode_is_float(mode)) {
		return mode == spillmode;
	} else {
		return 1;
	}
}

800
/**
Christoph Mallon's avatar
Christoph Mallon committed
801
 * Check if irn can load its operand at position i from memory (source addressmode).
802
803
804
805
806
 * @param self   Pointer to irn ops itself
 * @param irn    The irn to be checked
 * @param i      The operands position
 * @return Non-Zero if operand can be loaded
 */
807
static int ia32_possible_memory_operand(const ir_node *irn, unsigned int i) {
808
809
	ir_node *op = get_irn_n(irn, i);
	const ir_mode *mode = get_irn_mode(op);
810
	const ir_mode *spillmode = get_spill_mode(op);
811

812
813
814
	if (
		(i != n_ia32_binary_left && i != n_ia32_binary_right) || /* a "real" operand position must be requested */
		! is_ia32_irn(irn)                                    ||  /* must be an ia32 irn */
815
		get_ia32_am_arity(irn) != ia32_am_binary              ||  /* must be a binary operation TODO is this necessary? */
816
817
818
819
		get_ia32_op_type(irn) != ia32_Normal                  ||  /* must not already be a addressmode irn */
		! (get_ia32_am_support(irn) & ia32_am_Source)         ||  /* must be capable of source addressmode */
		! ia32_is_spillmode_compatible(mode, spillmode)       ||
		is_ia32_use_frame(irn))                                  /* must not already use frame */
820
821
		return 0;

822
	if (i == n_ia32_binary_left) {
823
824
825
826
827
828
		const arch_register_req_t *req;
		if(!is_ia32_commutative(irn))
			return 0;
		/* we can't swap left/right for limited registers
		 * (As this (currently) breaks constraint handling copies)
		 */
829
		req = get_ia32_in_req(irn, n_ia32_binary_left);
830
		if (req->type & arch_register_req_type_limited) {
831
832
833
834
			return 0;
		}
	}

835
836
837
	return 1;
}

838
839
static void ia32_perform_memory_operand(ir_node *irn, ir_node *spill,
                                        unsigned int i)
840
{
Matthias Braun's avatar
Matthias Braun committed
841
	ia32_code_gen_t *cg = ia32_current_cg;
842

843
	assert(ia32_possible_memory_operand(irn, i) && "Cannot perform memory operand change");
844

845
	if (i == n_ia32_binary_left) {
846
		ia32_swap_left_right(irn);
847
848
849
	}

	set_ia32_op_type(irn, ia32_AddrModeS);
850
	set_ia32_ls_mode(irn, get_irn_mode(get_irn_n(irn, i)));
851
	set_ia32_use_frame(irn);
852
	set_ia32_need_stackent(irn);
853

854
	set_irn_n(irn, n_ia32_base, get_irg_frame(get_irn_irg(irn)));
Matthias Braun's avatar
Matthias Braun committed
855
	set_irn_n(irn, n_ia32_binary_right, ia32_get_admissible_noreg(cg, irn, n_ia32_binary_right));
856
	set_irn_n(irn, n_ia32_mem, spill);
857

858
	/* immediates are only allowed on the right side */
859
	if (i == n_ia32_binary_left && is_ia32_Immediate(get_irn_n(irn, n_ia32_binary_left))) {
860
		ia32_swap_left_right(irn);
861
	}
862
863
}

Sebastian Hack's avatar
Sebastian Hack committed
864
865
static const be_abi_callbacks_t ia32_abi_callbacks = {
	ia32_abi_init,
866
	ia32_abi_done,
Sebastian Hack's avatar
Sebastian Hack committed
867
868
869
	ia32_abi_get_between_type,
	ia32_abi_dont_save_regs,
	ia32_abi_prologue,
870
	ia32_abi_epilogue
Sebastian Hack's avatar
Sebastian Hack committed
871
872
};

Christian Würdig's avatar
Christian Würdig committed
873
874
/* fill register allocator interface */

Matthias Braun's avatar
Matthias Braun committed
875
static const arch_irn_ops_t ia32_irn_ops = {
Christian Würdig's avatar
Christian Würdig committed
876
877
878
879
	ia32_get_irn_reg_req,
	ia32_set_irn_reg,
	ia32_get_irn_reg,
	ia32_classify,
880
	ia32_get_flags,
881
	ia32_get_frame_entity,
882
	ia32_set_frame_entity,
883
884
	ia32_set_frame_offset,
	ia32_get_sp_bias,
885
	ia32_get_inverse,
886
887
888
	ia32_get_op_estimated_cost,
	ia32_possible_memory_operand,
	ia32_perform_memory_operand,
Christian Würdig's avatar
Christian Würdig committed
889
890
891
892
893
894
895
896
897
898
899
900
901
};

/**************************************************
 *                _                         _  __
 *               | |                       (_)/ _|
 *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
 *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
 * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
 *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
 *                        __/ |
 *                       |___/
 **************************************************/

Michael Beck's avatar
Michael Beck committed
902
903
904
905
static ir_entity *mcount = NULL;

#define ID(s) new_id_from_chars(s, sizeof(s) - 1)

906
static void ia32_before_abi(void *self) {
Matthias Braun's avatar
Matthias Braun committed
907
908
909
910
911
	lower_mode_b_config_t lower_mode_b_config = {
		mode_Iu,  /* lowered mode */
		mode_Bu,  /* prefered mode for set */
		0,        /* don't lower direct compares */
	};
Christian Würdig's avatar
Christian Würdig committed
912
	ia32_code_gen_t *cg = self;
913

Matthias Braun's avatar
Matthias Braun committed
914
	ir_lower_mode_b(cg->irg, &lower_mode_b_config);
Michael Beck's avatar
Michael Beck committed
915
	if (cg->dump)
916
		be_dump(cg->irg, "-lower_modeb", dump_ir_block_graph_sched);
Michael Beck's avatar
Michael Beck committed
917
918
919
920
921
922
923
924
925
926
	if (cg->gprof)