Line data Source code
1 : /* Predicate aware uninitialized variable warning.
2 : Copyright (C) 2001-2026 Free Software Foundation, Inc.
3 : Contributed by Xinliang David Li <davidxl@google.com>
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #define INCLUDE_STRING
22 : #include "config.h"
23 : #include "system.h"
24 : #include "coretypes.h"
25 : #include "backend.h"
26 : #include "tree.h"
27 : #include "gimple.h"
28 : #include "tree-pass.h"
29 : #include "ssa.h"
30 : #include "gimple-pretty-print.h"
31 : #include "diagnostic-core.h"
32 : #include "fold-const.h"
33 : #include "gimple-iterator.h"
34 : #include "tree-ssa.h"
35 : #include "tree-cfg.h"
36 : #include "cfghooks.h"
37 : #include "attribs.h"
38 : #include "builtins.h"
39 : #include "calls.h"
40 : #include "gimple-range.h"
41 : #include "gimple-predicate-analysis.h"
42 : #include "domwalk.h"
43 : #include "tree-ssa-sccvn.h"
44 : #include "cfganal.h"
45 : #include "gcc-urlifier.h"
46 :
47 : /* This implements the pass that does predicate aware warning on uses of
48 : possibly uninitialized variables. The pass first collects the set of
49 : possibly uninitialized SSA names. For each such name, it walks through
50 : all its immediate uses. For each immediate use, it rebuilds the condition
51 : expression (the predicate) that guards the use. The predicate is then
52 : examined to see if the variable is always defined under that same condition.
53 : This is done either by pruning the unrealizable paths that lead to the
54 : default definitions or by checking if the predicate set that guards the
55 : defining paths is a superset of the use predicate. */
56 :
57 : /* Pointer set of potentially undefined ssa names, i.e.,
58 : ssa names that are defined by phi with operands that
59 : are not defined or potentially undefined. */
60 : static hash_set<tree> *possibly_undefined_names;
61 : static hash_map<gphi *, uninit_analysis::func_t::phi_arg_set_t> *defined_args;
62 :
63 : /* Returns the first bit position (starting from LSB)
64 : in mask that is non zero. Returns -1 if the mask is empty. */
65 : static int
66 111 : get_mask_first_set_bit (unsigned mask)
67 : {
68 111 : int pos = 0;
69 0 : if (mask == 0)
70 : return -1;
71 :
72 140 : while ((mask & (1 << pos)) == 0)
73 29 : pos++;
74 :
75 : return pos;
76 : }
77 : #define MASK_FIRST_SET_BIT(mask) get_mask_first_set_bit (mask)
78 :
79 : /* Return true if T, an SSA_NAME, has an undefined value. */
80 : static bool
81 6633542 : has_undefined_value_p (tree t)
82 : {
83 6633542 : return (ssa_undefined_value_p (t)
84 6626044 : || gimple_call_internal_p (SSA_NAME_DEF_STMT (t), IFN_DEFERRED_INIT)
85 13259263 : || (possibly_undefined_names
86 920894 : && possibly_undefined_names->contains (t)));
87 : }
88 :
89 : /* Return true if EXPR should suppress either uninitialized warning. */
90 :
91 : static inline bool
92 3151411 : get_no_uninit_warning (tree expr)
93 : {
94 1482509 : return warning_suppressed_p (expr, OPT_Wuninitialized);
95 : }
96 :
97 : /* Suppress both uninitialized warnings for EXPR. */
98 :
99 : static inline void
100 405 : set_no_uninit_warning (tree expr)
101 : {
102 405 : suppress_warning (expr, OPT_Wuninitialized);
103 : }
104 :
105 : /* Like has_undefined_value_p, but don't return true if the no-warning
106 : bit is set on SSA_NAME_VAR for either uninit warning. */
107 :
108 : static inline bool
109 923952 : uninit_undefined_value_p (tree t)
110 : {
111 923952 : if (!has_undefined_value_p (t))
112 : return false;
113 5988 : if (!SSA_NAME_VAR (t))
114 : return true;
115 5963 : return !get_no_uninit_warning (SSA_NAME_VAR (t));
116 : }
117 :
118 : /* Emit warnings for uninitialized variables. This is done in two passes.
119 :
120 : The first pass notices real uses of SSA names with undefined values.
121 : Such uses are unconditionally uninitialized, and we can be certain that
122 : such a use is a mistake. This pass is run before most optimizations,
123 : so that we catch as many as we can.
124 :
125 : The second pass follows PHI nodes to find uses that are potentially
126 : uninitialized. In this case we can't necessarily prove that the use
127 : is really uninitialized. This pass is run after most optimizations,
128 : so that we thread as many jumps and possible, and delete as much dead
129 : code as possible, in order to reduce false positives. We also look
130 : again for plain uninitialized variables, since optimization may have
131 : changed conditionally uninitialized to unconditionally uninitialized. */
132 :
133 : /* Emit warning OPT for variable VAR at the point in the program where
134 : the SSA_NAME T is being used uninitialized. The warning text is in
135 : MSGID and STMT is the statement that does the uninitialized read.
136 : PHI_ARG_LOC is the location of the PHI argument if T and VAR are one,
137 : or UNKNOWN_LOCATION otherwise. */
138 :
139 : static void
140 5709587 : warn_uninit (opt_code opt, tree t, tree var, gimple *context,
141 : location_t phi_arg_loc = UNKNOWN_LOCATION)
142 : {
143 : /* Bail if the value isn't provably uninitialized. */
144 5709587 : if (!has_undefined_value_p (t))
145 5709015 : return;
146 :
147 : /* Ignore COMPLEX_EXPR as initializing only a part of a complex
148 : turns in a COMPLEX_EXPR with the not initialized part being
149 : set to its previous (undefined) value. */
150 4760 : if (is_gimple_assign (context)
151 4760 : && gimple_assign_rhs_code (context) == COMPLEX_EXPR)
152 : return;
153 :
154 : /* Ignore REALPART_EXPR or IMAGPART_EXPR if its operand is a call to
155 : .DEFERRED_INIT. This is for handling the following case correctly:
156 :
157 : 1 typedef _Complex float C;
158 : 2 C foo (int cond)
159 : 3 {
160 : 4 C f;
161 : 5 __imag__ f = 0;
162 : 6 if (cond)
163 : 7 {
164 : 8 __real__ f = 1;
165 : 9 return f;
166 : 10 }
167 : 11 return f;
168 : 12 }
169 :
170 : with -ftrivial-auto-var-init, compiler will insert the following
171 : artificial initialization at line 4:
172 : f = .DEFERRED_INIT (f, 2);
173 : _1 = REALPART_EXPR <f>;
174 :
175 : without the following special handling, _1 = REALPART_EXPR <f> will
176 : be treated as the uninitialized use point, which is incorrect. (the
177 : real uninitialized use point is at line 11). */
178 4750 : auto assign = dyn_cast<gassign *> (context);
179 4175 : if (assign
180 4175 : && (gimple_assign_rhs_code (assign) == REALPART_EXPR
181 4168 : || gimple_assign_rhs_code (assign) == IMAGPART_EXPR))
182 : {
183 95 : tree v = gimple_assign_rhs1 (assign);
184 95 : if (TREE_CODE (TREE_OPERAND (v, 0)) == SSA_NAME
185 95 : && gimple_call_internal_p (SSA_NAME_DEF_STMT (TREE_OPERAND (v, 0)),
186 : IFN_DEFERRED_INIT))
187 : return;
188 : }
189 :
190 : /* Anonymous SSA_NAMEs shouldn't be uninitialized, but ssa_undefined_value_p
191 : can return true if the def stmt of an anonymous SSA_NAME is
192 : 1. A COMPLEX_EXPR created for conversion from scalar to complex. Use the
193 : underlying var of the COMPLEX_EXPRs real part in that case. See PR71581.
194 :
195 : Or
196 :
197 : 2. A call to .DEFERRED_INIT internal function. Since the original variable
198 : has been eliminated by optimization, we need to get the variable name,
199 : and variable declaration location from this call. We recorded variable
200 : name into VAR_NAME_STR, and will get location info and record warning
201 : suppressed info to VAR_DEF_STMT, which is the .DEFERRED_INIT call. */
202 :
203 4745 : const char *var_name_str = NULL;
204 4745 : gimple *var_def_stmt = NULL;
205 :
206 4995 : if (!var && !SSA_NAME_VAR (t))
207 : {
208 250 : var_def_stmt = SSA_NAME_DEF_STMT (t);
209 :
210 250 : if (gassign *ass = dyn_cast <gassign *> (var_def_stmt))
211 : {
212 7 : switch (gimple_assign_rhs_code (var_def_stmt))
213 : {
214 3 : case COMPLEX_EXPR:
215 3 : {
216 3 : tree v = gimple_assign_rhs1 (ass);
217 3 : if (TREE_CODE (v) == SSA_NAME
218 3 : && has_undefined_value_p (v)
219 6 : && zerop (gimple_assign_rhs2 (ass)))
220 3 : var = SSA_NAME_VAR (v);
221 : break;
222 : }
223 4 : case SSA_NAME:
224 4 : {
225 4 : tree v = gimple_assign_rhs1 (ass);
226 4 : if (TREE_CODE (v) == SSA_NAME
227 4 : && SSA_NAME_VAR (v))
228 : var = SSA_NAME_VAR (v);
229 : break;
230 : }
231 : default:;
232 : }
233 : }
234 :
235 250 : if (gimple_call_internal_p (var_def_stmt, IFN_DEFERRED_INIT))
236 : {
237 : /* Ignore the call to .DEFERRED_INIT that define the original
238 : var itself as the following case:
239 : temp = .DEFERRED_INIT (4, 2, “alt_reloc");
240 : alt_reloc = temp;
241 : In order to avoid generating warning for the fake usage
242 : at alt_reloc = temp.
243 : */
244 243 : tree lhs_var = NULL_TREE;
245 :
246 : /* Get the variable name from the 3rd argument of call. */
247 243 : tree var_name = gimple_call_arg (var_def_stmt, 2);
248 243 : var_name = TREE_OPERAND (TREE_OPERAND (var_name, 0), 0);
249 243 : var_name_str = TREE_STRING_POINTER (var_name);
250 :
251 243 : if (auto a_context = dyn_cast<gassign *> (context))
252 : {
253 236 : if (VAR_P (gimple_assign_lhs (a_context)))
254 : lhs_var = gimple_assign_lhs (a_context);
255 17 : else if (TREE_CODE (gimple_assign_lhs (a_context)) == SSA_NAME)
256 17 : lhs_var = SSA_NAME_VAR (gimple_assign_lhs (a_context));
257 : }
258 234 : if (lhs_var)
259 : {
260 : /* Get the name string for the LHS_VAR.
261 : Refer to routine gimple_add_init_for_auto_var. */
262 234 : if (DECL_NAME (lhs_var)
263 234 : && (strcmp (IDENTIFIER_POINTER (DECL_NAME (lhs_var)),
264 : var_name_str) == 0))
265 : return;
266 8 : else if (!DECL_NAME (lhs_var))
267 : {
268 6 : char lhs_var_name_str_buf[3 + (HOST_BITS_PER_INT + 2) / 3];
269 6 : sprintf (lhs_var_name_str_buf, "D.%u", DECL_UID (lhs_var));
270 6 : if (strcmp (lhs_var_name_str_buf, var_name_str) == 0)
271 6 : return;
272 : }
273 : }
274 : gcc_assert (var_name_str && var_def_stmt);
275 : }
276 : }
277 :
278 4513 : if (var == NULL_TREE && var_name_str == NULL)
279 : return;
280 :
281 : /* Avoid warning if we've already done so or if the warning has been
282 : suppressed. */
283 4513 : if (((warning_suppressed_p (context, OPT_Wuninitialized)
284 4506 : || (gimple_assign_single_p (context)
285 3756 : && get_no_uninit_warning (gimple_assign_rhs1 (context)))))
286 8823 : || (var && get_no_uninit_warning (var))
287 6586 : || (var_name_str
288 9 : && warning_suppressed_p (var_def_stmt, OPT_Wuninitialized)))
289 : return;
290 :
291 : /* Use either the location of the read statement or that of the PHI
292 : argument, or that of the uninitialized variable, in that order,
293 : whichever is valid. */
294 2072 : location_t location = UNKNOWN_LOCATION;
295 2072 : if (gimple_has_location (context))
296 2059 : location = gimple_location (context);
297 13 : else if (phi_arg_loc != UNKNOWN_LOCATION)
298 : location = phi_arg_loc;
299 8 : else if (var)
300 8 : location = DECL_SOURCE_LOCATION (var);
301 0 : else if (var_name_str)
302 0 : location = gimple_location (var_def_stmt);
303 :
304 2644 : auto_diagnostic_group d;
305 2072 : gcc_assert (opt == OPT_Wuninitialized || opt == OPT_Wmaybe_uninitialized);
306 2072 : if (var)
307 : {
308 2064 : if ((opt == OPT_Wuninitialized
309 1670 : && !warning_at (location, opt, "%qD is used uninitialized", var))
310 2502 : || (opt == OPT_Wmaybe_uninitialized
311 2064 : && !warning_at (location, opt, "%qD may be used uninitialized",
312 : var)))
313 1500 : return;
314 : }
315 8 : else if (var_name_str)
316 : {
317 8 : if ((opt == OPT_Wuninitialized
318 3 : && !warning_at (location, opt, "%qs is used uninitialized",
319 : var_name_str))
320 11 : || (opt == OPT_Wmaybe_uninitialized
321 8 : && !warning_at (location, opt, "%qs may be used uninitialized",
322 : var_name_str)))
323 : return;
324 : }
325 :
326 : /* Avoid subsequent warnings for reads of the same variable again. */
327 577 : if (var)
328 569 : suppress_warning (var, opt);
329 8 : else if (var_name_str)
330 8 : suppress_warning (var_def_stmt, opt);
331 :
332 : /* Issue a note pointing to the read variable unless the warning
333 : is at the same location. */
334 577 : location_t var_loc = var ? DECL_SOURCE_LOCATION (var)
335 577 : : gimple_location (var_def_stmt);
336 577 : if (location == var_loc)
337 : return;
338 :
339 572 : if (var)
340 564 : inform (var_loc, "%qD was declared here", var);
341 8 : else if (var_name_str)
342 8 : inform (var_loc, "%qs was declared here", var_name_str);
343 : }
344 :
345 : struct check_defs_data
346 : {
347 : /* If we found any may-defs besides must-def clobbers. */
348 : bool found_may_defs;
349 : };
350 :
351 : /* Return true if STMT is a call to built-in function all of whose
352 : by-reference arguments are const-qualified (i.e., the function can
353 : be assumed not to modify them). */
354 :
355 : static bool
356 3068536 : builtin_call_nomodifying_p (gimple *stmt)
357 : {
358 3068536 : if (!gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
359 : return false;
360 :
361 367505 : tree fndecl = gimple_call_fndecl (stmt);
362 367505 : if (!fndecl)
363 : return false;
364 :
365 367505 : tree fntype = TREE_TYPE (fndecl);
366 367505 : if (!fntype)
367 : return false;
368 :
369 : /* Check the called function's signature for non-constc pointers.
370 : If one is found, return false. */
371 367505 : unsigned argno = 0;
372 367505 : tree argtype;
373 367505 : function_args_iterator it;
374 653563 : FOREACH_FUNCTION_ARGS (fntype, argtype, it)
375 : {
376 454890 : if (VOID_TYPE_P (argtype))
377 : return true;
378 :
379 408336 : ++argno;
380 :
381 408336 : if (!POINTER_TYPE_P (argtype))
382 52755 : continue;
383 :
384 355581 : if (TYPE_READONLY (TREE_TYPE (argtype)))
385 233303 : continue;
386 :
387 : return false;
388 : }
389 :
390 : /* If the number of actual arguments to the call is less than or
391 : equal to the number of parameters, return false. */
392 198673 : unsigned nargs = gimple_call_num_args (stmt);
393 198673 : if (nargs <= argno)
394 : return false;
395 :
396 : /* Check arguments passed through the ellipsis in calls to variadic
397 : functions for pointers. If one is found that's a non-constant
398 : pointer, return false. */
399 213017 : for (; argno < nargs; ++argno)
400 : {
401 204840 : tree arg = gimple_call_arg (stmt, argno);
402 204840 : argtype = TREE_TYPE (arg);
403 204840 : if (!POINTER_TYPE_P (argtype))
404 13924 : continue;
405 :
406 190916 : if (TYPE_READONLY (TREE_TYPE (argtype)))
407 420 : continue;
408 :
409 : return false;
410 : }
411 :
412 : return true;
413 : }
414 :
415 : /* If ARG is a FNDECL parameter declared with attribute access none or
416 : write_only issue a warning for its read access via PTR. */
417 :
418 : static void
419 178947 : maybe_warn_read_write_only (tree fndecl, gimple *stmt, tree arg, tree ptr)
420 : {
421 178947 : if (!fndecl)
422 0 : return;
423 :
424 178947 : if (get_no_uninit_warning (arg))
425 : return;
426 :
427 178947 : tree fntype = TREE_TYPE (fndecl);
428 178947 : if (!fntype)
429 : return;
430 :
431 : /* Initialize a map of attribute access specifications for arguments
432 : to the function call. */
433 178947 : rdwr_map rdwr_idx;
434 178947 : init_attr_rdwr_indices (&rdwr_idx, TYPE_ATTRIBUTES (fntype));
435 :
436 178947 : unsigned argno = 0;
437 178947 : tree parms = DECL_ARGUMENTS (fndecl);
438 320473 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm), ++argno)
439 : {
440 319983 : if (parm != arg)
441 141526 : continue;
442 :
443 178929 : const attr_access* access = rdwr_idx.get (argno);
444 178929 : if (!access)
445 : break;
446 :
447 486 : if (access->mode != access_none
448 484 : && access->mode != access_write_only)
449 472 : continue;
450 :
451 14 : location_t stmtloc = gimple_location (stmt);
452 14 : if (!warning_at (stmtloc, OPT_Wmaybe_uninitialized,
453 : "%qE may be used uninitialized", ptr))
454 : break;
455 :
456 10 : suppress_warning (arg, OPT_Wmaybe_uninitialized);
457 :
458 10 : const char* const access_str =
459 10 : TREE_STRING_POINTER (access->to_external_string ());
460 :
461 10 : auto_urlify_attributes sentinel;
462 10 : location_t parmloc = DECL_SOURCE_LOCATION (parm);
463 10 : inform (parmloc, "accessing argument %u of a function declared with "
464 : "attribute %qs",
465 : argno + 1, access_str);
466 :
467 10 : break;
468 10 : }
469 178947 : }
470 :
471 : /* Callback for walk_aliased_vdefs. */
472 :
473 : static bool
474 3207170 : check_defs (ao_ref *ref, tree vdef, void *data_)
475 : {
476 3207170 : check_defs_data *data = (check_defs_data *)data_;
477 3207170 : gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
478 :
479 : /* Ignore the vdef if the definition statement is a call
480 : to .DEFERRED_INIT function. */
481 3207170 : if (gimple_call_internal_p (def_stmt, IFN_DEFERRED_INIT))
482 : return false;
483 :
484 : /* For address taken variable, a temporary variable is added between
485 : the variable and the call to .DEFERRED_INIT function as:
486 : _1 = .DEFERRED_INIT (4, 2, &"i1"[0]);
487 : i1 = _1;
488 : Ignore this vdef as well. */
489 3206675 : auto assign = dyn_cast<gassign *> (def_stmt);
490 1735152 : if (assign
491 1735152 : && gimple_assign_rhs_code (assign) == SSA_NAME)
492 : {
493 1352723 : tree tmp_var = gimple_assign_rhs1 (assign);
494 1352723 : if (gimple_call_internal_p (SSA_NAME_DEF_STMT (tmp_var),
495 : IFN_DEFERRED_INIT))
496 : return false;
497 : }
498 :
499 : /* The ASAN_MARK intrinsic doesn't modify the variable. */
500 3206628 : if (is_gimple_call (def_stmt))
501 : {
502 : /* The ASAN_MARK intrinsic doesn't modify the variable. */
503 1454986 : if (gimple_call_internal_p (def_stmt)
504 1454986 : && gimple_call_internal_fn (def_stmt) == IFN_ASAN_MARK)
505 : return false;
506 :
507 1453255 : if (tree fndecl = gimple_call_fndecl (def_stmt))
508 : {
509 : /* Some sanitizer calls pass integer arguments to built-ins
510 : that expect pointets. Avoid using gimple_call_builtin_p()
511 : which fails for such calls. */
512 1256666 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
513 : {
514 368848 : built_in_function fncode = DECL_FUNCTION_CODE (fndecl);
515 368848 : if (fncode > BEGIN_SANITIZER_BUILTINS
516 368848 : && fncode < END_SANITIZER_BUILTINS)
517 : return false;
518 : }
519 : }
520 : }
521 :
522 : /* End of VLA scope is not a kill. */
523 3204748 : if (gimple_call_builtin_p (def_stmt, BUILT_IN_STACK_RESTORE))
524 : return false;
525 :
526 : /* If this is a clobber then if it is not a kill walk past it. */
527 3204672 : if (gimple_clobber_p (def_stmt))
528 : {
529 136136 : if (stmt_kills_ref_p (def_stmt, ref))
530 : return true;
531 : return false;
532 : }
533 :
534 3068536 : if (builtin_call_nomodifying_p (def_stmt))
535 : return false;
536 :
537 : /* Found a may-def on this path. */
538 3013805 : data->found_may_defs = true;
539 3013805 : return true;
540 : }
541 :
542 : /* Counters and limits controlling the depth of analysis and
543 : strictness of the warning. */
544 : struct wlimits
545 : {
546 : /* Number of VDEFs encountered. */
547 : unsigned int vdef_cnt;
548 : /* Number of statements examined by walk_aliased_vdefs. */
549 : unsigned int oracle_cnt;
550 : /* Limit on the number of statements visited by walk_aliased_vdefs. */
551 : unsigned limit;
552 : /* Set when basic block with statement is executed unconditionally. */
553 : bool always_executed;
554 : /* Set to issue -Wmaybe-uninitialized. */
555 : bool wmaybe_uninit;
556 : };
557 :
558 : /* Determine if REF references an uninitialized operand and diagnose
559 : it if so. STMS is the referencing statement. LHS is the result
560 : of the access and may be null. RHS is the variable referenced by
561 : the access; it may not be null. */
562 :
563 : static tree
564 1479084 : maybe_warn_operand (ao_ref &ref, gimple *stmt, tree lhs, tree rhs,
565 : wlimits &wlims)
566 : {
567 1479084 : bool has_bit_insert = false;
568 1479084 : use_operand_p luse_p;
569 1479084 : imm_use_iterator liter;
570 :
571 1479084 : if (get_no_uninit_warning (rhs))
572 : return NULL_TREE;
573 :
574 : /* Do not warn if the base was marked so or this is a
575 : hard register var. */
576 1478108 : tree base = ao_ref_base (&ref);
577 1478108 : if ((VAR_P (base)
578 588530 : && DECL_HARD_REGISTER (base))
579 2956210 : || get_no_uninit_warning (base))
580 : return NULL_TREE;
581 :
582 : /* Do not warn if the access is zero size or if it's fully outside
583 : the object. */
584 1473331 : poly_int64 decl_size;
585 1473331 : if (known_size_p (ref.size)
586 1242808 : && known_eq (ref.max_size, ref.size)
587 2585392 : && (known_eq (ref.size, 0)
588 1112047 : || known_le (ref.offset + ref.size, 0)))
589 : return NULL_TREE;
590 :
591 1473261 : if (DECL_P (base)
592 602421 : && known_ge (ref.offset, 0)
593 602414 : && DECL_SIZE (base)
594 585603 : && poly_int_tree_p (DECL_SIZE (base), &decl_size)
595 2058863 : && known_le (decl_size, ref.offset))
596 : return NULL_TREE;
597 :
598 : /* Do not warn if the result of the access is then used for
599 : a BIT_INSERT_EXPR. */
600 1472751 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
601 3007984 : FOR_EACH_IMM_USE_FAST (luse_p, liter, lhs)
602 : {
603 1777805 : gimple *use_stmt = USE_STMT (luse_p);
604 : /* BIT_INSERT_EXPR first operand should not be considered
605 : a use for the purpose of uninit warnings. */
606 2636349 : if (gassign *ass = dyn_cast <gassign *> (use_stmt))
607 : {
608 858548 : if (gimple_assign_rhs_code (ass) == BIT_INSERT_EXPR
609 858548 : && luse_p->use == gimple_assign_rhs1_ptr (ass))
610 : {
611 : has_bit_insert = true;
612 : break;
613 : }
614 : }
615 1230183 : }
616 :
617 1230183 : if (has_bit_insert)
618 : return NULL_TREE;
619 :
620 : /* Limit the walking to a constant number of stmts after
621 : we overcommit quadratic behavior for small functions
622 : and O(n) behavior. */
623 1472747 : if (wlims.oracle_cnt > 128 * 128
624 36559 : && wlims.oracle_cnt > wlims.vdef_cnt * 2)
625 36559 : wlims.limit = 32;
626 :
627 1472747 : check_defs_data data;
628 1472747 : bool fentry_reached = false;
629 1472747 : data.found_may_defs = false;
630 1472747 : tree use = gimple_vuse (stmt);
631 1472747 : if (!use)
632 : return NULL_TREE;
633 1472489 : int res = walk_aliased_vdefs (&ref, use,
634 : check_defs, &data, NULL,
635 : &fentry_reached, wlims.limit);
636 1472489 : if (res == -1)
637 : {
638 33534 : wlims.oracle_cnt += wlims.limit;
639 33534 : return NULL_TREE;
640 : }
641 :
642 1438955 : wlims.oracle_cnt += res;
643 1438955 : if (data.found_may_defs)
644 : return NULL_TREE;
645 :
646 493466 : bool found_alloc = false;
647 493466 : bool found_clobber_deref_this = false;
648 :
649 493466 : if (fentry_reached)
650 : {
651 493435 : if (TREE_CODE (base) == MEM_REF)
652 214640 : base = TREE_OPERAND (base, 0);
653 :
654 : /* Follow the chain of SSA_NAME assignments looking for an alloca
655 : call (or VLA) or malloc/realloc, or for decls. If any is found
656 : (and in the latter case, the operand is a local variable) issue
657 : a warning. */
658 503862 : while (TREE_CODE (base) == SSA_NAME)
659 : {
660 223294 : gimple *def_stmt = SSA_NAME_DEF_STMT (base);
661 :
662 223294 : if (is_gimple_call (def_stmt)
663 223294 : && gimple_call_builtin_p (def_stmt))
664 : {
665 : /* Detect uses of uninitialized alloca/VLAs. */
666 156 : tree fndecl = gimple_call_fndecl (def_stmt);
667 156 : const built_in_function fncode = DECL_FUNCTION_CODE (fndecl);
668 156 : if (fncode == BUILT_IN_ALLOCA
669 156 : || fncode == BUILT_IN_ALLOCA_WITH_ALIGN
670 83 : || fncode == BUILT_IN_MALLOC)
671 113 : found_alloc = true;
672 : break;
673 : }
674 :
675 : /* The C++ FE for -flifetime-dse=2 marks this parameters
676 : of certain constructors with "clobber *this" attribute.
677 : Emit uninitialized warnings if we read from what this points
678 : to. This is similar to access (write_only, 1) attribute,
679 : except it is a -Wuninitialized warning rather than
680 : -Wmaybe-uninitialized and doesn't talk about access
681 : attribute. */
682 223138 : if (SSA_NAME_IS_DEFAULT_DEF (base)
683 178198 : && POINTER_TYPE_P (TREE_TYPE (base))
684 178198 : && SSA_NAME_VAR (base)
685 178198 : && TREE_CODE (SSA_NAME_VAR (base)) == PARM_DECL
686 401335 : && lookup_attribute ("clobber *this",
687 178197 : DECL_ATTRIBUTES (SSA_NAME_VAR (base))))
688 : {
689 : found_clobber_deref_this = true;
690 : break;
691 : }
692 :
693 223121 : auto assign = dyn_cast<gassign *> (def_stmt);
694 37872 : if (!assign)
695 : break;
696 :
697 37872 : tree_code code = gimple_assign_rhs_code (assign);
698 37872 : if (code != ADDR_EXPR && code != POINTER_PLUS_EXPR)
699 : break;
700 :
701 10427 : base = gimple_assign_rhs1 (assign);
702 10427 : if (TREE_CODE (base) == ADDR_EXPR)
703 1441 : base = TREE_OPERAND (base, 0);
704 :
705 10427 : if (DECL_P (base)
706 9397 : || TREE_CODE (base) == COMPONENT_REF)
707 1203 : rhs = base;
708 :
709 10427 : if (TREE_CODE (base) == MEM_REF)
710 15 : base = TREE_OPERAND (base, 0);
711 :
712 10427 : if (tree ba = get_base_address (base))
713 10427 : base = ba;
714 : }
715 :
716 : /* Replace the RHS expression with BASE so that it
717 : refers to it in the diagnostic (instead of to
718 : '<unknown>'). */
719 493435 : if (DECL_P (base)
720 138714 : && EXPR_P (rhs)
721 64058 : && TREE_CODE (rhs) != COMPONENT_REF)
722 493435 : rhs = base;
723 : }
724 :
725 : /* Do not warn if it can be initialized outside this function.
726 : If we did not reach function entry then we found killing
727 : clobbers on all paths to entry. */
728 493466 : if ((!found_alloc && !found_clobber_deref_this) && fentry_reached)
729 : {
730 493305 : if (TREE_CODE (base) == SSA_NAME)
731 : {
732 212737 : tree var = SSA_NAME_VAR (base);
733 189714 : if (var && TREE_CODE (var) == PARM_DECL)
734 : {
735 178947 : maybe_warn_read_write_only (cfun->decl, stmt, var, rhs);
736 178947 : return NULL_TREE;
737 : }
738 : }
739 :
740 314358 : if (!VAR_P (base)
741 314358 : || is_global_var (base))
742 : /* ??? We'd like to use ref_may_alias_global_p but that
743 : excludes global readonly memory and thus we get bogus
744 : warnings from p = cond ? "a" : "b" for example. */
745 : return NULL_TREE;
746 : }
747 :
748 : /* Strip the address-of expression from arrays passed to functions. */
749 1152 : if (TREE_CODE (rhs) == ADDR_EXPR)
750 0 : rhs = TREE_OPERAND (rhs, 0);
751 :
752 : /* Check again since RHS may have changed above. */
753 1152 : if (get_no_uninit_warning (rhs))
754 : return NULL_TREE;
755 :
756 : /* Avoid warning about empty types such as structs with no members.
757 : The first_field() test is important for C++ where the predicate
758 : alone isn't always sufficient. */
759 1146 : tree rhstype = TREE_TYPE (rhs);
760 1146 : if (POINTER_TYPE_P (rhstype))
761 149 : rhstype = TREE_TYPE (rhstype);
762 1146 : if (is_empty_type (rhstype))
763 : return NULL_TREE;
764 :
765 : /* Avoid diagnosing read-modify-write cycles that in the end only
766 : sets a subset of bits to zero or one.
767 : ??? Note that further reads will then appear (partly) initialized
768 : and will not be diagnosed. */
769 737 : gimple *use_stmt;
770 1218 : auto assign = dyn_cast<gassign *> (stmt);
771 485 : if (assign
772 485 : && gimple_assign_load_p (assign)
773 485 : && TREE_CODE (lhs) == SSA_NAME
774 463 : && single_imm_use (lhs, &luse_p, &use_stmt))
775 : {
776 405 : gassign *use_ass = dyn_cast <gassign *> (use_stmt);
777 409 : for (int i = 0; i < 4; ++i)
778 409 : if (use_ass
779 258 : && (gimple_assign_rhs_code (use_ass) == BIT_AND_EXPR
780 : || gimple_assign_rhs_code (use_ass) == BIT_IOR_EXPR
781 : || gimple_assign_rhs_code (use_ass) == VIEW_CONVERT_EXPR)
782 413 : && single_imm_use (gimple_assign_lhs (use_ass), &luse_p,
783 : &use_stmt))
784 4 : use_ass = dyn_cast <gassign *> (use_stmt);
785 : else
786 : break;
787 405 : if (use_ass
788 254 : && gimple_vdef (use_ass)
789 434 : && operand_equal_p (gimple_assign_rhs1 (stmt),
790 29 : gimple_assign_lhs (use_ass)))
791 : return NULL_TREE;
792 : }
793 :
794 733 : bool warned = false;
795 : /* We didn't find any may-defs so on all paths either
796 : reached function entry or a killing clobber. */
797 733 : location_t location = gimple_location (stmt);
798 733 : if (wlims.always_executed)
799 : {
800 583 : if (warning_at (location, OPT_Wuninitialized,
801 : "%qE is used uninitialized", rhs))
802 : {
803 : /* ??? This is only effective for decls as in
804 : gcc.dg/uninit-B-O0.c. Avoid doing this for maybe-uninit
805 : uses or accesses by functions as it may hide important
806 : locations. */
807 583 : if (lhs)
808 405 : set_no_uninit_warning (rhs);
809 : warned = true;
810 : }
811 : }
812 150 : else if (wlims.wmaybe_uninit)
813 139 : warned = warning_at (location, OPT_Wmaybe_uninitialized,
814 : "%qE may be used uninitialized", rhs);
815 :
816 544 : return warned ? base : NULL_TREE;
817 : }
818 :
819 :
820 : /* Diagnose passing addresses of uninitialized objects to either const
821 : pointer arguments to functions, or to functions declared with attribute
822 : access implying read access to those objects. */
823 :
824 : static void
825 1275107 : maybe_warn_pass_by_reference (gcall *stmt, wlimits &wlims)
826 : {
827 1275107 : if (!wlims.wmaybe_uninit)
828 785663 : return;
829 :
830 643964 : unsigned nargs = gimple_call_num_args (stmt);
831 643964 : if (!nargs)
832 : return;
833 :
834 585385 : tree fndecl = gimple_call_fndecl (stmt);
835 1293510 : tree fntype = gimple_call_fntype (stmt);
836 507847 : if (!fntype)
837 : return;
838 :
839 : /* Const function do not read their arguments. */
840 507847 : if (gimple_call_flags (stmt) & ECF_CONST)
841 : return;
842 :
843 496295 : const built_in_function fncode
844 446477 : = (fndecl && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)
845 621430 : ? DECL_FUNCTION_CODE (fndecl) : (built_in_function)BUILT_IN_LAST);
846 :
847 125135 : if (fncode == BUILT_IN_MEMCPY || fncode == BUILT_IN_MEMMOVE)
848 : /* Avoid diagnosing calls to raw memory functions (this is overly
849 : permissive; consider tightening it up). */
850 : return;
851 :
852 : /* Save the current warning setting and replace it either a "maybe"
853 : when passing addresses of uninitialized variables to const-qualified
854 : pointers or arguments declared with attribute read_write, or with
855 : a "certain" when passing them to arguments declared with attribute
856 : read_only. */
857 489444 : const bool save_always_executed = wlims.always_executed;
858 :
859 : /* Initialize a map of attribute access specifications for arguments
860 : to the function call. */
861 489444 : rdwr_map rdwr_idx;
862 489444 : init_attr_rdwr_indices (&rdwr_idx, TYPE_ATTRIBUTES (fntype));
863 :
864 489444 : tree argtype;
865 489444 : unsigned argno = 0;
866 489444 : function_args_iterator it;
867 :
868 1662871 : FOREACH_FUNCTION_ARGS (fntype, argtype, it)
869 : {
870 1593611 : ++argno;
871 :
872 1593611 : if (argno > nargs)
873 : break;
874 :
875 1173427 : if (!POINTER_TYPE_P (argtype))
876 1173187 : continue;
877 :
878 592956 : tree access_size = NULL_TREE;
879 592956 : const attr_access* access = rdwr_idx.get (argno - 1);
880 592956 : if (access)
881 : {
882 3041 : if (access->mode == access_none
883 2870 : || access->mode == access_write_only)
884 403 : continue;
885 :
886 4544 : if (access->mode == access_deferred
887 2638 : && !TYPE_READONLY (TREE_TYPE (argtype)))
888 1906 : continue;
889 :
890 732 : if (save_always_executed && access->mode == access_read_only)
891 : /* Attribute read_only arguments imply read access. */
892 236 : wlims.always_executed = true;
893 : else
894 : /* Attribute read_write arguments are documented as requiring
895 : initialized objects but it's expected that aggregates may
896 : be only partially initialized regardless. */
897 496 : wlims.always_executed = false;
898 :
899 732 : if (access->sizarg < nargs)
900 217 : access_size = gimple_call_arg (stmt, access->sizarg);
901 : }
902 589915 : else if (!TYPE_READONLY (TREE_TYPE (argtype)))
903 356044 : continue;
904 233871 : else if (save_always_executed && fncode != BUILT_IN_LAST)
905 : /* Const-qualified arguments to built-ins imply read access. */
906 21009 : wlims.always_executed = true;
907 : else
908 : /* Const-qualified arguments to ordinary functions imply a likely
909 : (but not definitive) read access. */
910 212862 : wlims.always_executed = false;
911 :
912 : /* Ignore args we are not going to read from. */
913 234603 : if (gimple_call_arg_flags (stmt, argno - 1)
914 234603 : & (EAF_UNUSED | EAF_NO_DIRECT_READ))
915 1991 : continue;
916 :
917 232612 : tree arg = gimple_call_arg (stmt, argno - 1);
918 232612 : if (!POINTER_TYPE_P (TREE_TYPE (arg)))
919 : /* Avoid actual arguments with invalid types. */
920 0 : continue;
921 :
922 232612 : ao_ref ref;
923 232612 : ao_ref_init_from_ptr_and_size (&ref, arg, access_size);
924 232612 : tree argbase = maybe_warn_operand (ref, stmt, NULL_TREE, arg, wlims);
925 232612 : if (!argbase)
926 232372 : continue;
927 :
928 240 : if (access && access->mode != access_deferred)
929 : {
930 48 : const char* const access_str =
931 48 : TREE_STRING_POINTER (access->to_external_string ());
932 :
933 48 : auto_urlify_attributes sentinel;
934 48 : if (fndecl)
935 : {
936 48 : location_t loc = DECL_SOURCE_LOCATION (fndecl);
937 48 : inform (loc, "in a call to %qD declared with "
938 : "attribute %qs here", fndecl, access_str);
939 : }
940 : else
941 : {
942 : /* Handle calls through function pointers. */
943 0 : location_t loc = gimple_location (stmt);
944 0 : inform (loc, "in a call to %qT declared with "
945 : "attribute %qs", fntype, access_str);
946 : }
947 48 : }
948 : else
949 : {
950 : /* For a declaration with no relevant attribute access create
951 : a dummy object and use the formatting function to avoid
952 : having to complicate things here. */
953 192 : attr_access ptr_access = { };
954 192 : if (!access)
955 173 : access = &ptr_access;
956 192 : const std::string argtypestr = access->array_as_string (argtype);
957 192 : if (fndecl)
958 : {
959 192 : location_t loc (DECL_SOURCE_LOCATION (fndecl));
960 192 : inform (loc, "by argument %u of type %s to %qD "
961 : "declared here",
962 : argno, argtypestr.c_str (), fndecl);
963 : }
964 : else
965 : {
966 : /* Handle calls through function pointers. */
967 0 : location_t loc (gimple_location (stmt));
968 0 : inform (loc, "by argument %u of type %s to %qT",
969 : argno, argtypestr.c_str (), fntype);
970 : }
971 192 : }
972 :
973 240 : if (DECL_P (argbase))
974 : {
975 219 : location_t loc = DECL_SOURCE_LOCATION (argbase);
976 219 : inform (loc, "%qD declared here", argbase);
977 : }
978 : }
979 :
980 489444 : wlims.always_executed = save_always_executed;
981 489444 : }
982 :
983 : /* Warn about an uninitialized PHI argument on the fallthru path to
984 : an always executed block BB. */
985 :
986 : static void
987 412857 : warn_uninit_phi_uses (basic_block bb)
988 : {
989 412857 : edge_iterator ei;
990 412857 : edge e, found = NULL, found_back = NULL;
991 : /* Look for a fallthru and possibly a single backedge. */
992 840874 : FOR_EACH_EDGE (e, ei, bb->preds)
993 : {
994 : /* Ignore backedges. */
995 535877 : if (dominated_by_p (CDI_DOMINATORS, e->src, bb))
996 : {
997 15660 : if (found_back)
998 : {
999 : found = NULL;
1000 : break;
1001 : }
1002 15160 : found_back = e;
1003 15160 : continue;
1004 : }
1005 520217 : if (found)
1006 : {
1007 : found = NULL;
1008 : break;
1009 : }
1010 : found = e;
1011 : }
1012 412857 : if (!found)
1013 107860 : return;
1014 :
1015 304997 : basic_block succ = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1016 339012 : for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
1017 34015 : gsi_next (&si))
1018 : {
1019 34015 : gphi *phi = si.phi ();
1020 34015 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, found);
1021 66299 : if (TREE_CODE (def) != SSA_NAME
1022 29634 : || !SSA_NAME_IS_DEFAULT_DEF (def)
1023 39362 : || virtual_operand_p (def))
1024 32284 : continue;
1025 : /* If there's a default def on the fallthru edge PHI
1026 : value and there's a use that post-dominates entry
1027 : then that use is uninitialized and we can warn. */
1028 1731 : imm_use_iterator iter;
1029 1731 : use_operand_p use_p;
1030 1731 : gimple *use_stmt = NULL;
1031 6125 : FOR_EACH_IMM_USE_FAST (use_p, iter, gimple_phi_result (phi))
1032 : {
1033 5506 : use_stmt = USE_STMT (use_p);
1034 5506 : if (gimple_location (use_stmt) != UNKNOWN_LOCATION
1035 4586 : && dominated_by_p (CDI_POST_DOMINATORS, succ,
1036 4586 : gimple_bb (use_stmt))
1037 : /* If we found a non-fallthru edge make sure the
1038 : use is inside the loop, otherwise the backedge
1039 : can serve as initialization. */
1040 6806 : && (!found_back
1041 1300 : || dominated_by_p (CDI_DOMINATORS, found_back->src,
1042 1300 : gimple_bb (use_stmt))))
1043 : break;
1044 4394 : use_stmt = NULL;
1045 1731 : }
1046 1731 : if (use_stmt)
1047 2224 : warn_uninit (OPT_Wuninitialized, def,
1048 1112 : SSA_NAME_VAR (def), use_stmt);
1049 : }
1050 : }
1051 :
1052 : /* Issue warnings about reads of uninitialized variables. WMAYBE_UNINIT
1053 : is true to issue -Wmaybe-uninitialized, otherwise -Wuninitialized. */
1054 :
1055 : static void
1056 249515 : warn_uninitialized_vars (bool wmaybe_uninit)
1057 : {
1058 : /* Counters and limits controlling the depth of the warning. */
1059 249515 : wlimits wlims = { };
1060 249515 : wlims.wmaybe_uninit = wmaybe_uninit;
1061 :
1062 249515 : auto_bb_flag ft_reachable (cfun);
1063 :
1064 : /* Mark blocks that are always executed when we ignore provably
1065 : not executed and EH and abnormal edges. */
1066 249515 : basic_block bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1067 412920 : while (!(bb->flags & ft_reachable))
1068 : {
1069 412857 : bb->flags |= ft_reachable;
1070 412857 : edge e = find_fallthru_edge (bb->succs);
1071 412857 : if (e && e->flags & EDGE_EXECUTABLE)
1072 : {
1073 44866 : bb = e->dest;
1074 44866 : continue;
1075 : }
1076 : /* Find a single executable edge. */
1077 367991 : edge_iterator ei;
1078 367991 : edge ee = NULL;
1079 734394 : FOR_EACH_EDGE (e, ei, bb->succs)
1080 517642 : if (e->flags & EDGE_EXECUTABLE)
1081 : {
1082 517442 : if (!ee)
1083 : ee = e;
1084 : else
1085 : {
1086 : ee = NULL;
1087 : break;
1088 : }
1089 : }
1090 367991 : if (ee)
1091 214964 : bb = ee->dest;
1092 : else
1093 153027 : bb = get_immediate_dominator (CDI_POST_DOMINATORS, bb);
1094 367991 : if (!bb || bb->index == EXIT_BLOCK)
1095 : break;
1096 : }
1097 :
1098 2836303 : FOR_EACH_BB_FN (bb, cfun)
1099 : {
1100 2586788 : wlims.always_executed = (bb->flags & ft_reachable);
1101 2586788 : bb->flags &= ~ft_reachable;
1102 :
1103 2586788 : edge_iterator ei;
1104 2586788 : edge e;
1105 2587586 : FOR_EACH_EDGE (e, ei, bb->preds)
1106 2587197 : if (e->flags & EDGE_EXECUTABLE)
1107 : break;
1108 : /* Skip unreachable blocks. For early analysis we use VN to
1109 : determine edge executability when wmaybe_uninit. */
1110 2586788 : if (!e)
1111 389 : continue;
1112 :
1113 2586399 : if (wlims.always_executed)
1114 412857 : warn_uninit_phi_uses (bb);
1115 :
1116 2586399 : gimple_stmt_iterator gsi;
1117 21097680 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1118 : {
1119 15924882 : gimple *stmt = gsi_stmt (gsi);
1120 :
1121 : /* The call is an artificial use, will not provide meaningful
1122 : error message. If the result of the call is used somewhere
1123 : else, we warn there instead. */
1124 15924882 : if (gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
1125 8906520 : continue;
1126 :
1127 15921115 : if (is_gimple_debug (stmt))
1128 7656747 : continue;
1129 :
1130 : /* We only do data flow with SSA_NAMEs, so that's all we
1131 : can warn about. */
1132 8264368 : use_operand_p use_p;
1133 8264368 : ssa_op_iter op_iter;
1134 16972374 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, op_iter, SSA_OP_USE)
1135 : {
1136 : /* BIT_INSERT_EXPR first operand should not be considered
1137 : a use for the purpose of uninit warnings. */
1138 8708006 : if (gassign *ass = dyn_cast <gassign *> (stmt))
1139 : {
1140 5743331 : if (gimple_assign_rhs_code (ass) == BIT_INSERT_EXPR
1141 5743331 : && use_p->use == gimple_assign_rhs1_ptr (ass))
1142 27 : continue;
1143 : }
1144 8707979 : tree use = USE_FROM_PTR (use_p);
1145 8707979 : if (wlims.always_executed)
1146 3551668 : warn_uninit (OPT_Wuninitialized, use,
1147 1775834 : SSA_NAME_VAR (use), stmt);
1148 6932145 : else if (wlims.wmaybe_uninit)
1149 7865084 : warn_uninit (OPT_Wmaybe_uninitialized, use,
1150 3932542 : SSA_NAME_VAR (use), stmt);
1151 : }
1152 :
1153 : /* For limiting the alias walk below we count all
1154 : vdefs in the function. */
1155 15392395 : if (gimple_vdef (stmt))
1156 2079451 : wlims.vdef_cnt++;
1157 :
1158 8264368 : if (gcall *call = dyn_cast <gcall *> (stmt))
1159 1275107 : maybe_warn_pass_by_reference (call, wlims);
1160 6989261 : else if (gimple_assign_load_p (stmt)
1161 6989261 : && gimple_has_location (stmt))
1162 : {
1163 1246472 : tree rhs = gimple_assign_rhs1 (stmt);
1164 1246472 : tree lhs = gimple_assign_lhs (stmt);
1165 :
1166 1246472 : ao_ref ref;
1167 1246472 : ao_ref_init (&ref, rhs);
1168 1246472 : tree var = maybe_warn_operand (ref, stmt, lhs, rhs, wlims);
1169 1246472 : if (!var)
1170 1246006 : continue;
1171 :
1172 466 : if (DECL_P (var))
1173 : {
1174 344 : location_t loc = DECL_SOURCE_LOCATION (var);
1175 344 : inform (loc, "%qD declared here", var);
1176 : }
1177 : }
1178 : }
1179 : }
1180 249515 : }
1181 :
1182 : /* Checks if the operand OPND of PHI is defined by
1183 : another phi with one operand defined by this PHI,
1184 : but the rest operands are all defined. If yes,
1185 : returns true to skip this operand as being
1186 : redundant. Can be enhanced to be more general. */
1187 :
1188 : static bool
1189 3740 : can_skip_redundant_opnd (tree opnd, gimple *phi)
1190 : {
1191 3740 : tree phi_def = gimple_phi_result (phi);
1192 3740 : gimple *op_def = SSA_NAME_DEF_STMT (opnd);
1193 3740 : if (gimple_code (op_def) != GIMPLE_PHI)
1194 : return false;
1195 :
1196 1977 : unsigned n = gimple_phi_num_args (op_def);
1197 2232 : for (unsigned i = 0; i < n; ++i)
1198 : {
1199 2222 : tree op = gimple_phi_arg_def (op_def, i);
1200 2222 : if (TREE_CODE (op) != SSA_NAME)
1201 8 : continue;
1202 2214 : if (op != phi_def && uninit_undefined_value_p (op))
1203 : return false;
1204 : }
1205 :
1206 : return true;
1207 : }
1208 :
1209 : /* Return a bitset holding the positions of arguments in PHI with empty
1210 : (or possibly empty) definitions. */
1211 :
1212 : static unsigned
1213 452382 : compute_uninit_opnds_pos (gphi *phi)
1214 : {
1215 452382 : unsigned uninit_opnds = 0;
1216 :
1217 452382 : unsigned n = gimple_phi_num_args (phi);
1218 : /* Bail out for phi with too many args. */
1219 452382 : if (n > uninit_analysis::func_t::max_phi_args)
1220 : return 0;
1221 :
1222 1575028 : for (unsigned i = 0; i < n; ++i)
1223 : {
1224 1122855 : tree op = gimple_phi_arg_def (phi, i);
1225 1122855 : if (TREE_CODE (op) == SSA_NAME
1226 921864 : && uninit_undefined_value_p (op)
1227 1126595 : && !can_skip_redundant_opnd (op, phi))
1228 : {
1229 3730 : if (cfun->has_nonlocal_label || cfun->calls_setjmp)
1230 : {
1231 : /* Ignore SSA_NAMEs that appear on abnormal edges
1232 : somewhere. */
1233 110 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
1234 63 : continue;
1235 : }
1236 3667 : MASK_SET_BIT (uninit_opnds, i);
1237 : }
1238 : }
1239 : /* If we have recorded guarded uses of may-uninit values mask those. */
1240 452173 : if (auto *def_mask = defined_args->get (phi))
1241 34 : uninit_opnds &= ~*def_mask;
1242 : return uninit_opnds;
1243 : }
1244 :
1245 : /* Function object type used to determine whether an expression
1246 : is of interest to the predicate analyzer. */
1247 :
1248 : struct uninit_undef_val_t: public uninit_analysis::func_t
1249 : {
1250 : virtual unsigned phi_arg_set (gphi *) override;
1251 : };
1252 :
1253 : /* Return a bitset of PHI arguments of interest. */
1254 :
1255 : unsigned
1256 1643 : uninit_undef_val_t::phi_arg_set (gphi *phi)
1257 : {
1258 1643 : return compute_uninit_opnds_pos (phi);
1259 : }
1260 :
1261 : /* sort helper for find_uninit_use. */
1262 :
1263 : static int
1264 165 : cand_cmp (const void *a, const void *b, void *data)
1265 : {
1266 165 : int *bb_to_rpo = (int *)data;
1267 165 : const gimple *sa = *(const gimple * const *)a;
1268 165 : const gimple *sb = *(const gimple * const *)b;
1269 165 : if (bb_to_rpo[gimple_bb (sa)->index] < bb_to_rpo[gimple_bb (sb)->index])
1270 : return -1;
1271 84 : else if (bb_to_rpo[gimple_bb (sa)->index] > bb_to_rpo[gimple_bb (sb)->index])
1272 45 : return 1;
1273 : return 0;
1274 : }
1275 :
1276 : /* Searches through all uses of a potentially
1277 : uninitialized variable defined by PHI and returns a use
1278 : statement if the use is not properly guarded. It returns
1279 : NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
1280 : holding the position(s) of uninit PHI operands. */
1281 :
1282 : static gimple *
1283 368 : find_uninit_use (gphi *phi, unsigned uninit_opnds, int *bb_to_rpo)
1284 : {
1285 : /* The Boolean predicate guarding the PHI definition. Initialized
1286 : lazily from PHI in the first call to is_use_guarded() and cached
1287 : for subsequent iterations. */
1288 368 : uninit_undef_val_t eval;
1289 368 : uninit_analysis def_preds (eval);
1290 :
1291 : /* First process PHIs and record other candidates. */
1292 368 : auto_vec<gimple *, 64> cands;
1293 368 : use_operand_p use_p;
1294 368 : imm_use_iterator iter;
1295 368 : tree phi_result = gimple_phi_result (phi);
1296 1157 : FOR_EACH_IMM_USE_FAST (use_p, iter, phi_result)
1297 : {
1298 789 : gimple *use_stmt = USE_STMT (use_p);
1299 789 : if (is_gimple_debug (use_stmt))
1300 253 : continue;
1301 :
1302 : /* Look through a single level of SSA name copies. This is
1303 : important for copies involving abnormals which we can't always
1304 : proapgate out but which result in spurious unguarded uses. */
1305 598 : use_operand_p use2_p;
1306 598 : gimple *use2_stmt;
1307 598 : if (gimple_assign_ssa_name_copy_p (use_stmt)
1308 598 : && single_imm_use (gimple_assign_lhs (use_stmt), &use2_p, &use2_stmt))
1309 : {
1310 8 : use_p = use2_p;
1311 8 : use_stmt = use2_stmt;
1312 : }
1313 :
1314 819 : if (gphi *use_phi = dyn_cast<gphi *> (use_stmt))
1315 : {
1316 377 : unsigned idx = PHI_ARG_INDEX_FROM_USE (use_p);
1317 377 : edge e = gimple_phi_arg_edge (use_phi, idx);
1318 : /* Do not look for uses in the next iteration of a loop, predicate
1319 : analysis will not use the appropriate predicates to prove
1320 : reachability. */
1321 377 : if (e->flags & EDGE_DFS_BACK)
1322 62 : continue;
1323 :
1324 349 : basic_block use_bb = e->src;
1325 349 : if (def_preds.is_use_guarded (use_stmt, use_bb, phi, uninit_opnds))
1326 : {
1327 : /* For a guarded use in a PHI record the PHI argument as
1328 : initialized. */
1329 34 : if (idx < uninit_analysis::func_t::max_phi_args)
1330 : {
1331 34 : bool existed_p;
1332 34 : auto &def_mask
1333 34 : = defined_args->get_or_insert (use_phi, &existed_p);
1334 34 : if (!existed_p)
1335 34 : def_mask = 0;
1336 34 : MASK_SET_BIT (def_mask, idx);
1337 : }
1338 34 : continue;
1339 34 : }
1340 :
1341 315 : if (dump_file && (dump_flags & TDF_DETAILS))
1342 : {
1343 0 : fprintf (dump_file, "Found unguarded use on edge %u -> %u: ",
1344 0 : e->src->index, e->dest->index);
1345 0 : print_gimple_stmt (dump_file, use_stmt, 0);
1346 : }
1347 : /* Found a phi use that is not guarded, mark the use as
1348 : possibly undefined. */
1349 315 : possibly_undefined_names->add (USE_FROM_PTR (use_p));
1350 : }
1351 : else
1352 221 : cands.safe_push (use_stmt);
1353 368 : }
1354 :
1355 : /* Sort candidates after RPO. */
1356 368 : cands.stablesort (cand_cmp, bb_to_rpo);
1357 368 : basic_block use_bb = NULL;
1358 1212 : for (gimple *use_stmt : cands)
1359 : {
1360 : /* We only have to try diagnosing the first use in each block. */
1361 207 : if (gimple_bb (use_stmt) == use_bb)
1362 0 : continue;
1363 :
1364 207 : use_bb = gimple_bb (use_stmt);
1365 207 : if (def_preds.is_use_guarded (use_stmt, use_bb, phi, uninit_opnds))
1366 108 : continue;
1367 :
1368 99 : if (dump_file && (dump_flags & TDF_DETAILS))
1369 : {
1370 0 : fprintf (dump_file, "Found unguarded use in bb %u: ",
1371 : use_bb->index);
1372 0 : print_gimple_stmt (dump_file, use_stmt, 0);
1373 : }
1374 : return use_stmt;
1375 : }
1376 :
1377 : return NULL;
1378 368 : }
1379 :
1380 : /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
1381 : and gives warning if there exists a runtime path from the entry to a
1382 : use of the PHI def that does not contain a definition. In other words,
1383 : the warning is on the real use. The more dead paths that can be pruned
1384 : by the compiler, the fewer false positives the warning is. */
1385 :
1386 : static void
1387 368 : warn_uninitialized_phi (gphi *phi, unsigned uninit_opnds, int *bb_to_rpo)
1388 : {
1389 368 : if (dump_file && (dump_flags & TDF_DETAILS))
1390 : {
1391 0 : fprintf (dump_file, "Examining phi: ");
1392 0 : print_gimple_stmt (dump_file, phi, 0);
1393 : }
1394 :
1395 368 : gimple *uninit_use_stmt = find_uninit_use (phi, uninit_opnds, bb_to_rpo);
1396 :
1397 : /* All uses are properly guarded. */
1398 368 : if (!uninit_use_stmt)
1399 : return;
1400 :
1401 99 : unsigned phiarg_index = MASK_FIRST_SET_BIT (uninit_opnds);
1402 99 : tree uninit_op = gimple_phi_arg_def (phi, phiarg_index);
1403 :
1404 99 : location_t loc = UNKNOWN_LOCATION;
1405 99 : if (gimple_phi_arg_has_location (phi, phiarg_index))
1406 : loc = gimple_phi_arg_location (phi, phiarg_index);
1407 : else
1408 : {
1409 84 : tree arg_def = gimple_phi_arg_def (phi, phiarg_index);
1410 84 : if (TREE_CODE (arg_def) == SSA_NAME)
1411 : {
1412 84 : gimple *def_stmt = SSA_NAME_DEF_STMT (arg_def);
1413 84 : if (gphi *arg_phi = dyn_cast<gphi *> (def_stmt))
1414 : {
1415 12 : unsigned uop = compute_uninit_opnds_pos (arg_phi);
1416 12 : unsigned idx = MASK_FIRST_SET_BIT (uop);
1417 12 : if (idx < gimple_phi_num_args (arg_phi)
1418 12 : && gimple_phi_arg_has_location (arg_phi, idx))
1419 : loc = gimple_phi_arg_location (arg_phi, idx);
1420 : }
1421 : }
1422 : }
1423 :
1424 198 : warn_uninit (OPT_Wmaybe_uninitialized, uninit_op,
1425 99 : SSA_NAME_VAR (uninit_op),
1426 : uninit_use_stmt, loc);
1427 : }
1428 :
1429 : static bool
1430 4068862 : gate_warn_uninitialized (void)
1431 : {
1432 3819902 : return warn_uninitialized || warn_maybe_uninitialized;
1433 : }
1434 :
1435 : namespace {
1436 :
1437 : const pass_data pass_data_late_warn_uninitialized =
1438 : {
1439 : GIMPLE_PASS, /* type */
1440 : "uninit", /* name */
1441 : OPTGROUP_NONE, /* optinfo_flags */
1442 : TV_NONE, /* tv_id */
1443 : PROP_ssa, /* properties_required */
1444 : 0, /* properties_provided */
1445 : 0, /* properties_destroyed */
1446 : 0, /* todo_flags_start */
1447 : 0, /* todo_flags_finish */
1448 : };
1449 :
1450 : class pass_late_warn_uninitialized : public gimple_opt_pass
1451 : {
1452 : public:
1453 605970 : pass_late_warn_uninitialized (gcc::context *ctxt)
1454 1211940 : : gimple_opt_pass (pass_data_late_warn_uninitialized, ctxt)
1455 : {}
1456 :
1457 : /* opt_pass methods: */
1458 302985 : opt_pass *clone () final override
1459 : {
1460 302985 : return new pass_late_warn_uninitialized (m_ctxt);
1461 : }
1462 1070735 : bool gate (function *) final override { return gate_warn_uninitialized (); }
1463 : unsigned int execute (function *) final override;
1464 :
1465 : }; // class pass_late_warn_uninitialized
1466 :
1467 : static void
1468 80915 : execute_late_warn_uninitialized (function *fun)
1469 : {
1470 80915 : calculate_dominance_info (CDI_DOMINATORS);
1471 80915 : calculate_dominance_info (CDI_POST_DOMINATORS);
1472 :
1473 : /* Mark all edges executable, warn_uninitialized_vars will skip
1474 : unreachable blocks. */
1475 80915 : set_all_edges_as_executable (fun);
1476 80915 : mark_dfs_back_edges (fun);
1477 80915 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fun));
1478 80915 : int n = pre_and_rev_post_order_compute_fn (fun, NULL, rpo, false);
1479 80915 : int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
1480 1647176 : for (int i = 0; i < n; ++i)
1481 1485346 : bb_to_rpo[rpo[i]] = i;
1482 :
1483 : /* Re-do the plain uninitialized variable check, as optimization may have
1484 : straightened control flow. Do this first so that we don't accidentally
1485 : get a "may be" warning when we'd have seen an "is" warning later. */
1486 80915 : warn_uninitialized_vars (/*warn_maybe_uninitialized=*/1);
1487 :
1488 80915 : timevar_push (TV_TREE_UNINIT);
1489 :
1490 : /* Avoid quadratic behavior when looking up case labels for edges. */
1491 80915 : start_recording_case_labels ();
1492 :
1493 80915 : possibly_undefined_names = new hash_set<tree>;
1494 80915 : defined_args = new hash_map<gphi *, uninit_analysis::func_t::phi_arg_set_t>;
1495 :
1496 : /* Walk the CFG in RPO order so we visit PHIs with defs that are
1497 : possibly uninitialized from other PHIs after those. The uninit
1498 : predicate analysis will then expand the PHIs predicate with
1499 : the predicates of the edges from such PHI defs. */
1500 1566261 : for (int i = 0; i < n; ++i)
1501 1485346 : for (auto gsi = gsi_start_phis (BASIC_BLOCK_FOR_FN (fun, rpo[i]));
1502 2151837 : !gsi_end_p (gsi); gsi_next (&gsi))
1503 : {
1504 666491 : gphi *phi = gsi.phi ();
1505 :
1506 : /* Don't look at virtual operands. */
1507 1332982 : if (virtual_operand_p (gimple_phi_result (phi)))
1508 215764 : continue;
1509 :
1510 450727 : unsigned uninit_opnds = compute_uninit_opnds_pos (phi);
1511 450727 : if (MASK_EMPTY (uninit_opnds))
1512 450359 : continue;
1513 :
1514 368 : warn_uninitialized_phi (phi, uninit_opnds, bb_to_rpo);
1515 : }
1516 :
1517 80915 : free (rpo);
1518 80915 : free (bb_to_rpo);
1519 161830 : delete possibly_undefined_names;
1520 80915 : possibly_undefined_names = NULL;
1521 161830 : delete defined_args;
1522 80915 : defined_args = NULL;
1523 80915 : end_recording_case_labels ();
1524 80915 : free_dominance_info (CDI_POST_DOMINATORS);
1525 80915 : timevar_pop (TV_TREE_UNINIT);
1526 80915 : }
1527 :
1528 : unsigned int
1529 80915 : pass_late_warn_uninitialized::execute (function *fun)
1530 : {
1531 80915 : execute_late_warn_uninitialized (fun);
1532 80915 : return 0;
1533 : }
1534 :
1535 : } // anon namespace
1536 :
1537 : gimple_opt_pass *
1538 302985 : make_pass_late_warn_uninitialized (gcc::context *ctxt)
1539 : {
1540 302985 : return new pass_late_warn_uninitialized (ctxt);
1541 : }
1542 :
1543 : static unsigned int
1544 168600 : execute_early_warn_uninitialized (struct function *fun)
1545 : {
1546 : /* Currently, this pass runs always but
1547 : execute_late_warn_uninitialized only runs with optimization. With
1548 : optimization we want to warn about possible uninitialized as late
1549 : as possible, thus don't do it here. However, without
1550 : optimization we need to warn here about "may be uninitialized". */
1551 168600 : calculate_dominance_info (CDI_DOMINATORS);
1552 168600 : calculate_dominance_info (CDI_POST_DOMINATORS);
1553 :
1554 : /* Use VN in its cheapest incarnation and without doing any
1555 : elimination to compute edge reachability. Don't bother when
1556 : we only warn for unconditionally executed code though. */
1557 168600 : if (!optimize)
1558 19690 : do_rpo_vn (fun, NULL, NULL, false, false, false, VN_NOWALK);
1559 : else
1560 148910 : set_all_edges_as_executable (fun);
1561 :
1562 168600 : warn_uninitialized_vars (/*warn_maybe_uninitialized=*/!optimize);
1563 :
1564 : /* Post-dominator information cannot be reliably updated. Free it
1565 : after the use. */
1566 :
1567 168600 : free_dominance_info (CDI_POST_DOMINATORS);
1568 168600 : return 0;
1569 : }
1570 :
1571 : namespace {
1572 :
1573 : const pass_data pass_data_early_warn_uninitialized =
1574 : {
1575 : GIMPLE_PASS, /* type */
1576 : "early_uninit", /* name */
1577 : OPTGROUP_NONE, /* optinfo_flags */
1578 : TV_TREE_UNINIT, /* tv_id */
1579 : PROP_ssa, /* properties_required */
1580 : 0, /* properties_provided */
1581 : 0, /* properties_destroyed */
1582 : 0, /* todo_flags_start */
1583 : 0, /* todo_flags_finish */
1584 : };
1585 :
1586 : class pass_early_warn_uninitialized : public gimple_opt_pass
1587 : {
1588 : public:
1589 302985 : pass_early_warn_uninitialized (gcc::context *ctxt)
1590 605970 : : gimple_opt_pass (pass_data_early_warn_uninitialized, ctxt)
1591 : {}
1592 :
1593 : /* opt_pass methods: */
1594 2998127 : bool gate (function *) final override { return gate_warn_uninitialized (); }
1595 168600 : unsigned int execute (function *fun) final override
1596 : {
1597 168600 : return execute_early_warn_uninitialized (fun);
1598 : }
1599 :
1600 : }; // class pass_early_warn_uninitialized
1601 :
1602 : } // anon namespace
1603 :
1604 : gimple_opt_pass *
1605 302985 : make_pass_early_warn_uninitialized (gcc::context *ctxt)
1606 : {
1607 302985 : return new pass_early_warn_uninitialized (ctxt);
1608 : }
|