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