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)
103 /* first optimize incsp->incsp combinations */
104 node = be_peephole_IncSP_IncSP(node);
106 offset = be_get_IncSP_offset(node);
107 /* can be transformed into Add OR Sub */
112 if (allowed_arm_immediate(offset, &v))
115 be_set_IncSP_offset(node, sign * arm_ror(v.values[0], v.rors[0]));
118 block = get_nodes_block(node);
119 for (cnt = 1; cnt < v.ops; ++cnt) {
120 int value = sign * arm_ror(v.values[cnt], v.rors[cnt]);
121 ir_node *incsp = be_new_IncSP(&arm_registers[REG_SP], block, node,
123 sched_add_after(node, incsp);
127 /* reattach IncSP users */
129 node = sched_next(first);
130 foreach_out_edge_safe(first, edge) {
131 ir_node *user = get_edge_src_irn(edge);
132 int pos = get_edge_src_pos(edge);
135 set_irn_n(user, pos, last);
140 * creates the address by Adds
142 static ir_node *gen_ptr_add(ir_node *node, ir_node *frame, arm_vals *v)
144 dbg_info *dbgi = get_irn_dbg_info(node);
145 ir_node *block = get_nodes_block(node);
149 ptr = new_bd_arm_Add_imm(dbgi, block, frame, v->values[0], v->rors[0]);
150 arch_set_irn_register(ptr, &arm_registers[REG_R12]);
151 sched_add_before(node, ptr);
153 for (cnt = 1; cnt < v->ops; ++cnt) {
154 ir_node *next = new_bd_arm_Add_imm(dbgi, block, ptr, v->values[cnt],
156 arch_set_irn_register(next, &arm_registers[REG_R12]);
157 sched_add_before(node, next);
164 * creates the address by Subs
166 static ir_node *gen_ptr_sub(ir_node *node, ir_node *frame, arm_vals *v)
168 dbg_info *dbgi = get_irn_dbg_info(node);
169 ir_node *block = get_nodes_block(node);
173 ptr = new_bd_arm_Sub_imm(dbgi, block, frame, v->values[0], v->rors[0]);
174 arch_set_irn_register(ptr, &arm_registers[REG_R12]);
175 sched_add_before(node, ptr);
177 for (cnt = 1; cnt < v->ops; ++cnt) {
178 ir_node *next = new_bd_arm_Sub_imm(dbgi, block, ptr, v->values[cnt],
180 arch_set_irn_register(next, &arm_registers[REG_R12]);
181 sched_add_before(node, next);
187 /** fix frame addresses which are too big */
188 static void peephole_arm_FrameAddr(ir_node *node)
190 arm_SymConst_attr_t *attr = get_arm_SymConst_attr(node);
191 int offset = attr->fp_offset;
196 if (allowed_arm_immediate(offset, &v))
199 base = get_irn_n(node, n_arm_FrameAddr_base);
200 /* TODO: suboptimal */
201 ptr = gen_ptr_add(node, base, &v);
204 set_irn_n(node, n_arm_FrameAddr_base, ptr);
208 * Fix stackpointer relative stores if the offset gets too big
210 static void peephole_arm_Str_Ldr(ir_node *node)
212 arm_load_store_attr_t *attr = get_arm_load_store_attr(node);
213 int offset = attr->offset;
218 if (allowed_arm_immediate(offset, &v))
221 /* we should only have too big offsets for frame entities */
222 if (!attr->is_frame_entity) {
224 "POSSIBLE ARM BACKEND PROBLEM: offset in Store too big\n");
231 if (is_arm_Str(node)) {
232 ptr = get_irn_n(node, n_arm_Str_ptr);
234 assert(is_arm_Ldr(node));
235 ptr = get_irn_n(node, n_arm_Ldr_ptr);
239 ptr = gen_ptr_add(node, ptr, &v);
241 ptr = gen_ptr_sub(node, ptr, &v);
244 /* TODO: sub-optimal, the last offset could probably be left inside the
246 if (is_arm_Str(node)) {
247 set_irn_n(node, n_arm_Str_ptr, ptr);
249 assert(is_arm_Ldr(node));
250 set_irn_n(node, n_arm_Ldr_ptr, ptr);
256 * Register a peephole optimization function.
258 static void register_peephole_optimisation(ir_op *op, peephole_opt_func func)
260 assert(op->ops.generic == NULL);
261 op->ops.generic = (op_func)func;
264 /* Perform peephole-optimizations. */
265 void arm_peephole_optimization(ir_graph *irg)
267 /* register peephole optimizations */
268 ir_clear_opcodes_generic_func();
269 register_peephole_optimisation(op_be_IncSP, peephole_be_IncSP);
270 register_peephole_optimisation(op_arm_Str, peephole_arm_Str_Ldr);
271 register_peephole_optimisation(op_arm_Ldr, peephole_arm_Str_Ldr);
272 register_peephole_optimisation(op_arm_FrameAddr, peephole_arm_FrameAddr);
274 be_peephole_opt(irg);