sparc_transform.c 76.3 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
49
#include "benode.h"
#include "beirg.h"
#include "beutil.h"
#include "betranshlp.h"
#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
65
static const arch_register_t *sp_reg = &sparc_registers[REG_SP];
static const arch_register_t *fp_reg = &sparc_registers[REG_FRAME_POINTER];
66
static calling_convention_t  *current_cconv = NULL;
67
static be_stackorder_t       *stackorder;
68
static ir_mode               *mode_gp;
69
static ir_mode               *mode_flags;
70
static ir_mode               *mode_fp;
71
72
static ir_mode               *mode_fp2;
//static ir_mode               *mode_fp4;
73
static pmap                  *node_to_stack;
74
75
76
77
static size_t                 start_mem_offset;
static ir_node               *start_mem;
static size_t                 start_g0_offset;
static ir_node               *start_g0;
78
79
static size_t                 start_g7_offset;
static ir_node               *start_g7;
80
81
82
83
84
85
86
static size_t                 start_sp_offset;
static ir_node               *start_sp;
static size_t                 start_fp_offset;
static ir_node               *start_fp;
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
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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],
};

105
static inline bool mode_needs_gp_reg(ir_mode *mode)
Hannes Rapp's avatar
Hannes Rapp committed
106
{
107
108
109
110
111
112
	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
113
114
}

115
116
117
/**
 * Create an And that will zero out upper bits.
 *
Michael Beck's avatar
Michael Beck committed
118
119
120
 * @param dbgi      debug info
 * @param block     the basic block
 * @param op        the original node
121
122
123
124
125
126
 * @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) {
127
		return new_bd_sparc_And_imm(dbgi, block, op, NULL, 0xFF);
128
	} else if (src_bits == 16) {
129
		ir_node *lshift = new_bd_sparc_Sll_imm(dbgi, block, op, NULL, 16);
130
		ir_node *rshift = new_bd_sparc_Srl_imm(dbgi, block, lshift, NULL, 16);
131
132
133
134
135
136
137
138
		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
139
140
141
142
143
 *
 * @param dbgi      debug info
 * @param block     the basic block
 * @param op        the original node
 * @param src_bits  number of lower bits that will remain
144
145
146
147
148
 */
static ir_node *gen_sign_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
                                   int src_bits)
{
	int shift_width = 32 - src_bits;
149
150
	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);
151
152
153
154
155
156
157
158
159
	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.
 */
160
static bool upper_bits_clean(ir_node *node, ir_mode *mode)
161
{
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
	switch ((ir_opcode)get_irn_opcode(node)) {
	case iro_And:
		if (!mode_is_signed(mode)) {
			return upper_bits_clean(get_And_left(node), mode)
			    || upper_bits_clean(get_And_right(node), mode);
		}
		/* FALLTHROUGH */
	case iro_Or:
	case iro_Eor:
		return upper_bits_clean(get_binop_left(node), mode)
		    && upper_bits_clean(get_binop_right(node), mode);

	case iro_Shr:
		if (mode_is_signed(mode)) {
			return false; /* TODO */
		} else {
			ir_node *right = get_Shr_right(node);
			if (is_Const(right)) {
				ir_tarval *tv  = get_Const_tarval(right);
				long       val = get_tarval_long(tv);
				if (val >= 32 - (long)get_mode_size_bits(mode))
					return true;
			}
			return upper_bits_clean(get_Shr_left(node), mode);
		}

	case iro_Shrs:
		return upper_bits_clean(get_Shrs_left(node), mode);

	case iro_Const: {
		ir_tarval *tv  = get_Const_tarval(node);
		long       val = get_tarval_long(tv);
		if (mode_is_signed(mode)) {
			long    shifted = val >> (get_mode_size_bits(mode)-1);
			return shifted == 0 || shifted == -1;
		} else {
			unsigned long shifted = (unsigned long)val;
			shifted >>= get_mode_size_bits(mode)-1;
			shifted >>= 1;
			return shifted == 0;
		}
	}

	case iro_Conv: {
		ir_mode *dest_mode = get_irn_mode(node);
		ir_node *op        = get_Conv_op(node);
		ir_mode *src_mode  = get_irn_mode(op);
		unsigned src_bits  = get_mode_size_bits(src_mode);
		unsigned dest_bits = get_mode_size_bits(dest_mode);
		/* downconvs are a nop */
		if (src_bits <= dest_bits)
			return upper_bits_clean(op, mode);
		if (dest_bits <= get_mode_size_bits(mode)
		    && mode_is_signed(dest_mode) == mode_is_signed(mode))
			return true;
		return false;
	}

	case iro_Proj: {
		ir_node *pred = get_Proj_pred(node);
		switch (get_irn_opcode(pred)) {
		case iro_Load: {
			ir_mode *load_mode = get_Load_mode(pred);
			unsigned load_bits = get_mode_size_bits(load_mode);
			unsigned bits      = get_mode_size_bits(mode);
			if (load_bits > bits)
				return false;
			if (mode_is_signed(mode) != mode_is_signed(load_mode))
				return false;
			return true;
		}
		default:
			break;
		}
	}
	default:
		break;
	}
240
241
242
	return false;
}

Michael Beck's avatar
Michael Beck committed
243
244
245
246
247
248
249
250
/**
 * 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
 */
251
252
253
254
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);
255
	assert(bits < 32);
256
257
258
259
260
261
262
263

	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
264
265
typedef enum {
	MATCH_NONE         = 0,
266
267
268
269
	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
270
} match_flags_t;
271
ENUM_BITSET(match_flags_t)
Hannes Rapp's avatar
Hannes Rapp committed
272
273

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
274
typedef ir_node* (*new_binop_fp_func) (dbg_info *dbgi, ir_node *block, ir_node *op1, ir_node *op2, ir_mode *mode);
275
typedef ir_node* (*new_binop_imm_func) (dbg_info *dbgi, ir_node *block, ir_node *op1, ir_entity *entity, int32_t immediate);
276
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
277
278

/**
279
 * checks if a node's value can be encoded as a immediate
Hannes Rapp's avatar
Hannes Rapp committed
280
281
282
 */
static bool is_imm_encodeable(const ir_node *node)
{
283
	long value;
Hannes Rapp's avatar
Hannes Rapp committed
284
285
286
	if (!is_Const(node))
		return false;

287
	value = get_tarval_long(get_Const_tarval(node));
288
	return sparc_is_value_imm_encodeable(value);
Hannes Rapp's avatar
Hannes Rapp committed
289
290
}

291
static bool needs_extension(ir_node *op)
292
{
293
294
295
296
	ir_mode *mode = get_irn_mode(op);
	if (get_mode_size_bits(mode) >= get_mode_size_bits(mode_gp))
		return false;
	return !upper_bits_clean(op, mode);
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
}

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

323
static ir_node *skip_downconv(ir_node *node)
324
325
326
327
328
329
330
{
	while (is_downconv(node)) {
		node = get_Conv_op(node);
	}
	return node;
}

Hannes Rapp's avatar
Hannes Rapp committed
331
332
333
/**
 * helper function for binop operations
 *
Michael Beck's avatar
Michael Beck committed
334
335
 * @param new_reg  register generation function ptr
 * @param new_imm  immediate generation function ptr
Hannes Rapp's avatar
Hannes Rapp committed
336
 */
Matthias Braun's avatar
Matthias Braun committed
337
338
339
340
341
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
342
{
343
344
	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
345
346
	ir_node  *new_op1;
	ir_node  *new_op2;
347
348
349
350
	ir_mode  *mode1;
	ir_mode  *mode2;

	if (flags & MATCH_MODE_NEUTRAL) {
351
352
		op1 = skip_downconv(op1);
		op2 = skip_downconv(op2);
353
354
355
	}
	mode1 = get_irn_mode(op1);
	mode2 = get_irn_mode(op2);
356
357
358
	/* 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
359
360

	if (is_imm_encodeable(op2)) {
361
		int32_t  immediate = get_tarval_long(get_Const_tarval(op2));
362
		new_op1 = be_transform_node(op1);
363
		if (! (flags & MATCH_MODE_NEUTRAL) && needs_extension(op1)) {
364
365
			new_op1 = gen_extension(dbgi, block, new_op1, mode1);
		}
366
		return new_imm(dbgi, block, new_op1, NULL, immediate);
Hannes Rapp's avatar
Hannes Rapp committed
367
368
	}
	new_op2 = be_transform_node(op2);
369
	if (! (flags & MATCH_MODE_NEUTRAL) && needs_extension(op2)) {
370
371
		new_op2 = gen_extension(dbgi, block, new_op2, mode2);
	}
Hannes Rapp's avatar
Hannes Rapp committed
372
373

	if ((flags & MATCH_COMMUTATIVE) && is_imm_encodeable(op1)) {
374
375
		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
376
377
	}

378
	new_op1 = be_transform_node(op1);
379
	if (! (flags & MATCH_MODE_NEUTRAL) && needs_extension(op1)) {
380
381
		new_op1 = gen_extension(dbgi, block, new_op1, mode1);
	}
Hannes Rapp's avatar
Hannes Rapp committed
382
383
384
	return new_reg(dbgi, block, new_op1, new_op2);
}

Matthias Braun's avatar
Matthias Braun committed
385
386
387
388
389
390
391
392
393
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
394
395
396
/**
 * helper function for FP binop operations
 */
397
static ir_node *gen_helper_binfpop(ir_node *node, ir_mode *mode,
398
399
400
                                   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
401
402
403
{
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *op1     = get_binop_left(node);
404
	ir_node  *new_op1 = be_transform_node(op1);
Hannes Rapp's avatar
Hannes Rapp committed
405
	ir_node  *op2     = get_binop_right(node);
406
	ir_node  *new_op2 = be_transform_node(op2);
Hannes Rapp's avatar
Hannes Rapp committed
407
	dbg_info *dbgi    = get_irn_dbg_info(node);
408
409
410
411
412
413
414
415
416
417
418
419
420
	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
421
422
}

423
424
425
426
427
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)
{
428
429
430
431
432
	ir_node  *block  = be_transform_node(get_nodes_block(node));
	ir_node  *op     = get_unop_op(node);
	ir_node  *new_op = be_transform_node(op);
	dbg_info *dbgi   = get_irn_dbg_info(node);
	unsigned  bits   = get_mode_size_bits(mode);
433
434
435

	switch (bits) {
	case 32:
436
		return new_func_single(dbgi, block, new_op, mode);
437
	case 64:
438
		return new_func_double(dbgi, block, new_op, mode);
439
	case 128:
440
		return new_func_quad(dbgi, block, new_op, mode);
441
442
443
444
445
446
	default:
		break;
	}
	panic("unsupported mode %+F for float op", mode);
}

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
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);
	ir_node  *new_op1;
	ir_node  *new_op2;

	/* 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));
473
		new_op1 = be_transform_node(op1);
474
475
476
477
478
479
480
481
482
483
484
485
		return new_binopx_imm(dbgi, block, new_op1, new_flags, NULL, immediate);
	}
	new_op2 = be_transform_node(op2);
	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);
	}
	new_op1 = be_transform_node(op1);
	return new_binopx_reg(dbgi, block, new_op1, new_op2, new_flags);

}

486
static ir_node *get_g0(ir_graph *irg)
487
{
488
489
490
491
492
493
494
	if (start_g0 == NULL) {
		/* this is already the transformed start node */
		ir_node *start = get_irg_start(irg);
		assert(is_sparc_Start(start));
		start_g0 = new_r_Proj(start, mode_gp, start_g0_offset);
	}
	return start_g0;
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
static ir_node *get_g7(ir_graph *irg)
{
	if (start_g7 == NULL) {
		ir_node *start = get_irg_start(irg);
		assert(is_sparc_Start(start));
		start_g7 = new_r_Proj(start, mode_gp, start_g7_offset);
	}
	return start_g7;
}

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

531
typedef struct address_t {
532
533
	ir_node   *ptr;
	ir_node   *ptr2;
534
535
536
537
	ir_entity *entity;
	int32_t    offset;
} address_t;

538
539
540
541
/**
 * Match a load/store address
 */
static void match_address(ir_node *ptr, address_t *address, bool use_ptr2)
542
543
{
	ir_node   *base   = ptr;
544
	ir_node   *ptr2   = NULL;
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
	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) {
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
		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) {
580
581
		ptr2 = be_transform_node(get_Add_right(base));
		base = be_transform_node(get_Add_left(base));
582
	} else {
583
only_offset:
584
		if (sparc_is_value_imm_encodeable(offset)) {
585
586
587
588
589
590
591
			base = be_transform_node(base);
		} else {
			base   = be_transform_node(ptr);
			offset = 0;
		}
	}

592
593
	address->ptr    = base;
	address->ptr2   = ptr2;
594
595
596
597
	address->entity = entity;
	address->offset = offset;
}

Hannes Rapp's avatar
Hannes Rapp committed
598
599
600
601
602
603
604
605
/**
 * Creates an sparc Add.
 *
 * @param node   FIRM node
 * @return the created sparc Add node
 */
static ir_node *gen_Add(ir_node *node)
{
606
607
	ir_mode *mode = get_irn_mode(node);
	ir_node *right;
Hannes Rapp's avatar
Hannes Rapp committed
608

609
	if (mode_is_float(mode)) {
610
611
		return gen_helper_binfpop(node, mode, new_bd_sparc_fadd_s,
		                          new_bd_sparc_fadd_d, new_bd_sparc_fadd_q);
612
	}
Hannes Rapp's avatar
Hannes Rapp committed
613

614
	/* special case: + 0x1000 can be represented as - 0x1000 */
615
	right = get_Add_right(node);
616
	if (is_Const(right)) {
Matthias Braun's avatar
Matthias Braun committed
617
618
619
		ir_node   *left = get_Add_left(node);
		ir_tarval *tv;
		uint32_t   val;
620
621
		/* is this simple address arithmetic? then we can let the linker do
		 * the calculation. */
622
		if (is_SymConst(left) && get_irn_n_edges(left) == 1) {
623
624
625
626
			dbg_info *dbgi  = get_irn_dbg_info(node);
			ir_node  *block = be_transform_node(get_nodes_block(node));
			address_t address;

627
628
629
630
			/* 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,
631
632
633
634
635
			                           address.entity, address.offset);
		}

		tv  = get_Const_tarval(right);
		val = get_tarval_long(tv);
636
637
638
639
640
		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);
641
			return new_bd_sparc_Sub_imm(dbgi, block, new_op, NULL, -0x1000);
642
643
644
		}
	}

645
646
	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
647
648
}

649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
static ir_node *gen_AddCC_t(ir_node *node)
{
	return gen_helper_binop(node, MATCH_COMMUTATIVE | MATCH_MODE_NEUTRAL,
	                        new_bd_sparc_AddCC_reg, new_bd_sparc_AddCC_imm);
}

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:
		panic("Invalid AddCC_t proj found");
	}
}

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
677
678
679
680
681
682
683
684
/**
 * 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
685
	ir_mode *mode = get_irn_mode(node);
686
687

	if (mode_is_float(mode)) {
688
689
		return gen_helper_binfpop(node, mode, new_bd_sparc_fsub_s,
		                          new_bd_sparc_fsub_d, new_bd_sparc_fsub_q);
690
	}
Hannes Rapp's avatar
Hannes Rapp committed
691

Matthias Braun's avatar
Matthias Braun committed
692
693
	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
694
695
}

696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
static ir_node *gen_SubCC_t(ir_node *node)
{
	return gen_helper_binop(node, MATCH_MODE_NEUTRAL,
	                        new_bd_sparc_SubCC_reg, new_bd_sparc_SubCC_imm);
}

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:
		panic("Invalid SubCC_t proj found");
	}
}

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

724
725
726
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)
727
728
729
730
731
{
	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,
732
		                          offset, is_frame_entity);
733
734
	} else if (bits == 64) {
		return new_bd_sparc_Ldf_d(dbgi, block, ptr, mem, mode, entity,
735
		                          offset, is_frame_entity);
736
737
738
	} else {
		assert(bits == 128);
		return new_bd_sparc_Ldf_q(dbgi, block, ptr, mem, mode, entity,
739
		                          offset, is_frame_entity);
740
741
742
	}
}

743
744
745
746
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)
747
748
749
750
{
	unsigned bits = get_mode_size_bits(mode);
	assert(mode_is_float(mode));
	if (bits == 32) {
751
		return new_bd_sparc_Stf_s(dbgi, block, value, ptr, mem, mode, entity,
752
		                          offset, is_frame_entity);
753
	} else if (bits == 64) {
754
		return new_bd_sparc_Stf_d(dbgi, block, value, ptr, mem, mode, entity,
755
		                          offset, is_frame_entity);
756
757
	} else {
		assert(bits == 128);
758
		return new_bd_sparc_Stf_q(dbgi, block, value, ptr, mem, mode, entity,
759
		                          offset, is_frame_entity);
760
761
762
	}
}

Hannes Rapp's avatar
Hannes Rapp committed
763
764
765
766
767
768
769
770
/**
 * Transforms a Load.
 *
 * @param node    the ir Load node
 * @return the created sparc Load node
 */
static ir_node *gen_Load(ir_node *node)
{
771
	dbg_info *dbgi     = get_irn_dbg_info(node);
Hannes Rapp's avatar
Hannes Rapp committed
772
773
774
775
776
777
	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;
778
779
	address_t address;

780
781
782
783
	if (get_Load_unaligned(node) == align_non_aligned) {
		panic("sparc: transformation of unaligned Loads not implemented yet");
	}

784
	if (mode_is_float(mode)) {
785
786
		match_address(ptr, &address, false);
		new_load = create_ldf(dbgi, block, address.ptr, new_mem, mode,
787
		                      address.entity, address.offset, false);
788
	} else {
789
790
791
792
793
794
795
796
797
798
		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);
		}
799
	}
Hannes Rapp's avatar
Hannes Rapp committed
800
801
	set_irn_pinned(new_load, get_irn_pinned(node));

Hannes Rapp's avatar
Hannes Rapp committed
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
	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);
820
821
822
	ir_node  *new_store = NULL;
	address_t address;

823
824
825
826
	if (get_Store_unaligned(node) == align_non_aligned) {
		panic("sparc: transformation of unaligned Stores not implemented yet");
	}

827
	if (mode_is_float(mode)) {
828
		ir_node *new_val = be_transform_node(val);
829
830
831
		/* TODO: variants with reg+reg address mode */
		match_address(ptr, &address, false);
		new_store = create_stf(dbgi, block, new_val, address.ptr, new_mem,
832
		                       mode, address.entity, address.offset, false);
833
	} else {
834
835
836
837
838
839
840
841
842
		ir_node *new_val;
		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);
		}
		new_val = be_transform_node(val);

		assert(dest_bits <= 32);
843
844
845
846
847
848
849
850
851
852
		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);
		}
853
854
	}
	set_irn_pinned(new_store, get_irn_pinned(node));
Hannes Rapp's avatar
Hannes Rapp committed
855
856
857
858

	return new_store;
}

859
860
/**
 * Creates an sparc Mul.
861
 * returns the lower 32bits of the 64bit multiply result
862
863
864
 *
 * @return the created sparc Mul node
 */
865
866
867
static ir_node *gen_Mul(ir_node *node)
{
	ir_mode *mode = get_irn_mode(node);
Hannes Rapp's avatar
Hannes Rapp committed
868
	if (mode_is_float(mode)) {
869
870
		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
871
	}
872

873
	return gen_helper_binop(node, MATCH_COMMUTATIVE | MATCH_MODE_NEUTRAL,
874
	                        new_bd_sparc_Mul_reg, new_bd_sparc_Mul_imm);
875
876
877
878
879
880
881
882
}

/**
 * Creates an sparc Mulh.
 * Mulh returns the upper 32bits of a mul instruction
 *
 * @return the created sparc Mulh node
 */
883
884
static ir_node *gen_Mulh(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
885
	ir_mode *mode = get_irn_mode(node);
886
887
888
889
890
	ir_node *mul;

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

891
892
893
894
895
896
897
	if (mode_is_signed(mode)) {
		mul = gen_helper_binop(node, MATCH_COMMUTATIVE, new_bd_sparc_SMulh_reg, new_bd_sparc_SMulh_imm);
		return new_r_Proj(mul, mode_gp, pn_sparc_SMulh_low);
	} else {
		mul = gen_helper_binop(node, MATCH_COMMUTATIVE, new_bd_sparc_UMulh_reg, new_bd_sparc_UMulh_imm);
		return new_r_Proj(mul, mode_gp, pn_sparc_UMulh_low);
	}
898
}
Hannes Rapp's avatar
Hannes Rapp committed
899

900
901
902
903
904
905
906
907
908
909
910
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);
}

911
912
913
914
915
/**
 * Creates an sparc Div.
 *
 * @return the created sparc Div node
 */
916
917
static ir_node *gen_Div(ir_node *node)
{
918
919
920
921
922
923
924
925
	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;
926

927
928
929
930
931
	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);
	}

932
	if (mode_is_signed(mode)) {
933
934
935
936
937
938
939
940
941
942
943
		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);
		}
944
	} else {
945
946
		ir_graph *irg       = get_irn_irg(node);
		ir_node  *left_high = get_g0(irg);
947
948
949
950
951
952
953
954
955
		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);
		}
956
	}
957

958
	return res;
959
960
}

961
962
963
/**
 * Transforms a Not node.
 *
Michael Beck's avatar
Michael Beck committed
964
 * @return the created sparc Not node
965
966
967
 */
static ir_node *gen_Not(ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
968
	ir_node  *op     = get_Not_op(node);
969
970
	ir_graph *irg    = get_irn_irg(node);
	ir_node  *zero   = get_g0(irg);
971
972
973
	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);
974

975
	/* Note: Not(Eor()) is normalize in firm localopts already so
Matthias Braun's avatar
Matthias Braun committed
976
977
978
	 * we don't match it for xnor here */

	/* Not can be represented with xnor 0, n */
979
	return new_bd_sparc_XNor_reg(dbgi, block, zero, new_op);
980
981
}

982
983
984
985
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,
986
987
                                 new_binop_imm_func new_not_imm,
                                 match_flags_t flags)
988
{
989
990
991
	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
992
		return gen_helper_binop_args(node, op2, get_Not_op(op1),
993
		                             flags,
994
		                             new_not_reg, new_not_imm);
995
	}
996
997
	if (is_Not(op2)) {
		return gen_helper_binop_args(node, op1, get_Not_op(op2),
998
		                             flags,
999
		                             new_not_reg, new_not_imm);
1000
	}
For faster browsing, not all history is shown. View entire blame