sparc_transform.c 91 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
#include <stdint.h>
12
#include <stdbool.h>
13

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

27
28
29
30
31
#include "benode.h"
#include "beirg.h"
#include "beutil.h"
#include "betranshlp.h"
#include "beabihelper.h"
Hannes Rapp's avatar
Hannes Rapp committed
32
33
34
35
36
37
38
39
#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"
40
#include "sparc_cconv.h"
Hannes Rapp's avatar
Hannes Rapp committed
41
42
43
44
45

#include <limits.h>

DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

46
47
48
49
50
typedef struct reg_info_t {
	size_t   offset;
	ir_node *irn;
} reg_info_t;

51
52
static const arch_register_t *sp_reg = &sparc_registers[REG_SP];
static const arch_register_t *fp_reg = &sparc_registers[REG_FRAME_POINTER];
53
static calling_convention_t  *current_cconv = NULL;
54
static be_stackorder_t       *stackorder;
55
static ir_mode               *mode_gp;
56
static ir_mode               *mode_flags;
57
static ir_mode               *mode_fp;
58
59
static ir_mode               *mode_fp2;
//static ir_mode               *mode_fp4;
60
static pmap                  *node_to_stack;
61
62
63
64
65
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;
66
67
68
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
69

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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],
};

87
static inline bool mode_needs_gp_reg(ir_mode *mode)
Hannes Rapp's avatar
Hannes Rapp committed
88
{
89
90
91
92
93
94
	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
95
96
}

97
98
99
/**
 * Create an And that will zero out upper bits.
 *
Michael Beck's avatar
Michael Beck committed
100
101
102
 * @param dbgi      debug info
 * @param block     the basic block
 * @param op        the original node
103
104
105
106
107
108
 * @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) {
109
		return new_bd_sparc_And_imm(dbgi, block, op, NULL, 0xFF);
110
	} else if (src_bits == 16) {
111
		ir_node *lshift = new_bd_sparc_Sll_imm(dbgi, block, op, NULL, 16);
112
		ir_node *rshift = new_bd_sparc_Srl_imm(dbgi, block, lshift, NULL, 16);
113
114
115
116
117
118
119
120
		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
121
122
123
124
125
 *
 * @param dbgi      debug info
 * @param block     the basic block
 * @param op        the original node
 * @param src_bits  number of lower bits that will remain
126
127
128
129
130
 */
static ir_node *gen_sign_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
                                   int src_bits)
{
	int shift_width = 32 - src_bits;
131
132
	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);
133
134
135
	return rshift_node;
}

Michael Beck's avatar
Michael Beck committed
136
137
138
139
140
141
142
143
/**
 * 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
 */
144
145
146
147
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);
148
	assert(bits < 32);
149
150
151
152
153
154
155
156

	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
157
typedef enum {
158
159
160
161
162
	MATCH_NONE          = 0,
	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) */
163
	MATCH_SIGN_EXT_LEFT = 1U << 2, /**< we need to sign-extend the left operand
Christoph Mallon's avatar
Christoph Mallon committed
164
	                                    (for cases when mode < 32bit) */
165
166
	MATCH_ZERO_EXT_LEFT = 1U << 3, /**< we need to zero-extend the left operand
                                            (for cases when mode < 32bit) */
Hannes Rapp's avatar
Hannes Rapp committed
167
} match_flags_t;
168
ENUM_BITSET(match_flags_t)
Hannes Rapp's avatar
Hannes Rapp committed
169
170

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
171
typedef ir_node* (*new_binop_fp_func) (dbg_info *dbgi, ir_node *block, ir_node *op1, ir_node *op2, ir_mode *mode);
172
typedef ir_node* (*new_binop_imm_func) (dbg_info *dbgi, ir_node *block, ir_node *op1, ir_entity *entity, int32_t immediate);
173
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
174
175

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

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

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

/**
yb9976's avatar
yb9976 committed
197
 * Check, if a given node is a Down-Conv, i.e. a integer Conv
198
 * from a mode with a mode with more bits to a mode with fewer bits.
199
200
201
202
203
204
205
206
207
208
 * 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
209
210
	ir_mode *src_mode  = get_irn_mode(get_Conv_op(node));
	ir_mode *dest_mode = get_irn_mode(node);
211
212
213
214
215
216
	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);
}

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

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
/**
 * 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:
310
			panic("unknown asm constraint '%c'", *c);
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
		}

		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') {
507
508
			/* TODO: match Add(Address,Const), ... */
			if (is_Address(pred)) {
509
				operand->kind = ASM_OPERAND_IMMEDIATE;
510
				operand->immediate_value_entity = get_Address_entity(pred);
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
582
583
				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;
}

584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/* Transforms the left operand of a binary operation.
 *
 * Performs sign/zero extension if necessary.
 */
static ir_node *gen_helper_binop_left(ir_node *left, dbg_info *dbgi,
                                      ir_node *block, match_flags_t flags)
{
	ir_mode *mode     = get_irn_mode(left);
	ir_node *new_left = be_transform_node(left);

	if (flags & MATCH_MODE_NEUTRAL)
		return new_left;

	int bits    = get_mode_size_bits(mode);
	int gp_bits = get_mode_size_bits(mode_gp);

	if (bits >= gp_bits)
		return new_left;

	if (flags & MATCH_SIGN_EXT_LEFT) {
		if (!mode_is_signed(mode) || needs_extension(left)) {
			return gen_sign_extension(dbgi, block, new_left, bits);
		}
	} else if (flags & MATCH_ZERO_EXT_LEFT) {
		if (mode_is_signed(mode) || needs_extension(left)) {
			return gen_zero_extension(dbgi, block, new_left, bits);
		}
	} else if (needs_extension(left)) {
		return gen_extension(dbgi, block, new_left, mode);
	}

	return new_left;
}

Hannes Rapp's avatar
Hannes Rapp committed
618
619
620
/**
 * helper function for binop operations
 *
Michael Beck's avatar
Michael Beck committed
621
622
 * @param new_reg  register generation function ptr
 * @param new_imm  immediate generation function ptr
Hannes Rapp's avatar
Hannes Rapp committed
623
 */
Matthias Braun's avatar
Matthias Braun committed
624
625
626
627
628
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
629
{
630
631
632
633
	dbg_info *dbgi  = get_irn_dbg_info(node);
	ir_node  *block = be_transform_node(get_nodes_block(node));

	if (flags & MATCH_MODE_NEUTRAL) {
634
635
		op1 = skip_downconv(op1);
		op2 = skip_downconv(op2);
636
	}
Matthias Braun's avatar
Matthias Braun committed
637
	ir_mode *mode2 = get_irn_mode(op2);
638
639
	/* we should not see 64bit code */
	assert(get_mode_size_bits(get_irn_mode(op1)) <= 32);
640
	assert(get_mode_size_bits(mode2) <= 32);
Hannes Rapp's avatar
Hannes Rapp committed
641
642

	if (is_imm_encodeable(op2)) {
643
		int32_t  immediate = get_tarval_long(get_Const_tarval(op2));
644
		ir_node *new_op1   = gen_helper_binop_left(op1, dbgi, block, flags);
645
		return new_imm(dbgi, block, new_op1, NULL, immediate);
Hannes Rapp's avatar
Hannes Rapp committed
646
	}
Matthias Braun's avatar
Matthias Braun committed
647
	ir_node *new_op2 = be_transform_node(op2);
648
	if (! (flags & MATCH_MODE_NEUTRAL) && needs_extension(op2)) {
649
650
		new_op2 = gen_extension(dbgi, block, new_op2, mode2);
	}
Hannes Rapp's avatar
Hannes Rapp committed
651
652

	if ((flags & MATCH_COMMUTATIVE) && is_imm_encodeable(op1)) {
653
654
		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
655
656
	}

657
	ir_node *new_op1 = gen_helper_binop_left(op1, dbgi, block, flags);
Hannes Rapp's avatar
Hannes Rapp committed
658
659
660
	return new_reg(dbgi, block, new_op1, new_op2);
}

Matthias Braun's avatar
Matthias Braun committed
661
662
663
664
665
666
667
668
669
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
670
671
672
/**
 * helper function for FP binop operations
 */
673
static ir_node *gen_helper_binfpop(ir_node *node, ir_mode *mode,
674
675
676
                                   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
677
678
679
{
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *op1     = get_binop_left(node);
680
	ir_node  *new_op1 = be_transform_node(op1);
Hannes Rapp's avatar
Hannes Rapp committed
681
	ir_node  *op2     = get_binop_right(node);
682
	ir_node  *new_op2 = be_transform_node(op2);
Hannes Rapp's avatar
Hannes Rapp committed
683
	dbg_info *dbgi    = get_irn_dbg_info(node);
684
685
686
687
688
689
690
691
692
693
694
695
696
	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
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
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
723
		ir_node *new_op1   = be_transform_node(op1);
724
725
		return new_binopx_imm(dbgi, block, new_op1, new_flags, NULL, immediate);
	}
Matthias Braun's avatar
Matthias Braun committed
726
	ir_node *new_op2 = be_transform_node(op2);
727
728
729
730
	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
731
	ir_node *new_op1 = be_transform_node(op1);
732
733
734
735
	return new_binopx_reg(dbgi, block, new_op1, new_op2, new_flags);

}

736
static ir_node *get_reg(ir_graph *const irg, reg_info_t *const reg)
737
{
738
	if (!reg->irn) {
739
		/* this is already the transformed start node */
740
		ir_node *const start = get_irg_start(irg);
741
		assert(is_sparc_Start(start));
742
743
		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);
744
	}
745
746
747
748
749
750
	return reg->irn;
}

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

753
754
static ir_node *get_g7(ir_graph *irg)
{
755
	return get_reg(irg, &start_g7);
756
757
758
759
760
761
762
763
764
765
766
767
768
}

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)
{
769
	if (is_tls_entity(entity)) {
770
771
772
773
774
775
776
777
778
779
780
781
		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;
	}
}

782
typedef struct address_t {
783
784
	ir_node   *ptr;
	ir_node   *ptr2;
785
786
787
788
	ir_entity *entity;
	int32_t    offset;
} address_t;

789
790
791
792
/**
 * Match a load/store address
 */
static void match_address(ir_node *ptr, address_t *address, bool use_ptr2)
793
794
795
796
797
798
799
800
801
802
803
804
805
806
{
	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 */

807
	/* we only use the entity if we're the only user otherwise we probably
808
809
	 * won't save anything but produce multiple sethi+or combinations with
	 * just different offsets */
Matthias Braun's avatar
Matthias Braun committed
810
811
	ir_node   *ptr2   = NULL;
	ir_entity *entity = NULL;
812
813
	if (is_Address(base) && get_irn_n_edges(base) == 1) {
		ir_entity *sc_entity = get_Address_entity(base);
814
815
816
817
		dbg_info  *dbgi      = get_irn_dbg_info(ptr);
		ir_node   *block     = get_nodes_block(ptr);
		ir_node   *new_block = be_transform_node(block);

818
		if (is_tls_entity(sc_entity)) {
819
820
821
822
823
824
825
826
827
828
829
830
			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) {
831
832
		ptr2 = be_transform_node(get_Add_right(base));
		base = be_transform_node(get_Add_left(base));
833
	} else {
834
only_offset:
835
		if (sparc_is_value_imm_encodeable(offset)) {
836
837
838
839
840
841
842
			base = be_transform_node(base);
		} else {
			base   = be_transform_node(ptr);
			offset = 0;
		}
	}

843
844
	address->ptr    = base;
	address->ptr2   = ptr2;
845
846
847
848
	address->entity = entity;
	address->offset = offset;
}

849
850
static ir_node *gen_Proj_ASM(ir_node *node)
{
851
	ir_mode *mode     = get_irn_mode(node);
852
853
	ir_node *pred     = get_Proj_pred(node);
	ir_node *new_pred = be_transform_node(pred);
854
	long     pos      = get_Proj_proj(node);
855

856
857
858
859
860
861
	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");
862
	}
863
864

	return new_r_Proj(new_pred, mode, pos);
865
866
}

Hannes Rapp's avatar
Hannes Rapp committed
867
868
869
870
871
872
873
874
/**
 * Creates an sparc Add.
 *
 * @param node   FIRM node
 * @return the created sparc Add node
 */
static ir_node *gen_Add(ir_node *node)
{
875
	ir_mode *mode = get_irn_mode(node);
876
	if (mode_is_float(mode)) {
877
878
		return gen_helper_binfpop(node, mode, new_bd_sparc_fadd_s,
		                          new_bd_sparc_fadd_d, new_bd_sparc_fadd_q);
879
	}
Hannes Rapp's avatar
Hannes Rapp committed
880

881
	/* special case: + 0x1000 can be represented as - 0x1000 */
Matthias Braun's avatar
Matthias Braun committed
882
	ir_node *right = get_Add_right(node);
883
	if (is_Const(right)) {
Matthias Braun's avatar
Matthias Braun committed
884
		ir_node   *left = get_Add_left(node);
885
886
		/* is this simple address arithmetic? then we can let the linker do
		 * the calculation. */
887
		if (is_Address(left) && get_irn_n_edges(left) == 1) {
888
889
890
			dbg_info *dbgi  = get_irn_dbg_info(node);
			ir_node  *block = be_transform_node(get_nodes_block(node));

891
			/* the value of use_ptr2 shouldn't matter here */
Matthias Braun's avatar
Matthias Braun committed
892
			address_t address;
893
894
895
			match_address(node, &address, false);
			assert(is_sparc_SetHi(address.ptr));
			return new_bd_sparc_Or_imm(dbgi, block, address.ptr,
896
897
898
			                           address.entity, address.offset);
		}

Matthias Braun's avatar
Matthias Braun committed
899
900
		ir_tarval *tv  = get_Const_tarval(right);
		uint32_t   val = get_tarval_long(tv);
901
902
903
904
905
		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);
906
			return new_bd_sparc_Sub_imm(dbgi, block, new_op, NULL, -0x1000);
907
908
909
		}
	}

910
911
	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
912
913
}

914
915
static ir_node *gen_AddCC_t(ir_node *node)
{
yb9976's avatar
yb9976 committed
916
917
918
	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,
919
920
	                             MATCH_COMMUTATIVE | MATCH_MODE_NEUTRAL,
	                             new_bd_sparc_AddCC_reg, new_bd_sparc_AddCC_imm);
yb9976's avatar
yb9976 committed
921
922
923
	arch_set_irn_register_out(new_node, pn_sparc_AddCC_flags, &sparc_registers[REG_FLAGS]);

	return new_node;
924
925
926
927
928
929
930
931
932
933
934
935
936
937
}

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:
938
		panic("Invalid proj found");
939
940
941
942
943
944
945
946
947
	}
}

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
948
949
950
951
952
953
954
955
/**
 * 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
956
	ir_mode *mode = get_irn_mode(node);
957
	if (mode_is_float(mode)) {
958
959
		return gen_helper_binfpop(node, mode, new_bd_sparc_fsub_s,
		                          new_bd_sparc_fsub_d, new_bd_sparc_fsub_q);
960
	}
Hannes Rapp's avatar
Hannes Rapp committed
961

Matthias Braun's avatar
Matthias Braun committed
962
963
	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
964
965
}

966
967
static ir_node *gen_SubCC_t(ir_node *node)
{
yb9976's avatar
yb9976 committed
968
969
970
	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,
971
	                             new_bd_sparc_SubCC_reg, new_bd_sparc_SubCC_imm);
yb9976's avatar
yb9976 committed
972
973
974
	arch_set_irn_register_out(new_node, pn_sparc_SubCC_flags, &sparc_registers[REG_FLAGS]);

	return new_node;
975
976
977
978
979
980
981
982
983
984
985
986
987
988
}

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:
989
		panic("Invalid proj found");
990
991
992
993
994
995
996
997
998
	}
}

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

999
1000
ir_node *create_ldf(dbg_info *dbgi, ir_node *block, ir_node *ptr,
                    ir_node *mem, ir_mode *mode, ir_entity *entity,
For faster browsing, not all history is shown. View entire blame