arm_transform.c 63 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2010 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.
 */

20
21
/**
 * @file
Michael Beck's avatar
Michael Beck committed
22
 * @brief   The codegenerator (transform FIRM into arm FIRM)
23
 * @author  Matthias Braun, Oliver Richter, Tobias Gneist, Michael Beck
24
25
 * @version $Id$
 */
26
27
28
29
30
31
32
33
#include "config.h"

#include "irnode_t.h"
#include "irgraph_t.h"
#include "irmode_t.h"
#include "irgmod.h"
#include "iredges.h"
#include "ircons.h"
Michael Beck's avatar
Michael Beck committed
34
#include "irprintf.h"
35
36
37
#include "dbginfo.h"
#include "iropt_t.h"
#include "debug.h"
Michael Beck's avatar
Michael Beck committed
38
#include "error.h"
39

40
#include "../benode.h"
41
#include "../beirg.h"
42
#include "../beutil.h"
43
#include "../betranshlp.h"
44
45
#include "../beabihelper.h"
#include "../beabi.h"
46

47
#include "bearch_arm_t.h"
48
49
#include "arm_nodes_attr.h"
#include "arm_transform.h"
50
#include "arm_optimize.h"
51
52
#include "arm_new_nodes.h"
#include "arm_map_regs.h"
53
#include "arm_cconv.h"
54
55
56
57
58

#include "gen_arm_regalloc_if.h"

#include <limits.h>

59
DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
Michael Beck's avatar
Michael Beck committed
60

61
static const arch_register_t *sp_reg = &arm_registers[REG_SP];
62
static ir_mode               *mode_gp;
63
static ir_mode               *mode_fp;
64
65
static beabi_helper_env_t    *abihelper;
static calling_convention_t  *cconv = NULL;
66
static arm_isa_t             *isa;
67
68
69
70

static pmap                  *node_to_stack;

static bool mode_needs_gp_reg(ir_mode *mode)
71
{
72
	return mode_is_int(mode) || mode_is_reference(mode);
73
74
}

Michael Beck's avatar
Michael Beck committed
75
/**
76
 * create firm graph for a constant
Michael Beck's avatar
Michael Beck committed
77
 */
78
79
80
static ir_node *create_const_graph_value(dbg_info *dbgi, ir_node *block,
                                         unsigned int value)
{
81
	ir_node *result;
82
	arm_vals v, vn;
Michael Beck's avatar
Michael Beck committed
83
	int cnt;
84

85
86
87
88
89
90
	/* We only have 8 bit immediates. So we possibly have to combine several
	 * operations to construct the desired value.
	 *
	 * we can either create the value by adding bits to 0 or by removing bits
	 * from an register with all bits set. Try which alternative needs fewer
	 * operations */
91
92
	arm_gen_vals_from_word(value, &v);
	arm_gen_vals_from_word(~value, &vn);
Michael Beck's avatar
Michael Beck committed
93
94
95

	if (vn.ops < v.ops) {
		/* remove bits */
96
		result = new_bd_arm_Mvn_imm(dbgi, block, vn.values[0], vn.rors[0]);
Michael Beck's avatar
Michael Beck committed
97
98

		for (cnt = 1; cnt < vn.ops; ++cnt) {
99
100
			result = new_bd_arm_Bic_imm(dbgi, block, result,
			                            vn.values[cnt], vn.rors[cnt]);
101
		}
102
	} else {
Michael Beck's avatar
Michael Beck committed
103
		/* add bits */
104
		result = new_bd_arm_Mov_imm(dbgi, block, v.values[0], v.rors[0]);
Michael Beck's avatar
Michael Beck committed
105
106

		for (cnt = 1; cnt < v.ops; ++cnt) {
107
108
			result = new_bd_arm_Or_imm(dbgi, block, result,
			                           v.values[cnt], v.rors[cnt]);
Michael Beck's avatar
Michael Beck committed
109
		}
110
111
112
113
	}
	return result;
}

114
115
116
117
118
/**
 * Create a DAG constructing a given Const.
 *
 * @param irn  a Firm const
 */
119
120
static ir_node *create_const_graph(ir_node *irn, ir_node *block)
{
Matthias Braun's avatar
Matthias Braun committed
121
122
123
	ir_tarval *tv   = get_Const_tarval(irn);
	ir_mode   *mode = get_tarval_mode(tv);
	unsigned   value;
124
125
126
127
128
129
130

	if (mode_is_reference(mode)) {
		/* ARM is 32bit, so we can safely convert a reference tarval into Iu */
		assert(get_mode_size_bits(mode) == get_mode_size_bits(mode_Iu));
		tv = tarval_convert_to(tv, mode_Iu);
	}
	value = get_tarval_long(tv);
131
	return create_const_graph_value(get_irn_dbg_info(irn), block, value);
132
133
}

Michael Beck's avatar
Michael Beck committed
134
/**
Michael Beck's avatar
Michael Beck committed
135
136
137
138
139
140
 * Create an And that will zero out upper bits.
 *
 * @param dbgi     debug info
 * @param block    the basic block
 * @param op       the original node
 * param src_bits  number of lower bits that will remain
Michael Beck's avatar
Michael Beck committed
141
 */
142
143
144
145
146
147
148
149
150
151
152
153
static ir_node *gen_zero_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
                                   int src_bits)
{
	if (src_bits == 8) {
		return new_bd_arm_And_imm(dbgi, block, op, 0xFF, 0);
	} else if (src_bits == 16) {
		ir_node *lshift = new_bd_arm_Mov_reg_shift_imm(dbgi, block, op, ARM_SHF_LSL_IMM, 16);
		ir_node *rshift = new_bd_arm_Mov_reg_shift_imm(dbgi, block, lshift, ARM_SHF_LSR_IMM, 16);
		return rshift;
	} else {
		panic("zero extension only supported for 8 and 16 bits");
	}
154
155
}

Michael Beck's avatar
Michael Beck committed
156
157
158
/**
 * Generate code for a sign extension.
 */
159
160
161
162
163
164
static ir_node *gen_sign_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
                                   int src_bits)
{
	int shift_width = 32 - src_bits;
	ir_node *lshift_node = new_bd_arm_Mov_reg_shift_imm(dbgi, block, op, ARM_SHF_LSL_IMM, shift_width);
	ir_node *rshift_node = new_bd_arm_Mov_reg_shift_imm(dbgi, block, lshift_node, ARM_SHF_ASR_IMM, shift_width);
165
166
167
	return rshift_node;
}

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
193
194
195
static ir_node *gen_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
                              ir_mode *orig_mode)
{
	int bits = get_mode_size_bits(orig_mode);
	if (bits == 32)
		return op;

	if (mode_is_signed(orig_mode)) {
		return gen_sign_extension(dbgi, block, op, bits);
	} else {
		return gen_zero_extension(dbgi, block, op, bits);
	}
}

/**
 * returns true if it is assured, that the upper bits of a node are "clean"
 * which means for a 16 or 8 bit value, that the upper bits in the register
 * are 0 for unsigned and a copy of the last significant bit for signed
 * numbers.
 */
static bool upper_bits_clean(ir_node *transformed_node, ir_mode *mode)
{
	(void) transformed_node;
	(void) mode;
	/* TODO */
	return false;
}

Michael Beck's avatar
Michael Beck committed
196
197
198
/**
 * Transforms a Conv node.
 *
199
 * @return The created ia32 Conv node
Michael Beck's avatar
Michael Beck committed
200
 */
201
202
static ir_node *gen_Conv(ir_node *node)
{
203
204
205
206
207
208
209
210
211
212
213
	ir_node  *block    = be_transform_node(get_nodes_block(node));
	ir_node  *op       = get_Conv_op(node);
	ir_node  *new_op   = be_transform_node(op);
	ir_mode  *src_mode = get_irn_mode(op);
	ir_mode  *dst_mode = get_irn_mode(node);
	dbg_info *dbg      = get_irn_dbg_info(node);

	if (src_mode == dst_mode)
		return new_op;

	if (mode_is_float(src_mode) || mode_is_float(dst_mode)) {
214
		if (USE_FPA(isa)) {
215
216
			if (mode_is_float(src_mode)) {
				if (mode_is_float(dst_mode)) {
Michael Beck's avatar
Michael Beck committed
217
					/* from float to float */
218
					return new_bd_arm_Mvf(dbg, block, new_op, dst_mode);
219
				} else {
Michael Beck's avatar
Michael Beck committed
220
					/* from float to int */
221
					panic("TODO");
Michael Beck's avatar
Michael Beck committed
222
				}
223
			} else {
Michael Beck's avatar
Michael Beck committed
224
				/* from int to float */
225
226
227
228
229
				if (!mode_is_signed(src_mode)) {
					panic("TODO");
				} else {
					return new_bd_arm_FltX(dbg, block, new_op, dst_mode);
				}
Michael Beck's avatar
Michael Beck committed
230
			}
231
		} else if (USE_VFP(isa)) {
232
			panic("VFP not supported yet");
233
		} else {
234
			panic("Softfloat not supported yet");
Michael Beck's avatar
Michael Beck committed
235
		}
236
	} else { /* complete in gp registers */
237
238
		int src_bits = get_mode_size_bits(src_mode);
		int dst_bits = get_mode_size_bits(dst_mode);
Michael Beck's avatar
Michael Beck committed
239
240
		int min_bits;
		ir_mode *min_mode;
241

Michael Beck's avatar
Michael Beck committed
242
		if (src_bits == dst_bits) {
Michael Beck's avatar
Michael Beck committed
243
			/* kill unnecessary conv */
Michael Beck's avatar
Michael Beck committed
244
			return new_op;
245
246
247
248
249
		}

		if (src_bits < dst_bits) {
			min_bits = src_bits;
			min_mode = src_mode;
Michael Beck's avatar
Michael Beck committed
250
		} else {
251
252
253
254
255
256
257
258
259
260
261
262
			min_bits = dst_bits;
			min_mode = dst_mode;
		}

		if (upper_bits_clean(new_op, min_mode)) {
			return new_op;
		}

		if (mode_is_signed(min_mode)) {
			return gen_sign_extension(dbg, block, new_op, min_bits);
		} else {
			return gen_zero_extension(dbg, block, new_op, min_bits);
263
264
265
266
		}
	}
}

267
268
269
270
271
272
273
typedef struct {
	unsigned char  imm_8;
	unsigned char  rot;
} arm_immediate_t;

static bool try_encode_as_immediate(const ir_node *node, arm_immediate_t *res)
{
Michael Beck's avatar
Michael Beck committed
274
	unsigned val, low_pos, high_pos;
275
276
277

	if (!is_Const(node))
		return false;
Michael Beck's avatar
Michael Beck committed
278

279
	val = get_tarval_long(get_Const_tarval(node));
Michael Beck's avatar
Michael Beck committed
280

281
282
283
284
285
286
287
288
289
	if (val == 0) {
		res->imm_8 = 0;
		res->rot   = 0;
		return true;
	}
	if (val <= 0xff) {
		res->imm_8 = val;
		res->rot   = 0;
		return true;
Michael Beck's avatar
Michael Beck committed
290
	}
291
292
293
294
295
296
297
	/* arm allows to use to rotate an 8bit immediate value by a multiple of 2
	   (= 0, 2, 4, 6, ...).
	   So we determine the smallest even position with a bit set
	   and the highest even position with no bit set anymore.
	   If the difference between these 2 is <= 8, then we can encode the value
	   as immediate.
	 */
Michael Beck's avatar
Michael Beck committed
298
299
	low_pos  = ntz(val) & ~1u;
	high_pos = (32-nlz(val)+1) & ~1u;
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318

	if (high_pos - low_pos <= 8) {
		res->imm_8 = val >> low_pos;
		res->rot   = 32 - low_pos;
		return true;
	}

	if (high_pos > 24) {
		res->rot = 34 - high_pos;
		val      = val >> (32-res->rot) | val << (res->rot);
		if (val <= 0xff) {
			res->imm_8 = val;
			return true;
		}
	}

	return false;
}

319
static bool is_downconv(const ir_node *node)
320
321
322
323
324
{
	ir_mode *src_mode;
	ir_mode *dest_mode;

	if (!is_Conv(node))
325
		return false;
326
327
328
329
330

	/* we only want to skip the conv when we're the only user
	 * (not optimal but for now...)
	 */
	if (get_irn_n_edges(node) > 1)
331
		return false;
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349

	src_mode  = get_irn_mode(get_Conv_op(node));
	dest_mode = get_irn_mode(node);
	return
		mode_needs_gp_reg(src_mode)  &&
		mode_needs_gp_reg(dest_mode) &&
		get_mode_size_bits(dest_mode) <= get_mode_size_bits(src_mode);
}

static ir_node *arm_skip_downconv(ir_node *node)
{
	while (is_downconv(node))
		node = get_Conv_op(node);
	return node;
}

typedef enum {
	MATCH_NONE         = 0,
Michael Beck's avatar
Michael Beck committed
350
	MATCH_COMMUTATIVE  = 1 << 0,  /**< commutative node */
Michael Beck's avatar
Michael Beck committed
351
352
353
	MATCH_REVERSE      = 1 << 1,  /**< support reverse opcode */
	MATCH_SIZE_NEUTRAL = 1 << 2,
	MATCH_SKIP_NOT     = 1 << 3,  /**< skip Not on ONE input */
354
} match_flags_t;
355
ENUM_BITSET(match_flags_t)
356

Michael Beck's avatar
Michael Beck committed
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
 * possible binop constructors.
 */
typedef struct arm_binop_factory_t {
	/** normal reg op reg operation. */
	ir_node *(*new_binop_reg)(dbg_info *dbgi, ir_node *block, ir_node *op1, ir_node *op2);
	/** normal reg op imm operation. */
	ir_node *(*new_binop_imm)(dbg_info *dbgi, ir_node *block, ir_node *op1, unsigned char imm8, unsigned char imm_rot);
	/** barrel shifter reg op (reg shift reg operation. */
	ir_node *(*new_binop_reg_shift_reg)(dbg_info *dbgi, ir_node *block, ir_node *left, ir_node *right, ir_node *shift, arm_shift_modifier_t shift_modifier);
	/** barrel shifter reg op (reg shift imm operation. */
	ir_node *(*new_binop_reg_shift_imm)(dbg_info *dbgi, ir_node *block, ir_node *left, ir_node *right, arm_shift_modifier_t shift_modifier, unsigned shift_immediate);
} arm_binop_factory_t;
370
371

static ir_node *gen_int_binop(ir_node *node, match_flags_t flags,
Michael Beck's avatar
Michael Beck committed
372
		const arm_binop_factory_t *factory)
373
374
375
376
377
378
379
380
381
{
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *op1     = get_binop_left(node);
	ir_node  *new_op1;
	ir_node  *op2     = get_binop_right(node);
	ir_node  *new_op2;
	dbg_info *dbgi    = get_irn_dbg_info(node);
	arm_immediate_t imm;

Michael Beck's avatar
Michael Beck committed
382
383
384
385
386
387
388
389
	if (flags & MATCH_SKIP_NOT) {
		if (is_Not(op1))
			op1 = get_Not_op(op1);
		else if (is_Not(op2))
			op2 = get_Not_op(op2);
		else
			panic("cannot execute MATCH_SKIP_NOT");
	}
390
391
392
393
394
395
396
397
398
	if (flags & MATCH_SIZE_NEUTRAL) {
		op1 = arm_skip_downconv(op1);
		op2 = arm_skip_downconv(op2);
	} else {
		assert(get_mode_size_bits(get_irn_mode(node)) == 32);
	}

	if (try_encode_as_immediate(op2, &imm)) {
		ir_node *new_op1 = be_transform_node(op1);
Michael Beck's avatar
Michael Beck committed
399
		return factory->new_binop_imm(dbgi, block, new_op1, imm.imm_8, imm.rot);
400
401
	}
	new_op2 = be_transform_node(op2);
Michael Beck's avatar
Michael Beck committed
402
403
404
405
406
    if ((flags & (MATCH_COMMUTATIVE|MATCH_REVERSE)) && try_encode_as_immediate(op1, &imm)) {
		if (flags & MATCH_REVERSE)
			return factory[1].new_binop_imm(dbgi, block, new_op2, imm.imm_8, imm.rot);
		else
			return factory[0].new_binop_imm(dbgi, block, new_op2, imm.imm_8, imm.rot);
407
408
409
	}
	new_op1 = be_transform_node(op1);

Michael Beck's avatar
Michael Beck committed
410
411
412
413
414
415
416
417
418
419
420
	/* check if we can fold in a Mov */
	if (is_arm_Mov(new_op2)) {
		const arm_shifter_operand_t *attr = get_arm_shifter_operand_attr_const(new_op2);

		switch (attr->shift_modifier) {
		case ARM_SHF_IMM:
		case ARM_SHF_ASR_IMM:
		case ARM_SHF_LSL_IMM:
		case ARM_SHF_LSR_IMM:
		case ARM_SHF_ROR_IMM:
			if (factory->new_binop_reg_shift_imm) {
Michael Beck's avatar
Michael Beck committed
421
				ir_node *mov_op = get_irn_n(new_op2, 0);
Michael Beck's avatar
Michael Beck committed
422
423
424
425
426
427
428
429
430
431
				return factory->new_binop_reg_shift_imm(dbgi, block, new_op1, mov_op,
					attr->shift_modifier, attr->shift_immediate);
			}
			break;

		case ARM_SHF_ASR_REG:
		case ARM_SHF_LSL_REG:
		case ARM_SHF_LSR_REG:
		case ARM_SHF_ROR_REG:
			if (factory->new_binop_reg_shift_reg) {
Michael Beck's avatar
Michael Beck committed
432
433
				ir_node *mov_op  = get_irn_n(new_op2, 0);
				ir_node *mov_sft = get_irn_n(new_op2, 1);
Michael Beck's avatar
Michael Beck committed
434
435
436
437
				return factory->new_binop_reg_shift_reg(dbgi, block, new_op1, mov_op, mov_sft,
					attr->shift_modifier);
			}
			break;
Matthias Braun's avatar
Matthias Braun committed
438
439
440
441
442
		case ARM_SHF_REG:
		case ARM_SHF_RRX:
			break;
		case ARM_SHF_INVALID:
			panic("invalid shift");
Michael Beck's avatar
Michael Beck committed
443
444
		}
	}
Michael Beck's avatar
Michael Beck committed
445
	if ((flags & (MATCH_COMMUTATIVE|MATCH_REVERSE)) && is_arm_Mov(new_op1)) {
Michael Beck's avatar
Michael Beck committed
446
		const arm_shifter_operand_t *attr = get_arm_shifter_operand_attr_const(new_op1);
Michael Beck's avatar
Michael Beck committed
447
		int idx = flags & MATCH_REVERSE ? 1 : 0;
Michael Beck's avatar
Michael Beck committed
448
449

		switch (attr->shift_modifier) {
Matthias Braun's avatar
Matthias Braun committed
450
		ir_node *mov_op, *mov_sft;
Michael Beck's avatar
Michael Beck committed
451
452
453
454
455
456

		case ARM_SHF_IMM:
		case ARM_SHF_ASR_IMM:
		case ARM_SHF_LSL_IMM:
		case ARM_SHF_LSR_IMM:
		case ARM_SHF_ROR_IMM:
Michael Beck's avatar
Michael Beck committed
457
			if (factory[idx].new_binop_reg_shift_imm) {
Michael Beck's avatar
Michael Beck committed
458
				mov_op = get_irn_n(new_op1, 0);
Michael Beck's avatar
Michael Beck committed
459
				return factory[idx].new_binop_reg_shift_imm(dbgi, block, new_op2, mov_op,
Michael Beck's avatar
Michael Beck committed
460
461
462
463
464
465
466
467
					attr->shift_modifier, attr->shift_immediate);
			}
			break;

		case ARM_SHF_ASR_REG:
		case ARM_SHF_LSL_REG:
		case ARM_SHF_LSR_REG:
		case ARM_SHF_ROR_REG:
Michael Beck's avatar
Michael Beck committed
468
			if (factory[idx].new_binop_reg_shift_reg) {
Michael Beck's avatar
Michael Beck committed
469
470
				mov_op  = get_irn_n(new_op1, 0);
				mov_sft = get_irn_n(new_op1, 1);
Michael Beck's avatar
Michael Beck committed
471
				return factory[idx].new_binop_reg_shift_reg(dbgi, block, new_op2, mov_op, mov_sft,
Michael Beck's avatar
Michael Beck committed
472
473
474
					attr->shift_modifier);
			}
			break;
Matthias Braun's avatar
Matthias Braun committed
475
476
477
478
479
480

		case ARM_SHF_REG:
		case ARM_SHF_RRX:
			break;
		case ARM_SHF_INVALID:
			panic("invalid shift");
Michael Beck's avatar
Michael Beck committed
481
482
483
		}
	}
	return factory->new_binop_reg(dbgi, block, new_op1, new_op2);
Michael Beck's avatar
Michael Beck committed
484
}
485
486

/**
487
 * Creates an ARM Add.
488
489
490
 *
 * @return the created arm Add node
 */
491
492
static ir_node *gen_Add(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
493
494
495
496
497
498
499
500
	static const arm_binop_factory_t add_factory = {
		new_bd_arm_Add_reg,
		new_bd_arm_Add_imm,
		new_bd_arm_Add_reg_shift_reg,
		new_bd_arm_Add_reg_shift_imm
	};

	ir_mode *mode = get_irn_mode(node);
Michael Beck's avatar
Michael Beck committed
501

502
	if (mode_is_float(mode)) {
503
504
505
506
507
508
		ir_node  *block   = be_transform_node(get_nodes_block(node));
		ir_node  *op1     = get_Add_left(node);
		ir_node  *op2     = get_Add_right(node);
		dbg_info *dbgi    = get_irn_dbg_info(node);
		ir_node  *new_op1 = be_transform_node(op1);
		ir_node  *new_op2 = be_transform_node(op2);
509
		if (USE_FPA(isa)) {
510
			return new_bd_arm_Adf(dbgi, block, new_op1, new_op2, mode);
511
		} else if (USE_VFP(isa)) {
Michael Beck's avatar
Michael Beck committed
512
			assert(mode != mode_E && "IEEE Extended FP not supported");
513
			panic("VFP not supported yet");
514
		} else {
515
			panic("Softfloat not supported yet");
516
517
		}
	} else {
518
#if 0
Michael Beck's avatar
Michael Beck committed
519
		/* check for MLA */
Michael Beck's avatar
Michael Beck committed
520
		if (is_arm_Mul(new_op1) && get_irn_n_edges(op1) == 1) {
521
			new_op3 = new_op2;
Michael Beck's avatar
Michael Beck committed
522
523
			new_op2 = get_irn_n(new_op1, 1);
			new_op1 = get_irn_n(new_op1, 0);
Michael Beck's avatar
Michael Beck committed
524

525
			return new_bd_arm_Mla(dbgi, block, new_op1, new_op2, new_op3);
Michael Beck's avatar
Michael Beck committed
526
		}
Michael Beck's avatar
Michael Beck committed
527
		if (is_arm_Mul(new_op2) && get_irn_n_edges(op2) == 1) {
528
529
530
			new_op3 = new_op1;
			new_op1 = get_irn_n(new_op2, 0);
			new_op2 = get_irn_n(new_op2, 1);
Michael Beck's avatar
Michael Beck committed
531

532
			return new_bd_arm_Mla(dbgi, block, new_op1, new_op2, new_op3);
Michael Beck's avatar
Michael Beck committed
533
		}
534
#endif
535

Michael Beck's avatar
Michael Beck committed
536
		return gen_int_binop(node, MATCH_COMMUTATIVE | MATCH_SIZE_NEUTRAL, &add_factory);
Michael Beck's avatar
Michael Beck committed
537
538
	}
}
539
540

/**
541
 * Creates an ARM Mul.
542
543
544
 *
 * @return the created arm Mul node
 */
545
546
static ir_node *gen_Mul(ir_node *node)
{
547
548
549
550
551
552
553
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *op1     = get_Mul_left(node);
	ir_node  *new_op1 = be_transform_node(op1);
	ir_node  *op2     = get_Mul_right(node);
	ir_node  *new_op2 = be_transform_node(op2);
	ir_mode  *mode    = get_irn_mode(node);
	dbg_info *dbg     = get_irn_dbg_info(node);
Michael Beck's avatar
Michael Beck committed
554

555
	if (mode_is_float(mode)) {
556
		if (USE_FPA(isa)) {
557
			return new_bd_arm_Muf(dbg, block, new_op1, new_op2, mode);
558
		} else if (USE_VFP(isa)) {
Michael Beck's avatar
Michael Beck committed
559
			assert(mode != mode_E && "IEEE Extended FP not supported");
560
			panic("VFP not supported yet");
561
		} else {
562
			panic("Softfloat not supported yet");
Michael Beck's avatar
Michael Beck committed
563
		}
564
	}
565
	assert(mode_is_data(mode));
566
	return new_bd_arm_Mul(dbg, block, new_op1, new_op2);
567
568
}

569
static ir_node *gen_Div(ir_node *node)
570
{
571
	ir_node  *block   = be_transform_node(get_nodes_block(node));
572
	ir_node  *op1     = get_Div_left(node);
573
	ir_node  *new_op1 = be_transform_node(op1);
574
	ir_node  *op2     = get_Div_right(node);
575
	ir_node  *new_op2 = be_transform_node(op2);
576
	ir_mode  *mode    = get_Div_resmode(node);
577
	dbg_info *dbg     = get_irn_dbg_info(node);
Michael Beck's avatar
Michael Beck committed
578

579
	assert(mode != mode_E && "IEEE Extended FP not supported");
580
581
	/* integer division should be replaced by builtin call */
	assert(mode_is_float(mode));
582

583
	if (USE_FPA(isa)) {
584
		return new_bd_arm_Dvf(dbg, block, new_op1, new_op2, mode);
585
	} else if (USE_VFP(isa)) {
Michael Beck's avatar
Michael Beck committed
586
		assert(mode != mode_E && "IEEE Extended FP not supported");
587
		panic("VFP not supported yet");
588
	} else {
589
		panic("Softfloat not supported yet");
Michael Beck's avatar
Michael Beck committed
590
	}
Michael Beck's avatar
Michael Beck committed
591
592
}

593
594
static ir_node *gen_And(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
595
596
597
598
599
600
	static const arm_binop_factory_t and_factory = {
		new_bd_arm_And_reg,
		new_bd_arm_And_imm,
		new_bd_arm_And_reg_shift_reg,
		new_bd_arm_And_reg_shift_imm
	};
Michael Beck's avatar
Michael Beck committed
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
	static const arm_binop_factory_t bic_factory = {
		new_bd_arm_Bic_reg,
		new_bd_arm_Bic_imm,
		new_bd_arm_Bic_reg_shift_reg,
		new_bd_arm_Bic_reg_shift_imm
	};

	/* check for and not */
	ir_node *left  = get_And_left(node);
	ir_node *right = get_And_right(node);

	if (is_Not(left) || is_Not(right)) {
		return gen_int_binop(node, MATCH_COMMUTATIVE | MATCH_SIZE_NEUTRAL | MATCH_SKIP_NOT,
			&bic_factory);
	}
Michael Beck's avatar
Michael Beck committed
616
617

	return gen_int_binop(node, MATCH_COMMUTATIVE | MATCH_SIZE_NEUTRAL, &and_factory);
618
}
619

620
621
static ir_node *gen_Or(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
622
623
624
625
626
627
628
629
	static const arm_binop_factory_t or_factory = {
		new_bd_arm_Or_reg,
		new_bd_arm_Or_imm,
		new_bd_arm_Or_reg_shift_reg,
		new_bd_arm_Or_reg_shift_imm
	};

	return gen_int_binop(node, MATCH_COMMUTATIVE | MATCH_SIZE_NEUTRAL, &or_factory);
630
}
631

632
633
static ir_node *gen_Eor(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
634
635
636
637
638
639
640
641
	static const arm_binop_factory_t eor_factory = {
		new_bd_arm_Eor_reg,
		new_bd_arm_Eor_imm,
		new_bd_arm_Eor_reg_shift_reg,
		new_bd_arm_Eor_reg_shift_imm
	};

	return gen_int_binop(node, MATCH_COMMUTATIVE | MATCH_SIZE_NEUTRAL, &eor_factory);
642
}
643

644
645
static ir_node *gen_Sub(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
646
647
648
649
650
651
652
653
654
655
656
657
658
	static const arm_binop_factory_t sub_rsb_factory[2] = {
		{
			new_bd_arm_Sub_reg,
			new_bd_arm_Sub_imm,
			new_bd_arm_Sub_reg_shift_reg,
			new_bd_arm_Sub_reg_shift_imm
		},
		{
			new_bd_arm_Rsb_reg,
			new_bd_arm_Rsb_imm,
			new_bd_arm_Rsb_reg_shift_reg,
			new_bd_arm_Rsb_reg_shift_imm
		}
Michael Beck's avatar
Michael Beck committed
659
660
	};

661
662
663
664
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *op1     = get_Sub_left(node);
	ir_node  *new_op1 = be_transform_node(op1);
	ir_node  *op2     = get_Sub_right(node);
Michael Beck's avatar
Michael Beck committed
665
	ir_node  *new_op2 = be_transform_node(op2);
666
	ir_mode  *mode    = get_irn_mode(node);
667
	dbg_info *dbgi    = get_irn_dbg_info(node);
Michael Beck's avatar
Michael Beck committed
668

669
	if (mode_is_float(mode)) {
670
		if (USE_FPA(isa)) {
671
			return new_bd_arm_Suf(dbgi, block, new_op1, new_op2, mode);
672
		} else if (USE_VFP(isa)) {
Michael Beck's avatar
Michael Beck committed
673
			assert(mode != mode_E && "IEEE Extended FP not supported");
674
			panic("VFP not supported yet");
675
		} else {
676
			panic("Softfloat not supported yet");
Michael Beck's avatar
Michael Beck committed
677
		}
678
	} else {
Michael Beck's avatar
Michael Beck committed
679
		return gen_int_binop(node, MATCH_SIZE_NEUTRAL | MATCH_REVERSE, sub_rsb_factory);
Michael Beck's avatar
Michael Beck committed
680
	}
681
}
Michael Beck's avatar
Michael Beck committed
682

Michael Beck's avatar
Michael Beck committed
683
684
685
686
/**
 * Checks if a given value can be used as an immediate for the given
 * ARM shift mode.
 */
687
688
689
690
691
692
693
694
695
696
static bool can_use_shift_constant(unsigned int val,
                                   arm_shift_modifier_t modifier)
{
	if (val <= 31)
		return true;
	if (val == 32 && modifier != ARM_SHF_LSL_REG && modifier != ARM_SHF_ROR_REG)
		return true;
	return false;
}

Michael Beck's avatar
Michael Beck committed
697
698
699
700
701
702
703
/**
 * generate an ARM shift instruction.
 *
 * @param node            the node
 * @param flags           matching flags
 * @param shift_modifier  initial encoding of the desired shift operation
 */
704
static ir_node *make_shift(ir_node *node, match_flags_t flags,
705
		arm_shift_modifier_t shift_modifier)
706
{
707
708
709
710
	ir_node  *block = be_transform_node(get_nodes_block(node));
	ir_node  *op1   = get_binop_left(node);
	ir_node  *op2   = get_binop_right(node);
	dbg_info *dbgi  = get_irn_dbg_info(node);
711
712
713
714
715
716
	ir_node  *new_op1;
	ir_node  *new_op2;

	if (flags & MATCH_SIZE_NEUTRAL) {
		op1 = arm_skip_downconv(op1);
		op2 = arm_skip_downconv(op2);
717
	}
718

719
	new_op1 = be_transform_node(op1);
720
	if (is_Const(op2)) {
Matthias Braun's avatar
Matthias Braun committed
721
		ir_tarval   *tv  = get_Const_tarval(op2);
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
		unsigned int val = get_tarval_long(tv);
		assert(tarval_is_long(tv));
		if (can_use_shift_constant(val, shift_modifier)) {
			switch (shift_modifier) {
			case ARM_SHF_LSL_REG: shift_modifier = ARM_SHF_LSL_IMM; break;
			case ARM_SHF_LSR_REG: shift_modifier = ARM_SHF_LSR_IMM; break;
			case ARM_SHF_ASR_REG: shift_modifier = ARM_SHF_ASR_IMM; break;
			case ARM_SHF_ROR_REG: shift_modifier = ARM_SHF_ROR_IMM; break;
			default: panic("unexpected shift modifier");
			}
			return new_bd_arm_Mov_reg_shift_imm(dbgi, block, new_op1,
			                                    shift_modifier, val);
		}
	}

737
	new_op2 = be_transform_node(op2);
738
739
	return new_bd_arm_Mov_reg_shift_reg(dbgi, block, new_op1, new_op2,
	                                    shift_modifier);
740
741
}

742
743
744
static ir_node *gen_Shl(ir_node *node)
{
	return make_shift(node, MATCH_SIZE_NEUTRAL, ARM_SHF_LSL_REG);
745
746
}

747
748
749
static ir_node *gen_Shr(ir_node *node)
{
	return make_shift(node, MATCH_NONE, ARM_SHF_LSR_REG);
750
751
}

752
753
754
static ir_node *gen_Shrs(ir_node *node)
{
	return make_shift(node, MATCH_NONE, ARM_SHF_ASR_REG);
755
756
}

757
758
static ir_node *gen_Ror(ir_node *node, ir_node *op1, ir_node *op2)
{
Michael Beck's avatar
Michael Beck committed
759
760
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *new_op1 = be_transform_node(op1);
761
	dbg_info *dbgi    = get_irn_dbg_info(node);
Michael Beck's avatar
Michael Beck committed
762
763
	ir_node  *new_op2 = be_transform_node(op2);

764
765
	return new_bd_arm_Mov_reg_shift_reg(dbgi, block, new_op1, new_op2,
	                                    ARM_SHF_ROR_REG);
Michael Beck's avatar
Michael Beck committed
766
767
}

768
769
static ir_node *gen_Rol(ir_node *node, ir_node *op1, ir_node *op2)
{
Michael Beck's avatar
Michael Beck committed
770
771
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *new_op1 = be_transform_node(op1);
772
	dbg_info *dbgi    = get_irn_dbg_info(node);
Michael Beck's avatar
Michael Beck committed
773
774
	ir_node  *new_op2 = be_transform_node(op2);

775
	/* Note: there is no Rol on arm, we have to use Ror */
776
777
778
	new_op2 = new_bd_arm_Rsb_imm(dbgi, block, new_op2, 32, 0);
	return new_bd_arm_Mov_reg_shift_reg(dbgi, block, new_op1, new_op2,
	                                    ARM_SHF_ROR_REG);
Michael Beck's avatar
Michael Beck committed
779
780
}

781
782
static ir_node *gen_Rotl(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
783
784
785
786
787
788
789
790
791
	ir_node *rotate = NULL;
	ir_node *op1    = get_Rotl_left(node);
	ir_node *op2    = get_Rotl_right(node);

	/* Firm has only RotL, so we are looking for a right (op2)
	   operand "-e+mode_size_bits" (it's an already modified "mode_size_bits-e",
	   that means we can create a RotR. */

	if (is_Add(op2)) {
Michael Beck's avatar
Michael Beck committed
792
		ir_node *right = get_Add_right(op2);
Michael Beck's avatar
Michael Beck committed
793
		if (is_Const(right)) {
Matthias Braun's avatar
Matthias Braun committed
794
795
796
797
			ir_tarval *tv   = get_Const_tarval(right);
			ir_mode   *mode = get_irn_mode(node);
			long       bits = get_mode_size_bits(mode);
			ir_node   *left = get_Add_left(op2);
Michael Beck's avatar
Michael Beck committed
798
799

			if (is_Minus(left) &&
Michael Beck's avatar
Michael Beck committed
800
			    tarval_is_long(tv)          &&
Michael Beck's avatar
Michael Beck committed
801
802
803
804
			    get_tarval_long(tv) == bits &&
			    bits                == 32)
				rotate = gen_Ror(node, op1, get_Minus_op(left));
		}
Michael Beck's avatar
Michael Beck committed
805
806
807
	} else if (is_Sub(op2)) {
		ir_node *left = get_Sub_left(op2);
		if (is_Const(left)) {
Matthias Braun's avatar
Matthias Braun committed
808
809
810
811
			ir_tarval *tv   = get_Const_tarval(left);
			ir_mode   *mode = get_irn_mode(node);
			long       bits = get_mode_size_bits(mode);
			ir_node   *right = get_Sub_right(op2);
Michael Beck's avatar
Michael Beck committed
812
813
814
815
816
817

			if (tarval_is_long(tv)          &&
			    get_tarval_long(tv) == bits &&
			    bits                == 32)
				rotate = gen_Ror(node, op1, right);
		}
818
	} else if (is_Const(op2)) {
Matthias Braun's avatar
Matthias Braun committed
819
820
821
		ir_tarval *tv   = get_Const_tarval(op2);
		ir_mode   *mode = get_irn_mode(node);
		long       bits = get_mode_size_bits(mode);
Michael Beck's avatar
Michael Beck committed
822

823
824
825
826
		if (tarval_is_long(tv) && bits == 32) {
			ir_node  *block   = be_transform_node(get_nodes_block(node));
			ir_node  *new_op1 = be_transform_node(op1);
			dbg_info *dbgi    = get_irn_dbg_info(node);
827

828
829
830
			bits = (bits - get_tarval_long(tv)) & 31;
			rotate = new_bd_arm_Mov_reg_shift_imm(dbgi, block, new_op1, ARM_SHF_ROR_IMM, bits);
		}
Michael Beck's avatar
Michael Beck committed
831
832
833
834
835
836
837
838
839
	}

	if (rotate == NULL) {
		rotate = gen_Rol(node, op1, op2);
	}

	return rotate;
}

840
841
static ir_node *gen_Not(ir_node *node)
{
842
843
844
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *op      = get_Not_op(node);
	ir_node  *new_op  = be_transform_node(op);
845
	dbg_info *dbgi    = get_irn_dbg_info(node);
Michael Beck's avatar
Michael Beck committed
846

Michael Beck's avatar
Michael Beck committed
847
848
849
850
851
	/* check if we can fold in a Mov */
	if (is_arm_Mov(new_op)) {
		const arm_shifter_operand_t *attr = get_arm_shifter_operand_attr_const(new_op);

		switch (attr->shift_modifier) {
Matthias Braun's avatar
Matthias Braun committed
852
		ir_node *mov_op, *mov_sft;
Michael Beck's avatar
Michael Beck committed
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870

		case ARM_SHF_IMM:
		case ARM_SHF_ASR_IMM:
		case ARM_SHF_LSL_IMM:
		case ARM_SHF_LSR_IMM:
		case ARM_SHF_ROR_IMM:
			mov_op = get_irn_n(new_op, 0);
			return new_bd_arm_Mvn_reg_shift_imm(dbgi, block, mov_op,
				attr->shift_modifier, attr->shift_immediate);

		case ARM_SHF_ASR_REG:
		case ARM_SHF_LSL_REG:
		case ARM_SHF_LSR_REG:
		case ARM_SHF_ROR_REG:
			mov_op  = get_irn_n(new_op, 0);
			mov_sft = get_irn_n(new_op, 1);
			return new_bd_arm_Mvn_reg_shift_reg(dbgi, block, mov_op, mov_sft,
				attr->shift_modifier);
Matthias Braun's avatar
Matthias Braun committed
871
872
873
874
875
876

		case ARM_SHF_REG:
		case ARM_SHF_RRX:
			break;
		case ARM_SHF_INVALID:
			panic("invalid shift");
Michael Beck's avatar
Michael Beck committed
877
878
		}
	}
879
880

	return new_bd_arm_Mvn_reg(dbgi, block, new_op);
881
882
}

883
884
static ir_node *gen_Minus(ir_node *node)
{
885
886
887
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *op      = get_Minus_op(node);
	ir_node  *new_op  = be_transform_node(op);
888
	dbg_info *dbgi    = get_irn_dbg_info(node);
889
	ir_mode  *mode    = get_irn_mode(node);
Michael Beck's avatar
Michael Beck committed
890

891
	if (mode_is_float(mode)) {
892
		if (USE_FPA(isa)) {
893
			return new_bd_arm_Mvf(dbgi, block, op, mode);
894
		} else if (USE_VFP(isa)) {
Michael Beck's avatar
Michael Beck committed
895
			assert(mode != mode_E && "IEEE Extended FP not supported");
896
			panic("VFP not supported yet");
897
		} else {
898
			panic("Softfloat not supported yet");
Michael Beck's avatar
Michael Beck committed
899
		}
900
	}
901
	assert(mode_is_data(mode));
902
	return new_bd_arm_Rsb_imm(dbgi, block, new_op, 0, 0);
903
904
}

905
906
static ir_node *gen_Load(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
907
908
909
910
911
912
	ir_node  *block    = be_transform_node(get_nodes_block(node));
	ir_node  *ptr      = get_Load_ptr(node);
	ir_node  *new_ptr  = be_transform_node(ptr);
	ir_node  *mem      = get_Load_mem(node);
	ir_node  *new_mem  = be_transform_node(mem);
	ir_mode  *mode     = get_Load_mode(node);
913
	dbg_info *dbgi      = get_irn_dbg_info(node);
Michael Beck's avatar
Michael Beck committed
914
	ir_node  *new_load = NULL;
915

916
917
918
	if (get_Load_unaligned(node) == align_non_aligned)
		panic("arm: unaligned Loads not supported yet");

Michael Beck's avatar
Michael Beck committed
919
	if (mode_is_float(mode)) {
920
		if (USE_FPA(isa)) {
921
922
			new_load = new_bd_arm_Ldf(dbgi, block, new_ptr, new_mem, mode,
			                          NULL, 0, 0, false);
923
		} else if (USE_VFP(isa)) {
Michael Beck's avatar
Michael Beck committed
924
			assert(mode != mode_E && "IEEE Extended FP not supported");
925
			panic("VFP not supported yet");
926
		} else {
927
			panic("Softfloat not supported yet");
Michael Beck's avatar
Michael Beck committed
928
		}
929
	} else {
930
		assert(mode_is_data(mode) && "unsupported mode for Load");
931

932
		new_load = new_bd_arm_Ldr(dbgi, block, new_ptr, new_mem, mode, NULL, 0, 0, false);
933
	}
Michael Beck's avatar
Michael Beck committed
934
	set_irn_pinned(new_load, get_irn_pinned(node));
935
936
937
938

	/* check for special case: the loaded value might not be used */
	if (be_get_Proj_for_pn(node, pn_Load_res) == NULL) {
		/* add a result proj and a Keep to produce a pseudo use */
939
		ir_node *proj = new_r_Proj(new_load, mode_Iu, pn_arm_Ldr_res);
940
		be_new_Keep(block, 1, &proj);
941
942
	}

Michael Beck's avatar
Michael Beck committed
943
	return new_load;
944
945
}

946
947
static ir_node *gen_Store(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
948
949
950
951
952
953
954
955
	ir_node  *block    = be_transform_node(get_nodes_block(node));
	ir_node  *ptr      = get_Store_ptr(node);
	ir_node  *new_ptr  = be_transform_node(ptr);
	ir_node  *mem      = get_Store_mem(node);
	ir_node  *new_mem  = be_transform_node(mem);
	ir_node  *val      = get_Store_value(node);
	ir_node  *new_val  = be_transform_node(val);
	ir_mode  *mode     = get_irn_mode(val);
956
	dbg_info *dbgi     = get_irn_dbg_info(node);
Michael Beck's avatar
Michael Beck committed
957
	ir_node *new_store = NULL;
958

959
960
961
	if (get_Store_unaligned(node) == align_non_aligned)
		panic("arm: unaligned Stores not supported yet");

Michael Beck's avatar
Michael Beck committed
962
	if (mode_is_float(mode)) {
963
		if (USE_FPA(isa)) {
964
965
			new_store = new_bd_arm_Stf(dbgi, block, new_ptr, new_val,
			                           new_mem, mode, NULL, 0, 0, false);
966
		} else if (USE_VFP(isa)) {
Michael Beck's avatar
Michael Beck committed
967
			assert(mode != mode_E && "IEEE Extended FP not supported");
968
			panic("VFP not supported yet");
Michael Beck's avatar
Michael Beck committed
969
		} else {
970
			panic("Softfloat not supported yet");
Michael Beck's avatar
Michael Beck committed
971
972
		}
	} else {
973
		assert(mode_is_data(mode) && "unsupported mode for Store");
974
975
		new_store = new_bd_arm_Str(dbgi, block, new_ptr, new_val, new_mem, mode,
		                           NULL, 0, 0, false);
976
	}
Michael Beck's avatar
Michael Beck committed
977
978
	set_irn_pinned(new_store, get_irn_pinned(node));
	return new_store;
979
980
}

981
982
983
984
985
986
987
988
989
static ir_node *gen_Jmp(ir_node *node)
{
	ir_node  *block     = get_nodes_block(node);
	ir_node  *new_block = be_transform_node(block);
	dbg_info *dbgi      = get_irn_dbg_info(node);

	return new_bd_arm_Jmp(dbgi, new_block);
}

990
991
static ir_node *gen_SwitchJmp(ir_node *node)
{
992
993
	ir_node  *block    = be_transform_node(get_nodes_block(node));
	ir_node  *selector = get_Cond_selector(node);
994
995
996
997
998
999
1000
	dbg_info *dbgi     = get_irn_dbg_info(node);
	ir_node *new_op = be_transform_node(selector);
	ir_node *const_graph;
	ir_node *sub;

	ir_node *proj;
	const ir_edge_t *edge;