arm_transform.c 68.2 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

6
7
/**
 * @file
Michael Beck's avatar
Michael Beck committed
8
 * @brief   The codegenerator (transform FIRM into arm FIRM)
9
 * @author  Matthias Braun, Oliver Richter, Tobias Gneist, Michael Beck
10
 */
11
#include "bearch_arm_t.h"
12
13
14
15
16
17
18
19
20
#include "irnode_t.h"
#include "irgraph_t.h"
#include "irmode_t.h"
#include "irgmod.h"
#include "iredges.h"
#include "ircons.h"
#include "dbginfo.h"
#include "iropt_t.h"
#include "debug.h"
Matthias Braun's avatar
Matthias Braun committed
21
#include "panic.h"
22
#include "util.h"
23

24
25
26
27
#include "benode.h"
#include "beirg.h"
#include "beutil.h"
#include "betranshlp.h"
28
29
30

#include "arm_nodes_attr.h"
#include "arm_transform.h"
31
#include "arm_optimize.h"
32
#include "arm_new_nodes.h"
33
#include "arm_cconv.h"
34
35

#include "gen_arm_regalloc_if.h"
Matthias Braun's avatar
Matthias Braun committed
36
#include "gen_arm_new_nodes.h"
37
38
39

#include <limits.h>

40
41
#define ARM_PO2_STACK_ALIGNMENT 3

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

44
static const arch_register_t *sp_reg = &arm_registers[REG_SP];
45
static ir_mode               *mode_fp;
46
static be_stackorder_t       *stackorder;
47
static calling_convention_t  *cconv = NULL;
48
49
static be_start_info_t        start_mem;
static be_start_info_t        start_sp;
50
51
static unsigned               start_params_offset;
static unsigned               start_callee_saves_offset;
52
53
static pmap                  *node_to_stack;

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
static const arch_register_t *const callee_saves[] = {
	&arm_registers[REG_R4],
	&arm_registers[REG_R5],
	&arm_registers[REG_R6],
	&arm_registers[REG_R7],
	&arm_registers[REG_R8],
	&arm_registers[REG_R9],
	&arm_registers[REG_R10],
	&arm_registers[REG_R11],
	&arm_registers[REG_LR],
};

static const arch_register_t *const caller_saves[] = {
	&arm_registers[REG_R0],
	&arm_registers[REG_R1],
	&arm_registers[REG_R2],
	&arm_registers[REG_R3],
	&arm_registers[REG_LR],

	&arm_registers[REG_F0],
	&arm_registers[REG_F1],
	&arm_registers[REG_F2],
	&arm_registers[REG_F3],
	&arm_registers[REG_F4],
	&arm_registers[REG_F5],
	&arm_registers[REG_F6],
	&arm_registers[REG_F7],
};

Matthias Braun's avatar
Matthias Braun committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
void arm_gen_vals_from_word(uint32_t value, arm_vals *result)
{
	/* TODO: not optimal yet, as we only "shift" the value and don't take
	 * advantage of rotations */

	/* special case: we prefer shift amount 0 */
	if (value <= 0xFF) {
		result->values[0] = value;
		result->rors[0]   = 0;
		result->ops       = 1;
		return;
	}

	int initial = 0;
	result->ops = 0;
	do {
		while ((value & 0x3) == 0) {
			value  >>= 2;
			initial += 2;
		}

		result->values[result->ops] = value & 0xFF;
		result->rors[result->ops]   = (32-initial) % 32;
		++result->ops;

		value  >>= 8;
		initial += 8;
	} while (value != 0);
}

Michael Beck's avatar
Michael Beck committed
113
/**
114
 * create firm graph for a constant
Michael Beck's avatar
Michael Beck committed
115
 */
116
static ir_node *create_const_graph_value(dbg_info *dbgi, ir_node *block,
Matthias Braun's avatar
Matthias Braun committed
117
                                         uint32_t value)
118
{
119
120
121
122
123
124
	/* 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 */
Matthias Braun's avatar
Matthias Braun committed
125
	arm_vals v;
126
	arm_gen_vals_from_word(value, &v);
Matthias Braun's avatar
Matthias Braun committed
127
	arm_vals vn;
128
	arm_gen_vals_from_word(~value, &vn);
Michael Beck's avatar
Michael Beck committed
129

Matthias Braun's avatar
Matthias Braun committed
130
	ir_node *result;
Michael Beck's avatar
Michael Beck committed
131
132
	if (vn.ops < v.ops) {
		/* remove bits */
133
		result = new_bd_arm_Mvn_imm(dbgi, block, vn.values[0], vn.rors[0]);
Michael Beck's avatar
Michael Beck committed
134

Matthias Braun's avatar
Matthias Braun committed
135
		for (unsigned cnt = 1; cnt < vn.ops; ++cnt) {
136
137
			result = new_bd_arm_Bic_imm(dbgi, block, result,
			                            vn.values[cnt], vn.rors[cnt]);
138
		}
139
	} else {
Michael Beck's avatar
Michael Beck committed
140
		/* add bits */
141
		result = new_bd_arm_Mov_imm(dbgi, block, v.values[0], v.rors[0]);
Michael Beck's avatar
Michael Beck committed
142

Matthias Braun's avatar
Matthias Braun committed
143
		for (unsigned cnt = 1; cnt < v.ops; ++cnt) {
144
145
			result = new_bd_arm_Or_imm(dbgi, block, result,
			                           v.values[cnt], v.rors[cnt]);
Michael Beck's avatar
Michael Beck committed
146
		}
147
148
149
150
	}
	return result;
}

151
152
153
154
155
/**
 * Create a DAG constructing a given Const.
 *
 * @param irn  a Firm const
 */
156
157
static ir_node *create_const_graph(ir_node *irn, ir_node *block)
{
Matthias Braun's avatar
Matthias Braun committed
158
159
	ir_tarval *tv   = get_Const_tarval(irn);
	ir_mode   *mode = get_tarval_mode(tv);
160
161
	if (mode_is_reference(mode)) {
		/* ARM is 32bit, so we can safely convert a reference tarval into Iu */
162
163
		assert(get_mode_size_bits(mode) == get_mode_size_bits(arm_mode_gp));
		tv = tarval_convert_to(tv, arm_mode_gp);
164
	}
Matthias Braun's avatar
Matthias Braun committed
165
	long value = get_tarval_long(tv);
166
	return create_const_graph_value(get_irn_dbg_info(irn), block, value);
167
168
}

Michael Beck's avatar
Michael Beck committed
169
/**
Michael Beck's avatar
Michael Beck committed
170
171
172
173
174
175
 * 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
176
 */
177
static ir_node *gen_zero_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
Matthias Braun's avatar
Matthias Braun committed
178
                                   unsigned src_bits)
179
180
181
182
183
184
185
186
187
188
{
	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");
	}
189
190
}

Michael Beck's avatar
Michael Beck committed
191
192
193
/**
 * Generate code for a sign extension.
 */
194
static ir_node *gen_sign_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
Matthias Braun's avatar
Matthias Braun committed
195
                                   unsigned src_bits)
196
{
Matthias Braun's avatar
Matthias Braun committed
197
	unsigned shift_width = 32 - src_bits;
198
199
	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);
200
201
202
	return rshift_node;
}

203
204
205
static ir_node *gen_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
                              ir_mode *orig_mode)
{
Matthias Braun's avatar
Matthias Braun committed
206
	unsigned bits = get_mode_size_bits(orig_mode);
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
	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)
{
Matthias Braun's avatar
Matthias Braun committed
225
226
	(void)transformed_node;
	(void)mode;
227
228
229
230
	/* TODO */
	return false;
}

Michael Beck's avatar
Michael Beck committed
231
232
233
/**
 * Transforms a Conv node.
 *
234
 * @return The created ia32 Conv node
Michael Beck's avatar
Michael Beck committed
235
 */
236
237
static ir_node *gen_Conv(ir_node *node)
{
238
239
240
241
242
243
244
245
246
247
248
	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)) {
249
		if (arm_cg_config.fpu == ARM_FPU_FPA) {
250
251
			if (mode_is_float(src_mode)) {
				if (mode_is_float(dst_mode)) {
Michael Beck's avatar
Michael Beck committed
252
					/* from float to float */
253
					return new_bd_arm_Mvf(dbg, block, new_op, dst_mode);
254
				} else {
Michael Beck's avatar
Michael Beck committed
255
					/* from float to int */
256
					panic("TODO");
Michael Beck's avatar
Michael Beck committed
257
				}
258
			} else {
Michael Beck's avatar
Michael Beck committed
259
				/* from int to float */
260
261
262
263
264
				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
265
			}
266
		} else {
267
			panic("softfloat not lowered");
Michael Beck's avatar
Michael Beck committed
268
		}
269
	} else { /* complete in gp registers */
Matthias Braun's avatar
Matthias Braun committed
270
271
		unsigned src_bits = get_mode_size_bits(src_mode);
		unsigned dst_bits = get_mode_size_bits(dst_mode);
Michael Beck's avatar
Michael Beck committed
272
		if (src_bits == dst_bits) {
Michael Beck's avatar
Michael Beck committed
273
			/* kill unnecessary conv */
Michael Beck's avatar
Michael Beck committed
274
			return new_op;
275
276
		}

Matthias Braun's avatar
Matthias Braun committed
277
278
		unsigned min_bits;
		ir_mode *min_mode;
279
280
281
		if (src_bits < dst_bits) {
			min_bits = src_bits;
			min_mode = src_mode;
Michael Beck's avatar
Michael Beck committed
282
		} else {
283
284
285
286
287
288
289
290
291
292
293
294
			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);
295
296
297
298
		}
	}
}

299
typedef struct {
Matthias Braun's avatar
Matthias Braun committed
300
301
	uint8_t imm_8;
	uint8_t rot;
302
303
304
305
306
307
} arm_immediate_t;

static bool try_encode_as_immediate(const ir_node *node, arm_immediate_t *res)
{
	if (!is_Const(node))
		return false;
Michael Beck's avatar
Michael Beck committed
308

309
	uint32_t val = get_Const_long(node);
Michael Beck's avatar
Michael Beck committed
310

311
312
313
314
315
316
317
318
319
	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
320
	}
321
322
323
324
325
326
327
	/* 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.
	 */
Matthias Braun's avatar
Matthias Braun committed
328
329
	unsigned low_pos  = ntz(val) & ~1u;
	unsigned high_pos = (32-nlz(val)+1) & ~1u;
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348

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

349
static bool is_downconv(const ir_node *node)
350
351
{
	if (!is_Conv(node))
352
		return false;
353
354

	/* we only want to skip the conv when we're the only user
Matthias Braun's avatar
Matthias Braun committed
355
	 * (not optimal but for now...) */
356
	if (get_irn_n_edges(node) > 1)
357
		return false;
358

Matthias Braun's avatar
Matthias Braun committed
359
360
	ir_mode *src_mode  = get_irn_mode(get_Conv_op(node));
	ir_mode *dest_mode = get_irn_mode(node);
Matthias Braun's avatar
Matthias Braun committed
361
362
	return get_mode_arithmetic(src_mode) == irma_twos_complement
	    && get_mode_arithmetic(dest_mode) == irma_twos_complement
Matthias Braun's avatar
Matthias Braun committed
363
	    && get_mode_size_bits(dest_mode) <= get_mode_size_bits(src_mode);
364
365
366
367
368
369
370
371
372
}

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

Matthias Braun's avatar
Matthias Braun committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
static bool is_sameconv(const ir_node *node)
{
	if (!is_Conv(node))
		return false;
	if (get_irn_n_edges(node) > 1)
		return false;
	ir_mode *src_mode = get_irn_mode(get_Conv_op(node));
	ir_mode *dst_mode = get_irn_mode(node);
	return get_mode_arithmetic(src_mode) == irma_twos_complement
	    && get_mode_arithmetic(dst_mode) == irma_twos_complement
	    && get_mode_size_bits(src_mode) == get_mode_size_bits(dst_mode);
}

static ir_node *arm_skip_sameconv(ir_node *node)
{
	while (is_sameconv(node))
		node = get_Conv_op(node);
	return node;
}

393
394
typedef enum {
	MATCH_NONE         = 0,
Michael Beck's avatar
Michael Beck committed
395
	MATCH_COMMUTATIVE  = 1 << 0,  /**< commutative node */
Michael Beck's avatar
Michael Beck committed
396
397
	MATCH_REVERSE      = 1 << 1,  /**< support reverse opcode */
	MATCH_SIZE_NEUTRAL = 1 << 2,
398
} match_flags_t;
399
ENUM_BITSET(match_flags_t)
400

Michael Beck's avatar
Michael Beck committed
401
402
403
404
405
406
407
408
409
410
411
412
413
/**
 * 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;
414

Matthias Braun's avatar
Matthias Braun committed
415
416
417
static ir_node *gen_int_binop_ops(ir_node *node, ir_node *op1, ir_node *op2,
                                  match_flags_t flags,
                                  const arm_binop_factory_t *factory)
418
{
Matthias Braun's avatar
Matthias Braun committed
419
420
	ir_node  *block = be_transform_node(get_nodes_block(node));
	dbg_info *dbgi  = get_irn_dbg_info(node);
421
422
423
424
425
426

	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);
Matthias Braun's avatar
Matthias Braun committed
427
428
		op1 = arm_skip_sameconv(op1);
		op2 = arm_skip_sameconv(op2);
429
430
	}

Matthias Braun's avatar
Matthias Braun committed
431
	arm_immediate_t imm;
432
	if (try_encode_as_immediate(op2, &imm)) {
Matthias Braun's avatar
Matthias Braun committed
433
		ir_node *new_op1 = be_transform_node(op1);
Michael Beck's avatar
Michael Beck committed
434
		return factory->new_binop_imm(dbgi, block, new_op1, imm.imm_8, imm.rot);
435
	}
Matthias Braun's avatar
Matthias Braun committed
436
	ir_node *new_op2 = be_transform_node(op2);
Michael Beck's avatar
Michael Beck committed
437
438
439
440
441
    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);
442
	}
Matthias Braun's avatar
Matthias Braun committed
443
	ir_node *new_op1 = be_transform_node(op1);
444

Michael Beck's avatar
Michael Beck committed
445
446
447
448
449
450
451
452
453
454
455
	/* 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) {
456
				ir_node *mov_op = get_irn_n(new_op2, n_arm_Mov_Rm);
Michael Beck's avatar
Michael Beck committed
457
458
459
460
461
462
463
464
465
466
				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) {
467
468
				ir_node *mov_op  = get_irn_n(new_op2, n_arm_Mov_Rm);
				ir_node *mov_sft = get_irn_n(new_op2, n_arm_Mov_Rs);
Michael Beck's avatar
Michael Beck committed
469
470
471
472
				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
473
474
475
476
477
		case ARM_SHF_REG:
		case ARM_SHF_RRX:
			break;
		case ARM_SHF_INVALID:
			panic("invalid shift");
Michael Beck's avatar
Michael Beck committed
478
479
		}
	}
Michael Beck's avatar
Michael Beck committed
480
	if ((flags & (MATCH_COMMUTATIVE|MATCH_REVERSE)) && is_arm_Mov(new_op1)) {
Michael Beck's avatar
Michael Beck committed
481
		const arm_shifter_operand_t *attr = get_arm_shifter_operand_attr_const(new_op1);
Michael Beck's avatar
Michael Beck committed
482
		int idx = flags & MATCH_REVERSE ? 1 : 0;
Michael Beck's avatar
Michael Beck committed
483
484
485
486
487
488
489

		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:
Michael Beck's avatar
Michael Beck committed
490
			if (factory[idx].new_binop_reg_shift_imm) {
491
				ir_node *mov_op = get_irn_n(new_op1, n_arm_Mov_Rm);
Michael Beck's avatar
Michael Beck committed
492
				return factory[idx].new_binop_reg_shift_imm(dbgi, block, new_op2, mov_op,
Michael Beck's avatar
Michael Beck committed
493
494
495
496
497
498
499
500
					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
501
			if (factory[idx].new_binop_reg_shift_reg) {
502
503
				ir_node *mov_op  = get_irn_n(new_op1, n_arm_Mov_Rm);
				ir_node *mov_sft = get_irn_n(new_op1, n_arm_Mov_Rs);
Michael Beck's avatar
Michael Beck committed
504
				return factory[idx].new_binop_reg_shift_reg(dbgi, block, new_op2, mov_op, mov_sft,
Michael Beck's avatar
Michael Beck committed
505
506
507
					attr->shift_modifier);
			}
			break;
Matthias Braun's avatar
Matthias Braun committed
508
509
510
511
512
513

		case ARM_SHF_REG:
		case ARM_SHF_RRX:
			break;
		case ARM_SHF_INVALID:
			panic("invalid shift");
Michael Beck's avatar
Michael Beck committed
514
515
516
		}
	}
	return factory->new_binop_reg(dbgi, block, new_op1, new_op2);
Michael Beck's avatar
Michael Beck committed
517
}
518

Matthias Braun's avatar
Matthias Braun committed
519
520
521
522
523
524
525
526
static ir_node *gen_int_binop(ir_node *node, match_flags_t flags,
                              const arm_binop_factory_t *factory)
{
	ir_node *op1 = get_binop_left(node);
	ir_node *op2 = get_binop_right(node);
	return gen_int_binop_ops(node, op1, op2, flags, factory);
}

527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
static ir_node *gen_Ror(ir_node *node, ir_node *op1, ir_node *op2,
                        bool negate_op)
{
	dbg_info *dbgi    = get_irn_dbg_info(node);
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *new_op1 = be_transform_node(op1);
	if (is_Const(op2)) {
		ir_tarval *tv   = get_Const_tarval(op2);
		ir_mode   *mode = get_irn_mode(node);
		long       bits = get_mode_size_bits(mode);
		if (tarval_is_long(tv) && bits == 32) {
			long val = get_tarval_long(tv);
			val = (negate_op ? bits - val : val) & 31;
			return new_bd_arm_Mov_reg_shift_imm(dbgi, block, new_op1, ARM_SHF_ROR_IMM, val);
		}
	}

Matthias Braun's avatar
Matthias Braun committed
544
	ir_node *new_op2 = be_transform_node(op2);
545
546
547
548
549
550
551
	if (negate_op) {
		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);
}

Matthias Braun's avatar
Matthias Braun committed
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
static bool is_low_mask(ir_tarval *tv)
{
	return get_tarval_popcount(tv) == 16 && get_tarval_highest_bit(tv) == 15;
}

static bool is_high_mask(ir_tarval *tv)
{
	return get_tarval_popcount(tv) == 16 && get_tarval_lowest_bit(tv) == 16;
}

static ir_node *match_pkh(ir_node *node)
{
	assert(is_Or(node) || is_Add(node));
	ir_node *left  = get_binop_left(node);
	ir_node *right = get_binop_right(node);
	if (!is_And(left) || !is_And(right))
		return NULL;
	ir_node *left_right  = get_And_right(left);
	ir_node *right_right = get_And_right(right);
	if (!is_Const(left_right) || !is_Const(right_right))
		return NULL;
	/* we want the low-mask on the right side */
	if (is_high_mask(get_Const_tarval(left_right))) {
		ir_node *tmp = left;
yb9976's avatar
yb9976 committed
576
577
		left       = right;
		right      = tmp;
Matthias Braun's avatar
Matthias Braun committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
		left_right = right_right;
	} else if (!is_high_mask(get_Const_tarval(right_right))) {
		return NULL;
	}
	if (!is_low_mask(get_Const_tarval(left_right)))
		return NULL;
	ir_node *left_left  = get_And_left(left);
	ir_node *right_left = get_And_left(right);
	static const arm_binop_factory_t pkhbt_pkhtb_factory[2] = {
		{
			new_bd_arm_Pkhbt_reg,
			new_bd_arm_Pkhbt_imm,
			new_bd_arm_Pkhbt_reg_shift_reg,
			new_bd_arm_Pkhbt_reg_shift_imm
		},
		{
			new_bd_arm_Pkhtb_reg,
			new_bd_arm_Pkhtb_imm,
			new_bd_arm_Pkhtb_reg_shift_reg,
			new_bd_arm_Pkhtb_reg_shift_imm
		}
	};
	return gen_int_binop_ops(node, left_left, right_left, MATCH_REVERSE,
	                         pkhbt_pkhtb_factory);
}

604
/**
605
 * Creates an ARM Add.
606
607
608
 *
 * @return the created arm Add node
 */
609
610
static ir_node *gen_Add(ir_node *node)
{
611
612
613
614
615
616
617
	ir_node *rotl_left;
	ir_node *rotl_right;
	if (be_pattern_is_rotl(node, &rotl_left, &rotl_right)) {
		if (is_Minus(rotl_right))
			return gen_Ror(node, rotl_left, get_Minus_op(rotl_right), false);
		return gen_Ror(node, rotl_left, rotl_right, true);
	}
Matthias Braun's avatar
Matthias Braun committed
618
619
620
	ir_node *pkh = match_pkh(node);
	if (pkh != NULL)
		return pkh;
621

Michael Beck's avatar
Michael Beck committed
622
	ir_mode *mode = get_irn_mode(node);
623
	if (mode_is_float(mode)) {
624
625
626
627
628
629
		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);
630
		if (arm_cg_config.fpu == ARM_FPU_FPA) {
631
			return new_bd_arm_Adf(dbgi, block, new_op1, new_op2, mode);
632
		} else {
633
			panic("softfloat not lowered");
634
635
		}
	} else {
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
		ir_node *left  = get_Add_left(node);
		ir_node *right = get_Add_right(node);
		ir_node *mul_left;
		ir_node *mul_right;
		ir_node *other;
		if (is_Mul(left)) {
			mul_left  = get_Mul_left(left);
			mul_right = get_Mul_right(left);
			other     = right;
			goto create_mla;
		} else if (is_Mul(right)) {
			mul_left  = get_Mul_left(right);
			mul_right = get_Mul_right(right);
			other     = left;
create_mla:;
			dbg_info *dbgi      = get_irn_dbg_info(node);
			ir_node  *block     = get_nodes_block(node);
			ir_node  *new_left  = be_transform_node(mul_left);
			ir_node  *new_right = be_transform_node(mul_right);
			ir_node  *new_add   = be_transform_node(other);
			if (arm_cg_config.variant < ARM_VARIANT_6)
				return new_bd_arm_MlaV5(dbgi, block, new_left, new_right,
				                        new_add);
			else
				return new_bd_arm_Mla(dbgi, block, new_left, new_right,
				                      new_add);
		}

		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
		};
		return gen_int_binop_ops(node, left, right,
		                         MATCH_COMMUTATIVE | MATCH_SIZE_NEUTRAL,
		                         &add_factory);
Michael Beck's avatar
Michael Beck committed
673
674
	}
}
675

Matthias Braun's avatar
Matthias Braun committed
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
static ir_node *gen_arm_AddS_t(ir_node *node)
{
	static const arm_binop_factory_t adds_factory = {
		new_bd_arm_AddS_reg,
		new_bd_arm_AddS_imm,
		new_bd_arm_AddS_reg_shift_reg,
		new_bd_arm_AddS_reg_shift_imm,
	};
	ir_node *left  = get_irn_n(node, n_arm_AddS_t_left);
	ir_node *right = get_irn_n(node, n_arm_AddS_t_right);
	ir_node *res   = gen_int_binop_ops(node, left, right,
	                                   MATCH_COMMUTATIVE | MATCH_SIZE_NEUTRAL,
	                                   &adds_factory);
	arch_set_irn_register_out(res, pn_arm_AddS_flags, &arm_registers[REG_FL]);
	return res;
}

static ir_node *gen_Proj_arm_AddS_t(ir_node *node)
{
695
	unsigned pn       = get_Proj_num(node);
Matthias Braun's avatar
Matthias Braun committed
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
	ir_node *pred     = get_Proj_pred(node);
	ir_node *new_pred = be_transform_node(pred);
	switch ((pn_arm_AddS_t)pn) {
	case pn_arm_AddS_t_res:
		return new_r_Proj(new_pred, arm_mode_gp, pn_arm_AddS_res);
	case pn_arm_AddS_t_flags:
		return new_r_Proj(new_pred, arm_mode_flags, pn_arm_AddS_flags);
	}
	panic("%+F: Invalid proj number", node);
}

static ir_node *gen_arm_AdC_t(ir_node *node)
{
	ir_node *left  = get_irn_n(node, n_arm_AdC_t_left);
	ir_node *right = get_irn_n(node, n_arm_AdC_t_right);
	ir_node *flags = get_irn_n(node, n_arm_AdC_t_flags);
	/* TODO: handle complete set of shifter operands */
	ir_node *new_left  = be_transform_node(left);
	ir_node *new_right = be_transform_node(right);
	ir_node *new_flags = be_transform_node(flags);

	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  *res       = new_bd_arm_AdC_reg(dbgi, new_block, new_left,
	                                         new_right, new_flags);
	return res;
}

725
/**
726
 * Creates an ARM Mul.
727
728
729
 *
 * @return the created arm Mul node
 */
730
731
static ir_node *gen_Mul(ir_node *node)
{
732
733
734
735
736
737
738
	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
739

740
	if (mode_is_float(mode)) {
741
		if (arm_cg_config.fpu == ARM_FPU_FPA) {
742
			return new_bd_arm_Muf(dbg, block, new_op1, new_op2, mode);
743
		} else {
744
			panic("softfloat not lowered");
Michael Beck's avatar
Michael Beck committed
745
		}
746
	}
747
	assert(mode_is_data(mode));
748
	if (arm_cg_config.variant < ARM_VARIANT_6) {
749
750
751
752
		return new_bd_arm_Mulv5(dbg, block, new_op1, new_op2);
	} else {
		return new_bd_arm_Mul(dbg, block, new_op1, new_op2);
	}
753
754
}

Matthias Braun's avatar
Matthias Braun committed
755
756
757
758
759
760
761
762
763
764
765
766
767
static ir_node *gen_arm_UMulL_t(ir_node *node)
{
	ir_node  *block     = be_transform_node(get_nodes_block(node));
	ir_node  *left      = get_irn_n(node, n_arm_UMulL_t_left);
	ir_node  *new_left  = be_transform_node(left);
	ir_node  *right     = get_irn_n(node, n_arm_UMulL_t_right);
	ir_node  *new_right = be_transform_node(right);
	dbg_info *dbgi      = get_irn_dbg_info(node);
	return new_bd_arm_UMulL(dbgi, block, new_left, new_right);
}

static ir_node *gen_Proj_arm_UMulL_t(ir_node *node)
{
768
	unsigned pn       = get_Proj_num(node);
Matthias Braun's avatar
Matthias Braun committed
769
770
771
772
773
774
775
776
777
778
779
	ir_node *pred     = get_Proj_pred(node);
	ir_node *new_pred = be_transform_node(pred);
	switch ((pn_arm_UMulL_t)pn) {
	case pn_arm_UMulL_t_low:
		return new_r_Proj(new_pred, arm_mode_gp, pn_arm_UMulL_low);
	case pn_arm_UMulL_t_high:
		return new_r_Proj(new_pred, arm_mode_gp, pn_arm_UMulL_high);
	}
	panic("%+F: Invalid proj number", node);
}

780
static ir_node *gen_Div(ir_node *node)
781
{
782
	ir_node  *block   = be_transform_node(get_nodes_block(node));
783
	ir_node  *op1     = get_Div_left(node);
784
	ir_node  *new_op1 = be_transform_node(op1);
785
	ir_node  *op2     = get_Div_right(node);
786
	ir_node  *new_op2 = be_transform_node(op2);
787
	ir_mode  *mode    = get_Div_resmode(node);
788
	dbg_info *dbg     = get_irn_dbg_info(node);
Michael Beck's avatar
Michael Beck committed
789

790
791
	/* integer division should be replaced by builtin call */
	assert(mode_is_float(mode));
792

793
	if (arm_cg_config.fpu == ARM_FPU_FPA) {
794
		return new_bd_arm_Dvf(dbg, block, new_op1, new_op2, mode);
795
	} else {
796
		panic("softfloat not lowered");
Michael Beck's avatar
Michael Beck committed
797
	}
Michael Beck's avatar
Michael Beck committed
798
799
}

800
801
static ir_node *gen_And(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
802
803
804
805
806
807
	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
808
809
810
811
812
813
814
815
816
817
	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);
Matthias Braun's avatar
Matthias Braun committed
818
819
820
821
822
823
824
825
826
827
828
	if (is_Not(right)) {
		ir_node *right_not = get_Not_op(right);
		return gen_int_binop_ops(node, left, right_not, MATCH_SIZE_NEUTRAL,
		                         &bic_factory);
	} else if (is_Not(left)) {
		ir_node *left_not = get_Not_op(left);
		return gen_int_binop_ops(node, right, left_not, MATCH_SIZE_NEUTRAL,
		                         &bic_factory);
	} else {
		return gen_int_binop(node, MATCH_COMMUTATIVE|MATCH_SIZE_NEUTRAL,
		                     &and_factory);
Michael Beck's avatar
Michael Beck committed
829
	}
830
}
831

832
833
static ir_node *gen_Or(ir_node *node)
{
834
835
836
837
838
839
840
	ir_node *rotl_left;
	ir_node *rotl_right;
	if (be_pattern_is_rotl(node, &rotl_left, &rotl_right)) {
		if (is_Minus(rotl_right))
			return gen_Ror(node, rotl_left, get_Minus_op(rotl_right), false);
		return gen_Ror(node, rotl_left, rotl_right, true);
	}
Matthias Braun's avatar
Matthias Braun committed
841
842
843
	ir_node *pkh = match_pkh(node);
	if (pkh != NULL)
		return pkh;
844

Michael Beck's avatar
Michael Beck committed
845
846
847
848
849
850
851
	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);
852
}
853

Matthias Braun's avatar
Matthias Braun committed
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
static ir_node *gen_arm_OrPl_t(ir_node *node)
{
	ir_node *left     = get_irn_n(node, n_arm_OrPl_t_left);
	ir_node *right    = get_irn_n(node, n_arm_OrPl_t_right);
	ir_node *falseval = get_irn_n(node, n_arm_OrPl_t_falseval);
	ir_node *flags    = get_irn_n(node, n_arm_OrPl_t_flags);
	/* TODO: handle complete set of shifter operands */
	ir_node *new_left     = be_transform_node(left);
	ir_node *new_right    = be_transform_node(right);
	ir_node *new_falseval = be_transform_node(falseval);
	ir_node *new_flags    = be_transform_node(flags);

	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  *res       = new_bd_arm_OrPl(dbgi, new_block, new_left, new_right,
	                                      new_falseval, new_flags);
	return res;
}

874
875
static ir_node *gen_Eor(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
876
877
878
879
880
881
	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
	};
Matthias Braun's avatar
Matthias Braun committed
882
883
	return gen_int_binop(node, MATCH_COMMUTATIVE | MATCH_SIZE_NEUTRAL,
	                     &eor_factory);
884
}
885

886
887
static ir_node *gen_Sub(ir_node *node)
{
888
889
890
	ir_mode *mode  = get_irn_mode(node);
	ir_node *left  = get_Sub_left(node);
	ir_node *right = get_Sub_right(node);
891
	if (mode_is_float(mode)) {
892
893
894
895
896
		ir_node  *block     = be_transform_node(get_nodes_block(node));
		ir_node  *new_left  = be_transform_node(left);
		ir_node  *new_right = be_transform_node(right);
		dbg_info *dbgi      = get_irn_dbg_info(node);

897
		if (arm_cg_config.fpu == ARM_FPU_FPA) {
898
			return new_bd_arm_Suf(dbgi, block, new_left, new_right, mode);
899
		} else {
900
			panic("softfloat not lowered");
Michael Beck's avatar
Michael Beck committed
901
		}
902
	} else {
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
		if (is_Mul(right) && arm_cg_config.variant >= ARM_VARIANT_6T2) {
			dbg_info *dbgi      = get_irn_dbg_info(node);
			ir_node  *block     = be_transform_node(get_nodes_block(node));
			ir_node  *mul_left  = get_Mul_left(right);
			ir_node  *mul_right = get_Mul_right(right);
			ir_node  *new_left  = be_transform_node(mul_left);
			ir_node  *new_right = be_transform_node(mul_right);
			ir_node  *new_sub   = be_transform_node(left);
			return new_bd_arm_Mls(dbgi, block, new_left, new_right, new_sub);
		}

		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
928
		return gen_int_binop(node, MATCH_SIZE_NEUTRAL | MATCH_REVERSE, sub_rsb_factory);
Michael Beck's avatar
Michael Beck committed
929
	}
930
}
Michael Beck's avatar
Michael Beck committed
931

Matthias Braun's avatar
Matthias Braun committed
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
static ir_node *gen_arm_SubS_t(ir_node *node)
{
	static const arm_binop_factory_t subs_factory[2] = {
		{
			new_bd_arm_SubS_reg,
			new_bd_arm_SubS_imm,
			new_bd_arm_SubS_reg_shift_reg,
			new_bd_arm_SubS_reg_shift_imm,
		},
		{
			new_bd_arm_RsbS_reg,
			new_bd_arm_RsbS_imm,
			new_bd_arm_RsbS_reg_shift_reg,
			new_bd_arm_RsbS_reg_shift_imm,
		},
	};
	ir_node *left  = get_irn_n(node, n_arm_SubS_t_left);
	ir_node *right = get_irn_n(node, n_arm_SubS_t_right);
	ir_node *res   = gen_int_binop_ops(node, left, right,
	                                   MATCH_SIZE_NEUTRAL | MATCH_REVERSE,
	                                   subs_factory);
	assert((int)pn_arm_SubS_flags == (int)pn_arm_RsbS_flags);
	arch_set_irn_register_out(res, pn_arm_SubS_flags, &arm_registers[REG_FL]);
	return res;
}

static ir_node *gen_Proj_arm_SubS_t(ir_node *node)
{
960
	unsigned pn       = get_Proj_num(node);
Matthias Braun's avatar
Matthias Braun committed
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
	ir_node *pred     = get_Proj_pred(node);
	ir_node *new_pred = be_transform_node(pred);
	assert((int)pn_arm_SubS_flags == (int)pn_arm_RsbS_flags);
	assert((int)pn_arm_SubS_res == (int)pn_arm_RsbS_res);
	switch ((pn_arm_SubS_t)pn) {
	case pn_arm_SubS_t_res:
		return new_r_Proj(new_pred, arm_mode_gp, pn_arm_SubS_res);
	case pn_arm_SubS_t_flags:
		return new_r_Proj(new_pred, arm_mode_flags, pn_arm_SubS_flags);
	}
	panic("%+F: Invalid proj number", node);
}

static ir_node *gen_arm_SbC_t(ir_node *node)
{
	ir_node *left  = get_irn_n(node, n_arm_SbC_t_left);
	ir_node *right = get_irn_n(node, n_arm_SbC_t_right);
	ir_node *flags = get_irn_n(node, n_arm_SbC_t_flags);
	/* TODO: handle complete set of shifter operands */
	ir_node *new_left  = be_transform_node(left);
	ir_node *new_right = be_transform_node(right);
	ir_node *new_flags = be_transform_node(flags);

	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  *res       = new_bd_arm_SbC_reg(dbgi, new_block, new_left,
	                                         new_right, new_flags);
	return res;
}

Michael Beck's avatar
Michael Beck committed
992
993
994
995
/**
 * Checks if a given value can be used as an immediate for the given
 * ARM shift mode.
 */
996
997
998
999
1000
1001
1002
1003
1004
1005
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
1006
1007
1008
1009
1010
1011
1012
/**
 * generate an ARM shift instruction.
 *
 * @param node            the node
 * @param flags           matching flags
 * @param shift_modifier  initial encoding of the desired shift operation
 */
1013
static ir_node *make_shift(ir_node *node, match_flags_t flags,
1014
		arm_shift_modifier_t shift_modifier)
1015
{
1016
1017
1018
1019
	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);
1020
	ir_mode  *mode  = get_irn_mode(node);
Matthias Braun's avatar
Matthias Braun committed
1021
1022
	if (get_mode_modulo_shift(mode) != 256)
		panic("modulo shift!=256 not supported");
1023

1024
1025
1026
	if (flags & MATCH_SIZE_NEUTRAL) {
		op1 = arm_skip_downconv(op1);
		op2 = arm_skip_downconv(op2);
1027
	}
1028

Matthias Braun's avatar
Matthias Braun committed
1029
	ir_node *new_op1 = be_transform_node(op1);
1030
	if (is_Const(op2)) {
1031
		unsigned int const val = get_Const_long(op2);
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
		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);
		}
	}

Matthias Braun's avatar
Matthias Braun committed
1045
	ir_node *new_op2 = be_transform_node(op2);
1046
1047
	return new_bd_arm_Mov_reg_shift_reg(dbgi, block, new_op1, new_op2,
	                                    shift_modifier);
1048
1049
}

1050
1051
1052
static ir_node *gen_Shl(ir_node *node)
{
	return make_shift(node, MATCH_SIZE_NEUTRAL, ARM_SHF_LSL_REG);
1053
1054
}

1055
1056
1057
static ir_node *gen_Shr(ir_node *node)
{
	return make_shift(node, MATCH_NONE, ARM_SHF_LSR_REG);
1058
1059
}

1060
1061
1062
static ir_node *gen_Shrs(ir_node *node)
{
	return make_shift(node, MATCH_NONE, ARM_SHF_ASR_REG);
1063
1064
}

1065
1066
static ir_node *gen_Not(ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
1067
1068
1069
1070
	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);
	dbg_info *dbgi   = get_irn_dbg_info(node);
Michael Beck's avatar
Michael Beck committed
1071

Michael Beck's avatar
Michael Beck committed
1072
1073
1074
1075
1076
1077
1078
1079
1080
	/* 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) {
		case ARM_SHF_IMM:
		case ARM_SHF_ASR_IMM:
		case ARM_SHF_LSL_IMM:
		case ARM_SHF_LSR_IMM:
Matthias Braun's avatar
Matthias Braun committed
1081
		case ARM_SHF_ROR_IMM: {
1082
			ir_node *mov_op = get_irn_n(new_op, n_arm_Mov_Rm);
Michael Beck's avatar
Michael Beck committed
1083
1084
			return new_bd_arm_Mvn_reg_shift_imm(dbgi, block, mov_op,
				attr->shift_modifier, attr->shift_immediate);
Matthias Braun's avatar
Matthias Braun committed
1085
		}
Michael Beck's avatar
Michael Beck committed
1086
1087
1088
1089

		case ARM_SHF_ASR_REG:
		case ARM_SHF_LSL_REG:
		case ARM_SHF_LSR_REG:
Matthias Braun's avatar
Matthias Braun committed
1090
		case ARM_SHF_ROR_REG: {
1091
1092
			ir_node *mov_op  = get_irn_n(new_op, n_arm_Mov_Rm);
			ir_node *mov_sft = get_irn_n(new_op, n_arm_Mov_Rs);
Michael Beck's avatar
Michael Beck committed
1093
1094
			return new_bd_arm_Mvn_reg_shift_reg(dbgi, block, mov_op, mov_sft,
				attr->shift_modifier);
Matthias Braun's avatar
Matthias Braun committed
1095
		}
Matthias Braun's avatar
Matthias Braun committed
1096
1097
1098
1099
1100
1101

		case ARM_SHF_REG:
		case ARM_SHF_RRX:
			break;
		case ARM_SHF_INVALID:
			panic("invalid shift");
Michael Beck's avatar
Michael Beck committed
1102
1103
		}
	}
1104
1105

	return new_bd_arm_Mvn_reg(dbgi, block, new_op);
1106
1107
}

1108
1109
static ir_node *gen_Minus(ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
1110
1111
1112
1113
1114
	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);
	dbg_info *dbgi   = get_irn_dbg_info(node);
	ir_mode  *mode   = get_irn_mode(node);
Michael Beck's avatar
Michael Beck committed
1115

1116
	if (mode_is_float(mode)) {
1117
		if (arm_cg_config.fpu == ARM_FPU_FPA) {
1118
			return new_bd_arm_Mvf(dbgi, block, op, mode);
1119
		} else {
1120
			panic("softfloat not lowered");
Michael Beck's avatar
Michael Beck committed
1121
		}
1122
	}
1123
	assert(mode_is_data(mode));
1124
	return new_bd_arm_Rsb_imm(dbgi, block, new_op, 0, 0);
1125
1126
}

1127
1128
static ir_node *gen_Load(ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
1129
1130
1131
1132
1133
1134
1135
	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);
	dbg_info *dbgi    = get_irn_dbg_info(node);
1136
	if (get_Load_unaligned(node) == align_non_aligned)
1137
		panic("unaligned Loads not supported yet");
1138

Matthias Braun's avatar
Matthias Braun committed
1139
	ir_node *new_load;
Michael Beck's avatar
Michael Beck committed
1140
	if (mode_is_float(mode)) {
1141
		if (arm_cg_config.fpu == ARM_FPU_FPA) {
1142
1143
			new_load = new_bd_arm_Ldf(dbgi, block, new_ptr, new_mem, mode,
			                          NULL, 0, 0, false);
1144
		} else {
1145
			panic("softfloat not lowered");
Michael Beck's avatar
Michael Beck committed
1146
		}
1147
	} else {
1148
		assert(mode_is_data(mode) && "unsupported mode for Load");
1149

1150
		new_load = new_bd_arm_Ldr(dbgi, block, new_ptr, new_mem, mode, NULL, 0, 0, false);
1151
	}
Michael Beck's avatar
Michael Beck committed
1152
	set_irn_pinned(new_load, get_irn_pinned(node));
1153
1154

	/* check for special case: the loaded value might not be used */
1155
	if (!get_Proj_for_pn(node, pn_Load_res)) {
1156
		/* add a result proj and a Keep to produce a pseudo use */
1157
1158
		ir_node *const in[] = { new_r_Proj(new_load, arm_mode_gp, pn_arm_Ldr_res) };
		be_new_Keep(block, ARRAY_SIZE(in), in);
1159
1160
	}

Michael Beck's avatar
Michael Beck committed
1161
	return new_load;
1162
1163
}

1164
1165
static ir_node *gen_Store(ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
1166
1167
1168
1169
1170
1171
1172
1173
1174
	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);
	dbg_info *dbgi    = get_irn_dbg_info(node);
1175
	if (get_Store_unaligned(node) == align_non_aligned)
1176
		panic("unaligned Stores not supported yet");
1177

Matthias Braun's avatar
Matthias Braun committed
1178
	ir_node *new_store;
Michael Beck's avatar
Michael Beck committed
1179
	if (mode_is_float(mode)) {
1180
		if (arm_cg_config.fpu == ARM_FPU_FPA) {
1181
1182
			new_store = new_bd_arm_Stf(dbgi, block, new_ptr, new_val,
			                           new_mem, mode, NULL, 0, 0, false);