annotate gcc/config/riscv/riscv-shorten-memrefs.c @ 19:2b5abeee2509 default tip

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