xref: /dflybsd-src/contrib/gcc-8.0/gcc/gimple-ssa-warn-alloca.c (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj /* Warn on problematic uses of alloca and variable length arrays.
2*38fd1498Szrj    Copyright (C) 2016-2018 Free Software Foundation, Inc.
3*38fd1498Szrj    Contributed by Aldy Hernandez <aldyh@redhat.com>.
4*38fd1498Szrj 
5*38fd1498Szrj This file is part of GCC.
6*38fd1498Szrj 
7*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
8*38fd1498Szrj the terms of the GNU General Public License as published by the Free
9*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
10*38fd1498Szrj version.
11*38fd1498Szrj 
12*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15*38fd1498Szrj for more details.
16*38fd1498Szrj 
17*38fd1498Szrj You should have received a copy of the GNU General Public License
18*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
19*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
20*38fd1498Szrj 
21*38fd1498Szrj #include "config.h"
22*38fd1498Szrj #include "system.h"
23*38fd1498Szrj #include "coretypes.h"
24*38fd1498Szrj #include "backend.h"
25*38fd1498Szrj #include "tree.h"
26*38fd1498Szrj #include "gimple.h"
27*38fd1498Szrj #include "tree-pass.h"
28*38fd1498Szrj #include "ssa.h"
29*38fd1498Szrj #include "gimple-pretty-print.h"
30*38fd1498Szrj #include "diagnostic-core.h"
31*38fd1498Szrj #include "fold-const.h"
32*38fd1498Szrj #include "gimple-iterator.h"
33*38fd1498Szrj #include "tree-ssa.h"
34*38fd1498Szrj #include "params.h"
35*38fd1498Szrj #include "tree-cfg.h"
36*38fd1498Szrj #include "calls.h"
37*38fd1498Szrj #include "cfgloop.h"
38*38fd1498Szrj #include "intl.h"
39*38fd1498Szrj 
40*38fd1498Szrj const pass_data pass_data_walloca = {
41*38fd1498Szrj   GIMPLE_PASS,
42*38fd1498Szrj   "walloca",
43*38fd1498Szrj   OPTGROUP_NONE,
44*38fd1498Szrj   TV_NONE,
45*38fd1498Szrj   PROP_cfg, // properties_required
46*38fd1498Szrj   0,	    // properties_provided
47*38fd1498Szrj   0,	    // properties_destroyed
48*38fd1498Szrj   0,	    // properties_start
49*38fd1498Szrj   0,	    // properties_finish
50*38fd1498Szrj };
51*38fd1498Szrj 
52*38fd1498Szrj class pass_walloca : public gimple_opt_pass
53*38fd1498Szrj {
54*38fd1498Szrj public:
pass_walloca(gcc::context * ctxt)55*38fd1498Szrj   pass_walloca (gcc::context *ctxt)
56*38fd1498Szrj     : gimple_opt_pass(pass_data_walloca, ctxt), first_time_p (false)
57*38fd1498Szrj   {}
clone()58*38fd1498Szrj   opt_pass *clone () { return new pass_walloca (m_ctxt); }
set_pass_param(unsigned int n,bool param)59*38fd1498Szrj   void set_pass_param (unsigned int n, bool param)
60*38fd1498Szrj     {
61*38fd1498Szrj       gcc_assert (n == 0);
62*38fd1498Szrj       first_time_p = param;
63*38fd1498Szrj     }
64*38fd1498Szrj   virtual bool gate (function *);
65*38fd1498Szrj   virtual unsigned int execute (function *);
66*38fd1498Szrj 
67*38fd1498Szrj  private:
68*38fd1498Szrj   // Set to TRUE the first time we run this pass on a function.
69*38fd1498Szrj   bool first_time_p;
70*38fd1498Szrj };
71*38fd1498Szrj 
72*38fd1498Szrj bool
gate(function * fun ATTRIBUTE_UNUSED)73*38fd1498Szrj pass_walloca::gate (function *fun ATTRIBUTE_UNUSED)
74*38fd1498Szrj {
75*38fd1498Szrj   // The first time this pass is called, it is called before
76*38fd1498Szrj   // optimizations have been run and range information is unavailable,
77*38fd1498Szrj   // so we can only perform strict alloca checking.
78*38fd1498Szrj   if (first_time_p)
79*38fd1498Szrj     return warn_alloca != 0;
80*38fd1498Szrj 
81*38fd1498Szrj   return ((unsigned HOST_WIDE_INT) warn_alloca_limit > 0
82*38fd1498Szrj 	  || (unsigned HOST_WIDE_INT) warn_vla_limit > 0);
83*38fd1498Szrj }
84*38fd1498Szrj 
85*38fd1498Szrj // Possible problematic uses of alloca.
86*38fd1498Szrj enum alloca_type {
87*38fd1498Szrj   // Alloca argument is within known bounds that are appropriate.
88*38fd1498Szrj   ALLOCA_OK,
89*38fd1498Szrj 
90*38fd1498Szrj   // Alloca argument is KNOWN to have a value that is too large.
91*38fd1498Szrj   ALLOCA_BOUND_DEFINITELY_LARGE,
92*38fd1498Szrj 
93*38fd1498Szrj   // Alloca argument may be too large.
94*38fd1498Szrj   ALLOCA_BOUND_MAYBE_LARGE,
95*38fd1498Szrj 
96*38fd1498Szrj   // Alloca argument is bounded but of an indeterminate size.
97*38fd1498Szrj   ALLOCA_BOUND_UNKNOWN,
98*38fd1498Szrj 
99*38fd1498Szrj   // Alloca argument was casted from a signed integer.
100*38fd1498Szrj   ALLOCA_CAST_FROM_SIGNED,
101*38fd1498Szrj 
102*38fd1498Szrj   // Alloca appears in a loop.
103*38fd1498Szrj   ALLOCA_IN_LOOP,
104*38fd1498Szrj 
105*38fd1498Szrj   // Alloca argument is 0.
106*38fd1498Szrj   ALLOCA_ARG_IS_ZERO,
107*38fd1498Szrj 
108*38fd1498Szrj   // Alloca call is unbounded.  That is, there is no controlling
109*38fd1498Szrj   // predicate for its argument.
110*38fd1498Szrj   ALLOCA_UNBOUNDED
111*38fd1498Szrj };
112*38fd1498Szrj 
113*38fd1498Szrj // Type of an alloca call with its corresponding limit, if applicable.
114*38fd1498Szrj struct alloca_type_and_limit {
115*38fd1498Szrj   enum alloca_type type;
116*38fd1498Szrj   // For ALLOCA_BOUND_MAYBE_LARGE and ALLOCA_BOUND_DEFINITELY_LARGE
117*38fd1498Szrj   // types, this field indicates the assumed limit if known or
118*38fd1498Szrj   // integer_zero_node if unknown.  For any other alloca types, this
119*38fd1498Szrj   // field is undefined.
120*38fd1498Szrj   wide_int limit;
121*38fd1498Szrj   alloca_type_and_limit ();
alloca_type_and_limitalloca_type_and_limit122*38fd1498Szrj   alloca_type_and_limit (enum alloca_type type,
123*38fd1498Szrj 			 wide_int i) : type(type), limit(i) { }
alloca_type_and_limitalloca_type_and_limit124*38fd1498Szrj   alloca_type_and_limit (enum alloca_type type) : type(type) { }
125*38fd1498Szrj };
126*38fd1498Szrj 
127*38fd1498Szrj // NOTE: When we get better range info, this entire function becomes
128*38fd1498Szrj // irrelevant, as it should be possible to get range info for an SSA
129*38fd1498Szrj // name at any point in the program.
130*38fd1498Szrj //
131*38fd1498Szrj // We have a few heuristics up our sleeve to determine if a call to
132*38fd1498Szrj // alloca() is within bounds.  Try them out and return the type of
133*38fd1498Szrj // alloca call with its assumed limit (if applicable).
134*38fd1498Szrj //
135*38fd1498Szrj // Given a known argument (ARG) to alloca() and an EDGE (E)
136*38fd1498Szrj // calculating said argument, verify that the last statement in the BB
137*38fd1498Szrj // in E->SRC is a gate comparing ARG to an acceptable bound for
138*38fd1498Szrj // alloca().  See examples below.
139*38fd1498Szrj //
140*38fd1498Szrj // If set, ARG_CASTED is the possible unsigned argument to which ARG
141*38fd1498Szrj // was casted to.  This is to handle cases where the controlling
142*38fd1498Szrj // predicate is looking at a casted value, not the argument itself.
143*38fd1498Szrj //    arg_casted = (size_t) arg;
144*38fd1498Szrj //    if (arg_casted < N)
145*38fd1498Szrj //      goto bb3;
146*38fd1498Szrj //    else
147*38fd1498Szrj //      goto bb5;
148*38fd1498Szrj //
149*38fd1498Szrj // MAX_SIZE is WARN_ALLOCA= adjusted for VLAs.  It is the maximum size
150*38fd1498Szrj // in bytes we allow for arg.
151*38fd1498Szrj 
152*38fd1498Szrj static struct alloca_type_and_limit
alloca_call_type_by_arg(tree arg,tree arg_casted,edge e,unsigned max_size)153*38fd1498Szrj alloca_call_type_by_arg (tree arg, tree arg_casted, edge e, unsigned max_size)
154*38fd1498Szrj {
155*38fd1498Szrj   basic_block bb = e->src;
156*38fd1498Szrj   gimple_stmt_iterator gsi = gsi_last_bb (bb);
157*38fd1498Szrj   gimple *last = gsi_stmt (gsi);
158*38fd1498Szrj   if (!last || gimple_code (last) != GIMPLE_COND)
159*38fd1498Szrj     return alloca_type_and_limit (ALLOCA_UNBOUNDED);
160*38fd1498Szrj 
161*38fd1498Szrj   enum tree_code cond_code = gimple_cond_code (last);
162*38fd1498Szrj   if (e->flags & EDGE_TRUE_VALUE)
163*38fd1498Szrj     ;
164*38fd1498Szrj   else if (e->flags & EDGE_FALSE_VALUE)
165*38fd1498Szrj     cond_code = invert_tree_comparison (cond_code, false);
166*38fd1498Szrj   else
167*38fd1498Szrj     return alloca_type_and_limit (ALLOCA_UNBOUNDED);
168*38fd1498Szrj 
169*38fd1498Szrj   // Check for:
170*38fd1498Szrj   //   if (ARG .COND. N)
171*38fd1498Szrj   //     goto <bb 3>;
172*38fd1498Szrj   //   else
173*38fd1498Szrj   //     goto <bb 4>;
174*38fd1498Szrj   //   <bb 3>:
175*38fd1498Szrj   //   alloca(ARG);
176*38fd1498Szrj   if ((cond_code == LE_EXPR
177*38fd1498Szrj        || cond_code == LT_EXPR
178*38fd1498Szrj        || cond_code == GT_EXPR
179*38fd1498Szrj        || cond_code == GE_EXPR)
180*38fd1498Szrj       && (gimple_cond_lhs (last) == arg
181*38fd1498Szrj 	  || gimple_cond_lhs (last) == arg_casted))
182*38fd1498Szrj     {
183*38fd1498Szrj       if (TREE_CODE (gimple_cond_rhs (last)) == INTEGER_CST)
184*38fd1498Szrj 	{
185*38fd1498Szrj 	  tree rhs = gimple_cond_rhs (last);
186*38fd1498Szrj 	  int tst = wi::cmpu (wi::to_widest (rhs), max_size);
187*38fd1498Szrj 	  if ((cond_code == LT_EXPR && tst == -1)
188*38fd1498Szrj 	      || (cond_code == LE_EXPR && (tst == -1 || tst == 0)))
189*38fd1498Szrj 	    return alloca_type_and_limit (ALLOCA_OK);
190*38fd1498Szrj 	  else
191*38fd1498Szrj 	    {
192*38fd1498Szrj 	      // Let's not get too specific as to how large the limit
193*38fd1498Szrj 	      // may be.  Someone's clearly an idiot when things
194*38fd1498Szrj 	      // degrade into "if (N > Y) alloca(N)".
195*38fd1498Szrj 	      if (cond_code == GT_EXPR || cond_code == GE_EXPR)
196*38fd1498Szrj 		rhs = integer_zero_node;
197*38fd1498Szrj 	      return alloca_type_and_limit (ALLOCA_BOUND_MAYBE_LARGE,
198*38fd1498Szrj 					    wi::to_wide (rhs));
199*38fd1498Szrj 	    }
200*38fd1498Szrj 	}
201*38fd1498Szrj       else
202*38fd1498Szrj 	return alloca_type_and_limit (ALLOCA_BOUND_UNKNOWN);
203*38fd1498Szrj     }
204*38fd1498Szrj 
205*38fd1498Szrj   // Similarly, but check for a comparison with an unknown LIMIT.
206*38fd1498Szrj   //   if (LIMIT .COND. ARG)
207*38fd1498Szrj   //     alloca(arg);
208*38fd1498Szrj   //
209*38fd1498Szrj   //   Where LIMIT has a bound of unknown range.
210*38fd1498Szrj   //
211*38fd1498Szrj   // Note: All conditions of the form (ARG .COND. XXXX) where covered
212*38fd1498Szrj   // by the previous check above, so we only need to look for (LIMIT
213*38fd1498Szrj   // .COND. ARG) here.
214*38fd1498Szrj   tree limit = gimple_cond_lhs (last);
215*38fd1498Szrj   if ((gimple_cond_rhs (last) == arg
216*38fd1498Szrj        || gimple_cond_rhs (last) == arg_casted)
217*38fd1498Szrj       && TREE_CODE (limit) == SSA_NAME)
218*38fd1498Szrj     {
219*38fd1498Szrj       wide_int min, max;
220*38fd1498Szrj       value_range_type range_type = get_range_info (limit, &min, &max);
221*38fd1498Szrj 
222*38fd1498Szrj       if (range_type == VR_UNDEFINED || range_type == VR_VARYING)
223*38fd1498Szrj 	return alloca_type_and_limit (ALLOCA_BOUND_UNKNOWN);
224*38fd1498Szrj 
225*38fd1498Szrj       // ?? It looks like the above `if' is unnecessary, as we never
226*38fd1498Szrj       // get any VR_RANGE or VR_ANTI_RANGE here.  If we had a range
227*38fd1498Szrj       // for LIMIT, I suppose we would have taken care of it in
228*38fd1498Szrj       // alloca_call_type(), or handled above where we handle (ARG .COND. N).
229*38fd1498Szrj       //
230*38fd1498Szrj       // If this ever triggers, we should probably figure out why and
231*38fd1498Szrj       // handle it, though it is likely to be just an ALLOCA_UNBOUNDED.
232*38fd1498Szrj       return alloca_type_and_limit (ALLOCA_UNBOUNDED);
233*38fd1498Szrj     }
234*38fd1498Szrj 
235*38fd1498Szrj   return alloca_type_and_limit (ALLOCA_UNBOUNDED);
236*38fd1498Szrj }
237*38fd1498Szrj 
238*38fd1498Szrj // Return TRUE if SSA's definition is a cast from a signed type.
239*38fd1498Szrj // If so, set *INVALID_CASTED_TYPE to the signed type.
240*38fd1498Szrj 
241*38fd1498Szrj static bool
cast_from_signed_p(tree ssa,tree * invalid_casted_type)242*38fd1498Szrj cast_from_signed_p (tree ssa, tree *invalid_casted_type)
243*38fd1498Szrj {
244*38fd1498Szrj   gimple *def = SSA_NAME_DEF_STMT (ssa);
245*38fd1498Szrj   if (def
246*38fd1498Szrj       && !gimple_nop_p (def)
247*38fd1498Szrj       && gimple_assign_cast_p (def)
248*38fd1498Szrj       && !TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def))))
249*38fd1498Szrj     {
250*38fd1498Szrj       *invalid_casted_type = TREE_TYPE (gimple_assign_rhs1 (def));
251*38fd1498Szrj       return true;
252*38fd1498Szrj     }
253*38fd1498Szrj   return false;
254*38fd1498Szrj }
255*38fd1498Szrj 
256*38fd1498Szrj // Return TRUE if X has a maximum range of MAX, basically covering the
257*38fd1498Szrj // entire domain, in which case it's no range at all.
258*38fd1498Szrj 
259*38fd1498Szrj static bool
is_max(tree x,wide_int max)260*38fd1498Szrj is_max (tree x, wide_int max)
261*38fd1498Szrj {
262*38fd1498Szrj   return wi::max_value (TREE_TYPE (x)) == max;
263*38fd1498Szrj }
264*38fd1498Szrj 
265*38fd1498Szrj // Analyze the alloca call in STMT and return the alloca type with its
266*38fd1498Szrj // corresponding limit (if applicable).  IS_VLA is set if the alloca
267*38fd1498Szrj // call was created by the gimplifier for a VLA.
268*38fd1498Szrj //
269*38fd1498Szrj // If the alloca call may be too large because of a cast from a signed
270*38fd1498Szrj // type to an unsigned type, set *INVALID_CASTED_TYPE to the
271*38fd1498Szrj // problematic signed type.
272*38fd1498Szrj 
273*38fd1498Szrj static struct alloca_type_and_limit
alloca_call_type(gimple * stmt,bool is_vla,tree * invalid_casted_type)274*38fd1498Szrj alloca_call_type (gimple *stmt, bool is_vla, tree *invalid_casted_type)
275*38fd1498Szrj {
276*38fd1498Szrj   gcc_assert (gimple_alloca_call_p (stmt));
277*38fd1498Szrj   bool tentative_cast_from_signed = false;
278*38fd1498Szrj   tree len = gimple_call_arg (stmt, 0);
279*38fd1498Szrj   tree len_casted = NULL;
280*38fd1498Szrj   wide_int min, max;
281*38fd1498Szrj   edge_iterator ei;
282*38fd1498Szrj   edge e;
283*38fd1498Szrj 
284*38fd1498Szrj   gcc_assert (!is_vla || (unsigned HOST_WIDE_INT) warn_vla_limit > 0);
285*38fd1498Szrj   gcc_assert (is_vla || (unsigned HOST_WIDE_INT) warn_alloca_limit > 0);
286*38fd1498Szrj 
287*38fd1498Szrj   // Adjust warn_alloca_max_size for VLAs, by taking the underlying
288*38fd1498Szrj   // type into account.
289*38fd1498Szrj   unsigned HOST_WIDE_INT max_size;
290*38fd1498Szrj   if (is_vla)
291*38fd1498Szrj     max_size = (unsigned HOST_WIDE_INT) warn_vla_limit;
292*38fd1498Szrj   else
293*38fd1498Szrj     max_size = (unsigned HOST_WIDE_INT) warn_alloca_limit;
294*38fd1498Szrj 
295*38fd1498Szrj   // Check for the obviously bounded case.
296*38fd1498Szrj   if (TREE_CODE (len) == INTEGER_CST)
297*38fd1498Szrj     {
298*38fd1498Szrj       if (tree_to_uhwi (len) > max_size)
299*38fd1498Szrj 	return alloca_type_and_limit (ALLOCA_BOUND_DEFINITELY_LARGE,
300*38fd1498Szrj 				      wi::to_wide (len));
301*38fd1498Szrj       if (integer_zerop (len))
302*38fd1498Szrj 	return alloca_type_and_limit (ALLOCA_ARG_IS_ZERO);
303*38fd1498Szrj 
304*38fd1498Szrj       return alloca_type_and_limit (ALLOCA_OK);
305*38fd1498Szrj     }
306*38fd1498Szrj 
307*38fd1498Szrj   // Check the range info if available.
308*38fd1498Szrj   if (TREE_CODE (len) == SSA_NAME)
309*38fd1498Szrj     {
310*38fd1498Szrj       value_range_type range_type = get_range_info (len, &min, &max);
311*38fd1498Szrj       if (range_type == VR_RANGE)
312*38fd1498Szrj 	{
313*38fd1498Szrj 	  if (wi::leu_p (max, max_size))
314*38fd1498Szrj 	    return alloca_type_and_limit (ALLOCA_OK);
315*38fd1498Szrj 	  else
316*38fd1498Szrj 	    {
317*38fd1498Szrj 	      // A cast may have created a range we don't care
318*38fd1498Szrj 	      // about.  For instance, a cast from 16-bit to
319*38fd1498Szrj 	      // 32-bit creates a range of 0..65535, even if there
320*38fd1498Szrj 	      // is not really a determinable range in the
321*38fd1498Szrj 	      // underlying code.  In this case, look through the
322*38fd1498Szrj 	      // cast at the original argument, and fall through
323*38fd1498Szrj 	      // to look at other alternatives.
324*38fd1498Szrj 	      //
325*38fd1498Szrj 	      // We only look at through the cast when its from
326*38fd1498Szrj 	      // unsigned to unsigned, otherwise we may risk
327*38fd1498Szrj 	      // looking at SIGNED_INT < N, which is clearly not
328*38fd1498Szrj 	      // what we want.  In this case, we'd be interested
329*38fd1498Szrj 	      // in a VR_RANGE of [0..N].
330*38fd1498Szrj 	      //
331*38fd1498Szrj 	      // Note: None of this is perfect, and should all go
332*38fd1498Szrj 	      // away with better range information.  But it gets
333*38fd1498Szrj 	      // most of the cases.
334*38fd1498Szrj 	      gimple *def = SSA_NAME_DEF_STMT (len);
335*38fd1498Szrj 	      if (gimple_assign_cast_p (def))
336*38fd1498Szrj 		{
337*38fd1498Szrj 		  tree rhs1 = gimple_assign_rhs1 (def);
338*38fd1498Szrj 		  tree rhs1type = TREE_TYPE (rhs1);
339*38fd1498Szrj 
340*38fd1498Szrj 		  // Bail if the argument type is not valid.
341*38fd1498Szrj 		  if (!INTEGRAL_TYPE_P (rhs1type))
342*38fd1498Szrj 		    return alloca_type_and_limit (ALLOCA_OK);
343*38fd1498Szrj 
344*38fd1498Szrj 		  if (TYPE_UNSIGNED (rhs1type))
345*38fd1498Szrj 		    {
346*38fd1498Szrj 		      len_casted = rhs1;
347*38fd1498Szrj 		      range_type = get_range_info (len_casted, &min, &max);
348*38fd1498Szrj 		    }
349*38fd1498Szrj 		}
350*38fd1498Szrj 	      // An unknown range or a range of the entire domain is
351*38fd1498Szrj 	      // really no range at all.
352*38fd1498Szrj 	      if (range_type == VR_VARYING
353*38fd1498Szrj 		  || (!len_casted && is_max (len, max))
354*38fd1498Szrj 		  || (len_casted && is_max (len_casted, max)))
355*38fd1498Szrj 		{
356*38fd1498Szrj 		  // Fall through.
357*38fd1498Szrj 		}
358*38fd1498Szrj 	      else if (range_type == VR_ANTI_RANGE)
359*38fd1498Szrj 		return alloca_type_and_limit (ALLOCA_UNBOUNDED);
360*38fd1498Szrj 	      else if (range_type != VR_VARYING)
361*38fd1498Szrj 		return alloca_type_and_limit (ALLOCA_BOUND_MAYBE_LARGE, max);
362*38fd1498Szrj 	    }
363*38fd1498Szrj 	}
364*38fd1498Szrj       else if (range_type == VR_ANTI_RANGE)
365*38fd1498Szrj 	{
366*38fd1498Szrj 	  // There may be some wrapping around going on.  Catch it
367*38fd1498Szrj 	  // with this heuristic.  Hopefully, this VR_ANTI_RANGE
368*38fd1498Szrj 	  // nonsense will go away, and we won't have to catch the
369*38fd1498Szrj 	  // sign conversion problems with this crap.
370*38fd1498Szrj 	  //
371*38fd1498Szrj 	  // This is here to catch things like:
372*38fd1498Szrj 	  // void foo(signed int n) {
373*38fd1498Szrj 	  //   if (n < 100)
374*38fd1498Szrj 	  //     alloca(n);
375*38fd1498Szrj 	  //   ...
376*38fd1498Szrj 	  // }
377*38fd1498Szrj 	  if (cast_from_signed_p (len, invalid_casted_type))
378*38fd1498Szrj 	    {
379*38fd1498Szrj 	      // Unfortunately this also triggers:
380*38fd1498Szrj 	      //
381*38fd1498Szrj 	      // __SIZE_TYPE__ n = (__SIZE_TYPE__)blah;
382*38fd1498Szrj 	      // if (n < 100)
383*38fd1498Szrj 	      //   alloca(n);
384*38fd1498Szrj 	      //
385*38fd1498Szrj 	      // ...which is clearly bounded.  So, double check that
386*38fd1498Szrj 	      // the paths leading up to the size definitely don't
387*38fd1498Szrj 	      // have a bound.
388*38fd1498Szrj 	      tentative_cast_from_signed = true;
389*38fd1498Szrj 	    }
390*38fd1498Szrj 	}
391*38fd1498Szrj       // No easily determined range and try other things.
392*38fd1498Szrj     }
393*38fd1498Szrj 
394*38fd1498Szrj   // If we couldn't find anything, try a few heuristics for things we
395*38fd1498Szrj   // can easily determine.  Check these misc cases but only accept
396*38fd1498Szrj   // them if all predecessors have a known bound.
397*38fd1498Szrj   struct alloca_type_and_limit ret = alloca_type_and_limit (ALLOCA_OK);
398*38fd1498Szrj   FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->preds)
399*38fd1498Szrj     {
400*38fd1498Szrj       gcc_assert (!len_casted || TYPE_UNSIGNED (TREE_TYPE (len_casted)));
401*38fd1498Szrj       ret = alloca_call_type_by_arg (len, len_casted, e, max_size);
402*38fd1498Szrj       if (ret.type != ALLOCA_OK)
403*38fd1498Szrj 	break;
404*38fd1498Szrj     }
405*38fd1498Szrj 
406*38fd1498Szrj   if (ret.type != ALLOCA_OK && tentative_cast_from_signed)
407*38fd1498Szrj     ret = alloca_type_and_limit (ALLOCA_CAST_FROM_SIGNED);
408*38fd1498Szrj 
409*38fd1498Szrj   // If we have a declared maximum size, we can take it into account.
410*38fd1498Szrj   if (ret.type != ALLOCA_OK
411*38fd1498Szrj       && gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX))
412*38fd1498Szrj     {
413*38fd1498Szrj       tree arg = gimple_call_arg (stmt, 2);
414*38fd1498Szrj       if (compare_tree_int (arg, max_size) <= 0)
415*38fd1498Szrj 	ret = alloca_type_and_limit (ALLOCA_OK);
416*38fd1498Szrj       else
417*38fd1498Szrj 	ret = alloca_type_and_limit (ALLOCA_BOUND_MAYBE_LARGE,
418*38fd1498Szrj 				     wi::to_wide (arg));
419*38fd1498Szrj     }
420*38fd1498Szrj 
421*38fd1498Szrj   return ret;
422*38fd1498Szrj }
423*38fd1498Szrj 
424*38fd1498Szrj // Return TRUE if STMT is in a loop, otherwise return FALSE.
425*38fd1498Szrj 
426*38fd1498Szrj static bool
in_loop_p(gimple * stmt)427*38fd1498Szrj in_loop_p (gimple *stmt)
428*38fd1498Szrj {
429*38fd1498Szrj   basic_block bb = gimple_bb (stmt);
430*38fd1498Szrj   return
431*38fd1498Szrj     bb->loop_father && bb->loop_father->header != ENTRY_BLOCK_PTR_FOR_FN (cfun);
432*38fd1498Szrj }
433*38fd1498Szrj 
434*38fd1498Szrj unsigned int
execute(function * fun)435*38fd1498Szrj pass_walloca::execute (function *fun)
436*38fd1498Szrj {
437*38fd1498Szrj   basic_block bb;
438*38fd1498Szrj   FOR_EACH_BB_FN (bb, fun)
439*38fd1498Szrj     {
440*38fd1498Szrj       for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
441*38fd1498Szrj 	   gsi_next (&si))
442*38fd1498Szrj 	{
443*38fd1498Szrj 	  gimple *stmt = gsi_stmt (si);
444*38fd1498Szrj 	  location_t loc = gimple_location (stmt);
445*38fd1498Szrj 
446*38fd1498Szrj 	  if (!gimple_alloca_call_p (stmt))
447*38fd1498Szrj 	    continue;
448*38fd1498Szrj 
449*38fd1498Szrj 	  const bool is_vla
450*38fd1498Szrj 	    = gimple_call_alloca_for_var_p (as_a <gcall *> (stmt));
451*38fd1498Szrj 
452*38fd1498Szrj 	  // Strict mode whining for VLAs is handled by the front-end,
453*38fd1498Szrj 	  // so we can safely ignore this case.  Also, ignore VLAs if
454*38fd1498Szrj 	  // the user doesn't care about them.
455*38fd1498Szrj 	  if (is_vla
456*38fd1498Szrj 	      && (warn_vla > 0 || !warn_vla_limit))
457*38fd1498Szrj 	    continue;
458*38fd1498Szrj 
459*38fd1498Szrj 	  if (!is_vla && (warn_alloca || !warn_alloca_limit))
460*38fd1498Szrj 	    {
461*38fd1498Szrj 	      if (warn_alloca)
462*38fd1498Szrj 		warning_at (loc, OPT_Walloca, G_("use of %<alloca%>"));
463*38fd1498Szrj 	      continue;
464*38fd1498Szrj 	    }
465*38fd1498Szrj 
466*38fd1498Szrj 	  tree invalid_casted_type = NULL;
467*38fd1498Szrj 	  struct alloca_type_and_limit t
468*38fd1498Szrj 	    = alloca_call_type (stmt, is_vla, &invalid_casted_type);
469*38fd1498Szrj 
470*38fd1498Szrj 	  // Even if we think the alloca call is OK, make sure it's not in a
471*38fd1498Szrj 	  // loop, except for a VLA, since VLAs are guaranteed to be cleaned
472*38fd1498Szrj 	  // up when they go out of scope, including in a loop.
473*38fd1498Szrj 	  if (t.type == ALLOCA_OK && !is_vla && in_loop_p (stmt))
474*38fd1498Szrj 	    t = alloca_type_and_limit (ALLOCA_IN_LOOP);
475*38fd1498Szrj 
476*38fd1498Szrj 	  enum opt_code wcode
477*38fd1498Szrj 	    = is_vla ? OPT_Wvla_larger_than_ : OPT_Walloca_larger_than_;
478*38fd1498Szrj 	  char buff[WIDE_INT_MAX_PRECISION / 4 + 4];
479*38fd1498Szrj 	  switch (t.type)
480*38fd1498Szrj 	    {
481*38fd1498Szrj 	    case ALLOCA_OK:
482*38fd1498Szrj 	      break;
483*38fd1498Szrj 	    case ALLOCA_BOUND_MAYBE_LARGE:
484*38fd1498Szrj 	      if (warning_at (loc, wcode,
485*38fd1498Szrj 			      is_vla ? G_("argument to variable-length array "
486*38fd1498Szrj 					  "may be too large")
487*38fd1498Szrj 			      : G_("argument to %<alloca%> may be too large"))
488*38fd1498Szrj 		  && t.limit != 0)
489*38fd1498Szrj 		{
490*38fd1498Szrj 		  print_decu (t.limit, buff);
491*38fd1498Szrj 		  inform (loc, G_("limit is %u bytes, but argument "
492*38fd1498Szrj 				  "may be as large as %s"),
493*38fd1498Szrj 			  is_vla ? warn_vla_limit : warn_alloca_limit, buff);
494*38fd1498Szrj 		}
495*38fd1498Szrj 	      break;
496*38fd1498Szrj 	    case ALLOCA_BOUND_DEFINITELY_LARGE:
497*38fd1498Szrj 	      if (warning_at (loc, wcode,
498*38fd1498Szrj 			      is_vla ? G_("argument to variable-length array "
499*38fd1498Szrj 					  "is too large")
500*38fd1498Szrj 			      : G_("argument to %<alloca%> is too large"))
501*38fd1498Szrj 		  && t.limit != 0)
502*38fd1498Szrj 		{
503*38fd1498Szrj 		  print_decu (t.limit, buff);
504*38fd1498Szrj 		  inform (loc, G_("limit is %u bytes, but argument is %s"),
505*38fd1498Szrj 			  is_vla ? warn_vla_limit : warn_alloca_limit, buff);
506*38fd1498Szrj 		}
507*38fd1498Szrj 	      break;
508*38fd1498Szrj 	    case ALLOCA_BOUND_UNKNOWN:
509*38fd1498Szrj 	      warning_at (loc, wcode,
510*38fd1498Szrj 			  is_vla ? G_("variable-length array bound is unknown")
511*38fd1498Szrj 			  : G_("%<alloca%> bound is unknown"));
512*38fd1498Szrj 	      break;
513*38fd1498Szrj 	    case ALLOCA_UNBOUNDED:
514*38fd1498Szrj 	      warning_at (loc, wcode,
515*38fd1498Szrj 			  is_vla ? G_("unbounded use of variable-length array")
516*38fd1498Szrj 			  : G_("unbounded use of %<alloca%>"));
517*38fd1498Szrj 	      break;
518*38fd1498Szrj 	    case ALLOCA_IN_LOOP:
519*38fd1498Szrj 	      gcc_assert (!is_vla);
520*38fd1498Szrj 	      warning_at (loc, wcode, G_("use of %<alloca%> within a loop"));
521*38fd1498Szrj 	      break;
522*38fd1498Szrj 	    case ALLOCA_CAST_FROM_SIGNED:
523*38fd1498Szrj 	      gcc_assert (invalid_casted_type != NULL_TREE);
524*38fd1498Szrj 	      warning_at (loc, wcode,
525*38fd1498Szrj 			  is_vla ? G_("argument to variable-length array "
526*38fd1498Szrj 				      "may be too large due to "
527*38fd1498Szrj 				      "conversion from %qT to %qT")
528*38fd1498Szrj 			  : G_("argument to %<alloca%> may be too large "
529*38fd1498Szrj 			       "due to conversion from %qT to %qT"),
530*38fd1498Szrj 			  invalid_casted_type, size_type_node);
531*38fd1498Szrj 	      break;
532*38fd1498Szrj 	    case ALLOCA_ARG_IS_ZERO:
533*38fd1498Szrj 	      warning_at (loc, wcode,
534*38fd1498Szrj 			  is_vla ? G_("argument to variable-length array "
535*38fd1498Szrj 				      "is zero")
536*38fd1498Szrj 			  : G_("argument to %<alloca%> is zero"));
537*38fd1498Szrj 	      break;
538*38fd1498Szrj 	    default:
539*38fd1498Szrj 	      gcc_unreachable ();
540*38fd1498Szrj 	    }
541*38fd1498Szrj 	}
542*38fd1498Szrj     }
543*38fd1498Szrj   return 0;
544*38fd1498Szrj }
545*38fd1498Szrj 
546*38fd1498Szrj gimple_opt_pass *
make_pass_walloca(gcc::context * ctxt)547*38fd1498Szrj make_pass_walloca (gcc::context *ctxt)
548*38fd1498Szrj {
549*38fd1498Szrj   return new pass_walloca (ctxt);
550*38fd1498Szrj }
551