arm_optimize.c 4.51 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_t.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
80
81
82
83
84
85
86
87
88
89
90
91
typedef ir_node *new_binop_imm(dbg_info*, ir_node*, ir_node*, unsigned char, unsigned char);

static ir_node *gen_ptr(new_binop_imm *const cons, ir_node *const node, ir_node *const frame, arm_vals const *const v)
{
	dbg_info *const dbgi  = get_irn_dbg_info(node);
	ir_node  *const block = get_nodes_block(node);
	ir_node        *ptr   = frame;
	for (unsigned cnt = 0; cnt < v->ops; ++cnt) {
		ptr = cons(dbgi, block, ptr, v->values[cnt], v->rors[cnt]);
		arch_set_irn_register(ptr, &arm_registers[REG_R12]);
		sched_add_before(node, ptr);
	}
	return ptr;
}

92
93
94
/**
 * creates the address by Adds
 */
Matthias Braun's avatar
Matthias Braun committed
95
static ir_node *gen_ptr_add(ir_node *node, ir_node *frame, const arm_vals *v)
96
{
97
	return gen_ptr(&new_bd_arm_Add_imm, node, frame, v);
98
99
100
}

/**
yb9976's avatar
yb9976 committed
101
102
 * creates the address by Subs
 */
Matthias Braun's avatar
Matthias Braun committed
103
static ir_node *gen_ptr_sub(ir_node *node, ir_node *frame, const arm_vals *v)
104
{
105
	return gen_ptr(&new_bd_arm_Sub_imm, node, frame, v);
106
107
}

108
109
110
/** fix frame addresses which are too big */
static void peephole_arm_FrameAddr(ir_node *node)
{
111
112
113
	arm_Address_attr_t *attr   = get_arm_Address_attr(node);
	int                 offset = attr->fp_offset;
	arm_vals            v;
114
115
116
	if (allowed_arm_immediate(offset, &v))
		return;

Matthias Braun's avatar
Matthias Braun committed
117
	ir_node *base = get_irn_n(node, n_arm_FrameAddr_base);
118
	/* TODO: suboptimal */
Matthias Braun's avatar
Matthias Braun committed
119
	ir_node *ptr = gen_ptr_add(node, base, &v);
120

121
122
	attr->fp_offset = 0;
	set_irn_n(node, n_arm_FrameAddr_base, ptr);
123
124
125
}

/**
126
 * Fix stackpointer relative stores if the offset gets too big
127
 */
128
129
130
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
131
	const int              offset  = attr->offset;
Matthias Braun's avatar
Matthias Braun committed
132
	arm_vals               v;
133
134
	if (allowed_arm_immediate(offset, &v))
		return;
135
136

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

Matthias Braun's avatar
Matthias Braun committed
141
	ir_node *ptr;
142
143
	if (is_arm_Str(node)) {
		ptr = get_irn_n(node, n_arm_Str_ptr);
144
	} else {
145
146
		assert(is_arm_Ldr(node));
		ptr = get_irn_n(node, n_arm_Ldr_ptr);
147
148
	}

149
150
	if (use_add) {
		ptr = gen_ptr_add(node, ptr, &v);
151
	} else {
152
		ptr = gen_ptr_sub(node, ptr, &v);
153
154
	}

155
156
157
158
159
160
161
162
163
	/* 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;
164
165
166
}

/* Perform peephole-optimizations. */
167
void arm_peephole_optimization(ir_graph *irg)
168
169
{
	/* register peephole optimizations */
Matthias Braun's avatar
Matthias Braun committed
170
	ir_clear_opcodes_generic_func();
171
172
173
174
	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);
175

176
	be_peephole_opt(irg);
177
}