2 * Copyright (C) 1995-2010 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 The codegenerator (transform FIRM into arm FIRM)
23 * @author Matthias Braun, Oliver Richter, Tobias Gneist, Michael Beck
28 #include "irgraph_t.h"
43 #include "betranshlp.h"
44 #include "beabihelper.h"
47 #include "bearch_arm_t.h"
48 #include "arm_nodes_attr.h"
49 #include "arm_transform.h"
50 #include "arm_optimize.h"
51 #include "arm_new_nodes.h"
52 #include "arm_map_regs.h"
53 #include "arm_cconv.h"
55 #include "gen_arm_regalloc_if.h"
59 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
61 static const arch_register_t *sp_reg = &arm_registers[REG_SP];
62 static ir_mode *mode_gp;
63 static ir_mode *mode_fp;
64 static beabi_helper_env_t *abihelper;
65 static be_stackorder_t *stackorder;
66 static calling_convention_t *cconv = NULL;
67 static arm_isa_t *isa;
69 static pmap *node_to_stack;
71 static const arch_register_t *const callee_saves[] = {
72 &arm_registers[REG_R4],
73 &arm_registers[REG_R5],
74 &arm_registers[REG_R6],
75 &arm_registers[REG_R7],
76 &arm_registers[REG_R8],
77 &arm_registers[REG_R9],
78 &arm_registers[REG_R10],
79 &arm_registers[REG_R11],
80 &arm_registers[REG_LR],
83 static const arch_register_t *const caller_saves[] = {
84 &arm_registers[REG_R0],
85 &arm_registers[REG_R1],
86 &arm_registers[REG_R2],
87 &arm_registers[REG_R3],
88 &arm_registers[REG_LR],
90 &arm_registers[REG_F0],
91 &arm_registers[REG_F1],
92 &arm_registers[REG_F2],
93 &arm_registers[REG_F3],
94 &arm_registers[REG_F4],
95 &arm_registers[REG_F5],
96 &arm_registers[REG_F6],
97 &arm_registers[REG_F7],
100 static bool mode_needs_gp_reg(ir_mode *mode)
102 return mode_is_int(mode) || mode_is_reference(mode);
106 * create firm graph for a constant
108 static ir_node *create_const_graph_value(dbg_info *dbgi, ir_node *block,
115 /* We only have 8 bit immediates. So we possibly have to combine several
116 * operations to construct the desired value.
118 * we can either create the value by adding bits to 0 or by removing bits
119 * from an register with all bits set. Try which alternative needs fewer
121 arm_gen_vals_from_word(value, &v);
122 arm_gen_vals_from_word(~value, &vn);
124 if (vn.ops < v.ops) {
126 result = new_bd_arm_Mvn_imm(dbgi, block, vn.values[0], vn.rors[0]);
128 for (cnt = 1; cnt < vn.ops; ++cnt) {
129 result = new_bd_arm_Bic_imm(dbgi, block, result,
130 vn.values[cnt], vn.rors[cnt]);
134 result = new_bd_arm_Mov_imm(dbgi, block, v.values[0], v.rors[0]);
136 for (cnt = 1; cnt < v.ops; ++cnt) {
137 result = new_bd_arm_Or_imm(dbgi, block, result,
138 v.values[cnt], v.rors[cnt]);
145 * Create a DAG constructing a given Const.
147 * @param irn a Firm const
149 static ir_node *create_const_graph(ir_node *irn, ir_node *block)
151 ir_tarval *tv = get_Const_tarval(irn);
152 ir_mode *mode = get_tarval_mode(tv);
155 if (mode_is_reference(mode)) {
156 /* ARM is 32bit, so we can safely convert a reference tarval into Iu */
157 assert(get_mode_size_bits(mode) == get_mode_size_bits(mode_Iu));
158 tv = tarval_convert_to(tv, mode_Iu);
160 value = get_tarval_long(tv);
161 return create_const_graph_value(get_irn_dbg_info(irn), block, value);
165 * Create an And that will zero out upper bits.
167 * @param dbgi debug info
168 * @param block the basic block
169 * @param op the original node
170 * param src_bits number of lower bits that will remain
172 static ir_node *gen_zero_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
176 return new_bd_arm_And_imm(dbgi, block, op, 0xFF, 0);
177 } else if (src_bits == 16) {
178 ir_node *lshift = new_bd_arm_Mov_reg_shift_imm(dbgi, block, op, ARM_SHF_LSL_IMM, 16);
179 ir_node *rshift = new_bd_arm_Mov_reg_shift_imm(dbgi, block, lshift, ARM_SHF_LSR_IMM, 16);
182 panic("zero extension only supported for 8 and 16 bits");
187 * Generate code for a sign extension.
189 static ir_node *gen_sign_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
192 int shift_width = 32 - src_bits;
193 ir_node *lshift_node = new_bd_arm_Mov_reg_shift_imm(dbgi, block, op, ARM_SHF_LSL_IMM, shift_width);
194 ir_node *rshift_node = new_bd_arm_Mov_reg_shift_imm(dbgi, block, lshift_node, ARM_SHF_ASR_IMM, shift_width);
198 static ir_node *gen_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
201 int bits = get_mode_size_bits(orig_mode);
205 if (mode_is_signed(orig_mode)) {
206 return gen_sign_extension(dbgi, block, op, bits);
208 return gen_zero_extension(dbgi, block, op, bits);
213 * returns true if it is assured, that the upper bits of a node are "clean"
214 * which means for a 16 or 8 bit value, that the upper bits in the register
215 * are 0 for unsigned and a copy of the last significant bit for signed
218 static bool upper_bits_clean(ir_node *transformed_node, ir_mode *mode)
220 (void) transformed_node;
227 * Transforms a Conv node.
229 * @return The created ia32 Conv node
231 static ir_node *gen_Conv(ir_node *node)
233 ir_node *block = be_transform_node(get_nodes_block(node));
234 ir_node *op = get_Conv_op(node);
235 ir_node *new_op = be_transform_node(op);
236 ir_mode *src_mode = get_irn_mode(op);
237 ir_mode *dst_mode = get_irn_mode(node);
238 dbg_info *dbg = get_irn_dbg_info(node);
240 if (src_mode == dst_mode)
243 if (mode_is_float(src_mode) || mode_is_float(dst_mode)) {
245 if (mode_is_float(src_mode)) {
246 if (mode_is_float(dst_mode)) {
247 /* from float to float */
248 return new_bd_arm_Mvf(dbg, block, new_op, dst_mode);
250 /* from float to int */
254 /* from int to float */
255 if (!mode_is_signed(src_mode)) {
258 return new_bd_arm_FltX(dbg, block, new_op, dst_mode);
261 } else if (USE_VFP(isa)) {
262 panic("VFP not supported yet");
264 panic("Softfloat not supported yet");
266 } else { /* complete in gp registers */
267 int src_bits = get_mode_size_bits(src_mode);
268 int dst_bits = get_mode_size_bits(dst_mode);
272 if (src_bits == dst_bits) {
273 /* kill unnecessary conv */
277 if (src_bits < dst_bits) {
285 if (upper_bits_clean(new_op, min_mode)) {
289 if (mode_is_signed(min_mode)) {
290 return gen_sign_extension(dbg, block, new_op, min_bits);
292 return gen_zero_extension(dbg, block, new_op, min_bits);
302 static bool try_encode_as_immediate(const ir_node *node, arm_immediate_t *res)
304 unsigned val, low_pos, high_pos;
309 val = get_tarval_long(get_Const_tarval(node));
321 /* arm allows to use to rotate an 8bit immediate value by a multiple of 2
323 So we determine the smallest even position with a bit set
324 and the highest even position with no bit set anymore.
325 If the difference between these 2 is <= 8, then we can encode the value
328 low_pos = ntz(val) & ~1u;
329 high_pos = (32-nlz(val)+1) & ~1u;
331 if (high_pos - low_pos <= 8) {
332 res->imm_8 = val >> low_pos;
333 res->rot = 32 - low_pos;
338 res->rot = 34 - high_pos;
339 val = val >> (32-res->rot) | val << (res->rot);
349 static bool is_downconv(const ir_node *node)
357 /* we only want to skip the conv when we're the only user
358 * (not optimal but for now...)
360 if (get_irn_n_edges(node) > 1)
363 src_mode = get_irn_mode(get_Conv_op(node));
364 dest_mode = get_irn_mode(node);
366 mode_needs_gp_reg(src_mode) &&
367 mode_needs_gp_reg(dest_mode) &&
368 get_mode_size_bits(dest_mode) <= get_mode_size_bits(src_mode);
371 static ir_node *arm_skip_downconv(ir_node *node)
373 while (is_downconv(node))
374 node = get_Conv_op(node);
380 MATCH_COMMUTATIVE = 1 << 0, /**< commutative node */
381 MATCH_REVERSE = 1 << 1, /**< support reverse opcode */
382 MATCH_SIZE_NEUTRAL = 1 << 2,
383 MATCH_SKIP_NOT = 1 << 3, /**< skip Not on ONE input */
385 ENUM_BITSET(match_flags_t)
388 * possible binop constructors.
390 typedef struct arm_binop_factory_t {
391 /** normal reg op reg operation. */
392 ir_node *(*new_binop_reg)(dbg_info *dbgi, ir_node *block, ir_node *op1, ir_node *op2);
393 /** normal reg op imm operation. */
394 ir_node *(*new_binop_imm)(dbg_info *dbgi, ir_node *block, ir_node *op1, unsigned char imm8, unsigned char imm_rot);
395 /** barrel shifter reg op (reg shift reg operation. */
396 ir_node *(*new_binop_reg_shift_reg)(dbg_info *dbgi, ir_node *block, ir_node *left, ir_node *right, ir_node *shift, arm_shift_modifier_t shift_modifier);
397 /** barrel shifter reg op (reg shift imm operation. */
398 ir_node *(*new_binop_reg_shift_imm)(dbg_info *dbgi, ir_node *block, ir_node *left, ir_node *right, arm_shift_modifier_t shift_modifier, unsigned shift_immediate);
399 } arm_binop_factory_t;
401 static ir_node *gen_int_binop(ir_node *node, match_flags_t flags,
402 const arm_binop_factory_t *factory)
404 ir_node *block = be_transform_node(get_nodes_block(node));
405 ir_node *op1 = get_binop_left(node);
407 ir_node *op2 = get_binop_right(node);
409 dbg_info *dbgi = get_irn_dbg_info(node);
412 if (flags & MATCH_SKIP_NOT) {
414 op1 = get_Not_op(op1);
415 else if (is_Not(op2))
416 op2 = get_Not_op(op2);
418 panic("cannot execute MATCH_SKIP_NOT");
420 if (flags & MATCH_SIZE_NEUTRAL) {
421 op1 = arm_skip_downconv(op1);
422 op2 = arm_skip_downconv(op2);
424 assert(get_mode_size_bits(get_irn_mode(node)) == 32);
427 if (try_encode_as_immediate(op2, &imm)) {
428 new_op1 = be_transform_node(op1);
429 return factory->new_binop_imm(dbgi, block, new_op1, imm.imm_8, imm.rot);
431 new_op2 = be_transform_node(op2);
432 if ((flags & (MATCH_COMMUTATIVE|MATCH_REVERSE)) && try_encode_as_immediate(op1, &imm)) {
433 if (flags & MATCH_REVERSE)
434 return factory[1].new_binop_imm(dbgi, block, new_op2, imm.imm_8, imm.rot);
436 return factory[0].new_binop_imm(dbgi, block, new_op2, imm.imm_8, imm.rot);
438 new_op1 = be_transform_node(op1);
440 /* check if we can fold in a Mov */
441 if (is_arm_Mov(new_op2)) {
442 const arm_shifter_operand_t *attr = get_arm_shifter_operand_attr_const(new_op2);
444 switch (attr->shift_modifier) {
446 case ARM_SHF_ASR_IMM:
447 case ARM_SHF_LSL_IMM:
448 case ARM_SHF_LSR_IMM:
449 case ARM_SHF_ROR_IMM:
450 if (factory->new_binop_reg_shift_imm) {
451 ir_node *mov_op = get_irn_n(new_op2, 0);
452 return factory->new_binop_reg_shift_imm(dbgi, block, new_op1, mov_op,
453 attr->shift_modifier, attr->shift_immediate);
457 case ARM_SHF_ASR_REG:
458 case ARM_SHF_LSL_REG:
459 case ARM_SHF_LSR_REG:
460 case ARM_SHF_ROR_REG:
461 if (factory->new_binop_reg_shift_reg) {
462 ir_node *mov_op = get_irn_n(new_op2, 0);
463 ir_node *mov_sft = get_irn_n(new_op2, 1);
464 return factory->new_binop_reg_shift_reg(dbgi, block, new_op1, mov_op, mov_sft,
465 attr->shift_modifier);
471 case ARM_SHF_INVALID:
472 panic("invalid shift");
475 if ((flags & (MATCH_COMMUTATIVE|MATCH_REVERSE)) && is_arm_Mov(new_op1)) {
476 const arm_shifter_operand_t *attr = get_arm_shifter_operand_attr_const(new_op1);
477 int idx = flags & MATCH_REVERSE ? 1 : 0;
479 switch (attr->shift_modifier) {
480 ir_node *mov_op, *mov_sft;
483 case ARM_SHF_ASR_IMM:
484 case ARM_SHF_LSL_IMM:
485 case ARM_SHF_LSR_IMM:
486 case ARM_SHF_ROR_IMM:
487 if (factory[idx].new_binop_reg_shift_imm) {
488 mov_op = get_irn_n(new_op1, 0);
489 return factory[idx].new_binop_reg_shift_imm(dbgi, block, new_op2, mov_op,
490 attr->shift_modifier, attr->shift_immediate);
494 case ARM_SHF_ASR_REG:
495 case ARM_SHF_LSL_REG:
496 case ARM_SHF_LSR_REG:
497 case ARM_SHF_ROR_REG:
498 if (factory[idx].new_binop_reg_shift_reg) {
499 mov_op = get_irn_n(new_op1, 0);
500 mov_sft = get_irn_n(new_op1, 1);
501 return factory[idx].new_binop_reg_shift_reg(dbgi, block, new_op2, mov_op, mov_sft,
502 attr->shift_modifier);
509 case ARM_SHF_INVALID:
510 panic("invalid shift");
513 return factory->new_binop_reg(dbgi, block, new_op1, new_op2);
517 * Creates an ARM Add.
519 * @return the created arm Add node
521 static ir_node *gen_Add(ir_node *node)
523 static const arm_binop_factory_t add_factory = {
526 new_bd_arm_Add_reg_shift_reg,
527 new_bd_arm_Add_reg_shift_imm
530 ir_mode *mode = get_irn_mode(node);
532 if (mode_is_float(mode)) {
533 ir_node *block = be_transform_node(get_nodes_block(node));
534 ir_node *op1 = get_Add_left(node);
535 ir_node *op2 = get_Add_right(node);
536 dbg_info *dbgi = get_irn_dbg_info(node);
537 ir_node *new_op1 = be_transform_node(op1);
538 ir_node *new_op2 = be_transform_node(op2);
540 return new_bd_arm_Adf(dbgi, block, new_op1, new_op2, mode);
541 } else if (USE_VFP(isa)) {
542 panic("VFP not supported yet");
544 panic("Softfloat not supported yet");
549 if (is_arm_Mul(new_op1) && get_irn_n_edges(op1) == 1) {
551 new_op2 = get_irn_n(new_op1, 1);
552 new_op1 = get_irn_n(new_op1, 0);
554 return new_bd_arm_Mla(dbgi, block, new_op1, new_op2, new_op3);
556 if (is_arm_Mul(new_op2) && get_irn_n_edges(op2) == 1) {
558 new_op1 = get_irn_n(new_op2, 0);
559 new_op2 = get_irn_n(new_op2, 1);
561 return new_bd_arm_Mla(dbgi, block, new_op1, new_op2, new_op3);
565 return gen_int_binop(node, MATCH_COMMUTATIVE | MATCH_SIZE_NEUTRAL, &add_factory);
570 * Creates an ARM Mul.
572 * @return the created arm Mul node
574 static ir_node *gen_Mul(ir_node *node)
576 ir_node *block = be_transform_node(get_nodes_block(node));
577 ir_node *op1 = get_Mul_left(node);
578 ir_node *new_op1 = be_transform_node(op1);
579 ir_node *op2 = get_Mul_right(node);
580 ir_node *new_op2 = be_transform_node(op2);
581 ir_mode *mode = get_irn_mode(node);
582 dbg_info *dbg = get_irn_dbg_info(node);
584 if (mode_is_float(mode)) {
586 return new_bd_arm_Muf(dbg, block, new_op1, new_op2, mode);
587 } else if (USE_VFP(isa)) {
588 panic("VFP not supported yet");
590 panic("Softfloat not supported yet");
593 assert(mode_is_data(mode));
594 return new_bd_arm_Mul(dbg, block, new_op1, new_op2);
597 static ir_node *gen_Div(ir_node *node)
599 ir_node *block = be_transform_node(get_nodes_block(node));
600 ir_node *op1 = get_Div_left(node);
601 ir_node *new_op1 = be_transform_node(op1);
602 ir_node *op2 = get_Div_right(node);
603 ir_node *new_op2 = be_transform_node(op2);
604 ir_mode *mode = get_Div_resmode(node);
605 dbg_info *dbg = get_irn_dbg_info(node);
607 /* integer division should be replaced by builtin call */
608 assert(mode_is_float(mode));
611 return new_bd_arm_Dvf(dbg, block, new_op1, new_op2, mode);
612 } else if (USE_VFP(isa)) {
613 panic("VFP not supported yet");
615 panic("Softfloat not supported yet");
619 static ir_node *gen_And(ir_node *node)
621 static const arm_binop_factory_t and_factory = {
624 new_bd_arm_And_reg_shift_reg,
625 new_bd_arm_And_reg_shift_imm
627 static const arm_binop_factory_t bic_factory = {
630 new_bd_arm_Bic_reg_shift_reg,
631 new_bd_arm_Bic_reg_shift_imm
634 /* check for and not */
635 ir_node *left = get_And_left(node);
636 ir_node *right = get_And_right(node);
638 if (is_Not(left) || is_Not(right)) {
639 return gen_int_binop(node, MATCH_COMMUTATIVE | MATCH_SIZE_NEUTRAL | MATCH_SKIP_NOT,
643 return gen_int_binop(node, MATCH_COMMUTATIVE | MATCH_SIZE_NEUTRAL, &and_factory);
646 static ir_node *gen_Or(ir_node *node)
648 static const arm_binop_factory_t or_factory = {
651 new_bd_arm_Or_reg_shift_reg,
652 new_bd_arm_Or_reg_shift_imm
655 return gen_int_binop(node, MATCH_COMMUTATIVE | MATCH_SIZE_NEUTRAL, &or_factory);
658 static ir_node *gen_Eor(ir_node *node)
660 static const arm_binop_factory_t eor_factory = {
663 new_bd_arm_Eor_reg_shift_reg,
664 new_bd_arm_Eor_reg_shift_imm
667 return gen_int_binop(node, MATCH_COMMUTATIVE | MATCH_SIZE_NEUTRAL, &eor_factory);
670 static ir_node *gen_Sub(ir_node *node)
672 static const arm_binop_factory_t sub_rsb_factory[2] = {
676 new_bd_arm_Sub_reg_shift_reg,
677 new_bd_arm_Sub_reg_shift_imm
682 new_bd_arm_Rsb_reg_shift_reg,
683 new_bd_arm_Rsb_reg_shift_imm
687 ir_node *block = be_transform_node(get_nodes_block(node));
688 ir_node *op1 = get_Sub_left(node);
689 ir_node *new_op1 = be_transform_node(op1);
690 ir_node *op2 = get_Sub_right(node);
691 ir_node *new_op2 = be_transform_node(op2);
692 ir_mode *mode = get_irn_mode(node);
693 dbg_info *dbgi = get_irn_dbg_info(node);
695 if (mode_is_float(mode)) {
697 return new_bd_arm_Suf(dbgi, block, new_op1, new_op2, mode);
698 } else if (USE_VFP(isa)) {
699 panic("VFP not supported yet");
701 panic("Softfloat not supported yet");
704 return gen_int_binop(node, MATCH_SIZE_NEUTRAL | MATCH_REVERSE, sub_rsb_factory);
709 * Checks if a given value can be used as an immediate for the given
712 static bool can_use_shift_constant(unsigned int val,
713 arm_shift_modifier_t modifier)
717 if (val == 32 && modifier != ARM_SHF_LSL_REG && modifier != ARM_SHF_ROR_REG)
723 * generate an ARM shift instruction.
725 * @param node the node
726 * @param flags matching flags
727 * @param shift_modifier initial encoding of the desired shift operation
729 static ir_node *make_shift(ir_node *node, match_flags_t flags,
730 arm_shift_modifier_t shift_modifier)
732 ir_node *block = be_transform_node(get_nodes_block(node));
733 ir_node *op1 = get_binop_left(node);
734 ir_node *op2 = get_binop_right(node);
735 dbg_info *dbgi = get_irn_dbg_info(node);
736 ir_mode *mode = get_irn_mode(node);
740 if (get_mode_modulo_shift(mode) != 32)
741 panic("modulo shift!=32 not supported");
743 if (flags & MATCH_SIZE_NEUTRAL) {
744 op1 = arm_skip_downconv(op1);
745 op2 = arm_skip_downconv(op2);
748 new_op1 = be_transform_node(op1);
750 ir_tarval *tv = get_Const_tarval(op2);
751 unsigned int val = get_tarval_long(tv);
752 assert(tarval_is_long(tv));
753 if (can_use_shift_constant(val, shift_modifier)) {
754 switch (shift_modifier) {
755 case ARM_SHF_LSL_REG: shift_modifier = ARM_SHF_LSL_IMM; break;
756 case ARM_SHF_LSR_REG: shift_modifier = ARM_SHF_LSR_IMM; break;
757 case ARM_SHF_ASR_REG: shift_modifier = ARM_SHF_ASR_IMM; break;
758 case ARM_SHF_ROR_REG: shift_modifier = ARM_SHF_ROR_IMM; break;
759 default: panic("unexpected shift modifier");
761 return new_bd_arm_Mov_reg_shift_imm(dbgi, block, new_op1,
762 shift_modifier, val);
766 new_op2 = be_transform_node(op2);
767 return new_bd_arm_Mov_reg_shift_reg(dbgi, block, new_op1, new_op2,
771 static ir_node *gen_Shl(ir_node *node)
773 return make_shift(node, MATCH_SIZE_NEUTRAL, ARM_SHF_LSL_REG);
776 static ir_node *gen_Shr(ir_node *node)
778 return make_shift(node, MATCH_NONE, ARM_SHF_LSR_REG);
781 static ir_node *gen_Shrs(ir_node *node)
783 return make_shift(node, MATCH_NONE, ARM_SHF_ASR_REG);
786 static ir_node *gen_Ror(ir_node *node, ir_node *op1, ir_node *op2)
788 ir_node *block = be_transform_node(get_nodes_block(node));
789 ir_node *new_op1 = be_transform_node(op1);
790 dbg_info *dbgi = get_irn_dbg_info(node);
791 ir_node *new_op2 = be_transform_node(op2);
793 return new_bd_arm_Mov_reg_shift_reg(dbgi, block, new_op1, new_op2,
797 static ir_node *gen_Rol(ir_node *node, ir_node *op1, ir_node *op2)
799 ir_node *block = be_transform_node(get_nodes_block(node));
800 ir_node *new_op1 = be_transform_node(op1);
801 dbg_info *dbgi = get_irn_dbg_info(node);
802 ir_node *new_op2 = be_transform_node(op2);
804 /* Note: there is no Rol on arm, we have to use Ror */
805 new_op2 = new_bd_arm_Rsb_imm(dbgi, block, new_op2, 32, 0);
806 return new_bd_arm_Mov_reg_shift_reg(dbgi, block, new_op1, new_op2,
810 static ir_node *gen_Rotl(ir_node *node)
812 ir_node *rotate = NULL;
813 ir_node *op1 = get_Rotl_left(node);
814 ir_node *op2 = get_Rotl_right(node);
816 /* Firm has only RotL, so we are looking for a right (op2)
817 operand "-e+mode_size_bits" (it's an already modified "mode_size_bits-e",
818 that means we can create a RotR. */
821 ir_node *right = get_Add_right(op2);
822 if (is_Const(right)) {
823 ir_tarval *tv = get_Const_tarval(right);
824 ir_mode *mode = get_irn_mode(node);
825 long bits = get_mode_size_bits(mode);
826 ir_node *left = get_Add_left(op2);
828 if (is_Minus(left) &&
829 tarval_is_long(tv) &&
830 get_tarval_long(tv) == bits &&
832 rotate = gen_Ror(node, op1, get_Minus_op(left));
834 } else if (is_Sub(op2)) {
835 ir_node *left = get_Sub_left(op2);
836 if (is_Const(left)) {
837 ir_tarval *tv = get_Const_tarval(left);
838 ir_mode *mode = get_irn_mode(node);
839 long bits = get_mode_size_bits(mode);
840 ir_node *right = get_Sub_right(op2);
842 if (tarval_is_long(tv) &&
843 get_tarval_long(tv) == bits &&
845 rotate = gen_Ror(node, op1, right);
847 } else if (is_Const(op2)) {
848 ir_tarval *tv = get_Const_tarval(op2);
849 ir_mode *mode = get_irn_mode(node);
850 long bits = get_mode_size_bits(mode);
852 if (tarval_is_long(tv) && bits == 32) {
853 ir_node *block = be_transform_node(get_nodes_block(node));
854 ir_node *new_op1 = be_transform_node(op1);
855 dbg_info *dbgi = get_irn_dbg_info(node);
857 bits = (bits - get_tarval_long(tv)) & 31;
858 rotate = new_bd_arm_Mov_reg_shift_imm(dbgi, block, new_op1, ARM_SHF_ROR_IMM, bits);
862 if (rotate == NULL) {
863 rotate = gen_Rol(node, op1, op2);
869 static ir_node *gen_Not(ir_node *node)
871 ir_node *block = be_transform_node(get_nodes_block(node));
872 ir_node *op = get_Not_op(node);
873 ir_node *new_op = be_transform_node(op);
874 dbg_info *dbgi = get_irn_dbg_info(node);
876 /* check if we can fold in a Mov */
877 if (is_arm_Mov(new_op)) {
878 const arm_shifter_operand_t *attr = get_arm_shifter_operand_attr_const(new_op);
880 switch (attr->shift_modifier) {
881 ir_node *mov_op, *mov_sft;
884 case ARM_SHF_ASR_IMM:
885 case ARM_SHF_LSL_IMM:
886 case ARM_SHF_LSR_IMM:
887 case ARM_SHF_ROR_IMM:
888 mov_op = get_irn_n(new_op, 0);
889 return new_bd_arm_Mvn_reg_shift_imm(dbgi, block, mov_op,
890 attr->shift_modifier, attr->shift_immediate);
892 case ARM_SHF_ASR_REG:
893 case ARM_SHF_LSL_REG:
894 case ARM_SHF_LSR_REG:
895 case ARM_SHF_ROR_REG:
896 mov_op = get_irn_n(new_op, 0);
897 mov_sft = get_irn_n(new_op, 1);
898 return new_bd_arm_Mvn_reg_shift_reg(dbgi, block, mov_op, mov_sft,
899 attr->shift_modifier);
904 case ARM_SHF_INVALID:
905 panic("invalid shift");
909 return new_bd_arm_Mvn_reg(dbgi, block, new_op);
912 static ir_node *gen_Minus(ir_node *node)
914 ir_node *block = be_transform_node(get_nodes_block(node));
915 ir_node *op = get_Minus_op(node);
916 ir_node *new_op = be_transform_node(op);
917 dbg_info *dbgi = get_irn_dbg_info(node);
918 ir_mode *mode = get_irn_mode(node);
920 if (mode_is_float(mode)) {
922 return new_bd_arm_Mvf(dbgi, block, op, mode);
923 } else if (USE_VFP(isa)) {
924 panic("VFP not supported yet");
926 panic("Softfloat not supported yet");
929 assert(mode_is_data(mode));
930 return new_bd_arm_Rsb_imm(dbgi, block, new_op, 0, 0);
933 static ir_node *gen_Load(ir_node *node)
935 ir_node *block = be_transform_node(get_nodes_block(node));
936 ir_node *ptr = get_Load_ptr(node);
937 ir_node *new_ptr = be_transform_node(ptr);
938 ir_node *mem = get_Load_mem(node);
939 ir_node *new_mem = be_transform_node(mem);
940 ir_mode *mode = get_Load_mode(node);
941 dbg_info *dbgi = get_irn_dbg_info(node);
942 ir_node *new_load = NULL;
944 if (get_Load_unaligned(node) == align_non_aligned)
945 panic("unaligned Loads not supported yet");
947 if (mode_is_float(mode)) {
949 new_load = new_bd_arm_Ldf(dbgi, block, new_ptr, new_mem, mode,
951 } else if (USE_VFP(isa)) {
952 panic("VFP not supported yet");
954 panic("Softfloat not supported yet");
957 assert(mode_is_data(mode) && "unsupported mode for Load");
959 new_load = new_bd_arm_Ldr(dbgi, block, new_ptr, new_mem, mode, NULL, 0, 0, false);
961 set_irn_pinned(new_load, get_irn_pinned(node));
963 /* check for special case: the loaded value might not be used */
964 if (be_get_Proj_for_pn(node, pn_Load_res) == NULL) {
965 /* add a result proj and a Keep to produce a pseudo use */
966 ir_node *proj = new_r_Proj(new_load, mode_Iu, pn_arm_Ldr_res);
967 be_new_Keep(block, 1, &proj);
973 static ir_node *gen_Store(ir_node *node)
975 ir_node *block = be_transform_node(get_nodes_block(node));
976 ir_node *ptr = get_Store_ptr(node);
977 ir_node *new_ptr = be_transform_node(ptr);
978 ir_node *mem = get_Store_mem(node);
979 ir_node *new_mem = be_transform_node(mem);
980 ir_node *val = get_Store_value(node);
981 ir_node *new_val = be_transform_node(val);
982 ir_mode *mode = get_irn_mode(val);
983 dbg_info *dbgi = get_irn_dbg_info(node);
984 ir_node *new_store = NULL;
986 if (get_Store_unaligned(node) == align_non_aligned)
987 panic("unaligned Stores not supported yet");
989 if (mode_is_float(mode)) {
991 new_store = new_bd_arm_Stf(dbgi, block, new_ptr, new_val,
992 new_mem, mode, NULL, 0, 0, false);
993 } else if (USE_VFP(isa)) {
994 panic("VFP not supported yet");
996 panic("Softfloat not supported yet");
999 assert(mode_is_data(mode) && "unsupported mode for Store");
1000 new_store = new_bd_arm_Str(dbgi, block, new_ptr, new_val, new_mem, mode,
1003 set_irn_pinned(new_store, get_irn_pinned(node));
1007 static ir_node *gen_Jmp(ir_node *node)
1009 ir_node *block = get_nodes_block(node);
1010 ir_node *new_block = be_transform_node(block);
1011 dbg_info *dbgi = get_irn_dbg_info(node);
1013 return new_bd_arm_Jmp(dbgi, new_block);
1016 static ir_node *gen_Switch(ir_node *node)
1018 ir_graph *irg = get_irn_irg(node);
1019 ir_node *block = be_transform_node(get_nodes_block(node));
1020 ir_node *selector = get_Switch_selector(node);
1021 dbg_info *dbgi = get_irn_dbg_info(node);
1022 ir_node *new_op = be_transform_node(selector);
1023 ir_mode *mode = get_irn_mode(selector);
1024 const ir_switch_table *table = get_Switch_table(node);
1025 unsigned n_outs = get_Switch_n_outs(node);
1027 table = ir_switch_table_duplicate(irg, table);
1029 /* switch with smaller modes not implemented yet */
1030 assert(get_mode_size_bits(mode) == 32);
1032 return new_bd_arm_SwitchJmp(dbgi, block, new_op, n_outs, table);
1035 static ir_node *gen_Cmp(ir_node *node)
1037 ir_node *block = be_transform_node(get_nodes_block(node));
1038 ir_node *op1 = get_Cmp_left(node);
1039 ir_node *op2 = get_Cmp_right(node);
1040 ir_mode *cmp_mode = get_irn_mode(op1);
1041 dbg_info *dbgi = get_irn_dbg_info(node);
1046 if (mode_is_float(cmp_mode)) {
1047 /* TODO: this is broken... */
1048 new_op1 = be_transform_node(op1);
1049 new_op2 = be_transform_node(op2);
1051 return new_bd_arm_Cmfe(dbgi, block, new_op1, new_op2, false);
1054 assert(get_irn_mode(op2) == cmp_mode);
1055 is_unsigned = !mode_is_signed(cmp_mode);
1057 /* integer compare, TODO: use shifter_op in all its combinations */
1058 new_op1 = be_transform_node(op1);
1059 new_op1 = gen_extension(dbgi, block, new_op1, cmp_mode);
1060 new_op2 = be_transform_node(op2);
1061 new_op2 = gen_extension(dbgi, block, new_op2, cmp_mode);
1062 return new_bd_arm_Cmp_reg(dbgi, block, new_op1, new_op2, false,
1066 static ir_node *gen_Cond(ir_node *node)
1068 ir_node *selector = get_Cond_selector(node);
1069 ir_relation relation;
1074 assert(is_Cmp(selector));
1076 block = be_transform_node(get_nodes_block(node));
1077 dbgi = get_irn_dbg_info(node);
1078 flag_node = be_transform_node(selector);
1079 relation = get_Cmp_relation(selector);
1081 return new_bd_arm_B(dbgi, block, flag_node, relation);
1087 FPA_IMM_MAX = FPA_IMM_DOUBLE
1090 static ir_tarval *fpa_imm[FPA_IMM_MAX + 1][fpa_max];
1094 * Check, if a floating point tarval is an fpa immediate, i.e.
1095 * one of 0, 1, 2, 3, 4, 5, 10, or 0.5.
1097 static int is_fpa_immediate(tarval *tv)
1099 ir_mode *mode = get_tarval_mode(tv);
1102 switch (get_mode_size_bits(mode)) {
1111 if (tarval_is_negative(tv)) {
1112 tv = tarval_neg(tv);
1116 for (j = 0; j < fpa_max; ++j) {
1117 if (tv == fpa_imm[i][j])
1124 static ir_node *gen_Const(ir_node *node)
1126 ir_node *block = be_transform_node(get_nodes_block(node));
1127 ir_mode *mode = get_irn_mode(node);
1128 dbg_info *dbg = get_irn_dbg_info(node);
1130 if (mode_is_float(mode)) {
1132 ir_tarval *tv = get_Const_tarval(node);
1133 node = new_bd_arm_fConst(dbg, block, tv);
1135 } else if (USE_VFP(isa)) {
1136 panic("VFP not supported yet");
1138 panic("Softfloat not supported yet");
1141 return create_const_graph(node, block);
1144 static ir_node *gen_SymConst(ir_node *node)
1146 ir_node *block = be_transform_node(get_nodes_block(node));
1147 ir_entity *entity = get_SymConst_entity(node);
1148 dbg_info *dbgi = get_irn_dbg_info(node);
1151 new_node = new_bd_arm_SymConst(dbgi, block, entity, 0);
1155 static ir_node *ints_to_double(dbg_info *dbgi, ir_node *block, ir_node *node0,
1158 /* the good way to do this would be to use the stm (store multiple)
1159 * instructions, since our input is nearly always 2 consecutive 32bit
1161 ir_graph *irg = current_ir_graph;
1162 ir_node *stack = get_irg_frame(irg);
1163 ir_node *nomem = get_irg_no_mem(irg);
1164 ir_node *str0 = new_bd_arm_Str(dbgi, block, stack, node0, nomem, mode_gp,
1166 ir_node *str1 = new_bd_arm_Str(dbgi, block, stack, node1, nomem, mode_gp,
1168 ir_node *in[2] = { str0, str1 };
1169 ir_node *sync = new_r_Sync(block, 2, in);
1171 set_irn_pinned(str0, op_pin_state_floats);
1172 set_irn_pinned(str1, op_pin_state_floats);
1174 ldf = new_bd_arm_Ldf(dbgi, block, stack, sync, mode_D, NULL, 0, 0, true);
1175 set_irn_pinned(ldf, op_pin_state_floats);
1177 return new_r_Proj(ldf, mode_fp, pn_arm_Ldf_res);
1180 static ir_node *int_to_float(dbg_info *dbgi, ir_node *block, ir_node *node)
1182 ir_graph *irg = current_ir_graph;
1183 ir_node *stack = get_irg_frame(irg);
1184 ir_node *nomem = get_irg_no_mem(irg);
1185 ir_node *str = new_bd_arm_Str(dbgi, block, stack, node, nomem, mode_gp,
1188 set_irn_pinned(str, op_pin_state_floats);
1190 ldf = new_bd_arm_Ldf(dbgi, block, stack, str, mode_F, NULL, 0, 0, true);
1191 set_irn_pinned(ldf, op_pin_state_floats);
1193 return new_r_Proj(ldf, mode_fp, pn_arm_Ldf_res);
1196 static ir_node *float_to_int(dbg_info *dbgi, ir_node *block, ir_node *node)
1198 ir_graph *irg = current_ir_graph;
1199 ir_node *stack = get_irg_frame(irg);
1200 ir_node *nomem = get_irg_no_mem(irg);
1201 ir_node *stf = new_bd_arm_Stf(dbgi, block, stack, node, nomem, mode_F,
1204 set_irn_pinned(stf, op_pin_state_floats);
1206 ldr = new_bd_arm_Ldr(dbgi, block, stack, stf, mode_gp, NULL, 0, 0, true);
1207 set_irn_pinned(ldr, op_pin_state_floats);
1209 return new_r_Proj(ldr, mode_gp, pn_arm_Ldr_res);
1212 static void double_to_ints(dbg_info *dbgi, ir_node *block, ir_node *node,
1213 ir_node **out_value0, ir_node **out_value1)
1215 ir_graph *irg = current_ir_graph;
1216 ir_node *stack = get_irg_frame(irg);
1217 ir_node *nomem = get_irg_no_mem(irg);
1218 ir_node *stf = new_bd_arm_Stf(dbgi, block, stack, node, nomem, mode_D,
1220 ir_node *ldr0, *ldr1;
1221 set_irn_pinned(stf, op_pin_state_floats);
1223 ldr0 = new_bd_arm_Ldr(dbgi, block, stack, stf, mode_gp, NULL, 0, 0, true);
1224 set_irn_pinned(ldr0, op_pin_state_floats);
1225 ldr1 = new_bd_arm_Ldr(dbgi, block, stack, stf, mode_gp, NULL, 0, 4, true);
1226 set_irn_pinned(ldr1, op_pin_state_floats);
1228 *out_value0 = new_r_Proj(ldr0, mode_gp, pn_arm_Ldr_res);
1229 *out_value1 = new_r_Proj(ldr1, mode_gp, pn_arm_Ldr_res);
1232 static ir_node *gen_CopyB(ir_node *node)
1234 ir_node *block = be_transform_node(get_nodes_block(node));
1235 ir_node *src = get_CopyB_src(node);
1236 ir_node *new_src = be_transform_node(src);
1237 ir_node *dst = get_CopyB_dst(node);
1238 ir_node *new_dst = be_transform_node(dst);
1239 ir_node *mem = get_CopyB_mem(node);
1240 ir_node *new_mem = be_transform_node(mem);
1241 dbg_info *dbg = get_irn_dbg_info(node);
1242 int size = get_type_size_bytes(get_CopyB_type(node));
1246 src_copy = be_new_Copy(block, new_src);
1247 dst_copy = be_new_Copy(block, new_dst);
1249 return new_bd_arm_CopyB(dbg, block, dst_copy, src_copy,
1250 new_bd_arm_EmptyReg(dbg, block),
1251 new_bd_arm_EmptyReg(dbg, block),
1252 new_bd_arm_EmptyReg(dbg, block),
1257 * Transform builtin clz.
1259 static ir_node *gen_clz(ir_node *node)
1261 ir_node *block = be_transform_node(get_nodes_block(node));
1262 dbg_info *dbg = get_irn_dbg_info(node);
1263 ir_node *op = get_irn_n(node, 1);
1264 ir_node *new_op = be_transform_node(op);
1266 /* TODO armv5 instruction, otherwise create a call */
1267 return new_bd_arm_Clz(dbg, block, new_op);
1271 * Transform Builtin node.
1273 static ir_node *gen_Builtin(ir_node *node)
1275 ir_builtin_kind kind = get_Builtin_kind(node);
1279 case ir_bk_debugbreak:
1280 case ir_bk_return_address:
1281 case ir_bk_frame_address:
1282 case ir_bk_prefetch:
1286 return gen_clz(node);
1289 case ir_bk_popcount:
1293 case ir_bk_inner_trampoline:
1296 panic("Builtin %s not implemented", get_builtin_kind_name(kind));
1300 * Transform Proj(Builtin) node.
1302 static ir_node *gen_Proj_Builtin(ir_node *proj)
1304 ir_node *node = get_Proj_pred(proj);
1305 ir_node *new_node = be_transform_node(node);
1306 ir_builtin_kind kind = get_Builtin_kind(node);
1309 case ir_bk_return_address:
1310 case ir_bk_frame_address:
1315 case ir_bk_popcount:
1317 assert(get_Proj_proj(proj) == pn_Builtin_max+1);
1320 case ir_bk_debugbreak:
1321 case ir_bk_prefetch:
1323 assert(get_Proj_proj(proj) == pn_Builtin_M);
1326 case ir_bk_inner_trampoline:
1329 panic("Builtin %s not implemented", get_builtin_kind_name(kind));
1332 static ir_node *gen_Proj_Load(ir_node *node)
1334 ir_node *load = get_Proj_pred(node);
1335 ir_node *new_load = be_transform_node(load);
1336 dbg_info *dbgi = get_irn_dbg_info(node);
1337 long proj = get_Proj_proj(node);
1339 /* renumber the proj */
1340 switch (get_arm_irn_opcode(new_load)) {
1342 /* handle all gp loads equal: they have the same proj numbers. */
1343 if (proj == pn_Load_res) {
1344 return new_rd_Proj(dbgi, new_load, mode_Iu, pn_arm_Ldr_res);
1345 } else if (proj == pn_Load_M) {
1346 return new_rd_Proj(dbgi, new_load, mode_M, pn_arm_Ldr_M);
1350 if (proj == pn_Load_res) {
1351 ir_mode *mode = get_Load_mode(load);
1352 return new_rd_Proj(dbgi, new_load, mode, pn_arm_Ldf_res);
1353 } else if (proj == pn_Load_M) {
1354 return new_rd_Proj(dbgi, new_load, mode_M, pn_arm_Ldf_M);
1360 panic("Unsupported Proj from Load");
1363 static ir_node *gen_Proj_CopyB(ir_node *node)
1365 ir_node *pred = get_Proj_pred(node);
1366 ir_node *new_pred = be_transform_node(pred);
1367 dbg_info *dbgi = get_irn_dbg_info(node);
1368 long proj = get_Proj_proj(node);
1372 if (is_arm_CopyB(new_pred)) {
1373 return new_rd_Proj(dbgi, new_pred, mode_M, pn_arm_CopyB_M);
1379 panic("Unsupported Proj from CopyB");
1382 static ir_node *gen_Proj_Div(ir_node *node)
1384 ir_node *pred = get_Proj_pred(node);
1385 ir_node *new_pred = be_transform_node(pred);
1386 dbg_info *dbgi = get_irn_dbg_info(node);
1387 ir_mode *mode = get_irn_mode(node);
1388 long proj = get_Proj_proj(node);
1392 return new_rd_Proj(dbgi, new_pred, mode_M, pn_arm_Dvf_M);
1394 return new_rd_Proj(dbgi, new_pred, mode, pn_arm_Dvf_res);
1398 panic("Unsupported Proj from Div");
1402 * Transform the Projs from a Cmp.
1404 static ir_node *gen_Proj_Cmp(ir_node *node)
1407 /* we should only be here in case of a Mux node */
1411 static ir_node *gen_Proj_Start(ir_node *node)
1413 ir_node *block = get_nodes_block(node);
1414 ir_node *new_block = be_transform_node(block);
1415 long proj = get_Proj_proj(node);
1417 switch ((pn_Start) proj) {
1418 case pn_Start_X_initial_exec:
1419 /* we exchange the ProjX with a jump */
1420 return new_bd_arm_Jmp(NULL, new_block);
1423 return be_prolog_get_memory(abihelper);
1425 case pn_Start_T_args:
1426 return new_r_Bad(get_irn_irg(block), mode_T);
1428 case pn_Start_P_frame_base:
1429 return be_prolog_get_reg_value(abihelper, sp_reg);
1431 panic("unexpected start proj: %ld\n", proj);
1434 static ir_node *gen_Proj_Proj_Start(ir_node *node)
1436 long pn = get_Proj_proj(node);
1437 ir_node *block = get_nodes_block(node);
1438 ir_node *new_block = be_transform_node(block);
1439 ir_entity *entity = get_irg_entity(current_ir_graph);
1440 ir_type *method_type = get_entity_type(entity);
1441 ir_type *param_type = get_method_param_type(method_type, pn);
1442 const reg_or_stackslot_t *param;
1444 /* Proj->Proj->Start must be a method argument */
1445 assert(get_Proj_proj(get_Proj_pred(node)) == pn_Start_T_args);
1447 param = &cconv->parameters[pn];
1449 if (param->reg0 != NULL) {
1450 /* argument transmitted in register */
1451 ir_mode *mode = get_type_mode(param_type);
1452 ir_node *value = be_prolog_get_reg_value(abihelper, param->reg0);
1454 if (mode_is_float(mode)) {
1455 ir_node *value1 = NULL;
1457 if (param->reg1 != NULL) {
1458 value1 = be_prolog_get_reg_value(abihelper, param->reg1);
1459 } else if (param->entity != NULL) {
1460 ir_graph *irg = get_irn_irg(node);
1461 ir_node *fp = get_irg_frame(irg);
1462 ir_node *mem = be_prolog_get_memory(abihelper);
1463 ir_node *ldr = new_bd_arm_Ldr(NULL, new_block, fp, mem,
1464 mode_gp, param->entity,
1466 value1 = new_r_Proj(ldr, mode_gp, pn_arm_Ldr_res);
1469 /* convert integer value to float */
1470 if (value1 == NULL) {
1471 value = int_to_float(NULL, new_block, value);
1473 value = ints_to_double(NULL, new_block, value, value1);
1478 /* argument transmitted on stack */
1479 ir_graph *irg = get_irn_irg(node);
1480 ir_node *fp = get_irg_frame(irg);
1481 ir_node *mem = be_prolog_get_memory(abihelper);
1482 ir_mode *mode = get_type_mode(param->type);
1486 if (mode_is_float(mode)) {
1487 load = new_bd_arm_Ldf(NULL, new_block, fp, mem, mode,
1488 param->entity, 0, 0, true);
1489 value = new_r_Proj(load, mode_fp, pn_arm_Ldf_res);
1491 load = new_bd_arm_Ldr(NULL, new_block, fp, mem, mode,
1492 param->entity, 0, 0, true);
1493 value = new_r_Proj(load, mode_gp, pn_arm_Ldr_res);
1495 set_irn_pinned(load, op_pin_state_floats);
1502 * Finds number of output value of a mode_T node which is constrained to
1503 * a single specific register.
1505 static int find_out_for_reg(ir_node *node, const arch_register_t *reg)
1507 int n_outs = arch_get_irn_n_outs(node);
1510 for (o = 0; o < n_outs; ++o) {
1511 const arch_register_req_t *req = arch_get_irn_register_req_out(node, o);
1512 if (req == reg->single_req)
1518 static ir_node *gen_Proj_Proj_Call(ir_node *node)
1520 long pn = get_Proj_proj(node);
1521 ir_node *call = get_Proj_pred(get_Proj_pred(node));
1522 ir_node *new_call = be_transform_node(call);
1523 ir_type *function_type = get_Call_type(call);
1524 calling_convention_t *cconv
1525 = arm_decide_calling_convention(NULL, function_type);
1526 const reg_or_stackslot_t *res = &cconv->results[pn];
1530 /* TODO 64bit modes */
1531 assert(res->reg0 != NULL && res->reg1 == NULL);
1532 regn = find_out_for_reg(new_call, res->reg0);
1534 panic("Internal error in calling convention for return %+F", node);
1536 mode = res->reg0->reg_class->mode;
1538 arm_free_calling_convention(cconv);
1540 return new_r_Proj(new_call, mode, regn);
1543 static ir_node *gen_Proj_Call(ir_node *node)
1545 long pn = get_Proj_proj(node);
1546 ir_node *call = get_Proj_pred(node);
1547 ir_node *new_call = be_transform_node(call);
1549 switch ((pn_Call) pn) {
1551 return new_r_Proj(new_call, mode_M, 0);
1552 case pn_Call_X_regular:
1553 case pn_Call_X_except:
1554 case pn_Call_T_result:
1557 panic("Unexpected Call proj %ld\n", pn);
1561 * Transform a Proj node.
1563 static ir_node *gen_Proj(ir_node *node)
1565 ir_node *pred = get_Proj_pred(node);
1566 long proj = get_Proj_proj(node);
1568 switch (get_irn_opcode(pred)) {
1570 if (proj == pn_Store_M) {
1571 return be_transform_node(pred);
1573 panic("Unsupported Proj from Store");
1576 return gen_Proj_Load(node);
1578 return gen_Proj_Call(node);
1580 return gen_Proj_CopyB(node);
1582 return gen_Proj_Div(node);
1584 return gen_Proj_Cmp(node);
1586 return gen_Proj_Start(node);
1590 return be_duplicate_node(node);
1592 ir_node *pred_pred = get_Proj_pred(pred);
1593 if (is_Call(pred_pred)) {
1594 return gen_Proj_Proj_Call(node);
1595 } else if (is_Start(pred_pred)) {
1596 return gen_Proj_Proj_Start(node);
1601 return gen_Proj_Builtin(node);
1603 panic("code selection didn't expect Proj after %+F\n", pred);
1607 typedef ir_node *(*create_const_node_func)(dbg_info *db, ir_node *block);
1609 static inline ir_node *create_const(ir_graph *irg, ir_node **place,
1610 create_const_node_func func,
1611 const arch_register_t* reg)
1613 ir_node *block, *res;
1618 block = get_irg_start_block(irg);
1619 res = func(NULL, block);
1620 arch_set_irn_register(res, reg);
1625 static ir_node *gen_Unknown(ir_node *node)
1627 ir_node *block = get_nodes_block(node);
1628 ir_node *new_block = be_transform_node(block);
1629 dbg_info *dbgi = get_irn_dbg_info(node);
1631 /* just produce a 0 */
1632 ir_mode *mode = get_irn_mode(node);
1633 if (mode_is_float(mode)) {
1634 ir_tarval *tv = get_mode_null(mode);
1635 ir_node *fconst = new_bd_arm_fConst(dbgi, new_block, tv);
1637 } else if (mode_needs_gp_reg(mode)) {
1638 return create_const_graph_value(dbgi, new_block, 0);
1641 panic("Unexpected Unknown mode");
1645 * Produces the type which sits between the stack args and the locals on the
1646 * stack. It will contain the return address and space to store the old base
1648 * @return The Firm type modeling the ABI between type.
1650 static ir_type *arm_get_between_type(void)
1652 static ir_type *between_type = NULL;
1654 if (between_type == NULL) {
1655 between_type = new_type_class(new_id_from_str("arm_between_type"));
1656 set_type_size_bytes(between_type, 0);
1659 return between_type;
1662 static void create_stacklayout(ir_graph *irg)
1664 ir_entity *entity = get_irg_entity(irg);
1665 ir_type *function_type = get_entity_type(entity);
1666 be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
1671 /* calling conventions must be decided by now */
1672 assert(cconv != NULL);
1674 /* construct argument type */
1675 arg_type = new_type_struct(id_mangle_u(get_entity_ident(entity), new_id_from_chars("arg_type", 8)));
1676 n_params = get_method_n_params(function_type);
1677 for (p = 0; p < n_params; ++p) {
1678 reg_or_stackslot_t *param = &cconv->parameters[p];
1682 if (param->type == NULL)
1685 snprintf(buf, sizeof(buf), "param_%d", p);
1686 id = new_id_from_str(buf);
1687 param->entity = new_entity(arg_type, id, param->type);
1688 set_entity_offset(param->entity, param->offset);
1691 /* TODO: what about external functions? we don't know most of the stack
1692 * layout for them. And probably don't need all of this... */
1693 memset(layout, 0, sizeof(*layout));
1695 layout->frame_type = get_irg_frame_type(irg);
1696 layout->between_type = arm_get_between_type();
1697 layout->arg_type = arg_type;
1698 layout->initial_offset = 0;
1699 layout->initial_bias = 0;
1700 layout->sp_relative = true;
1702 assert(N_FRAME_TYPES == 3);
1703 layout->order[0] = layout->frame_type;
1704 layout->order[1] = layout->between_type;
1705 layout->order[2] = layout->arg_type;
1709 * transform the start node to the prolog code
1711 static ir_node *gen_Start(ir_node *node)
1713 ir_graph *irg = get_irn_irg(node);
1714 ir_entity *entity = get_irg_entity(irg);
1715 ir_type *function_type = get_entity_type(entity);
1716 ir_node *block = get_nodes_block(node);
1717 ir_node *new_block = be_transform_node(block);
1718 dbg_info *dbgi = get_irn_dbg_info(node);
1722 /* stackpointer is important at function prolog */
1723 be_prolog_add_reg(abihelper, sp_reg,
1724 arch_register_req_type_produces_sp | arch_register_req_type_ignore);
1725 /* function parameters in registers */
1726 for (i = 0; i < get_method_n_params(function_type); ++i) {
1727 const reg_or_stackslot_t *param = &cconv->parameters[i];
1728 if (param->reg0 != NULL)
1729 be_prolog_add_reg(abihelper, param->reg0, arch_register_req_type_none);
1730 if (param->reg1 != NULL)
1731 be_prolog_add_reg(abihelper, param->reg1, arch_register_req_type_none);
1733 /* announce that we need the values of the callee save regs */
1734 for (i = 0; i != ARRAY_SIZE(callee_saves); ++i) {
1735 be_prolog_add_reg(abihelper, callee_saves[i], arch_register_req_type_none);
1738 start = be_prolog_create_start(abihelper, dbgi, new_block);
1742 static ir_node *get_stack_pointer_for(ir_node *node)
1744 /* get predecessor in stack_order list */
1745 ir_node *stack_pred = be_get_stack_pred(stackorder, node);
1748 if (stack_pred == NULL) {
1749 /* first stack user in the current block. We can simply use the
1750 * initial sp_proj for it */
1751 ir_node *sp_proj = be_prolog_get_reg_value(abihelper, sp_reg);
1755 be_transform_node(stack_pred);
1756 stack = pmap_get(ir_node, node_to_stack, stack_pred);
1757 if (stack == NULL) {
1758 return get_stack_pointer_for(stack_pred);
1765 * transform a Return node into epilogue code + return statement
1767 static ir_node *gen_Return(ir_node *node)
1769 ir_node *block = get_nodes_block(node);
1770 ir_node *new_block = be_transform_node(block);
1771 dbg_info *dbgi = get_irn_dbg_info(node);
1772 ir_node *mem = get_Return_mem(node);
1773 ir_node *new_mem = be_transform_node(mem);
1774 size_t n_callee_saves = ARRAY_SIZE(callee_saves);
1775 ir_node *sp_proj = get_stack_pointer_for(node);
1776 size_t n_res = get_Return_n_ress(node);
1780 be_epilog_begin(abihelper);
1781 be_epilog_set_memory(abihelper, new_mem);
1782 /* connect stack pointer with initial stack pointer. fix_stack phase
1783 will later serialize all stack pointer adjusting nodes */
1784 be_epilog_add_reg(abihelper, sp_reg,
1785 arch_register_req_type_produces_sp | arch_register_req_type_ignore,
1789 for (i = 0; i < n_res; ++i) {
1790 ir_node *res_value = get_Return_res(node, i);
1791 ir_node *new_res_value = be_transform_node(res_value);
1792 const reg_or_stackslot_t *slot = &cconv->results[i];
1793 const arch_register_t *reg = slot->reg0;
1794 assert(slot->reg1 == NULL);
1795 be_epilog_add_reg(abihelper, reg, arch_register_req_type_none, new_res_value);
1798 /* connect callee saves with their values at the function begin */
1799 for (i = 0; i < n_callee_saves; ++i) {
1800 const arch_register_t *reg = callee_saves[i];
1801 ir_node *value = be_prolog_get_reg_value(abihelper, reg);
1802 be_epilog_add_reg(abihelper, reg, arch_register_req_type_none, value);
1805 /* epilog code: an incsp */
1806 bereturn = be_epilog_create_return(abihelper, dbgi, new_block);
1811 static ir_node *gen_Call(ir_node *node)
1813 ir_graph *irg = get_irn_irg(node);
1814 ir_node *callee = get_Call_ptr(node);
1815 ir_node *block = get_nodes_block(node);
1816 ir_node *new_block = be_transform_node(block);
1817 ir_node *mem = get_Call_mem(node);
1818 ir_node *new_mem = be_transform_node(mem);
1819 dbg_info *dbgi = get_irn_dbg_info(node);
1820 ir_type *type = get_Call_type(node);
1821 calling_convention_t *cconv = arm_decide_calling_convention(NULL, type);
1822 size_t n_params = get_Call_n_params(node);
1823 size_t const n_param_regs = cconv->n_reg_params;
1824 /* max inputs: memory, callee, register arguments */
1825 size_t const max_inputs = 2 + n_param_regs;
1826 ir_node **in = ALLOCAN(ir_node*, max_inputs);
1827 ir_node **sync_ins = ALLOCAN(ir_node*, max_inputs);
1828 struct obstack *obst = be_get_be_obst(irg);
1829 const arch_register_req_t **in_req
1830 = OALLOCNZ(obst, const arch_register_req_t*, max_inputs);
1831 size_t in_arity = 0;
1832 size_t sync_arity = 0;
1833 size_t const n_caller_saves = ARRAY_SIZE(caller_saves);
1834 ir_entity *entity = NULL;
1835 ir_node *incsp = NULL;
1842 assert(n_params == get_method_n_params(type));
1844 /* construct arguments */
1847 in_req[in_arity] = arch_no_register_req;
1851 for (p = 0; p < n_params; ++p) {
1852 ir_node *value = get_Call_param(node, p);
1853 ir_node *new_value = be_transform_node(value);
1854 ir_node *new_value1 = NULL;
1855 const reg_or_stackslot_t *param = &cconv->parameters[p];
1856 ir_type *param_type = get_method_param_type(type, p);
1857 ir_mode *mode = get_type_mode(param_type);
1860 if (mode_is_float(mode) && param->reg0 != NULL) {
1861 unsigned size_bits = get_mode_size_bits(mode);
1862 if (size_bits == 64) {
1863 double_to_ints(dbgi, new_block, new_value, &new_value,
1866 assert(size_bits == 32);
1867 new_value = float_to_int(dbgi, new_block, new_value);
1871 /* put value into registers */
1872 if (param->reg0 != NULL) {
1873 in[in_arity] = new_value;
1874 in_req[in_arity] = param->reg0->single_req;
1876 if (new_value1 == NULL)
1879 if (param->reg1 != NULL) {
1880 assert(new_value1 != NULL);
1881 in[in_arity] = new_value1;
1882 in_req[in_arity] = param->reg1->single_req;
1887 /* we need a store if we're here */
1888 if (new_value1 != NULL) {
1889 new_value = new_value1;
1893 /* create a parameter frame if necessary */
1894 if (incsp == NULL) {
1895 ir_node *new_frame = get_stack_pointer_for(node);
1896 incsp = be_new_IncSP(sp_reg, new_block, new_frame,
1897 cconv->param_stack_size, 1);
1899 if (mode_is_float(mode)) {
1900 str = new_bd_arm_Stf(dbgi, new_block, incsp, new_value, new_mem,
1901 mode, NULL, 0, param->offset, true);
1903 str = new_bd_arm_Str(dbgi, new_block, incsp, new_value, new_mem,
1904 mode, NULL, 0, param->offset, true);
1906 sync_ins[sync_arity++] = str;
1908 assert(in_arity <= max_inputs);
1910 /* construct memory input */
1911 if (sync_arity == 0) {
1912 in[mem_pos] = new_mem;
1913 } else if (sync_arity == 1) {
1914 in[mem_pos] = sync_ins[0];
1916 in[mem_pos] = new_rd_Sync(NULL, new_block, sync_arity, sync_ins);
1919 /* TODO: use a generic symconst matcher here */
1920 if (is_SymConst(callee)) {
1921 entity = get_SymConst_entity(callee);
1923 /* TODO: finish load matcher here */
1926 if (is_Proj(callee) && is_Load(get_Proj_pred(callee))) {
1927 ir_node *load = get_Proj_pred(callee);
1928 ir_node *ptr = get_Load_ptr(load);
1929 ir_node *new_ptr = be_transform_node(ptr);
1930 ir_node *mem = get_Load_mem(load);
1931 ir_node *new_mem = be_transform_node(mem);
1932 ir_mode *mode = get_Load_mode(node);
1936 in[in_arity] = be_transform_node(callee);
1937 in_req[in_arity] = arm_reg_classes[CLASS_arm_gp].class_req;
1946 out_arity = 1 + n_caller_saves;
1948 if (entity != NULL) {
1949 /* TODO: use a generic symconst matcher here
1950 * so we can also handle entity+offset, etc. */
1951 res = new_bd_arm_Bl(dbgi, new_block, in_arity, in, out_arity,entity, 0);
1954 * - use a proper shifter_operand matcher
1955 * - we could also use LinkLdrPC
1957 res = new_bd_arm_LinkMovPC(dbgi, new_block, in_arity, in, out_arity,
1961 if (incsp != NULL) {
1962 /* IncSP to destroy the call stackframe */
1963 incsp = be_new_IncSP(sp_reg, new_block, incsp, -cconv->param_stack_size,
1965 /* if we are the last IncSP producer in a block then we have to keep
1967 * Note: This here keeps all producers which is more than necessary */
1968 add_irn_dep(incsp, res);
1971 pmap_insert(node_to_stack, node, incsp);
1974 arch_set_irn_register_reqs_in(res, in_req);
1976 /* create output register reqs */
1977 arch_set_irn_register_req_out(res, 0, arch_no_register_req);
1978 for (o = 0; o < n_caller_saves; ++o) {
1979 const arch_register_t *reg = caller_saves[o];
1980 arch_set_irn_register_req_out(res, o+1, reg->single_req);
1983 /* copy pinned attribute */
1984 set_irn_pinned(res, get_irn_pinned(node));
1986 arm_free_calling_convention(cconv);
1990 static ir_node *gen_Sel(ir_node *node)
1992 dbg_info *dbgi = get_irn_dbg_info(node);
1993 ir_node *block = get_nodes_block(node);
1994 ir_node *new_block = be_transform_node(block);
1995 ir_node *ptr = get_Sel_ptr(node);
1996 ir_node *new_ptr = be_transform_node(ptr);
1997 ir_entity *entity = get_Sel_entity(node);
1999 /* must be the frame pointer all other sels must have been lowered
2001 assert(is_Proj(ptr) && is_Start(get_Proj_pred(ptr)));
2003 return new_bd_arm_FrameAddr(dbgi, new_block, new_ptr, entity, 0);
2007 * Change some phi modes
2009 static ir_node *gen_Phi(ir_node *node)
2011 const arch_register_req_t *req;
2012 ir_node *block = be_transform_node(get_nodes_block(node));
2013 ir_graph *irg = current_ir_graph;
2014 dbg_info *dbgi = get_irn_dbg_info(node);
2015 ir_mode *mode = get_irn_mode(node);
2018 if (mode_needs_gp_reg(mode)) {
2019 /* we shouldn't have any 64bit stuff around anymore */
2020 assert(get_mode_size_bits(mode) <= 32);
2021 /* all integer operations are on 32bit registers now */
2023 req = arm_reg_classes[CLASS_arm_gp].class_req;
2025 req = arch_no_register_req;
2028 /* phi nodes allow loops, so we use the old arguments for now
2029 * and fix this later */
2030 phi = new_ir_node(dbgi, irg, block, op_Phi, mode, get_irn_arity(node),
2031 get_irn_in(node) + 1);
2032 copy_node_attr(irg, node, phi);
2033 be_duplicate_deps(node, phi);
2035 arch_set_irn_register_req_out(phi, 0, req);
2037 be_enqueue_preds(node);
2044 * Enters all transform functions into the generic pointer
2046 static void arm_register_transformers(void)
2048 be_start_transform_setup();
2050 be_set_transform_function(op_Add, gen_Add);
2051 be_set_transform_function(op_And, gen_And);
2052 be_set_transform_function(op_Call, gen_Call);
2053 be_set_transform_function(op_Cmp, gen_Cmp);
2054 be_set_transform_function(op_Cond, gen_Cond);
2055 be_set_transform_function(op_Const, gen_Const);
2056 be_set_transform_function(op_Conv, gen_Conv);
2057 be_set_transform_function(op_CopyB, gen_CopyB);
2058 be_set_transform_function(op_Div, gen_Div);
2059 be_set_transform_function(op_Eor, gen_Eor);
2060 be_set_transform_function(op_Jmp, gen_Jmp);
2061 be_set_transform_function(op_Load, gen_Load);
2062 be_set_transform_function(op_Minus, gen_Minus);
2063 be_set_transform_function(op_Mul, gen_Mul);
2064 be_set_transform_function(op_Not, gen_Not);
2065 be_set_transform_function(op_Or, gen_Or);
2066 be_set_transform_function(op_Phi, gen_Phi);
2067 be_set_transform_function(op_Proj, gen_Proj);
2068 be_set_transform_function(op_Return, gen_Return);
2069 be_set_transform_function(op_Rotl, gen_Rotl);
2070 be_set_transform_function(op_Sel, gen_Sel);
2071 be_set_transform_function(op_Shl, gen_Shl);
2072 be_set_transform_function(op_Shr, gen_Shr);
2073 be_set_transform_function(op_Shrs, gen_Shrs);
2074 be_set_transform_function(op_Start, gen_Start);
2075 be_set_transform_function(op_Store, gen_Store);
2076 be_set_transform_function(op_Sub, gen_Sub);
2077 be_set_transform_function(op_Switch, gen_Switch);
2078 be_set_transform_function(op_SymConst, gen_SymConst);
2079 be_set_transform_function(op_Unknown, gen_Unknown);
2080 be_set_transform_function(op_Builtin, gen_Builtin);
2084 * Initialize fpa Immediate support.
2086 static void arm_init_fpa_immediate(void)
2088 /* 0, 1, 2, 3, 4, 5, 10, or 0.5. */
2089 fpa_imm[FPA_IMM_FLOAT][fpa_null] = get_mode_null(mode_F);
2090 fpa_imm[FPA_IMM_FLOAT][fpa_one] = get_mode_one(mode_F);
2091 fpa_imm[FPA_IMM_FLOAT][fpa_two] = new_tarval_from_str("2", 1, mode_F);
2092 fpa_imm[FPA_IMM_FLOAT][fpa_three] = new_tarval_from_str("3", 1, mode_F);
2093 fpa_imm[FPA_IMM_FLOAT][fpa_four] = new_tarval_from_str("4", 1, mode_F);
2094 fpa_imm[FPA_IMM_FLOAT][fpa_five] = new_tarval_from_str("5", 1, mode_F);
2095 fpa_imm[FPA_IMM_FLOAT][fpa_ten] = new_tarval_from_str("10", 2, mode_F);
2096 fpa_imm[FPA_IMM_FLOAT][fpa_half] = new_tarval_from_str("0.5", 3, mode_F);
2098 fpa_imm[FPA_IMM_DOUBLE][fpa_null] = get_mode_null(mode_D);
2099 fpa_imm[FPA_IMM_DOUBLE][fpa_one] = get_mode_one(mode_D);
2100 fpa_imm[FPA_IMM_DOUBLE][fpa_two] = new_tarval_from_str("2", 1, mode_D);
2101 fpa_imm[FPA_IMM_DOUBLE][fpa_three] = new_tarval_from_str("3", 1, mode_D);
2102 fpa_imm[FPA_IMM_DOUBLE][fpa_four] = new_tarval_from_str("4", 1, mode_D);
2103 fpa_imm[FPA_IMM_DOUBLE][fpa_five] = new_tarval_from_str("5", 1, mode_D);
2104 fpa_imm[FPA_IMM_DOUBLE][fpa_ten] = new_tarval_from_str("10", 2, mode_D);
2105 fpa_imm[FPA_IMM_DOUBLE][fpa_half] = new_tarval_from_str("0.5", 3, mode_D);
2109 * Transform a Firm graph into an ARM graph.
2111 void arm_transform_graph(ir_graph *irg)
2113 static int imm_initialized = 0;
2114 ir_entity *entity = get_irg_entity(irg);
2115 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
2116 ir_type *frame_type;
2121 if (! imm_initialized) {
2122 arm_init_fpa_immediate();
2123 imm_initialized = 1;
2125 arm_register_transformers();
2127 isa = (arm_isa_t*) arch_env;
2129 node_to_stack = pmap_create();
2131 assert(abihelper == NULL);
2132 abihelper = be_abihelper_prepare(irg);
2133 stackorder = be_collect_stacknodes(irg);
2134 assert(cconv == NULL);
2135 cconv = arm_decide_calling_convention(irg, get_entity_type(entity));
2136 create_stacklayout(irg);
2138 be_transform_graph(irg, NULL);
2140 be_abihelper_finish(abihelper);
2142 be_free_stackorder(stackorder);
2145 arm_free_calling_convention(cconv);
2148 frame_type = get_irg_frame_type(irg);
2149 if (get_type_state(frame_type) == layout_undefined) {
2150 default_layout_compound_type(frame_type);
2153 pmap_destroy(node_to_stack);
2154 node_to_stack = NULL;
2156 be_add_missing_keeps(irg);
2159 void arm_init_transform(void)
2161 FIRM_DBG_REGISTER(dbg, "firm.be.arm.transform");