2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief This file implements the mapping of 64Bit intrinsic
23 * functions to code or library calls.
24 * @author Michael Beck
34 #include "iroptimize.h"
40 #include "ia32_new_nodes.h"
41 #include "bearch_ia32_t.h"
42 #include "gen_ia32_regalloc_if.h"
45 /** The array of all intrinsics that must be mapped. */
46 static i_record *intrinsics;
48 /** An array to cache all entities. */
49 static ir_entity *i_ents[iro_Last + 1];
52 * Maps all intrinsic calls that the backend support
53 * and map all instructions the backend did not support
56 void ia32_handle_intrinsics(void)
58 if (intrinsics && ARR_LEN(intrinsics) > 0) {
59 lower_intrinsics(intrinsics, ARR_LEN(intrinsics), /*part_block_used=*/1);
63 #define BINOP_Left_Low 0
64 #define BINOP_Left_High 1
65 #define BINOP_Right_Low 2
66 #define BINOP_Right_High 3
69 * Reroute edges from the pn_Call_T_result proj of a call.
71 * @param resproj the pn_Call_T_result Proj
72 * @param l_res the lower 32 bit result
73 * @param h_res the upper 32 bit result or NULL
75 static void reroute_result(ir_node *resproj, ir_node *l_res, ir_node *h_res)
77 foreach_out_edge_safe(resproj, edge) {
78 ir_node *proj = get_edge_src_irn(edge);
79 long pn = get_Proj_proj(proj);
82 edges_reroute(proj, l_res);
83 } else if (pn == 1 && h_res != NULL) {
84 edges_reroute(proj, h_res);
86 panic("Unsupported Result-Proj from Call found");
92 * Replace a call be a tuple of l_res, h_res.
94 * @param call the call node to replace
95 * @param l_res the lower 32 bit result
96 * @param h_res the upper 32 bit result or NULL
97 * @param irg the graph to replace on
98 * @param block the block to replace on (always the call block)
100 static void resolve_call(ir_node *call, ir_node *l_res, ir_node *h_res, ir_graph *irg, ir_node *block)
102 ir_node *jmp, *res, *in[2];
103 ir_node *nomem = get_irg_no_mem(irg);
106 if (edges_activated(irg)) {
107 /* use rerouting to prevent some warning in the backend */
108 foreach_out_edge_safe(call, edge) {
109 ir_node *proj = get_edge_src_irn(edge);
110 pn_Call pn = (pn_Call)get_Proj_proj(proj);
113 case pn_Call_X_regular:
115 * We do not check here if this call really has exception and regular Proj's.
116 * new_r_Jmp might than be CSEd with the real exit jmp and then bad things happen
117 * (in movgen.c from 186.crafty for example).
118 * So be sure the newly created Jmp cannot CSE.
120 old_cse = get_opt_cse();
122 jmp = new_r_Jmp(block);
123 set_opt_cse(old_cse);
124 edges_reroute(proj, jmp);
127 case pn_Call_X_except:
128 /* should not happen here */
129 edges_reroute(proj, new_r_Bad(irg, mode_X));
132 /* should not happen here */
133 edges_reroute(proj, nomem);
135 case pn_Call_T_result:
136 reroute_result(proj, l_res, h_res);
139 panic("Wrong Proj from Call");
145 /* no edges, build Tuple */
151 res = new_r_Tuple(block, 2, in);
156 * We do not check here if this call really has exception and regular Proj's.
157 * new_r_Jmp might than be CSEd with the real exit jmp and then bad things happen
158 * (in movgen.c from 186.crafty for example).
159 * So be sure the newly created Jmp cannot CSE.
161 old_cse = get_opt_cse();
163 jmp = new_r_Jmp(block);
164 set_opt_cse(old_cse);
166 ir_node *const in[] = {
168 [pn_Call_T_result] = res,
169 [pn_Call_X_regular] = jmp,
170 [pn_Call_X_except] = new_r_Bad(irg, mode_X),
172 turn_into_tuple(call, ARRAY_SIZE(in), in);
177 * Map an Add (a_l, a_h, b_l, b_h)
179 static int map_Add(ir_node *call, void *ctx)
181 dbg_info *dbg = get_irn_dbg_info(call);
182 ir_node *block = get_nodes_block(call);
183 ir_node **params = get_Call_param_arr(call);
184 ir_type *method = get_Call_type(call);
185 ir_node *a_l = params[BINOP_Left_Low];
186 ir_node *a_h = params[BINOP_Left_High];
187 ir_node *b_l = params[BINOP_Right_Low];
188 ir_node *b_h = params[BINOP_Right_High];
189 ir_mode *l_mode = get_type_mode(get_method_res_type(method, 0));
190 ir_mode *h_mode = get_type_mode(get_method_res_type(method, 1));
191 ir_mode *mode_flags = ia32_reg_classes[CLASS_ia32_flags].mode;
192 ir_node *add_low, *add_high, *flags;
193 ir_node *l_res, *h_res;
196 /* l_res = a_l + b_l */
197 /* h_res = a_h + b_h + carry */
199 add_low = new_bd_ia32_l_Add(dbg, block, a_l, b_l, mode_T);
200 flags = new_r_Proj(add_low, mode_flags, pn_ia32_flags);
201 add_high = new_bd_ia32_l_Adc(dbg, block, a_h, b_h, flags, h_mode);
203 l_res = new_r_Proj(add_low, l_mode, pn_ia32_res);
206 resolve_call(call, l_res, h_res, current_ir_graph, block);
211 * Map a Sub (a_l, a_h, b_l, b_h)
213 static int map_Sub(ir_node *call, void *ctx)
215 dbg_info *dbg = get_irn_dbg_info(call);
216 ir_node *block = get_nodes_block(call);
217 ir_node **params = get_Call_param_arr(call);
218 ir_type *method = get_Call_type(call);
219 ir_node *a_l = params[BINOP_Left_Low];
220 ir_node *a_h = params[BINOP_Left_High];
221 ir_node *b_l = params[BINOP_Right_Low];
222 ir_node *b_h = params[BINOP_Right_High];
223 ir_mode *l_mode = get_type_mode(get_method_res_type(method, 0));
224 ir_mode *h_mode = get_type_mode(get_method_res_type(method, 1));
225 ir_mode *mode_flags = ia32_reg_classes[CLASS_ia32_flags].mode;
226 ir_node *sub_low, *sub_high, *flags;
227 ir_node *l_res, *h_res;
230 /* l_res = a_l - b_l */
231 /* h_res = a_h - b_h - carry */
233 sub_low = new_bd_ia32_l_Sub(dbg, block, a_l, b_l, mode_T);
234 flags = new_r_Proj(sub_low, mode_flags, pn_ia32_flags);
235 sub_high = new_bd_ia32_l_Sbb(dbg, block, a_h, b_h, flags, h_mode);
237 l_res = new_r_Proj(sub_low, l_mode, pn_ia32_res);
240 resolve_call(call, l_res, h_res, current_ir_graph, block);
245 * Checks where node high is a sign extension of low.
247 static int is_sign_extend(ir_node *low, ir_node *high)
252 ir_tarval *shift_count;
254 high_r = get_Shrs_right(high);
255 if (!is_Const(high_r)) return 0;
257 shift_count = get_Const_tarval(high_r);
258 if (!tarval_is_long(shift_count)) return 0;
259 if (get_tarval_long(shift_count) != 31) return 0;
261 high_l = get_Shrs_left(high);
263 if (is_Conv(low) && get_Conv_op(low) == high_l) return 1;
264 if (is_Conv(high_l) && get_Conv_op(high_l) == low) return 1;
265 } else if (is_Const(low) && is_Const(high)) {
266 ir_tarval *tl = get_Const_tarval(low);
267 ir_tarval *th = get_Const_tarval(high);
269 if (tarval_is_long(th) && tarval_is_long(tl)) {
270 long l = get_tarval_long(tl);
271 long h = get_tarval_long(th);
273 return (h == 0 && l >= 0) || (h == -1 && l < 0);
281 * Map a Mul (a_l, a_h, b_l, b_h)
283 static int map_Mul(ir_node *call, void *ctx)
285 dbg_info *dbg = get_irn_dbg_info(call);
286 ir_node *block = get_nodes_block(call);
287 ir_node **params = get_Call_param_arr(call);
288 ir_type *method = get_Call_type(call);
289 ir_node *a_l = params[BINOP_Left_Low];
290 ir_node *a_h = params[BINOP_Left_High];
291 ir_node *b_l = params[BINOP_Right_Low];
292 ir_node *b_h = params[BINOP_Right_High];
293 ir_mode *l_mode = get_type_mode(get_method_res_type(method, 0));
294 ir_mode *h_mode = get_type_mode(get_method_res_type(method, 1));
295 ir_node *l_res, *h_res, *mul, *pEDX, *add;
308 /* handle the often used case of 32x32=64 mul */
309 if (is_sign_extend(a_l, a_h) && is_sign_extend(b_l, b_h)) {
310 mul = new_bd_ia32_l_IMul(dbg, block, a_l, b_l);
311 h_res = new_rd_Proj(dbg, mul, h_mode, pn_ia32_l_IMul_res_high);
312 l_res = new_rd_Proj(dbg, mul, l_mode, pn_ia32_l_IMul_res_low);
314 /* note that zero extension is handled hare efficiently */
315 mul = new_bd_ia32_l_Mul(dbg, block, a_l, b_l);
316 pEDX = new_rd_Proj(dbg, mul, h_mode, pn_ia32_l_Mul_res_high);
317 l_res = new_rd_Proj(dbg, mul, l_mode, pn_ia32_l_Mul_res_low);
319 b_l = new_rd_Conv(dbg, block, b_l, h_mode);
320 mul = new_rd_Mul( dbg, block, a_h, b_l, h_mode);
321 add = new_rd_Add( dbg, block, mul, pEDX, h_mode);
322 a_l = new_rd_Conv(dbg, block, a_l, h_mode);
323 mul = new_rd_Mul( dbg, block, a_l, b_h, h_mode);
324 h_res = new_rd_Add( dbg, block, add, mul, h_mode);
326 resolve_call(call, l_res, h_res, current_ir_graph, block);
332 * Map a Minus (a_l, a_h)
334 static int map_Minus(ir_node *call, void *ctx)
336 dbg_info *dbg = get_irn_dbg_info(call);
337 ir_node *block = get_nodes_block(call);
338 ir_node **params = get_Call_param_arr(call);
339 ir_type *method = get_Call_type(call);
340 ir_node *a_l = params[BINOP_Left_Low];
341 ir_node *a_h = params[BINOP_Left_High];
342 ir_mode *l_mode = get_type_mode(get_method_res_type(method, 0));
343 ir_mode *h_mode = get_type_mode(get_method_res_type(method, 1));
344 ir_node *l_res, *h_res, *res;
347 res = new_bd_ia32_Minus64Bit(dbg, block, a_l, a_h);
348 l_res = new_r_Proj(res, l_mode, pn_ia32_Minus64Bit_low_res);
349 h_res = new_r_Proj(res, h_mode, pn_ia32_Minus64Bit_high_res);
351 resolve_call(call, l_res, h_res, current_ir_graph, block);
356 #define ID(x) new_id_from_chars(x, sizeof(x)-1)
359 * Maps a Div. Change into a library call.
361 static int map_Div(ir_node *call, void *ctx)
363 ia32_intrinsic_env_t *env = (ia32_intrinsic_env_t*)ctx;
364 ir_type *method = get_Call_type(call);
365 ir_mode *h_mode = get_type_mode(get_method_res_type(method, 1));
368 ir_graph *irg = get_irn_irg(call);
371 if (mode_is_signed(h_mode)) {
372 /* 64bit signed Division */
375 /* create library entity */
376 ident *id = ID("__divdi3");
377 ent = env->divdi3 = create_compilerlib_entity(id, method);
380 /* 64bit unsigned Division */
383 /* create library entity */
384 ident *id = ID("__udivdi3");
385 ent = env->udivdi3 = create_compilerlib_entity(id, method);
389 ptr = get_Call_ptr(call);
391 ptr = new_r_SymConst(irg, get_irn_mode(ptr), sym, symconst_addr_ent);
392 set_Call_ptr(call, ptr);
398 * Maps a Mod. Change into a library call
400 static int map_Mod(ir_node *call, void *ctx)
402 ia32_intrinsic_env_t *env = (ia32_intrinsic_env_t*)ctx;
403 ir_type *method = get_Call_type(call);
404 ir_mode *h_mode = get_type_mode(get_method_res_type(method, 1));
407 ir_graph *irg = get_irn_irg(call);
410 if (mode_is_signed(h_mode)) {
411 /* 64bit signed Modulo */
414 /* create library entity */
415 ident *id = ID("__moddi3");
416 ent = env->moddi3 = create_compilerlib_entity(id, method);
419 /* 64bit signed Modulo */
422 /* create library entity */
423 ident *id = ID("__umoddi3");
424 ent = env->umoddi3 = create_compilerlib_entity(id, method);
428 ptr = get_Call_ptr(call);
430 ptr = new_r_SymConst(irg, get_irn_mode(ptr), sym, symconst_addr_ent);
431 set_Call_ptr(call, ptr);
439 static int map_Conv(ir_node *call, void *ctx)
441 ir_graph *irg = current_ir_graph;
442 dbg_info *dbg = get_irn_dbg_info(call);
443 ir_node *block = get_nodes_block(call);
444 ir_node **params = get_Call_param_arr(call);
445 ir_type *method = get_Call_type(call);
446 int n = get_Call_n_params(call);
447 ir_node *l_res, *h_res;
451 ir_node *float_to_ll;
453 /* We have a Conv float -> long long here */
454 ir_node *a_f = params[0];
455 ir_mode *l_res_mode = get_type_mode(get_method_res_type(method, 0));
456 ir_mode *h_res_mode = get_type_mode(get_method_res_type(method, 1));
458 assert(mode_is_float(get_irn_mode(a_f)) && "unexpected Conv call");
460 if (mode_is_signed(h_res_mode)) {
461 /* convert from float to signed 64bit */
462 float_to_ll = new_bd_ia32_l_FloattoLL(dbg, block, a_f);
464 l_res = new_r_Proj(float_to_ll, l_res_mode,
465 pn_ia32_l_FloattoLL_res_low);
466 h_res = new_r_Proj(float_to_ll, h_res_mode,
467 pn_ia32_l_FloattoLL_res_high);
469 /* Convert from float to unsigned 64bit. */
470 ir_tarval *flt_tv = new_tarval_from_str("9223372036854775808", 19, ia32_mode_E);
471 ir_node *flt_corr = new_r_Const(irg, flt_tv);
472 ir_node *lower_blk = block;
474 ir_node *cmp, *proj, *cond, *blk, *int_phi, *flt_phi;
478 upper_blk = get_nodes_block(call);
480 a_f = new_rd_Conv(dbg, upper_blk, a_f, ia32_mode_E);
481 cmp = new_rd_Cmp(dbg, upper_blk, a_f, flt_corr, ir_relation_less);
482 cond = new_rd_Cond(dbg, upper_blk, cmp);
483 in[0] = new_r_Proj(cond, mode_X, pn_Cond_true);
484 in[1] = new_r_Proj(cond, mode_X, pn_Cond_false);
485 blk = new_r_Block(irg, 1, &in[1]);
486 in[1] = new_r_Jmp(blk);
488 set_irn_in(lower_blk, 2, in);
491 in[0] = new_r_Const(irg, get_mode_null(h_res_mode));
492 in[1] = new_r_Const_long(irg, h_res_mode, 0x80000000);
494 int_phi = new_r_Phi(lower_blk, 2, in, h_res_mode);
497 in[1] = new_rd_Sub(dbg, upper_blk, a_f, flt_corr, ia32_mode_E);
499 flt_phi = new_r_Phi(lower_blk, 2, in, ia32_mode_E);
501 /* fix Phi links for next part_block() */
503 add_Block_phi(lower_blk, int_phi);
505 add_Block_phi(lower_blk, flt_phi);
507 float_to_ll = new_bd_ia32_l_FloattoLL(dbg, lower_blk, flt_phi);
509 l_res = new_r_Proj(float_to_ll, l_res_mode,
510 pn_ia32_l_FloattoLL_res_low);
511 h_res = new_r_Proj(float_to_ll, h_res_mode,
512 pn_ia32_l_FloattoLL_res_high);
514 h_res = new_rd_Add(dbg, lower_blk, h_res, int_phi, h_res_mode);
516 /* move the call and its Proj's to the lower block */
517 set_nodes_block(call, lower_blk);
519 for (proj = (ir_node*)get_irn_link(call); proj != NULL;
520 proj = (ir_node*)get_irn_link(proj)) {
521 set_nodes_block(proj, lower_blk);
526 resolve_call(call, l_res, h_res, irg, block);
528 ir_node *ll_to_float;
530 /* We have a Conv long long -> float here */
531 ir_node *a_l = params[BINOP_Left_Low];
532 ir_node *a_h = params[BINOP_Left_High];
533 ir_mode *fres_mode = get_type_mode(get_method_res_type(method, 0));
535 assert(! mode_is_float(get_irn_mode(a_l))
536 && ! mode_is_float(get_irn_mode(a_h)));
538 ll_to_float = new_bd_ia32_l_LLtoFloat(dbg, block, a_h, a_l, fres_mode);
541 resolve_call(call, ll_to_float, NULL, irg, block);
543 panic("unexpected Conv call %+F", call);
549 /* Ia32 implementation of intrinsic mapping. */
550 ir_entity *ia32_create_intrinsic_fkt(ir_type *method, const ir_op *op,
551 const ir_mode *imode, const ir_mode *omode,
555 ir_entity **ent = NULL;
556 i_mapper_func mapper;
559 intrinsics = NEW_ARR_F(i_record, 0);
561 switch (get_op_code(op)) {
563 ent = &i_ents[iro_Add];
567 ent = &i_ents[iro_Sub];
571 ent = &i_ents[iro_Mul];
575 ent = &i_ents[iro_Minus];
579 ent = &i_ents[iro_Div];
583 ent = &i_ents[iro_Mod];
587 ent = &i_ents[iro_Conv];
591 fprintf(stderr, "FIXME: unhandled op for ia32 intrinsic function %s\n", get_id_str(op->name));
592 return def_create_intrinsic_fkt(method, op, imode, omode, context);
596 ident *id = id_mangle(ID("L"), get_op_ident(op));
597 *ent = new_entity(get_glob_type(), id, method);
598 set_entity_visibility(*ent, ir_visibility_private);
601 elt.i_call.kind = INTRINSIC_CALL;
602 elt.i_call.i_ent = *ent;
603 elt.i_call.i_mapper = mapper;
604 elt.i_call.ctx = context;
605 elt.i_call.link = NULL;
607 ARR_APP1(i_record, intrinsics, elt);