1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
/* Shorten memrefs pass for RISC-V.
Copyright (C) 2018-2022 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#define IN_TARGET_CODE 1
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "rtl.h"
#include "backend.h"
#include "regs.h"
#include "target.h"
#include "memmodel.h"
#include "emit-rtl.h"
#include "df.h"
#include "predict.h"
#include "tree-pass.h"
/* Try to make more use of compressed load and store instructions by replacing
a load/store at address BASE + LARGE_OFFSET with a new load/store at address
NEW BASE + SMALL OFFSET. If NEW BASE is stored in a compressed register, the
load/store can be compressed. Since creating NEW BASE incurs an overhead,
the change is only attempted when BASE is referenced by at least four
load/stores in the same basic block. */
namespace {
const pass_data pass_data_shorten_memrefs =
{
RTL_PASS, /* type */
"shorten_memrefs", /* name */
OPTGROUP_NONE, /* optinfo_flags */
TV_NONE, /* tv_id */
0, /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
0, /* todo_flags_finish */
};
class pass_shorten_memrefs : public rtl_opt_pass
{
public:
pass_shorten_memrefs (gcc::context *ctxt)
: rtl_opt_pass (pass_data_shorten_memrefs, ctxt)
{}
/* opt_pass methods: */
virtual bool gate (function *)
{
return TARGET_RVC && riscv_mshorten_memrefs && optimize > 0;
}
virtual unsigned int execute (function *);
private:
typedef int_hash <HOST_WIDE_INT, 0> regno_hash;
typedef hash_map <regno_hash, int> regno_map;
regno_map * analyze (basic_block bb);
void transform (regno_map *m, basic_block bb);
bool get_si_mem_base_reg (rtx mem, rtx *addr, bool *extend);
}; // class pass_shorten_memrefs
bool
pass_shorten_memrefs::get_si_mem_base_reg (rtx mem, rtx *addr, bool *extend)
{
/* Whether it's sign/zero extended. */
if (GET_CODE (mem) == ZERO_EXTEND || GET_CODE (mem) == SIGN_EXTEND)
{
*extend = true;
mem = XEXP (mem, 0);
}
if (!MEM_P (mem) || GET_MODE (mem) != SImode)
return false;
*addr = XEXP (mem, 0);
return GET_CODE (*addr) == PLUS && REG_P (XEXP (*addr, 0));
}
/* Count how many times each regno is referenced as base address for a memory
access. */
pass_shorten_memrefs::regno_map *
pass_shorten_memrefs::analyze (basic_block bb)
{
regno_map *m = hash_map<regno_hash, int>::create_ggc (10);
rtx_insn *insn;
regstat_init_n_sets_and_refs ();
FOR_BB_INSNS (bb, insn)
{
if (!NONJUMP_INSN_P (insn))
continue;
rtx pat = PATTERN (insn);
if (GET_CODE (pat) != SET)
continue;
/* Analyze stores first then loads. */
for (int i = 0; i < 2; i++)
{
rtx mem = XEXP (pat, i);
rtx addr;
bool extend = false;
if (get_si_mem_base_reg (mem, &addr, &extend))
{
HOST_WIDE_INT regno = REGNO (XEXP (addr, 0));
/* Do not count store zero as these cannot be compressed. */
if (i == 0)
{
if (XEXP (pat, 1) == CONST0_RTX (GET_MODE (XEXP (pat, 1))))
continue;
}
if (REG_N_REFS (regno) < 4)
continue;
m->get_or_insert (regno)++;
}
}
}
regstat_free_n_sets_and_refs ();
return m;
}
/* Convert BASE + LARGE_OFFSET to NEW_BASE + SMALL_OFFSET for each load/store
with a base reg referenced at least 4 times. */
void
pass_shorten_memrefs::transform (regno_map *m, basic_block bb)
{
rtx_insn *insn;
FOR_BB_INSNS (bb, insn)
{
if (!NONJUMP_INSN_P (insn))
continue;
rtx pat = PATTERN (insn);
if (GET_CODE (pat) != SET)
continue;
start_sequence ();
/* Transform stores first then loads. */
for (int i = 0; i < 2; i++)
{
rtx mem = XEXP (pat, i);
rtx addr;
bool extend = false;
if (get_si_mem_base_reg (mem, &addr, &extend))
{
HOST_WIDE_INT regno = REGNO (XEXP (addr, 0));
/* Do not transform store zero as these cannot be compressed. */
if (i == 0)
{
if (XEXP (pat, 1) == CONST0_RTX (GET_MODE (XEXP (pat, 1))))
continue;
}
if (m->get_or_insert (regno) > 3)
{
if (extend)
{
addr
= targetm.legitimize_address (addr, addr,
GET_MODE (XEXP (mem, 0)));
XEXP (XEXP (pat, i), 0)
= replace_equiv_address (XEXP (mem, 0), addr);
}
else
{
addr = targetm.legitimize_address (addr, addr,
GET_MODE (mem));
XEXP (pat, i) = replace_equiv_address (mem, addr);
}
df_insn_rescan (insn);
}
}
}
rtx_insn *seq = get_insns ();
end_sequence ();
emit_insn_before (seq, insn);
}
}
unsigned int
pass_shorten_memrefs::execute (function *fn)
{
basic_block bb;
FOR_ALL_BB_FN (bb, fn)
{
regno_map *m;
if (optimize_bb_for_speed_p (bb))
continue;
m = analyze (bb);
transform (m, bb);
}
return 0;
}
} // anon namespace
rtl_opt_pass *
make_pass_shorten_memrefs (gcc::context *ctxt)
{
return new pass_shorten_memrefs (ctxt);
}
|