annotate gcc/config/mips/frame-header-opt.c @ 111:04ced10e8804

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