bearch_ia32.c 67.1 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"
42
#include "irflag.h"
Christian Würdig's avatar
Christian Würdig committed
43
#include "irgmod.h"
Christian Würdig's avatar
Christian Würdig committed
44
#include "irgopt.h"
45
#include "irbitset.h"
46
#include "irgopt.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"
79
#include "ia32_common_transform.h"
Christian Würdig's avatar
Christian Würdig committed
80
#include "ia32_transform.h"
Christian Würdig's avatar
Christian Würdig committed
81
82
#include "ia32_emitter.h"
#include "ia32_map_regs.h"
Christian Würdig's avatar
Christian Würdig committed
83
#include "ia32_optimize.h"
84
#include "ia32_x87.h"
85
#include "ia32_dbg_stat.h"
Christian Würdig's avatar
Christian Würdig committed
86
#include "ia32_finish.h"
87
#include "ia32_util.h"
88
#include "ia32_fpu.h"
89
#include "ia32_architecture.h"
90

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

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

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

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

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
 * 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 */
};


121
122
123
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,
124
                                    create_const_node_func func,
125
                                    const arch_register_t* reg)
126
127
128
129
130
131
132
133
134
135
136
{
	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;

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

	return res;
}

143
/* Creates the unique per irg GP NoReg node. */
144
ir_node *ia32_new_NoReg_gp(ia32_code_gen_t *cg) {
145
146
147
148
149
150
151
152
153
154
155
156
	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]);
157
158
}

159
160
161
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]);
162
}
163

164
165
166
167
168
169
170
171
172
173
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]);
}

174
175
176
177
178
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]);
}

179

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

Matthias Braun's avatar
Matthias Braun committed
186
187
188
	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])
189
		return ia32_new_NoReg_gp(cg);
Matthias Braun's avatar
Matthias Braun committed
190

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

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

Christian Würdig's avatar
Christian Würdig committed
209
210
211
212
213
/**
 * 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.
 */
214
static const arch_register_req_t *ia32_get_irn_reg_req(const ir_node *node,
215
216
													   int pos)
{
Michael Beck's avatar
Michael Beck committed
217
218
	ir_mode *mode = get_irn_mode(node);
	long    node_pos;
Matthias Braun's avatar
Matthias Braun committed
219

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

308
	irn = skip_Proj_const(irn);
309

Christian Würdig's avatar
Christian Würdig committed
310
	if (is_cfop(irn))
311
312
313
314
315
316
317
318
		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;

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

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

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

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

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

334
335
336
337
338
339
340
341
342
343
344
345
	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
346
	}
347
348

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

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

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

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

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

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

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

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

	return 0;
}

397
398
399
400
401
402
/**
 * 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
403
404
405
406
static void ia32_abi_dont_save_regs(void *self, pset *s)
{
	ia32_abi_env_t *env = self;
	if(env->flags.try_omit_fp)
407
		pset_insert_ptr(s, env->aenv->bp);
Sebastian Hack's avatar
Sebastian Hack committed
408
409
}

410
/**
Michael Beck's avatar
Michael Beck committed
411
 * Generate the routine prologue.
412
 *
413
414
415
416
 * @param self       The callback object.
 * @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.
 * @param stack_bias Points to the current stack bias, can be modified if needed.
417
 *
418
 * @return           The register which shall be used as a stack frame base.
419
420
421
 *
 * All nodes which define registers in @p reg_map must keep @p reg_map current.
 */
422
static const arch_register_t *ia32_abi_prologue(void *self, ir_node **mem, pmap *reg_map, int *stack_bias)
Sebastian Hack's avatar
Sebastian Hack committed
423
{
424
425
426
	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
427

Christian Würdig's avatar
Christian Würdig committed
428
	if (! env->flags.try_omit_fp) {
429
430
431
432
433
434
		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
435

436
437
438
		/* 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);

439
		/* push ebp */
440
441
442
		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);
443
444

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

448
449
450
		/* this modifies the stack bias, because we pushed 32bit */
		*stack_bias -= 4;

451
		/* move esp to ebp */
452
		curr_bp  = be_new_Copy(arch_env->bp->reg_class, irg, bl, curr_sp);
453
454
		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
455
		be_node_set_flags(curr_bp, BE_OUT_POS(0), arch_irn_flags_ignore);
Sebastian Hack's avatar
Sebastian Hack committed
456

457
		/* beware: the copy must be done before any other sp use */
458
		curr_sp = be_new_CopyKeep_single(arch_env->sp->reg_class, irg, bl, curr_sp, curr_bp, get_irn_mode(curr_sp));
459
460
		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);
461
462
		be_node_set_flags(curr_sp, BE_OUT_POS(0), arch_irn_flags_ignore);

463
464
		be_abi_reg_map_set(reg_map, arch_env->sp, curr_sp);
		be_abi_reg_map_set(reg_map, arch_env->bp, curr_bp);
465

466
		return arch_env->bp;
Sebastian Hack's avatar
Sebastian Hack committed
467
468
	}

469
	return arch_env->sp;
Sebastian Hack's avatar
Sebastian Hack committed
470
471
}

Michael Beck's avatar
Michael Beck committed
472
473
474
/**
 * Generate the routine epilogue.
 * @param self    The callback object.
475
 * @param bl      The block for the epilog
Michael Beck's avatar
Michael Beck committed
476
477
478
479
480
481
 * @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
482
483
static void ia32_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map)
{
484
	ia32_abi_env_t   *env      = self;
485
	const arch_env_t *arch_env = env->aenv;
486
487
488
	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
489

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

496
		if (ia32_cg_config.use_leave) {
497
			ir_node *leave;
498

499
			/* leave */
500
			leave   = new_rd_ia32_Leave(NULL, irg, bl, curr_sp, curr_bp);
501
			set_ia32_flags(leave, arch_irn_flags_ignore);
Christoph Mallon's avatar
Christoph Mallon committed
502
503
			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);
504
		} else {
505
506
			ir_node *pop;

Christoph Mallon's avatar
Christoph Mallon committed
507
508
			/* the old SP is not needed anymore (kill the proj) */
			assert(is_Proj(curr_sp));
509
			kill_node(curr_sp);
Christoph Mallon's avatar
Christoph Mallon committed
510

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

			/* pop ebp */
517
			pop     = new_rd_ia32_Pop(NULL, env->irg, bl, *mem, curr_sp);
518
			set_ia32_flags(pop, arch_irn_flags_ignore);
Christoph Mallon's avatar
Christoph Mallon committed
519
520
			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);
521

Christoph Mallon's avatar
Christoph Mallon committed
522
			*mem = new_r_Proj(irg, bl, pop, mode_M, pn_ia32_Pop_M);
523
		}
524
525
		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
526
527
	}

528
529
	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
530
531
}

532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/**
 * 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
557
558
559
560
561
562
563
/**
 * 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)
{
564
#define IDENT(s) new_id_from_chars(s, sizeof(s)-1)
565
566
567
568
	static ir_type *omit_fp_between_type = NULL;
	static ir_type *between_type         = NULL;

	ia32_abi_env_t *env = self;
Sebastian Hack's avatar
Sebastian Hack committed
569

570
	if (! between_type) {
571
		ir_entity *old_bp_ent;
572
		ir_entity *ret_addr_ent;
573
574
575
576
		ir_entity *omit_fp_ret_addr_ent;

		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
577

578
579
580
		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
581

582
583
584
		set_entity_offset(old_bp_ent, 0);
		set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
		set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
585
		set_type_state(between_type, layout_fixed);
586
587
588
589
590
591
592

		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);

		set_entity_offset(omit_fp_ret_addr_ent, 0);
		set_type_size_bytes(omit_fp_between_type, get_type_size_bytes(ret_addr_type));
		set_type_state(omit_fp_between_type, layout_fixed);
Sebastian Hack's avatar
Sebastian Hack committed
593
594
	}

595
	return env->flags.try_omit_fp ? omit_fp_between_type : between_type;
596
#undef IDENT
Sebastian Hack's avatar
Sebastian Hack committed
597
598
}

599
600
601
602
603
604
605
606
/**
 * 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
 */
607
static int ia32_get_op_estimated_cost(const ir_node *irn)
608
{
609
	int            cost;
Christian Würdig's avatar
Christian Würdig committed
610
	ia32_op_type_t op_tp;
Adam Szalkowski's avatar
Adam Szalkowski committed
611

Christian Würdig's avatar
Christian Würdig committed
612
	if (is_Proj(irn))
613
614
615
		return 0;
	if (!is_ia32_irn(irn))
		return 0;
Adam Szalkowski's avatar
Adam Szalkowski committed
616

Christian Würdig's avatar
Christian Würdig committed
617
618
619
620
621
622
623
624
625
	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
626
		int size = get_ia32_copyb_size(irn);
Christian Würdig's avatar
Christian Würdig committed
627
628
629
630
631
		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) {
		/*
632
633
634
			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
635
		*/
636
637
638
639
		if (is_ia32_use_frame(irn) || (
		    	is_ia32_NoReg_GP(get_irn_n(irn, n_ia32_base)) &&
		    	is_ia32_NoReg_GP(get_irn_n(irn, n_ia32_index))
		    )) {
640
641
642
643
			cost += 5;
		} else {
			cost += 20;
		}
Christian Würdig's avatar
Christian Würdig committed
644
645
646
	}

	return cost;
647
648
}

Christian Würdig's avatar
Christian Würdig committed
649
650
651
652
653
654
655
656
657
/**
 * 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
 */
658
static arch_inverse_t *ia32_get_inverse(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obst) {
659
660
	ir_graph *irg;
	ir_mode  *mode;
661
	ir_mode  *irn_mode;
662
	ir_node  *block, *noreg, *nomem;
663
	dbg_info *dbg;
664
665
666
667
668
669

	/* 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) */
670
	if (i != n_ia32_binary_left && i != n_ia32_binary_right)
671
672
673
674
675
676
		return NULL;

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

677
678
679
680
681
	/* TODO: adjust for new immediates... */
	ir_fprintf(stderr, "TODO: fix get_inverse for new immediates (%+F)\n",
	           irn);
	return NULL;

682
683
	irg      = get_irn_irg(irn);
	block    = get_nodes_block(irn);
684
	mode     = get_irn_mode(irn);
685
686
687
688
	irn_mode = get_irn_mode(irn);
	noreg    = get_irn_n(irn, 0);
	nomem    = new_r_NoMem(irg);
	dbg      = get_irn_dbg_info(irn);
689
690

	/* initialize structure */
691
	inverse->nodes = obstack_alloc(obst, 2 * sizeof(inverse->nodes[0]));
692
	inverse->costs = 0;
693
	inverse->n     = 1;
694

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

	return inverse;
Christian Würdig's avatar
Christian Würdig committed
773
774
}

775
776
777
778
779
780
781
782
static ir_mode *get_spill_mode_mode(const ir_mode *mode)
{
	if(mode_is_float(mode))
		return mode_D;

	return mode_Iu;
}

783
784
785
/**
 * Get the mode that should be used for spilling value node
 */
786
static ir_mode *get_spill_mode(const ir_node *node)
787
788
{
	ir_mode *mode = get_irn_mode(node);
789
	return get_spill_mode_mode(mode);
790
791
792
}

/**
Michael Beck's avatar
Michael Beck committed
793
 * Checks whether an addressmode reload for a node with mode mode is compatible
794
795
796
797
798
799
800
801
802
803
804
 * 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;
	}
}

805
/**
Christoph Mallon's avatar
Christoph Mallon committed
806
 * Check if irn can load its operand at position i from memory (source addressmode).
807
808
809
810
811
 * @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
 */
812
static int ia32_possible_memory_operand(const ir_node *irn, unsigned int i) {
813
814
	ir_node *op = get_irn_n(irn, i);
	const ir_mode *mode = get_irn_mode(op);
815
	const ir_mode *spillmode = get_spill_mode(op);
816

817
818
819
	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 */
820
		get_ia32_am_arity(irn) != ia32_am_binary              ||  /* must be a binary operation TODO is this necessary? */
821
822
823
824
		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 */
825
826
		return 0;

827
	if (i == n_ia32_binary_left) {
828
829
830
831
832
833
		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)
		 */
834
		req = get_ia32_in_req(irn, n_ia32_binary_left);
835
		if (req->type & arch_register_req_type_limited) {
836
837
838
839
			return 0;
		}
	}

840
841
842
	return 1;
}

843
844
static void ia32_perform_memory_operand(ir_node *irn, ir_node *spill,
                                        unsigned int i)
845
{
846
847
848
	ir_mode *load_mode;
	ir_mode *dest_op_mode;

Matthias Braun's avatar
Matthias Braun committed
849
	ia32_code_gen_t *cg = ia32_current_cg;
850

851
	assert(ia32_possible_memory_operand(irn, i) && "Cannot perform memory operand change");
852

853
	if (i == n_ia32_binary_left) {
854
		ia32_swap_left_right(irn);
855
856
857
	}

	set_ia32_op_type(irn, ia32_AddrModeS);
858
859
860
861
862
863

	load_mode    = get_irn_mode(get_irn_n(irn, i));
	dest_op_mode = get_ia32_ls_mode(irn);
	if (get_mode_size_bits(load_mode) <= get_mode_size_bits(dest_op_mode)) {
		set_ia32_ls_mode(irn, load_mode);
	}
864
	set_ia32_use_frame(irn);
865
	set_ia32_need_stackent(irn);
866

867
	set_irn_n(irn, n_ia32_base, get_irg_frame(get_irn_irg(irn)));
Matthias Braun's avatar
Matthias Braun committed
868
	set_irn_n(irn, n_ia32_binary_right, ia32_get_admissible_noreg(cg, irn, n_ia32_binary_right));
869
	set_irn_n(irn, n_ia32_mem, spill);
870
	set_ia32_is_reload(irn);
871

872
	/* immediates are only allowed on the right side */
873
	if (i == n_ia32_binary_left && is_ia32_Immediate(get_irn_n(irn, n_ia32_binary_left))) {
874
		ia32_swap_left_right(irn);
875
	}
876
877
}

Sebastian Hack's avatar
Sebastian Hack committed
878
879
static const be_abi_callbacks_t ia32_abi_callbacks = {
	ia32_abi_init,
880
	ia32_abi_done,
Sebastian Hack's avatar
Sebastian Hack committed
881
882
883
	ia32_abi_get_between_type,
	ia32_abi_dont_save_regs,
	ia32_abi_prologue,
884
	ia32_abi_epilogue
Sebastian Hack's avatar
Sebastian Hack committed
885
886
};

Christian Würdig's avatar
Christian Würdig committed
887
888
/* fill register allocator interface */

Matthias Braun's avatar
Matthias Braun committed
889
static const arch_irn_ops_t ia32_irn_ops = {
Christian Würdig's avatar
Christian Würdig committed
890
891
892
893
	ia32_get_irn_reg_req,
	ia32_set_irn_reg,
	ia32_get_irn_reg,
	ia32_classify,
894
	ia32_get_flags,
895
	ia32_get_frame_entity,
896
	ia32_set_frame_entity,
897
898
	ia32_set_frame_offset,
	ia32_get_sp_bias,
899
	ia32_get_inverse,
900
901
902
	ia32_get_op_estimated_cost,
	ia32_possible_memory_operand,
	ia32_perform_memory_operand,
Christian Würdig's avatar
Christian Würdig committed
903
904
905
906
907
908
909
910
911
912
913
914
915
};

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

Michael Beck's avatar
Michael Beck committed
916
917
918
919
static ir_entity *mcount = NULL;

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

920
static void ia32_before_abi(void *self) {
Matthias Braun's avatar
Matthias Braun committed
921
922
923
924
925
	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
926
	ia32_code_gen_t *cg = self;