* gcc.dg/atomic/c11-atomic-exec-5.c (dg-additional-options): Use
[official-gcc.git] / gcc / gimple-ssa-isolate-paths.c
blob897b1804fa1e49549d109f2d018fd907dfab110b
1 /* Detect paths through the CFG which can never be executed in a conforming
2 program and isolate them.
4 Copyright (C) 2013-2014 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "basic-block.h"
28 #include "tree-ssa-alias.h"
29 #include "internal-fn.h"
30 #include "gimple-expr.h"
31 #include "is-a.h"
32 #include "gimple.h"
33 #include "gimple-iterator.h"
34 #include "gimple-walk.h"
35 #include "tree-ssa.h"
36 #include "stringpool.h"
37 #include "tree-ssanames.h"
38 #include "gimple-ssa.h"
39 #include "tree-ssa-operands.h"
40 #include "tree-phinodes.h"
41 #include "ssa-iterators.h"
42 #include "cfgloop.h"
43 #include "tree-pass.h"
44 #include "tree-cfg.h"
47 static bool cfg_altered;
49 /* Callback for walk_stmt_load_store_ops.
51 Return TRUE if OP will dereference the tree stored in DATA, FALSE
52 otherwise.
54 This routine only makes a superficial check for a dereference. Thus,
55 it must only be used if it is safe to return a false negative. */
56 static bool
57 check_loadstore (gimple stmt, tree op, tree, void *data)
59 if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
60 && operand_equal_p (TREE_OPERAND (op, 0), (tree)data, 0))
62 TREE_THIS_VOLATILE (op) = 1;
63 TREE_SIDE_EFFECTS (op) = 1;
64 update_stmt (stmt);
65 return true;
67 return false;
70 /* Insert a trap after SI and remove SI and all statements after the trap. */
72 static void
73 insert_trap_and_remove_trailing_statements (gimple_stmt_iterator *si_p, tree op)
75 /* We want the NULL pointer dereference to actually occur so that
76 code that wishes to catch the signal can do so.
78 If the dereference is a load, then there's nothing to do as the
79 LHS will be a throw-away SSA_NAME and the RHS is the NULL dereference.
81 If the dereference is a store and we can easily transform the RHS,
82 then simplify the RHS to enable more DCE. Note that we require the
83 statement to be a GIMPLE_ASSIGN which filters out calls on the RHS. */
84 gimple stmt = gsi_stmt (*si_p);
85 if (walk_stmt_load_store_ops (stmt, (void *)op, NULL, check_loadstore)
86 && is_gimple_assign (stmt)
87 && INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
89 /* We just need to turn the RHS into zero converted to the proper
90 type. */
91 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
92 gimple_assign_set_rhs_code (stmt, INTEGER_CST);
93 gimple_assign_set_rhs1 (stmt, fold_convert (type, integer_zero_node));
94 update_stmt (stmt);
97 gimple new_stmt
98 = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
99 gimple_seq seq = NULL;
100 gimple_seq_add_stmt (&seq, new_stmt);
102 /* If we had a NULL pointer dereference, then we want to insert the
103 __builtin_trap after the statement, for the other cases we want
104 to insert before the statement. */
105 if (walk_stmt_load_store_ops (stmt, (void *)op,
106 check_loadstore,
107 check_loadstore))
108 gsi_insert_after (si_p, seq, GSI_NEW_STMT);
109 else
110 gsi_insert_before (si_p, seq, GSI_NEW_STMT);
112 /* We must remove statements from the end of the block so that we
113 never reference a released SSA_NAME. */
114 basic_block bb = gimple_bb (gsi_stmt (*si_p));
115 for (gimple_stmt_iterator si = gsi_last_bb (bb);
116 gsi_stmt (si) != gsi_stmt (*si_p);
117 si = gsi_last_bb (bb))
119 stmt = gsi_stmt (si);
120 unlink_stmt_vdef (stmt);
121 gsi_remove (&si, true);
122 release_defs (stmt);
126 /* BB when reached via incoming edge E will exhibit undefined behaviour
127 at STMT. Isolate and optimize the path which exhibits undefined
128 behaviour.
130 Isolation is simple. Duplicate BB and redirect E to BB'.
132 Optimization is simple as well. Replace STMT in BB' with an
133 unconditional trap and remove all outgoing edges from BB'.
135 DUPLICATE is a pre-existing duplicate, use it as BB' if it exists.
137 Return BB'. */
139 basic_block
140 isolate_path (basic_block bb, basic_block duplicate,
141 edge e, gimple stmt, tree op)
143 gimple_stmt_iterator si, si2;
144 edge_iterator ei;
145 edge e2;
147 /* First duplicate BB if we have not done so already and remove all
148 the duplicate's outgoing edges as duplicate is going to unconditionally
149 trap. Removing the outgoing edges is both an optimization and ensures
150 we don't need to do any PHI node updates. */
151 if (!duplicate)
153 duplicate = duplicate_block (bb, NULL, NULL);
154 for (ei = ei_start (duplicate->succs); (e2 = ei_safe_edge (ei)); )
155 remove_edge (e2);
158 /* Complete the isolation step by redirecting E to reach DUPLICATE. */
159 e2 = redirect_edge_and_branch (e, duplicate);
160 if (e2)
161 flush_pending_stmts (e2);
164 /* There may be more than one statement in DUPLICATE which exhibits
165 undefined behaviour. Ultimately we want the first such statement in
166 DUPLCIATE so that we're able to delete as much code as possible.
168 So each time we discover undefined behaviour in DUPLICATE, search for
169 the statement which triggers undefined behaviour. If found, then
170 transform the statement into a trap and delete everything after the
171 statement. If not found, then this particular instance was subsumed by
172 an earlier instance of undefined behaviour and there's nothing to do.
174 This is made more complicated by the fact that we have STMT, which is in
175 BB rather than in DUPLICATE. So we set up two iterators, one for each
176 block and walk forward looking for STMT in BB, advancing each iterator at
177 each step.
179 When we find STMT the second iterator should point to STMT's equivalent in
180 duplicate. If DUPLICATE ends before STMT is found in BB, then there's
181 nothing to do.
183 Ignore labels and debug statements. */
184 si = gsi_start_nondebug_after_labels_bb (bb);
185 si2 = gsi_start_nondebug_after_labels_bb (duplicate);
186 while (!gsi_end_p (si) && !gsi_end_p (si2) && gsi_stmt (si) != stmt)
188 gsi_next_nondebug (&si);
189 gsi_next_nondebug (&si2);
192 /* This would be an indicator that we never found STMT in BB, which should
193 never happen. */
194 gcc_assert (!gsi_end_p (si));
196 /* If we did not run to the end of DUPLICATE, then SI points to STMT and
197 SI2 points to the duplicate of STMT in DUPLICATE. Insert a trap
198 before SI2 and remove SI2 and all trailing statements. */
199 if (!gsi_end_p (si2))
200 insert_trap_and_remove_trailing_statements (&si2, op);
202 return duplicate;
205 /* Look for PHI nodes which feed statements in the same block where
206 the value of the PHI node implies the statement is erroneous.
208 For example, a NULL PHI arg value which then feeds a pointer
209 dereference.
211 When found isolate and optimize the path associated with the PHI
212 argument feeding the erroneous statement. */
213 static void
214 find_implicit_erroneous_behaviour (void)
216 basic_block bb;
218 FOR_EACH_BB_FN (bb, cfun)
220 gimple_stmt_iterator si;
222 /* Out of an abundance of caution, do not isolate paths to a
223 block where the block has any abnormal outgoing edges.
225 We might be able to relax this in the future. We have to detect
226 when we have to split the block with the NULL dereference and
227 the trap we insert. We have to preserve abnormal edges out
228 of the isolated block which in turn means updating PHIs at
229 the targets of those abnormal outgoing edges. */
230 if (has_abnormal_or_eh_outgoing_edge_p (bb))
231 continue;
233 /* First look for a PHI which sets a pointer to NULL and which
234 is then dereferenced within BB. This is somewhat overly
235 conservative, but probably catches most of the interesting
236 cases. */
237 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
239 gimple phi = gsi_stmt (si);
240 tree lhs = gimple_phi_result (phi);
242 /* If the result is not a pointer, then there is no need to
243 examine the arguments. */
244 if (!POINTER_TYPE_P (TREE_TYPE (lhs)))
245 continue;
247 /* PHI produces a pointer result. See if any of the PHI's
248 arguments are NULL.
250 When we remove an edge, we want to reprocess the current
251 index, hence the ugly way we update I for each iteration. */
252 basic_block duplicate = NULL;
253 for (unsigned i = 0, next_i = 0;
254 i < gimple_phi_num_args (phi);
255 i = next_i)
257 tree op = gimple_phi_arg_def (phi, i);
259 next_i = i + 1;
261 if (!integer_zerop (op))
262 continue;
264 edge e = gimple_phi_arg_edge (phi, i);
265 imm_use_iterator iter;
266 gimple use_stmt;
268 /* We've got a NULL PHI argument. Now see if the
269 PHI's result is dereferenced within BB. */
270 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
272 /* We only care about uses in BB. Catching cases in
273 in other blocks would require more complex path
274 isolation code. */
275 if (gimple_bb (use_stmt) != bb)
276 continue;
278 if (infer_nonnull_range (use_stmt, lhs,
279 flag_isolate_erroneous_paths_dereference,
280 flag_isolate_erroneous_paths_attribute))
283 duplicate = isolate_path (bb, duplicate,
284 e, use_stmt, lhs);
286 /* When we remove an incoming edge, we need to
287 reprocess the Ith element. */
288 next_i = i;
289 cfg_altered = true;
297 /* Look for statements which exhibit erroneous behaviour. For example
298 a NULL pointer dereference.
300 When found, optimize the block containing the erroneous behaviour. */
301 static void
302 find_explicit_erroneous_behaviour (void)
304 basic_block bb;
306 FOR_EACH_BB_FN (bb, cfun)
308 gimple_stmt_iterator si;
310 /* Out of an abundance of caution, do not isolate paths to a
311 block where the block has any abnormal outgoing edges.
313 We might be able to relax this in the future. We have to detect
314 when we have to split the block with the NULL dereference and
315 the trap we insert. We have to preserve abnormal edges out
316 of the isolated block which in turn means updating PHIs at
317 the targets of those abnormal outgoing edges. */
318 if (has_abnormal_or_eh_outgoing_edge_p (bb))
319 continue;
321 /* Now look at the statements in the block and see if any of
322 them explicitly dereference a NULL pointer. This happens
323 because of jump threading and constant propagation. */
324 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
326 gimple stmt = gsi_stmt (si);
328 /* By passing null_pointer_node, we can use infer_nonnull_range
329 to detect explicit NULL pointer dereferences and other uses
330 where a non-NULL value is required. */
331 if (infer_nonnull_range (stmt, null_pointer_node,
332 flag_isolate_erroneous_paths_dereference,
333 flag_isolate_erroneous_paths_attribute))
335 insert_trap_and_remove_trailing_statements (&si,
336 null_pointer_node);
338 /* And finally, remove all outgoing edges from BB. */
339 edge e;
340 for (edge_iterator ei = ei_start (bb->succs);
341 (e = ei_safe_edge (ei)); )
342 remove_edge (e);
344 /* Ignore any more operands on this statement and
345 continue the statement iterator (which should
346 terminate its loop immediately. */
347 cfg_altered = true;
348 break;
353 /* Search the function for statements which, if executed, would cause
354 the program to fault such as a dereference of a NULL pointer.
356 Such a program can't be valid if such a statement was to execute
357 according to ISO standards.
359 We detect explicit NULL pointer dereferences as well as those implied
360 by a PHI argument having a NULL value which unconditionally flows into
361 a dereference in the same block as the PHI.
363 In the former case we replace the offending statement with an
364 unconditional trap and eliminate the outgoing edges from the statement's
365 basic block. This may expose secondary optimization opportunities.
367 In the latter case, we isolate the path(s) with the NULL PHI
368 feeding the dereference. We can then replace the offending statement
369 and eliminate the outgoing edges in the duplicate. Again, this may
370 expose secondary optimization opportunities.
372 A warning for both cases may be advisable as well.
374 Other statically detectable violations of the ISO standard could be
375 handled in a similar way, such as out-of-bounds array indexing. */
377 static unsigned int
378 gimple_ssa_isolate_erroneous_paths (void)
380 initialize_original_copy_tables ();
382 /* Search all the blocks for edges which, if traversed, will
383 result in undefined behaviour. */
384 cfg_altered = false;
386 /* First handle cases where traversal of a particular edge
387 triggers undefined behaviour. These cases require creating
388 duplicate blocks and thus new SSA_NAMEs.
390 We want that process complete prior to the phase where we start
391 removing edges from the CFG. Edge removal may ultimately result in
392 removal of PHI nodes and thus releasing SSA_NAMEs back to the
393 name manager.
395 If the two processes run in parallel we could release an SSA_NAME
396 back to the manager but we could still have dangling references
397 to the released SSA_NAME in unreachable blocks.
398 that any released names not have dangling references in the IL. */
399 find_implicit_erroneous_behaviour ();
400 find_explicit_erroneous_behaviour ();
402 free_original_copy_tables ();
404 /* We scramble the CFG and loop structures a bit, clean up
405 appropriately. We really should incrementally update the
406 loop structures, in theory it shouldn't be that hard. */
407 if (cfg_altered)
409 free_dominance_info (CDI_DOMINATORS);
410 free_dominance_info (CDI_POST_DOMINATORS);
411 loops_state_set (LOOPS_NEED_FIXUP);
412 return TODO_cleanup_cfg | TODO_update_ssa;
414 return 0;
417 namespace {
418 const pass_data pass_data_isolate_erroneous_paths =
420 GIMPLE_PASS, /* type */
421 "isolate-paths", /* name */
422 OPTGROUP_NONE, /* optinfo_flags */
423 TV_ISOLATE_ERRONEOUS_PATHS, /* tv_id */
424 ( PROP_cfg | PROP_ssa ), /* properties_required */
425 0, /* properties_provided */
426 0, /* properties_destroyed */
427 0, /* todo_flags_start */
428 0, /* todo_flags_finish */
431 class pass_isolate_erroneous_paths : public gimple_opt_pass
433 public:
434 pass_isolate_erroneous_paths (gcc::context *ctxt)
435 : gimple_opt_pass (pass_data_isolate_erroneous_paths, ctxt)
438 /* opt_pass methods: */
439 opt_pass * clone () { return new pass_isolate_erroneous_paths (m_ctxt); }
440 virtual bool gate (function *)
442 /* If we do not have a suitable builtin function for the trap statement,
443 then do not perform the optimization. */
444 return (flag_isolate_erroneous_paths_dereference != 0
445 || flag_isolate_erroneous_paths_attribute != 0);
448 virtual unsigned int execute (function *)
450 return gimple_ssa_isolate_erroneous_paths ();
453 }; // class pass_isolate_erroneous_paths
456 gimple_opt_pass *
457 make_pass_isolate_erroneous_paths (gcc::context *ctxt)
459 return new pass_isolate_erroneous_paths (ctxt);