1 /* Detect paths through the CFG which can never be executed in a conforming
2 program and isolate them.
5 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
28 #include "basic-block.h"
31 #include "tree-ssanames.h"
32 #include "gimple-ssa.h"
33 #include "tree-ssa-operands.h"
34 #include "tree-phinodes.h"
35 #include "ssa-iterators.h"
37 #include "tree-pass.h"
40 static bool cfg_altered;
42 /* Insert a trap before SI and remove SI and all statements after SI. */
45 insert_trap_and_remove_trailing_statements (gimple_stmt_iterator *si_p)
47 gimple_seq seq = NULL;
48 gimple stmt = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
49 gimple_seq_add_stmt (&seq, stmt);
50 gsi_insert_before (si_p, seq, GSI_SAME_STMT);
52 /* Now delete all remaining statements in this block. */
53 for (; !gsi_end_p (*si_p);)
55 stmt = gsi_stmt (*si_p);
56 unlink_stmt_vdef (stmt);
57 gsi_remove (si_p, true);
62 /* BB when reached via incoming edge E will exhibit undefined behaviour
63 at STMT. Isolate and optimize the path which exhibits undefined
66 Isolation is simple. Duplicate BB and redirect E to BB'.
68 Optimization is simple as well. Replace STMT in BB' with an
69 unconditional trap and remove all outgoing edges from BB'.
71 DUPLICATE is a pre-existing duplicate, use it as BB' if it exists.
76 isolate_path (basic_block bb, basic_block duplicate, edge e, gimple stmt)
78 gimple_stmt_iterator si, si2;
83 /* First duplicate BB if we have not done so already and remove all
84 the duplicate's outgoing edges as duplicate is going to unconditionally
85 trap. Removing the outgoing edges is both an optimization and ensures
86 we don't need to do any PHI node updates. */
89 duplicate = duplicate_block (bb, NULL, NULL);
90 for (ei = ei_start (duplicate->succs); (e2 = ei_safe_edge (ei)); )
94 /* Complete the isolation step by redirecting E to reach DUPLICATE. */
95 e2 = redirect_edge_and_branch (e, duplicate);
97 flush_pending_stmts (e2);
100 /* There may be more than one statement in DUPLICATE which exhibits
101 undefined behaviour. Ultimately we want the first such statement in
102 DUPLCIATE so that we're able to delete as much code as possible.
104 So each time we discover undefined behaviour in DUPLICATE, search for
105 the statement which triggers undefined behaviour. If found, then
106 transform the statement into a trap and delete everything after the
107 statement. If not found, then this particular instance was subsumed by
108 an earlier instance of undefined behaviour and there's nothing to do.
110 This is made more complicated by the fact that we have STMT, which is in
111 BB rather than in DUPLICATE. So we set up two iterators, one for each
112 block and walk forward looking for STMT in BB, advancing each iterator at
115 When we find STMT the second iterator should point to STMT's equivalent in
116 duplicate. If DUPLICATE ends before STMT is found in BB, then there's
119 Ignore labels and debug statements. */
120 si = gsi_start_nondebug_after_labels_bb (bb);
121 si2 = gsi_start_nondebug_after_labels_bb (duplicate);
122 while (!gsi_end_p (si) && !gsi_end_p (si2) && gsi_stmt (si) != stmt)
124 gsi_next_nondebug (&si);
125 gsi_next_nondebug (&si2);
128 /* This would be an indicator that we never found STMT in BB, which should
130 gcc_assert (!gsi_end_p (si));
132 /* If we did not run to the end of DUPLICATE, then SI points to STMT and
133 SI2 points to the duplicate of STMT in DUPLICATE. Insert a trap
134 before SI2 and remove SI2 and all trailing statements. */
135 if (!gsi_end_p (si2))
136 insert_trap_and_remove_trailing_statements (&si2);
141 /* Search the function for statements which, if executed, would cause
142 the program to fault such as a dereference of a NULL pointer.
144 Such a program can't be valid if such a statement was to execute
145 according to ISO standards.
147 We detect explicit NULL pointer dereferences as well as those implied
148 by a PHI argument having a NULL value which unconditionally flows into
149 a dereference in the same block as the PHI.
151 In the former case we replace the offending statement with an
152 unconditional trap and eliminate the outgoing edges from the statement's
153 basic block. This may expose secondary optimization opportunities.
155 In the latter case, we isolate the path(s) with the NULL PHI
156 feeding the dereference. We can then replace the offending statement
157 and eliminate the outgoing edges in the duplicate. Again, this may
158 expose secondary optimization opportunities.
160 A warning for both cases may be advisable as well.
162 Other statically detectable violations of the ISO standard could be
163 handled in a similar way, such as out-of-bounds array indexing. */
166 gimple_ssa_isolate_erroneous_paths (void)
170 initialize_original_copy_tables ();
172 /* Search all the blocks for edges which, if traversed, will
173 result in undefined behaviour. */
177 gimple_stmt_iterator si;
179 /* First look for a PHI which sets a pointer to NULL and which
180 is then dereferenced within BB. This is somewhat overly
181 conservative, but probably catches most of the interesting
183 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
185 gimple phi = gsi_stmt (si);
186 tree lhs = gimple_phi_result (phi);
188 /* If the result is not a pointer, then there is no need to
189 examine the arguments. */
190 if (!POINTER_TYPE_P (TREE_TYPE (lhs)))
193 /* PHI produces a pointer result. See if any of the PHI's
196 When we remove an edge, we want to reprocess the current
197 index, hence the ugly way we update I for each iteration. */
198 basic_block duplicate = NULL;
199 for (unsigned i = 0, next_i = 0;
200 i < gimple_phi_num_args (phi);
203 tree op = gimple_phi_arg_def (phi, i);
207 if (!integer_zerop (op))
210 edge e = gimple_phi_arg_edge (phi, i);
211 imm_use_iterator iter;
214 /* We've got a NULL PHI argument. Now see if the
215 PHI's result is dereferenced within BB. */
216 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
218 /* We only care about uses in BB. Catching cases in
219 in other blocks would require more complex path
221 if (gimple_bb (use_stmt) != bb)
224 if (infer_nonnull_range (use_stmt, lhs))
226 duplicate = isolate_path (bb, duplicate,
229 /* When we remove an incoming edge, we need to
230 reprocess the Ith element. */
238 /* Now look at the statements in the block and see if any of
239 them explicitly dereference a NULL pointer. This happens
240 because of jump threading and constant propagation. */
241 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
243 gimple stmt = gsi_stmt (si);
245 /* By passing null_pointer_node, we can use infer_nonnull_range
246 to detect explicit NULL pointer dereferences and other uses
247 where a non-NULL value is required. */
248 if (infer_nonnull_range (stmt, null_pointer_node))
250 insert_trap_and_remove_trailing_statements (&si);
252 /* And finally, remove all outgoing edges from BB. */
254 for (edge_iterator ei = ei_start (bb->succs);
255 (e = ei_safe_edge (ei)); )
258 /* Ignore any more operands on this statement and
259 continue the statement iterator (which should
260 terminate its loop immediately. */
266 free_original_copy_tables ();
268 /* We scramble the CFG and loop structures a bit, clean up
269 appropriately. We really should incrementally update the
270 loop structures, in theory it shouldn't be that hard. */
273 free_dominance_info (CDI_DOMINATORS);
274 free_dominance_info (CDI_POST_DOMINATORS);
275 loops_state_set (LOOPS_NEED_FIXUP);
276 return TODO_cleanup_cfg | TODO_update_ssa;
282 gate_isolate_erroneous_paths (void)
284 /* If we do not have a suitable builtin function for the trap statement,
285 then do not perform the optimization. */
286 return (flag_isolate_erroneous_paths != 0);
290 const pass_data pass_data_isolate_erroneous_paths =
292 GIMPLE_PASS, /* type */
293 "isolate-paths", /* name */
294 OPTGROUP_NONE, /* optinfo_flags */
296 true, /* has_execute */
297 TV_ISOLATE_ERRONEOUS_PATHS, /* tv_id */
298 ( PROP_cfg | PROP_ssa ), /* properties_required */
299 0, /* properties_provided */
300 0, /* properties_destroyed */
301 0, /* todo_flags_start */
302 TODO_verify_ssa, /* todo_flags_finish */
305 class pass_isolate_erroneous_paths : public gimple_opt_pass
308 pass_isolate_erroneous_paths (gcc::context *ctxt)
309 : gimple_opt_pass (pass_data_isolate_erroneous_paths, ctxt)
312 /* opt_pass methods: */
313 opt_pass * clone () { return new pass_isolate_erroneous_paths (m_ctxt); }
314 bool gate () { return gate_isolate_erroneous_paths (); }
315 unsigned int execute () { return gimple_ssa_isolate_erroneous_paths (); }
317 }; // class pass_uncprop
321 make_pass_isolate_erroneous_paths (gcc::context *ctxt)
323 return new pass_isolate_erroneous_paths (ctxt);