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 Implements several optimizations for ARM.
23 * @author Michael Beck
33 #include "bepeephole.h"
36 #include "arm_optimize.h"
37 #include "gen_arm_regalloc_if.h"
38 #include "gen_arm_new_nodes.h"
39 #include "arm_nodes_attr.h"
40 #include "arm_new_nodes.h"
42 static unsigned arm_ror(unsigned v, unsigned ror)
44 return (v << (32 - ror)) | (v >> ror);
48 * construct 8bit values and rot amounts for a value.
50 void arm_gen_vals_from_word(unsigned int value, arm_vals *result)
54 /* TODO: not optimal yet, as we only "shift" the value and don't take advantage of rotations */
56 /* special case: we prefer shift amount 0 */
58 result->values[0] = value;
66 while ( (value & 0x3) == 0) {
71 result->values[result->ops] = value & 0xFF;
72 result->rors[result->ops] = (32-initial) % 32;
81 * Returns non.zero if the given offset can be directly encoded into an ARM
84 static int allowed_arm_immediate(int offset, arm_vals *result)
86 arm_gen_vals_from_word(offset, result);
87 return result->ops <= 1;
91 * Fix an IncSP node if the offset gets too big
93 static void peephole_be_IncSP(ir_node *node)
102 const ir_edge_t *edge;
103 const ir_edge_t *next;
105 /* first optimize incsp->incsp combinations */
106 node = be_peephole_IncSP_IncSP(node);
108 offset = be_get_IncSP_offset(node);
109 /* can be transformed into Add OR Sub */
114 if (allowed_arm_immediate(offset, &v))
117 be_set_IncSP_offset(node, sign * arm_ror(v.values[0], v.rors[0]));
120 block = get_nodes_block(node);
121 for (cnt = 1; cnt < v.ops; ++cnt) {
122 int value = sign * arm_ror(v.values[cnt], v.rors[cnt]);
123 ir_node *incsp = be_new_IncSP(&arm_registers[REG_SP], block, node,
125 sched_add_after(node, incsp);
129 /* reattach IncSP users */
131 node = sched_next(first);
132 foreach_out_edge_safe(first, edge, next) {
133 ir_node *user = get_edge_src_irn(edge);
134 int pos = get_edge_src_pos(edge);
137 set_irn_n(user, pos, last);
142 * creates the address by Adds
144 static ir_node *gen_ptr_add(ir_node *node, ir_node *frame, arm_vals *v)
146 dbg_info *dbgi = get_irn_dbg_info(node);
147 ir_node *block = get_nodes_block(node);
151 ptr = new_bd_arm_Add_imm(dbgi, block, frame, v->values[0], v->rors[0]);
152 arch_set_irn_register(ptr, &arm_registers[REG_R12]);
153 sched_add_before(node, ptr);
155 for (cnt = 1; cnt < v->ops; ++cnt) {
156 ir_node *next = new_bd_arm_Add_imm(dbgi, block, ptr, v->values[cnt],
158 arch_set_irn_register(next, &arm_registers[REG_R12]);
159 sched_add_before(node, next);
166 * creates the address by Subs
168 static ir_node *gen_ptr_sub(ir_node *node, ir_node *frame, arm_vals *v)
170 dbg_info *dbgi = get_irn_dbg_info(node);
171 ir_node *block = get_nodes_block(node);
175 ptr = new_bd_arm_Sub_imm(dbgi, block, frame, v->values[0], v->rors[0]);
176 arch_set_irn_register(ptr, &arm_registers[REG_R12]);
177 sched_add_before(node, ptr);
179 for (cnt = 1; cnt < v->ops; ++cnt) {
180 ir_node *next = new_bd_arm_Sub_imm(dbgi, block, ptr, v->values[cnt],
182 arch_set_irn_register(next, &arm_registers[REG_R12]);
183 sched_add_before(node, next);
189 /** fix frame addresses which are too big */
190 static void peephole_arm_FrameAddr(ir_node *node)
192 arm_SymConst_attr_t *attr = get_arm_SymConst_attr(node);
193 int offset = attr->fp_offset;
198 if (allowed_arm_immediate(offset, &v))
201 base = get_irn_n(node, n_arm_FrameAddr_base);
202 /* TODO: suboptimal */
203 ptr = gen_ptr_add(node, base, &v);
206 set_irn_n(node, n_arm_FrameAddr_base, ptr);
210 * Fix stackpointer relative stores if the offset gets too big
212 static void peephole_arm_Str_Ldr(ir_node *node)
214 arm_load_store_attr_t *attr = get_arm_load_store_attr(node);
215 int offset = attr->offset;
220 if (allowed_arm_immediate(offset, &v))
223 /* we should only have too big offsets for frame entities */
224 if (!attr->is_frame_entity) {
226 "POSSIBLE ARM BACKEND PROBLEM: offset in Store too big\n");
233 if (is_arm_Str(node)) {
234 ptr = get_irn_n(node, n_arm_Str_ptr);
236 assert(is_arm_Ldr(node));
237 ptr = get_irn_n(node, n_arm_Ldr_ptr);
241 ptr = gen_ptr_add(node, ptr, &v);
243 ptr = gen_ptr_sub(node, ptr, &v);
246 /* TODO: sub-optimal, the last offset could probably be left inside the
248 if (is_arm_Str(node)) {
249 set_irn_n(node, n_arm_Str_ptr, ptr);
251 assert(is_arm_Ldr(node));
252 set_irn_n(node, n_arm_Ldr_ptr, ptr);
258 * Register a peephole optimization function.
260 static void register_peephole_optimisation(ir_op *op, peephole_opt_func func)
262 assert(op->ops.generic == NULL);
263 op->ops.generic = (op_func)func;
266 /* Perform peephole-optimizations. */
267 void arm_peephole_optimization(ir_graph *irg)
269 /* register peephole optimizations */
270 ir_clear_opcodes_generic_func();
271 register_peephole_optimisation(op_be_IncSP, peephole_be_IncSP);
272 register_peephole_optimisation(op_arm_Str, peephole_arm_Str_Ldr);
273 register_peephole_optimisation(op_arm_Ldr, peephole_arm_Str_Ldr);
274 register_peephole_optimisation(op_arm_FrameAddr, peephole_arm_FrameAddr);
276 be_peephole_opt(irg);