amd64_transform.c 61.6 KB
Newer Older
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
4
5
6
7
8
9
 */

/**
 * @file
 * @brief   code selection (transform FIRM into amd64 FIRM)
 */
Matthias Braun's avatar
Matthias Braun committed
10
11
12
13
14
#include "debug.h"
#include "error.h"
#include "heights.h"
#include "ircons.h"
#include "irgmod.h"
15
16
#include "irgraph_t.h"
#include "irmode_t.h"
Matthias Braun's avatar
Matthias Braun committed
17
#include "irnode_t.h"
18
#include "iropt_t.h"
19
#include "tv_t.h"
Matthias Braun's avatar
Matthias Braun committed
20
#include "util.h"
21

22
23
24
#include "benode.h"
#include "betranshlp.h"
#include "beutil.h"
25
#include "bearch_amd64_t.h"
26
27
#include "beirg.h"
#include "beabihelper.h"
Matthias Braun's avatar
Matthias Braun committed
28
#include "besched.h"
29

Matthias Braun's avatar
Matthias Braun committed
30
31
#include "amd64_cconv.h"
#include "amd64_new_nodes.h"
32
33
#include "amd64_nodes_attr.h"
#include "amd64_transform.h"
Matthias Braun's avatar
Matthias Braun committed
34
#include "../ia32/x86_address_mode.h"
35
36
37
38
39

#include "gen_amd64_regalloc_if.h"

DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

40
41
42
43
44
45
typedef struct reg_info_t {
	size_t   offset;
	ir_node *irn;
} reg_info_t;

static ir_mode         *mode_gp;
Matthias Braun's avatar
Matthias Braun committed
46
static ir_mode         *mode_flags;
47
48
49
50
51
52
53
54
static amd64_cconv_t   *current_cconv = NULL;
static reg_info_t       start_mem;
static reg_info_t       start_sp;
static reg_info_t       start_fp;
static size_t           start_callee_saves_offset;
static size_t           start_params_offset;
static pmap            *node_to_stack;
static be_stackorder_t *stackorder;
55

Matthias Braun's avatar
Matthias Braun committed
56
57
58
59
60
61
62
63
64
static const arch_register_req_t amd64_requirement_gp = {
	arch_register_req_type_normal,
	&amd64_reg_classes[CLASS_amd64_gp],
	NULL,
	0,
	0,
	1
};

Matthias Braun's avatar
Matthias Braun committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#define BIT(x)    (1u << x)

static const arch_register_req_t amd64_requirement_gp_same_0 = {
	arch_register_req_type_normal | arch_register_req_type_should_be_same,
	&amd64_reg_classes[CLASS_amd64_gp],
	NULL,
	BIT(0),
	0,
	1
};

static const arch_register_req_t amd64_requirement_gp_same_0_not_1 = {
	arch_register_req_type_normal | arch_register_req_type_should_be_same
	| arch_register_req_type_must_be_different,
	&amd64_reg_classes[CLASS_amd64_gp],
	NULL,
	BIT(0),
	BIT(1),
	1
};

static const unsigned amd64_limited_gp_rcx [] = { BIT(REG_GP_RCX) };
static const arch_register_req_t amd64_requirement_rcx = {
	arch_register_req_type_limited,
	&amd64_reg_classes[CLASS_amd64_gp],
	amd64_limited_gp_rcx,
	0,
	0,
	1
};

96
97
98
99
100
101
102
103
104
105
static const unsigned amd64_limited_gp_rax [] = { BIT(REG_GP_RAX) };
static const arch_register_req_t amd64_requirement_rax = {
	arch_register_req_type_limited,
	&amd64_reg_classes[CLASS_amd64_gp],
	amd64_limited_gp_rax,
	0,
	0,
	1
};

Matthias Braun's avatar
Matthias Braun committed
106
107
108
109
110
111
static const arch_register_req_t *mem_reqs[] = {
	&arch_no_requirement,
};

static const arch_register_req_t *reg_mem_reqs[] = {
	&amd64_requirement_gp,
Matthias Braun's avatar
Matthias Braun committed
112
113
114
	&arch_no_requirement,
};

Matthias Braun's avatar
Matthias Braun committed
115
116
static const arch_register_req_t *reg_reg_mem_reqs[] = {
	&amd64_requirement_gp,
Matthias Braun's avatar
Matthias Braun committed
117
118
119
120
	&amd64_requirement_gp,
	&arch_no_requirement,
};

Matthias Braun's avatar
Matthias Braun committed
121
122
static const arch_register_req_t *reg_reg_reg_mem_reqs[] = {
	&amd64_requirement_gp,
Matthias Braun's avatar
Matthias Braun committed
123
124
125
126
127
	&amd64_requirement_gp,
	&amd64_requirement_gp,
	&arch_no_requirement,
};

Matthias Braun's avatar
Matthias Braun committed
128
129
static const arch_register_req_t *reg_reg_reqs[] = {
	&amd64_requirement_gp,
130
131
132
	&amd64_requirement_gp,
};

133
134
135
136
137
static const arch_register_req_t *rax_reg_reqs[] = {
	&amd64_requirement_rax,
	&amd64_requirement_gp,
};

Matthias Braun's avatar
Matthias Braun committed
138
139
140
141
142
143
144
145
146
static const arch_register_req_t *reg_reqs[] = {
	&amd64_requirement_gp,
};

static const arch_register_req_t *reg_rcx_reqs[] = {
	&amd64_requirement_gp,
	&amd64_requirement_rcx,
};

147
148
149
static const arch_register_req_t *no_reqs[] = {
};

Matthias Braun's avatar
Matthias Braun committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
static inline bool mode_needs_gp_reg(ir_mode *mode)
{
	return get_mode_arithmetic(mode) == irma_twos_complement;
}

static bool is_downconv(const ir_node *node)
{
	if (!is_Conv(node))
		return false;

	ir_mode *dest_mode = get_irn_mode(node);
	if (!mode_needs_gp_reg(dest_mode))
		return false;
	ir_mode *src_mode = get_irn_mode(get_Conv_op(node));
	if (!mode_needs_gp_reg(src_mode))
		return false;
	return get_mode_size_bits(dest_mode) <= get_mode_size_bits(src_mode);
}

static ir_node *skip_downconv(ir_node *node)
{
	while (is_downconv(node)) {
		if (get_irn_n_edges(node) > 1)
			break;
		node = get_Conv_op(node);
	}
	return node;
}

static bool is_sameconv(const ir_node *node)
{
	if (!is_Conv(node))
		return false;
	ir_mode *dest_mode = get_irn_mode(node);
	if (!mode_needs_gp_reg(dest_mode))
		return false;
	ir_mode *src_mode = get_irn_mode(get_Conv_op(node));
	if (!mode_needs_gp_reg(src_mode))
		return false;
	return get_mode_size_bits(dest_mode) == get_mode_size_bits(src_mode);
}

static ir_node *skip_sameconv(ir_node *node)
193
{
Matthias Braun's avatar
Matthias Braun committed
194
195
196
197
198
199
	while (is_sameconv(node)) {
		if (get_irn_n_edges(node) > 1)
			break;
		node = get_Conv_op(node);
	}
	return node;
200
201
}

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
static ir_node *get_reg(ir_graph *const irg, reg_info_t *const reg)
{
	if (!reg->irn) {
		/* this is already the transformed start node */
		ir_node *const start = get_irg_start(irg);
		assert(is_amd64_Start(start));
		arch_register_class_t const *const cls
			= arch_get_irn_register_req_out(start, reg->offset)->cls;
		reg->irn = new_r_Proj(start, cls ? cls->mode : mode_M, reg->offset);
	}
	return reg->irn;
}

static ir_node *get_initial_sp(ir_graph *irg)
{
	return get_reg(irg, &start_sp);
}

static ir_node *get_initial_fp(ir_graph *irg)
{
	return get_reg(irg, &start_fp);
}

static ir_node *get_initial_mem(ir_graph *irg)
{
	return get_reg(irg, &start_mem);
}

static ir_node *get_frame_base(ir_graph *irg)
{
	if (current_cconv->omit_fp) {
		return get_initial_sp(irg);
	} else {
		return get_initial_fp(irg);
	}
}
238

239
240
static ir_node *gen_Const(ir_node *node)
{
241
	ir_node  *block = be_transform_node(get_nodes_block(node));
242
	dbg_info *dbgi  = get_irn_dbg_info(node);
243
	ir_mode  *mode  = get_irn_mode(node);
244
245
246
247
248
	if (!mode_needs_gp_reg(mode))
		panic("amd64: float constant not supported yet");
	ir_tarval *tv = get_Const_tarval(node);
	uint64_t val = get_tarval_uint64(tv);
	amd64_insn_mode_t imode = val > UINT32_MAX ? INSN_MODE_64 : INSN_MODE_32;
Matthias Braun's avatar
Matthias Braun committed
249
	return new_bd_amd64_MovImm(dbgi, block, imode, val, NULL);
250
251
}

Matthias Braun's avatar
Matthias Braun committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
typedef enum reference_mode_t {
	REFERENCE_DIRECT,
	REFERENCE_IP_RELATIVE,
	REFERENCE_GOT,
} reference_mode_t;

static reference_mode_t need_relative_addressing(const ir_entity *entity)
{
	if (!be_options.pic)
		return REFERENCE_DIRECT;

	/* simply everything is instruction pointer relative, external functions
	 * use a global offset table */
	return entity_has_definition(entity)
	   && (get_entity_linkage(entity) & IR_LINKAGE_MERGE) == 0
	    ? REFERENCE_IP_RELATIVE : REFERENCE_GOT;
}

270
static ir_node *gen_Address(ir_node *node)
271
272
273
{
	ir_node   *block  = be_transform_node(get_nodes_block(node));
	dbg_info  *dbgi   = get_irn_dbg_info(node);
274
	ir_entity *entity = get_Address_entity(node);
275

Matthias Braun's avatar
Matthias Braun committed
276
277
	/* do we need RIP-relative addressing because of PIC? */
	reference_mode_t mode = need_relative_addressing(entity);
Matthias Braun's avatar
Matthias Braun committed
278
	if (mode == REFERENCE_DIRECT)
Matthias Braun's avatar
Matthias Braun committed
279
280
281
282
		return new_bd_amd64_MovImm(dbgi, block, INSN_MODE_64, 0, entity);

	amd64_addr_t addr;
	memset(&addr, 0, sizeof(addr));
283
	addr.base_input  = NO_INPUT;
Matthias Braun's avatar
Matthias Braun committed
284
285
	addr.index_input = NO_INPUT;
	addr.mem_input   = NO_INPUT;
Matthias Braun's avatar
Matthias Braun committed
286
287

	if (mode == REFERENCE_IP_RELATIVE) {
Matthias Braun's avatar
Matthias Braun committed
288
289
		addr.immediate.entity = entity;
		return new_bd_amd64_Lea(dbgi, block, 0, NULL, INSN_MODE_64, addr);
Matthias Braun's avatar
Matthias Braun committed
290
291
	} else {
		assert(mode == REFERENCE_GOT);
Matthias Braun's avatar
Matthias Braun committed
292
		addr.immediate.entity = new_got_entry_entity(entity);
293
294
		ir_node *load = new_bd_amd64_Mov(dbgi, block, 0, NULL, INSN_MODE_64,
										 AMD64_OP_ADDR, addr);
Matthias Braun's avatar
Matthias Braun committed
295
		return new_r_Proj(load, mode_gp, pn_amd64_Mov_res);
Matthias Braun's avatar
Matthias Braun committed
296
297
298
299
	}
}

typedef ir_node *(*construct_binop_func)(dbg_info *dbgi, ir_node *block,
Matthias Braun's avatar
Matthias Braun committed
300
	int arity, ir_node *in[], const amd64_binop_addr_attr_t *attr_init);
Matthias Braun's avatar
Matthias Braun committed
301

302
303
304
305
typedef ir_node *(*construct_rax_binop_func)(dbg_info *dbgi, ir_node *block,
	int arity, ir_node *in[], amd64_insn_mode_t insn_mode,
	amd64_op_mode_t op_mode, amd64_addr_t addr);

Matthias Braun's avatar
Matthias Braun committed
306
307
308
309
310
311
312
313
typedef enum match_flags_t {
	match_am           = 1 << 0,
	match_mode_neutral = 1 << 1,
	match_immediate    = 1 << 2,
	match_commutative  = 1 << 3,
} match_flags_t;

typedef struct amd64_args_t {
Matthias Braun's avatar
Matthias Braun committed
314
315
316
317
	amd64_binop_addr_attr_t     attr;
	ir_node                    *mem_proj;
	ir_node                    *in[4];
	int                         arity;
Matthias Braun's avatar
Matthias Braun committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
	const arch_register_req_t **reqs;
} amd64_args_t;

static amd64_insn_mode_t get_insn_mode_from_mode(const ir_mode *mode)
{
	switch (get_mode_size_bits(mode)) {
	case  8: return INSN_MODE_8;
	case 16: return INSN_MODE_16;
	case 32: return INSN_MODE_32;
	case 64: return INSN_MODE_64;
	}
	panic("unexpected mode");
}

Matthias Braun's avatar
Matthias Braun committed
332
static bool match_immediate_32(amd64_imm32_t *imm, const ir_node *op,
333
334
                               bool can_match_ip_relative,
                               bool upper32_dont_care)
Matthias Braun's avatar
Matthias Braun committed
335
336
{
	assert(mode_needs_gp_reg(get_irn_mode(op)));
Matthias Braun's avatar
Matthias Braun committed
337
	assert(imm->offset == 0 && imm->entity == NULL);
Matthias Braun's avatar
Matthias Braun committed
338
339
340
341
342
343
344
345
	if (is_Const(op)) {
		ir_tarval *tv = get_Const_tarval(op);
		if (!tarval_is_long(tv))
			return false;
		long    lval = get_tarval_long(tv);
		int32_t val  = (int32_t)lval;
		if ((long)val != lval)
			return false;
346
347
348
349
350
		/** the immediate value is signed extended to 64bit, sometimes
		 * this is not what we want. */
		if (!upper32_dont_care && val < 0
		    && !mode_is_signed(get_tarval_mode(tv)))
		    return false;
Matthias Braun's avatar
Matthias Braun committed
351
352
353
354
355
356
		imm->offset = val;
		return true;
	} else if (can_match_ip_relative && is_Address(op)) {
		/* TODO: check if entity is in lower 4GB address space/relative */
		ir_entity *entity = get_Address_entity(op);
		imm->entity = entity;
Matthias Braun's avatar
Matthias Braun committed
357
358
		return true;
	}
Matthias Braun's avatar
Matthias Braun committed
359
	/* TODO: SymConst, Add(SymConst, Const) ... */
Matthias Braun's avatar
Matthias Braun committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
	return false;
}

static ir_heights_t *heights;

static bool source_am_possible(ir_node *block, ir_node *node, ir_node *other)
{
	if (!is_Proj(node))
		return false;
	ir_node *load = get_Proj_pred(node);
	if (!is_Load(load))
		return false;
	assert(get_Proj_proj(node) == pn_Load_res);
	if (get_nodes_block(load) != block)
		return false;
	/* make sure we are the only user */
	if (get_irn_n_edges(node) != 1)
		return false;
	/* ia32 backend claims this can happen, use an assert for now and see
	 * if we hit it :) */
	assert(!be_is_transformed(node));
	/* make sure the other input does not depend on the load */
Matthias Braun's avatar
Matthias Braun committed
382
	if (other != NULL && get_nodes_block(other) == block
Matthias Braun's avatar
Matthias Braun committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
	    && heights_reachable_in_block(heights, other, load))
		return false;

	return true;
}

static bool needs_extension(ir_node *op)
{
	ir_mode *mode = get_irn_mode(op);
	if (get_mode_size_bits(mode) >= 32)
		return false;
	return !be_upper_bits_clean(op, mode);
}

397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
static bool use_address_matching(match_flags_t flags, ir_node *block,
                                 ir_node *op1, ir_node *op2,
                                 ir_node **out_load, ir_node **out_op)
{
	bool flags_match = flags & match_am;

	if (flags_match && source_am_possible(block, op2, op1)) {
		(*out_load) = get_Proj_pred(op2);
		(*out_op)   = op1;
		return true;
	} else if (flags_match && (flags & match_commutative)
               && source_am_possible(block, op1, op2)) {
		(*out_load) = get_Proj_pred(op1);
		(*out_op)   = op2;
		return true;
	}
	return false;
}

416
417
static void perform_address_matching(ir_node *ptr, int *arity,
                                     ir_node **in, amd64_addr_t *addr)
418
419
420
421
{
	x86_address_t maddr;
	memset(&maddr, 0, sizeof(maddr));

422
	x86_create_address_mode(&maddr, ptr, x86_create_am_normal);
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
	assert(maddr.frame_entity == NULL);

	if (maddr.base != NULL) {
		int base_input   = (*arity)++;
		addr->base_input = base_input;
		in[base_input]   = be_transform_node(maddr.base);
	} else {
		addr->base_input = NO_INPUT;
	}
	if (maddr.index != NULL) {
		int index_input = (*arity)++;
		addr->index_input = index_input;
		in[index_input]  = be_transform_node(maddr.index);
	} else {
		addr->index_input = NO_INPUT;
	}
	addr->immediate.entity = maddr.entity;
	addr->immediate.offset = maddr.offset;
	addr->log_scale        = maddr.scale;
}

Matthias Braun's avatar
Matthias Braun committed
444
445
446
static void match_binop(amd64_args_t *args, ir_node *block,
                        ir_mode *mode, ir_node *op1, ir_node *op2,
                        match_flags_t flags)
Matthias Braun's avatar
Matthias Braun committed
447
448
449
{
	memset(args, 0, sizeof(*args));

450
	bool use_am;
Matthias Braun's avatar
Matthias Braun committed
451
	bool use_immediate = flags & match_immediate;
452
	bool mode_neutral  = flags & match_mode_neutral;
Matthias Braun's avatar
Matthias Braun committed
453
454
455
456

	unsigned mode_bits = get_mode_size_bits(mode);
	if (mode_bits == 8 || mode_bits == 16)
		use_am = false;
Matthias Braun's avatar
Matthias Braun committed
457
	args->attr.base.insn_mode = get_insn_mode_from_mode(mode);
Matthias Braun's avatar
Matthias Braun committed
458
459

	/* TODO: legalize phase */
460
	if (mode_neutral) {
Matthias Braun's avatar
Matthias Braun committed
461
462
463
464
465
466
467
468
469
		op1 = skip_downconv(op1);
		op2 = skip_downconv(op2);
	} else {
		/* TODO: extend inputs? */
		(void)needs_extension;
	}

	ir_node *load;
	ir_node *op;
470
471
472

	use_am = use_address_matching(flags, block, op1, op2, &load, &op);

473
474
	if (use_immediate
	    && match_immediate_32(&args->attr.u.immediate, op2, false, mode_neutral)) {
Matthias Braun's avatar
Matthias Braun committed
475
		/* fine, we found an immediate */
Matthias Braun's avatar
Matthias Braun committed
476
477
478
		args->attr.base.base.op_mode = AMD64_OP_REG_IMM;
		args->in[args->arity++]      = be_transform_node(op1);
		args->reqs                   = reg_reqs;
479
	} else if (use_am) {
Matthias Braun's avatar
Matthias Braun committed
480
481
482
483
		ir_node *new_op        = be_transform_node(op);
		int      reg_input     = args->arity++;
		args->attr.u.reg_input = reg_input;
		args->in[reg_input]    = new_op;
484
		amd64_addr_t *addr     = &args->attr.base.addr;
Matthias Braun's avatar
Matthias Braun committed
485

486
487
488
489
490
491
492
493
494
495
496
497
498
		ir_node *ptr = get_Load_ptr(load);
		perform_address_matching(ptr, &(args->arity), args->in, addr);

		args->reqs = reg_mem_reqs;
		if (addr->base_input != NO_INPUT && addr->index_input != NO_INPUT) {
			args->reqs = reg_reg_reg_mem_reqs;
		} else if(addr->base_input != NO_INPUT || addr->index_input != NO_INPUT) {
			args->reqs = reg_reg_mem_reqs;
		}
		ir_node *new_mem    = be_transform_node(get_Load_mem(load));
		int mem_input       = args->arity++;
		args->in[mem_input] = new_mem;
		addr->mem_input     = mem_input;
499
500
501

		args->mem_proj               = be_get_Proj_for_pn(load, pn_Load_M);
		args->attr.base.base.op_mode = AMD64_OP_ADDR_REG;
Matthias Braun's avatar
Matthias Braun committed
502
503
504
505
506
	} else {
		/* simply transform the arguments */
		args->in[args->arity++] = be_transform_node(op1);
		args->in[args->arity++] = be_transform_node(op2);
		args->reqs              = reg_reg_reqs;
Matthias Braun's avatar
Matthias Braun committed
507
		args->attr.base.base.op_mode = AMD64_OP_REG_REG;
Matthias Braun's avatar
Matthias Braun committed
508
509
510
511
512
513
514
515
516
	}
}

static ir_node *gen_binop_am(ir_node *node, ir_node *op1, ir_node *op2,
                             construct_binop_func func, match_flags_t flags)
{
	ir_node *block = get_nodes_block(node);
	ir_mode *mode  = get_irn_mode(node);
	amd64_args_t args;
Matthias Braun's avatar
Matthias Braun committed
517
	match_binop(&args, block, mode, op1, op2, flags);
Matthias Braun's avatar
Matthias Braun committed
518
519
520
521

	dbg_info *const dbgi      = get_irn_dbg_info(node);
	ir_node  *const new_block = be_transform_node(block);

Matthias Braun's avatar
Matthias Braun committed
522
	ir_node *new_node = func(dbgi, new_block, args.arity, args.in, &args.attr);
Matthias Braun's avatar
Matthias Braun committed
523
524
525
526
527
528
529
530
531
532
533
	arch_set_irn_register_reqs_in(new_node, args.reqs);
	arch_set_irn_register_req_out(new_node, 0, &amd64_requirement_gp_same_0);

	if (args.mem_proj != NULL) {
		ir_node *load = get_Proj_pred(args.mem_proj);
		be_set_transformed_node(load, new_node);
	}

	return new_r_Proj(new_node, mode_gp, pn_amd64_Sub_res);
}

534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/* Transform arguments for a binop using RAX and register as input */
static void gen_binop_rax_reg(ir_node *op1, ir_node *op2, ir_node **in,
                              int *arity, amd64_op_mode_t *op_mode,
                              const arch_register_req_t ***reqs)
{
	in[(*arity)++] = be_transform_node(op1);
	in[(*arity)++] = be_transform_node(op2);
	*reqs          = rax_reg_reqs;
	*op_mode       = AMD64_OP_RAX_REG;
}

static ir_node *gen_binop_rax(ir_node *node, ir_node *op1, ir_node *op2,
                              construct_rax_binop_func make_node,
                              match_flags_t flags)
{
	bool use_am;
	bool mode_neutral  = flags & match_mode_neutral;
	assert(! (flags & match_immediate));

	ir_mode *mode = get_irn_mode(op1);

	unsigned mode_bits = get_mode_size_bits(mode);
	amd64_insn_mode_t insn_mode = get_insn_mode_from_mode(mode);

	/* TODO: legalize phase */
	if (mode_neutral) {
		op1 = skip_downconv(op1);
		op2 = skip_downconv(op2);
	} else {
		/* TODO: extend inputs? */
		(void)needs_extension;
	}

	ir_node *load;
	ir_node *op;
	ir_node *mem_proj;
	ir_node *block = get_nodes_block(node);
	ir_node *in[4];
	int      arity = 0;
	const arch_register_req_t **reqs;
	amd64_op_mode_t op_mode;
	amd64_addr_t    addr;
	memset(&addr, 0, sizeof(addr));

	use_am = use_address_matching(flags, block, op1, op2, &load, &op);
579
580
581
	if (mode_bits == 8 || mode_bits == 16)
		use_am = false;

582
583
584
585
586
	if (use_am) {
		ir_node *new_op    = be_transform_node(op);
		int      reg_input = arity++;
		in[reg_input]      = new_op;

587
588
589
590
591
592
593
594
595
596
597
598
599
600
		ir_node *ptr = get_Load_ptr(load);
		perform_address_matching(ptr, &arity, in, &addr);

		reqs = reg_mem_reqs;
		if (addr.base_input != NO_INPUT && addr.index_input != NO_INPUT) {
			reqs = reg_reg_reg_mem_reqs;
		} else if(addr.base_input != NO_INPUT || addr.index_input != NO_INPUT) {
			reqs = reg_reg_mem_reqs;
		}

		ir_node *new_mem = be_transform_node(get_Load_mem(load));
		int mem_input    = arity++;
		in[mem_input]    = new_mem;
		addr.mem_input   = mem_input;
601
602
603
604
605
606
607
608

		mem_proj                = be_get_Proj_for_pn(load, pn_Load_M);
		op_mode                 = AMD64_OP_RAX_ADDR;
	} else {
		/* simply transform the arguments */
		gen_binop_rax_reg(op1, op2, in, &arity, &op_mode, &reqs);
	}

609
	assert((size_t)arity <= ARRAY_SIZE(in));
610
611
612
613
614
615
616
617
618
	(void)mem_proj;
	dbg_info *dbgi      = get_irn_dbg_info(node);
	ir_node  *new_block = be_transform_node(block);
	ir_node  *new_node  = make_node(dbgi, new_block, arity, in, insn_mode,
	                                op_mode, addr);
	arch_set_irn_register_reqs_in(new_node, reqs);
	return new_node;
}

Matthias Braun's avatar
Matthias Braun committed
619
typedef ir_node *(*construct_shift_func)(dbg_info *dbgi, ir_node *block,
Matthias Braun's avatar
Matthias Braun committed
620
	int arity, ir_node *in[], const amd64_shift_attr_t *attr_init);
Matthias Braun's avatar
Matthias Braun committed
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635

static ir_node *gen_shift_binop(ir_node *node, ir_node *op1, ir_node *op2,
                                construct_shift_func func, match_flags_t flags)
{
	ir_mode *mode = get_irn_mode(node);
	assert(!mode_is_float(mode));

	if (get_mode_modulo_shift(mode) != 32 && get_mode_size_bits(mode) != 64)
		panic("insupported modulo shift used");

	ir_node *in[3];
	int      arity = 0;
	if (flags & match_mode_neutral) {
		op1 = skip_downconv(op1);
		in[arity++] = be_transform_node(op1);
Matthias Braun's avatar
Matthias Braun committed
636
		mode = get_mode_size_bits(mode) > 32 ? mode_gp : mode_Iu;
Matthias Braun's avatar
Matthias Braun committed
637
638
	} else {
		op1 = skip_sameconv(op1);
639
640
641

		/* Use 8/16bit operations instead of doing zext/upconv */
		in[arity++] = be_transform_node(op1);
Matthias Braun's avatar
Matthias Braun committed
642
643
644
645
646
647
648
649
650
651
652
	}

	/* we can skip any convs for the shift count, as it only uses the lowest
	 * 5/6 bits anyway */
	while (is_Conv(op2) && get_irn_n_edges(op2) == 1) {
		ir_node *const op = get_Conv_op(op2);
		if (mode_is_float(get_irn_mode(op)))
			break;
		op2 = op;
	}

Matthias Braun's avatar
Matthias Braun committed
653
654
	amd64_shift_attr_t attr;
	memset(&attr, 0, sizeof(attr));
Matthias Braun's avatar
Matthias Braun committed
655
656
657
	const arch_register_req_t **reqs;
	const arch_register_req_t  *out_req0;
	if (is_Const(op2)) {
Matthias Braun's avatar
Matthias Braun committed
658
659
660
661
		attr.base.op_mode = AMD64_OP_SHIFT_IMM;
		reqs              = reg_reqs;
		out_req0          = &amd64_requirement_gp_same_0;
		attr.immediate    = get_tarval_long(get_Const_tarval(op2));
Matthias Braun's avatar
Matthias Braun committed
662
	} else {
Matthias Braun's avatar
Matthias Braun committed
663
664
665
666
		attr.base.op_mode = AMD64_OP_SHIFT_REG;
		in[arity++]       = be_transform_node(op2);
		reqs              = reg_rcx_reqs;
		out_req0          = &amd64_requirement_gp_same_0_not_1;
Matthias Braun's avatar
Matthias Braun committed
667
	}
Matthias Braun's avatar
Matthias Braun committed
668
	attr.insn_mode = get_insn_mode_from_mode(mode);
Matthias Braun's avatar
Matthias Braun committed
669

Matthias Braun's avatar
Matthias Braun committed
670
671
672
673
	ir_node  *const block     = get_nodes_block(node);
	dbg_info *const dbgi      = get_irn_dbg_info(node);
	ir_node  *const new_block = be_transform_node(block);
	ir_node  *const new_node  = func(dbgi, new_block, arity, in, &attr);
Matthias Braun's avatar
Matthias Braun committed
674
675
676
	arch_set_irn_register_reqs_in(new_node, reqs);
	arch_set_irn_register_req_out(new_node, 0, out_req0);
	return new_node;
677
678
}

Matthias Braun's avatar
Matthias Braun committed
679
680
681
682
683
684
685
686
687
688
static ir_node *gen_Add(ir_node *const node)
{
	ir_node *op1 = get_Add_left(node);
	ir_node *op2 = get_Add_right(node);
	ir_node *res = gen_binop_am(node, op1, op2, new_bd_amd64_Add,
	                            match_immediate | match_am | match_mode_neutral
	                            | match_commutative);
	x86_mark_non_am(node);
	return res;
}
689

Matthias Braun's avatar
Matthias Braun committed
690
static ir_node *gen_Sub(ir_node *const node)
691
{
Matthias Braun's avatar
Matthias Braun committed
692
	/* do not match AM yet until we have a sub->neg+add rule in amd64_finish */
693
694
	dbg_info *const dbgi    = get_irn_dbg_info(node);
	ir_node  *const block   = be_transform_node(get_nodes_block(node));
Matthias Braun's avatar
Matthias Braun committed
695
696
	ir_node  *const op1     = get_Sub_left(node);
	ir_node  *const op2     = get_Sub_right(node);
697
698
	ir_node  *const new_op1 = be_transform_node(op1);
	ir_node  *const new_op2 = be_transform_node(op2);
699
	ir_mode  *const mode    = get_irn_mode(node);
Matthias Braun's avatar
Matthias Braun committed
700
701
702
703
704
705
706
707
708
	ir_node *in[2] = { new_op1, new_op2 };
	amd64_binop_addr_attr_t attr;
	memset(&attr, 0, sizeof(attr));
	attr.base.base.op_mode = AMD64_OP_REG_REG;
	attr.base.insn_mode    = get_insn_mode_from_mode(mode);
	ir_node *res = new_bd_amd64_Sub(dbgi, block, ARRAY_SIZE(in), in, &attr);
	arch_set_irn_register_reqs_in(res, reg_reg_reqs);
	arch_set_irn_register_req_out(res, 0, &amd64_requirement_gp_same_0);
	return new_r_Proj(res, mode_gp, pn_amd64_Sub_res);
Matthias Braun's avatar
Matthias Braun committed
709
710
711
712
713
714
715
716
717
}

static ir_node *gen_And(ir_node *const node)
{
	ir_node *op1 = get_And_left(node);
	ir_node *op2 = get_And_right(node);
	return gen_binop_am(node, op1, op2, new_bd_amd64_And,
	                    match_immediate | match_am | match_mode_neutral
	                    | match_commutative);
718
719
}

Matthias Braun's avatar
Matthias Braun committed
720
721
722
723
724
725
726
727
728
729
730
731
732
static ir_node *gen_Eor(ir_node *const node)
{
	ir_node *op1 = get_Eor_left(node);
	ir_node *op2 = get_Eor_right(node);
	return gen_binop_am(node, op1, op2, new_bd_amd64_Xor,
	                    match_immediate | match_am | match_mode_neutral
	                    | match_commutative);
}

static ir_node *gen_Or(ir_node *const node)
{
	ir_node *op1 = get_Or_left(node);
	ir_node *op2 = get_Or_right(node);
733
	return gen_binop_am(node, op1, op2, new_bd_amd64_Or,
Matthias Braun's avatar
Matthias Braun committed
734
735
736
737
738
739
	                    match_immediate | match_am | match_mode_neutral
	                    | match_commutative);
}

static ir_node *gen_Mul(ir_node *const node)
{
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
	ir_node *op1  = get_Mul_left(node);
	ir_node *op2  = get_Mul_right(node);
	ir_mode *mode = get_irn_mode(node);

	if (get_mode_size_bits(mode) < 16) {
		/* imulb only supports rax - reg form */
		ir_node *new_node =
                           gen_binop_rax(node, op1, op2, new_bd_amd64_IMul1Op,
                                         match_mode_neutral
                                       | match_commutative);
		return new_r_Proj(new_node, mode_gp, pn_amd64_IMul1Op_res_low);
	} else {
		return gen_binop_am(node, op1, op2, new_bd_amd64_IMul,
                                         match_immediate | match_am | match_mode_neutral
                                         | match_commutative);
	}
Matthias Braun's avatar
Matthias Braun committed
756
757
}

758
759
static ir_node *gen_Mulh(ir_node *const node)
{
760
761
762
763
764
765
766
767
768
769
770
771
	ir_node *op1  = get_Mulh_left(node);
	ir_node *op2  = get_Mulh_right(node);
	ir_mode *mode = get_irn_mode(op1);
	ir_node *new_node;

	if(mode_is_signed(mode)) {
		new_node = gen_binop_rax(node, op1, op2, new_bd_amd64_IMul1Op,
                        /* match_am TODO */
                        match_mode_neutral | match_commutative);
        return new_r_Proj(new_node, mode_gp, pn_amd64_IMul1Op_res_high);
	} else {
		new_node = gen_binop_rax(node, op1, op2, new_bd_amd64_Mul,
772
773
                         /* match_am TODO */
                         match_mode_neutral | match_commutative);
774
775
		return new_r_Proj(new_node, mode_gp, pn_amd64_Mul_res_high);
	}
776
777
}

Matthias Braun's avatar
Matthias Braun committed
778
779
780
781
782
static ir_node *gen_Shl(ir_node *const node)
{
	ir_node *op1 = get_Shl_left(node);
	ir_node *op2 = get_Shl_right(node);
	return gen_shift_binop(node, op1, op2, new_bd_amd64_Shl,
Matthias Braun's avatar
Matthias Braun committed
783
	                       match_immediate | match_mode_neutral);
Matthias Braun's avatar
Matthias Braun committed
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
}

static ir_node *gen_Shr(ir_node *const node)
{
	ir_node *op1 = get_Shr_left(node);
	ir_node *op2 = get_Shr_right(node);
	return gen_shift_binop(node, op1, op2, new_bd_amd64_Shr,
	                       match_immediate);
}

static ir_node *gen_Shrs(ir_node *const node)
{
	ir_node *op1 = get_Shrs_left(node);
	ir_node *op2 = get_Shrs_right(node);
	return gen_shift_binop(node, op1, op2, new_bd_amd64_Sar,
	                       match_immediate);
}
Robin Redeker's avatar
Robin Redeker committed
801

Matthias Braun's avatar
Matthias Braun committed
802
static ir_node *create_div(ir_node *const node, ir_mode *const mode,
803
                           ir_node *const op1, ir_node *const op2)
Matthias Braun's avatar
Matthias Braun committed
804
805
806
807
808
{
	dbg_info *const dbgi    = get_irn_dbg_info(node);
	ir_node  *const block   = be_transform_node(get_nodes_block(node));
	amd64_insn_mode_t insn_mode
		= get_mode_size_bits(mode) > 32 ? INSN_MODE_64 : INSN_MODE_32;
809

Matthias Braun's avatar
Matthias Braun committed
810
	ir_node *res;
811
812
813
814
815
	ir_node *in[3];
	int      arity = 0;
	const arch_register_req_t **reqs;
	amd64_op_mode_t op_mode;

Matthias Braun's avatar
Matthias Braun committed
816
	if (mode_is_signed(mode)) {
817
818
		ir_node  *const new_op1 = be_transform_node(op1);

Matthias Braun's avatar
Matthias Braun committed
819
820
821
822
823
		amd64_shift_attr_t attr;
		memset(&attr, 0, sizeof(attr));
		attr.base.op_mode = AMD64_OP_SHIFT_IMM;
		attr.insn_mode    = insn_mode;
		attr.immediate    = get_mode_size_bits(mode)-1;
824
		ir_node *sar_in[1] = { new_op1 };
Matthias Braun's avatar
Matthias Braun committed
825
		ir_node *const upper
826
827
828
829
830
			= new_bd_amd64_Sar(dbgi, block, ARRAY_SIZE(sar_in), sar_in, &attr);
		arch_set_irn_register_reqs_in(upper, reg_reqs);

		in[arity++] = upper;
		gen_binop_rax_reg(op1, op2, in, &arity, &op_mode, &reqs);
Matthias Braun's avatar
Matthias Braun committed
831
832
833

		amd64_addr_t addr;
		memset(&addr, 0, sizeof(addr));
834
835
		res = new_bd_amd64_IDiv(dbgi, block, arity, in, insn_mode,
                                op_mode, addr);
Matthias Braun's avatar
Matthias Braun committed
836
	} else {
837
838
		const arch_register_req_t **zero_reqs;
		zero_reqs           = reg_reqs;
Matthias Braun's avatar
Matthias Braun committed
839
		ir_node *const zero = new_bd_amd64_Xor0(dbgi, block);
840
841
842
843
844
		in[arity++]         = zero;
		arch_set_irn_register_reqs_in(zero, zero_reqs);

		gen_binop_rax_reg(op1, op2, in, &arity, &op_mode, &reqs);

Matthias Braun's avatar
Matthias Braun committed
845
846
		amd64_addr_t addr;
		memset(&addr, 0, sizeof(addr));
847
848
		res  = new_bd_amd64_Div(dbgi, block, arity, in, insn_mode,
                                op_mode, addr);
Matthias Braun's avatar
Matthias Braun committed
849
	}
850
851

	arch_set_irn_register_reqs_in(res, reqs);
Matthias Braun's avatar
Matthias Braun committed
852
853
854
855
856
857
858
859
860
861
	return res;
}

static ir_node *gen_Div(ir_node *const node)
{
	ir_mode *const mode = get_Div_resmode(node);
	if (!mode_needs_gp_reg(mode))
		panic("amd64: float div NIY");
	ir_node *const op1 = get_Div_left(node);
	ir_node *const op2 = get_Div_right(node);
862
	return create_div(node, mode, op1, op2);
Matthias Braun's avatar
Matthias Braun committed
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
}

static ir_node *gen_Proj_Div(ir_node *const node)
{
	ir_node *pred     = get_Proj_pred(node);
	ir_node *new_pred = be_transform_node(pred);
	long     pn       = get_Proj_proj(node);

	assert((long)pn_amd64_Div_M == (long)pn_amd64_IDiv_M);
	assert((long)pn_amd64_Div_res_div == (long)pn_amd64_IDiv_res_div);
	switch((pn_Div)pn) {
	case pn_Div_M:
		return new_r_Proj(new_pred, mode_M, pn_amd64_Div_M);
	case pn_Div_res:
		return new_r_Proj(new_pred, mode_gp, pn_amd64_Div_res_div);
	case pn_Div_X_except:
	case pn_Div_X_regular:
		panic("amd64 exception NIY");
	}
	panic("invalid Div Proj");
}

static ir_node *gen_Mod(ir_node *const node)
{
	ir_mode *const mode = get_Mod_resmode(node);
	ir_node *const op1  = get_Mod_left(node);
	ir_node *const op2  = get_Mod_right(node);
	assert(mode_needs_gp_reg(mode));
891
	return create_div(node, mode, op1, op2);
Matthias Braun's avatar
Matthias Braun committed
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
}

static ir_node *gen_Proj_Mod(ir_node *const node)
{
	ir_node *pred     = get_Proj_pred(node);
	ir_node *new_pred = be_transform_node(pred);
	long     pn       = get_Proj_proj(node);

	assert((long)pn_amd64_Div_M == (long)pn_amd64_IDiv_M);
	assert((long)pn_amd64_Div_res_mod == (long)pn_amd64_IDiv_res_mod);
	switch((pn_Mod)pn) {
	case pn_Mod_M:
		return new_r_Proj(new_pred, mode_M, pn_amd64_Div_M);
	case pn_Mod_res:
		return new_r_Proj(new_pred, mode_gp, pn_amd64_Div_res_mod);
	case pn_Mod_X_except:
	case pn_Mod_X_regular:
		panic("amd64 exception NIY");
	}
	panic("invalid Mod Proj");
}

Matthias Braun's avatar
Matthias Braun committed
914
915
916
typedef ir_node* (*unop_constructor)(dbg_info*,ir_node*block,ir_node*op,amd64_insn_mode_t insn_mode);

static ir_node *gen_unop(ir_node *const node, int op_pos, unop_constructor gen)
917
{
918
919
	dbg_info *const dbgi   = get_irn_dbg_info(node);
	ir_node  *const block  = be_transform_node(get_nodes_block(node));
920
	ir_node  *const op     = get_irn_n(node, op_pos);
921
	ir_node  *const new_op = be_transform_node(op);
Matthias Braun's avatar
Matthias Braun committed
922
	ir_mode  *const mode   = get_irn_mode(node);
923

Matthias Braun's avatar
Matthias Braun committed
924
925
926
	amd64_insn_mode_t insn_mode
		= get_mode_size_bits(mode) > 32 ? INSN_MODE_64 : INSN_MODE_32;
	return gen(dbgi, block, new_op, insn_mode);
927
928
}

929
930
931
932
static ir_node *gen_Minus(ir_node *const node)
{
	return gen_unop(node, n_Minus_op, &new_bd_amd64_Neg);
}
Matthias Braun's avatar
Matthias Braun committed
933
static ir_node *gen_Not(ir_node *const node)
934
935
936
{
	return gen_unop(node, n_Not_op, &new_bd_amd64_Not);
}
937

938
939
static ir_node *gen_Sel(ir_node *const node)
{
940
941
942
943
944
945
946
	ir_node   *block     = get_nodes_block(node);
	ir_node   *new_block = be_transform_node(block);
	dbg_info  *dbgi      = get_irn_dbg_info(node);
	ir_node   *ptr       = get_Sel_ptr(node);
	ir_graph  *irg       = get_irn_irg(node);
	ir_node   *base      = get_frame_base(irg);
	ir_entity *entity    = get_Sel_entity(node);
947
948
949
950
	if (!is_Proj(ptr) || !is_Start(get_Proj_pred(ptr)))
		panic("Sel not lowered");
	if (get_Sel_n_indexs(node) > 0)
		panic("array Sel not lowered %+F", node);
951
952
953
	if (is_parameter_entity(entity) &&
	    get_entity_parameter_number(entity) == IR_VA_START_PARAMETER_NUMBER)
	    panic("va_start NIY");
Matthias Braun's avatar
Matthias Braun committed
954
955
956
957
	amd64_addr_t addr;
	memset(&addr, 0, sizeof(addr));
	addr.base_input  = 0;
	addr.index_input = NO_INPUT;
958
	addr.immediate.entity = entity;
959
960
	ir_node *in[] = { base };
	ir_node *res = new_bd_amd64_Lea(dbgi, new_block, ARRAY_SIZE(in), in,
Matthias Braun's avatar
Matthias Braun committed
961
	                                INSN_MODE_64, addr);
Matthias Braun's avatar
Matthias Braun committed
962
	arch_set_irn_register_reqs_in(res, reg_reqs);
963
	return res;
964
965
}

966
967
static ir_node *gen_IJmp(ir_node *node)
{
968
969
970
971
972
	dbg_info *dbgi      = get_irn_dbg_info(node);
	ir_node  *block     = get_nodes_block(node);
	ir_node  *new_block = be_transform_node(block);
	ir_node  *op        = get_IJmp_target(node);
	ir_mode  *mode      = get_irn_mode(op);
973
974
975
	assert(mode == mode_P);

	int arity = 0;
976
	ir_node *in[3];
977
978
979
	amd64_addr_t addr;
	memset(&addr, 0, sizeof(addr));

980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
	const arch_register_req_t **reqs;
	amd64_op_mode_t             op_mode;
	ir_node                    *mem_proj = NULL;
	if (match_immediate_32(&addr.immediate, op, true, false)) {
		// TODO: do we need a must_match_ip_relative in match_immediate_32
		op_mode = AMD64_OP_UNOP_IMM32;
		arity   = 0;
		reqs    = no_reqs;
	} else if (source_am_possible(block, op, NULL)) {
		ir_node *load     = get_Proj_pred(op);
		ir_node *load_ptr = get_Load_ptr(load);
		mem_proj          = be_get_Proj_for_pn(load, pn_Load_M);

		perform_address_matching(load_ptr, &arity, in, &addr);
		assert((size_t)arity < ARRAY_SIZE(in));

		reqs = mem_reqs;
		if (addr.base_input != NO_INPUT && addr.index_input != NO_INPUT) {
			reqs = reg_reg_mem_reqs;
		} else if(addr.base_input != NO_INPUT || addr.index_input != NO_INPUT) {
			reqs = reg_mem_reqs;