sparc_transform.c 63.2 KB
Newer Older
Hannes Rapp's avatar
Hannes Rapp committed
1
/*
2
 * Copyright (C) 1995-2010 University of Karlsruhe.  All right reserved.
Hannes Rapp's avatar
Hannes Rapp committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

/**
 * @file
 * @brief   code selection (transform FIRM into SPARC FIRM)
Matthias Braun's avatar
Matthias Braun committed
23
 * @author  Hannes Rapp, Matthias Braun
Michael Beck's avatar
Michael Beck committed
24
 * @version $Id$
Hannes Rapp's avatar
Hannes Rapp committed
25
26
27
 */
#include "config.h"

28
#include <stdint.h>
29
#include <stdbool.h>
30

Hannes Rapp's avatar
Hannes Rapp committed
31
32
33
34
35
36
37
#include "irnode_t.h"
#include "irgraph_t.h"
#include "irmode_t.h"
#include "irgmod.h"
#include "iredges.h"
#include "ircons.h"
#include "irprintf.h"
38
#include "iroptimize.h"
Hannes Rapp's avatar
Hannes Rapp committed
39
40
41
42
#include "dbginfo.h"
#include "iropt_t.h"
#include "debug.h"
#include "error.h"
Matthias Braun's avatar
Matthias Braun committed
43
#include "util.h"
Hannes Rapp's avatar
Hannes Rapp committed
44
45
46
47
48

#include "../benode.h"
#include "../beirg.h"
#include "../beutil.h"
#include "../betranshlp.h"
49
#include "../beabihelper.h"
Hannes Rapp's avatar
Hannes Rapp committed
50
51
52
53
54
55
56
57
#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"
58
#include "sparc_cconv.h"
Hannes Rapp's avatar
Hannes Rapp committed
59
60
61
62
63

#include <limits.h>

DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

64
static beabi_helper_env_t    *abihelper;
65
66
static const arch_register_t *sp_reg = &sparc_registers[REG_SP];
static const arch_register_t *fp_reg = &sparc_registers[REG_FRAME_POINTER];
67
68
69
static calling_convention_t  *cconv  = NULL;
static ir_mode               *mode_gp;
static ir_mode               *mode_fp;
70
71
static ir_mode               *mode_fp2;
//static ir_mode               *mode_fp4;
72
static pmap                  *node_to_stack;
Hannes Rapp's avatar
Hannes Rapp committed
73

74
static inline bool mode_needs_gp_reg(ir_mode *mode)
Hannes Rapp's avatar
Hannes Rapp committed
75
{
76
77
78
79
80
81
	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
82
83
}

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

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

Michael Beck's avatar
Michael Beck committed
137
138
139
140
141
142
143
144
/**
 * 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
 */
145
146
147
148
149
150
151
152
153
154
155
156
157
158
static ir_node *gen_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
                              ir_mode *orig_mode)
{
	int bits = get_mode_size_bits(orig_mode);
	if (bits == 32)
		return op;

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

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
 */
static bool is_imm_encodeable(const ir_node *node)
{
178
	long value;
Hannes Rapp's avatar
Hannes Rapp committed
179
180
181
	if (!is_Const(node))
		return false;

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

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
static bool needs_extension(ir_mode *mode)
{
	return get_mode_size_bits(mode) < get_mode_size_bits(mode_gp);
}

/**
 * Check, if a given node is a Down-Conv, ie. a integer Conv
 * 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)
{
	ir_mode *src_mode;
	ir_mode *dest_mode;

	if (!is_Conv(node))
		return false;

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

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

Hannes Rapp's avatar
Hannes Rapp committed
223
224
225
/**
 * helper function for binop operations
 *
Michael Beck's avatar
Michael Beck committed
226
227
 * @param new_reg  register generation function ptr
 * @param new_imm  immediate generation function ptr
Hannes Rapp's avatar
Hannes Rapp committed
228
 */
Matthias Braun's avatar
Matthias Braun committed
229
230
231
232
233
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
234
{
235
236
	dbg_info *dbgi  = get_irn_dbg_info(node);
	ir_node  *block = be_transform_node(get_nodes_block(node));
Hannes Rapp's avatar
Hannes Rapp committed
237
238
	ir_node  *new_op1;
	ir_node  *new_op2;
239
240
241
242
243
244
245
246
247
	ir_mode  *mode1;
	ir_mode  *mode2;

	if (flags & MATCH_MODE_NEUTRAL) {
		op1 = sparc_skip_downconv(op1);
		op2 = sparc_skip_downconv(op2);
	}
	mode1 = get_irn_mode(op1);
	mode2 = get_irn_mode(op2);
248
249
250
	/* 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
251
252

	if (is_imm_encodeable(op2)) {
253
254
255
256
257
		ir_node *new_op1   = be_transform_node(op1);
		int32_t  immediate = get_tarval_long(get_Const_tarval(op2));
		if (! (flags & MATCH_MODE_NEUTRAL) && needs_extension(mode1)) {
			new_op1 = gen_extension(dbgi, block, new_op1, mode1);
		}
258
		return new_imm(dbgi, block, new_op1, NULL, immediate);
Hannes Rapp's avatar
Hannes Rapp committed
259
260
	}
	new_op2 = be_transform_node(op2);
261
262
263
	if (! (flags & MATCH_MODE_NEUTRAL) && needs_extension(mode2)) {
		new_op2 = gen_extension(dbgi, block, new_op2, mode2);
	}
Hannes Rapp's avatar
Hannes Rapp committed
264
265

	if ((flags & MATCH_COMMUTATIVE) && is_imm_encodeable(op1)) {
266
267
		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
268
269
	}

270
271
272
273
	new_op1 = be_transform_node(op1);
	if (! (flags & MATCH_MODE_NEUTRAL) && needs_extension(mode1)) {
		new_op1 = gen_extension(dbgi, block, new_op1, mode1);
	}
Hannes Rapp's avatar
Hannes Rapp committed
274
275
276
	return new_reg(dbgi, block, new_op1, new_op2);
}

Matthias Braun's avatar
Matthias Braun committed
277
278
279
280
281
282
283
284
285
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
286
287
288
/**
 * helper function for FP binop operations
 */
289
static ir_node *gen_helper_binfpop(ir_node *node, ir_mode *mode,
290
291
292
                                   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
293
294
295
{
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *op1     = get_binop_left(node);
296
	ir_node  *new_op1 = be_transform_node(op1);
Hannes Rapp's avatar
Hannes Rapp committed
297
	ir_node  *op2     = get_binop_right(node);
298
	ir_node  *new_op2 = be_transform_node(op2);
Hannes Rapp's avatar
Hannes Rapp committed
299
	dbg_info *dbgi    = get_irn_dbg_info(node);
300
301
302
303
304
305
306
307
308
309
310
311
312
	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
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
static ir_node *gen_helper_unfpop(ir_node *node, ir_mode *mode,
                                  new_unop_fp_func new_func_single,
                                  new_unop_fp_func new_func_double,
                                  new_unop_fp_func new_func_quad)
{
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *op1     = get_binop_left(node);
	ir_node  *new_op1 = be_transform_node(op1);
	dbg_info *dbgi    = get_irn_dbg_info(node);
	unsigned  bits    = get_mode_size_bits(mode);

	switch (bits) {
	case 32:
		return new_func_single(dbgi, block, new_op1, mode);
	case 64:
		return new_func_double(dbgi, block, new_op1, mode);
	case 128:
		return new_func_quad(dbgi, block, new_op1, mode);
	default:
		break;
	}
	panic("unsupported mode %+F for float op", mode);
}

339
340
static ir_node *get_g0(void)
{
341
	return be_prolog_get_reg_value(abihelper, &sparc_registers[REG_G0]);
342
343
}

344
typedef struct address_t {
345
346
	ir_node   *ptr;
	ir_node   *ptr2;
347
348
349
350
	ir_entity *entity;
	int32_t    offset;
} address_t;

351
352
353
354
/**
 * Match a load/store address
 */
static void match_address(ir_node *ptr, address_t *address, bool use_ptr2)
355
356
{
	ir_node   *base   = ptr;
357
	ir_node   *ptr2   = NULL;
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
	int32_t    offset = 0;
	ir_entity *entity = NULL;

	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 */
	if (is_SymConst(base) && get_irn_n_edges(base) == 1) {
		dbg_info *dbgi      = get_irn_dbg_info(ptr);
		ir_node  *block     = get_nodes_block(ptr);
		ir_node  *new_block = be_transform_node(block);
		entity = get_SymConst_entity(base);
		base   = new_bd_sparc_SetHi(dbgi, new_block, entity, offset);
380
381
382
	} else if (use_ptr2 && is_Add(base) && entity == NULL && offset == 0) {
		ptr2 = be_transform_node(get_Add_right(base));
		base = be_transform_node(get_Add_left(base));
383
	} else {
384
		if (sparc_is_value_imm_encodeable(offset)) {
385
386
387
388
389
390
391
			base = be_transform_node(base);
		} else {
			base   = be_transform_node(ptr);
			offset = 0;
		}
	}

392
393
	address->ptr    = base;
	address->ptr2   = ptr2;
394
395
396
397
	address->entity = entity;
	address->offset = offset;
}

Hannes Rapp's avatar
Hannes Rapp committed
398
399
400
401
402
403
404
405
/**
 * Creates an sparc Add.
 *
 * @param node   FIRM node
 * @return the created sparc Add node
 */
static ir_node *gen_Add(ir_node *node)
{
406
407
	ir_mode *mode = get_irn_mode(node);
	ir_node *right;
Hannes Rapp's avatar
Hannes Rapp committed
408

409
	if (mode_is_float(mode)) {
410
411
		return gen_helper_binfpop(node, mode, new_bd_sparc_fadd_s,
		                          new_bd_sparc_fadd_d, new_bd_sparc_fadd_q);
412
	}
Hannes Rapp's avatar
Hannes Rapp committed
413

414
	/* special case: + 0x1000 can be represented as - 0x1000 */
415
	right = get_Add_right(node);
416
	if (is_Const(right)) {
Matthias Braun's avatar
Matthias Braun committed
417
418
419
		ir_node   *left = get_Add_left(node);
		ir_tarval *tv;
		uint32_t   val;
420
421
		/* is this simple address arithmetic? then we can let the linker do
		 * the calculation. */
422
		if (is_SymConst(left) && get_irn_n_edges(left) == 1) {
423
424
425
426
			dbg_info *dbgi  = get_irn_dbg_info(node);
			ir_node  *block = be_transform_node(get_nodes_block(node));
			address_t address;

427
428
429
430
			/* the value of use_ptr2 shouldn't matter here */
			match_address(node, &address, false);
			assert(is_sparc_SetHi(address.ptr));
			return new_bd_sparc_Or_imm(dbgi, block, address.ptr,
431
432
433
434
435
			                           address.entity, address.offset);
		}

		tv  = get_Const_tarval(right);
		val = get_tarval_long(tv);
436
437
438
439
440
		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);
441
			return new_bd_sparc_Sub_imm(dbgi, block, new_op, NULL, -0x1000);
442
443
444
		}
	}

445
446
	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
447
448
449
450
451
452
453
454
455
456
}

/**
 * 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
457
	ir_mode *mode = get_irn_mode(node);
458
459

	if (mode_is_float(mode)) {
460
461
		return gen_helper_binfpop(node, mode, new_bd_sparc_fsub_s,
		                          new_bd_sparc_fsub_d, new_bd_sparc_fsub_q);
462
	}
Hannes Rapp's avatar
Hannes Rapp committed
463

Matthias Braun's avatar
Matthias Braun committed
464
465
	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
466
467
}

468
469
static ir_node *create_ldf(dbg_info *dbgi, ir_node *block, ir_node *ptr,
                           ir_node *mem, ir_mode *mode, ir_entity *entity,
470
                           long offset, bool is_frame_entity)
471
472
473
474
475
{
	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,
476
		                          offset, is_frame_entity);
477
478
	} else if (bits == 64) {
		return new_bd_sparc_Ldf_d(dbgi, block, ptr, mem, mode, entity,
479
		                          offset, is_frame_entity);
480
481
482
	} else {
		assert(bits == 128);
		return new_bd_sparc_Ldf_q(dbgi, block, ptr, mem, mode, entity,
483
		                          offset, is_frame_entity);
484
485
486
	}
}

487
488
static ir_node *create_stf(dbg_info *dbgi, ir_node *block, ir_node *value,
                           ir_node *ptr, ir_node *mem, ir_mode *mode,
489
                           ir_entity *entity, long offset,
490
491
492
493
494
                           bool is_frame_entity)
{
	unsigned bits = get_mode_size_bits(mode);
	assert(mode_is_float(mode));
	if (bits == 32) {
495
		return new_bd_sparc_Stf_s(dbgi, block, value, ptr, mem, mode, entity,
496
		                          offset, is_frame_entity);
497
	} else if (bits == 64) {
498
		return new_bd_sparc_Stf_d(dbgi, block, value, ptr, mem, mode, entity,
499
		                          offset, is_frame_entity);
500
501
	} else {
		assert(bits == 128);
502
		return new_bd_sparc_Stf_q(dbgi, block, value, ptr, mem, mode, entity,
503
		                          offset, is_frame_entity);
504
505
506
	}
}

Hannes Rapp's avatar
Hannes Rapp committed
507
508
509
510
511
512
513
514
/**
 * Transforms a Load.
 *
 * @param node    the ir Load node
 * @return the created sparc Load node
 */
static ir_node *gen_Load(ir_node *node)
{
515
	dbg_info *dbgi     = get_irn_dbg_info(node);
Hannes Rapp's avatar
Hannes Rapp committed
516
517
518
519
520
521
	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);
	ir_node  *new_load = NULL;
522
523
	address_t address;

524
525
526
527
	if (get_Load_unaligned(node) == align_non_aligned) {
		panic("sparc: transformation of unaligned Loads not implemented yet");
	}

528
	if (mode_is_float(mode)) {
529
530
		match_address(ptr, &address, false);
		new_load = create_ldf(dbgi, block, address.ptr, new_mem, mode,
531
		                      address.entity, address.offset, false);
532
	} else {
533
534
535
536
537
538
539
540
541
542
		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);
		}
543
	}
Hannes Rapp's avatar
Hannes Rapp committed
544
545
	set_irn_pinned(new_load, get_irn_pinned(node));

Hannes Rapp's avatar
Hannes Rapp committed
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
	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_node  *new_val  = be_transform_node(val);
	ir_mode  *mode     = get_irn_mode(val);
	dbg_info *dbgi     = get_irn_dbg_info(node);
565
566
567
	ir_node  *new_store = NULL;
	address_t address;

568
569
570
571
	if (get_Store_unaligned(node) == align_non_aligned) {
		panic("sparc: transformation of unaligned Stores not implemented yet");
	}

572
	if (mode_is_float(mode)) {
573
574
575
		/* TODO: variants with reg+reg address mode */
		match_address(ptr, &address, false);
		new_store = create_stf(dbgi, block, new_val, address.ptr, new_mem,
576
		                       mode, address.entity, address.offset, false);
577
	} else {
578
		assert(get_mode_size_bits(mode) <= 32);
579
580
581
582
583
584
585
586
587
588
		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);
		}
589
590
	}
	set_irn_pinned(new_store, get_irn_pinned(node));
Hannes Rapp's avatar
Hannes Rapp committed
591
592
593
594

	return new_store;
}

595
596
/**
 * Creates an sparc Mul.
597
 * returns the lower 32bits of the 64bit multiply result
598
599
600
 *
 * @return the created sparc Mul node
 */
601
602
603
static ir_node *gen_Mul(ir_node *node)
{
	ir_mode *mode = get_irn_mode(node);
Hannes Rapp's avatar
Hannes Rapp committed
604
	if (mode_is_float(mode)) {
605
606
		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
607
	}
608

609
	return gen_helper_binop(node, MATCH_COMMUTATIVE | MATCH_MODE_NEUTRAL,
610
	                        new_bd_sparc_Mul_reg, new_bd_sparc_Mul_imm);
611
612
613
614
615
616
617
618
}

/**
 * Creates an sparc Mulh.
 * Mulh returns the upper 32bits of a mul instruction
 *
 * @return the created sparc Mulh node
 */
619
620
static ir_node *gen_Mulh(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
621
	ir_mode *mode = get_irn_mode(node);
622
623
624
625
626
	ir_node *mul;

	if (mode_is_float(mode))
		panic("FP not supported yet");

627
	mul = gen_helper_binop(node, MATCH_COMMUTATIVE, new_bd_sparc_Mulh_reg, new_bd_sparc_Mulh_imm);
628
	return new_r_Proj(mul, mode_gp, pn_sparc_Mulh_low);
629
}
Hannes Rapp's avatar
Hannes Rapp committed
630

631
632
633
634
635
636
637
638
639
640
641
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);
}

642
643
644
645
646
/**
 * Creates an sparc Div.
 *
 * @return the created sparc Div node
 */
647
648
static ir_node *gen_Div(ir_node *node)
{
649
650
651
652
653
654
655
656
	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);
	ir_node  *res;
657

658
659
660
661
662
	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);
	}

663
	if (mode_is_signed(mode)) {
664
665
666
667
668
669
670
671
672
673
674
		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);
		}
675
	} else {
676
677
678
679
680
681
682
683
684
685
		ir_node *left_high = get_g0();
		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);
		}
686
	}
687

688
	return res;
689
690
}

691
#if 0
692
693
694
static ir_node *gen_Abs(ir_node *node)
{
	ir_mode *const mode = get_irn_mode(node);
695

696
	if (mode_is_float(mode)) {
697
698
		return gen_helper_unfpop(node, mode, new_bd_sparc_fabs_s,
		                         new_bd_sparc_fabs_d, new_bd_sparc_fabs_q);
699
700
701
702
703
	} else {
		ir_node  *const block  = be_transform_node(get_nodes_block(node));
		dbg_info *const dbgi   = get_irn_dbg_info(node);
		ir_node  *const op     = get_Abs_op(node);
		ir_node  *const new_op = be_transform_node(op);
704
		ir_node  *const sra    = new_bd_sparc_Sra_imm(dbgi, block, new_op, NULL, 31);
705
706
707
		ir_node  *const xor    = new_bd_sparc_Xor_reg(dbgi, block, new_op, sra);
		ir_node  *const sub    = new_bd_sparc_Sub_reg(dbgi, block, xor,    sra);
		return sub;
708
	}
709
}
710
#endif
711
712
713
714

/**
 * Transforms a Not node.
 *
Michael Beck's avatar
Michael Beck committed
715
 * @return the created sparc Not node
716
717
718
 */
static ir_node *gen_Not(ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
719
720
	ir_node  *op     = get_Not_op(node);
	ir_node  *zero   = get_g0();
721
722
723
	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);
724

725
	/* Note: Not(Eor()) is normalize in firm localopts already so
Matthias Braun's avatar
Matthias Braun committed
726
727
728
	 * we don't match it for xnor here */

	/* Not can be represented with xnor 0, n */
729
	return new_bd_sparc_XNor_reg(dbgi, block, zero, new_op);
730
731
}

732
733
734
735
736
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,
                                 new_binop_imm_func new_not_imm)
737
{
738
739
740
	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
741
		return gen_helper_binop_args(node, op2, get_Not_op(op1),
742
743
		                             MATCH_MODE_NEUTRAL,
		                             new_not_reg, new_not_imm);
744
	}
745
746
747
748
	if (is_Not(op2)) {
		return gen_helper_binop_args(node, op1, get_Not_op(op2),
		                             MATCH_MODE_NEUTRAL,
		                             new_not_reg, new_not_imm);
749
	}
750
751
752
753
	return gen_helper_binop_args(node, op1, op2,
								 MATCH_MODE_NEUTRAL | MATCH_COMMUTATIVE,
								 new_reg, new_imm);
}
754

755
756
757
758
759
760
761
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,
	                        new_bd_sparc_AndN_imm);
762
763
764
765
}

static ir_node *gen_Or(ir_node *node)
{
766
767
768
769
770
	return gen_helper_bitop(node,
	                        new_bd_sparc_Or_reg,
	                        new_bd_sparc_Or_imm,
	                        new_bd_sparc_OrN_reg,
	                        new_bd_sparc_OrN_imm);
771
772
}

773
static ir_node *gen_Eor(ir_node *node)
Hannes Rapp's avatar
Hannes Rapp committed
774
{
775
776
777
778
779
	return gen_helper_bitop(node,
	                        new_bd_sparc_Xor_reg,
	                        new_bd_sparc_Xor_imm,
	                        new_bd_sparc_XNor_reg,
	                        new_bd_sparc_XNor_imm);
Hannes Rapp's avatar
Hannes Rapp committed
780
781
}

782
783
static ir_node *gen_Shl(ir_node *node)
{
784
	return gen_helper_binop(node, MATCH_NONE, new_bd_sparc_Sll_reg, new_bd_sparc_Sll_imm);
785
786
787
788
}

static ir_node *gen_Shr(ir_node *node)
{
789
	return gen_helper_binop(node, MATCH_NONE, new_bd_sparc_Srl_reg, new_bd_sparc_Srl_imm);
790
}
Hannes Rapp's avatar
Hannes Rapp committed
791

792
static ir_node *gen_Shrs(ir_node *node)
793
{
794
	return gen_helper_binop(node, MATCH_NONE, new_bd_sparc_Sra_reg, new_bd_sparc_Sra_imm);
795
796
}

797
798
799
800
801
/**
 * Transforms a Minus node.
 */
static ir_node *gen_Minus(ir_node *node)
{
802
803
804
805
806
807
	ir_mode  *mode = get_irn_mode(node);
	ir_node  *op;
	ir_node  *block;
	ir_node  *new_op;
	ir_node  *zero;
	dbg_info *dbgi;
808
809

	if (mode_is_float(mode)) {
810
811
		return gen_helper_unfpop(node, mode, new_bd_sparc_fneg_s,
		                         new_bd_sparc_fneg_d, new_bd_sparc_fneg_q);
812
	}
813
814
815
816
817
818
	block  = be_transform_node(get_nodes_block(node));
	dbgi   = get_irn_dbg_info(node);
	op     = get_Minus_op(node);
	new_op = be_transform_node(op);
	zero   = get_g0();
	return new_bd_sparc_Sub_reg(dbgi, block, zero, new_op);
819
820
}

821
/**
Michael Beck's avatar
Michael Beck committed
822
 * Create an entity for a given (floating point) tarval
823
 */
Matthias Braun's avatar
Matthias Braun committed
824
static ir_entity *create_float_const_entity(ir_tarval *tv)
825
{
826
827
828
	const arch_env_t *arch_env = be_get_irg_arch_env(current_ir_graph);
	sparc_isa_t      *isa      = (sparc_isa_t*) arch_env;
	ir_entity        *entity   = (ir_entity*) pmap_get(isa->constants, tv);
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
	ir_initializer_t *initializer;
	ir_mode          *mode;
	ir_type          *type;
	ir_type          *glob;

	if (entity != NULL)
		return entity;

	mode   = get_tarval_mode(tv);
	type   = get_type_for_mode(mode);
	glob   = get_glob_type();
	entity = new_entity(glob, id_unique("C%u"), type);
	set_entity_visibility(entity, ir_visibility_private);
	add_entity_linkage(entity, IR_LINKAGE_CONSTANT);

	initializer = create_initializer_tarval(tv);
	set_entity_initializer(entity, initializer);

847
	pmap_insert(isa->constants, tv, entity);
848
849
850
	return entity;
}

851
852
853
854
static ir_node *gen_float_const(dbg_info *dbgi, ir_node *block, ir_tarval *tv)
{
	ir_entity *entity = create_float_const_entity(tv);
	ir_node   *hi     = new_bd_sparc_SetHi(dbgi, block, entity, 0);
855
	ir_node   *mem    = get_irg_no_mem(current_ir_graph);
856
857
858
	ir_mode   *mode   = get_tarval_mode(tv);
	ir_node   *new_op
		= create_ldf(dbgi, block, hi, mem, mode, entity, 0, false);
859
	ir_node   *proj   = new_r_Proj(new_op, mode, pn_sparc_Ldf_res);
860
861
862
863
864

	set_irn_pinned(new_op, op_pin_state_floats);
	return proj;
}

865
866
static ir_node *gen_Const(ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
867
868
869
	ir_node   *block = be_transform_node(get_nodes_block(node));
	ir_mode   *mode  = get_irn_mode(node);
	dbg_info  *dbgi  = get_irn_dbg_info(node);
870
	ir_tarval *tv    = get_Const_tarval(node);
Matthias Braun's avatar
Matthias Braun committed
871
	long       value;
Hannes Rapp's avatar
Hannes Rapp committed
872
873

	if (mode_is_float(mode)) {
874
		return gen_float_const(dbgi, block, tv);
Hannes Rapp's avatar
Hannes Rapp committed
875
	}
Hannes Rapp's avatar
Hannes Rapp committed
876

877
878
	value = get_tarval_long(tv);
	if (value == 0) {
879
		return get_g0();
880
	} else if (sparc_is_value_imm_encodeable(value)) {
881
		return new_bd_sparc_Or_imm(dbgi, block, get_g0(), NULL, value);
882
	} else {
883
		ir_node *hi = new_bd_sparc_SetHi(dbgi, block, NULL, value);
884
		if ((value & 0x3ff) != 0) {
885
			return new_bd_sparc_Or_imm(dbgi, block, hi, NULL, value & 0x3ff);
886
887
888
		} else {
			return hi;
		}
889
	}
Hannes Rapp's avatar
Hannes Rapp committed
890
891
}

892
static ir_mode *get_cmp_mode(ir_node *b_value)
893
894
895
{
	ir_node *op;

896
	if (!is_Cmp(b_value))
897
		panic("can't determine cond signednes (no cmp)");
898
	op = get_Cmp_left(b_value);
899
	return get_irn_mode(op);
900
901
}

902
903
904
905
906
static ir_node *make_address(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_Or_imm(dbgi, block, hi, entity, offset);
907
908
909

	if (get_entity_owner(entity) == get_tls_type())
		panic("thread local storage not supported yet in sparc backend");
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
	return low;
}

static ir_node *gen_SwitchJmp(ir_node *node)
{
	dbg_info        *dbgi         = get_irn_dbg_info(node);
	ir_node         *block        = be_transform_node(get_nodes_block(node));
	ir_node         *selector     = get_Cond_selector(node);
	ir_node         *new_selector = be_transform_node(selector);
	long             default_pn   = get_Cond_default_proj(node);
	ir_entity       *entity;
	ir_node         *table_address;
	ir_node         *index;
	ir_node         *load;
	ir_node         *address;

	/* switch with smaller mode not implemented yet */
	assert(get_mode_size_bits(get_irn_mode(selector)) == 32);

	entity = new_entity(NULL, id_unique("TBL%u"), get_unknown_type());
	set_entity_visibility(entity, ir_visibility_private);
	add_entity_linkage(entity, IR_LINKAGE_CONSTANT);

	/* TODO: this code does not construct code to check for access
	 * out-of bounds of the jumptable yet. I think we should put this stuff
	 * into the switch_lowering phase to get some additional optimisations
	 * done. */

	/* construct base address */
939
	table_address = make_address(dbgi, block, entity, 0);
940
941
942
	/* scale index */
	index = new_bd_sparc_Sll_imm(dbgi, block, new_selector, NULL, 2);
	/* load from jumptable */
943
	load = new_bd_sparc_Ld_reg(dbgi, block, table_address, index,
944
	                           get_irg_no_mem(current_ir_graph),
945
946
947
948
949
950
	                           mode_gp);
	address = new_r_Proj(load, mode_gp, pn_sparc_Ld_res);

	return new_bd_sparc_SwitchJmp(dbgi, block, address, default_pn, entity);
}

Hannes Rapp's avatar
Hannes Rapp committed
951
952
static ir_node *gen_Cond(ir_node *node)
{
953
954
955
956
957
958
959
960
	ir_node    *selector = get_Cond_selector(node);
	ir_mode    *mode     = get_irn_mode(selector);
	ir_mode    *cmp_mode;
	ir_node    *block;
	ir_node    *flag_node;
	bool        is_unsigned;
	ir_relation relation;
	dbg_info   *dbgi;
Hannes Rapp's avatar
Hannes Rapp committed
961
962
963

	// switch/case jumps
	if (mode != mode_b) {
964
		return gen_SwitchJmp(node);
Hannes Rapp's avatar
Hannes Rapp committed
965
966
967
	}

	// regular if/else jumps
968
	assert(is_Cmp(selector));
Hannes Rapp's avatar
Hannes Rapp committed
969

970
971
	cmp_mode = get_cmp_mode(selector);

972
973
	block       = be_transform_node(get_nodes_block(node));
	dbgi        = get_irn_dbg_info(node);
974
	flag_node   = be_transform_node(selector);
975
	relation    = get_Cmp_relation(selector);
976
977
978
	is_unsigned = !mode_is_signed(cmp_mode);
	if (mode_is_float(cmp_mode)) {
		assert(!is_unsigned);
979
		return new_bd_sparc_fbfcc(dbgi, block, flag_node, relation);
980
	} else {
981
		return new_bd_sparc_Bicc(dbgi, block, flag_node, relation, is_unsigned);
982
	}
Hannes Rapp's avatar
Hannes Rapp committed
983
984
985
986
987
988
989
}

/**
 * transform Cmp
 */
static ir_node *gen_Cmp(ir_node *node)
{
990
991
992
	ir_node *op1      = get_Cmp_left(node);
	ir_node *op2      = get_Cmp_right(node);
	ir_mode *cmp_mode = get_irn_mode(op1);
Hannes Rapp's avatar
Hannes Rapp committed
993
994
	assert(get_irn_mode(op2) == cmp_mode);

995
	if (mode_is_float(cmp_mode)) {
996
997
998
999
1000
		ir_node  *block   = be_transform_node(get_nodes_block(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);
		unsigned  bits    = get_mode_size_bits(cmp_mode);
1001
1002
1003
1004
1005
1006
1007
1008
		if (bits == 32) {
			return new_bd_sparc_fcmp_s(dbgi, block, new_op1, new_op2, cmp_mode);
		} else if (bits == 64) {
			return new_bd_sparc_fcmp_d(dbgi, block, new_op1, new_op2, cmp_mode);
		} else {
			assert(bits == 128);
			return new_bd_sparc_fcmp_q(dbgi, block, new_op1, new_op2, cmp_mode);
		}
Hannes Rapp's avatar
Hannes Rapp committed
1009
1010
	}

1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
	/* when we compare a bitop like and,or,... with 0 then we can directly use
	 * the bitopcc variant.
	 * Currently we only do this when we're the only user of the node...
	 */
	if (is_Const(op2) && is_Const_null(op2) && get_irn_n_edges(op1) == 1) {
		if (is_And(op1)) {
			return gen_helper_bitop(op1,
			                        new_bd_sparc_AndCCZero_reg,
			                        new_bd_sparc_AndCCZero_imm,
			                        new_bd_sparc_AndNCCZero_reg,
			                        new_bd_sparc_AndNCCZero_imm);
		} else if (is_Or(op1)) {
			return gen_helper_bitop(op1,
			                        new_bd_sparc_OrCCZero_reg,
			                        new_bd_sparc_OrCCZero_imm,
			                        new_bd_sparc_OrNCCZero_reg,
			                        new_bd_sparc_OrNCCZero_imm);
		} else if (is_Eor(op1)) {
			return gen_helper_bitop(op1,
			                        new_bd_sparc_XorCCZero_reg,
			                        new_bd_sparc_XorCCZero_imm,
			                        new_bd_sparc_XNorCCZero_reg,
			                        new_bd_sparc_XNorCCZero_imm);
		}
	}

Hannes Rapp's avatar
Hannes Rapp committed
1037
	/* integer compare */
1038
1039
	return gen_helper_binop_args(node, op1, op2, MATCH_NONE,
	                             new_bd_sparc_Cmp_reg, new_bd_sparc_Cmp_imm);
Hannes Rapp's avatar
Hannes Rapp committed
1040
1041
1042
1043
1044
1045
1046
}

/**
 * Transforms a SymConst node.
 */
static ir_node *gen_SymConst(ir_node *node)
{
1047
1048
1049
1050
	ir_entity *entity    = get_SymConst_entity(node);
	dbg_info  *dbgi      = get_irn_dbg_info(node);
	ir_node   *block     = get_nodes_block(node);
	ir_node   *new_block = be_transform_node(block);
1051
	return make_address(dbgi, new_block, entity, 0);
Hannes Rapp's avatar
Hannes Rapp committed
1052
1053
}

1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
static ir_node *create_fftof(dbg_info *dbgi, ir_node *block, ir_node *op,
                             ir_mode *src_mode, ir_mode *dst_mode)
{
	unsigned src_bits = get_mode_size_bits(src_mode);
	unsigned dst_bits = get_mode_size_bits(dst_mode);
	if (src_bits == 32) {
		if (dst_bits == 64) {
			return new_bd_sparc_fftof_s_d(dbgi, block, op, src_mode, dst_mode);
		} else {
			assert(dst_bits == 128);
			return new_bd_sparc_fftof_s_q(dbgi, block, op, src_mode, dst_mode);
		}
	} else if (src_bits == 64) {
		if (dst_bits == 32) {
			return new_bd_sparc_fftof_d_s(dbgi, block, op, src_mode, dst_mode);
		} else {
			assert(dst_bits == 128);
			return new_bd_sparc_fftof_d_q(dbgi, block, op, src_mode, dst_mode);
		}
	} else {
		assert(src_bits == 128);
		if (dst_bits == 32) {
			return new_bd_sparc_fftof_q_s(dbgi, block, op, src_mode, dst_mode);
		} else {
			assert(dst_bits == 64);
			return new_bd_sparc_fftof_q_d(dbgi, block, op, src_mode, dst_mode);
		}
	}
}

static ir_node *create_ftoi(dbg_info *dbgi, ir_node *block, ir_node *op,
                            ir_mode *src_mode)
{
1087
1088
	ir_node  *ftoi;
	unsigned  bits = get_mode_size_bits(src_mode);
1089
	if (bits == 32) {
1090
		ftoi = new_bd_sparc_fftoi_s(dbgi, block, op, src_mode);
1091
	} else if (bits == 64) {
1092
		ftoi = new_bd_sparc_fftoi_d(dbgi, block, op, src_mode);
1093
1094
	} else {
		assert(bits == 128);
1095
1096
1097
1098
1099
1100
		ftoi = new_bd_sparc_fftoi_q(dbgi, block, op, src_mode);
	}

	{
	ir_graph *irg   = get_irn_irg(block);
	ir_node  *sp    = get_irg_frame(irg);
1101
	ir_node  *nomem = get_irg_no_mem(irg);
1102
1103
1104
1105
1106
1107
1108
1109
	ir_node  *stf   = create_stf(dbgi, block, ftoi, sp, nomem, src_mode,
	                             NULL, 0, true);
	ir_node  *ld    = new_bd_sparc_Ld_imm(dbgi, block, sp, stf, mode_gp,
	                                      NULL, 0, true);
	ir_node  *res   = new_r_Proj(ld, mode_gp, pn_sparc_Ld_res);
	set_irn_pinned(stf, op_pin_state_floats);
	set_irn_pinned(ld, op_pin_state_floats);
	return res;
1110
1111
1112
1113
1114
1115
	}
}

static ir_node *create_itof(dbg_info *dbgi, ir_node *block, ir_node *op,
                            ir_mode *dst_mode)
{
1116
1117
	ir_graph *irg   = get_irn_irg(block);
	ir_node  *sp    = get_irg_frame(irg);
1118
	ir_node  *nomem = get_irg_no_mem(irg);
1119
1120
1121
1122
1123
1124
1125
1126
1127
	ir_node  *st    = new_bd_sparc_St_imm(dbgi, block, op, sp, nomem,
	                                      mode_gp, NULL, 0, true);
	ir_node  *ldf   = new_bd_sparc_Ldf_s(dbgi, block, sp, st, mode_fp,
	                                     NULL, 0, true);
	ir_node  *res   = new_r_Proj(ldf, mode_fp, pn_sparc_Ldf_res);
	unsigned  bits  = get_mode_size_bits(dst_mode);
	set_irn_pinned(st, op_pin_state_floats);
	set_irn_pinned(ldf, op_pin_state_floats);

1128
	if (bits == 32) {
1129
		return new_bd_sparc_fitof_s(dbgi, block, res, dst_mode);
1130
	} else if (bits == 64) {
1131
		return new_bd_sparc_fitof_d(dbgi, block, res, dst_mode);
1132
1133
	} else {
		assert(bits == 128);
1134
		return new_bd_sparc_fitof_q(dbgi, block, res, dst_mode);
1135
1136
1137
	}
}

1138
1139
static ir_node *gen_Conv(ir_node *node)
{
Hannes Rapp's avatar
Hannes Rapp committed
1140
1141
1142
1143
1144
	ir_node  *block    = be_transform_node(get_nodes_block(node));
	ir_node  *op       = get_Conv_op(node);
	ir_mode  *src_mode = get_irn_mode(op);
	ir_mode  *dst_mode = get_irn_mode(node);
	dbg_info *dbg      = get_irn_dbg_info(node);
1145
	ir_node  *new_op;
Hannes Rapp's avatar
Hannes Rapp committed
1146

Hannes Rapp's avatar
Hannes Rapp committed
1147
1148
1149
	int src_bits = get_mode_size_bits(src_mode);
	int dst_bits = get_mode_size_bits(dst_mode);

1150
1151
1152
1153
	if (src_mode == mode_b)
		panic("ConvB not lowered %+F", node);

	new_op = be_transform_node(op);
Hannes Rapp's avatar
Hannes Rapp committed
1154
1155
1156
1157
	if (src_mode == dst_mode)
		return new_op;

	if (mode_is_float(src_mode) || mode_is_float(dst_mode)) {
Hannes Rapp's avatar
Hannes Rapp committed
1158
1159
1160
1161
		assert((src_bits <= 64 && dst_bits <= 64) && "quad FP not implemented");

		if (mode_is_float(src_mode)) {
			if (mode_is_float(dst_mode)) {
Michael Beck's avatar
Michael Beck committed
1162
				/* float -> float conv */
1163
				return create_fftof(dbg, block, new_op, src_mode, dst_mode);
Hannes Rapp's avatar
Hannes Rapp committed
1164
			} else {
Michael Beck's avatar
Michael Beck committed
1165
				/* float -> int conv */
1166
1167
				if (!mode_is_signed(dst_mode))
					panic("float to unsigned not implemented yet");
1168
				return create_ftoi(dbg, block, new_op, src_mode);
Hannes Rapp's avatar
Hannes Rapp committed
1169
1170
			}
		} else {
Michael Beck's avatar
Michael Beck committed
1171
			/* int -> float conv */
1172
1173
1174
1175
1176
			if (src_bits < 32) {
				new_op = gen_extension(dbg, block, new_op, src_mode);
			} else if (src_bits == 32 && !mode_is_signed(src_mode)) {
				panic("unsigned to float not lowered!");
			}
1177
			return create_itof(dbg, block, new_op, dst_mode);
Hannes Rapp's avatar
Hannes Rapp committed
1178
		}
1179
1180
	} else if (src_mode == mode_b) {
		panic("ConvB not lowered %+F", node);