bearch_ia32.c 67.8 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
#include "config.h"
27

Matthias Braun's avatar
Matthias Braun committed
28
29
#include "lc_opts.h"
#include "lc_opts_enum.h"
30

Christian Würdig's avatar
Christian Würdig committed
31
32
#include <math.h>

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

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

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

75
76
#include "ia32_new_nodes.h"
#include "gen_ia32_regalloc_if.h"
77
#include "gen_ia32_machine.h"
78
#include "ia32_common_transform.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
#ifdef FIRM_GRGEN_BE
#include "ia32_pbqp_transform.h"
92
93

transformer_t be_transformer = TRANSFORMER_DEFAULT;
94
95
#endif

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

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

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

104
105
106
107
108
109
110
111
112
113
114
115
116
/**
 * 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 __divdi3 library call */
	NULL,    /* entity for __moddi3 library call */
	NULL,    /* entity for __udivdi3 library call */
	NULL,    /* entity for __umoddi3 library call */
};


117
typedef ir_node *(*create_const_node_func) (dbg_info *dbg, ir_node *block);
118

119
static inline ir_node *create_const(ia32_code_gen_t *cg, ir_node **place,
120
                                    create_const_node_func func,
121
                                    const arch_register_t* reg)
122
123
124
125
126
127
128
{
	ir_node *block, *res;

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

	block = get_irg_start_block(cg->irg);
129
	res = func(NULL, block);
130
	arch_set_irn_register(res, reg);
131
132
	*place = res;

133
134
	add_irn_dep(get_irg_end(cg->irg), res);
	/* add_irn_dep(get_irg_start(cg->irg), res); */
135
136
137
138

	return res;
}

139
/* Creates the unique per irg GP NoReg node. */
140
ir_node *ia32_new_NoReg_gp(ia32_code_gen_t *cg) {
141
	return create_const(cg, &cg->noreg_gp, new_bd_ia32_NoReg_GP,
142
143
144
145
	                    &ia32_gp_regs[REG_GP_NOREG]);
}

ir_node *ia32_new_NoReg_vfp(ia32_code_gen_t *cg) {
146
	return create_const(cg, &cg->noreg_vfp, new_bd_ia32_NoReg_VFP,
147
148
149
150
	                    &ia32_vfp_regs[REG_VFP_NOREG]);
}

ir_node *ia32_new_NoReg_xmm(ia32_code_gen_t *cg) {
151
	return create_const(cg, &cg->noreg_xmm, new_bd_ia32_NoReg_XMM,
152
	                    &ia32_xmm_regs[REG_XMM_NOREG]);
153
154
}

155
ir_node *ia32_new_Unknown_gp(ia32_code_gen_t *cg) {
156
	return create_const(cg, &cg->unknown_gp, new_bd_ia32_Unknown_GP,
157
	                    &ia32_gp_regs[REG_GP_UKNWN]);
158
}
159

160
ir_node *ia32_new_Unknown_vfp(ia32_code_gen_t *cg) {
161
	return create_const(cg, &cg->unknown_vfp, new_bd_ia32_Unknown_VFP,
162
163
164
165
	                    &ia32_vfp_regs[REG_VFP_UKNWN]);
}

ir_node *ia32_new_Unknown_xmm(ia32_code_gen_t *cg) {
166
	return create_const(cg, &cg->unknown_xmm, new_bd_ia32_Unknown_XMM,
167
168
169
	                    &ia32_xmm_regs[REG_XMM_UKNWN]);
}

170
ir_node *ia32_new_Fpu_truncate(ia32_code_gen_t *cg) {
171
	return create_const(cg, &cg->fpu_trunc_mode, new_bd_ia32_ChangeCW,
172
173
174
                        &ia32_fp_cw_regs[REG_FPCW]);
}

175

176
/**
177
 * Returns the admissible noreg register node for input register pos of node irn.
178
 */
179
180
static ir_node *ia32_get_admissible_noreg(ia32_code_gen_t *cg, ir_node *irn, int pos)
{
181
	const arch_register_req_t *req = arch_get_register_req(irn, pos);
182

Matthias Braun's avatar
Matthias Braun committed
183
184
	assert(req != NULL && "Missing register requirements");
	if (req->cls == &ia32_reg_classes[CLASS_ia32_gp])
185
		return ia32_new_NoReg_gp(cg);
Matthias Braun's avatar
Matthias Braun committed
186

187
188
189
190
191
	if (ia32_cg_config.use_sse2) {
		return ia32_new_NoReg_xmm(cg);
	} else {
		return ia32_new_NoReg_vfp(cg);
	}
192
193
}

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

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

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

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

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

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

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

Matthias Braun's avatar
Matthias Braun committed
241
		assert(req != NULL);
242

Matthias Braun's avatar
Matthias Braun committed
243
		return req;
Christian Würdig's avatar
Christian Würdig committed
244
245
	}

Matthias Braun's avatar
Matthias Braun committed
246
247
	/* unknowns should be transformed already */
	return arch_no_register_req;
Christian Würdig's avatar
Christian Würdig committed
248
}
Christian Würdig's avatar
Christian Würdig committed
249

250
static arch_irn_class_t ia32_classify(const ir_node *irn) {
251
	arch_irn_class_t classification = 0;
252

253
	irn = skip_Proj_const(irn);
254

Christian Würdig's avatar
Christian Würdig committed
255
	if (is_cfop(irn))
256
257
258
		classification |= arch_irn_class_branch;

	if (! is_ia32_irn(irn))
259
		return classification;
260

261
	if (is_ia32_is_reload(irn))
262
263
		classification |= arch_irn_class_reload;

264
265
266
	if (is_ia32_is_spill(irn))
		classification |= arch_irn_class_spill;

Matthias Braun's avatar
Matthias Braun committed
267
268
269
	if (is_ia32_is_remat(irn))
		classification |= arch_irn_class_remat;

270
	return classification;
Christian Würdig's avatar
Christian Würdig committed
271
}
Christian Würdig's avatar
Christian Würdig committed
272

273
274
275
/**
 * The IA32 ABI callback object.
 */
276
typedef struct {
277
278
279
	be_abi_call_flags_bits_t flags;  /**< The call flags. */
	const arch_env_t *aenv;          /**< The architecture environment. */
	ir_graph *irg;                   /**< The associated graph. */
280
281
} ia32_abi_env_t;

282
static ir_entity *ia32_get_frame_entity(const ir_node *irn) {
Christian Würdig's avatar
Christian Würdig committed
283
	return is_ia32_irn(irn) ? get_ia32_frame_ent(irn) : NULL;
284
285
}

286
static void ia32_set_frame_entity(ir_node *irn, ir_entity *ent) {
287
288
289
	set_ia32_frame_ent(irn, ent);
}

290
static void ia32_set_frame_offset(ir_node *irn, int bias)
291
292
293
{
	if (get_ia32_frame_ent(irn) == NULL)
		return;
294

295
	if (is_ia32_Pop(irn) || is_ia32_PopMem(irn)) {
Matthias Braun's avatar
Matthias Braun committed
296
297
		ia32_code_gen_t *cg = ia32_current_cg;
		int omit_fp = be_abi_omit_fp(cg->birg->abi);
298
299
300
301
302
303
		if (omit_fp) {
			/* Pop nodes modify the stack pointer before calculating the
			 * destination address, so fix this here
			 */
			bias -= 4;
		}
304
	}
305
	add_ia32_am_offs_int(irn, bias);
306
307
}

308
static int ia32_get_sp_bias(const ir_node *node)
309
{
310
311
312
	if (is_ia32_Call(node))
		return -(int)get_ia32_call_attr_const(node)->pop;

313
314
315
	if (is_ia32_Push(node))
		return 4;

316
	if (is_ia32_Pop(node) || is_ia32_PopMem(node))
317
		return -4;
318
319
320
321

	return 0;
}

322
/**
Michael Beck's avatar
Michael Beck committed
323
 * Generate the routine prologue.
324
 *
325
326
327
328
 * @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.
329
 *
330
 * @return           The register which shall be used as a stack frame base.
331
332
333
 *
 * All nodes which define registers in @p reg_map must keep @p reg_map current.
 */
334
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
335
{
336
337
338
	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
339

Christian Würdig's avatar
Christian Würdig committed
340
	if (! env->flags.try_omit_fp) {
341
		ir_graph *irg     = env->irg;
342
343
344
		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);
345
		ir_node  *noreg   = ia32_new_NoReg_gp(cg);
346
		ir_node  *push;
Sebastian Hack's avatar
Sebastian Hack committed
347

348
349
350
		/* mark bp register as ignore */
		be_set_constr_single_reg_out(get_Proj_pred(curr_bp),
				get_Proj_proj(curr_bp), arch_env->bp, arch_register_req_type_ignore);
351

352
		/* push ebp */
353
		push    = new_bd_ia32_Push(NULL, bl, noreg, noreg, *mem, curr_bp, curr_sp);
354
355
		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);
356
357

		/* the push must have SP out register */
358
		arch_set_irn_register(curr_sp, arch_env->sp);
359

360
361
362
		/* this modifies the stack bias, because we pushed 32bit */
		*stack_bias -= 4;

363
		/* move esp to ebp */
364
365
366
		curr_bp = be_new_Copy(arch_env->bp->reg_class, irg, bl, curr_sp);
		be_set_constr_single_reg_out(curr_bp, 0, arch_env->bp,
		                             arch_register_req_type_ignore);
Sebastian Hack's avatar
Sebastian Hack committed
367

368
		/* beware: the copy must be done before any other sp use */
369
		curr_sp = be_new_CopyKeep_single(arch_env->sp->reg_class, irg, bl, curr_sp, curr_bp, get_irn_mode(curr_sp));
370
371
		be_set_constr_single_reg_out(curr_sp, 0, arch_env->sp,
				                     arch_register_req_type_produces_sp);
372

373
374
		be_abi_reg_map_set(reg_map, arch_env->sp, curr_sp);
		be_abi_reg_map_set(reg_map, arch_env->bp, curr_bp);
375

376
		return arch_env->bp;
Sebastian Hack's avatar
Sebastian Hack committed
377
378
	}

379
	return arch_env->sp;
Sebastian Hack's avatar
Sebastian Hack committed
380
381
}

Michael Beck's avatar
Michael Beck committed
382
383
384
/**
 * Generate the routine epilogue.
 * @param self    The callback object.
385
 * @param bl      The block for the epilog
Michael Beck's avatar
Michael Beck committed
386
387
388
389
390
391
 * @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
392
393
static void ia32_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map)
{
394
	ia32_abi_env_t   *env      = self;
395
	const arch_env_t *arch_env = env->aenv;
396
397
398
	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
399

400
401
	if (env->flags.try_omit_fp) {
		/* simply remove the stack frame here */
402
		curr_sp = be_new_IncSP(arch_env->sp, irg, bl, curr_sp, BE_STACK_FRAME_SIZE_SHRINK, 0);
403
	} else {
404
		ir_mode *mode_bp = arch_env->bp->reg_class->mode;
Sebastian Hack's avatar
Sebastian Hack committed
405

406
		if (ia32_cg_config.use_leave) {
407
			ir_node *leave;
408

409
			/* leave */
410
			leave   = new_bd_ia32_Leave(NULL, bl, curr_bp);
Christoph Mallon's avatar
Christoph Mallon committed
411
412
			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);
413
		} else {
414
415
			ir_node *pop;

Christoph Mallon's avatar
Christoph Mallon committed
416
417
			/* the old SP is not needed anymore (kill the proj) */
			assert(is_Proj(curr_sp));
418
			kill_node(curr_sp);
Christoph Mallon's avatar
Christoph Mallon committed
419

420
			/* copy ebp to esp */
Christoph Mallon's avatar
Christoph Mallon committed
421
			curr_sp = be_new_Copy(&ia32_reg_classes[CLASS_ia32_gp], irg, bl, curr_bp);
422
			arch_set_irn_register(curr_sp, arch_env->sp);
423
424
			be_set_constr_single_reg_out(curr_sp, 0, arch_env->sp,
				                         arch_register_req_type_ignore);
425
426

			/* pop ebp */
427
			pop     = new_bd_ia32_PopEbp(NULL, bl, *mem, curr_sp);
Christoph Mallon's avatar
Christoph Mallon committed
428
429
			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);
430

Christoph Mallon's avatar
Christoph Mallon committed
431
			*mem = new_r_Proj(irg, bl, pop, mode_M, pn_ia32_Pop_M);
432
		}
433
434
		arch_set_irn_register(curr_sp, arch_env->sp);
		arch_set_irn_register(curr_bp, arch_env->bp);
Sebastian Hack's avatar
Sebastian Hack committed
435
436
	}

437
438
	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
439
440
}

441
442
443
444
445
446
447
448
449
/**
 * 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)
{
450
451
	ia32_abi_env_t      *env = XMALLOC(ia32_abi_env_t);
	be_abi_call_flags_t  fl  = be_abi_call_get_flags(call);
452
453
454
455
456
457
458
459
460
461
462
463
464
465
	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
466
467
468
469
470
471
472
/**
 * 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)
{
473
#define IDENT(s) new_id_from_chars(s, sizeof(s)-1)
474
475
476
477
	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
478

479
	if (! between_type) {
480
		ir_entity *old_bp_ent;
481
		ir_entity *ret_addr_ent;
482
483
484
485
		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
486

487
488
489
		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
490

491
492
493
		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));
494
		set_type_state(between_type, layout_fixed);
495
496
497
498
499
500
501

		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
502
503
	}

504
	return env->flags.try_omit_fp ? omit_fp_between_type : between_type;
505
#undef IDENT
Sebastian Hack's avatar
Sebastian Hack committed
506
507
}

508
509
510
511
512
513
514
515
/**
 * 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
 */
516
static int ia32_get_op_estimated_cost(const ir_node *irn)
517
{
518
	int            cost;
Christian Würdig's avatar
Christian Würdig committed
519
	ia32_op_type_t op_tp;
Adam Szalkowski's avatar
Adam Szalkowski committed
520

Christian Würdig's avatar
Christian Würdig committed
521
	if (is_Proj(irn))
522
523
524
		return 0;
	if (!is_ia32_irn(irn))
		return 0;
Adam Szalkowski's avatar
Adam Szalkowski committed
525

Christian Würdig's avatar
Christian Würdig committed
526
527
528
529
530
531
532
533
534
	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
535
		int size = get_ia32_copyb_size(irn);
Christian Würdig's avatar
Christian Würdig committed
536
537
538
539
540
		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) {
		/*
541
542
543
			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
544
		*/
545
546
547
548
		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))
		    )) {
549
550
551
552
			cost += 5;
		} else {
			cost += 20;
		}
Christian Würdig's avatar
Christian Würdig committed
553
554
555
	}

	return cost;
556
557
}

Christian Würdig's avatar
Christian Würdig committed
558
559
560
561
562
563
564
565
566
/**
 * 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
 */
567
static arch_inverse_t *ia32_get_inverse(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obst) {
568
	ir_mode  *mode;
569
	ir_mode  *irn_mode;
570
	ir_node  *block, *noreg, *nomem;
571
	dbg_info *dbg;
572
573
574
575
576
577

	/* 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) */
578
	if (i != n_ia32_binary_left && i != n_ia32_binary_right)
579
580
581
582
583
584
		return NULL;

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

585
586
587
588
589
	/* TODO: adjust for new immediates... */
	ir_fprintf(stderr, "TODO: fix get_inverse for new immediates (%+F)\n",
	           irn);
	return NULL;

590
	block    = get_nodes_block(irn);
591
	mode     = get_irn_mode(irn);
592
593
	irn_mode = get_irn_mode(irn);
	noreg    = get_irn_n(irn, 0);
594
	nomem    = new_NoMem();
595
	dbg      = get_irn_dbg_info(irn);
596
597

	/* initialize structure */
598
	inverse->nodes = obstack_alloc(obst, 2 * sizeof(inverse->nodes[0]));
599
	inverse->costs = 0;
600
	inverse->n     = 1;
601

602
	switch (get_ia32_irn_opcode(irn)) {
603
		case iro_ia32_Add:
604
#if 0
605
606
607
			if (get_ia32_immop_type(irn) == ia32_ImmConst) {
				/* we have an add with a const here */
				/* invers == add with negated const */
608
				inverse->nodes[0] = new_bd_ia32_Add(dbg, block, noreg, noreg, nomem, get_irn_n(irn, i), noreg);
609
610
611
612
613
614
615
616
				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 */
617
				inverse->nodes[0] = new_bd_ia32_Sub(dbg, block, noreg, noreg, nomem, get_irn_n(irn, i), noreg);
618
				inverse->costs   += 2;
619
620
621
622
				copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
			}
			else {
				/* normal add: inverse == sub */
623
				inverse->nodes[0] = new_bd_ia32_Sub(dbg, block, noreg, noreg, nomem, (ir_node*) irn, get_irn_n(irn, i ^ 1));
624
				inverse->costs   += 2;
625
			}
626
#endif
627
628
			break;
		case iro_ia32_Sub:
629
#if 0
630
631
632
			if (get_ia32_immop_type(irn) != ia32_ImmNone) {
				/* we have a sub with a const/symconst here */
				/* invers == add with this const */
633
				inverse->nodes[0] = new_bd_ia32_Add(dbg, block, noreg, noreg, nomem, get_irn_n(irn, i), noreg);
634
635
636
637
638
				inverse->costs   += (get_ia32_immop_type(irn) == ia32_ImmSymConst) ? 5 : 1;
				copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
			}
			else {
				/* normal sub */
639
				if (i == n_ia32_binary_left) {
640
					inverse->nodes[0] = new_bd_ia32_Add(dbg, block, noreg, noreg, nomem, (ir_node*) irn, get_irn_n(irn, 3));
641
642
				}
				else {
643
					inverse->nodes[0] = new_bd_ia32_Sub(dbg, block, noreg, noreg, nomem, get_irn_n(irn, n_ia32_binary_left), (ir_node*) irn);
644
645
646
				}
				inverse->costs += 1;
			}
647
#endif
648
			break;
649
		case iro_ia32_Xor:
650
#if 0
651
652
			if (get_ia32_immop_type(irn) != ia32_ImmNone) {
				/* xor with const: inverse = xor */
653
				inverse->nodes[0] = new_bd_ia32_Xor(dbg, block, noreg, noreg, nomem, get_irn_n(irn, i), noreg);
654
655
656
657
658
				inverse->costs   += (get_ia32_immop_type(irn) == ia32_ImmSymConst) ? 5 : 1;
				copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
			}
			else {
				/* normal xor */
659
				inverse->nodes[0] = new_bd_ia32_Xor(dbg, block, noreg, noreg, nomem, (ir_node *) irn, get_irn_n(irn, i));
660
661
				inverse->costs   += 1;
			}
662
#endif
663
			break;
664
		case iro_ia32_Not: {
665
			inverse->nodes[0] = new_bd_ia32_Not(dbg, block, (ir_node*) irn);
666
667
			inverse->costs   += 1;
			break;
668
		}
669
		case iro_ia32_Neg: {
670
			inverse->nodes[0] = new_bd_ia32_Neg(dbg, block, (ir_node*) irn);
671
672
			inverse->costs   += 1;
			break;
673
		}
674
675
676
677
678
679
		default:
			/* inverse operation not supported */
			return NULL;
	}

	return inverse;
Christian Würdig's avatar
Christian Würdig committed
680
681
}

682
683
684
685
686
687
688
689
static ir_mode *get_spill_mode_mode(const ir_mode *mode)
{
	if(mode_is_float(mode))
		return mode_D;

	return mode_Iu;
}

690
691
692
/**
 * Get the mode that should be used for spilling value node
 */
693
static ir_mode *get_spill_mode(const ir_node *node)
694
695
{
	ir_mode *mode = get_irn_mode(node);
696
	return get_spill_mode_mode(mode);
697
698
699
}

/**
Michael Beck's avatar
Michael Beck committed
700
 * Checks whether an addressmode reload for a node with mode mode is compatible
701
702
703
704
 * with a spillslot of mode spill_mode
 */
static int ia32_is_spillmode_compatible(const ir_mode *mode, const ir_mode *spillmode)
{
705
	return !mode_is_float(mode) || mode == spillmode;
706
707
}

708
/**
Christoph Mallon's avatar
Christoph Mallon committed
709
 * Check if irn can load its operand at position i from memory (source addressmode).
710
711
712
713
 * @param irn    The irn to be checked
 * @param i      The operands position
 * @return Non-Zero if operand can be loaded
 */
714
715
716
717
static int ia32_possible_memory_operand(const ir_node *irn, unsigned int i)
{
	ir_node       *op        = get_irn_n(irn, i);
	const ir_mode *mode      = get_irn_mode(op);
718
	const ir_mode *spillmode = get_spill_mode(op);
719

720
721
722
723
	if (!is_ia32_irn(irn)                              ||  /* must be an ia32 irn */
	    get_ia32_op_type(irn) != ia32_Normal           ||  /* must not already be a addressmode irn */
	    !ia32_is_spillmode_compatible(mode, spillmode) ||
	    is_ia32_use_frame(irn))                            /* must not already use frame */
724
725
		return 0;

726
727
728
729
	switch (get_ia32_am_support(irn)) {
		case ia32_am_none:
			return 0;

730
		case ia32_am_unary:
731
732
733
			if (i != n_ia32_unary_op)
				return 0;
			break;
734
735
736
737
738
739
740
741
742
743
744
745
746
747

		case ia32_am_binary:
			switch (i) {
				case n_ia32_binary_left: {
					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)
					 */
					req = get_ia32_in_req(irn, n_ia32_binary_left);
					if (req->type & arch_register_req_type_limited)
						return 0;
748
					break;
749
750
751
				}

				case n_ia32_binary_right:
752
					break;
753

754
755
756
				default:
					return 0;
			}
757
			break;
758
759

		default:
760
			panic("Unknown AM type");
761
	}
762
763
764
765
766
767
768

	/* HACK: must not already use "real" memory.
	 * This can happen for Call and Div */
	if (!is_NoMem(get_irn_n(irn, n_ia32_mem)))
		return 0;

	return 1;
769
770
}

771
772
static void ia32_perform_memory_operand(ir_node *irn, ir_node *spill,
                                        unsigned int i)
773
{
774
775
776
	ir_mode *load_mode;
	ir_mode *dest_op_mode;

777
	assert(ia32_possible_memory_operand(irn, i) && "Cannot perform memory operand change");
778
779

	set_ia32_op_type(irn, ia32_AddrModeS);
780
781
782
783
784
785

	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);
	}
786
	set_ia32_use_frame(irn);
787
	set_ia32_need_stackent(irn);
788

789
790
	if (i == n_ia32_binary_left                    &&
	    get_ia32_am_support(irn) == ia32_am_binary &&
791
792
	    /* immediates are only allowed on the right side */
	    !is_ia32_Immediate(get_irn_n(irn, n_ia32_binary_right))) {
793
		ia32_swap_left_right(irn);
794
		i = n_ia32_binary_right;
795
	}
796

797
798
	assert(is_NoMem(get_irn_n(irn, n_ia32_mem)));

799
800
801
802
	set_irn_n(irn, n_ia32_base, get_irg_frame(get_irn_irg(irn)));
	set_irn_n(irn, n_ia32_mem,  spill);
	set_irn_n(irn, i,           ia32_get_admissible_noreg(ia32_current_cg, irn, i));
	set_ia32_is_reload(irn);
803
804
}

Sebastian Hack's avatar
Sebastian Hack committed
805
806
static const be_abi_callbacks_t ia32_abi_callbacks = {
	ia32_abi_init,
807
	ia32_abi_done,
Sebastian Hack's avatar
Sebastian Hack committed
808
809
	ia32_abi_get_between_type,
	ia32_abi_prologue,
810
	ia32_abi_epilogue
Sebastian Hack's avatar
Sebastian Hack committed
811
812
};

Christian Würdig's avatar
Christian Würdig committed
813
814
/* fill register allocator interface */

Matthias Braun's avatar
Matthias Braun committed
815
static const arch_irn_ops_t ia32_irn_ops = {
Christian Würdig's avatar
Christian Würdig committed
816
817
	ia32_get_irn_reg_req,
	ia32_classify,
818
	ia32_get_frame_entity,
819
	ia32_set_frame_entity,
820
821
	ia32_set_frame_offset,
	ia32_get_sp_bias,
822
	ia32_get_inverse,
823
824
825
	ia32_get_op_estimated_cost,
	ia32_possible_memory_operand,
	ia32_perform_memory_operand,
Christian Würdig's avatar
Christian Würdig committed
826
827
828
829
830
831
832
833
834
835
836
837
838
};

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

Michael Beck's avatar
Michael Beck committed
839
840
841
842
static ir_entity *mcount = NULL;

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

843
static void ia32_before_abi(void *self) {
Matthias Braun's avatar
Matthias Braun committed
844
845
	lower_mode_b_config_t lower_mode_b_config = {
		mode_Iu,  /* lowered mode */
yb9976's avatar
typo    
yb9976 committed
846
		mode_Bu,  /* preferred mode for set */
Matthias Braun's avatar
Matthias Braun committed
847
848
		0,        /* don't lower direct compares */
	};
Christian Würdig's avatar
Christian Würdig committed
849
	ia32_code_gen_t *cg = self;
850

Matthias Braun's avatar
Matthias Braun committed
851
	ir_lower_mode_b(cg->irg, &lower_mode_b_config);
Michael Beck's avatar
Michael Beck committed
852
	if (cg->dump)
853
		be_dump(cg->irg, "-lower_modeb", dump_ir_block_graph_sched);
Michael Beck's avatar
Michael Beck committed
854
855
856
857
858
859
860
861
862
863
	if (cg->gprof) {
		if (mcount == NULL) {
			ir_type *tp = new_type_method(ID("FKT.mcount"), 0, 0);
			mcount = new_entity(get_glob_type(), ID("mcount"), tp);
			/* FIXME: enter the right ld_ident here */
			set_entity_ld_ident(mcount, get_entity_ident(mcount));
			set_entity_visibility(mcount, visibility_external_allocated);
		}
		instrument_initcall(cg->irg, mcount);
	}
864
865
866
867
868
869
}

/**
 * Transforms the standard firm graph into
 * an ia32 firm graph
 */
870
871
872
873
static void ia32_prepare_graph(void *self)
{
	ia32_code_gen_t *cg  = self;
	ir_graph        *irg = cg->irg;
874

yb9976's avatar
typo    
yb9976 committed
875
	/* do local optimizations */
876
877
878
879
880
881
	optimize_graph_df(irg);

	/* we have to do cfopt+remove_critical_edges as we can't have Bad-blocks
	 * or critical edges in the backend */
	optimize_cf(irg);
	remove_critical_cf_edges(irg);
Matthias Braun's avatar
Matthias Braun committed
882

883
884
885
886
887
888
889
	/* TODO: we often have dead code reachable through out-edges here. So for
	 * now we rebuild edges (as we need correct user count for code selection)
	 */
#if 1
	edges_deactivate(cg->irg);
	edges_activate(cg->irg);
#endif
890

Michael Beck's avatar
Michael Beck committed
891
	if (cg->dump)
Matthias Braun's avatar
Matthias Braun committed
892
893
		be_dump(cg->irg, "-pre_transform", dump_ir_block_graph_sched);

894
	switch (be_transformer) {
895
896
897
898
	case TRANSFORMER_DEFAULT:
		/* transform remaining nodes into assembler instructions */
		ia32_transform_graph(cg);
		break;
899

Christoph Mallon's avatar