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