arm_optimize.c 5.19 KB
Newer Older
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
4
5
6
7
8
9
10
11
12
 */

/**
 * @file
 * @brief       Implements several optimizations for ARM.
 * @author      Michael Beck
 */
#include "irgmod.h"
#include "ircons.h"
13
#include "iredges.h"
14

15
#include "bediagnostic.h"
16
#include "benode.h"
17
18
19
20
21
22
#include "bepeephole.h"
#include "besched.h"

#include "arm_optimize.h"
#include "gen_arm_regalloc_if.h"
#include "gen_arm_new_nodes.h"
23
24
#include "arm_nodes_attr.h"
#include "arm_new_nodes.h"
25

Matthias Braun's avatar
Matthias Braun committed
26
static uint32_t arm_ror(uint32_t v, uint32_t ror)
27
28
{
	return (v << (32 - ror)) | (v >> ror);
29
30
31
}

/**
32
33
 * Returns non.zero if the given offset can be directly encoded into an ARM
 * instruction.
34
 */
Matthias Braun's avatar
Matthias Braun committed
35
static bool allowed_arm_immediate(int offset, arm_vals *result)
36
{
37
38
39
40
41
42
43
	arm_gen_vals_from_word(offset, result);
	return result->ops <= 1;
}

/**
 * Fix an IncSP node if the offset gets too big
 */
44
45
static void peephole_be_IncSP(ir_node *node)
{
46
	/* first optimize incsp->incsp combinations */
47
48
	if (be_peephole_IncSP_IncSP(node))
		return;
49

Matthias Braun's avatar
Matthias Braun committed
50
	int offset = be_get_IncSP_offset(node);
51
	/* can be transformed into Add OR Sub */
Matthias Braun's avatar
Matthias Braun committed
52
	int sign = 1;
53
54
55
56
	if (offset < 0) {
		sign = -1;
		offset = -offset;
	}
Matthias Braun's avatar
Matthias Braun committed
57
	arm_vals v;
58
59
60
	if (allowed_arm_immediate(offset, &v))
		return;

61
	be_set_IncSP_offset(node, sign * arm_ror(v.values[0], v.rors[0]));
62

Matthias Braun's avatar
Matthias Braun committed
63
64
65
	ir_node *first = node;
	ir_node *block = get_nodes_block(node);
	for (unsigned cnt = 1; cnt < v.ops; ++cnt) {
66
67
		int      value = sign * arm_ror(v.values[cnt], v.rors[cnt]);
		ir_node *incsp = be_new_IncSP(&arm_registers[REG_SP], block, node,
68
		                             value, 1);
69
70
		sched_add_after(node, incsp);
		node = incsp;
71
	}
72
73

	/* reattach IncSP users */
74
	edges_reroute_except(first, node, sched_next(first));
75
76
77
78
79
}

/**
 * creates the address by Adds
 */
Matthias Braun's avatar
Matthias Braun committed
80
static ir_node *gen_ptr_add(ir_node *node, ir_node *frame, const arm_vals *v)
81
{
82
	dbg_info *dbgi  = get_irn_dbg_info(node);
83
	ir_node  *block = get_nodes_block(node);
Matthias Braun's avatar
Matthias Braun committed
84
85
	ir_node  *ptr   = new_bd_arm_Add_imm(dbgi, block, frame, v->values[0],
	                                     v->rors[0]);
86
	arch_set_irn_register(ptr, &arm_registers[REG_R12]);
87
88
	sched_add_before(node, ptr);

Matthias Braun's avatar
Matthias Braun committed
89
	for (unsigned cnt = 1; cnt < v->ops; ++cnt) {
90
91
		ir_node *next = new_bd_arm_Add_imm(dbgi, block, ptr, v->values[cnt],
		                                   v->rors[cnt]);
92
		arch_set_irn_register(next, &arm_registers[REG_R12]);
93
94
95
96
97
98
99
100
101
		sched_add_before(node, next);
		ptr = next;
	}
	return ptr;
}

/**
* creates the address by Subs
*/
Matthias Braun's avatar
Matthias Braun committed
102
static ir_node *gen_ptr_sub(ir_node *node, ir_node *frame, const arm_vals *v)
103
{
104
	dbg_info *dbgi  = get_irn_dbg_info(node);
105
	ir_node  *block = get_nodes_block(node);
Matthias Braun's avatar
Matthias Braun committed
106
107
	ir_node  *ptr   = new_bd_arm_Sub_imm(dbgi, block, frame, v->values[0],
	                                     v->rors[0]);
108
	arch_set_irn_register(ptr, &arm_registers[REG_R12]);
109
110
	sched_add_before(node, ptr);

Matthias Braun's avatar
Matthias Braun committed
111
	for (unsigned cnt = 1; cnt < v->ops; ++cnt) {
112
113
		ir_node *next = new_bd_arm_Sub_imm(dbgi, block, ptr, v->values[cnt],
		                                   v->rors[cnt]);
114
		arch_set_irn_register(next, &arm_registers[REG_R12]);
115
116
117
118
119
120
		sched_add_before(node, next);
		ptr = next;
	}
	return ptr;
}

121
122
123
/** fix frame addresses which are too big */
static void peephole_arm_FrameAddr(ir_node *node)
{
124
125
126
	arm_Address_attr_t *attr   = get_arm_Address_attr(node);
	int                 offset = attr->fp_offset;
	arm_vals            v;
127
128
129
	if (allowed_arm_immediate(offset, &v))
		return;

Matthias Braun's avatar
Matthias Braun committed
130
	ir_node *base = get_irn_n(node, n_arm_FrameAddr_base);
131
	/* TODO: suboptimal */
Matthias Braun's avatar
Matthias Braun committed
132
	ir_node *ptr = gen_ptr_add(node, base, &v);
133

134
135
	attr->fp_offset = 0;
	set_irn_n(node, n_arm_FrameAddr_base, ptr);
136
137
138
}

/**
139
 * Fix stackpointer relative stores if the offset gets too big
140
 */
141
142
143
static void peephole_arm_Str_Ldr(ir_node *node)
{
	arm_load_store_attr_t *attr    = get_arm_load_store_attr(node);
Andreas Zwinkau's avatar
Andreas Zwinkau committed
144
	const int              offset  = attr->offset;
Matthias Braun's avatar
Matthias Braun committed
145
	arm_vals               v;
146
147
	if (allowed_arm_immediate(offset, &v))
		return;
148
149

	/* we should only have too big offsets for frame entities */
150
151
	if (!attr->is_frame_entity)
		be_errorf(node, "POSSIBLE ARM BACKEND PROBLEM: offset in Store too big");
Matthias Braun's avatar
Matthias Braun committed
152
	bool use_add = offset >= 0;
153

Matthias Braun's avatar
Matthias Braun committed
154
	ir_node *ptr;
155
156
	if (is_arm_Str(node)) {
		ptr = get_irn_n(node, n_arm_Str_ptr);
157
	} else {
158
159
		assert(is_arm_Ldr(node));
		ptr = get_irn_n(node, n_arm_Ldr_ptr);
160
161
	}

162
163
	if (use_add) {
		ptr = gen_ptr_add(node, ptr, &v);
164
	} else {
165
		ptr = gen_ptr_sub(node, ptr, &v);
166
167
	}

168
169
170
171
172
173
174
175
176
	/* TODO: sub-optimal, the last offset could probably be left inside the
	   store */
	if (is_arm_Str(node)) {
		set_irn_n(node, n_arm_Str_ptr, ptr);
	} else {
		assert(is_arm_Ldr(node));
		set_irn_n(node, n_arm_Ldr_ptr, ptr);
	}
	attr->offset = 0;
177
178
179
180
181
}

/**
 * Register a peephole optimization function.
 */
182
static void register_peephole_optimization(ir_op *op, peephole_opt_func func)
183
{
184
185
186
187
188
	assert(op->ops.generic == NULL);
	op->ops.generic = (op_func)func;
}

/* Perform peephole-optimizations. */
189
void arm_peephole_optimization(ir_graph *irg)
190
191
{
	/* register peephole optimizations */
Matthias Braun's avatar
Matthias Braun committed
192
	ir_clear_opcodes_generic_func();
193
194
195
196
	register_peephole_optimization(op_be_IncSP,      peephole_be_IncSP);
	register_peephole_optimization(op_arm_Str,       peephole_arm_Str_Ldr);
	register_peephole_optimization(op_arm_Ldr,       peephole_arm_Str_Ldr);
	register_peephole_optimization(op_arm_FrameAddr, peephole_arm_FrameAddr);
197

198
	be_peephole_opt(irg);
199
}