invoke.texi (-fisolate-erroneous-paths): Document.
[platform/upstream/gcc.git] / gcc / gimple-ssa-isolate-paths.c
1 /* Detect paths through the CFG which can never be executed in a conforming
2    program and isolate them.
3
4    Copyright (C) 2013
5    Free Software Foundation, Inc.
6
7 This file is part of GCC.
8
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)
12 any later version.
13
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.
18
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/>.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "basic-block.h"
29 #include "gimple.h"
30 #include "tree-ssa.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"
36 #include "cfgloop.h"
37 #include "tree-pass.h"
38
39
40 static bool cfg_altered;
41
42 /* Insert a trap before SI and remove SI and all statements after SI.  */
43
44 static void
45 insert_trap_and_remove_trailing_statements (gimple_stmt_iterator *si_p)
46 {
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);
51
52   /* Now delete all remaining statements in this block.  */
53   for (; !gsi_end_p (*si_p);)
54     {
55       stmt = gsi_stmt (*si_p);
56       unlink_stmt_vdef (stmt);
57       gsi_remove (si_p, true);
58       release_defs (stmt);
59     }
60 }
61
62 /* BB when reached via incoming edge E will exhibit undefined behaviour
63    at STMT.  Isolate and optimize the path which exhibits undefined
64    behaviour.
65
66    Isolation is simple.  Duplicate BB and redirect E to BB'.
67
68    Optimization is simple as well.  Replace STMT in BB' with an
69    unconditional trap and remove all outgoing edges from BB'.
70
71    DUPLICATE is a pre-existing duplicate, use it as BB' if it exists.
72
73    Return BB'.  */
74
75 basic_block
76 isolate_path (basic_block bb, basic_block duplicate, edge e, gimple stmt)
77 {
78   gimple_stmt_iterator si, si2;
79   edge_iterator ei;
80   edge e2;
81   
82
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.  */
87   if (!duplicate)
88     {
89       duplicate = duplicate_block (bb, NULL, NULL);
90       for (ei = ei_start (duplicate->succs); (e2 = ei_safe_edge (ei)); )
91         remove_edge (e2);
92     }
93
94   /* Complete the isolation step by redirecting E to reach DUPLICATE.  */
95   e2 = redirect_edge_and_branch (e, duplicate);
96   if (e2)
97     flush_pending_stmts (e2);
98
99
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.
103
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. 
109
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
113      each step.
114
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
117      nothing to do. 
118
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)
123     {
124       gsi_next_nondebug (&si);
125       gsi_next_nondebug (&si2);
126     }
127
128   /* This would be an indicator that we never found STMT in BB, which should
129      never happen.  */
130   gcc_assert (!gsi_end_p (si));
131
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);
137
138   return duplicate;
139 }
140
141 /* Search the function for statements which, if executed, would cause
142    the program to fault such as a dereference of a NULL pointer.
143
144    Such a program can't be valid if such a statement was to execute
145    according to ISO standards.
146
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.
150
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.
154
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.
159
160    A warning for both cases may be advisable as well.
161
162    Other statically detectable violations of the ISO standard could be
163    handled in a similar way, such as out-of-bounds array indexing.  */
164
165 static unsigned int
166 gimple_ssa_isolate_erroneous_paths (void)
167 {
168   basic_block bb;
169
170   initialize_original_copy_tables ();
171
172   /* Search all the blocks for edges which, if traversed, will
173      result in undefined behaviour.  */
174   cfg_altered = false;
175   FOR_EACH_BB (bb)
176     {
177       gimple_stmt_iterator si;
178
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
182          cases.   */
183       for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
184         {
185           gimple phi = gsi_stmt (si);
186           tree lhs = gimple_phi_result (phi);
187
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)))
191             continue;
192
193           /* PHI produces a pointer result.  See if any of the PHI's
194              arguments are NULL. 
195
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);
201                i = next_i)
202             {
203               tree op = gimple_phi_arg_def (phi, i);
204
205               next_i = i + 1;
206         
207               if (!integer_zerop (op))
208                 continue;
209
210               edge e = gimple_phi_arg_edge (phi, i);
211               imm_use_iterator iter;
212               gimple use_stmt;
213
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)
217                 {
218                   /* We only care about uses in BB.  Catching cases in
219                      in other blocks would require more complex path
220                      isolation code.  */
221                   if (gimple_bb (use_stmt) != bb)
222                     continue;
223
224                   if (infer_nonnull_range (use_stmt, lhs))
225                     {
226                       duplicate = isolate_path (bb, duplicate,
227                                                 e, use_stmt);
228
229                       /* When we remove an incoming edge, we need to
230                          reprocess the Ith element.  */
231                       next_i = i;
232                       cfg_altered = true;
233                     }
234                 }
235             }
236         }
237
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))
242         {
243           gimple stmt = gsi_stmt (si);
244
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))
249             {
250               insert_trap_and_remove_trailing_statements (&si);
251
252               /* And finally, remove all outgoing edges from BB.  */
253               edge e;
254               for (edge_iterator ei = ei_start (bb->succs);
255                    (e = ei_safe_edge (ei)); )
256                 remove_edge (e);
257
258               /* Ignore any more operands on this statement and
259                  continue the statement iterator (which should
260                  terminate its loop immediately.  */
261               cfg_altered = true;
262               break;
263             }
264         }
265     }
266   free_original_copy_tables ();
267
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.  */
271   if (cfg_altered)
272     {
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;
277     }
278   return 0;
279 }
280
281 static bool
282 gate_isolate_erroneous_paths (void)
283 {
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);
287 }
288
289 namespace {
290 const pass_data pass_data_isolate_erroneous_paths =
291 {
292   GIMPLE_PASS, /* type */
293   "isolate-paths", /* name */
294   OPTGROUP_NONE, /* optinfo_flags */
295   true, /* has_gate */
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 */
303 };
304
305 class pass_isolate_erroneous_paths : public gimple_opt_pass
306 {
307 public:
308   pass_isolate_erroneous_paths (gcc::context *ctxt)
309     : gimple_opt_pass (pass_data_isolate_erroneous_paths, ctxt)
310   {}
311
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 (); }
316
317 }; // class pass_uncprop
318 }
319
320 gimple_opt_pass *
321 make_pass_isolate_erroneous_paths (gcc::context *ctxt)
322 {
323   return new pass_isolate_erroneous_paths (ctxt);
324 }