1*38fd1498Szrj /* Gimple decl, type, and expression support functions.
2*38fd1498Szrj
3*38fd1498Szrj Copyright (C) 2007-2018 Free Software Foundation, Inc.
4*38fd1498Szrj Contributed by Aldy Hernandez <aldyh@redhat.com>
5*38fd1498Szrj
6*38fd1498Szrj This file is part of GCC.
7*38fd1498Szrj
8*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
9*38fd1498Szrj the terms of the GNU General Public License as published by the Free
10*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
11*38fd1498Szrj version.
12*38fd1498Szrj
13*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
15*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16*38fd1498Szrj for more details.
17*38fd1498Szrj
18*38fd1498Szrj You should have received a copy of the GNU General Public License
19*38fd1498Szrj along with GCC; see the file COPYING3. If not see
20*38fd1498Szrj <http://www.gnu.org/licenses/>. */
21*38fd1498Szrj
22*38fd1498Szrj #include "config.h"
23*38fd1498Szrj #include "system.h"
24*38fd1498Szrj #include "coretypes.h"
25*38fd1498Szrj #include "backend.h"
26*38fd1498Szrj #include "tree.h"
27*38fd1498Szrj #include "gimple.h"
28*38fd1498Szrj #include "stringpool.h"
29*38fd1498Szrj #include "gimple-ssa.h"
30*38fd1498Szrj #include "fold-const.h"
31*38fd1498Szrj #include "tree-eh.h"
32*38fd1498Szrj #include "gimplify.h"
33*38fd1498Szrj #include "stor-layout.h"
34*38fd1498Szrj #include "demangle.h"
35*38fd1498Szrj #include "hash-set.h"
36*38fd1498Szrj #include "rtl.h"
37*38fd1498Szrj #include "tree-pass.h"
38*38fd1498Szrj #include "stringpool.h"
39*38fd1498Szrj #include "attribs.h"
40*38fd1498Szrj
41*38fd1498Szrj /* ----- Type related ----- */
42*38fd1498Szrj
43*38fd1498Szrj /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is a
44*38fd1498Szrj useless type conversion, otherwise return false.
45*38fd1498Szrj
46*38fd1498Szrj This function implicitly defines the middle-end type system. With
47*38fd1498Szrj the notion of 'a < b' meaning that useless_type_conversion_p (a, b)
48*38fd1498Szrj holds and 'a > b' meaning that useless_type_conversion_p (b, a) holds,
49*38fd1498Szrj the following invariants shall be fulfilled:
50*38fd1498Szrj
51*38fd1498Szrj 1) useless_type_conversion_p is transitive.
52*38fd1498Szrj If a < b and b < c then a < c.
53*38fd1498Szrj
54*38fd1498Szrj 2) useless_type_conversion_p is not symmetric.
55*38fd1498Szrj From a < b does not follow a > b.
56*38fd1498Szrj
57*38fd1498Szrj 3) Types define the available set of operations applicable to values.
58*38fd1498Szrj A type conversion is useless if the operations for the target type
59*38fd1498Szrj is a subset of the operations for the source type. For example
60*38fd1498Szrj casts to void* are useless, casts from void* are not (void* can't
61*38fd1498Szrj be dereferenced or offsetted, but copied, hence its set of operations
62*38fd1498Szrj is a strict subset of that of all other data pointer types). Casts
63*38fd1498Szrj to const T* are useless (can't be written to), casts from const T*
64*38fd1498Szrj to T* are not. */
65*38fd1498Szrj
66*38fd1498Szrj bool
useless_type_conversion_p(tree outer_type,tree inner_type)67*38fd1498Szrj useless_type_conversion_p (tree outer_type, tree inner_type)
68*38fd1498Szrj {
69*38fd1498Szrj /* Do the following before stripping toplevel qualifiers. */
70*38fd1498Szrj if (POINTER_TYPE_P (inner_type)
71*38fd1498Szrj && POINTER_TYPE_P (outer_type))
72*38fd1498Szrj {
73*38fd1498Szrj /* Do not lose casts between pointers to different address spaces. */
74*38fd1498Szrj if (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
75*38fd1498Szrj != TYPE_ADDR_SPACE (TREE_TYPE (inner_type)))
76*38fd1498Szrj return false;
77*38fd1498Szrj /* Do not lose casts to function pointer types. */
78*38fd1498Szrj if ((TREE_CODE (TREE_TYPE (outer_type)) == FUNCTION_TYPE
79*38fd1498Szrj || TREE_CODE (TREE_TYPE (outer_type)) == METHOD_TYPE)
80*38fd1498Szrj && !(TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE
81*38fd1498Szrj || TREE_CODE (TREE_TYPE (inner_type)) == METHOD_TYPE))
82*38fd1498Szrj return false;
83*38fd1498Szrj }
84*38fd1498Szrj
85*38fd1498Szrj /* From now on qualifiers on value types do not matter. */
86*38fd1498Szrj inner_type = TYPE_MAIN_VARIANT (inner_type);
87*38fd1498Szrj outer_type = TYPE_MAIN_VARIANT (outer_type);
88*38fd1498Szrj
89*38fd1498Szrj if (inner_type == outer_type)
90*38fd1498Szrj return true;
91*38fd1498Szrj
92*38fd1498Szrj /* Changes in machine mode are never useless conversions because the RTL
93*38fd1498Szrj middle-end expects explicit conversions between modes. */
94*38fd1498Szrj if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type))
95*38fd1498Szrj return false;
96*38fd1498Szrj
97*38fd1498Szrj /* If both the inner and outer types are integral types, then the
98*38fd1498Szrj conversion is not necessary if they have the same mode and
99*38fd1498Szrj signedness and precision, and both or neither are boolean. */
100*38fd1498Szrj if (INTEGRAL_TYPE_P (inner_type)
101*38fd1498Szrj && INTEGRAL_TYPE_P (outer_type))
102*38fd1498Szrj {
103*38fd1498Szrj /* Preserve changes in signedness or precision. */
104*38fd1498Szrj if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
105*38fd1498Szrj || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
106*38fd1498Szrj return false;
107*38fd1498Szrj
108*38fd1498Szrj /* Preserve conversions to/from BOOLEAN_TYPE if types are not
109*38fd1498Szrj of precision one. */
110*38fd1498Szrj if (((TREE_CODE (inner_type) == BOOLEAN_TYPE)
111*38fd1498Szrj != (TREE_CODE (outer_type) == BOOLEAN_TYPE))
112*38fd1498Szrj && TYPE_PRECISION (outer_type) != 1)
113*38fd1498Szrj return false;
114*38fd1498Szrj
115*38fd1498Szrj /* We don't need to preserve changes in the types minimum or
116*38fd1498Szrj maximum value in general as these do not generate code
117*38fd1498Szrj unless the types precisions are different. */
118*38fd1498Szrj return true;
119*38fd1498Szrj }
120*38fd1498Szrj
121*38fd1498Szrj /* Scalar floating point types with the same mode are compatible. */
122*38fd1498Szrj else if (SCALAR_FLOAT_TYPE_P (inner_type)
123*38fd1498Szrj && SCALAR_FLOAT_TYPE_P (outer_type))
124*38fd1498Szrj return true;
125*38fd1498Szrj
126*38fd1498Szrj /* Fixed point types with the same mode are compatible. */
127*38fd1498Szrj else if (FIXED_POINT_TYPE_P (inner_type)
128*38fd1498Szrj && FIXED_POINT_TYPE_P (outer_type))
129*38fd1498Szrj return TYPE_SATURATING (inner_type) == TYPE_SATURATING (outer_type);
130*38fd1498Szrj
131*38fd1498Szrj /* We need to take special care recursing to pointed-to types. */
132*38fd1498Szrj else if (POINTER_TYPE_P (inner_type)
133*38fd1498Szrj && POINTER_TYPE_P (outer_type))
134*38fd1498Szrj {
135*38fd1498Szrj /* We do not care for const qualification of the pointed-to types
136*38fd1498Szrj as const qualification has no semantic value to the middle-end. */
137*38fd1498Szrj
138*38fd1498Szrj /* Otherwise pointers/references are equivalent. */
139*38fd1498Szrj return true;
140*38fd1498Szrj }
141*38fd1498Szrj
142*38fd1498Szrj /* Recurse for complex types. */
143*38fd1498Szrj else if (TREE_CODE (inner_type) == COMPLEX_TYPE
144*38fd1498Szrj && TREE_CODE (outer_type) == COMPLEX_TYPE)
145*38fd1498Szrj return useless_type_conversion_p (TREE_TYPE (outer_type),
146*38fd1498Szrj TREE_TYPE (inner_type));
147*38fd1498Szrj
148*38fd1498Szrj /* Recurse for vector types with the same number of subparts. */
149*38fd1498Szrj else if (TREE_CODE (inner_type) == VECTOR_TYPE
150*38fd1498Szrj && TREE_CODE (outer_type) == VECTOR_TYPE
151*38fd1498Szrj && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type))
152*38fd1498Szrj return useless_type_conversion_p (TREE_TYPE (outer_type),
153*38fd1498Szrj TREE_TYPE (inner_type));
154*38fd1498Szrj
155*38fd1498Szrj else if (TREE_CODE (inner_type) == ARRAY_TYPE
156*38fd1498Szrj && TREE_CODE (outer_type) == ARRAY_TYPE)
157*38fd1498Szrj {
158*38fd1498Szrj /* Preserve various attributes. */
159*38fd1498Szrj if (TYPE_REVERSE_STORAGE_ORDER (inner_type)
160*38fd1498Szrj != TYPE_REVERSE_STORAGE_ORDER (outer_type))
161*38fd1498Szrj return false;
162*38fd1498Szrj if (TYPE_STRING_FLAG (inner_type) != TYPE_STRING_FLAG (outer_type))
163*38fd1498Szrj return false;
164*38fd1498Szrj
165*38fd1498Szrj /* Conversions from array types with unknown extent to
166*38fd1498Szrj array types with known extent are not useless. */
167*38fd1498Szrj if (!TYPE_DOMAIN (inner_type) && TYPE_DOMAIN (outer_type))
168*38fd1498Szrj return false;
169*38fd1498Szrj
170*38fd1498Szrj /* Nor are conversions from array types with non-constant size to
171*38fd1498Szrj array types with constant size or to different size. */
172*38fd1498Szrj if (TYPE_SIZE (outer_type)
173*38fd1498Szrj && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST
174*38fd1498Szrj && (!TYPE_SIZE (inner_type)
175*38fd1498Szrj || TREE_CODE (TYPE_SIZE (inner_type)) != INTEGER_CST
176*38fd1498Szrj || !tree_int_cst_equal (TYPE_SIZE (outer_type),
177*38fd1498Szrj TYPE_SIZE (inner_type))))
178*38fd1498Szrj return false;
179*38fd1498Szrj
180*38fd1498Szrj /* Check conversions between arrays with partially known extents.
181*38fd1498Szrj If the array min/max values are constant they have to match.
182*38fd1498Szrj Otherwise allow conversions to unknown and variable extents.
183*38fd1498Szrj In particular this declares conversions that may change the
184*38fd1498Szrj mode to BLKmode as useless. */
185*38fd1498Szrj if (TYPE_DOMAIN (inner_type)
186*38fd1498Szrj && TYPE_DOMAIN (outer_type)
187*38fd1498Szrj && TYPE_DOMAIN (inner_type) != TYPE_DOMAIN (outer_type))
188*38fd1498Szrj {
189*38fd1498Szrj tree inner_min = TYPE_MIN_VALUE (TYPE_DOMAIN (inner_type));
190*38fd1498Szrj tree outer_min = TYPE_MIN_VALUE (TYPE_DOMAIN (outer_type));
191*38fd1498Szrj tree inner_max = TYPE_MAX_VALUE (TYPE_DOMAIN (inner_type));
192*38fd1498Szrj tree outer_max = TYPE_MAX_VALUE (TYPE_DOMAIN (outer_type));
193*38fd1498Szrj
194*38fd1498Szrj /* After gimplification a variable min/max value carries no
195*38fd1498Szrj additional information compared to a NULL value. All that
196*38fd1498Szrj matters has been lowered to be part of the IL. */
197*38fd1498Szrj if (inner_min && TREE_CODE (inner_min) != INTEGER_CST)
198*38fd1498Szrj inner_min = NULL_TREE;
199*38fd1498Szrj if (outer_min && TREE_CODE (outer_min) != INTEGER_CST)
200*38fd1498Szrj outer_min = NULL_TREE;
201*38fd1498Szrj if (inner_max && TREE_CODE (inner_max) != INTEGER_CST)
202*38fd1498Szrj inner_max = NULL_TREE;
203*38fd1498Szrj if (outer_max && TREE_CODE (outer_max) != INTEGER_CST)
204*38fd1498Szrj outer_max = NULL_TREE;
205*38fd1498Szrj
206*38fd1498Szrj /* Conversions NULL / variable <- cst are useless, but not
207*38fd1498Szrj the other way around. */
208*38fd1498Szrj if (outer_min
209*38fd1498Szrj && (!inner_min
210*38fd1498Szrj || !tree_int_cst_equal (inner_min, outer_min)))
211*38fd1498Szrj return false;
212*38fd1498Szrj if (outer_max
213*38fd1498Szrj && (!inner_max
214*38fd1498Szrj || !tree_int_cst_equal (inner_max, outer_max)))
215*38fd1498Szrj return false;
216*38fd1498Szrj }
217*38fd1498Szrj
218*38fd1498Szrj /* Recurse on the element check. */
219*38fd1498Szrj return useless_type_conversion_p (TREE_TYPE (outer_type),
220*38fd1498Szrj TREE_TYPE (inner_type));
221*38fd1498Szrj }
222*38fd1498Szrj
223*38fd1498Szrj else if ((TREE_CODE (inner_type) == FUNCTION_TYPE
224*38fd1498Szrj || TREE_CODE (inner_type) == METHOD_TYPE)
225*38fd1498Szrj && TREE_CODE (inner_type) == TREE_CODE (outer_type))
226*38fd1498Szrj {
227*38fd1498Szrj tree outer_parm, inner_parm;
228*38fd1498Szrj
229*38fd1498Szrj /* If the return types are not compatible bail out. */
230*38fd1498Szrj if (!useless_type_conversion_p (TREE_TYPE (outer_type),
231*38fd1498Szrj TREE_TYPE (inner_type)))
232*38fd1498Szrj return false;
233*38fd1498Szrj
234*38fd1498Szrj /* Method types should belong to a compatible base class. */
235*38fd1498Szrj if (TREE_CODE (inner_type) == METHOD_TYPE
236*38fd1498Szrj && !useless_type_conversion_p (TYPE_METHOD_BASETYPE (outer_type),
237*38fd1498Szrj TYPE_METHOD_BASETYPE (inner_type)))
238*38fd1498Szrj return false;
239*38fd1498Szrj
240*38fd1498Szrj /* A conversion to an unprototyped argument list is ok. */
241*38fd1498Szrj if (!prototype_p (outer_type))
242*38fd1498Szrj return true;
243*38fd1498Szrj
244*38fd1498Szrj /* If the unqualified argument types are compatible the conversion
245*38fd1498Szrj is useless. */
246*38fd1498Szrj if (TYPE_ARG_TYPES (outer_type) == TYPE_ARG_TYPES (inner_type))
247*38fd1498Szrj return true;
248*38fd1498Szrj
249*38fd1498Szrj for (outer_parm = TYPE_ARG_TYPES (outer_type),
250*38fd1498Szrj inner_parm = TYPE_ARG_TYPES (inner_type);
251*38fd1498Szrj outer_parm && inner_parm;
252*38fd1498Szrj outer_parm = TREE_CHAIN (outer_parm),
253*38fd1498Szrj inner_parm = TREE_CHAIN (inner_parm))
254*38fd1498Szrj if (!useless_type_conversion_p
255*38fd1498Szrj (TYPE_MAIN_VARIANT (TREE_VALUE (outer_parm)),
256*38fd1498Szrj TYPE_MAIN_VARIANT (TREE_VALUE (inner_parm))))
257*38fd1498Szrj return false;
258*38fd1498Szrj
259*38fd1498Szrj /* If there is a mismatch in the number of arguments the functions
260*38fd1498Szrj are not compatible. */
261*38fd1498Szrj if (outer_parm || inner_parm)
262*38fd1498Szrj return false;
263*38fd1498Szrj
264*38fd1498Szrj /* Defer to the target if necessary. */
265*38fd1498Szrj if (TYPE_ATTRIBUTES (inner_type) || TYPE_ATTRIBUTES (outer_type))
266*38fd1498Szrj return comp_type_attributes (outer_type, inner_type) != 0;
267*38fd1498Szrj
268*38fd1498Szrj return true;
269*38fd1498Szrj }
270*38fd1498Szrj
271*38fd1498Szrj /* For aggregates we rely on TYPE_CANONICAL exclusively and require
272*38fd1498Szrj explicit conversions for types involving to be structurally
273*38fd1498Szrj compared types. */
274*38fd1498Szrj else if (AGGREGATE_TYPE_P (inner_type)
275*38fd1498Szrj && TREE_CODE (inner_type) == TREE_CODE (outer_type))
276*38fd1498Szrj return TYPE_CANONICAL (inner_type)
277*38fd1498Szrj && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type);
278*38fd1498Szrj
279*38fd1498Szrj else if (TREE_CODE (inner_type) == OFFSET_TYPE
280*38fd1498Szrj && TREE_CODE (outer_type) == OFFSET_TYPE)
281*38fd1498Szrj return useless_type_conversion_p (TREE_TYPE (outer_type),
282*38fd1498Szrj TREE_TYPE (inner_type))
283*38fd1498Szrj && useless_type_conversion_p
284*38fd1498Szrj (TYPE_OFFSET_BASETYPE (outer_type),
285*38fd1498Szrj TYPE_OFFSET_BASETYPE (inner_type));
286*38fd1498Szrj
287*38fd1498Szrj return false;
288*38fd1498Szrj }
289*38fd1498Szrj
290*38fd1498Szrj
291*38fd1498Szrj /* ----- Decl related ----- */
292*38fd1498Szrj
293*38fd1498Szrj /* Set sequence SEQ to be the GIMPLE body for function FN. */
294*38fd1498Szrj
295*38fd1498Szrj void
gimple_set_body(tree fndecl,gimple_seq seq)296*38fd1498Szrj gimple_set_body (tree fndecl, gimple_seq seq)
297*38fd1498Szrj {
298*38fd1498Szrj struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
299*38fd1498Szrj if (fn == NULL)
300*38fd1498Szrj {
301*38fd1498Szrj /* If FNDECL still does not have a function structure associated
302*38fd1498Szrj with it, then it does not make sense for it to receive a
303*38fd1498Szrj GIMPLE body. */
304*38fd1498Szrj gcc_assert (seq == NULL);
305*38fd1498Szrj }
306*38fd1498Szrj else
307*38fd1498Szrj fn->gimple_body = seq;
308*38fd1498Szrj }
309*38fd1498Szrj
310*38fd1498Szrj
311*38fd1498Szrj /* Return the body of GIMPLE statements for function FN. After the
312*38fd1498Szrj CFG pass, the function body doesn't exist anymore because it has
313*38fd1498Szrj been split up into basic blocks. In this case, it returns
314*38fd1498Szrj NULL. */
315*38fd1498Szrj
316*38fd1498Szrj gimple_seq
gimple_body(tree fndecl)317*38fd1498Szrj gimple_body (tree fndecl)
318*38fd1498Szrj {
319*38fd1498Szrj struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
320*38fd1498Szrj return fn ? fn->gimple_body : NULL;
321*38fd1498Szrj }
322*38fd1498Szrj
323*38fd1498Szrj /* Return true when FNDECL has Gimple body either in unlowered
324*38fd1498Szrj or CFG form. */
325*38fd1498Szrj bool
gimple_has_body_p(tree fndecl)326*38fd1498Szrj gimple_has_body_p (tree fndecl)
327*38fd1498Szrj {
328*38fd1498Szrj struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
329*38fd1498Szrj return (gimple_body (fndecl) || (fn && fn->cfg && !(fn->curr_properties & PROP_rtl)));
330*38fd1498Szrj }
331*38fd1498Szrj
332*38fd1498Szrj /* Return a printable name for symbol DECL. */
333*38fd1498Szrj
334*38fd1498Szrj const char *
gimple_decl_printable_name(tree decl,int verbosity)335*38fd1498Szrj gimple_decl_printable_name (tree decl, int verbosity)
336*38fd1498Szrj {
337*38fd1498Szrj if (!DECL_NAME (decl))
338*38fd1498Szrj return NULL;
339*38fd1498Szrj
340*38fd1498Szrj if (HAS_DECL_ASSEMBLER_NAME_P (decl) && DECL_ASSEMBLER_NAME_SET_P (decl))
341*38fd1498Szrj {
342*38fd1498Szrj int dmgl_opts = DMGL_NO_OPTS;
343*38fd1498Szrj
344*38fd1498Szrj if (verbosity >= 2)
345*38fd1498Szrj {
346*38fd1498Szrj dmgl_opts = DMGL_VERBOSE
347*38fd1498Szrj | DMGL_ANSI
348*38fd1498Szrj | DMGL_GNU_V3
349*38fd1498Szrj | DMGL_RET_POSTFIX;
350*38fd1498Szrj if (TREE_CODE (decl) == FUNCTION_DECL)
351*38fd1498Szrj dmgl_opts |= DMGL_PARAMS;
352*38fd1498Szrj }
353*38fd1498Szrj
354*38fd1498Szrj const char *mangled_str
355*38fd1498Szrj = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME_RAW (decl));
356*38fd1498Szrj const char *str = cplus_demangle_v3 (mangled_str, dmgl_opts);
357*38fd1498Szrj return str ? str : mangled_str;
358*38fd1498Szrj }
359*38fd1498Szrj
360*38fd1498Szrj return IDENTIFIER_POINTER (DECL_NAME (decl));
361*38fd1498Szrj }
362*38fd1498Szrj
363*38fd1498Szrj
364*38fd1498Szrj /* Create a new VAR_DECL and copy information from VAR to it. */
365*38fd1498Szrj
366*38fd1498Szrj tree
copy_var_decl(tree var,tree name,tree type)367*38fd1498Szrj copy_var_decl (tree var, tree name, tree type)
368*38fd1498Szrj {
369*38fd1498Szrj tree copy = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, name, type);
370*38fd1498Szrj
371*38fd1498Szrj TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (var);
372*38fd1498Szrj TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (var);
373*38fd1498Szrj DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (var);
374*38fd1498Szrj DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (var);
375*38fd1498Szrj DECL_IGNORED_P (copy) = DECL_IGNORED_P (var);
376*38fd1498Szrj DECL_CONTEXT (copy) = DECL_CONTEXT (var);
377*38fd1498Szrj TREE_NO_WARNING (copy) = TREE_NO_WARNING (var);
378*38fd1498Szrj TREE_USED (copy) = 1;
379*38fd1498Szrj DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
380*38fd1498Szrj DECL_ATTRIBUTES (copy) = DECL_ATTRIBUTES (var);
381*38fd1498Szrj if (DECL_USER_ALIGN (var))
382*38fd1498Szrj {
383*38fd1498Szrj SET_DECL_ALIGN (copy, DECL_ALIGN (var));
384*38fd1498Szrj DECL_USER_ALIGN (copy) = 1;
385*38fd1498Szrj }
386*38fd1498Szrj
387*38fd1498Szrj return copy;
388*38fd1498Szrj }
389*38fd1498Szrj
390*38fd1498Szrj /* Strip off a legitimate source ending from the input string NAME of
391*38fd1498Szrj length LEN. Rather than having to know the names used by all of
392*38fd1498Szrj our front ends, we strip off an ending of a period followed by
393*38fd1498Szrj up to four characters. (like ".cpp".) */
394*38fd1498Szrj
395*38fd1498Szrj static inline void
remove_suffix(char * name,int len)396*38fd1498Szrj remove_suffix (char *name, int len)
397*38fd1498Szrj {
398*38fd1498Szrj int i;
399*38fd1498Szrj
400*38fd1498Szrj for (i = 2; i < 7 && len > i; i++)
401*38fd1498Szrj {
402*38fd1498Szrj if (name[len - i] == '.')
403*38fd1498Szrj {
404*38fd1498Szrj name[len - i] = '\0';
405*38fd1498Szrj break;
406*38fd1498Szrj }
407*38fd1498Szrj }
408*38fd1498Szrj }
409*38fd1498Szrj
410*38fd1498Szrj /* Create a new temporary name with PREFIX. Return an identifier. */
411*38fd1498Szrj
412*38fd1498Szrj static GTY(()) unsigned int tmp_var_id_num;
413*38fd1498Szrj
414*38fd1498Szrj tree
create_tmp_var_name(const char * prefix)415*38fd1498Szrj create_tmp_var_name (const char *prefix)
416*38fd1498Szrj {
417*38fd1498Szrj char *tmp_name;
418*38fd1498Szrj
419*38fd1498Szrj if (prefix)
420*38fd1498Szrj {
421*38fd1498Szrj char *preftmp = ASTRDUP (prefix);
422*38fd1498Szrj
423*38fd1498Szrj remove_suffix (preftmp, strlen (preftmp));
424*38fd1498Szrj clean_symbol_name (preftmp);
425*38fd1498Szrj
426*38fd1498Szrj prefix = preftmp;
427*38fd1498Szrj }
428*38fd1498Szrj
429*38fd1498Szrj ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
430*38fd1498Szrj return get_identifier (tmp_name);
431*38fd1498Szrj }
432*38fd1498Szrj
433*38fd1498Szrj /* Create a new temporary variable declaration of type TYPE.
434*38fd1498Szrj Do NOT push it into the current binding. */
435*38fd1498Szrj
436*38fd1498Szrj tree
create_tmp_var_raw(tree type,const char * prefix)437*38fd1498Szrj create_tmp_var_raw (tree type, const char *prefix)
438*38fd1498Szrj {
439*38fd1498Szrj tree tmp_var;
440*38fd1498Szrj
441*38fd1498Szrj tmp_var = build_decl (input_location,
442*38fd1498Szrj VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
443*38fd1498Szrj type);
444*38fd1498Szrj
445*38fd1498Szrj /* The variable was declared by the compiler. */
446*38fd1498Szrj DECL_ARTIFICIAL (tmp_var) = 1;
447*38fd1498Szrj /* And we don't want debug info for it. */
448*38fd1498Szrj DECL_IGNORED_P (tmp_var) = 1;
449*38fd1498Szrj /* And we don't want even the fancy names of those printed in
450*38fd1498Szrj -fdump-final-insns= dumps. */
451*38fd1498Szrj DECL_NAMELESS (tmp_var) = 1;
452*38fd1498Szrj
453*38fd1498Szrj /* Make the variable writable. */
454*38fd1498Szrj TREE_READONLY (tmp_var) = 0;
455*38fd1498Szrj
456*38fd1498Szrj DECL_EXTERNAL (tmp_var) = 0;
457*38fd1498Szrj TREE_STATIC (tmp_var) = 0;
458*38fd1498Szrj TREE_USED (tmp_var) = 1;
459*38fd1498Szrj
460*38fd1498Szrj return tmp_var;
461*38fd1498Szrj }
462*38fd1498Szrj
463*38fd1498Szrj /* Create a new temporary variable declaration of type TYPE. DO push the
464*38fd1498Szrj variable into the current binding. Further, assume that this is called
465*38fd1498Szrj only from gimplification or optimization, at which point the creation of
466*38fd1498Szrj certain types are bugs. */
467*38fd1498Szrj
468*38fd1498Szrj tree
create_tmp_var(tree type,const char * prefix)469*38fd1498Szrj create_tmp_var (tree type, const char *prefix)
470*38fd1498Szrj {
471*38fd1498Szrj tree tmp_var;
472*38fd1498Szrj
473*38fd1498Szrj /* We don't allow types that are addressable (meaning we can't make copies),
474*38fd1498Szrj or incomplete. We also used to reject every variable size objects here,
475*38fd1498Szrj but now support those for which a constant upper bound can be obtained.
476*38fd1498Szrj The processing for variable sizes is performed in gimple_add_tmp_var,
477*38fd1498Szrj point at which it really matters and possibly reached via paths not going
478*38fd1498Szrj through this function, e.g. after direct calls to create_tmp_var_raw. */
479*38fd1498Szrj gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
480*38fd1498Szrj
481*38fd1498Szrj tmp_var = create_tmp_var_raw (type, prefix);
482*38fd1498Szrj gimple_add_tmp_var (tmp_var);
483*38fd1498Szrj return tmp_var;
484*38fd1498Szrj }
485*38fd1498Szrj
486*38fd1498Szrj /* Create a new temporary variable declaration of type TYPE by calling
487*38fd1498Szrj create_tmp_var and if TYPE is a vector or a complex number, mark the new
488*38fd1498Szrj temporary as gimple register. */
489*38fd1498Szrj
490*38fd1498Szrj tree
create_tmp_reg(tree type,const char * prefix)491*38fd1498Szrj create_tmp_reg (tree type, const char *prefix)
492*38fd1498Szrj {
493*38fd1498Szrj tree tmp;
494*38fd1498Szrj
495*38fd1498Szrj tmp = create_tmp_var (type, prefix);
496*38fd1498Szrj if (TREE_CODE (type) == COMPLEX_TYPE
497*38fd1498Szrj || TREE_CODE (type) == VECTOR_TYPE)
498*38fd1498Szrj DECL_GIMPLE_REG_P (tmp) = 1;
499*38fd1498Szrj
500*38fd1498Szrj return tmp;
501*38fd1498Szrj }
502*38fd1498Szrj
503*38fd1498Szrj /* Create a new temporary variable declaration of type TYPE by calling
504*38fd1498Szrj create_tmp_var and if TYPE is a vector or a complex number, mark the new
505*38fd1498Szrj temporary as gimple register. */
506*38fd1498Szrj
507*38fd1498Szrj tree
create_tmp_reg_fn(struct function * fn,tree type,const char * prefix)508*38fd1498Szrj create_tmp_reg_fn (struct function *fn, tree type, const char *prefix)
509*38fd1498Szrj {
510*38fd1498Szrj tree tmp;
511*38fd1498Szrj
512*38fd1498Szrj tmp = create_tmp_var_raw (type, prefix);
513*38fd1498Szrj gimple_add_tmp_var_fn (fn, tmp);
514*38fd1498Szrj if (TREE_CODE (type) == COMPLEX_TYPE
515*38fd1498Szrj || TREE_CODE (type) == VECTOR_TYPE)
516*38fd1498Szrj DECL_GIMPLE_REG_P (tmp) = 1;
517*38fd1498Szrj
518*38fd1498Szrj return tmp;
519*38fd1498Szrj }
520*38fd1498Szrj
521*38fd1498Szrj
522*38fd1498Szrj /* ----- Expression related ----- */
523*38fd1498Szrj
524*38fd1498Szrj /* Extract the operands and code for expression EXPR into *SUBCODE_P,
525*38fd1498Szrj *OP1_P, *OP2_P and *OP3_P respectively. */
526*38fd1498Szrj
527*38fd1498Szrj void
extract_ops_from_tree(tree expr,enum tree_code * subcode_p,tree * op1_p,tree * op2_p,tree * op3_p)528*38fd1498Szrj extract_ops_from_tree (tree expr, enum tree_code *subcode_p, tree *op1_p,
529*38fd1498Szrj tree *op2_p, tree *op3_p)
530*38fd1498Szrj {
531*38fd1498Szrj enum gimple_rhs_class grhs_class;
532*38fd1498Szrj
533*38fd1498Szrj *subcode_p = TREE_CODE (expr);
534*38fd1498Szrj grhs_class = get_gimple_rhs_class (*subcode_p);
535*38fd1498Szrj
536*38fd1498Szrj if (grhs_class == GIMPLE_TERNARY_RHS)
537*38fd1498Szrj {
538*38fd1498Szrj *op1_p = TREE_OPERAND (expr, 0);
539*38fd1498Szrj *op2_p = TREE_OPERAND (expr, 1);
540*38fd1498Szrj *op3_p = TREE_OPERAND (expr, 2);
541*38fd1498Szrj }
542*38fd1498Szrj else if (grhs_class == GIMPLE_BINARY_RHS)
543*38fd1498Szrj {
544*38fd1498Szrj *op1_p = TREE_OPERAND (expr, 0);
545*38fd1498Szrj *op2_p = TREE_OPERAND (expr, 1);
546*38fd1498Szrj *op3_p = NULL_TREE;
547*38fd1498Szrj }
548*38fd1498Szrj else if (grhs_class == GIMPLE_UNARY_RHS)
549*38fd1498Szrj {
550*38fd1498Szrj *op1_p = TREE_OPERAND (expr, 0);
551*38fd1498Szrj *op2_p = NULL_TREE;
552*38fd1498Szrj *op3_p = NULL_TREE;
553*38fd1498Szrj }
554*38fd1498Szrj else if (grhs_class == GIMPLE_SINGLE_RHS)
555*38fd1498Szrj {
556*38fd1498Szrj *op1_p = expr;
557*38fd1498Szrj *op2_p = NULL_TREE;
558*38fd1498Szrj *op3_p = NULL_TREE;
559*38fd1498Szrj }
560*38fd1498Szrj else
561*38fd1498Szrj gcc_unreachable ();
562*38fd1498Szrj }
563*38fd1498Szrj
564*38fd1498Szrj /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
565*38fd1498Szrj
566*38fd1498Szrj void
gimple_cond_get_ops_from_tree(tree cond,enum tree_code * code_p,tree * lhs_p,tree * rhs_p)567*38fd1498Szrj gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
568*38fd1498Szrj tree *lhs_p, tree *rhs_p)
569*38fd1498Szrj {
570*38fd1498Szrj gcc_assert (COMPARISON_CLASS_P (cond)
571*38fd1498Szrj || TREE_CODE (cond) == TRUTH_NOT_EXPR
572*38fd1498Szrj || is_gimple_min_invariant (cond)
573*38fd1498Szrj || SSA_VAR_P (cond));
574*38fd1498Szrj
575*38fd1498Szrj extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
576*38fd1498Szrj
577*38fd1498Szrj /* Canonicalize conditionals of the form 'if (!VAL)'. */
578*38fd1498Szrj if (*code_p == TRUTH_NOT_EXPR)
579*38fd1498Szrj {
580*38fd1498Szrj *code_p = EQ_EXPR;
581*38fd1498Szrj gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
582*38fd1498Szrj *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
583*38fd1498Szrj }
584*38fd1498Szrj /* Canonicalize conditionals of the form 'if (VAL)' */
585*38fd1498Szrj else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
586*38fd1498Szrj {
587*38fd1498Szrj *code_p = NE_EXPR;
588*38fd1498Szrj gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
589*38fd1498Szrj *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
590*38fd1498Szrj }
591*38fd1498Szrj }
592*38fd1498Szrj
593*38fd1498Szrj /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
594*38fd1498Szrj
595*38fd1498Szrj bool
is_gimple_lvalue(tree t)596*38fd1498Szrj is_gimple_lvalue (tree t)
597*38fd1498Szrj {
598*38fd1498Szrj return (is_gimple_addressable (t)
599*38fd1498Szrj || TREE_CODE (t) == WITH_SIZE_EXPR
600*38fd1498Szrj /* These are complex lvalues, but don't have addresses, so they
601*38fd1498Szrj go here. */
602*38fd1498Szrj || TREE_CODE (t) == BIT_FIELD_REF);
603*38fd1498Szrj }
604*38fd1498Szrj
605*38fd1498Szrj /* Return true if T is a GIMPLE condition. */
606*38fd1498Szrj
607*38fd1498Szrj bool
is_gimple_condexpr(tree t)608*38fd1498Szrj is_gimple_condexpr (tree t)
609*38fd1498Szrj {
610*38fd1498Szrj return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
611*38fd1498Szrj && !tree_could_throw_p (t)
612*38fd1498Szrj && is_gimple_val (TREE_OPERAND (t, 0))
613*38fd1498Szrj && is_gimple_val (TREE_OPERAND (t, 1))));
614*38fd1498Szrj }
615*38fd1498Szrj
616*38fd1498Szrj /* Return true if T is a gimple address. */
617*38fd1498Szrj
618*38fd1498Szrj bool
is_gimple_address(const_tree t)619*38fd1498Szrj is_gimple_address (const_tree t)
620*38fd1498Szrj {
621*38fd1498Szrj tree op;
622*38fd1498Szrj
623*38fd1498Szrj if (TREE_CODE (t) != ADDR_EXPR)
624*38fd1498Szrj return false;
625*38fd1498Szrj
626*38fd1498Szrj op = TREE_OPERAND (t, 0);
627*38fd1498Szrj while (handled_component_p (op))
628*38fd1498Szrj {
629*38fd1498Szrj if ((TREE_CODE (op) == ARRAY_REF
630*38fd1498Szrj || TREE_CODE (op) == ARRAY_RANGE_REF)
631*38fd1498Szrj && !is_gimple_val (TREE_OPERAND (op, 1)))
632*38fd1498Szrj return false;
633*38fd1498Szrj
634*38fd1498Szrj op = TREE_OPERAND (op, 0);
635*38fd1498Szrj }
636*38fd1498Szrj
637*38fd1498Szrj if (CONSTANT_CLASS_P (op)
638*38fd1498Szrj || TREE_CODE (op) == TARGET_MEM_REF
639*38fd1498Szrj || TREE_CODE (op) == MEM_REF)
640*38fd1498Szrj return true;
641*38fd1498Szrj
642*38fd1498Szrj switch (TREE_CODE (op))
643*38fd1498Szrj {
644*38fd1498Szrj case PARM_DECL:
645*38fd1498Szrj case RESULT_DECL:
646*38fd1498Szrj case LABEL_DECL:
647*38fd1498Szrj case FUNCTION_DECL:
648*38fd1498Szrj case VAR_DECL:
649*38fd1498Szrj case CONST_DECL:
650*38fd1498Szrj return true;
651*38fd1498Szrj
652*38fd1498Szrj default:
653*38fd1498Szrj return false;
654*38fd1498Szrj }
655*38fd1498Szrj }
656*38fd1498Szrj
657*38fd1498Szrj /* Return true if T is a gimple invariant address. */
658*38fd1498Szrj
659*38fd1498Szrj bool
is_gimple_invariant_address(const_tree t)660*38fd1498Szrj is_gimple_invariant_address (const_tree t)
661*38fd1498Szrj {
662*38fd1498Szrj const_tree op;
663*38fd1498Szrj
664*38fd1498Szrj if (TREE_CODE (t) != ADDR_EXPR)
665*38fd1498Szrj return false;
666*38fd1498Szrj
667*38fd1498Szrj op = strip_invariant_refs (TREE_OPERAND (t, 0));
668*38fd1498Szrj if (!op)
669*38fd1498Szrj return false;
670*38fd1498Szrj
671*38fd1498Szrj if (TREE_CODE (op) == MEM_REF)
672*38fd1498Szrj {
673*38fd1498Szrj const_tree op0 = TREE_OPERAND (op, 0);
674*38fd1498Szrj return (TREE_CODE (op0) == ADDR_EXPR
675*38fd1498Szrj && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
676*38fd1498Szrj || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
677*38fd1498Szrj }
678*38fd1498Szrj
679*38fd1498Szrj return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
680*38fd1498Szrj }
681*38fd1498Szrj
682*38fd1498Szrj /* Return true if T is a gimple invariant address at IPA level
683*38fd1498Szrj (so addresses of variables on stack are not allowed). */
684*38fd1498Szrj
685*38fd1498Szrj bool
is_gimple_ip_invariant_address(const_tree t)686*38fd1498Szrj is_gimple_ip_invariant_address (const_tree t)
687*38fd1498Szrj {
688*38fd1498Szrj const_tree op;
689*38fd1498Szrj
690*38fd1498Szrj if (TREE_CODE (t) != ADDR_EXPR)
691*38fd1498Szrj return false;
692*38fd1498Szrj
693*38fd1498Szrj op = strip_invariant_refs (TREE_OPERAND (t, 0));
694*38fd1498Szrj if (!op)
695*38fd1498Szrj return false;
696*38fd1498Szrj
697*38fd1498Szrj if (TREE_CODE (op) == MEM_REF)
698*38fd1498Szrj {
699*38fd1498Szrj const_tree op0 = TREE_OPERAND (op, 0);
700*38fd1498Szrj return (TREE_CODE (op0) == ADDR_EXPR
701*38fd1498Szrj && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
702*38fd1498Szrj || decl_address_ip_invariant_p (TREE_OPERAND (op0, 0))));
703*38fd1498Szrj }
704*38fd1498Szrj
705*38fd1498Szrj return CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op);
706*38fd1498Szrj }
707*38fd1498Szrj
708*38fd1498Szrj /* Return true if T is a GIMPLE minimal invariant. It's a restricted
709*38fd1498Szrj form of function invariant. */
710*38fd1498Szrj
711*38fd1498Szrj bool
is_gimple_min_invariant(const_tree t)712*38fd1498Szrj is_gimple_min_invariant (const_tree t)
713*38fd1498Szrj {
714*38fd1498Szrj if (TREE_CODE (t) == ADDR_EXPR)
715*38fd1498Szrj return is_gimple_invariant_address (t);
716*38fd1498Szrj
717*38fd1498Szrj return is_gimple_constant (t);
718*38fd1498Szrj }
719*38fd1498Szrj
720*38fd1498Szrj /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
721*38fd1498Szrj form of gimple minimal invariant. */
722*38fd1498Szrj
723*38fd1498Szrj bool
is_gimple_ip_invariant(const_tree t)724*38fd1498Szrj is_gimple_ip_invariant (const_tree t)
725*38fd1498Szrj {
726*38fd1498Szrj if (TREE_CODE (t) == ADDR_EXPR)
727*38fd1498Szrj return is_gimple_ip_invariant_address (t);
728*38fd1498Szrj
729*38fd1498Szrj return is_gimple_constant (t);
730*38fd1498Szrj }
731*38fd1498Szrj
732*38fd1498Szrj /* Return true if T is a non-aggregate register variable. */
733*38fd1498Szrj
734*38fd1498Szrj bool
is_gimple_reg(tree t)735*38fd1498Szrj is_gimple_reg (tree t)
736*38fd1498Szrj {
737*38fd1498Szrj if (virtual_operand_p (t))
738*38fd1498Szrj return false;
739*38fd1498Szrj
740*38fd1498Szrj if (TREE_CODE (t) == SSA_NAME)
741*38fd1498Szrj return true;
742*38fd1498Szrj
743*38fd1498Szrj if (!is_gimple_variable (t))
744*38fd1498Szrj return false;
745*38fd1498Szrj
746*38fd1498Szrj if (!is_gimple_reg_type (TREE_TYPE (t)))
747*38fd1498Szrj return false;
748*38fd1498Szrj
749*38fd1498Szrj /* A volatile decl is not acceptable because we can't reuse it as
750*38fd1498Szrj needed. We need to copy it into a temp first. */
751*38fd1498Szrj if (TREE_THIS_VOLATILE (t))
752*38fd1498Szrj return false;
753*38fd1498Szrj
754*38fd1498Szrj /* We define "registers" as things that can be renamed as needed,
755*38fd1498Szrj which with our infrastructure does not apply to memory. */
756*38fd1498Szrj if (needs_to_live_in_memory (t))
757*38fd1498Szrj return false;
758*38fd1498Szrj
759*38fd1498Szrj /* Hard register variables are an interesting case. For those that
760*38fd1498Szrj are call-clobbered, we don't know where all the calls are, since
761*38fd1498Szrj we don't (want to) take into account which operations will turn
762*38fd1498Szrj into libcalls at the rtl level. For those that are call-saved,
763*38fd1498Szrj we don't currently model the fact that calls may in fact change
764*38fd1498Szrj global hard registers, nor do we examine ASM_CLOBBERS at the tree
765*38fd1498Szrj level, and so miss variable changes that might imply. All around,
766*38fd1498Szrj it seems safest to not do too much optimization with these at the
767*38fd1498Szrj tree level at all. We'll have to rely on the rtl optimizers to
768*38fd1498Szrj clean this up, as there we've got all the appropriate bits exposed. */
769*38fd1498Szrj if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
770*38fd1498Szrj return false;
771*38fd1498Szrj
772*38fd1498Szrj /* Complex and vector values must have been put into SSA-like form.
773*38fd1498Szrj That is, no assignments to the individual components. */
774*38fd1498Szrj if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
775*38fd1498Szrj || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
776*38fd1498Szrj return DECL_GIMPLE_REG_P (t);
777*38fd1498Szrj
778*38fd1498Szrj return true;
779*38fd1498Szrj }
780*38fd1498Szrj
781*38fd1498Szrj
782*38fd1498Szrj /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
783*38fd1498Szrj
784*38fd1498Szrj bool
is_gimple_val(tree t)785*38fd1498Szrj is_gimple_val (tree t)
786*38fd1498Szrj {
787*38fd1498Szrj /* Make loads from volatiles and memory vars explicit. */
788*38fd1498Szrj if (is_gimple_variable (t)
789*38fd1498Szrj && is_gimple_reg_type (TREE_TYPE (t))
790*38fd1498Szrj && !is_gimple_reg (t))
791*38fd1498Szrj return false;
792*38fd1498Szrj
793*38fd1498Szrj return (is_gimple_variable (t) || is_gimple_min_invariant (t));
794*38fd1498Szrj }
795*38fd1498Szrj
796*38fd1498Szrj /* Similarly, but accept hard registers as inputs to asm statements. */
797*38fd1498Szrj
798*38fd1498Szrj bool
is_gimple_asm_val(tree t)799*38fd1498Szrj is_gimple_asm_val (tree t)
800*38fd1498Szrj {
801*38fd1498Szrj if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
802*38fd1498Szrj return true;
803*38fd1498Szrj
804*38fd1498Szrj return is_gimple_val (t);
805*38fd1498Szrj }
806*38fd1498Szrj
807*38fd1498Szrj /* Return true if T is a GIMPLE minimal lvalue. */
808*38fd1498Szrj
809*38fd1498Szrj bool
is_gimple_min_lval(tree t)810*38fd1498Szrj is_gimple_min_lval (tree t)
811*38fd1498Szrj {
812*38fd1498Szrj if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
813*38fd1498Szrj return false;
814*38fd1498Szrj return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
815*38fd1498Szrj }
816*38fd1498Szrj
817*38fd1498Szrj /* Return true if T is a valid function operand of a CALL_EXPR. */
818*38fd1498Szrj
819*38fd1498Szrj bool
is_gimple_call_addr(tree t)820*38fd1498Szrj is_gimple_call_addr (tree t)
821*38fd1498Szrj {
822*38fd1498Szrj return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
823*38fd1498Szrj }
824*38fd1498Szrj
825*38fd1498Szrj /* Return true if T is a valid address operand of a MEM_REF. */
826*38fd1498Szrj
827*38fd1498Szrj bool
is_gimple_mem_ref_addr(tree t)828*38fd1498Szrj is_gimple_mem_ref_addr (tree t)
829*38fd1498Szrj {
830*38fd1498Szrj return (is_gimple_reg (t)
831*38fd1498Szrj || TREE_CODE (t) == INTEGER_CST
832*38fd1498Szrj || (TREE_CODE (t) == ADDR_EXPR
833*38fd1498Szrj && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
834*38fd1498Szrj || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
835*38fd1498Szrj }
836*38fd1498Szrj
837*38fd1498Szrj /* Hold trees marked addressable during expand. */
838*38fd1498Szrj
839*38fd1498Szrj static hash_set<tree> *mark_addressable_queue;
840*38fd1498Szrj
841*38fd1498Szrj /* Mark X as addressable or queue it up if called during expand. We
842*38fd1498Szrj don't want to apply it immediately during expand because decls are
843*38fd1498Szrj made addressable at that point due to RTL-only concerns, such as
844*38fd1498Szrj uses of memcpy for block moves, and TREE_ADDRESSABLE changes
845*38fd1498Szrj is_gimple_reg, which might make it seem like a variable that used
846*38fd1498Szrj to be a gimple_reg shouldn't have been an SSA name. So we queue up
847*38fd1498Szrj this flag setting and only apply it when we're done with GIMPLE and
848*38fd1498Szrj only RTL issues matter. */
849*38fd1498Szrj
850*38fd1498Szrj static void
mark_addressable_1(tree x)851*38fd1498Szrj mark_addressable_1 (tree x)
852*38fd1498Szrj {
853*38fd1498Szrj if (!currently_expanding_to_rtl)
854*38fd1498Szrj {
855*38fd1498Szrj TREE_ADDRESSABLE (x) = 1;
856*38fd1498Szrj return;
857*38fd1498Szrj }
858*38fd1498Szrj
859*38fd1498Szrj if (!mark_addressable_queue)
860*38fd1498Szrj mark_addressable_queue = new hash_set<tree>();
861*38fd1498Szrj mark_addressable_queue->add (x);
862*38fd1498Szrj }
863*38fd1498Szrj
864*38fd1498Szrj /* Adaptor for mark_addressable_1 for use in hash_set traversal. */
865*38fd1498Szrj
866*38fd1498Szrj bool
867*38fd1498Szrj mark_addressable_2 (tree const &x, void * ATTRIBUTE_UNUSED = NULL)
868*38fd1498Szrj {
869*38fd1498Szrj mark_addressable_1 (x);
870*38fd1498Szrj return false;
871*38fd1498Szrj }
872*38fd1498Szrj
873*38fd1498Szrj /* Mark all queued trees as addressable, and empty the queue. To be
874*38fd1498Szrj called right after clearing CURRENTLY_EXPANDING_TO_RTL. */
875*38fd1498Szrj
876*38fd1498Szrj void
flush_mark_addressable_queue()877*38fd1498Szrj flush_mark_addressable_queue ()
878*38fd1498Szrj {
879*38fd1498Szrj gcc_assert (!currently_expanding_to_rtl);
880*38fd1498Szrj if (mark_addressable_queue)
881*38fd1498Szrj {
882*38fd1498Szrj mark_addressable_queue->traverse<void*, mark_addressable_2> (NULL);
883*38fd1498Szrj delete mark_addressable_queue;
884*38fd1498Szrj mark_addressable_queue = NULL;
885*38fd1498Szrj }
886*38fd1498Szrj }
887*38fd1498Szrj
888*38fd1498Szrj /* Mark X addressable. Unlike the langhook we expect X to be in gimple
889*38fd1498Szrj form and we don't do any syntax checking. */
890*38fd1498Szrj
891*38fd1498Szrj void
mark_addressable(tree x)892*38fd1498Szrj mark_addressable (tree x)
893*38fd1498Szrj {
894*38fd1498Szrj while (handled_component_p (x))
895*38fd1498Szrj x = TREE_OPERAND (x, 0);
896*38fd1498Szrj if (TREE_CODE (x) == MEM_REF
897*38fd1498Szrj && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
898*38fd1498Szrj x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
899*38fd1498Szrj if (!VAR_P (x)
900*38fd1498Szrj && TREE_CODE (x) != PARM_DECL
901*38fd1498Szrj && TREE_CODE (x) != RESULT_DECL)
902*38fd1498Szrj return;
903*38fd1498Szrj mark_addressable_1 (x);
904*38fd1498Szrj
905*38fd1498Szrj /* Also mark the artificial SSA_NAME that points to the partition of X. */
906*38fd1498Szrj if (TREE_CODE (x) == VAR_DECL
907*38fd1498Szrj && !DECL_EXTERNAL (x)
908*38fd1498Szrj && !TREE_STATIC (x)
909*38fd1498Szrj && cfun->gimple_df != NULL
910*38fd1498Szrj && cfun->gimple_df->decls_to_pointers != NULL)
911*38fd1498Szrj {
912*38fd1498Szrj tree *namep = cfun->gimple_df->decls_to_pointers->get (x);
913*38fd1498Szrj if (namep)
914*38fd1498Szrj mark_addressable_1 (*namep);
915*38fd1498Szrj }
916*38fd1498Szrj }
917*38fd1498Szrj
918*38fd1498Szrj /* Returns true iff T is a valid RHS for an assignment to a renamed
919*38fd1498Szrj user -- or front-end generated artificial -- variable. */
920*38fd1498Szrj
921*38fd1498Szrj bool
is_gimple_reg_rhs(tree t)922*38fd1498Szrj is_gimple_reg_rhs (tree t)
923*38fd1498Szrj {
924*38fd1498Szrj return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
925*38fd1498Szrj }
926*38fd1498Szrj
927*38fd1498Szrj #include "gt-gimple-expr.h"
928