annotate gcc/config/mips/frame-header-opt.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Analyze functions to determine if callers need to allocate a frame header
kono
parents:
diff changeset
2 on the stack. The frame header is used by callees to save their arguments.
kono
parents:
diff changeset
3 This optimization is specific to TARGET_OLDABI targets. For TARGET_NEWABI
kono
parents:
diff changeset
4 targets, if a frame header is required, it is allocated by the callee.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
7 Copyright (C) 2015-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 This file is part of GCC.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is free software; you can redistribute it and/or modify it
kono
parents:
diff changeset
12 under the terms of the GNU General Public License as published by the
kono
parents:
diff changeset
13 Free Software Foundation; either version 3, or (at your option) any
kono
parents:
diff changeset
14 later version.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 GCC is distributed in the hope that it will be useful, but WITHOUT
kono
parents:
diff changeset
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
19 for more details.
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
22 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
23 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
26 #define IN_TARGET_CODE 1
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
27
111
kono
parents:
diff changeset
28 #include "config.h"
kono
parents:
diff changeset
29 #include "system.h"
kono
parents:
diff changeset
30 #include "context.h"
kono
parents:
diff changeset
31 #include "coretypes.h"
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
32 #include "backend.h"
111
kono
parents:
diff changeset
33 #include "tree.h"
kono
parents:
diff changeset
34 #include "tree-core.h"
kono
parents:
diff changeset
35 #include "tree-pass.h"
kono
parents:
diff changeset
36 #include "target.h"
kono
parents:
diff changeset
37 #include "target-globals.h"
kono
parents:
diff changeset
38 #include "profile-count.h"
kono
parents:
diff changeset
39 #include "cgraph.h"
kono
parents:
diff changeset
40 #include "function.h"
kono
parents:
diff changeset
41 #include "basic-block.h"
kono
parents:
diff changeset
42 #include "gimple.h"
kono
parents:
diff changeset
43 #include "gimple-iterator.h"
kono
parents:
diff changeset
44 #include "gimple-walk.h"
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 static unsigned int frame_header_opt (void);
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 namespace {
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 const pass_data pass_data_ipa_frame_header_opt =
kono
parents:
diff changeset
51 {
kono
parents:
diff changeset
52 IPA_PASS, /* type */
kono
parents:
diff changeset
53 "frame-header-opt", /* name */
kono
parents:
diff changeset
54 OPTGROUP_NONE, /* optinfo_flags */
kono
parents:
diff changeset
55 TV_CGRAPHOPT, /* tv_id */
kono
parents:
diff changeset
56 0, /* properties_required */
kono
parents:
diff changeset
57 0, /* properties_provided */
kono
parents:
diff changeset
58 0, /* properties_destroyed */
kono
parents:
diff changeset
59 0, /* todo_flags_start */
kono
parents:
diff changeset
60 0, /* todo_flags_finish */
kono
parents:
diff changeset
61 };
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 class pass_ipa_frame_header_opt : public ipa_opt_pass_d
kono
parents:
diff changeset
64 {
kono
parents:
diff changeset
65 public:
kono
parents:
diff changeset
66 pass_ipa_frame_header_opt (gcc::context *ctxt)
kono
parents:
diff changeset
67 : ipa_opt_pass_d (pass_data_ipa_frame_header_opt, ctxt,
kono
parents:
diff changeset
68 NULL, /* generate_summary */
kono
parents:
diff changeset
69 NULL, /* write_summary */
kono
parents:
diff changeset
70 NULL, /* read_summary */
kono
parents:
diff changeset
71 NULL, /* write_optimization_summary */
kono
parents:
diff changeset
72 NULL, /* read_optimization_summary */
kono
parents:
diff changeset
73 NULL, /* stmt_fixup */
kono
parents:
diff changeset
74 0, /* function_transform_todo_flags_start */
kono
parents:
diff changeset
75 NULL, /* function_transform */
kono
parents:
diff changeset
76 NULL) /* variable_transform */
kono
parents:
diff changeset
77 {}
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 /* opt_pass methods: */
kono
parents:
diff changeset
80 virtual bool gate (function *)
kono
parents:
diff changeset
81 {
kono
parents:
diff changeset
82 /* This optimization has no affect if TARGET_NEWABI. If optimize
kono
parents:
diff changeset
83 is not at least 1 then the data needed for the optimization is
kono
parents:
diff changeset
84 not available and nothing will be done anyway. */
kono
parents:
diff changeset
85 return TARGET_OLDABI && flag_frame_header_optimization && optimize > 0;
kono
parents:
diff changeset
86 }
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 virtual unsigned int execute (function *) { return frame_header_opt (); }
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 }; // class pass_ipa_frame_header_opt
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 } // anon namespace
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 static ipa_opt_pass_d *
kono
parents:
diff changeset
95 make_pass_ipa_frame_header_opt (gcc::context *ctxt)
kono
parents:
diff changeset
96 {
kono
parents:
diff changeset
97 return new pass_ipa_frame_header_opt (ctxt);
kono
parents:
diff changeset
98 }
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 void
kono
parents:
diff changeset
101 mips_register_frame_header_opt (void)
kono
parents:
diff changeset
102 {
kono
parents:
diff changeset
103 opt_pass *p = make_pass_ipa_frame_header_opt (g);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
104 struct register_pass_info f = { p, "comdats", 1, PASS_POS_INSERT_AFTER };
111
kono
parents:
diff changeset
105 register_pass (&f);
kono
parents:
diff changeset
106 }
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 /* Return true if it is certain that this is a leaf function. False if it is
kono
parents:
diff changeset
110 not a leaf function or if it is impossible to tell. */
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 static bool
kono
parents:
diff changeset
113 is_leaf_function (function *fn)
kono
parents:
diff changeset
114 {
kono
parents:
diff changeset
115 basic_block bb;
kono
parents:
diff changeset
116 gimple_stmt_iterator gsi;
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 /* If we do not have a cfg for this function be conservative and assume
kono
parents:
diff changeset
119 it is not a leaf function. */
kono
parents:
diff changeset
120 if (fn->cfg == NULL)
kono
parents:
diff changeset
121 return false;
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 FOR_EACH_BB_FN (bb, fn)
kono
parents:
diff changeset
124 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
kono
parents:
diff changeset
125 if (is_gimple_call (gsi_stmt (gsi)))
kono
parents:
diff changeset
126 return false;
kono
parents:
diff changeset
127 return true;
kono
parents:
diff changeset
128 }
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 /* Return true if this function has inline assembly code or if we cannot
kono
parents:
diff changeset
131 be certain that it does not. False if we know that there is no inline
kono
parents:
diff changeset
132 assembly. */
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 static bool
kono
parents:
diff changeset
135 has_inlined_assembly (function *fn)
kono
parents:
diff changeset
136 {
kono
parents:
diff changeset
137 basic_block bb;
kono
parents:
diff changeset
138 gimple_stmt_iterator gsi;
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 /* If we do not have a cfg for this function be conservative and assume
kono
parents:
diff changeset
141 it is may have inline assembly. */
kono
parents:
diff changeset
142 if (fn->cfg == NULL)
kono
parents:
diff changeset
143 return true;
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 FOR_EACH_BB_FN (bb, fn)
kono
parents:
diff changeset
146 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
kono
parents:
diff changeset
147 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_ASM)
kono
parents:
diff changeset
148 return true;
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 return false;
kono
parents:
diff changeset
151 }
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 /* Return true if this function will use the stack space allocated by its
kono
parents:
diff changeset
154 caller or if we cannot determine for certain that it does not. */
kono
parents:
diff changeset
155
kono
parents:
diff changeset
156 static bool
kono
parents:
diff changeset
157 needs_frame_header_p (function *fn)
kono
parents:
diff changeset
158 {
kono
parents:
diff changeset
159 tree t;
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 if (fn->decl == NULL)
kono
parents:
diff changeset
162 return true;
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 if (fn->stdarg)
kono
parents:
diff changeset
165 return true;
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 for (t = DECL_ARGUMENTS (fn->decl); t; t = TREE_CHAIN (t))
kono
parents:
diff changeset
168 {
kono
parents:
diff changeset
169 if (!use_register_for_decl (t))
kono
parents:
diff changeset
170 return true;
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 /* Some 64-bit types may get copied to general registers using the frame
kono
parents:
diff changeset
173 header, see mips_output_64bit_xfer. Checking for SImode only may be
kono
parents:
diff changeset
174 overly restrictive but it is guaranteed to be safe. */
kono
parents:
diff changeset
175 if (DECL_MODE (t) != SImode)
kono
parents:
diff changeset
176 return true;
kono
parents:
diff changeset
177 }
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 return false;
kono
parents:
diff changeset
180 }
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 /* Return true if the argument stack space allocated by function FN is used.
kono
parents:
diff changeset
183 Return false if the space is needed or if the need for the space cannot
kono
parents:
diff changeset
184 be determined. */
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 static bool
kono
parents:
diff changeset
187 callees_functions_use_frame_header (function *fn)
kono
parents:
diff changeset
188 {
kono
parents:
diff changeset
189 basic_block bb;
kono
parents:
diff changeset
190 gimple_stmt_iterator gsi;
kono
parents:
diff changeset
191 gimple *stmt;
kono
parents:
diff changeset
192 tree called_fn_tree;
kono
parents:
diff changeset
193 function *called_fn;
kono
parents:
diff changeset
194
kono
parents:
diff changeset
195 if (fn->cfg == NULL)
kono
parents:
diff changeset
196 return true;
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 FOR_EACH_BB_FN (bb, fn)
kono
parents:
diff changeset
199 {
kono
parents:
diff changeset
200 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
kono
parents:
diff changeset
201 {
kono
parents:
diff changeset
202 stmt = gsi_stmt (gsi);
kono
parents:
diff changeset
203 if (is_gimple_call (stmt))
kono
parents:
diff changeset
204 {
kono
parents:
diff changeset
205 called_fn_tree = gimple_call_fndecl (stmt);
kono
parents:
diff changeset
206 if (called_fn_tree != NULL)
kono
parents:
diff changeset
207 {
kono
parents:
diff changeset
208 called_fn = DECL_STRUCT_FUNCTION (called_fn_tree);
kono
parents:
diff changeset
209 if (called_fn == NULL
kono
parents:
diff changeset
210 || DECL_WEAK (called_fn_tree)
kono
parents:
diff changeset
211 || has_inlined_assembly (called_fn)
kono
parents:
diff changeset
212 || !is_leaf_function (called_fn)
kono
parents:
diff changeset
213 || !called_fn->machine->does_not_use_frame_header)
kono
parents:
diff changeset
214 return true;
kono
parents:
diff changeset
215 }
kono
parents:
diff changeset
216 else
kono
parents:
diff changeset
217 return true;
kono
parents:
diff changeset
218 }
kono
parents:
diff changeset
219 }
kono
parents:
diff changeset
220 }
kono
parents:
diff changeset
221 return false;
kono
parents:
diff changeset
222 }
kono
parents:
diff changeset
223
kono
parents:
diff changeset
224 /* Set the callers_may_not_allocate_frame flag for any function which
kono
parents:
diff changeset
225 function FN calls because FN may not allocate a frame header. */
kono
parents:
diff changeset
226
kono
parents:
diff changeset
227 static void
kono
parents:
diff changeset
228 set_callers_may_not_allocate_frame (function *fn)
kono
parents:
diff changeset
229 {
kono
parents:
diff changeset
230 basic_block bb;
kono
parents:
diff changeset
231 gimple_stmt_iterator gsi;
kono
parents:
diff changeset
232 gimple *stmt;
kono
parents:
diff changeset
233 tree called_fn_tree;
kono
parents:
diff changeset
234 function *called_fn;
kono
parents:
diff changeset
235
kono
parents:
diff changeset
236 if (fn->cfg == NULL)
kono
parents:
diff changeset
237 return;
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 FOR_EACH_BB_FN (bb, fn)
kono
parents:
diff changeset
240 {
kono
parents:
diff changeset
241 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
kono
parents:
diff changeset
242 {
kono
parents:
diff changeset
243 stmt = gsi_stmt (gsi);
kono
parents:
diff changeset
244 if (is_gimple_call (stmt))
kono
parents:
diff changeset
245 {
kono
parents:
diff changeset
246 called_fn_tree = gimple_call_fndecl (stmt);
kono
parents:
diff changeset
247 if (called_fn_tree != NULL)
kono
parents:
diff changeset
248 {
kono
parents:
diff changeset
249 called_fn = DECL_STRUCT_FUNCTION (called_fn_tree);
kono
parents:
diff changeset
250 if (called_fn != NULL)
kono
parents:
diff changeset
251 called_fn->machine->callers_may_not_allocate_frame = true;
kono
parents:
diff changeset
252 }
kono
parents:
diff changeset
253 }
kono
parents:
diff changeset
254 }
kono
parents:
diff changeset
255 }
kono
parents:
diff changeset
256 return;
kono
parents:
diff changeset
257 }
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 /* Scan each function to determine those that need its frame headers. Perform
kono
parents:
diff changeset
260 a second scan to determine if the allocation can be skipped because none of
kono
parents:
diff changeset
261 their callees require the frame header. */
kono
parents:
diff changeset
262
kono
parents:
diff changeset
263 static unsigned int
kono
parents:
diff changeset
264 frame_header_opt ()
kono
parents:
diff changeset
265 {
kono
parents:
diff changeset
266 struct cgraph_node *node;
kono
parents:
diff changeset
267 function *fn;
kono
parents:
diff changeset
268
kono
parents:
diff changeset
269 FOR_EACH_DEFINED_FUNCTION (node)
kono
parents:
diff changeset
270 {
kono
parents:
diff changeset
271 fn = node->get_fun ();
kono
parents:
diff changeset
272 if (fn != NULL)
kono
parents:
diff changeset
273 fn->machine->does_not_use_frame_header = !needs_frame_header_p (fn);
kono
parents:
diff changeset
274 }
kono
parents:
diff changeset
275
kono
parents:
diff changeset
276 FOR_EACH_DEFINED_FUNCTION (node)
kono
parents:
diff changeset
277 {
kono
parents:
diff changeset
278 fn = node->get_fun ();
kono
parents:
diff changeset
279 if (fn != NULL)
kono
parents:
diff changeset
280 fn->machine->optimize_call_stack
kono
parents:
diff changeset
281 = !callees_functions_use_frame_header (fn) && !is_leaf_function (fn);
kono
parents:
diff changeset
282 }
kono
parents:
diff changeset
283
kono
parents:
diff changeset
284 FOR_EACH_DEFINED_FUNCTION (node)
kono
parents:
diff changeset
285 {
kono
parents:
diff changeset
286 fn = node->get_fun ();
kono
parents:
diff changeset
287 if (fn != NULL && fn->machine->optimize_call_stack)
kono
parents:
diff changeset
288 set_callers_may_not_allocate_frame (fn);
kono
parents:
diff changeset
289 }
kono
parents:
diff changeset
290
kono
parents:
diff changeset
291 return 0;
kono
parents:
diff changeset
292 }