sparc_transform.c 89.8 KB
Newer Older
Hannes Rapp's avatar
Hannes Rapp committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Hannes Rapp's avatar
Hannes Rapp committed
4
5
6
7
8
 */

/**
 * @file
 * @brief   code selection (transform FIRM into SPARC FIRM)
Matthias Braun's avatar
Matthias Braun committed
9
 * @author  Hannes Rapp, Matthias Braun
Hannes Rapp's avatar
Hannes Rapp committed
10
11
12
 */
#include "config.h"

13
#include <stdint.h>
14
#include <stdbool.h>
15

Hannes Rapp's avatar
Hannes Rapp committed
16
17
18
19
20
21
#include "irnode_t.h"
#include "irgraph_t.h"
#include "irmode_t.h"
#include "irgmod.h"
#include "iredges.h"
#include "ircons.h"
22
#include "iroptimize.h"
Hannes Rapp's avatar
Hannes Rapp committed
23
24
25
26
#include "dbginfo.h"
#include "iropt_t.h"
#include "debug.h"
#include "error.h"
Matthias Braun's avatar
Matthias Braun committed
27
#include "util.h"
Hannes Rapp's avatar
Hannes Rapp committed
28

29
30
31
32
33
#include "benode.h"
#include "beirg.h"
#include "beutil.h"
#include "betranshlp.h"
#include "beabihelper.h"
Hannes Rapp's avatar
Hannes Rapp committed
34
35
36
37
38
39
40
41
#include "bearch_sparc_t.h"

#include "sparc_nodes_attr.h"
#include "sparc_transform.h"
#include "sparc_new_nodes.h"
#include "gen_sparc_new_nodes.h"

#include "gen_sparc_regalloc_if.h"
42
#include "sparc_cconv.h"
Hannes Rapp's avatar
Hannes Rapp committed
43
44
45
46
47

#include <limits.h>

DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

48
49
50
51
52
typedef struct reg_info_t {
	size_t   offset;
	ir_node *irn;
} reg_info_t;

53
54
static const arch_register_t *sp_reg = &sparc_registers[REG_SP];
static const arch_register_t *fp_reg = &sparc_registers[REG_FRAME_POINTER];
55
static calling_convention_t  *current_cconv = NULL;
56
static be_stackorder_t       *stackorder;
57
static ir_mode               *mode_gp;
58
static ir_mode               *mode_flags;
59
static ir_mode               *mode_fp;
60
61
static ir_mode               *mode_fp2;
//static ir_mode               *mode_fp4;
62
static pmap                  *node_to_stack;
63
64
65
66
67
static reg_info_t             start_mem;
static reg_info_t             start_g0;
static reg_info_t             start_g7;
static reg_info_t             start_sp;
static reg_info_t             start_fp;
68
69
70
static ir_node               *frame_base;
static size_t                 start_params_offset;
static size_t                 start_callee_saves_offset;
Hannes Rapp's avatar
Hannes Rapp committed
71

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
static const arch_register_t *const omit_fp_callee_saves[] = {
	&sparc_registers[REG_L0],
	&sparc_registers[REG_L1],
	&sparc_registers[REG_L2],
	&sparc_registers[REG_L3],
	&sparc_registers[REG_L4],
	&sparc_registers[REG_L5],
	&sparc_registers[REG_L6],
	&sparc_registers[REG_L7],
	&sparc_registers[REG_I0],
	&sparc_registers[REG_I1],
	&sparc_registers[REG_I2],
	&sparc_registers[REG_I3],
	&sparc_registers[REG_I4],
	&sparc_registers[REG_I5],
};

89
static inline bool mode_needs_gp_reg(ir_mode *mode)
Hannes Rapp's avatar
Hannes Rapp committed
90
{
91
92
93
94
95
96
	if (mode_is_int(mode) || mode_is_reference(mode)) {
		/* we should only see 32bit code */
		assert(get_mode_size_bits(mode) <= 32);
		return true;
	}
	return false;
Hannes Rapp's avatar
Hannes Rapp committed
97
98
}

99
100
101
/**
 * Create an And that will zero out upper bits.
 *
Michael Beck's avatar
Michael Beck committed
102
103
104
 * @param dbgi      debug info
 * @param block     the basic block
 * @param op        the original node
105
106
107
108
109
110
 * @param src_bits  number of lower bits that will remain
 */
static ir_node *gen_zero_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
                                   int src_bits)
{
	if (src_bits == 8) {
111
		return new_bd_sparc_And_imm(dbgi, block, op, NULL, 0xFF);
112
	} else if (src_bits == 16) {
113
		ir_node *lshift = new_bd_sparc_Sll_imm(dbgi, block, op, NULL, 16);
114
		ir_node *rshift = new_bd_sparc_Srl_imm(dbgi, block, lshift, NULL, 16);
115
116
117
118
119
120
121
122
		return rshift;
	} else {
		panic("zero extension only supported for 8 and 16 bits");
	}
}

/**
 * Generate code for a sign extension.
Michael Beck's avatar
Michael Beck committed
123
124
125
126
127
 *
 * @param dbgi      debug info
 * @param block     the basic block
 * @param op        the original node
 * @param src_bits  number of lower bits that will remain
128
129
130
131
132
 */
static ir_node *gen_sign_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
                                   int src_bits)
{
	int shift_width = 32 - src_bits;
133
134
	ir_node *lshift_node = new_bd_sparc_Sll_imm(dbgi, block, op, NULL, shift_width);
	ir_node *rshift_node = new_bd_sparc_Sra_imm(dbgi, block, lshift_node, NULL, shift_width);
135
136
137
	return rshift_node;
}

Michael Beck's avatar
Michael Beck committed
138
139
140
141
142
143
144
145
/**
 * Extend a value to 32 bit signed/unsigned depending on its mode.
 *
 * @param dbgi      debug info
 * @param block     the basic block
 * @param op        the original node
 * @param orig_mode the original mode of op
 */
146
147
148
149
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);
150
	assert(bits < 32);
151
152
153
154
155
156
157
158

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

Hannes Rapp's avatar
Hannes Rapp committed
159
160
typedef enum {
	MATCH_NONE         = 0,
161
162
163
164
	MATCH_COMMUTATIVE  = 1U << 0, /**< commutative operation. */
	MATCH_MODE_NEUTRAL = 1U << 1, /**< the higher bits of the inputs don't
	                                   influence the significant lower bit at
	                                   all (for cases where mode < 32bit) */
Hannes Rapp's avatar
Hannes Rapp committed
165
} match_flags_t;
166
ENUM_BITSET(match_flags_t)
Hannes Rapp's avatar
Hannes Rapp committed
167
168

typedef ir_node* (*new_binop_reg_func) (dbg_info *dbgi, ir_node *block, ir_node *op1, ir_node *op2);
Hannes Rapp's avatar
Hannes Rapp committed
169
typedef ir_node* (*new_binop_fp_func) (dbg_info *dbgi, ir_node *block, ir_node *op1, ir_node *op2, ir_mode *mode);
170
typedef ir_node* (*new_binop_imm_func) (dbg_info *dbgi, ir_node *block, ir_node *op1, ir_entity *entity, int32_t immediate);
171
typedef ir_node* (*new_unop_fp_func) (dbg_info *dbgi, ir_node *block, ir_node *op1, ir_mode *mode);
Hannes Rapp's avatar
Hannes Rapp committed
172
173

/**
174
 * checks if a node's value can be encoded as a immediate
Hannes Rapp's avatar
Hannes Rapp committed
175
176
177
178
179
180
 */
static bool is_imm_encodeable(const ir_node *node)
{
	if (!is_Const(node))
		return false;

Matthias Braun's avatar
Matthias Braun committed
181
	long value = get_tarval_long(get_Const_tarval(node));
182
	return sparc_is_value_imm_encodeable(value);
Hannes Rapp's avatar
Hannes Rapp committed
183
184
}

185
static bool needs_extension(ir_node *op)
186
{
187
	ir_mode *mode = get_irn_mode(op);
188
189
	unsigned gp_bits = get_mode_size_bits(mode_gp);
	if (get_mode_size_bits(mode) >= gp_bits)
190
		return false;
191
	return !be_upper_bits_clean(op, mode);
192
193
194
}

/**
yb9976's avatar
yb9976 committed
195
 * Check, if a given node is a Down-Conv, i.e. a integer Conv
196
197
198
199
200
201
202
203
204
205
206
 * from a mode with a mode with more bits to a mode with lesser bits.
 * Moreover, we return only true if the node has not more than 1 user.
 *
 * @param node   the node
 * @return non-zero if node is a Down-Conv
 */
static bool is_downconv(const ir_node *node)
{
	if (!is_Conv(node))
		return false;

Matthias Braun's avatar
Matthias Braun committed
207
208
	ir_mode *src_mode  = get_irn_mode(get_Conv_op(node));
	ir_mode *dest_mode = get_irn_mode(node);
209
210
211
212
213
214
	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);
}

215
static ir_node *skip_downconv(ir_node *node)
216
217
218
219
220
221
222
{
	while (is_downconv(node)) {
		node = get_Conv_op(node);
	}
	return node;
}

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/**
 * An assembler constraint.
 */
typedef struct constraint_t {
	const arch_register_class_t *cls;
	char                         all_registers_allowed;
	char                         immediate_type;
	int                          same_as;
} constraint_t;

static void parse_asm_constraints(constraint_t *const constraint,
                                  ident *const constraint_text,
                                  bool const is_output)
{
	memset(constraint, 0, sizeof(constraint[0]));
	constraint->same_as = -1;

	char const *c = get_id_str(constraint_text);
	if (*c == '\0') {
		/* a memory constraint: no need to do anything in backend about it
		 * (the dependencies are already respected by the memory edge of
		 * the node) */
		return;
	}

	char                         immediate_type        = '\0';
	arch_register_class_t const *cls                   = NULL;
	bool                         all_registers_allowed = false;
	int                          same_as               = -1;
	while (*c != 0) {
		arch_register_class_t const *new_cls = NULL;
		char                         new_imm = '\0';
		switch (*c) {
		/* Skip spaces, out/in-out marker */
		case ' ':
		case '\t':
		case '\n':
		case '=':
		case '+':
		case '&': break;
		case '*':
			++c;
			break;
		case '#':
			while (*c != 0 && *c != ',')
				++c;
			break;
		case 'r':
			new_cls               = &sparc_reg_classes[CLASS_sparc_gp];
			all_registers_allowed = true;
			break;
		case 'e':
		case 'f':
			new_cls               = &sparc_reg_classes[CLASS_sparc_fp];
			all_registers_allowed = true;
			break;
		case 'A':
		case 'I':
			new_cls = &sparc_reg_classes[CLASS_sparc_gp];
			new_imm = *c;
			break;

		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9': {
			if (is_output)
				panic("can only specify same constraint on input");

			int p;
			sscanf(c, "%d%n", &same_as, &p);
			if (same_as >= 0) {
				c += p;
				continue;
			}
			break;
		}

		default:
			panic("unknown asm constraint '%c' found in (%+F)", *c, current_ir_graph);
		}

		if (new_cls) {
			if (!cls) {
				cls = new_cls;
			} else if (cls != new_cls) {
				panic("multiple register classes not supported");
			}
		}

		if (new_imm != '\0') {
			if (immediate_type == '\0') {
				immediate_type = new_imm;
			} else if (immediate_type != new_imm) {
				panic("multiple immediate types not supported");
			}
		}

		++c;
	}

	if (same_as >= 0) {
		if (cls != NULL)
			panic("same as and register constraint not supported");
		if (immediate_type != '\0')
			panic("same as and immediate constraint not supported");
	}

	if (!cls && same_as < 0)
		panic("no constraint specified for assembler input");

	constraint->same_as               = same_as;
	constraint->cls                   = cls;
	constraint->all_registers_allowed = all_registers_allowed;
	constraint->immediate_type        = immediate_type;
}

static const arch_register_t *find_register(const char *name)
{
	for (size_t i = 0; i < N_SPARC_REGISTERS; ++i) {
		const arch_register_t *const reg = &sparc_registers[i];
		if (strcmp(reg->name, name) == 0)
			return reg;
	}
	return NULL;
}

static arch_register_req_t const *make_register_req(ir_graph *const irg,
	constraint_t const *const c, int const n_outs,
	arch_register_req_t const **const out_reqs, int const pos)
{
	int const same_as = c->same_as;
	if (same_as >= 0) {
		if (same_as >= n_outs)
			panic("invalid output number in same_as constraint");

		struct obstack            *const obst  = get_irg_obstack(irg);
		arch_register_req_t       *const req   = OALLOC(obst, arch_register_req_t);
		arch_register_req_t const *const other = out_reqs[same_as];
		*req            = *other;
		req->type      |= arch_register_req_type_should_be_same;
		req->other_same = 1U << pos;

		/* Switch constraints. This is because in firm we have same_as
		 * constraints on the output constraints while in the gcc asm syntax
		 * they are specified on the input constraints. */
		out_reqs[same_as] = req;
		return other;
	}

	return c->cls->class_req;
}

void sparc_init_asm_constraints(void)
{
	static unsigned char const register_flags[] = {
		'r', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
	};
	for (size_t i = 0; i < ARRAY_SIZE(register_flags); ++i) {
		unsigned char const c = register_flags[i];
		asm_constraint_flags[c] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
	}

	asm_constraint_flags['A'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
	asm_constraint_flags['I'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;

	/* Note there are many more flags in gcc which we can't properly support
	 * at the moment. see gcc/config/sparc/constraints.md */
}

int sparc_is_valid_clobber(const char *clobber)
{
	return strcmp(clobber, "memory") == 0 || strcmp(clobber, "cc") == 0;
}

static ir_node *gen_ASM(ir_node *node)
{
	int       n_inputs     = get_ASM_n_inputs(node);
	size_t    n_clobbers   = 0;
	ident   **clobbers     = get_ASM_clobbers(node);
	ir_graph *irg          = get_irn_irg(node);
	//unsigned  clobber_bits[BITSET_SIZE_ELEMS(N_SPARC_REGISTERS)];

	for (size_t c = 0; c < get_ASM_n_clobbers(node); ++c) {
		const char *const clobber = get_id_str(clobbers[c]);
		if (strcmp(clobber, "memory") == 0)
			continue;
		if (strcmp(clobber, "cc") == 0) {
			n_clobbers += 2;
			continue;
		}

		const arch_register_t *reg = find_register(clobber);
		if (reg == NULL)
			panic("invalid clobber in sparc asm");

#if 0
		rbitset_set(clobber_bits, reg->global_index);
		++n_clobbers;
#else
		panic("clobbers not correctly supported yet");
#endif
	}
	size_t n_out_constraints = get_ASM_n_output_constraints(node);
	size_t n_outs            = n_out_constraints + n_clobbers;

	const ir_asm_constraint *in_constraints  = get_ASM_input_constraints(node);
	const ir_asm_constraint *out_constraints = get_ASM_output_constraints(node);

	/* determine number of operands */
	unsigned n_operands = 0;
	for (size_t out_idx = 0; out_idx < n_out_constraints; ++out_idx) {
		const ir_asm_constraint *constraint = &out_constraints[out_idx];
		if (constraint->pos+1 > n_operands)
			n_operands = constraint->pos+1;
	}
	for (int i = 0; i < n_inputs; ++i) {
		const ir_asm_constraint *constraint = &in_constraints[i];
		if (constraint->pos+1 > n_operands)
			n_operands = constraint->pos+1;
	}

	struct obstack      *const obst = get_irg_obstack(irg);
	sparc_asm_operand_t *const operands
		= NEW_ARR_DZ(sparc_asm_operand_t, obst, n_operands);

	/* construct output constraints */
	size_t                      out_size = n_outs + 1;
	const arch_register_req_t **out_reg_reqs
		= OALLOCN(obst, const arch_register_req_t*, out_size);

	size_t out_idx;
	for (out_idx = 0; out_idx < n_out_constraints; ++out_idx) {
		const ir_asm_constraint *constraint = &out_constraints[out_idx];
		unsigned                 pos        = constraint->pos;
		constraint_t             parsed_constraint;
		parse_asm_constraints(&parsed_constraint, constraint->constraint, true);

		assert(parsed_constraint.immediate_type == 0);
		arch_register_req_t const *const req
			= make_register_req(irg, &parsed_constraint, n_out_constraints,
			                    out_reg_reqs, out_idx);
		out_reg_reqs[out_idx] = req;

		/* TODO: adjust register_req for clobbers */

		sparc_asm_operand_t *const operand = &operands[pos];
		operand->kind = ASM_OPERAND_OUTPUT_VALUE;
		operand->pos  = out_idx;
	}

	/* inputs + input constraints */
	int       max_ins      = n_inputs+1;
	ir_node **in = ALLOCANZ(ir_node*, max_ins);
	const arch_register_req_t **in_reg_reqs
		= OALLOCN(obst, const arch_register_req_t*, max_ins);
	int n_ins = 0;
	for (int i = 0; i < n_inputs; ++i) {
		ir_node                 *pred         = get_ASM_input(node, i);
		const ir_asm_constraint *constraint   = &in_constraints[i];
		unsigned                 pos          = constraint->pos;

		constraint_t parsed_constraint;
		parse_asm_constraints(&parsed_constraint, constraint->constraint, false);

		sparc_asm_operand_t *const operand = &operands[pos];

		/* try to use an immediate value */
		char imm_type = parsed_constraint.immediate_type;
		if (imm_type == 'I') {
			if (is_imm_encodeable(pred)) {
				operand->kind = ASM_OPERAND_IMMEDIATE;
				operand->immediate_value = get_tarval_long(get_Const_tarval(pred));
				continue;
			}
		} else if (imm_type == 'A') {
			/* TODO: match Add(SymConst,Const), ... */
			if (is_SymConst(pred)) {
				operand->kind = ASM_OPERAND_IMMEDIATE;
				operand->immediate_value_entity = get_SymConst_entity(pred);
				continue;
			} else if (is_Const(pred)) {
				operand->kind = ASM_OPERAND_IMMEDIATE;
				operand->immediate_value = get_tarval_long(get_Const_tarval(pred));
				continue;
			}
		}

		arch_register_req_t const *const req = make_register_req(irg, &parsed_constraint, n_out_constraints, out_reg_reqs, i);
		in_reg_reqs[i] = req;

		int      op_pos      = n_ins++;
		ir_node *new_pred = be_transform_node(pred);
		in[op_pos]    = new_pred;
		operand->kind = ASM_OPERAND_INPUT_VALUE;
		operand->pos  = op_pos;
	}

	int      mem_pos     = n_ins++;
	ir_node *mem         = get_ASM_mem(node);
	in[mem_pos]          = be_transform_node(mem);
	in_reg_reqs[mem_pos] = arch_no_register_req;

	/* parse clobbers */
	for (size_t c = 0; c < get_ASM_n_clobbers(node); ++c) {
		const char *const clobber = get_id_str(clobbers[c]);
		if (strcmp(clobber, "memory") == 0)
			continue;
		if (strcmp(clobber, "cc") == 0) {
			const arch_register_t *flags_reg
				= &sparc_registers[REG_FLAGS];
			out_reg_reqs[out_idx++] = flags_reg->single_req;
			const arch_register_t *fpflags_reg
				= &sparc_registers[REG_FPFLAGS];
			out_reg_reqs[out_idx++] = fpflags_reg->single_req;
			continue;
		}

		const arch_register_t *reg = find_register(clobber);
		assert(reg != NULL); /* otherwise we had a panic earlier */
		out_reg_reqs[out_idx++] = reg->single_req;
	}

	/* append none register requirement for the memory output */
	if (n_outs+1 >= out_size) {
		out_size = n_outs + 1;
		const arch_register_req_t **new_out_reg_reqs
			= OALLOCN(obst, const arch_register_req_t*, out_size);
		memcpy(new_out_reg_reqs, out_reg_reqs,
			   n_outs * sizeof(new_out_reg_reqs[0]));
		out_reg_reqs = new_out_reg_reqs;
	}

	/* add a new (dummy) output which occupies the register */
	out_reg_reqs[n_outs] = arch_no_register_req;
	++n_outs;

	dbg_info *const dbgi      = get_irn_dbg_info(node);
	ir_node  *const block     = get_nodes_block(node);
	ir_node  *const new_block = be_transform_node(block);
	ident    *const text      = get_ASM_text(node);
	ir_node  *const new_node
		= new_bd_sparc_ASM(dbgi, new_block, n_ins, in, n_outs, text, operands);

	backend_info_t *const info = be_get_info(new_node);
	for (size_t o = 0; o < n_outs; ++o) {
		info->out_infos[o].req = out_reg_reqs[o];
	}
	arch_set_irn_register_reqs_in(new_node, in_reg_reqs);

	return new_node;
}

Hannes Rapp's avatar
Hannes Rapp committed
582
583
584
/**
 * helper function for binop operations
 *
Michael Beck's avatar
Michael Beck committed
585
586
 * @param new_reg  register generation function ptr
 * @param new_imm  immediate generation function ptr
Hannes Rapp's avatar
Hannes Rapp committed
587
 */
Matthias Braun's avatar
Matthias Braun committed
588
589
590
591
592
static ir_node *gen_helper_binop_args(ir_node *node,
                                      ir_node *op1, ir_node *op2,
                                      match_flags_t flags,
                                      new_binop_reg_func new_reg,
                                      new_binop_imm_func new_imm)
Hannes Rapp's avatar
Hannes Rapp committed
593
{
594
595
596
597
	dbg_info *dbgi  = get_irn_dbg_info(node);
	ir_node  *block = be_transform_node(get_nodes_block(node));

	if (flags & MATCH_MODE_NEUTRAL) {
598
599
		op1 = skip_downconv(op1);
		op2 = skip_downconv(op2);
600
	}
Matthias Braun's avatar
Matthias Braun committed
601
602
	ir_mode *mode1 = get_irn_mode(op1);
	ir_mode *mode2 = get_irn_mode(op2);
603
604
605
	/* we shouldn't see 64bit code */
	assert(get_mode_size_bits(mode1) <= 32);
	assert(get_mode_size_bits(mode2) <= 32);
Hannes Rapp's avatar
Hannes Rapp committed
606
607

	if (is_imm_encodeable(op2)) {
608
		int32_t  immediate = get_tarval_long(get_Const_tarval(op2));
Matthias Braun's avatar
Matthias Braun committed
609
		ir_node *new_op1 = be_transform_node(op1);
610
		if (! (flags & MATCH_MODE_NEUTRAL) && needs_extension(op1)) {
611
612
			new_op1 = gen_extension(dbgi, block, new_op1, mode1);
		}
613
		return new_imm(dbgi, block, new_op1, NULL, immediate);
Hannes Rapp's avatar
Hannes Rapp committed
614
	}
Matthias Braun's avatar
Matthias Braun committed
615
	ir_node *new_op2 = be_transform_node(op2);
616
	if (! (flags & MATCH_MODE_NEUTRAL) && needs_extension(op2)) {
617
618
		new_op2 = gen_extension(dbgi, block, new_op2, mode2);
	}
Hannes Rapp's avatar
Hannes Rapp committed
619
620

	if ((flags & MATCH_COMMUTATIVE) && is_imm_encodeable(op1)) {
621
622
		int32_t immediate = get_tarval_long(get_Const_tarval(op1));
		return new_imm(dbgi, block, new_op2, NULL, immediate);
Hannes Rapp's avatar
Hannes Rapp committed
623
624
	}

Matthias Braun's avatar
Matthias Braun committed
625
	ir_node *new_op1 = be_transform_node(op1);
626
	if (! (flags & MATCH_MODE_NEUTRAL) && needs_extension(op1)) {
627
628
		new_op1 = gen_extension(dbgi, block, new_op1, mode1);
	}
Hannes Rapp's avatar
Hannes Rapp committed
629
630
631
	return new_reg(dbgi, block, new_op1, new_op2);
}

Matthias Braun's avatar
Matthias Braun committed
632
633
634
635
636
637
638
639
640
static ir_node *gen_helper_binop(ir_node *node, match_flags_t flags,
                                 new_binop_reg_func new_reg,
                                 new_binop_imm_func new_imm)
{
	ir_node *op1 = get_binop_left(node);
	ir_node *op2 = get_binop_right(node);
	return gen_helper_binop_args(node, op1, op2, flags, new_reg, new_imm);
}

Hannes Rapp's avatar
Hannes Rapp committed
641
642
643
/**
 * helper function for FP binop operations
 */
644
static ir_node *gen_helper_binfpop(ir_node *node, ir_mode *mode,
645
646
647
                                   new_binop_fp_func new_func_single,
                                   new_binop_fp_func new_func_double,
                                   new_binop_fp_func new_func_quad)
Hannes Rapp's avatar
Hannes Rapp committed
648
649
650
{
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *op1     = get_binop_left(node);
651
	ir_node  *new_op1 = be_transform_node(op1);
Hannes Rapp's avatar
Hannes Rapp committed
652
	ir_node  *op2     = get_binop_right(node);
653
	ir_node  *new_op2 = be_transform_node(op2);
Hannes Rapp's avatar
Hannes Rapp committed
654
	dbg_info *dbgi    = get_irn_dbg_info(node);
655
656
657
658
659
660
661
662
663
664
665
666
667
	unsigned  bits    = get_mode_size_bits(mode);

	switch (bits) {
	case 32:
		return new_func_single(dbgi, block, new_op1, new_op2, mode);
	case 64:
		return new_func_double(dbgi, block, new_op1, new_op2, mode);
	case 128:
		return new_func_quad(dbgi, block, new_op1, new_op2, mode);
	default:
		break;
	}
	panic("unsupported mode %+F for float op", mode);
Hannes Rapp's avatar
Hannes Rapp committed
668
669
}

670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
typedef ir_node* (*new_binopx_imm_func)(dbg_info *dbgi, ir_node *block,
                                        ir_node *op1, ir_node *flags,
                                        ir_entity *imm_entity, int32_t imm);

typedef ir_node* (*new_binopx_reg_func)(dbg_info *dbgi, ir_node *block,
                                        ir_node *op1, ir_node *op2,
                                        ir_node *flags);

static ir_node *gen_helper_binopx(ir_node *node, match_flags_t match_flags,
                                  new_binopx_reg_func new_binopx_reg,
                                  new_binopx_imm_func new_binopx_imm)
{
	dbg_info *dbgi      = get_irn_dbg_info(node);
	ir_node  *block     = be_transform_node(get_nodes_block(node));
	ir_node  *op1       = get_irn_n(node, 0);
	ir_node  *op2       = get_irn_n(node, 1);
	ir_node  *flags     = get_irn_n(node, 2);
	ir_node  *new_flags = be_transform_node(flags);

	/* only support for mode-neutral implemented so far */
	assert(match_flags & MATCH_MODE_NEUTRAL);

	if (is_imm_encodeable(op2)) {
		int32_t  immediate = get_tarval_long(get_Const_tarval(op2));
Matthias Braun's avatar
Matthias Braun committed
694
		ir_node *new_op1   = be_transform_node(op1);
695
696
		return new_binopx_imm(dbgi, block, new_op1, new_flags, NULL, immediate);
	}
Matthias Braun's avatar
Matthias Braun committed
697
	ir_node *new_op2 = be_transform_node(op2);
698
699
700
701
	if ((match_flags & MATCH_COMMUTATIVE) && is_imm_encodeable(op1)) {
		int32_t immediate = get_tarval_long(get_Const_tarval(op1));
		return new_binopx_imm(dbgi, block, new_op2, new_flags, NULL, immediate);
	}
Matthias Braun's avatar
Matthias Braun committed
702
	ir_node *new_op1 = be_transform_node(op1);
703
704
705
706
	return new_binopx_reg(dbgi, block, new_op1, new_op2, new_flags);

}

707
static ir_node *get_reg(ir_graph *const irg, reg_info_t *const reg)
708
{
709
	if (!reg->irn) {
710
		/* this is already the transformed start node */
711
		ir_node *const start = get_irg_start(irg);
712
		assert(is_sparc_Start(start));
713
714
		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);
715
	}
716
717
718
719
720
721
	return reg->irn;
}

static ir_node *get_g0(ir_graph *irg)
{
	return get_reg(irg, &start_g0);
722
723
}

724
725
static ir_node *get_g7(ir_graph *irg)
{
726
	return get_reg(irg, &start_g7);
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
}

static ir_node *make_tls_offset(dbg_info *dbgi, ir_node *block,
                                ir_entity *entity, int32_t offset)
{
	ir_node  *hi  = new_bd_sparc_SetHi(dbgi, block, entity, offset);
	ir_node  *low = new_bd_sparc_Xor_imm(dbgi, block, hi, entity, offset);
	return low;
}

static ir_node *make_address(dbg_info *dbgi, ir_node *block, ir_entity *entity,
                             int32_t offset)
{
	if (get_entity_owner(entity) == get_tls_type()) {
		ir_graph *irg     = get_irn_irg(block);
		ir_node  *g7      = get_g7(irg);
		ir_node  *offsetn = make_tls_offset(dbgi, block, entity, offset);
		ir_node  *add     = new_bd_sparc_Add_reg(dbgi, block, g7, offsetn);
		return add;
	} else {
		ir_node *hi  = new_bd_sparc_SetHi(dbgi, block, entity, offset);
		ir_node *low = new_bd_sparc_Or_imm(dbgi, block, hi, entity, offset);
		return low;
	}
}

753
typedef struct address_t {
754
755
	ir_node   *ptr;
	ir_node   *ptr2;
756
757
758
759
	ir_entity *entity;
	int32_t    offset;
} address_t;

760
761
762
763
/**
 * Match a load/store address
 */
static void match_address(ir_node *ptr, address_t *address, bool use_ptr2)
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
{
	ir_node   *base   = ptr;
	int32_t    offset = 0;

	if (is_Add(base)) {
		ir_node *add_right = get_Add_right(base);
		if (is_Const(add_right)) {
			base    = get_Add_left(base);
			offset += get_tarval_long(get_Const_tarval(add_right));
		}
	}
	/* Note that we don't match sub(x, Const) or chains of adds/subs
	 * because this should all be normalized by now */

	/* we only use the symconst if we're the only user otherwise we probably
	 * won't save anything but produce multiple sethi+or combinations with
	 * just different offsets */
Matthias Braun's avatar
Matthias Braun committed
781
782
	ir_node   *ptr2   = NULL;
	ir_entity *entity = NULL;
783
	if (is_SymConst(base) && get_irn_n_edges(base) == 1) {
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
		ir_entity *sc_entity = get_SymConst_entity(base);
		dbg_info  *dbgi      = get_irn_dbg_info(ptr);
		ir_node   *block     = get_nodes_block(ptr);
		ir_node   *new_block = be_transform_node(block);

		if (get_entity_owner(sc_entity) == get_tls_type()) {
			if (!use_ptr2) {
				goto only_offset;
			} else {
				ptr2   = make_tls_offset(dbgi, new_block, sc_entity, offset);
				offset = 0;
				base   = get_g7(get_irn_irg(base));
			}
		} else {
			entity = sc_entity;
			base   = new_bd_sparc_SetHi(dbgi, new_block, entity, offset);
		}
	} else if (use_ptr2 && is_Add(base) && offset == 0) {
802
803
		ptr2 = be_transform_node(get_Add_right(base));
		base = be_transform_node(get_Add_left(base));
804
	} else {
805
only_offset:
806
		if (sparc_is_value_imm_encodeable(offset)) {
807
808
809
810
811
812
813
			base = be_transform_node(base);
		} else {
			base   = be_transform_node(ptr);
			offset = 0;
		}
	}

814
815
	address->ptr    = base;
	address->ptr2   = ptr2;
816
817
818
819
	address->entity = entity;
	address->offset = offset;
}

820
821
static ir_node *gen_Proj_ASM(ir_node *node)
{
822
	ir_mode *mode     = get_irn_mode(node);
823
824
	ir_node *pred     = get_Proj_pred(node);
	ir_node *new_pred = be_transform_node(pred);
825
	long     pos      = get_Proj_proj(node);
826

827
828
829
830
831
832
	if (mode == mode_M) {
		pos = arch_get_irn_n_outs(new_pred)-1;
	} else if (mode_needs_gp_reg(mode)) {
		mode = mode_gp;
	} else {
		panic("unexpected proj mode at ASM");
833
	}
834
835

	return new_r_Proj(new_pred, mode, pos);
836
837
}

Hannes Rapp's avatar
Hannes Rapp committed
838
839
840
841
842
843
844
845
/**
 * Creates an sparc Add.
 *
 * @param node   FIRM node
 * @return the created sparc Add node
 */
static ir_node *gen_Add(ir_node *node)
{
846
	ir_mode *mode = get_irn_mode(node);
847
	if (mode_is_float(mode)) {
848
849
		return gen_helper_binfpop(node, mode, new_bd_sparc_fadd_s,
		                          new_bd_sparc_fadd_d, new_bd_sparc_fadd_q);
850
	}
Hannes Rapp's avatar
Hannes Rapp committed
851

852
	/* special case: + 0x1000 can be represented as - 0x1000 */
Matthias Braun's avatar
Matthias Braun committed
853
	ir_node *right = get_Add_right(node);
854
	if (is_Const(right)) {
Matthias Braun's avatar
Matthias Braun committed
855
		ir_node   *left = get_Add_left(node);
856
857
		/* is this simple address arithmetic? then we can let the linker do
		 * the calculation. */
858
		if (is_SymConst(left) && get_irn_n_edges(left) == 1) {
859
860
861
			dbg_info *dbgi  = get_irn_dbg_info(node);
			ir_node  *block = be_transform_node(get_nodes_block(node));

862
			/* the value of use_ptr2 shouldn't matter here */
Matthias Braun's avatar
Matthias Braun committed
863
			address_t address;
864
865
866
			match_address(node, &address, false);
			assert(is_sparc_SetHi(address.ptr));
			return new_bd_sparc_Or_imm(dbgi, block, address.ptr,
867
868
869
			                           address.entity, address.offset);
		}

Matthias Braun's avatar
Matthias Braun committed
870
871
		ir_tarval *tv  = get_Const_tarval(right);
		uint32_t   val = get_tarval_long(tv);
872
873
874
875
876
		if (val == 0x1000) {
			dbg_info *dbgi   = get_irn_dbg_info(node);
			ir_node  *block  = be_transform_node(get_nodes_block(node));
			ir_node  *op     = get_Add_left(node);
			ir_node  *new_op = be_transform_node(op);
877
			return new_bd_sparc_Sub_imm(dbgi, block, new_op, NULL, -0x1000);
878
879
880
		}
	}

881
882
	return gen_helper_binop(node, MATCH_COMMUTATIVE | MATCH_MODE_NEUTRAL,
	                        new_bd_sparc_Add_reg, new_bd_sparc_Add_imm);
Hannes Rapp's avatar
Hannes Rapp committed
883
884
}

885
886
static ir_node *gen_AddCC_t(ir_node *node)
{
yb9976's avatar
yb9976 committed
887
888
889
	ir_node *left     = get_irn_n(node, n_sparc_AddCC_t_left);
	ir_node *right    = get_irn_n(node, n_sparc_AddCC_t_right);
	ir_node *new_node = gen_helper_binop_args(node, left, right,
890
891
	                             MATCH_COMMUTATIVE | MATCH_MODE_NEUTRAL,
	                             new_bd_sparc_AddCC_reg, new_bd_sparc_AddCC_imm);
yb9976's avatar
yb9976 committed
892
893
894
	arch_set_irn_register_out(new_node, pn_sparc_AddCC_flags, &sparc_registers[REG_FLAGS]);

	return new_node;
895
896
897
898
899
900
901
902
903
904
905
906
907
908
}

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

	switch (pn) {
	case pn_sparc_AddCC_t_res:
		return new_r_Proj(new_pred, mode_gp, pn_sparc_AddCC_res);
	case pn_sparc_AddCC_t_flags:
		return new_r_Proj(new_pred, mode_flags, pn_sparc_AddCC_flags);
	default:
909
		panic("Invalid proj found");
910
911
912
913
914
915
916
917
918
	}
}

static ir_node *gen_AddX_t(ir_node *node)
{
	return gen_helper_binopx(node, MATCH_COMMUTATIVE | MATCH_MODE_NEUTRAL,
	                         new_bd_sparc_AddX_reg, new_bd_sparc_AddX_imm);
}

Hannes Rapp's avatar
Hannes Rapp committed
919
920
921
922
923
924
925
926
/**
 * Creates an sparc Sub.
 *
 * @param node       FIRM node
 * @return the created sparc Sub node
 */
static ir_node *gen_Sub(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
927
	ir_mode *mode = get_irn_mode(node);
928
	if (mode_is_float(mode)) {
929
930
		return gen_helper_binfpop(node, mode, new_bd_sparc_fsub_s,
		                          new_bd_sparc_fsub_d, new_bd_sparc_fsub_q);
931
	}
Hannes Rapp's avatar
Hannes Rapp committed
932

Matthias Braun's avatar
Matthias Braun committed
933
934
	return gen_helper_binop(node, MATCH_MODE_NEUTRAL,
	                        new_bd_sparc_Sub_reg, new_bd_sparc_Sub_imm);
Hannes Rapp's avatar
Hannes Rapp committed
935
936
}

937
938
static ir_node *gen_SubCC_t(ir_node *node)
{
yb9976's avatar
yb9976 committed
939
940
941
	ir_node *left     = get_irn_n(node, n_sparc_SubCC_t_left);
	ir_node *right    = get_irn_n(node, n_sparc_SubCC_t_right);
	ir_node *new_node = gen_helper_binop_args(node, left, right, MATCH_MODE_NEUTRAL,
942
	                             new_bd_sparc_SubCC_reg, new_bd_sparc_SubCC_imm);
yb9976's avatar
yb9976 committed
943
944
945
	arch_set_irn_register_out(new_node, pn_sparc_SubCC_flags, &sparc_registers[REG_FLAGS]);

	return new_node;
946
947
948
949
950
951
952
953
954
955
956
957
958
959
}

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

	switch (pn) {
	case pn_sparc_SubCC_t_res:
		return new_r_Proj(new_pred, mode_gp, pn_sparc_SubCC_res);
	case pn_sparc_SubCC_t_flags:
		return new_r_Proj(new_pred, mode_flags, pn_sparc_SubCC_flags);
	default:
960
		panic("Invalid proj found");
961
962
963
964
965
966
967
968
969
	}
}

static ir_node *gen_SubX_t(ir_node *node)
{
	return gen_helper_binopx(node, MATCH_MODE_NEUTRAL,
	                         new_bd_sparc_SubX_reg, new_bd_sparc_SubX_imm);
}

970
971
972
ir_node *create_ldf(dbg_info *dbgi, ir_node *block, ir_node *ptr,
                    ir_node *mem, ir_mode *mode, ir_entity *entity,
                    long offset, bool is_frame_entity)
973
974
975
976
977
{
	unsigned bits = get_mode_size_bits(mode);
	assert(mode_is_float(mode));
	if (bits == 32) {
		return new_bd_sparc_Ldf_s(dbgi, block, ptr, mem, mode, entity,
978
		                          offset, is_frame_entity);
979
980
	} else if (bits == 64) {
		return new_bd_sparc_Ldf_d(dbgi, block, ptr, mem, mode, entity,
981
		                          offset, is_frame_entity);
982
983
984
	} else {
		assert(bits == 128);
		return new_bd_sparc_Ldf_q(dbgi, block, ptr, mem, mode, entity,
985
		                          offset, is_frame_entity);
986
987
988
	}
}

989
990
991
992
ir_node *create_stf(dbg_info *dbgi, ir_node *block, ir_node *value,
                    ir_node *ptr, ir_node *mem, ir_mode *mode,
                    ir_entity *entity, long offset,
                    bool is_frame_entity)
993
994
995
996
{
	unsigned bits = get_mode_size_bits(mode);
	assert(mode_is_float(mode));
	if (bits == 32) {
997
		return new_bd_sparc_Stf_s(dbgi, block, value, ptr, mem, mode, entity,
998
		                          offset, is_frame_entity);
999
	} else if (bits == 64) {
1000
		return new_bd_sparc_Stf_d(dbgi, block, value, ptr, mem, mode, entity,
1001
		                          offset, is_frame_entity);
1002
1003
	} else {
		assert(bits == 128);
1004
		return new_bd_sparc_Stf_q(dbgi, block, value, ptr, mem, mode, entity,
1005
		                          offset, is_frame_entity);
1006
1007
1008
	}
}

Hannes Rapp's avatar
Hannes Rapp committed
1009
1010
1011
1012
1013
1014
1015
1016
/**
 * Transforms a Load.
 *
 * @param node    the ir Load node
 * @return the created sparc Load node
 */
static ir_node *gen_Load(ir_node *node)
{
1017
	dbg_info *dbgi     = get_irn_dbg_info(node);
Hannes Rapp's avatar
Hannes Rapp committed
1018
1019
1020
1021
1022
	ir_mode  *mode     = get_Load_mode(node);
	ir_node  *block    = be_transform_node(get_nodes_block(node));
	ir_node  *ptr      = get_Load_ptr(node);
	ir_node  *mem      = get_Load_mem(node);
	ir_node  *new_mem  = be_transform_node(mem);
1023

1024
	if (get_Load_unaligned(node) == align_non_aligned) {
1025
		panic("transformation of unaligned Loads not implemented yet");
1026
1027
	}

Matthias Braun's avatar
Matthias Braun committed
1028
	ir_node  *new_load;
1029
	if (mode_is_float(mode)) {
Matthias Braun's avatar
Matthias Braun committed
1030
		address_t address;
1031
1032
		match_address(ptr, &address, false);
		new_load = create_ldf(dbgi, block, address.ptr, new_mem, mode,
1033
		                      address.entity, address.offset, false);
1034
	} else {
Matthias Braun's avatar
Matthias Braun committed
1035
		address_t address;
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
		match_address(ptr, &address, true);
		if (address.ptr2 != NULL) {
			assert(address.entity == NULL && address.offset == 0);
			new_load = new_bd_sparc_Ld_reg(dbgi, block, address.ptr,
			                               address.ptr2, new_mem, mode);
		} else {
			new_load = new_bd_sparc_Ld_imm(dbgi, block, address.ptr, new_mem,
			                               mode, address.entity, address.offset,
			                               false);
		}
1046
	}
Hannes Rapp's avatar
Hannes Rapp committed
1047
1048
	set_irn_pinned(new_load, get_irn_pinned(node));

Hannes Rapp's avatar
Hannes Rapp committed
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
	return new_load;
}

/**
 * Transforms a Store.
 *
 * @param node    the ir Store node
 * @return the created sparc Store node
 */
static ir_node *gen_Store(ir_node *node)
{
	ir_node  *block    = be_transform_node(get_nodes_block(node));
	ir_node  *ptr      = get_Store_ptr(node);
	ir_node  *mem      = get_Store_mem(node);
	ir_node  *new_mem  = be_transform_node(mem);
	ir_node  *val      = get_Store_value(node);
	ir_mode  *mode     = get_irn_mode(val);
	dbg_info *dbgi     = get_irn_dbg_info(node);
1067

1068
	if (get_Store_unaligned(node) == align_non_aligned) {
1069
		panic("transformation of unaligned Stores not implemented yet");
1070
1071
	}

Matthias Braun's avatar
Matthias Braun committed
1072
	ir_node  *new_store;
1073
	if (mode_is_float(mode)) {
1074
		ir_node *new_val = be_transform_node(val);
1075
		/* TODO: variants with reg+reg address mode */
Matthias Braun's avatar
Matthias Braun committed
1076
		address_t address;
1077
1078
		match_address(ptr, &address, false);
		new_store = create_stf(dbgi, block, new_val, address.ptr, new_mem,
1079
		                       mode, address.entity, address.offset, false);
1080
	} else {
1081
1082
1083
1084
1085
		unsigned dest_bits = get_mode_size_bits(mode);
		while (is_downconv(node)
		       && get_mode_size_bits(get_irn_mode(node)) >= dest_bits) {
		    val = get_Conv_op(val);
		}
Matthias Braun's avatar
Matthias Braun committed
1086
		ir_node *new_val = be_transform_node(val);
1087
1088

		assert(dest_bits <= 32);
Matthias Braun's avatar
Matthias Braun committed
1089
		address_t address;
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
		match_address(ptr, &address, true);
		if (address.ptr2 != NULL) {
			assert(address.entity == NULL && address.offset == 0);
			new_store = new_bd_sparc_St_reg(dbgi, block, new_val, address.ptr,
			                                address.ptr2, new_mem, mode);
		} else {
			new_store = new_bd_sparc_St_imm(dbgi, block, new_val, address.ptr,
			                                new_mem, mode, address.entity,
			                                address.offset, false);
		}
1100
1101
	}
	set_irn_pinned(new_store, get_irn_pinned(node));
Hannes Rapp's avatar
Hannes Rapp committed
1102
1103
1104
1105

	return new_store;
}

1106
1107
/**
 * Creates an sparc Mul.
1108
 * returns the lower 32bits of the 64bit multiply result
1109
1110
1111
 *
 * @return the created sparc Mul node
 */
1112
1113
1114
static ir_node *gen_Mul(ir_node *node)
{
	ir_mode *mode = get_irn_mode(node);
Hannes Rapp's avatar
Hannes Rapp committed
1115
	if (mode_is_float(mode)) {
1116
1117
		return gen_helper_binfpop(node, mode, new_bd_sparc_fmul_s,
		                          new_bd_sparc_fmul_d, new_bd_sparc_fmul_q);
Hannes Rapp's avatar
Hannes Rapp committed
1118
	}
1119

1120
	return gen_helper_binop(node, MATCH_COMMUTATIVE | MATCH_MODE_NEUTRAL,
1121
	                        new_bd_sparc_Mul_reg, new_bd_sparc_Mul_imm);
1122
1123
1124
1125
1126
1127
1128
1129
}

/**
 * Creates an sparc Mulh.
 * Mulh returns the upper 32bits of a mul instruction
 *
 * @return the created sparc Mulh node
 */
1130
1131
static ir_node *gen_Mulh(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
1132
	ir_mode *mode = get_irn_mode(node);
1133
1134
1135
	if (mode_is_float(mode))
		panic("FP not supported yet");

1136
	if (mode_is_signed(mode)) {
Matthias Braun's avatar
Matthias Braun committed
1137
		ir_node *mul = gen_helper_binop(node, MATCH_COMMUTATIVE, new_bd_sparc_SMulh_reg, new_bd_sparc_SMulh_imm);
1138
1139
		return new_r_Proj(mul, mode_gp, pn_sparc_SMulh_low);
	} else {
Matthias Braun's avatar
Matthias Braun committed
1140
		ir_node *mul = gen_helper_binop(node, MATCH_COMMUTATIVE, new_bd_sparc_UMulh_reg, new_bd_sparc_UMulh_imm);
1141
1142
		return new_r_Proj(mul, mode_gp, pn_sparc_UMulh_low);
	}
1143
}
Hannes Rapp's avatar
Hannes Rapp committed
1144

1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
static ir_node *gen_sign_extension_value(ir_node *node)
{
	ir_node *block     = get_nodes_block(node);
	ir_node *new_block = be_transform_node(block);
	ir_node *new_node  = be_transform_node(node);
	/* TODO: we could do some shortcuts for some value types probably.
	 * (For constants or other cases where we know the sign bit in
	 *  advance) */
	return new_bd_sparc_Sra_imm(NULL, new_block, new_node, NULL, 31);
}

1156
1157
1158
1159
1160
/**
 * Creates an sparc Div.
 *
 * @return the created sparc Div node
 */
1161
1162
static ir_node *gen_Div(ir_node *node)
{
1163
1164
1165
1166
1167
1168
1169
	dbg_info *dbgi      = get_irn_dbg_info(node);
	ir_node  *block     = get_nodes_block(node);
	ir_node  *new_block = be_transform_node(block);
	ir_mode  *mode      = get_Div_resmode(node);
	ir_node  *left      = get_Div_left(node);
	ir_node  *left_low  = be_transform_node(left);
	ir_node  *right     = get_Div_right(node);
1170

1171
1172
1173
1174
1175
	if (mode_is_float(mode)) {
		return gen_helper_binfpop(node, mode, new_bd_sparc_fdiv_s,
								  new_bd_sparc_fdiv_d, new_bd_sparc_fdiv_q);
	}

Matthias Braun's avatar
Matthias Braun committed
1176
	ir_node *res;
1177
	if (mode_is_signed(mode)) {
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
		ir_node *left_high = gen_sign_extension_value(left);

		if (is_imm_encodeable(right)) {
			int32_t immediate = get_tarval_long(get_Const_tarval(right));
			res = new_bd_sparc_SDiv_imm(dbgi, new_block, left_high, left_low,
			                            NULL, immediate);
		} else {
			ir_node *new_right = be_transform_node(right);
			res = new_bd_sparc_SDiv_reg(dbgi, new_block, left_high, left_low,
			                            new_right);
		}
1189
	} else {
1190
1191
		ir_graph *irg       = get_irn_irg(node);
		ir_node  *left_high = get_g0(irg);
1192
1193
1194
1195
1196
1197
1198
1199
1200
		if (is_imm_encodeable(right)) {
			int32_t immediate = get_tarval_long(get_Const_tarval(right));
			res = new_bd_sparc_UDiv_imm(dbgi, new_block, left_high, left_low,
			                            NULL, immediate);
		} else {
			ir_node *new_right = be_transform_node(right);
			res = new_bd_sparc_UDiv_reg(dbgi, new_block, left_high, left_low,
			                            new_right);
		}
1201
	}
1202

1203
	return res;
1204
1205
}

1206
1207
1208
/**
 * Transforms a Not node.
 *
Michael Beck's avatar
Michael Beck committed
1209
 * @return the created sparc Not node
1210
1211
1212
 */
static ir_node *gen_Not(ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
1213
	ir_node  *op     = get_Not_op(node);
1214
1215
	ir_graph *irg    = get_irn_irg(node);
	ir_node  *zero   = get_g0(irg);
1216
1217
1218
	dbg_info *dbgi   = get_irn_dbg_info(node);
	ir_node  *block  = be_transform_node(get_nodes_block(node));
	ir_node  *new_op = be_transform_node(op);
1219

1220
	/* Note: Not(Eor()) is normalize in firm localopts already so
Matthias Braun's avatar
Matthias Braun committed
1221
1222
1223
	 * we don't match it for xnor here */

	/* Not can be represented with xnor 0, n */
1224
	return new_bd_sparc_XNor_reg(dbgi, block, zero, new_op);
1225
1226
}

1227
1228
1229
1230
static ir_node *gen_helper_bitop(ir_node *node,
                                 new_binop_reg_func new_reg,
                                 new_binop_imm_func new_imm,
                                 new_binop_reg_func new_not_reg,
1231
1232
                                 new_binop_imm_func new_not_imm,
                                 match_flags_t flags)
1233
{
1234
1235
1236
	ir_node *op1 = get_binop_left(node);
	ir_node *op2 = get_binop_right(node);
	if (is_Not(op1)) {
Matthias Braun's avatar
Matthias Braun committed
1237
		return gen_helper_binop_args(node, op2, get_Not_op(op1),
1238
		                             flags,
1239
		                             new_not_reg, new_not_imm);
1240
	}
1241
1242
	if (is_Not(op2)) {
		return gen_helper_binop_args(node, op1, get_Not_op(op2),
1243
		                             flags,
1244
		                             new_not_reg, new_not_imm);
1245
	}
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
	if (is_Const(op2) && get_irn_n_edges(op2) == 1) {
		ir_tarval *tv    = get_Const_tarval(op2);
		long       value = get_tarval_long(tv);
		if (!sparc_is_value_imm_encodeable(value)) {
			long notvalue = ~value;
			if ((notvalue & 0x3ff) == 0) {
				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  *new_op2
					= new_bd_sparc_SetHi(NULL, new_block, NULL, notvalue);
				ir_node  *new_op1   = be_transform_node(op1);
				ir_node  *result
					= new_not_reg(dbgi, new_block, new_op1, new_op2);
				return result;
			}
		}
	}
1264
	return gen_helper_binop_args(node, op1, op2,
1265
								 flags | MATCH_COMMUTATIVE,
1266
1267
								 new_reg, new_imm);
}
1268

1269
1270
1271
1272
1273
1274
static ir_node *gen_And(ir_node *node)
{
	return gen_helper_bitop(node,
	                        new_bd_sparc_And_reg,
	                        new_bd_sparc_And_imm,
	                        new_bd_sparc_AndN_reg,
1275
1276
	                        new_bd_sparc_AndN_imm,
	                        MATCH_MODE_NEUTRAL);
1277
1278
1279
1280
}

static ir_node *gen_Or(ir_node *node)
{
1281
1282
1283
1284
	return gen_helper_bitop(node,
	                        new_bd_sparc_Or_reg,
	                        new_bd_sparc_Or_imm,
	                        new_bd_sparc_OrN_reg,
1285
1286
	                        new_bd_sparc_OrN_imm,
	                        MATCH_MODE_NEUTRAL);
1287
1288
}

1289
static ir_node *gen_Eor(ir_node *node)
Hannes Rapp's avatar
Hannes Rapp committed
1290
{
1291
1292
1293
1294
	return gen_helper_bitop(node,
	                        new_bd_sparc_Xor_reg,
	                        new_bd_sparc_Xor_imm,
	                        new_bd_sparc_XNor_reg,
1295
1296
	                        new_bd_sparc_XNor_imm,
	                        MATCH_MODE_NEUTRAL);
Hannes Rapp's avatar
Hannes Rapp committed
1297
1298
}

1299
1300
static ir_node *gen_Shl(ir_node *node)
{
1301
1302
	ir_mode *mode = get_irn_mode(node);
	if (get_mode_modulo_shift(mode) != 32)
1303
		panic("modulo_shift!=32 not supported");
1304
	return gen_helper_binop(node, MATCH_NONE, new_bd_sparc_Sll_reg, new_bd_sparc_Sll_imm);
1305
1306
1307
1308
}

static ir_node *gen_Shr(ir_node *node)
{
1309
1310
	ir_mode *mode = get_irn_mode(node);
	if (get_mode_modulo_shift(mode) != 32)
1311