arm_lower64.c 14.9 KB
Newer Older
Matthias Braun's avatar
Matthias Braun committed
1
2
3
4
5
6
7
8
9
10
/*
 * This file is part of libFirm.
 * Copyright (C) 2014 University of Karlsruhe.
 */

/**
 * @file
 * @brief    ARM 64bit lowering
 * @author   Matthias Braun
 */
11
12
#include "arm_bearch_t.h"

Matthias Braun's avatar
Matthias Braun committed
13
#include "arm_nodes_attr.h"
14
#include "benode.h"
Matthias Braun's avatar
Matthias Braun committed
15
16
17
#include "gen_arm_new_nodes.h"
#include "gen_arm_regalloc_if.h"
#include "ircons_t.h"
18
#include "iredges_t.h"
Matthias Braun's avatar
Matthias Braun committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "lower_dw.h"
#include "panic.h"
#include "util.h"

static void lower64_add(ir_node *node, ir_mode *mode)
{
	dbg_info *dbgi       = get_irn_dbg_info(node);
	ir_node  *block      = get_nodes_block(node);
	ir_node  *left       = get_Add_left(node);
	ir_node  *right      = get_Add_right(node);
	ir_node  *left_low   = get_lowered_low(left);
	ir_node  *left_high  = get_lowered_high(left);
	ir_node  *right_low  = get_lowered_low(right);
	ir_node  *right_high = get_lowered_high(right);
	ir_node  *adds       = new_bd_arm_AddS_t(dbgi, block, left_low, right_low);
	ir_mode  *mode_low   = get_irn_mode(left_low);
	ir_node  *res_low    = new_r_Proj(adds, mode_low, pn_arm_AddS_t_res);
	ir_node  *res_flags  = new_r_Proj(adds, mode_ANY, pn_arm_AddS_t_flags);
	ir_node  *adc        = new_bd_arm_AdC_t(dbgi, block, left_high,
	                                        right_high, res_flags, mode);
	ir_set_dw_lowered(node, res_low, adc);
}

static void lower64_sub(ir_node *node, ir_mode *mode)
{
	dbg_info *dbgi       = get_irn_dbg_info(node);
	ir_node  *block      = get_nodes_block(node);
	ir_node  *left       = get_Sub_left(node);
	ir_node  *right      = get_Sub_right(node);
	ir_node  *left_low   = get_lowered_low(left);
	ir_node  *left_high  = get_lowered_high(left);
	ir_node  *right_low  = get_lowered_low(right);
	ir_node  *right_high = get_lowered_high(right);
	ir_node  *subs       = new_bd_arm_SubS_t(dbgi, block, left_low, right_low);
	ir_mode  *mode_low   = get_irn_mode(left_low);
	ir_node  *res_low    = new_r_Proj(subs, mode_low, pn_arm_SubS_t_res);
	ir_node  *res_flags  = new_r_Proj(subs, mode_ANY, pn_arm_SubS_t_flags);
	ir_node  *sbc        = new_bd_arm_SbC_t(dbgi, block, left_high,
	                                        right_high, res_flags, mode);
	ir_set_dw_lowered(node, res_low, sbc);
}

static void lower64_minus(ir_node *node, ir_mode *mode)
{
	dbg_info *dbgi         = get_irn_dbg_info(node);
	ir_graph *irg          = get_irn_irg(node);
	ir_node  *block        = get_nodes_block(node);
	ir_node  *op           = get_Minus_op(node);
	ir_node  *right_low    = get_lowered_low(op);
	ir_node  *right_high   = get_lowered_high(op);
	ir_mode  *low_unsigned = get_irn_mode(right_low);
	ir_node  *left_low     = new_r_Const_null(irg, low_unsigned);
	ir_node  *left_high    = new_r_Const_null(irg, mode);
	ir_node  *subs         = new_bd_arm_SubS_t(dbgi, block, left_low,
	                                           right_low);
	ir_node  *res_low      = new_r_Proj(subs, low_unsigned, pn_arm_SubS_t_res);
	ir_node  *res_flags    = new_r_Proj(subs, mode_ANY, pn_arm_SubS_t_flags);
	ir_node  *sbc          = new_bd_arm_SbC_t(dbgi, block, left_high,
	                                          right_high, res_flags, mode);
	ir_set_dw_lowered(node, res_low, sbc);
}

static void lower64_mul(ir_node *node, ir_mode *mode)
{
	dbg_info *dbgi       = get_irn_dbg_info(node);
	ir_node  *block      = get_nodes_block(node);
	ir_node  *left       = get_Mul_left(node);
	ir_node  *right      = get_Mul_right(node);
	ir_node  *left_low   = get_lowered_low(left);
	ir_node  *left_high  = get_lowered_high(left);
	ir_node  *right_low  = get_lowered_low(right);
	ir_node  *right_high = get_lowered_high(right);
	ir_node  *conv_l_low = new_rd_Conv(dbgi, block, left_low, mode);
92
	ir_node  *mul1       = new_rd_Mul(dbgi, block, conv_l_low, right_high);
Matthias Braun's avatar
Matthias Braun committed
93
94
95
96
	ir_node  *umull      = new_bd_arm_UMulL_t(dbgi, block, left_low, right_low);
	ir_mode  *umode      = get_irn_mode(right_low);
	ir_node  *umull_low  = new_r_Proj(umull, umode, pn_arm_UMulL_t_low);
	ir_node  *umull_high = new_r_Proj(umull, mode, pn_arm_UMulL_t_high);
Matthias Braun's avatar
Matthias Braun committed
97
	ir_node  *conv_r_low = new_rd_Conv(dbgi, block, right_low, mode);
98
	ir_node  *mul2       = new_rd_Mul(dbgi, block, conv_r_low, left_high);
99
100
	ir_node  *add1       = new_rd_Add(dbgi, block, mul2, mul1);
	ir_node  *add2       = new_rd_Add(dbgi, block, add1, umull_high);
Matthias Braun's avatar
Matthias Braun committed
101
	ir_set_dw_lowered(node, umull_low, add2);
Matthias Braun's avatar
Matthias Braun committed
102
103
104
105
106
}

static ir_entity *ldivmod;
static ir_entity *uldivmod;

107
108
static ir_entity *make_divmod(char const *const name, ir_type *const even, ir_type *const odd)
{
109
	ir_type *const mtp = new_type_method(4, 4, false);
110
111
112
113
114
115
116
117
118
	set_method_param_type(mtp, 0, even);
	set_method_param_type(mtp, 1, odd);
	set_method_param_type(mtp, 2, even);
	set_method_param_type(mtp, 3, odd);
	set_method_res_type(mtp, 0, even);
	set_method_res_type(mtp, 1, odd);
	set_method_res_type(mtp, 2, even);
	set_method_res_type(mtp, 3, odd);

119
120
	ident *const id = new_id_from_str(name);
	return create_compilerlib_entity(id, mtp);
121
122
}

Matthias Braun's avatar
Matthias Braun committed
123
124
125
static void create_divmod_intrinsics(ir_mode *mode_unsigned,
                                     ir_mode *mode_signed)
{
126
127
	ir_type *const tp_unsigned = get_type_for_mode(mode_unsigned);
	uldivmod = make_divmod("__aeabi_uldivmod", tp_unsigned, tp_unsigned);
Matthias Braun's avatar
Matthias Braun committed
128

129
130
131
132
	ir_type *const tp_signed = get_type_for_mode(mode_signed);
	ir_type *const even      = arm_cg_config.big_endian ? tp_signed   : tp_unsigned;
	ir_type *const odd       = arm_cg_config.big_endian ? tp_unsigned : tp_signed;
	ldivmod = make_divmod("__aeabi_ldivmod", even, odd);
Matthias Braun's avatar
Matthias Braun committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
}

static void lower_divmod(ir_node *node, ir_node *left, ir_node *right,
                         ir_node *mem, ir_mode *mode, int res_offset)
{
	dbg_info  *dbgi       = get_irn_dbg_info(node);
	ir_node   *block      = get_nodes_block(node);
	ir_node   *left_low   = get_lowered_low(left);
	ir_node   *left_high  = get_lowered_high(left);
	ir_node   *right_low  = get_lowered_low(right);
	ir_node   *right_high = get_lowered_high(right);
	ir_mode   *node_mode  = get_irn_mode(left);
	ir_entity *entity     = mode_is_signed(node_mode) ? ldivmod : uldivmod;
	ir_type   *mtp        = get_entity_type(entity);
	ir_graph  *irg        = get_irn_irg(node);
	ir_node   *addr       = new_r_Address(irg, entity);
	ir_node   *in[4];
	if (arm_cg_config.big_endian) {
		in[0] = left_high;
		in[1] = left_low;
		in[2] = right_high;
		in[3] = right_low;
	} else {
		in[0] = left_low;
		in[1] = left_high;
		in[2] = right_low;
		in[3] = right_high;
	}
	ir_node *call    = new_rd_Call(dbgi, block, mem, addr, ARRAY_SIZE(in), in,
	                               mtp);
	ir_node *resproj = new_r_Proj(call, mode_T, pn_Call_T_result);
	set_irn_pinned(call, get_irn_pinned(node));
	foreach_out_edge_safe(node, edge) {
		ir_node *proj = get_edge_src_irn(edge);
		if (!is_Proj(proj))
			continue;

170
		switch ((pn_Div)get_Proj_num(proj)) {
Matthias Braun's avatar
Matthias Braun committed
171
172
173
		case pn_Div_M:
			/* reroute to the call */
			set_Proj_pred(proj, call);
174
			set_Proj_num(proj, pn_Call_M);
Matthias Braun's avatar
Matthias Braun committed
175
176
177
			break;
		case pn_Div_X_regular:
			set_Proj_pred(proj, call);
178
			set_Proj_num(proj, pn_Call_X_regular);
Matthias Braun's avatar
Matthias Braun committed
179
180
181
			break;
		case pn_Div_X_except:
			set_Proj_pred(proj, call);
182
			set_Proj_num(proj, pn_Call_X_except);
Matthias Braun's avatar
Matthias Braun committed
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
			break;
		case pn_Div_res: {
			ir_mode *low_mode = get_irn_mode(left_low);
			if (arm_cg_config.big_endian) {
				ir_node *res_low  = new_r_Proj(resproj, low_mode, res_offset+1);
				ir_node *res_high = new_r_Proj(resproj, mode,     res_offset);
				ir_set_dw_lowered(proj, res_low, res_high);
			} else {
				ir_node *res_low  = new_r_Proj(resproj, low_mode, res_offset);
				ir_node *res_high = new_r_Proj(resproj, mode,     res_offset+1);
				ir_set_dw_lowered(proj, res_low, res_high);
			}
			break;
		}
		}
		/* mark this proj: we have handled it already, otherwise we might fall
		 * into out new nodes. */
		mark_irn_visited(proj);
	}
}

static void lower64_div(ir_node *node, ir_mode *mode)
{
	ir_node *left  = get_Div_left(node);
	ir_node *right = get_Div_right(node);
	ir_node *mem   = get_Div_mem(node);
	lower_divmod(node, left, right, mem, mode, 0);
}

static void lower64_mod(ir_node *node, ir_mode *mode)
{
	ir_node *left  = get_Mod_left(node);
	ir_node *right = get_Mod_right(node);
	ir_node *mem   = get_Mod_mem(node);
	lower_divmod(node, left, right, mem, mode, 2);
}

static void lower64_shl(ir_node *node, ir_mode *mode)
{
	/* the following algo works, because we have modulo shift 256 */
	assert(get_mode_modulo_shift(mode) == 256);
	dbg_info *dbgi       = get_irn_dbg_info(node);
	ir_node  *block      = get_nodes_block(node);
	ir_node  *left       = get_Shl_left(node);
	ir_node  *left_low   = get_lowered_low(left);
	ir_node  *left_high  = get_lowered_high(left);
	ir_node  *right      = get_Shl_right(node);
	ir_mode  *umode      = get_irn_mode(left_low);
	ir_node  *right_low;
	if (get_mode_size_bits(get_irn_mode(right)) == 64) {
		right_low = get_lowered_low(right);
	} else {
		right_low = new_rd_Conv(dbgi, block, right, umode);
	}
Christoph Mallon's avatar
Christoph Mallon committed
237
	/* Res_lo = L_hi << R | L_lo << (R - 32) | L_lo >> (32 - R) */
238
	ir_node  *shl1     = new_rd_Shl(dbgi, block, left_high, right_low);
Christoph Mallon's avatar
Christoph Mallon committed
239
240
241
242
	ir_graph *irg      = get_irn_irg(node);
	ir_node  *c32      = new_r_Const_long(irg, umode, 32);
	ir_node  *sub      = new_rd_Sub(dbgi, block, right_low, c32, umode);
	ir_node  *llo_conv = new_rd_Conv(dbgi, block, left_low, mode);
243
	ir_node  *shl2     = new_rd_Shl(dbgi, block, llo_conv, sub);
244
	ir_node  *or       = new_rd_Or(dbgi, block, shl1, shl2);
Christoph Mallon's avatar
Christoph Mallon committed
245
	ir_node  *sub2     = new_rd_Sub(dbgi, block, c32, right_low, umode);
246
	ir_node  *shr      = new_rd_Shr(dbgi, block, llo_conv, sub2);
247
	ir_node  *or2      = new_rd_Or(dbgi, block, or, shr);
Christoph Mallon's avatar
Christoph Mallon committed
248
	/* Res_lo = L_lo << R */
249
	ir_node  *low      = new_rd_Shl(dbgi, block, left_low, right_low);
Matthias Braun's avatar
Matthias Braun committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
	ir_set_dw_lowered(node, low, or2);
}

static void lower64_shr(ir_node *node, ir_mode *mode)
{
	/* the following algo works, because we have modulo shift 256 */
	assert(get_mode_modulo_shift(mode) == 256);
	dbg_info *dbgi       = get_irn_dbg_info(node);
	ir_node  *block      = get_nodes_block(node);
	ir_node  *left       = get_Shr_left(node);
	ir_node  *left_low   = get_lowered_low(left);
	ir_node  *left_high  = get_lowered_high(left);
	ir_node  *right      = get_Shr_right(node);
	ir_mode  *umode      = get_irn_mode(left_low);
	ir_node  *right_low;
	if (get_mode_size_bits(get_irn_mode(right)) == 64) {
		right_low = get_lowered_low(right);
	} else {
		right_low = new_rd_Conv(dbgi, block, right, umode);
	}
Christoph Mallon's avatar
Christoph Mallon committed
270
	/* Res_lo = L_lo >> R | L_hi >> (R - 32) | L_hi << (32 - R) */
271
	ir_node  *shr1     = new_rd_Shr(dbgi, block, left_low, right_low);
Christoph Mallon's avatar
Christoph Mallon committed
272
273
274
275
	ir_graph *irg      = get_irn_irg(node);
	ir_node  *c32      = new_r_Const_long(irg, umode, 32);
	ir_node  *sub      = new_rd_Sub(dbgi, block, right_low, c32, umode);
	ir_node  *lhi_conv = new_rd_Conv(dbgi, block, left_high, umode);
276
	ir_node  *shr2     = new_rd_Shr(dbgi, block, lhi_conv, sub);
277
	ir_node  *or       = new_rd_Or(dbgi, block, shr1, shr2);
Christoph Mallon's avatar
Christoph Mallon committed
278
	ir_node  *sub2     = new_rd_Sub(dbgi, block, c32, right_low, umode);
279
	ir_node  *shl      = new_rd_Shl(dbgi, block, lhi_conv, sub2);
280
	ir_node  *or2      = new_rd_Or(dbgi, block, or, shl);
Christoph Mallon's avatar
Christoph Mallon committed
281
	/* Res_hi = L_hi >> R */
282
	ir_node  *shr3       = new_rd_Shr(dbgi, block, left_high, right_low);
Matthias Braun's avatar
Matthias Braun committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
	ir_set_dw_lowered(node, or2, shr3);
}

static void lower64_shrs(ir_node *node, ir_mode *mode)
{
	(void)mode;
	/* the following algo works, because we have modulo shift 256 */
	assert(get_mode_modulo_shift(mode) == 256);
	dbg_info *dbgi       = get_irn_dbg_info(node);
	ir_node  *block      = get_nodes_block(node);
	ir_node  *left       = get_Shrs_left(node);
	ir_node  *left_low   = get_lowered_low(left);
	ir_node  *left_high  = get_lowered_high(left);
	ir_node  *right      = get_Shrs_right(node);
	ir_mode  *umode      = get_irn_mode(left_low);
	ir_node  *right_low;
	if (get_mode_size_bits(get_irn_mode(right)) == 64) {
		right_low = get_lowered_low(right);
	} else {
		right_low = new_rd_Conv(dbgi, block, right, umode);
	}
304
	ir_node  *shr        = new_rd_Shr(dbgi, block, left_low, right_low);
Matthias Braun's avatar
Matthias Braun committed
305
306
307
308
309
310
311
	ir_graph *irg        = get_irn_irg(node);
	ir_node  *c32        = new_r_Const_long(irg, umode, 32);
	ir_node  *sub        = new_rd_Sub(dbgi, block, c32, right_low, umode);
	ir_node  *subs       = new_bd_arm_SubS_t(dbgi, block, right_low, c32);
	ir_node  *subs_res   = new_r_Proj(subs, umode, pn_arm_SubS_t_res);
	ir_node  *subs_flags = new_r_Proj(subs, mode_ANY, pn_arm_SubS_t_flags);
	ir_node  *left_highu = new_rd_Conv(dbgi, block, left_high, umode);
312
	ir_node  *shl        = new_rd_Shl(dbgi, block, left_highu, sub);
313
	ir_node  *or         = new_rd_Or(dbgi, block, shr, shl);
314
	ir_node  *shrs       = new_rd_Shrs(dbgi, block, left_highu, subs_res);
Matthias Braun's avatar
Matthias Braun committed
315
316
	ir_node  *orpl       = new_bd_arm_OrPl_t(dbgi, block, or, shrs, or,
	                                         subs_flags, umode);
317
	ir_node  *shrs2      = new_rd_Shrs(dbgi, block, left_high, right_low);
Matthias Braun's avatar
Matthias Braun committed
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
	ir_set_dw_lowered(node, orpl, shrs2);
}

static ir_entity *create_64_intrinsic_fkt(ir_type *method, const ir_op *op,
                                          const ir_mode *imode,
                                          const ir_mode *omode, void *context)
{
	(void)context;
	(void)omode;

	const char *name;
	if (op == op_Conv) {
		if (mode_is_float(imode)) {
			assert(get_mode_size_bits(omode) == 64);
			if (get_mode_size_bits(imode) == 64) {
				name = mode_is_signed(omode) ? "__fixdfdi" : "__fixunsdfdi";
			} else if (get_mode_size_bits(imode) == 32) {
				name = mode_is_signed(omode) ? "__fixsfdi" : "__fixunssfdi";
			} else {
				assert(get_mode_size_bits(imode) == 128);
				panic("can't conver long double to long long yet");
			}
		} else if (mode_is_float(omode)) {
			assert(get_mode_size_bits(imode) == 64);
			if (get_mode_size_bits(omode) == 64) {
				name = mode_is_signed(imode) ? "__floatdidf" : "__floatundidf";
			} else if (get_mode_size_bits(omode) == 32) {
				name = mode_is_signed(imode) ? "__floatdisf" : "__floatundisf";
			} else {
				assert(get_mode_size_bits(omode) == 128);
				panic("can't convert long long to long double yet");
			}
		} else {
			panic("can't lower 64bit Conv");
		}
	} else {
354
		panic("cannot lower unexpected 64bit operation %s", get_op_name(op));
Matthias Braun's avatar
Matthias Braun committed
355
356
357
	}
	ident     *id     = new_id_from_str(name);
	ir_type   *glob   = get_glob_type();
358
359
360
	ir_entity *result = new_global_entity(glob, id, method,
	                                      ir_visibility_external,
	                                      IR_LINKAGE_DEFAULT);
Matthias Braun's avatar
Matthias Braun committed
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
	return result;
}

void arm_lower_64bit(void)
{
	ir_mode *word_unsigned = arm_mode_gp;
	ir_mode *word_signed   = find_signed_mode(word_unsigned);
	lwrdw_param_t lower_dw_params = {
		create_64_intrinsic_fkt,
		NULL,
		word_unsigned,
		word_signed,
		64, /* doubleword size */
		arm_cg_config.big_endian,
	};

	create_divmod_intrinsics(word_unsigned, word_signed);

	/* make sure opcodes are initialized */
380
	arm_create_opcodes();
Matthias Braun's avatar
Matthias Braun committed
381
382
383
384
385
386
387
388
389
390
391
392
393

	ir_prepare_dw_lowering(&lower_dw_params);
	ir_register_dw_lower_function(op_Add,   lower64_add);
	ir_register_dw_lower_function(op_Div,   lower64_div);
	ir_register_dw_lower_function(op_Minus, lower64_minus);
	ir_register_dw_lower_function(op_Mod,   lower64_mod);
	ir_register_dw_lower_function(op_Mul,   lower64_mul);
	ir_register_dw_lower_function(op_Shl,   lower64_shl);
	ir_register_dw_lower_function(op_Shr,   lower64_shr);
	ir_register_dw_lower_function(op_Shrs,  lower64_shrs);
	ir_register_dw_lower_function(op_Sub,   lower64_sub);
	ir_lower_dw_ops();
}