Debian Codegraph @

gcc/ada/gcc-interface/utils2.cc

raw
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * U T I L S 2 *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2025, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License along with GCC; see the file COPYING3. If not see *
19 * <http://www.gnu.org/licenses/>. *
20 * *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
23 * *
24 ****************************************************************************/
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "memmodel.h"
30 #include "tm.h"
31 #include "vec.h"
32 #include "alias.h"
33 #include "tree.h"
34 #include "inchash.h"
35 #include "builtins.h"
36 #include "expmed.h"
37 #include "fold-const.h"
38 #include "optabs-query.h"
39 #include "stor-layout.h"
40 #include "stringpool.h"
41 #include "varasm.h"
42 #include "flags.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "tree-inline.h"
46
47 #include "ada.h"
48 #include "types.h"
49 #include "atree.h"
50 #include "elists.h"
51 #include "namet.h"
52 #include "nlists.h"
53 #include "snames.h"
54 #include "stringt.h"
55 #include "uintp.h"
56 #include "fe.h"
57 #include "sinfo.h"
58 #include "einfo.h"
59 #include "ada-tree.h"
60 #include "gigi.h"
61
62 /* Return the base type of TYPE. */
63
64 tree
65 get_base_type (tree type)
66 {
67 if (TREE_CODE (type) == RECORD_TYPE
68 && TYPE_JUSTIFIED_MODULAR_P (type))
69 type = TREE_TYPE (TYPE_FIELDS (type));
70
71 while (TREE_TYPE (type)
72 && (TREE_CODE (type) == INTEGER_TYPE
73 || SCALAR_FLOAT_TYPE_P (type)))
74 type = TREE_TYPE (type);
75
76 return type;
77 }
78
79 /* EXP is a GCC tree representing an address. See if we can find how strictly
80 the object at this address is aligned and, if so, return the alignment of
81 the object in bits. Otherwise return 0. */
82
83 unsigned int
84 known_alignment (tree exp)
85 {
86 unsigned int this_alignment;
87 unsigned int lhs, rhs;
88
89 switch (TREE_CODE (exp))
90 {
91 CASE_CONVERT:
92 case VIEW_CONVERT_EXPR:
93 case NON_LVALUE_EXPR:
94 /* Conversions between pointers and integers don't change the alignment
95 of the underlying object. */
96 this_alignment = known_alignment (TREE_OPERAND (exp, 0));
97 break;
98
99 case COMPOUND_EXPR:
100 /* The value of a COMPOUND_EXPR is that of its second operand. */
101 this_alignment = known_alignment (TREE_OPERAND (exp, 1));
102 break;
103
104 case PLUS_EXPR:
105 case MINUS_EXPR:
106 /* If two addresses are added, the alignment of the result is the
107 minimum of the two alignments. */
108 lhs = known_alignment (TREE_OPERAND (exp, 0));
109 rhs = known_alignment (TREE_OPERAND (exp, 1));
110 this_alignment = MIN (lhs, rhs);
111 break;
112
113 case POINTER_PLUS_EXPR:
114 /* If this is the pattern built for aligning types, decode it. */
115 if (TREE_CODE (TREE_OPERAND (exp, 1)) == BIT_AND_EXPR
116 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 1), 0)) == NEGATE_EXPR)
117 {
118 tree op = TREE_OPERAND (TREE_OPERAND (exp, 1), 1);
119 return
120 known_alignment (fold_build1 (BIT_NOT_EXPR, TREE_TYPE (op), op));
121 }
122
123 /* If we don't know the alignment of the offset, we assume that
124 of the base. */
125 lhs = known_alignment (TREE_OPERAND (exp, 0));
126 rhs = known_alignment (TREE_OPERAND (exp, 1));
127
128 if (rhs == 0)
129 this_alignment = lhs;
130 else
131 this_alignment = MIN (lhs, rhs);
132 break;
133
134 case COND_EXPR:
135 /* If there is a choice between two values, use the smaller one. */
136 lhs = known_alignment (TREE_OPERAND (exp, 1));
137 rhs = known_alignment (TREE_OPERAND (exp, 2));
138 this_alignment = MIN (lhs, rhs);
139 break;
140
141 case INTEGER_CST:
142 {
143 unsigned HOST_WIDE_INT c = TREE_INT_CST_LOW (exp);
144 /* The first part of this represents the lowest bit in the constant,
145 but it is originally in bytes, not bits. */
146 this_alignment = (c & -c) * BITS_PER_UNIT;
147 }
148 break;
149
150 case MULT_EXPR:
151 /* If we know the alignment of just one side, use it. Otherwise,
152 use the product of the alignments. */
153 lhs = known_alignment (TREE_OPERAND (exp, 0));
154 rhs = known_alignment (TREE_OPERAND (exp, 1));
155
156 if (lhs == 0)
157 this_alignment = rhs;
158 else if (rhs == 0)
159 this_alignment = lhs;
160 else
161 this_alignment = MIN (lhs * rhs, BIGGEST_ALIGNMENT);
162 break;
163
164 case BIT_AND_EXPR:
165 /* A bit-and expression is as aligned as the maximum alignment of the
166 operands. We typically get here for a complex lhs and a constant
167 negative power of two on the rhs to force an explicit alignment, so
168 don't bother looking at the lhs. */
169 this_alignment = known_alignment (TREE_OPERAND (exp, 1));
170 break;
171
172 case ADDR_EXPR:
173 if (DECL_P (TREE_OPERAND (exp, 0)))
174 this_alignment = DECL_ALIGN (TREE_OPERAND (exp, 0));
175 else
176 this_alignment = get_object_alignment (TREE_OPERAND (exp, 0));
177 break;
178
179 case CALL_EXPR:
180 {
181 tree fndecl = get_callee_fndecl (exp);
182 if (fndecl == malloc_decl || fndecl == realloc_decl)
183 return get_target_system_allocator_alignment () * BITS_PER_UNIT;
184
185 tree t = maybe_inline_call_in_expr (exp);
186 if (t)
187 return known_alignment (t);
188 }
189
190 /* ... fall through ... */
191
192 default:
193 /* For other pointer expressions, we assume that the pointed-to object
194 is at least as aligned as the pointed-to type. Beware that we can
195 have a dummy type here (e.g. a Taft Amendment type), for which the
196 alignment is meaningless and should be ignored. */
197 if (POINTER_TYPE_P (TREE_TYPE (exp))
198 && !TYPE_IS_DUMMY_P (TREE_TYPE (TREE_TYPE (exp)))
199 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (exp))))
200 this_alignment = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
201 else
202 this_alignment = 0;
203 break;
204 }
205
206 return this_alignment;
207 }
208
209 /* We have a comparison or assignment operation on two types, T1 and T2, which
210 are either both array types or both record types. T1 is assumed to be for
211 the left hand side operand, and T2 for the right hand side. Return the
212 type that both operands should be converted to for the operation, if any.
213 Otherwise return zero. */
214
215 static tree
216 find_common_type (tree t1, tree t2)
217 {
218 /* ??? As of today, various constructs lead to here with types of different
219 sizes even when both constants (e.g. tagged types, packable vs regular
220 component types, padded vs unpadded types, ...). While some of these
221 would better be handled upstream (types should be made consistent before
222 calling into build_binary_op), some others are really expected and we
223 have to be careful. */
224
225 const bool variable_record_on_lhs
226 = (TREE_CODE (t1) == RECORD_TYPE
227 && TREE_CODE (t2) == RECORD_TYPE
228 && get_variant_part (t1)
229 && !get_variant_part (t2));
230
231 const bool variable_array_on_lhs
232 = (TREE_CODE (t1) == ARRAY_TYPE
233 && TREE_CODE (t2) == ARRAY_TYPE
234 && !TREE_CONSTANT (TYPE_MIN_VALUE (TYPE_DOMAIN (t1)))
235 && TREE_CONSTANT (TYPE_MIN_VALUE (TYPE_DOMAIN (t2))));
236
237 /* We must avoid writing more than what the target can hold if this is for
238 an assignment and the case of tagged types is handled in build_binary_op
239 so we use the lhs type if it is known to be smaller or of constant size
240 and the rhs type is not, whatever the modes. We also force t1 in case of
241 constant size equality to minimize occurrences of view conversions on the
242 lhs of an assignment, except for the case of types with a variable part
243 on the lhs but not on the rhs to make the conversion simpler. */
244 if (TREE_CONSTANT (TYPE_SIZE (t1))
245 && (!TREE_CONSTANT (TYPE_SIZE (t2))
246 || tree_int_cst_lt (TYPE_SIZE (t1), TYPE_SIZE (t2))
247 || (TYPE_SIZE (t1) == TYPE_SIZE (t2)
248 && !variable_record_on_lhs
249 && !variable_array_on_lhs)))
250 return t1;
251
252 /* Otherwise, if the lhs type is non-BLKmode, use it, except for the case of
253 a non-BLKmode rhs and array types with a variable part on the lhs but not
254 on the rhs to make sure the conversion is preserved during gimplification.
255 Note that we know that we will not have any alignment problems since, if
256 we did, the non-BLKmode type could not have been used. */
257 if (TYPE_MODE (t1) != BLKmode
258 && (TYPE_MODE (t2) == BLKmode || !variable_array_on_lhs))
259 return t1;
260
261 /* If the rhs type is of constant size, use it whatever the modes. At
262 this point it is known to be smaller, or of constant size and the
263 lhs type is not. */
264 if (TREE_CONSTANT (TYPE_SIZE (t2)))
265 return t2;
266
267 /* Otherwise, if the rhs type is non-BLKmode, use it. */
268 if (TYPE_MODE (t2) != BLKmode)
269 return t2;
270
271 /* In this case, both types have variable size and BLKmode. It's
272 probably best to leave the "type mismatch" because changing it
273 could cause a bad self-referential reference. */
274 return NULL_TREE;
275 }
276
277 /* Return an expression tree representing an equality comparison of A1 and A2,
278 two objects of type ARRAY_TYPE. The result should be of type RESULT_TYPE.
279
280 Two arrays are equal in one of two ways: (1) if both have zero length in
281 some dimension (not necessarily the same dimension) or (2) if the lengths
282 in each dimension are equal and the data is equal. We perform the length
283 tests in as efficient a manner as possible. */
284
285 static tree
286 compare_arrays_for_equality (location_t loc, tree result_type, tree a1, tree a2)
287 {
288 tree result = convert (result_type, boolean_true_node);
289 tree a1_is_null = convert (result_type, boolean_false_node);
290 tree a2_is_null = convert (result_type, boolean_false_node);
291 tree t1 = TREE_TYPE (a1);
292 tree t2 = TREE_TYPE (a2);
293 bool a1_side_effects_p = TREE_SIDE_EFFECTS (a1);
294 bool a2_side_effects_p = TREE_SIDE_EFFECTS (a2);
295 bool length_zero_p = false;
296
297 /* If the operands have side-effects, they need to be evaluated only once
298 in spite of the multiple references in the comparison. */
299 if (a1_side_effects_p)
300 a1 = gnat_protect_expr (a1);
301
302 if (a2_side_effects_p)
303 a2 = gnat_protect_expr (a2);
304
305 /* Process each dimension separately and compare the lengths. If any
306 dimension has a length known to be zero, set LENGTH_ZERO_P to true
307 in order to suppress the comparison of the data at the end. */
308 while (TREE_CODE (t1) == ARRAY_TYPE && TREE_CODE (t2) == ARRAY_TYPE)
309 {
310 tree dom1 = TYPE_DOMAIN (t1);
311 tree dom2 = TYPE_DOMAIN (t2);
312 tree length1 = size_binop (PLUS_EXPR,
313 size_binop (MINUS_EXPR,
314 TYPE_MAX_VALUE (dom1),
315 TYPE_MIN_VALUE (dom1)),
316 size_one_node);
317 tree length2 = size_binop (PLUS_EXPR,
318 size_binop (MINUS_EXPR,
319 TYPE_MAX_VALUE (dom2),
320 TYPE_MIN_VALUE (dom2)),
321 size_one_node);
322 tree ind1 = TYPE_INDEX_TYPE (dom1);
323 tree ind2 = TYPE_INDEX_TYPE (dom2);
324 tree base_type = maybe_character_type (get_base_type (ind1));
325 tree lb1 = convert (base_type, TYPE_MIN_VALUE (ind1));
326 tree ub1 = convert (base_type, TYPE_MAX_VALUE (ind1));
327 tree lb2 = convert (base_type, TYPE_MIN_VALUE (ind2));
328 tree ub2 = convert (base_type, TYPE_MAX_VALUE (ind2));
329 tree comparison, this_a1_is_null, this_a2_is_null;
330
331 /* If the length of the first array is a constant and that of the second
332 array is not, swap our operands to have the constant second. */
333 if (TREE_CODE (length1) == INTEGER_CST
334 && TREE_CODE (length2) != INTEGER_CST)
335 {
336 tree tem;
337 bool btem;
338
339 tem = a1, a1 = a2, a2 = tem;
340 tem = t1, t1 = t2, t2 = tem;
341 tem = lb1, lb1 = lb2, lb2 = tem;
342 tem = ub1, ub1 = ub2, ub2 = tem;
343 tem = length1, length1 = length2, length2 = tem;
344 tem = a1_is_null, a1_is_null = a2_is_null, a2_is_null = tem;
345 btem = a1_side_effects_p, a1_side_effects_p = a2_side_effects_p,
346 a2_side_effects_p = btem;
347 }
348
349 /* If the length of the second array is the constant zero, we can just
350 use the original stored bounds for the first array and see whether
351 last < first holds. */
352 if (integer_zerop (length2))
353 {
354 length_zero_p = true;
355
356 lb1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (lb1, a1);
357 ub1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (ub1, a1);
358
359 comparison = fold_build2_loc (loc, LT_EXPR, result_type, ub1, lb1);
360
361 this_a1_is_null = comparison;
362 this_a2_is_null = convert (result_type, boolean_true_node);
363 }
364
365 /* Otherwise, if the length is some other constant value, we know that
366 this dimension in the second array cannot be superflat, so we can
367 just use its length computed from the actual stored bounds. */
368 else if (TREE_CODE (length2) == INTEGER_CST)
369 {
370 /* Note that we know that LB2 and UB2 are constant and hence
371 cannot contain a PLACEHOLDER_EXPR. */
372 lb1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (lb1, a1);
373 ub1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (ub1, a1);
374
375 comparison
376 = fold_build2_loc (loc, EQ_EXPR, result_type,
377 build_binary_op (MINUS_EXPR, base_type,
378 ub1, lb1),
379 build_binary_op (MINUS_EXPR, base_type,
380 ub2, lb2));
381 this_a1_is_null
382 = fold_build2_loc (loc, LT_EXPR, result_type, ub1, lb1);
383
384 this_a2_is_null = convert (result_type, boolean_false_node);
385 }
386
387 /* Otherwise, compare the computed lengths. */
388 else
389 {
390 length1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (length1, a1);
391 length2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (length2, a2);
392
393 comparison
394 = fold_build2_loc (loc, EQ_EXPR, result_type, length1, length2);
395
396 lb1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (lb1, a1);
397 ub1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (ub1, a1);
398
399 this_a1_is_null
400 = fold_build2_loc (loc, LT_EXPR, result_type, ub1, lb1);
401
402 lb2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (lb2, a2);
403 ub2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (ub2, a2);
404
405 this_a2_is_null
406 = fold_build2_loc (loc, LT_EXPR, result_type, ub2, lb2);
407 }
408
409 /* Append expressions for this dimension to the final expressions. */
410 result = build_binary_op (TRUTH_ANDIF_EXPR, result_type,
411 result, comparison);
412
413 a1_is_null = build_binary_op (TRUTH_ORIF_EXPR, result_type,
414 this_a1_is_null, a1_is_null);
415
416 a2_is_null = build_binary_op (TRUTH_ORIF_EXPR, result_type,
417 this_a2_is_null, a2_is_null);
418
419 t1 = TREE_TYPE (t1);
420 t2 = TREE_TYPE (t2);
421 }
422
423 /* Unless the length of some dimension is known to be zero, compare the
424 data in the array. */
425 if (!length_zero_p)
426 {
427 tree type = find_common_type (TREE_TYPE (a1), TREE_TYPE (a2));
428 tree comparison;
429
430 if (type)
431 {
432 a1 = convert (type, a1),
433 a2 = convert (type, a2);
434 }
435
436 comparison = fold_build2_loc (loc, EQ_EXPR, result_type, a1, a2);
437
438 result
439 = build_binary_op (TRUTH_ANDIF_EXPR, result_type, result, comparison);
440 }
441
442 /* The result is also true if both sizes are zero. */
443 result = build_binary_op (TRUTH_ORIF_EXPR, result_type,
444 build_binary_op (TRUTH_ANDIF_EXPR, result_type,
445 a1_is_null, a2_is_null),
446 result);
447
448 /* If the operands have side-effects, they need to be evaluated before
449 doing the tests above since the place they otherwise would end up
450 being evaluated at run time could be wrong. */
451 if (a1_side_effects_p)
452 result = build2 (COMPOUND_EXPR, result_type, a1, result);
453
454 if (a2_side_effects_p)
455 result = build2 (COMPOUND_EXPR, result_type, a2, result);
456
457 return result;
458 }
459
460 /* Return an expression tree representing an ordering comparison of A1 and A2,
461 two objects of type ARRAY_TYPE. The result should be of type RESULT_TYPE.
462
463 A1 is less than A2 according to the following alternative:
464 - when A1's length is less than A2'length: if every element of A1 is equal
465 to its counterpart in A2 or the first differing is lesser in A1 than A2,
466 - otherwise: if not every element of A2 is equal to its counterpart in A1
467 and the first differing is lesser in A1 than A2.
468
469 The other 3 ordering comparisons can be easily deduced from this one. */
470
471 static tree
472 compare_arrays_for_ordering (location_t loc, tree result_type, tree a1, tree a2)
473 {
474 const bool a1_side_effects_p = TREE_SIDE_EFFECTS (a1);
475 const bool a2_side_effects_p = TREE_SIDE_EFFECTS (a2);
476 tree t1 = TREE_TYPE (a1);
477 tree t2 = TREE_TYPE (a2);
478 tree dom1 = TYPE_DOMAIN (t1);
479 tree dom2 = TYPE_DOMAIN (t2);
480 tree length1 = size_binop (PLUS_EXPR,
481 size_binop (MINUS_EXPR,
482 TYPE_MAX_VALUE (dom1),
483 TYPE_MIN_VALUE (dom1)),
484 size_one_node);
485 tree length2 = size_binop (PLUS_EXPR,
486 size_binop (MINUS_EXPR,
487 TYPE_MAX_VALUE (dom2),
488 TYPE_MIN_VALUE (dom2)),
489 size_one_node);
490 tree addr1, addr2, fndecl, result;
491
492 /* If the lengths are known at compile time, fold the alternative and let the
493 gimplifier optimize the case of power-of-two lengths. */
494 if (TREE_CODE (length1) == INTEGER_CST && TREE_CODE (length2) == INTEGER_CST)
495 return tree_int_cst_compare (length1, length2) < 0
496 ? fold_build2_loc (loc, LE_EXPR, result_type, a1, convert (t1, a2))
497 : fold_build2_loc (loc, LT_EXPR, result_type, convert (t2, a1), a2);
498
499 /* If the operands have side-effects, they need to be evaluated only once
500 in spite of the multiple references in the comparison. */
501 if (a1_side_effects_p)
502 a1 = gnat_protect_expr (a1);
503
504 if (a2_side_effects_p)
505 a2 = gnat_protect_expr (a2);
506
507 length1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (length1, a1);
508 length2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (length2, a2);
509
510 /* If the lengths are not known at compile time, call memcmp directly with
511 the actual lengths since a1 and a2 may have the same nominal subtype. */
512 addr1 = build_fold_addr_expr_loc (loc, a1);
513 addr2 = build_fold_addr_expr_loc (loc, a2);
514 fndecl = builtin_decl_implicit (BUILT_IN_MEMCMP);
515
516 result
517 = fold_build3_loc (loc, COND_EXPR, result_type,
518 fold_build2_loc (loc, LT_EXPR, boolean_type_node,
519 length1, length2),
520 fold_build2_loc (loc, LE_EXPR, result_type,
521 build_call_expr_loc (loc, fndecl, 3,
522 addr1, addr2,
523 length1),
524 integer_zero_node),
525 fold_build2_loc (loc, LT_EXPR, result_type,
526 build_call_expr_loc (loc, fndecl, 3,
527 addr1, addr2,
528 length2),
529 integer_zero_node));
530
531 /* If the operands have side-effects, they need to be evaluated before
532 doing the tests above since the place they otherwise would end up
533 being evaluated at run time could be wrong. */
534 if (a1_side_effects_p)
535 result = build2 (COMPOUND_EXPR, result_type, a1, result);
536
537 if (a2_side_effects_p)
538 result = build2 (COMPOUND_EXPR, result_type, a2, result);
539
540 return result;
541 }
542
543 /* Return an expression tree representing an equality comparison of P1 and P2,
544 two objects of fat pointer type. The result should be of type RESULT_TYPE.
545
546 Two fat pointers are equal in one of two ways: (1) if both have a null
547 pointer to the array or (2) if they contain the same couple of pointers.
548 We perform the comparison in as efficient a manner as possible. */
549
550 static tree
551 compare_fat_pointers (location_t loc, tree result_type, tree p1, tree p2)
552 {
553 tree p1_array, p2_array, p1_bounds, p2_bounds, same_array, same_bounds;
554 tree p1_array_is_null, p2_array_is_null;
555
556 /* If either operand has side-effects, they have to be evaluated only once
557 in spite of the multiple references to the operand in the comparison. */
558 p1 = gnat_protect_expr (p1);
559 p2 = gnat_protect_expr (p2);
560
561 /* The constant folder doesn't fold fat pointer types so we do it here. */
562 if (TREE_CODE (p1) == CONSTRUCTOR)
563 p1_array = CONSTRUCTOR_ELT (p1, 0)->value;
564 else
565 p1_array = build_component_ref (p1, TYPE_FIELDS (TREE_TYPE (p1)), true);
566
567 p1_array_is_null
568 = fold_build2_loc (loc, EQ_EXPR, result_type, p1_array,
569 fold_convert_loc (loc, TREE_TYPE (p1_array),
570 null_pointer_node));
571
572 if (TREE_CODE (p2) == CONSTRUCTOR)
573 p2_array = CONSTRUCTOR_ELT (p2, 0)->value;
574 else
575 p2_array = build_component_ref (p2, TYPE_FIELDS (TREE_TYPE (p2)), true);
576
577 p2_array_is_null
578 = fold_build2_loc (loc, EQ_EXPR, result_type, p2_array,
579 fold_convert_loc (loc, TREE_TYPE (p2_array),
580 null_pointer_node));
581
582 /* If one of the pointers to the array is null, just compare the other. */
583 if (integer_zerop (p1_array))
584 return p2_array_is_null;
585 else if (integer_zerop (p2_array))
586 return p1_array_is_null;
587
588 /* Otherwise, do the fully-fledged comparison. */
589 same_array
590 = fold_build2_loc (loc, EQ_EXPR, result_type, p1_array, p2_array);
591
592 if (TREE_CODE (p1) == CONSTRUCTOR)
593 p1_bounds = CONSTRUCTOR_ELT (p1, 1)->value;
594 else
595 p1_bounds
596 = build_component_ref (p1, DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (p1))),
597 true);
598
599 if (TREE_CODE (p2) == CONSTRUCTOR)
600 p2_bounds = CONSTRUCTOR_ELT (p2, 1)->value;
601 else
602 p2_bounds
603 = build_component_ref (p2, DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (p2))),
604 true);
605
606 same_bounds
607 = fold_build2_loc (loc, EQ_EXPR, result_type, p1_bounds, p2_bounds);
608
609 /* P1_ARRAY == P2_ARRAY && (P1_ARRAY == NULL || P1_BOUNDS == P2_BOUNDS). */
610 return build_binary_op (TRUTH_ANDIF_EXPR, result_type, same_array,
611 build_binary_op (TRUTH_ORIF_EXPR, result_type,
612 p1_array_is_null, same_bounds));
613 }
614
615 /* Try to compute the reduction of OP modulo MODULUS in PRECISION bits with a
616 division-free algorithm. Return NULL_TREE if this is not easily doable. */
617
618 tree
619 fast_modulo_reduction (tree op, tree modulus, unsigned int precision)
620 {
621 const tree type = TREE_TYPE (op);
622 const unsigned int type_precision = TYPE_PRECISION (type);
623
624 /* The implementation is host-dependent for the time being. */
625 if (type_precision <= HOST_BITS_PER_WIDE_INT)
626 {
627 const unsigned HOST_WIDE_INT d = tree_to_uhwi (modulus);
628 unsigned HOST_WIDE_INT ml, mh;
629 int pre_shift, post_shift;
630 tree t;
631
632 /* The trick is to replace the division by d with a multiply-and-shift
633 sequence parameterized by a (multiplier, shifter) pair computed from
634 d, the precision of the type and the needed precision:
635
636 op / d = (op * multiplier) >> shifter
637
638 But choose_multiplier provides a slightly different interface:
639
640 op / d = (op h* multiplier) >> reduced_shifter
641
642 that makes things easier by using a high-part multiplication. */
643 mh = choose_multiplier (d, type_precision, precision, &ml, &post_shift);
644
645 /* If the suggested multiplier is more than TYPE_PRECISION bits, we can
646 do better for even divisors, using an initial right shift. */
647 if (mh != 0 && (d & 1) == 0)
648 {
649 pre_shift = ctz_or_zero (d);
650 mh = choose_multiplier (d >> pre_shift, type_precision,
651 precision - pre_shift, &ml, &post_shift);
652 }
653 else
654 pre_shift = 0;
655
656 /* If the suggested multiplier is still more than TYPE_PRECISION bits,
657 or the TYPE_MODE does not have a high-part multiply, try again with
658 a larger type up to the word size. */
659 if (mh != 0 || !can_mult_highpart_p (TYPE_MODE (type), true))
660 {
661 if (type_precision < BITS_PER_WORD)
662 {
663 const scalar_int_mode m
664 = smallest_int_mode_for_size (type_precision + 1).require ();
665 tree new_type = gnat_type_for_mode (m, 1);
666 op = fold_convert (new_type, op);
667 modulus = fold_convert (new_type, modulus);
668 t = fast_modulo_reduction (op, modulus, precision);
669 if (t)
670 return fold_convert (type, t);
671 }
672
673 return NULL_TREE;
674 }
675
676 /* This computes op - (op / modulus) * modulus with PRECISION bits. */
677 op = gnat_protect_expr (op);
678
679 /* t = op >> pre_shift
680 t = t h* ml
681 t = t >> post_shift
682 t = t * modulus */
683 if (pre_shift)
684 t = fold_build2 (RSHIFT_EXPR, type, op,
685 build_int_cst (type, pre_shift));
686 else
687 t = op;
688 t = fold_build2 (MULT_HIGHPART_EXPR, type, t, build_int_cst (type, ml));
689 if (post_shift)
690 t = fold_build2 (RSHIFT_EXPR, type, t,
691 build_int_cst (type, post_shift));
692 t = fold_build2 (MULT_EXPR, type, t, modulus);
693
694 return fold_build2 (MINUS_EXPR, type, op, t);
695 }
696
697 else
698 return NULL_TREE;
699 }
700
701 /* Compute the result of applying OP_CODE to LHS and RHS, where both are of
702 TYPE. We know that TYPE is a modular type with a nonbinary modulus. */
703
704 static tree
705 nonbinary_modular_operation (enum tree_code op_code, tree type, tree lhs,
706 tree rhs)
707 {
708 tree modulus = TYPE_MODULUS (type);
709 unsigned precision = tree_floor_log2 (modulus) + 1;
710 tree op_type, result, fmr;
711
712 /* For the logical operations, we only need PRECISION bits. For addition and
713 subtraction, we need one more, and for multiplication twice as many. */
714 if (op_code == PLUS_EXPR || op_code == MINUS_EXPR)
715 precision += 1;
716 else if (op_code == MULT_EXPR)
717 precision *= 2;
718
719 /* If the type is not wide enough, make a new type of the needed precision
720 and convert modulus and operands to it. Use a type with full precision
721 for its mode since operations are ultimately performed in the mode. */
722 if (TYPE_PRECISION (type) < precision)
723 {
724 const scalar_int_mode m
725 = smallest_int_mode_for_size (precision).require ();
726 op_type = gnat_type_for_mode (m, 1);
727 modulus = fold_convert (op_type, modulus);
728 lhs = fold_convert (op_type, lhs);
729 rhs = fold_convert (op_type, rhs);
730 }
731 else
732 op_type = type;
733
734 /* Do the operation, then we'll fix it up. */
735 result = fold_build2 (op_code, op_type, lhs, rhs);
736
737 /* Unconditionally add the modulus to the result for a subtraction, this gets
738 rid of all its peculiarities by cancelling out the addition of the binary
739 modulus in the case where the subtraction wraps around in OP_TYPE, and may
740 even generate better code on architectures with conditional moves. */
741 if (op_code == MINUS_EXPR)
742 result = fold_build2 (PLUS_EXPR, op_type, result, modulus);
743
744 /* For a multiplication, we first try to do a modulo reduction by means of a
745 (multiplier, shifter) pair in the needed precision up to the word size, or
746 else we fall back to a standard modulo operation. But not when optimizing
747 for size, because it will be longer than a div+mul+sub sequence. */
748 if (op_code == MULT_EXPR)
749 {
750 if (!optimize_size
751 && precision <= BITS_PER_WORD
752 && (fmr = fast_modulo_reduction (result, modulus, precision)))
753 result = fmr;
754 else
755 result = fold_build2 (TRUNC_MOD_EXPR, op_type, result, modulus);
756 }
757
758 /* For the other operations, subtract the modulus if we are >= it. */
759 else
760 {
761 result = gnat_protect_expr (result);
762 result = fold_build3 (COND_EXPR, op_type,
763 fold_build2 (GE_EXPR, boolean_type_node,
764 result, modulus),
765 fold_build2 (MINUS_EXPR, op_type,
766 result, modulus),
767 result);
768 }
769
770 return fold_convert (type, result);
771 }
772
773 /* This page contains routines that implement the Ada semantics with regard
774 to atomic objects. They are fully piggybacked on the middle-end support
775 for atomic loads and stores.
776
777 *** Memory barriers and volatile objects ***
778
779 We implement the weakened form of the C.6(16) clause that was introduced
780 in Ada 2012 (AI05-117). Earlier forms of this clause wouldn't have been
781 implementable without significant performance hits on modern platforms.
782
783 We also take advantage of the requirements imposed on shared variables by
784 9.10 (conditions for sequential actions) to have non-erroneous execution
785 and consider that C.6(16) and C.6(17) only prescribe an uniform order of
786 volatile updates with regard to sequential actions, i.e. with regard to
787 reads or updates of atomic objects.
788
789 As such, an update of an atomic object by a task requires that all earlier
790 accesses to volatile objects have completed. Similarly, later accesses to
791 volatile objects cannot be reordered before the update of the atomic object.
792 So, memory barriers both before and after the atomic update are needed.
793
794 For a read of an atomic object, to avoid seeing writes of volatile objects
795 by a task earlier than by the other tasks, a memory barrier is needed before
796 the atomic read. Finally, to avoid reordering later reads or updates of
797 volatile objects to before the atomic read, a barrier is needed after the
798 atomic read.
799
800 So, memory barriers are needed before and after atomic reads and updates.
801 And, in order to simplify the implementation, we use full memory barriers
802 in all cases, i.e. we enforce sequential consistency for atomic accesses. */
803
804 /* Return the size of TYPE, which must be a positive power of 2. */
805
806 unsigned int
807 resolve_atomic_size (tree type)
808 {
809 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
810
811 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
812 return size;
813
814 /* We shouldn't reach here without having already detected that the size
815 isn't compatible with an atomic access. */
816 gcc_assert (Serious_Errors_Detected);
817
818 return 0;
819 }
820
821 /* Build an atomic load for the underlying atomic object in SRC. SYNC is
822 true if the load requires synchronization. */
823
824 tree
825 build_atomic_load (tree src, bool sync)
826 {
827 tree ptr_type
828 = build_pointer_type
829 (build_qualified_type (void_type_node,
830 TYPE_QUAL_ATOMIC | TYPE_QUAL_VOLATILE));
831 tree mem_model
832 = build_int_cst (integer_type_node,
833 sync ? MEMMODEL_SEQ_CST : MEMMODEL_RELAXED);
834 tree orig_src = src;
835 tree type, t, addr, val;
836 unsigned int size;
837 int fncode;
838
839 /* Remove conversions to get the address of the underlying object. */
840 src = remove_conversions (src, false);
841 type = TREE_TYPE (src);
842 size = resolve_atomic_size (type);
843 if (size == 0)
844 return orig_src;
845
846 fncode = (int) BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
847 t = builtin_decl_implicit ((enum built_in_function) fncode);
848
849 addr = build_unary_op (ADDR_EXPR, ptr_type, src);
850 val = build_call_expr (t, 2, addr, mem_model);
851
852 /* First reinterpret the loaded bits in the original type of the load,
853 then convert to the expected result type. */
854 t = fold_build1 (VIEW_CONVERT_EXPR, type, val);
855 return convert (TREE_TYPE (orig_src), t);
856 }
857
858 /* Build an atomic store from SRC to the underlying atomic object in DEST.
859 SYNC is true if the store requires synchronization. */
860
861 tree
862 build_atomic_store (tree dest, tree src, bool sync)
863 {
864 tree ptr_type
865 = build_pointer_type
866 (build_qualified_type (void_type_node,
867 TYPE_QUAL_ATOMIC | TYPE_QUAL_VOLATILE));
868 tree mem_model
869 = build_int_cst (integer_type_node,
870 sync ? MEMMODEL_SEQ_CST : MEMMODEL_RELAXED);
871 tree orig_dest = dest;
872 tree type, t, int_type, addr;
873 unsigned int size;
874 int fncode;
875
876 /* Remove conversions to get the address of the underlying object. */
877 dest = remove_conversions (dest, false);
878 type = TREE_TYPE (dest);
879 size = resolve_atomic_size (type);
880 if (size == 0)
881 return build_binary_op (MODIFY_EXPR, NULL_TREE, orig_dest, src);
882
883 fncode = (int) BUILT_IN_ATOMIC_STORE_N + exact_log2 (size) + 1;
884 t = builtin_decl_implicit ((enum built_in_function) fncode);
885 int_type = gnat_type_for_size (BITS_PER_UNIT * size, 1);
886
887 /* First convert the bits to be stored to the original type of the store,
888 then reinterpret them in the effective type. But if the original type
889 is a padded type with the same size, convert to the inner type instead,
890 as we don't want to artificially introduce a CONSTRUCTOR here. */
891 if (TYPE_IS_PADDING_P (type)
892 && TYPE_SIZE (type) == TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (type))))
893 src = convert (TREE_TYPE (TYPE_FIELDS (type)), src);
894 else
895 src = convert (type, src);
896 src = fold_build1 (VIEW_CONVERT_EXPR, int_type, src);
897 addr = build_unary_op (ADDR_EXPR, ptr_type, dest);
898
899 return build_call_expr (t, 3, addr, src, mem_model);
900 }
901
902 /* Build a load-modify-store sequence from SRC to DEST. GNAT_NODE is used for
903 the location of the sequence. Note that, even though the load and the store
904 are both atomic, the sequence itself is not atomic. */
905
906 tree
907 build_load_modify_store (tree dest, tree src, Node_Id gnat_node)
908 {
909 /* We will be modifying DEST below so we build a copy. */
910 dest = copy_node (dest);
911 tree ref = dest;
912
913 while (handled_component_p (ref))
914 {
915 /* The load should already have been generated during the translation
916 of the GNAT destination tree; find it out in the GNU tree. */
917 if (TREE_CODE (TREE_OPERAND (ref, 0)) == VIEW_CONVERT_EXPR)
918 {
919 tree op = TREE_OPERAND (TREE_OPERAND (ref, 0), 0);
920 if (TREE_CODE (op) == CALL_EXPR && call_is_atomic_load (op))
921 {
922 tree type = TREE_TYPE (TREE_OPERAND (ref, 0));
923 tree t = CALL_EXPR_ARG (op, 0);
924 tree obj, temp, stmt;
925
926 /* Find out the loaded object. */
927 if (TREE_CODE (t) == NOP_EXPR)
928 t = TREE_OPERAND (t, 0);
929 if (TREE_CODE (t) == ADDR_EXPR)
930 obj = TREE_OPERAND (t, 0);
931 else
932 obj = build1 (INDIRECT_REF, type, t);
933
934 /* Drop atomic and volatile qualifiers for the temporary. */
935 type = TYPE_MAIN_VARIANT (type);
936
937 /* And drop BLKmode, if need be, to put it into a register. */
938 if (TYPE_MODE (type) == BLKmode)
939 {
940 unsigned int size = tree_to_uhwi (TYPE_SIZE (type));
941 type = copy_type (type);
942 machine_mode mode = int_mode_for_size (size, 0).else_blk ();
943 SET_TYPE_MODE (type, mode);
944 }
945
946 /* Create the temporary by inserting a SAVE_EXPR. */
947 temp = build1 (SAVE_EXPR, type,
948 build1 (VIEW_CONVERT_EXPR, type, op));
949 TREE_OPERAND (ref, 0) = temp;
950
951 start_stmt_group ();
952
953 /* Build the modify of the temporary. */
954 stmt = build_binary_op (MODIFY_EXPR, NULL_TREE, dest, src);
955 add_stmt_with_node (stmt, gnat_node);
956
957 /* Build the store to the object. */
958 stmt = build_atomic_store (obj, temp, false);
959 add_stmt_with_node (stmt, gnat_node);
960
961 return end_stmt_group ();
962 }
963 }
964
965 TREE_OPERAND (ref, 0) = copy_node (TREE_OPERAND (ref, 0));
966 ref = TREE_OPERAND (ref, 0);
967 }
968
969 /* Something went wrong earlier if we have not found the atomic load. */
970 gcc_unreachable ();
971 }
972
973 /* Make a binary operation of kind OP_CODE. RESULT_TYPE is the type
974 desired for the result. Usually the operation is to be performed
975 in that type. For INIT_EXPR and MODIFY_EXPR, RESULT_TYPE must be
976 NULL_TREE. For ARRAY_REF, RESULT_TYPE may be NULL_TREE, in which
977 case the type to be used will be derived from the operands.
978 Don't fold the result if NO_FOLD is true.
979
980 This function is very much unlike the ones for C and C++ since we
981 have already done any type conversion and matching required. All we
982 have to do here is validate the work done by SEM and handle subtypes. */
983
984 tree
985 build_binary_op (enum tree_code op_code, tree result_type,
986 tree left_operand, tree right_operand,
987 bool no_fold)
988 {
989 tree left_type = TREE_TYPE (left_operand);
990 tree right_type = TREE_TYPE (right_operand);
991 tree left_base_type = get_base_type (left_type);
992 tree right_base_type = get_base_type (right_type);
993 tree operation_type = result_type;
994 tree best_type = NULL_TREE;
995 tree modulus, result;
996 bool has_side_effects = false;
997
998 if (operation_type
999 && TREE_CODE (operation_type) == RECORD_TYPE
1000 && TYPE_JUSTIFIED_MODULAR_P (operation_type))
1001 operation_type = TREE_TYPE (TYPE_FIELDS (operation_type));
1002
1003 if (operation_type && TYPE_IS_EXTRA_SUBTYPE_P (operation_type))
1004 operation_type = get_base_type (operation_type);
1005
1006 modulus = (operation_type
1007 && TREE_CODE (operation_type) == INTEGER_TYPE
1008 && TYPE_MODULAR_P (operation_type)
1009 ? TYPE_MODULUS (operation_type) : NULL_TREE);
1010
1011 switch (op_code)
1012 {
1013 case INIT_EXPR:
1014 case MODIFY_EXPR:
1015 gcc_checking_assert (!result_type);
1016
1017 /* If there were integral or pointer conversions on the LHS, remove
1018 them; we'll be putting them back below if needed. Likewise for
1019 conversions between record types, except for justified modular types.
1020 But don't do this if the right operand is not BLKmode (for packed
1021 arrays) unless we are not changing the mode, or if both ooperands
1022 are view conversions to the same type. */
1023 while ((CONVERT_EXPR_P (left_operand)
1024 || TREE_CODE (left_operand) == VIEW_CONVERT_EXPR)
1025 && (((INTEGRAL_TYPE_P (left_type)
1026 || POINTER_TYPE_P (left_type))
1027 && (INTEGRAL_TYPE_P (operand_type (left_operand))
1028 || POINTER_TYPE_P (operand_type (left_operand))))
1029 || (TREE_CODE (left_type) == RECORD_TYPE
1030 && !TYPE_JUSTIFIED_MODULAR_P (left_type)
1031 && TREE_CODE (operand_type (left_operand)) == RECORD_TYPE
1032 && (TYPE_MODE (right_type) == BLKmode
1033 || TYPE_MODE (left_type)
1034 == TYPE_MODE (operand_type (left_operand)))
1035 && !(TREE_CODE (left_operand) == VIEW_CONVERT_EXPR
1036 && TREE_CODE (right_operand) == VIEW_CONVERT_EXPR
1037 && left_type == right_type))))
1038 {
1039 left_operand = TREE_OPERAND (left_operand, 0);
1040 left_type = TREE_TYPE (left_operand);
1041 }
1042
1043 /* If a class-wide type may be involved, force use of the RHS type. */
1044 if ((TREE_CODE (right_type) == RECORD_TYPE
1045 || TREE_CODE (right_type) == UNION_TYPE)
1046 && TYPE_ALIGN_OK (right_type))
1047 operation_type = right_type;
1048
1049 /* If we are copying between padded objects with compatible types, use
1050 the padded view of the objects, this is very likely more efficient.
1051 Likewise for a padded object that is assigned a constructor, if we
1052 can convert the constructor to the inner type, to avoid putting a
1053 VIEW_CONVERT_EXPR on the LHS. But don't do so if we wouldn't have
1054 actually copied anything. */
1055 else if (TYPE_IS_PADDING_P (left_type)
1056 && TREE_CONSTANT (TYPE_SIZE (left_type))
1057 && ((TREE_CODE (right_operand) == COMPONENT_REF
1058 && TYPE_MAIN_VARIANT (left_type)
1059 == TYPE_MAIN_VARIANT (operand_type (right_operand)))
1060 || (TREE_CODE (right_operand) == CONSTRUCTOR
1061 && !CONTAINS_PLACEHOLDER_P
1062 (DECL_SIZE (TYPE_FIELDS (left_type)))))
1063 && !integer_zerop (TYPE_SIZE (right_type)))
1064 {
1065 /* We make an exception for a BLKmode type padding a non-BLKmode
1066 inner type and do the conversion of the LHS right away, since
1067 unchecked_convert wouldn't do it properly. */
1068 if (TYPE_MODE (left_type) == BLKmode
1069 && TYPE_MODE (right_type) != BLKmode
1070 && TREE_CODE (right_operand) != CONSTRUCTOR)
1071 {
1072 operation_type = right_type;
1073 left_operand = convert (operation_type, left_operand);
1074 left_type = operation_type;
1075 }
1076 else
1077 operation_type = left_type;
1078 }
1079
1080 /* If we have a call to a function that returns with variable size, use
1081 the RHS type in case we want to use the return slot optimization. */
1082 else if (TREE_CODE (right_operand) == CALL_EXPR
1083 && return_type_with_variable_size_p (right_type))
1084 operation_type = right_type;
1085
1086 /* Find the best type to use for copying between aggregate types. */
1087 else if (((TREE_CODE (left_type) == ARRAY_TYPE
1088 && TREE_CODE (right_type) == ARRAY_TYPE)
1089 || (TREE_CODE (left_type) == RECORD_TYPE
1090 && TREE_CODE (right_type) == RECORD_TYPE))
1091 && (best_type = find_common_type (left_type, right_type)))
1092 operation_type = best_type;
1093
1094 /* Otherwise use the LHS type. */
1095 else
1096 operation_type = left_type;
1097
1098 /* Ensure everything on the LHS is valid. If we have a field reference,
1099 strip anything that get_inner_reference can handle. Then remove any
1100 conversions between types having the same code and mode. And mark
1101 VIEW_CONVERT_EXPRs with TREE_ADDRESSABLE. When done, we must have
1102 either an INDIRECT_REF, a NULL_EXPR, a SAVE_EXPR or a DECL node. */
1103 result = left_operand;
1104 while (true)
1105 {
1106 tree restype = TREE_TYPE (result);
1107
1108 if (TREE_CODE (result) == COMPONENT_REF
1109 || TREE_CODE (result) == ARRAY_REF
1110 || TREE_CODE (result) == ARRAY_RANGE_REF)
1111 while (handled_component_p (result))
1112 result = TREE_OPERAND (result, 0);
1113
1114 else if (TREE_CODE (result) == REALPART_EXPR
1115 || TREE_CODE (result) == IMAGPART_EXPR
1116 || (CONVERT_EXPR_P (result)
1117 && (((TREE_CODE (restype)
1118 == TREE_CODE (operand_type (result))
1119 && TYPE_MODE (restype)
1120 == TYPE_MODE (operand_type (result))))
1121 || TYPE_ALIGN_OK (restype))))
1122 result = TREE_OPERAND (result, 0);
1123
1124 else if (TREE_CODE (result) == VIEW_CONVERT_EXPR)
1125 {
1126 TREE_ADDRESSABLE (result) = 1;
1127 result = TREE_OPERAND (result, 0);
1128 }
1129
1130 else
1131 break;
1132 }
1133
1134 gcc_assert (INDIRECT_REF_P (result)
1135 || TREE_CODE (result) == NULL_EXPR
1136 || TREE_CODE (result) == SAVE_EXPR
1137 || DECL_P (result));
1138
1139 /* Convert the right operand to the operation type unless it is
1140 either already of the correct type or if the type involves a
1141 placeholder, since the RHS may not have the same record type. */
1142 if (operation_type != right_type
1143 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (operation_type)))
1144 {
1145 right_operand = convert (operation_type, right_operand);
1146 right_type = operation_type;
1147 }
1148
1149 /* If the left operand is not of the same type as the operation
1150 type, wrap it up in a VIEW_CONVERT_EXPR. */
1151 if (left_type != operation_type)
1152 left_operand = unchecked_convert (operation_type, left_operand, false);
1153
1154 has_side_effects = true;
1155 modulus = NULL_TREE;
1156 break;
1157
1158 case ARRAY_REF:
1159 if (!operation_type)
1160 operation_type = TREE_TYPE (left_type);
1161
1162 /* ... fall through ... */
1163
1164 case ARRAY_RANGE_REF:
1165 /* First look through conversion between type variants. Note that
1166 this changes neither the operation type nor the type domain. */
1167 if (TREE_CODE (left_operand) == VIEW_CONVERT_EXPR
1168 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (left_operand, 0)))
1169 == TYPE_MAIN_VARIANT (left_type))
1170 {
1171 left_operand = TREE_OPERAND (left_operand, 0);
1172 left_type = TREE_TYPE (left_operand);
1173 }
1174
1175 /* For a range, make sure the element type is consistent. */
1176 if (op_code == ARRAY_RANGE_REF
1177 && TREE_TYPE (operation_type) != TREE_TYPE (left_type))
1178 {
1179 operation_type = copy_type (operation_type);
1180 TREE_TYPE (operation_type) = TREE_TYPE (left_type);
1181
1182 /* Declare it now since it will never be declared otherwise. This
1183 is necessary to ensure that its subtrees are properly marked. */
1184 create_type_decl (TYPE_NAME (operation_type), operation_type, true,
1185 false, Empty);
1186 }
1187
1188 /* Then convert the right operand to its base type. This will prevent
1189 unneeded sign conversions when sizetype is wider than integer. */
1190 right_operand = convert (right_base_type, right_operand);
1191 right_operand = convert_to_index_type (right_operand);
1192 modulus = NULL_TREE;
1193 break;
1194
1195 case TRUTH_ANDIF_EXPR:
1196 case TRUTH_ORIF_EXPR:
1197 case TRUTH_AND_EXPR:
1198 case TRUTH_OR_EXPR:
1199 case TRUTH_XOR_EXPR:
1200 gcc_checking_assert
1201 (TREE_CODE (get_base_type (result_type)) == BOOLEAN_TYPE);
1202 operation_type = left_base_type;
1203 left_operand = convert (operation_type, left_operand);
1204 right_operand = convert (operation_type, right_operand);
1205 break;
1206
1207 case GE_EXPR:
1208 case LE_EXPR:
1209 case GT_EXPR:
1210 case LT_EXPR:
1211 case EQ_EXPR:
1212 case NE_EXPR:
1213 gcc_checking_assert
1214 (TREE_CODE (get_base_type (result_type)) == BOOLEAN_TYPE);
1215 /* If either operand is a NULL_EXPR, just return a new one. */
1216 if (TREE_CODE (left_operand) == NULL_EXPR)
1217 return build2 (op_code, result_type,
1218 build1 (NULL_EXPR, integer_type_node,
1219 TREE_OPERAND (left_operand, 0)),
1220 integer_zero_node);
1221
1222 else if (TREE_CODE (right_operand) == NULL_EXPR)
1223 return build2 (op_code, result_type,
1224 build1 (NULL_EXPR, integer_type_node,
1225 TREE_OPERAND (right_operand, 0)),
1226 integer_zero_node);
1227
1228 /* If either object is a justified modular types, get the
1229 fields from within. */
1230 if (TREE_CODE (left_type) == RECORD_TYPE
1231 && TYPE_JUSTIFIED_MODULAR_P (left_type))
1232 {
1233 left_operand = convert (TREE_TYPE (TYPE_FIELDS (left_type)),
1234 left_operand);
1235 left_type = TREE_TYPE (left_operand);
1236 left_base_type = get_base_type (left_type);
1237 }
1238
1239 if (TREE_CODE (right_type) == RECORD_TYPE
1240 && TYPE_JUSTIFIED_MODULAR_P (right_type))
1241 {
1242 right_operand = convert (TREE_TYPE (TYPE_FIELDS (right_type)),
1243 right_operand);
1244 right_type = TREE_TYPE (right_operand);
1245 right_base_type = get_base_type (right_type);
1246 }
1247
1248 /* If both objects are arrays, compare them specially. */
1249 if ((TREE_CODE (left_type) == ARRAY_TYPE
1250 || (TREE_CODE (left_type) == INTEGER_TYPE
1251 && TYPE_HAS_ACTUAL_BOUNDS_P (left_type)))
1252 && (TREE_CODE (right_type) == ARRAY_TYPE
1253 || (TREE_CODE (right_type) == INTEGER_TYPE
1254 && TYPE_HAS_ACTUAL_BOUNDS_P (right_type))))
1255 {
1256 if (op_code == EQ_EXPR || op_code == NE_EXPR)
1257 {
1258 result
1259 = compare_arrays_for_equality (input_location, result_type,
1260 left_operand, right_operand);
1261 if (op_code == NE_EXPR)
1262 result = invert_truthvalue_loc (input_location, result);
1263 }
1264
1265 else
1266 {
1267 /* Swap the operands to canonicalize to LT_EXPR or GE_EXPR. */
1268 if (op_code == GT_EXPR || op_code == LE_EXPR)
1269 result
1270 = compare_arrays_for_ordering (input_location, result_type,
1271 right_operand, left_operand);
1272
1273 else
1274 result
1275 = compare_arrays_for_ordering (input_location, result_type,
1276 left_operand, right_operand);
1277
1278 /* GE_EXPR is (not LT_EXPR) for discrete array types. */
1279 if (op_code == GE_EXPR || op_code == LE_EXPR)
1280 result = invert_truthvalue_loc (input_location, result);
1281 }
1282
1283 return result;
1284 }
1285
1286 /* Otherwise, the base types must be the same, unless they are both (fat)
1287 pointer types or record types. In the latter case, use the best type
1288 and convert both operands to that type. */
1289 if (left_base_type != right_base_type)
1290 {
1291 if (TYPE_IS_FAT_POINTER_P (left_base_type)
1292 && TYPE_IS_FAT_POINTER_P (right_base_type))
1293 {
1294 gcc_assert (TYPE_MAIN_VARIANT (left_base_type)
1295 == TYPE_MAIN_VARIANT (right_base_type));
1296 best_type = left_base_type;
1297 }
1298
1299 else if (POINTER_TYPE_P (left_base_type)
1300 && POINTER_TYPE_P (right_base_type))
1301 {
1302 tree left_ref_type = TREE_TYPE (left_base_type);
1303 tree right_ref_type = TREE_TYPE (right_base_type);
1304
1305 /* Anonymous access types in Ada 2005 may point to compatible
1306 object subtypes or function types in the language sense. */
1307 gcc_assert (FUNCTION_POINTER_TYPE_P (left_ref_type)
1308 == FUNCTION_POINTER_TYPE_P (right_ref_type));
1309 best_type = left_base_type;
1310 }
1311
1312 else if (TREE_CODE (left_base_type) == RECORD_TYPE
1313 && TREE_CODE (right_base_type) == RECORD_TYPE)
1314 {
1315 /* The only way this is permitted is if both types have the same
1316 name. In that case, one of them must not be self-referential.
1317 Use it as the best type. Even better with a fixed size. */
1318 gcc_assert (TYPE_NAME (left_base_type)
1319 && TYPE_NAME (left_base_type)
1320 == TYPE_NAME (right_base_type));
1321
1322 if (TREE_CONSTANT (TYPE_SIZE (left_base_type)))
1323 best_type = left_base_type;
1324 else if (TREE_CONSTANT (TYPE_SIZE (right_base_type)))
1325 best_type = right_base_type;
1326 else if (!CONTAINS_PLACEHOLDER_P (TYPE_SIZE (left_base_type)))
1327 best_type = left_base_type;
1328 else if (!CONTAINS_PLACEHOLDER_P (TYPE_SIZE (right_base_type)))
1329 best_type = right_base_type;
1330 else
1331 gcc_unreachable ();
1332 }
1333
1334 else
1335 gcc_unreachable ();
1336
1337 left_operand = convert (best_type, left_operand);
1338 right_operand = convert (best_type, right_operand);
1339 }
1340 else
1341 {
1342 left_operand = convert (left_base_type, left_operand);
1343 right_operand = convert (right_base_type, right_operand);
1344 }
1345
1346 /* If both objects are fat pointers, compare them specially. */
1347 if (TYPE_IS_FAT_POINTER_P (left_base_type))
1348 {
1349 result
1350 = compare_fat_pointers (input_location,
1351 result_type, left_operand, right_operand);
1352 if (op_code == NE_EXPR)
1353 result = invert_truthvalue_loc (EXPR_LOCATION (result), result);
1354 else
1355 gcc_assert (op_code == EQ_EXPR);
1356
1357 return result;
1358 }
1359
1360 modulus = NULL_TREE;
1361 break;
1362
1363 case LSHIFT_EXPR:
1364 case RSHIFT_EXPR:
1365 case LROTATE_EXPR:
1366 case RROTATE_EXPR:
1367 /* The RHS of a shift can be any type. Also, ignore any modulus
1368 (we used to abort, but this is needed for unchecked conversion
1369 to modular types). Otherwise, processing is the same as normal. */
1370 gcc_assert (operation_type == left_base_type);
1371 modulus = NULL_TREE;
1372 left_operand = convert (operation_type, left_operand);
1373 break;
1374
1375 case BIT_AND_EXPR:
1376 case BIT_IOR_EXPR:
1377 case BIT_XOR_EXPR:
1378 /* For binary modulus, if the inputs are in range, so are the
1379 outputs. */
1380 if (modulus && integer_pow2p (modulus))
1381 modulus = NULL_TREE;
1382 goto common;
1383
1384 case COMPLEX_EXPR:
1385 gcc_assert (TREE_TYPE (result_type) == left_base_type
1386 && TREE_TYPE (result_type) == right_base_type);
1387 left_operand = convert (left_base_type, left_operand);
1388 right_operand = convert (right_base_type, right_operand);
1389 break;
1390
1391 case TRUNC_DIV_EXPR: case TRUNC_MOD_EXPR:
1392 case CEIL_DIV_EXPR: case CEIL_MOD_EXPR:
1393 case FLOOR_DIV_EXPR: case FLOOR_MOD_EXPR:
1394 case ROUND_DIV_EXPR: case ROUND_MOD_EXPR:
1395 /* These always produce results lower than either operand. */
1396 modulus = NULL_TREE;
1397 goto common;
1398
1399 case POINTER_PLUS_EXPR:
1400 gcc_assert (operation_type == left_base_type
1401 && sizetype == right_base_type);
1402 left_operand = convert (operation_type, left_operand);
1403 right_operand = convert (sizetype, right_operand);
1404 break;
1405
1406 case PLUS_NOMOD_EXPR:
1407 case MINUS_NOMOD_EXPR:
1408 if (op_code == PLUS_NOMOD_EXPR)
1409 op_code = PLUS_EXPR;
1410 else
1411 op_code = MINUS_EXPR;
1412 modulus = NULL_TREE;
1413
1414 /* ... fall through ... */
1415
1416 case PLUS_EXPR:
1417 case MINUS_EXPR:
1418 /* Avoid doing arithmetics in ENUMERAL_TYPE or BOOLEAN_TYPE like the
1419 other compilers. Contrary to C, Ada doesn't allow arithmetics in
1420 these types but can generate addition/subtraction for Succ/Pred. */
1421 if (operation_type
1422 && (TREE_CODE (operation_type) == ENUMERAL_TYPE
1423 || TREE_CODE (operation_type) == BOOLEAN_TYPE))
1424 operation_type = left_base_type = right_base_type
1425 = gnat_type_for_mode (TYPE_MODE (operation_type),
1426 TYPE_UNSIGNED (operation_type));
1427
1428 /* ... fall through ... */
1429
1430 default:
1431 common:
1432 /* The result type should be the same as the base types of the
1433 both operands (and they should be the same). Convert
1434 everything to the result type. */
1435
1436 gcc_assert (operation_type == left_base_type
1437 && left_base_type == right_base_type);
1438 left_operand = convert (operation_type, left_operand);
1439 right_operand = convert (operation_type, right_operand);
1440 }
1441
1442 if (modulus && !integer_pow2p (modulus))
1443 {
1444 result = nonbinary_modular_operation (op_code, operation_type,
1445 left_operand, right_operand);
1446 modulus = NULL_TREE;
1447 }
1448 /* If either operand is a NULL_EXPR, just return a new one. */
1449 else if (TREE_CODE (left_operand) == NULL_EXPR)
1450 return build1 (NULL_EXPR, operation_type, TREE_OPERAND (left_operand, 0));
1451 else if (TREE_CODE (right_operand) == NULL_EXPR)
1452 return build1 (NULL_EXPR, operation_type, TREE_OPERAND (right_operand, 0));
1453 else if (op_code == ARRAY_REF || op_code == ARRAY_RANGE_REF)
1454 {
1455 result = build4 (op_code, operation_type, left_operand, right_operand,
1456 NULL_TREE, NULL_TREE);
1457 if (!no_fold)
1458 result = fold (result);
1459 }
1460 else if (op_code == INIT_EXPR || op_code == MODIFY_EXPR)
1461 result = build2 (op_code, void_type_node, left_operand, right_operand);
1462 else if (no_fold)
1463 result = build2 (op_code, operation_type, left_operand, right_operand);
1464 else
1465 result
1466 = fold_build2 (op_code, operation_type, left_operand, right_operand);
1467
1468 if (TREE_CONSTANT (result))
1469 ;
1470 else if (op_code == ARRAY_REF || op_code == ARRAY_RANGE_REF)
1471 {
1472 if (TYPE_VOLATILE (operation_type))
1473 TREE_THIS_VOLATILE (result) = 1;
1474 }
1475 else if (TREE_CONSTANT (left_operand) && TREE_CONSTANT (right_operand))
1476 TREE_CONSTANT (result) = 1;
1477
1478 if (has_side_effects)
1479 TREE_SIDE_EFFECTS (result) = 1;
1480
1481 /* If we are working with modular types, perform the MOD operation
1482 if something above hasn't eliminated the need for it. */
1483 if (modulus)
1484 {
1485 modulus = convert (operation_type, modulus);
1486 if (no_fold)
1487 result = build2 (FLOOR_MOD_EXPR, operation_type, result, modulus);
1488 else
1489 result = fold_build2 (FLOOR_MOD_EXPR, operation_type, result, modulus);
1490 }
1491
1492 if (result_type && result_type != operation_type)
1493 result = convert (result_type, result);
1494
1495 return result;
1496 }
1497
1498 /* Similar, but for unary operations. */
1499
1500 tree
1501 build_unary_op (enum tree_code op_code, tree result_type, tree operand)
1502 {
1503 tree type = TREE_TYPE (operand);
1504 tree base_type = get_base_type (type);
1505 tree operation_type = result_type;
1506 tree result;
1507
1508 if (operation_type
1509 && TREE_CODE (operation_type) == RECORD_TYPE
1510 && TYPE_JUSTIFIED_MODULAR_P (operation_type))
1511 operation_type = TREE_TYPE (TYPE_FIELDS (operation_type));
1512
1513 if (operation_type
1514 && TREE_CODE (operation_type) == INTEGER_TYPE
1515 && TYPE_EXTRA_SUBTYPE_P (operation_type))
1516 operation_type = get_base_type (operation_type);
1517
1518 switch (op_code)
1519 {
1520 case REALPART_EXPR:
1521 case IMAGPART_EXPR:
1522 if (!operation_type)
1523 result_type = operation_type = TREE_TYPE (type);
1524 else
1525 gcc_assert (result_type == TREE_TYPE (type));
1526
1527 result = fold_build1 (op_code, operation_type, operand);
1528 break;
1529
1530 case TRUTH_NOT_EXPR:
1531 gcc_checking_assert
1532 (TREE_CODE (get_base_type (result_type)) == BOOLEAN_TYPE);
1533 result = invert_truthvalue_loc (EXPR_LOCATION (operand), operand);
1534 /* When not optimizing, fold the result as invert_truthvalue_loc
1535 doesn't fold the result of comparisons. This is intended to undo
1536 the trick used for boolean rvalues in gnat_to_gnu. */
1537 if (!optimize)
1538 result = fold (result);
1539 break;
1540
1541 case ATTR_ADDR_EXPR:
1542 case ADDR_EXPR:
1543 switch (TREE_CODE (operand))
1544 {
1545 case INDIRECT_REF:
1546 case UNCONSTRAINED_ARRAY_REF:
1547 result = TREE_OPERAND (operand, 0);
1548
1549 /* Make sure the type here is a pointer, not a reference.
1550 GCC wants pointer types for function addresses. */
1551 if (!result_type)
1552 result_type = build_pointer_type (type);
1553
1554 /* If the underlying object can alias everything, propagate the
1555 property since we are effectively retrieving the object. */
1556 if (POINTER_TYPE_P (TREE_TYPE (result))
1557 && TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (result)))
1558 {
1559 if (TREE_CODE (result_type) == POINTER_TYPE
1560 && !TYPE_REF_CAN_ALIAS_ALL (result_type))
1561 result_type
1562 = build_pointer_type_for_mode (TREE_TYPE (result_type),
1563 TYPE_MODE (result_type),
1564 true);
1565 else if (TREE_CODE (result_type) == REFERENCE_TYPE
1566 && !TYPE_REF_CAN_ALIAS_ALL (result_type))
1567 result_type
1568 = build_reference_type_for_mode (TREE_TYPE (result_type),
1569 TYPE_MODE (result_type),
1570 true);
1571 }
1572 break;
1573
1574 case NULL_EXPR:
1575 result = operand;
1576 TREE_TYPE (result) = type = build_pointer_type (type);
1577 break;
1578
1579 case COMPOUND_EXPR:
1580 /* Fold a compound expression if it has unconstrained array type
1581 since the middle-end cannot handle it. But we don't it in the
1582 general case because it may introduce aliasing issues if the
1583 first operand is an indirect assignment and the second operand
1584 the corresponding address, e.g. for an allocator. However do
1585 it for a return value to expose it for later recognition. */
1586 if (TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE
1587 || (VAR_P (TREE_OPERAND (operand, 1))
1588 && DECL_RETURN_VALUE_P (TREE_OPERAND (operand, 1))))
1589 {
1590 result = build_unary_op (ADDR_EXPR, result_type,
1591 TREE_OPERAND (operand, 1));
1592 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
1593 TREE_OPERAND (operand, 0), result);
1594 break;
1595 }
1596 goto common;
1597
1598 case ARRAY_REF:
1599 case ARRAY_RANGE_REF:
1600 case COMPONENT_REF:
1601 case BIT_FIELD_REF:
1602 /* If this is for 'Address, find the address of the prefix and add
1603 the offset to the field. Otherwise, do this the normal way. */
1604 if (op_code == ATTR_ADDR_EXPR)
1605 {
1606 poly_int64 bitsize;
1607 poly_int64 bitpos;
1608 tree offset, inner;
1609 machine_mode mode;
1610 int unsignedp, reversep, volatilep;
1611
1612 inner = get_inner_reference (operand, &bitsize, &bitpos, &offset,
1613 &mode, &unsignedp, &reversep,
1614 &volatilep);
1615
1616 /* If INNER is a padding type whose field has a self-referential
1617 size, convert to that inner type. We know the offset is zero
1618 and we need to have that type visible. */
1619 if (type_is_padding_self_referential (TREE_TYPE (inner)))
1620 inner = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (inner))),
1621 inner);
1622
1623 /* Compute the offset as a byte offset from INNER. */
1624 if (!offset)
1625 offset = size_zero_node;
1626
1627 offset
1628 = size_binop (PLUS_EXPR, offset,
1629 size_int (bits_to_bytes_round_down (bitpos)));
1630
1631 /* Take the address of INNER, formally convert it to a pointer
1632 to the operand type, and finally add the offset. */
1633 inner = build_unary_op (ADDR_EXPR, NULL_TREE, inner);
1634 inner
1635 = fold_convert (build_pointer_type (TREE_TYPE (operand)),
1636 inner);
1637 result = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (inner),
1638 inner, offset);
1639 break;
1640 }
1641 goto common;
1642
1643 case CONSTRUCTOR:
1644 /* If this is just a constructor for a padded record, we can
1645 just take the address of the single field and convert it to
1646 a pointer to our type. */
1647 if (TYPE_IS_PADDING_P (type))
1648 {
1649 result
1650 = build_unary_op (ADDR_EXPR,
1651 build_pointer_type (TREE_TYPE (operand)),
1652 CONSTRUCTOR_ELT (operand, 0)->value);
1653 break;
1654 }
1655 goto common;
1656
1657 case NOP_EXPR:
1658 if (AGGREGATE_TYPE_P (type)
1659 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (operand, 0))))
1660 return build_unary_op (ADDR_EXPR, result_type,
1661 TREE_OPERAND (operand, 0));
1662
1663 /* ... fallthru ... */
1664
1665 case VIEW_CONVERT_EXPR:
1666 /* If this just a variant conversion or if the conversion doesn't
1667 change the mode, get the result type from this type and go down.
1668 This is needed for conversions of CONST_DECLs, to eventually get
1669 to the address of their CORRESPONDING_VARs. */
1670 if ((TYPE_MAIN_VARIANT (type)
1671 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (operand, 0))))
1672 || (TYPE_MODE (type) != BLKmode
1673 && (TYPE_MODE (type)
1674 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (operand, 0))))))
1675 return build_unary_op (ADDR_EXPR,
1676 (result_type ? result_type
1677 : build_pointer_type (type)),
1678 TREE_OPERAND (operand, 0));
1679 goto common;
1680
1681 case CONST_DECL:
1682 operand = DECL_CONST_CORRESPONDING_VAR (operand);
1683
1684 /* ... fall through ... */
1685
1686 default:
1687 common:
1688
1689 /* If we are taking the address of a padded record whose field
1690 contains a template, take the address of the field. */
1691 if (TYPE_IS_PADDING_P (type)
1692 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (type))) == RECORD_TYPE
1693 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (TYPE_FIELDS (type))))
1694 {
1695 type = TREE_TYPE (TYPE_FIELDS (type));
1696 operand = convert (type, operand);
1697 }
1698
1699 gnat_mark_addressable (operand);
1700 result = build_fold_addr_expr (operand);
1701 }
1702
1703 if (TREE_CONSTANT (operand) || staticp (operand))
1704 TREE_CONSTANT (result) = 1;
1705
1706 break;
1707
1708 case INDIRECT_REF:
1709 {
1710 tree t = remove_conversions (operand, false);
1711 bool can_never_be_null = DECL_P (t) && DECL_CAN_NEVER_BE_NULL_P (t);
1712
1713 /* If TYPE is a thin pointer, either first retrieve the base if this
1714 is an expression with an offset built for the initialization of an
1715 object with an unconstrained nominal subtype, or else convert to
1716 the fat pointer. */
1717 if (TYPE_IS_THIN_POINTER_P (type))
1718 {
1719 tree rec_type = TREE_TYPE (type);
1720
1721 if (TREE_CODE (operand) == POINTER_PLUS_EXPR
1722 && TREE_OPERAND (operand, 1)
1723 == byte_position (DECL_CHAIN (TYPE_FIELDS (rec_type)))
1724 && TREE_CODE (TREE_OPERAND (operand, 0)) == NOP_EXPR)
1725 {
1726 operand = TREE_OPERAND (TREE_OPERAND (operand, 0), 0);
1727 type = TREE_TYPE (operand);
1728 }
1729 else if (TYPE_UNCONSTRAINED_ARRAY (rec_type))
1730 {
1731 operand
1732 = convert (TREE_TYPE (TYPE_UNCONSTRAINED_ARRAY (rec_type)),
1733 operand);
1734 type = TREE_TYPE (operand);
1735 }
1736 }
1737
1738 /* If we want to refer to an unconstrained array, use the appropriate
1739 expression. But this will never survive down to the back-end. */
1740 if (TYPE_IS_FAT_POINTER_P (type))
1741 {
1742 result = build1 (UNCONSTRAINED_ARRAY_REF,
1743 TYPE_UNCONSTRAINED_ARRAY (type), operand);
1744 TREE_READONLY (result)
1745 = TYPE_READONLY (TYPE_UNCONSTRAINED_ARRAY (type));
1746 }
1747
1748 /* If we are dereferencing an ADDR_EXPR, return its operand. */
1749 else if (TREE_CODE (operand) == ADDR_EXPR)
1750 result = TREE_OPERAND (operand, 0);
1751
1752 /* Otherwise, build and fold the indirect reference. */
1753 else
1754 {
1755 result = build_fold_indirect_ref (operand);
1756 TREE_READONLY (result) = TYPE_READONLY (TREE_TYPE (type));
1757 }
1758
1759 if (!TYPE_IS_FAT_POINTER_P (type) && TYPE_VOLATILE (TREE_TYPE (type)))
1760 {
1761 TREE_SIDE_EFFECTS (result) = 1;
1762 if (INDIRECT_REF_P (result))
1763 TREE_THIS_VOLATILE (result) = TYPE_VOLATILE (TREE_TYPE (result));
1764 }
1765
1766 if ((INDIRECT_REF_P (result)
1767 || TREE_CODE (result) == UNCONSTRAINED_ARRAY_REF)
1768 && can_never_be_null)
1769 TREE_THIS_NOTRAP (result) = 1;
1770
1771 break;
1772 }
1773
1774 case NEGATE_EXPR:
1775 case BIT_NOT_EXPR:
1776 {
1777 tree modulus = ((operation_type
1778 && TREE_CODE (operation_type) == INTEGER_TYPE
1779 && TYPE_MODULAR_P (operation_type))
1780 ? TYPE_MODULUS (operation_type) : NULL_TREE);
1781 int mod_pow2 = modulus && integer_pow2p (modulus);
1782
1783 /* If this is a modular type, there are various possibilities
1784 depending on the operation and whether the modulus is a
1785 power of two or not. */
1786
1787 if (modulus)
1788 {
1789 gcc_assert (operation_type == base_type);
1790 operand = convert (operation_type, operand);
1791
1792 /* The fastest in the negate case for binary modulus is
1793 the straightforward code; the TRUNC_MOD_EXPR below
1794 is an AND operation. */
1795 if (op_code == NEGATE_EXPR && mod_pow2)
1796 result = fold_build2 (TRUNC_MOD_EXPR, operation_type,
1797 fold_build1 (NEGATE_EXPR, operation_type,
1798 operand),
1799 modulus);
1800
1801 /* For nonbinary negate case, return zero for zero operand,
1802 else return the modulus minus the operand. If the modulus
1803 is a power of two minus one, we can do the subtraction
1804 as an XOR since it is equivalent and faster on most machines. */
1805 else if (op_code == NEGATE_EXPR && !mod_pow2)
1806 {
1807 if (integer_pow2p (fold_build2 (PLUS_EXPR, operation_type,
1808 modulus,
1809 build_int_cst (operation_type,
1810 1))))
1811 result = fold_build2 (BIT_XOR_EXPR, operation_type,
1812 operand, modulus);
1813 else
1814 result = fold_build2 (MINUS_EXPR, operation_type,
1815 modulus, operand);
1816
1817 result = fold_build3 (COND_EXPR, operation_type,
1818 fold_build2 (NE_EXPR,
1819 boolean_type_node,
1820 operand,
1821 build_int_cst
1822 (operation_type, 0)),
1823 result, operand);
1824 }
1825 else
1826 {
1827 /* For the NOT cases, we need a constant equal to
1828 the modulus minus one. For a binary modulus, we
1829 XOR against the constant and subtract the operand from
1830 that constant for nonbinary modulus. */
1831
1832 tree cnst = fold_build2 (MINUS_EXPR, operation_type, modulus,
1833 build_int_cst (operation_type, 1));
1834
1835 if (mod_pow2)
1836 result = fold_build2 (BIT_XOR_EXPR, operation_type,
1837 operand, cnst);
1838 else
1839 result = fold_build2 (MINUS_EXPR, operation_type,
1840 cnst, operand);
1841 }
1842
1843 break;
1844 }
1845 }
1846
1847 /* ... fall through ... */
1848
1849 default:
1850 gcc_assert (operation_type == base_type);
1851 result = fold_build1 (op_code, operation_type,
1852 convert (operation_type, operand));
1853 }
1854
1855 if (result_type && TREE_TYPE (result) != result_type)
1856 result = convert (result_type, result);
1857
1858 return result;
1859 }
1860
1861 /* Similar, but for COND_EXPR. */
1862
1863 tree
1864 build_cond_expr (tree result_type, tree condition_operand,
1865 tree true_operand, tree false_operand)
1866 {
1867 bool addr_p = false;
1868 tree result;
1869
1870 /* The front-end verified that result, true and false operands have
1871 same base type. Convert everything to the result type. */
1872 true_operand = convert (result_type, true_operand);
1873 false_operand = convert (result_type, false_operand);
1874
1875 /* If the result type is unconstrained or variable-sized, take the address
1876 of the operands and then dereference the result. Likewise if the result
1877 type is passed by reference, because creating a temporary of this type is
1878 not allowed. */
1879 if (TREE_CODE (result_type) == UNCONSTRAINED_ARRAY_TYPE
1880 || type_contains_placeholder_p (result_type)
1881 || !TREE_CONSTANT (TYPE_SIZE (result_type))
1882 || TYPE_IS_BY_REFERENCE_P (result_type))
1883 {
1884 result_type = build_pointer_type (result_type);
1885 true_operand = build_unary_op (ADDR_EXPR, result_type, true_operand);
1886 false_operand = build_unary_op (ADDR_EXPR, result_type, false_operand);
1887 addr_p = true;
1888 }
1889
1890 result = fold_build3 (COND_EXPR, result_type, condition_operand,
1891 true_operand, false_operand);
1892
1893 /* If we have a common SAVE_EXPR (possibly surrounded by arithmetics)
1894 in both arms, make sure it gets evaluated by moving it ahead of the
1895 conditional expression. This is necessary because it is evaluated
1896 in only one place at run time and would otherwise be uninitialized
1897 in one of the arms. */
1898 true_operand = skip_simple_arithmetic (true_operand);
1899 false_operand = skip_simple_arithmetic (false_operand);
1900
1901 if (true_operand == false_operand && TREE_CODE (true_operand) == SAVE_EXPR)
1902 result = build2 (COMPOUND_EXPR, result_type, true_operand, result);
1903
1904 if (addr_p)
1905 result = build_unary_op (INDIRECT_REF, NULL_TREE, result);
1906
1907 return result;
1908 }
1909
1910 /* Similar, but for COMPOUND_EXPR. */
1911
1912 tree
1913 build_compound_expr (tree result_type, tree stmt_operand, tree expr_operand)
1914 {
1915 bool addr_p = false;
1916 tree result;
1917
1918 /* If the result type is unconstrained, take the address of the operand and
1919 then dereference the result. Likewise if the result type is passed by
1920 reference, but this is natively handled in the gimplifier. */
1921 if (TREE_CODE (result_type) == UNCONSTRAINED_ARRAY_TYPE
1922 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (result_type)))
1923 {
1924 result_type = build_pointer_type (result_type);
1925 expr_operand = build_unary_op (ADDR_EXPR, result_type, expr_operand);
1926 addr_p = true;
1927 }
1928
1929 result = fold_build2 (COMPOUND_EXPR, result_type, stmt_operand,
1930 expr_operand);
1931
1932 if (addr_p)
1933 result = build_unary_op (INDIRECT_REF, NULL_TREE, result);
1934
1935 return result;
1936 }
1937
1938 /* Conveniently construct a function call expression. FNDECL names the
1939 function to be called, N is the number of arguments, and the "..."
1940 parameters are the argument expressions. Unlike build_call_expr
1941 this doesn't fold the call, hence it will always return a CALL_EXPR. */
1942
1943 tree
1944 build_call_n_expr (tree fndecl, int n, ...)
1945 {
1946 va_list ap;
1947 tree fntype = TREE_TYPE (fndecl);
1948 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
1949
1950 va_start (ap, n);
1951 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
1952 va_end (ap);
1953 return fn;
1954 }
1955
1956 /* Build a goto to LABEL for a raise, with an optional call to Local_Raise.
1957 MSG gives the exception's identity for the call to Local_Raise, if any. */
1958
1959 static tree
1960 build_goto_raise (Entity_Id gnat_label, int msg)
1961 {
1962 tree gnu_label = gnat_to_gnu_entity (gnat_label, NULL_TREE, false);
1963 tree gnu_result = build1 (GOTO_EXPR, void_type_node, gnu_label);
1964 Entity_Id local_raise = Get_Local_Raise_Call_Entity ();
1965
1966 /* If Local_Raise is present, build Local_Raise (Exception'Identity). */
1967 if (Present (local_raise))
1968 {
1969 tree gnu_local_raise
1970 = gnat_to_gnu_entity (local_raise, NULL_TREE, false);
1971 tree gnu_exception_entity
1972 = gnat_to_gnu_entity (Get_RT_Exception_Entity (msg), NULL_TREE, false);
1973 tree gnu_call
1974 = build_call_n_expr (gnu_local_raise, 1,
1975 build_unary_op (ADDR_EXPR, NULL_TREE,
1976 gnu_exception_entity));
1977 gnu_result
1978 = build2 (COMPOUND_EXPR, void_type_node, gnu_call, gnu_result);
1979 }
1980
1981 TREE_USED (gnu_label) = 1;
1982 return gnu_result;
1983 }
1984
1985 /* Expand the SLOC of GNAT_NODE, if present, into tree location information
1986 pointed to by FILENAME, LINE and COL. Fall back to the current location
1987 if GNAT_NODE is absent or has no SLOC. */
1988
1989 static void
1990 expand_sloc (Node_Id gnat_node, tree *filename, tree *line, tree *col)
1991 {
1992 const char *str;
1993 int line_number, column_number;
1994
1995 if (Debug_Flag_NN || Exception_Locations_Suppressed)
1996 {
1997 str = "";
1998 line_number = 0;
1999 column_number = 0;
2000 }
2001 else if (Present (gnat_node) && Sloc (gnat_node) != No_Location)
2002 {
2003 str = Get_Name_String
2004 (Debug_Source_Name (Get_Source_File_Index (Sloc (gnat_node))));
2005 line_number = Get_Logical_Line_Number (Sloc (gnat_node));
2006 column_number = Get_Column_Number (Sloc (gnat_node));
2007 }
2008 else
2009 {
2010 str = lbasename (LOCATION_FILE (input_location));
2011 line_number = LOCATION_LINE (input_location);
2012 column_number = LOCATION_COLUMN (input_location);
2013 }
2014
2015 const int len = strlen (str);
2016 *filename = build_string (len, str);
2017 TREE_TYPE (*filename) = build_array_type (char_type_node,
2018 build_index_type (size_int (len)));
2019 *line = build_int_cst (NULL_TREE, line_number);
2020 if (col)
2021 *col = build_int_cst (NULL_TREE, column_number);
2022 }
2023
2024 /* Build a call to a function that raises an exception and passes file name
2025 and line number, if requested. MSG says which exception function to call.
2026 GNAT_NODE is the node conveying the source location for which the error
2027 should be signaled, or Empty in which case the error is signaled for the
2028 current location. KIND says which kind of exception node this is for,
2029 among N_Raise_{Constraint,Storage,Program}_Error. */
2030
2031 tree
2032 build_call_raise (int msg, Node_Id gnat_node, char kind)
2033 {
2034 Entity_Id gnat_label = get_exception_label (kind);
2035 tree fndecl = gnat_raise_decls[msg];
2036 tree filename, line;
2037
2038 /* If this is to be done as a goto, handle that case. */
2039 if (Present (gnat_label))
2040 return build_goto_raise (gnat_label, msg);
2041
2042 expand_sloc (gnat_node, &filename, &line, NULL);
2043
2044 return
2045 build_call_n_expr (fndecl, 2,
2046 build1 (ADDR_EXPR,
2047 build_pointer_type (char_type_node),
2048 filename),
2049 line);
2050 }
2051
2052 /* Similar to build_call_raise, with extra information about the column
2053 where the check failed. */
2054
2055 tree
2056 build_call_raise_column (int msg, Node_Id gnat_node, char kind)
2057 {
2058 Entity_Id gnat_label = get_exception_label (kind);
2059 tree fndecl = gnat_raise_decls_ext[msg];
2060 tree filename, line, col;
2061
2062 /* If this is to be done as a goto, handle that case. */
2063 if (Present (gnat_label))
2064 return build_goto_raise (gnat_label, msg);
2065
2066 expand_sloc (gnat_node, &filename, &line, &col);
2067
2068 return
2069 build_call_n_expr (fndecl, 3,
2070 build1 (ADDR_EXPR,
2071 build_pointer_type (char_type_node),
2072 filename),
2073 line, col);
2074 }
2075
2076 /* Similar to build_call_raise_column, for an index or range check exception ,
2077 with extra information of the form "INDEX out of range FIRST..LAST". */
2078
2079 tree
2080 build_call_raise_range (int msg, Node_Id gnat_node, char kind,
2081 tree index, tree first, tree last)
2082 {
2083 Entity_Id gnat_label = get_exception_label (kind);
2084 tree fndecl = gnat_raise_decls_ext[msg];
2085 tree filename, line, col;
2086
2087 /* If this is to be done as a goto, handle that case. */
2088 if (Present (gnat_label))
2089 return build_goto_raise (gnat_label, msg);
2090
2091 expand_sloc (gnat_node, &filename, &line, &col);
2092
2093 return
2094 build_call_n_expr (fndecl, 6,
2095 build1 (ADDR_EXPR,
2096 build_pointer_type (char_type_node),
2097 filename),
2098 line, col,
2099 convert (integer_type_node, index),
2100 convert (integer_type_node, first),
2101 convert (integer_type_node, last));
2102 }
2103
2104 /* qsort comparer for the bit positions of two constructor elements
2105 for record components. */
2106
2107 static int
2108 compare_elmt_bitpos (const void *rt1, const void *rt2)
2109 {
2110 const constructor_elt * const elmt1 = (const constructor_elt *) rt1;
2111 const constructor_elt * const elmt2 = (const constructor_elt *) rt2;
2112 const_tree const field1 = elmt1->index;
2113 const_tree const field2 = elmt2->index;
2114 const int ret
2115 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
2116
2117 if (ret)
2118 return ret;
2119
2120 /* The bit position can be the same if one of the fields has zero size.
2121 In this case, if the other has nonzero size, put the former first to
2122 match the layout done by components_to_record. Otherwise, preserve
2123 the order of the source code. */
2124
2125 const bool field1_zero_size = integer_zerop (DECL_SIZE (field1));
2126 const bool field2_zero_size = integer_zerop (DECL_SIZE (field2));
2127
2128 if (field1_zero_size && !field2_zero_size)
2129 return -1;
2130 else if (!field1_zero_size && field2_zero_size)
2131 return 1;
2132 else
2133 return (int) (DECL_UID (field1) - DECL_UID (field2));
2134 }
2135
2136 /* Return a CONSTRUCTOR of TYPE whose elements are V. */
2137
2138 tree
2139 gnat_build_constructor (tree type, vec<constructor_elt, va_gc> *v)
2140 {
2141 bool allconstant = (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST);
2142 bool read_only = true;
2143 bool side_effects = false;
2144 tree result, obj, val;
2145 unsigned int n_elmts;
2146
2147 /* Scan the elements to see if they are all constant or if any has side
2148 effects, to let us set global flags on the resulting constructor. Count
2149 the elements along the way for possible sorting purposes below. */
2150 FOR_EACH_CONSTRUCTOR_ELT (v, n_elmts, obj, val)
2151 {
2152 /* The predicate must be in keeping with output_constructor and, unlike
2153 initializer_constant_valid_p, we accept "&{...}" because we'll put
2154 the CONSTRUCTOR into the constant pool during gimplification. */
2155 if ((!TREE_CONSTANT (val) && !TREE_STATIC (val))
2156 || (TREE_CODE (type) == RECORD_TYPE
2157 && CONSTRUCTOR_BITFIELD_P (obj)
2158 && !initializer_constant_valid_for_bitfield_p (val))
2159 || (!initializer_constant_valid_p (val,
2160 TREE_TYPE (val),
2161 TYPE_REVERSE_STORAGE_ORDER (type))
2162 && !(TREE_CODE (val) == ADDR_EXPR
2163 && TREE_CODE (TREE_OPERAND (val, 0)) == CONSTRUCTOR
2164 && TREE_CONSTANT (TREE_OPERAND (val, 0)))))
2165 allconstant = false;
2166
2167 if (!TREE_READONLY (val))
2168 read_only = false;
2169
2170 if (TREE_SIDE_EFFECTS (val))
2171 side_effects = true;
2172 }
2173
2174 /* For record types with constant components only, sort field list
2175 by increasing bit position. This is necessary to ensure the
2176 constructor can be output as static data. */
2177 if (allconstant && TREE_CODE (type) == RECORD_TYPE && n_elmts > 1)
2178 v->qsort (compare_elmt_bitpos);
2179
2180 result = build_constructor (type, v);
2181 CONSTRUCTOR_NO_CLEARING (result) = 1;
2182 TREE_CONSTANT (result) = TREE_STATIC (result) = allconstant;
2183 TREE_SIDE_EFFECTS (result) = side_effects;
2184 TREE_READONLY (result) = TYPE_READONLY (type) || read_only || allconstant;
2185 return result;
2186 }
2187
2188 /* Return a COMPONENT_REF to access FIELD in RECORD, or NULL_TREE if the field
2189 is not found in the record. Don't fold the result if NO_FOLD is true. */
2190
2191 static tree
2192 build_simple_component_ref (tree record, tree field, bool no_fold)
2193 {
2194 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (record));
2195 tree ref;
2196
2197 /* The failure of this assertion will very likely come from a missing
2198 insertion of an explicit dereference. */
2199 gcc_assert (RECORD_OR_UNION_TYPE_P (type));
2200
2201 /* The type must be frozen at this point. */
2202 gcc_assert (COMPLETE_TYPE_P (type));
2203
2204 /* Try to fold a conversion from another record or union type unless the type
2205 contains a placeholder as it might be needed for a later substitution. */
2206 if (TREE_CODE (record) == VIEW_CONVERT_EXPR
2207 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (record, 0)))
2208 && !type_contains_placeholder_p (type))
2209 {
2210 tree op = TREE_OPERAND (record, 0);
2211
2212 /* If this is an unpadding operation, convert the underlying object to
2213 the unpadded type directly. */
2214 if (TYPE_IS_PADDING_P (type) && field == TYPE_FIELDS (type))
2215 return convert (TREE_TYPE (field), op);
2216
2217 /* Otherwise try to access FIELD directly in the underlying type, but
2218 make sure that the form of the reference doesn't change too much;
2219 this can happen for an unconstrained bit-packed array type whose
2220 constrained form can be an integer type. */
2221 ref = build_simple_component_ref (op, field, no_fold);
2222 if (ref && TREE_CODE (TREE_TYPE (ref)) == TREE_CODE (TREE_TYPE (field)))
2223 return ref;
2224 }
2225
2226 /* If this field is not in the specified record, see if we can find a field
2227 in the specified record whose original field is the same as this one. */
2228 if (DECL_CONTEXT (field) != type)
2229 {
2230 tree new_field;
2231
2232 /* First loop through normal components. */
2233 for (new_field = TYPE_FIELDS (type);
2234 new_field;
2235 new_field = DECL_CHAIN (new_field))
2236 if (SAME_FIELD_P (field, new_field))
2237 break;
2238
2239 /* Next, loop through DECL_INTERNAL_P components if we haven't found the
2240 component in the first search. Doing this search in two steps is
2241 required to avoid hidden homonymous fields in the _Parent field. */
2242 if (!new_field)
2243 for (new_field = TYPE_FIELDS (type);
2244 new_field;
2245 new_field = DECL_CHAIN (new_field))
2246 if (DECL_INTERNAL_P (new_field)
2247 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (new_field)))
2248 {
2249 tree field_ref
2250 = build_simple_component_ref (record, new_field, no_fold);
2251 ref = build_simple_component_ref (field_ref, field, no_fold);
2252 if (ref)
2253 return ref;
2254 }
2255
2256 field = new_field;
2257 }
2258
2259 if (!field)
2260 return NULL_TREE;
2261
2262 /* If the field's offset has overflowed, do not try to access it, as doing
2263 so may trigger sanity checks deeper in the back-end. Note that we don't
2264 need to warn since this will be done on trying to declare the object. */
2265 if (TREE_CODE (DECL_FIELD_OFFSET (field)) == INTEGER_CST
2266 && TREE_OVERFLOW (DECL_FIELD_OFFSET (field)))
2267 return build1 (NULL_EXPR, TREE_TYPE (field),
2268 build_call_raise (SE_Object_Too_Large, Empty,
2269 N_Raise_Storage_Error));
2270
2271 ref = build3 (COMPONENT_REF, TREE_TYPE (field), record, field, NULL_TREE);
2272
2273 if (TREE_READONLY (record)
2274 || TREE_READONLY (field)
2275 || TYPE_READONLY (type))
2276 TREE_READONLY (ref) = 1;
2277
2278 if (TREE_THIS_VOLATILE (record)
2279 || TREE_THIS_VOLATILE (field)
2280 || TYPE_VOLATILE (type))
2281 TREE_THIS_VOLATILE (ref) = 1;
2282
2283 if (no_fold)
2284 return ref;
2285
2286 /* The generic folder may punt in this case because the inner array type
2287 can be self-referential, but folding is in fact not problematic. */
2288 if (TREE_CODE (record) == CONSTRUCTOR
2289 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (record)))
2290 {
2291 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (record);
2292 unsigned HOST_WIDE_INT idx;
2293 tree index, value;
2294 FOR_EACH_CONSTRUCTOR_ELT (elts, idx, index, value)
2295 if (index == field)
2296 return value;
2297 return ref;
2298 }
2299
2300 return fold (ref);
2301 }
2302
2303 /* Likewise, but return NULL_EXPR and generate a Program_Error if the
2304 field is not found in the record. */
2305
2306 tree
2307 build_component_ref (tree record, tree field, bool no_fold)
2308 {
2309 tree ref = build_simple_component_ref (record, field, no_fold);
2310 if (ref)
2311 return ref;
2312
2313 /* The missing field should have been detected in the front-end. */
2314 gigi_checking_assert (false);
2315
2316 /* Assume this is an invalid user field so raise Program_Error. */
2317 return build1 (NULL_EXPR, TREE_TYPE (field),
2318 build_call_raise (PE_Explicit_Raise, Empty,
2319 N_Raise_Program_Error));
2320 }
2321
2322 /* Helper for build_call_alloc_dealloc, with arguments to be interpreted
2323 identically. Process the case where a GNAT_PROC to call is provided. */
2324
2325 static inline tree
2326 build_call_alloc_dealloc_proc (tree gnu_obj, tree gnu_size, tree gnu_type,
2327 Entity_Id gnat_proc, Entity_Id gnat_pool)
2328 {
2329 tree gnu_proc = gnat_to_gnu (gnat_proc);
2330 tree gnu_align = size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT);
2331
2332 tree gnu_call;
2333
2334 /* A storage pool's underlying type is a record type for both predefined
2335 storage pools and GNAT simple storage pools. The return and secondary
2336 stacks use the same mechanism, but their pool object is an integer. */
2337 if (Is_Record_Type (Underlying_Type (Etype (gnat_pool))))
2338 {
2339 /* The size is the third parameter; the alignment is the
2340 same type. */
2341 Entity_Id gnat_size_type
2342 = Etype (Next_Formal (Next_Formal (First_Formal (gnat_proc))));
2343 tree gnu_size_type = gnat_to_gnu_type (gnat_size_type);
2344
2345 tree gnu_pool = gnat_to_gnu (gnat_pool);
2346 tree gnu_pool_addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_pool);
2347
2348 gnu_size = convert (gnu_size_type, gnu_size);
2349 gnu_align = convert (gnu_size_type, gnu_align);
2350
2351 /* The first arg is always the address of the storage pool; next
2352 comes the address of the object, for a deallocator, then the
2353 size and alignment. */
2354 if (gnu_obj)
2355 gnu_call = build_call_n_expr (gnu_proc, 4, gnu_pool_addr, gnu_obj,
2356 gnu_size, gnu_align);
2357 else
2358 gnu_call = build_call_n_expr (gnu_proc, 3, gnu_pool_addr,
2359 gnu_size, gnu_align);
2360 }
2361
2362 else
2363 {
2364 /* The size is the second parameter. */
2365 Entity_Id gnat_size_type
2366 = Etype (Next_Formal (First_Formal (gnat_proc)));
2367 tree gnu_size_type = gnat_to_gnu_type (gnat_size_type);
2368
2369 /* Deallocation is not supported for return and secondary stacks. */
2370 gcc_assert (!gnu_obj);
2371
2372 gnu_size = convert (gnu_size_type, gnu_size);
2373 gnu_align = convert (gnu_size_type, gnu_align);
2374
2375 if (DECL_BUILT_IN_CLASS (gnu_proc) == BUILT_IN_FRONTEND
2376 && DECL_FE_FUNCTION_CODE (gnu_proc) == BUILT_IN_RETURN_SLOT)
2377 {
2378 /* This must be a function that returns by invisible reference. */
2379 gcc_assert (current_function_decl
2380 && TREE_ADDRESSABLE (TREE_TYPE (current_function_decl)));
2381 tree gnu_ret_size;
2382
2383 gnu_call = DECL_RESULT (current_function_decl);
2384
2385 /* The allocation has already been done by the caller so we check that
2386 we are not going to overflow the return slot. */
2387 if (TYPE_CI_CO_LIST (TREE_TYPE (current_function_decl)))
2388 gnu_ret_size
2389 = TYPE_SIZE_UNIT
2390 (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (TREE_TYPE (gnu_call)))));
2391 else
2392 gnu_ret_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (gnu_call)));
2393
2394 gnu_call
2395 = fold_build3 (COND_EXPR, TREE_TYPE (gnu_call),
2396 fold_build2 (LE_EXPR, boolean_type_node,
2397 fold_convert (sizetype, gnu_size),
2398 gnu_ret_size),
2399 gnu_call,
2400 build_call_raise (PE_Explicit_Raise, Empty,
2401 N_Raise_Program_Error));
2402 }
2403
2404 else
2405 gnu_call = build_call_n_expr (gnu_proc, 2, gnu_size, gnu_align);
2406 }
2407
2408 return gnu_call;
2409 }
2410
2411 /* Helper for build_call_alloc_dealloc, to build and return an allocator for
2412 DATA_SIZE bytes aimed at containing a DATA_TYPE object, using the default
2413 __gnat_malloc allocator. Honor DATA_TYPE alignments greater than what the
2414 latter offers. */
2415
2416 static inline tree
2417 maybe_wrap_malloc (tree data_size, tree data_type, Node_Id gnat_node)
2418 {
2419 /* When the DATA_TYPE alignment is stricter than what malloc offers
2420 (super-aligned case), we allocate an "aligning" wrapper type and return
2421 the address of its single data field with the malloc's return value
2422 stored just in front. */
2423
2424 unsigned int data_align = TYPE_ALIGN (data_type);
2425 unsigned int system_allocator_alignment
2426 = get_target_system_allocator_alignment () * BITS_PER_UNIT;
2427
2428 tree aligning_type
2429 = ((data_align > system_allocator_alignment)
2430 ? make_aligning_type (data_type, data_align, data_size,
2431 system_allocator_alignment,
2432 POINTER_SIZE / BITS_PER_UNIT,
2433 gnat_node)
2434 : NULL_TREE);
2435
2436 tree size_to_malloc
2437 = aligning_type ? TYPE_SIZE_UNIT (aligning_type) : data_size;
2438
2439 tree malloc_ptr = build_call_n_expr (malloc_decl, 1, size_to_malloc);
2440
2441 Check_Restriction_No_Dependence_On_System (Name_Memory, gnat_node);
2442
2443 if (aligning_type)
2444 {
2445 /* Latch malloc's return value and get a pointer to the aligning field
2446 first. */
2447 tree storage_ptr = gnat_protect_expr (malloc_ptr);
2448
2449 tree aligning_record_addr
2450 = convert (build_pointer_type (aligning_type), storage_ptr);
2451
2452 tree aligning_record
2453 = build_unary_op (INDIRECT_REF, NULL_TREE, aligning_record_addr);
2454
2455 tree aligning_field
2456 = build_component_ref (aligning_record, TYPE_FIELDS (aligning_type),
2457 false);
2458
2459 tree aligning_field_addr
2460 = build_unary_op (ADDR_EXPR, NULL_TREE, aligning_field);
2461
2462 /* Then arrange to store the allocator's return value ahead
2463 and return. */
2464 tree storage_ptr_slot_addr
2465 = build_binary_op (POINTER_PLUS_EXPR, ptr_type_node,
2466 convert (ptr_type_node, aligning_field_addr),
2467 size_int (-(HOST_WIDE_INT) POINTER_SIZE
2468 / BITS_PER_UNIT));
2469
2470 tree storage_ptr_slot
2471 = build_unary_op (INDIRECT_REF, NULL_TREE,
2472 convert (build_pointer_type (ptr_type_node),
2473 storage_ptr_slot_addr));
2474
2475 return
2476 build2 (COMPOUND_EXPR, TREE_TYPE (aligning_field_addr),
2477 build_binary_op (INIT_EXPR, NULL_TREE,
2478 storage_ptr_slot, storage_ptr),
2479 aligning_field_addr);
2480 }
2481 else
2482 return malloc_ptr;
2483 }
2484
2485 /* Helper for build_call_alloc_dealloc, to release a DATA_TYPE object
2486 designated by DATA_PTR using the __gnat_free entry point. */
2487
2488 static inline tree
2489 maybe_wrap_free (tree data_ptr, tree data_type, Node_Id gnat_node)
2490 {
2491 /* In the regular alignment case, we pass the data pointer straight to free.
2492 In the superaligned case, we need to retrieve the initial allocator
2493 return value, stored in front of the data block at allocation time. */
2494
2495 unsigned int data_align = TYPE_ALIGN (data_type);
2496 unsigned int system_allocator_alignment
2497 = get_target_system_allocator_alignment () * BITS_PER_UNIT;
2498
2499 tree free_ptr;
2500
2501 Check_Restriction_No_Dependence_On_System (Name_Memory, gnat_node);
2502
2503 if (data_align > system_allocator_alignment)
2504 {
2505 /* DATA_FRONT_PTR (void *)
2506 = (void *)DATA_PTR - (void *)sizeof (void *)) */
2507 tree data_front_ptr
2508 = build_binary_op
2509 (POINTER_PLUS_EXPR, ptr_type_node,
2510 convert (ptr_type_node, data_ptr),
2511 size_int (-(HOST_WIDE_INT) POINTER_SIZE / BITS_PER_UNIT));
2512
2513 /* FREE_PTR (void *) = *(void **)DATA_FRONT_PTR */
2514 free_ptr
2515 = build_unary_op
2516 (INDIRECT_REF, NULL_TREE,
2517 convert (build_pointer_type (ptr_type_node), data_front_ptr));
2518 }
2519 else
2520 free_ptr = data_ptr;
2521
2522 return build_call_n_expr (free_decl, 1, free_ptr);
2523 }
2524
2525 /* Build a GCC tree to call an allocation or deallocation function.
2526 If GNU_OBJ is nonzero, it is an object to deallocate. Otherwise,
2527 generate an allocation.
2528
2529 GNU_SIZE is the number of bytes to allocate and GNU_TYPE is the contained
2530 object type, used to determine the to-be-honored address alignment.
2531 GNAT_PROC, if present, is a procedure to call and GNAT_POOL is the storage
2532 pool to use. If not present, malloc and free are used. GNAT_NODE is used
2533 to provide an error location for restriction violation messages. */
2534
2535 tree
2536 build_call_alloc_dealloc (tree gnu_obj, tree gnu_size, tree gnu_type,
2537 Entity_Id gnat_proc, Entity_Id gnat_pool,
2538 Node_Id gnat_node)
2539 {
2540 /* Explicit proc to call ? This one is assumed to deal with the type
2541 alignment constraints. */
2542 if (Present (gnat_proc))
2543 return build_call_alloc_dealloc_proc (gnu_obj, gnu_size, gnu_type,
2544 gnat_proc, gnat_pool);
2545
2546 /* Otherwise, object to "free" or "malloc" with possible special processing
2547 for alignments stricter than what the default allocator honors. */
2548 else if (gnu_obj)
2549 return maybe_wrap_free (gnu_obj, gnu_type, gnat_node);
2550 else
2551 {
2552 /* Assert that we no longer can be called with this special pool. */
2553 gcc_assert (gnat_pool != -1);
2554
2555 /* Check that we aren't violating the associated restriction. */
2556 if (!(Nkind (gnat_node) == N_Allocator && Comes_From_Source (gnat_node)))
2557 {
2558 Check_No_Implicit_Heap_Alloc (gnat_node);
2559 if (Has_Task (Etype (gnat_node)))
2560 Check_No_Implicit_Task_Alloc (gnat_node);
2561 if (Has_Protected (Etype (gnat_node)))
2562 Check_No_Implicit_Protected_Alloc (gnat_node);
2563 }
2564 return maybe_wrap_malloc (gnu_size, gnu_type, gnat_node);
2565 }
2566 }
2567
2568 /* Build a GCC tree that corresponds to allocating an object of TYPE whose
2569 initial value is INIT, if INIT is nonzero. Convert the expression to
2570 RESULT_TYPE, which must be some pointer type, and return the result.
2571
2572 GNAT_PROC and GNAT_POOL optionally give the procedure to call and
2573 the storage pool to use. GNAT_NODE is used to provide an error
2574 location for restriction violation messages. If IGNORE_INIT_TYPE is
2575 true, ignore the type of INIT for the purpose of determining the size;
2576 this will cause the maximum size to be allocated if TYPE is of
2577 self-referential size. */
2578
2579 tree
2580 build_allocator (tree type, tree init, tree result_type, Entity_Id gnat_proc,
2581 Entity_Id gnat_pool, Node_Id gnat_node, bool ignore_init_type)
2582 {
2583 const bool pool_is_storage_model
2584 = Present (gnat_pool)
2585 && Has_Storage_Model_Type_Aspect (Etype (gnat_pool))
2586 && Present (Storage_Model_Copy_To (gnat_pool));
2587 tree size, storage, storage_deref, storage_init;
2588
2589 /* If the initializer, if present, is a NULL_EXPR, just return a new one. */
2590 if (init && TREE_CODE (init) == NULL_EXPR)
2591 return build1 (NULL_EXPR, result_type, TREE_OPERAND (init, 0));
2592
2593 /* If we are just annotating types, also return a NULL_EXPR. */
2594 else if (type_annotate_only)
2595 return build1 (NULL_EXPR, result_type,
2596 build_call_raise (CE_Range_Check_Failed, gnat_node,
2597 N_Raise_Constraint_Error));
2598
2599 /* If the initializer, if present, is a COND_EXPR, deal with each branch. */
2600 else if (init && TREE_CODE (init) == COND_EXPR)
2601 return build3 (COND_EXPR, result_type, TREE_OPERAND (init, 0),
2602 build_allocator (type, TREE_OPERAND (init, 1), result_type,
2603 gnat_proc, gnat_pool, gnat_node,
2604 ignore_init_type),
2605 build_allocator (type, TREE_OPERAND (init, 2), result_type,
2606 gnat_proc, gnat_pool, gnat_node,
2607 ignore_init_type));
2608
2609 /* If RESULT_TYPE is a fat or thin pointer, set SIZE to be the sum of the
2610 sizes of the object and its template. Allocate the whole thing and
2611 fill in the parts that are known. */
2612 else if (TYPE_IS_FAT_OR_THIN_POINTER_P (result_type))
2613 {
2614 tree storage_type
2615 = build_unc_object_type_from_ptr (result_type, type,
2616 get_identifier ("ALLOC"), false);
2617 tree template_type = TREE_TYPE (TYPE_FIELDS (storage_type));
2618 tree storage_ptr_type = build_pointer_type (storage_type);
2619 tree lhs, rhs;
2620
2621 size = TYPE_SIZE_UNIT (storage_type);
2622 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, init);
2623
2624 /* If the size overflows, pass -1 so Storage_Error will be raised. */
2625 if (TREE_CODE (size) == INTEGER_CST && !valid_constant_size_p (size))
2626 size = size_int (-1);
2627
2628 storage = build_call_alloc_dealloc (NULL_TREE, size, storage_type,
2629 gnat_proc, gnat_pool, gnat_node);
2630 storage = convert (storage_ptr_type, gnat_protect_expr (storage));
2631 storage_deref = build_unary_op (INDIRECT_REF, NULL_TREE, storage);
2632 TREE_THIS_NOTRAP (storage_deref) = 1;
2633
2634 /* If there is an initializing expression, then make a constructor for
2635 the entire object including the bounds and copy it into the object.
2636 If there is no initializing expression, just set the bounds. Note
2637 that, if we have a storage model, we need to copy the initializing
2638 expression separately from the bounds. */
2639 if (init && !pool_is_storage_model)
2640 {
2641 vec<constructor_elt, va_gc> *v;
2642 vec_alloc (v, 2);
2643
2644 CONSTRUCTOR_APPEND_ELT (v, TYPE_FIELDS (storage_type),
2645 build_template (template_type, type, init));
2646 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (TYPE_FIELDS (storage_type)),
2647 init);
2648
2649 lhs = storage_deref;
2650 rhs = gnat_build_constructor (storage_type, v);
2651 }
2652 else
2653 {
2654 lhs = build_component_ref (storage_deref, TYPE_FIELDS (storage_type),
2655 false);
2656 rhs = build_template (template_type, type, init);
2657 }
2658
2659 if (pool_is_storage_model)
2660 {
2661 storage_init = build_storage_model_store (gnat_pool, lhs, rhs);
2662 if (init)
2663 {
2664 start_stmt_group ();
2665 add_stmt (storage_init);
2666 lhs
2667 = build_component_ref (storage_deref,
2668 DECL_CHAIN (TYPE_FIELDS (storage_type)),
2669 false);
2670 rhs = init;
2671 size = TYPE_SIZE_UNIT (TREE_TYPE (lhs));
2672 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, init);
2673 tree t = build_storage_model_store (gnat_pool, lhs, rhs, size);
2674 add_stmt (t);
2675 storage_init = end_stmt_group ();
2676 }
2677 }
2678 else
2679 storage_init = build_binary_op (INIT_EXPR, NULL_TREE, lhs, rhs);
2680
2681 return build2 (COMPOUND_EXPR, result_type,
2682 storage_init, convert (result_type, storage));
2683 }
2684
2685 size = TYPE_SIZE_UNIT (type);
2686
2687 /* If we have an initializing expression, see if its size is simpler
2688 than the size from the type. */
2689 if (!ignore_init_type && init && TYPE_SIZE_UNIT (TREE_TYPE (init))
2690 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (init))) == INTEGER_CST
2691 || CONTAINS_PLACEHOLDER_P (size)))
2692 size = TYPE_SIZE_UNIT (TREE_TYPE (init));
2693
2694 /* If the size is still self-referential, reference the initializing
2695 expression, if it is present. If not, this must have been a call
2696 to allocate a library-level object, in which case we just use the
2697 maximum size. */
2698 if (!ignore_init_type && init)
2699 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, init);
2700 else if (CONTAINS_PLACEHOLDER_P (size))
2701 size = max_size (size, true);
2702
2703 /* If the size overflows, pass -1 so Storage_Error will be raised. */
2704 if (TREE_CODE (size) == INTEGER_CST && !valid_constant_size_p (size))
2705 size = size_int (-1);
2706
2707 storage = convert (result_type,
2708 build_call_alloc_dealloc (NULL_TREE, size, type,
2709 gnat_proc, gnat_pool,
2710 gnat_node));
2711
2712 /* If we have an initial value, protect the new address, assign the value
2713 and return the address with a COMPOUND_EXPR. */
2714 if (init)
2715 {
2716 storage = gnat_protect_expr (storage);
2717 storage_deref = build_unary_op (INDIRECT_REF, NULL_TREE, storage);
2718 TREE_THIS_NOTRAP (storage_deref) = 1;
2719 if (pool_is_storage_model)
2720 storage_init
2721 = build_storage_model_store (gnat_pool, storage_deref, init, size);
2722 else
2723 storage_init
2724 = build_binary_op (INIT_EXPR, NULL_TREE, storage_deref, init);
2725 return build2 (COMPOUND_EXPR, result_type, storage_init, storage);
2726 }
2727
2728 return storage;
2729 }
2730
2731 /* Build a call to a copy procedure of a storage model given by an object.
2732 DEST, SRC and SIZE are as for a call to memcpy. GNAT_SMO is the entity
2733 for the storage model object and COPY_TO says which procedure to use. */
2734
2735 static tree
2736 build_storage_model_copy (Entity_Id gnat_smo, tree dest, tree src, tree size,
2737 bool copy_to)
2738 {
2739 const Entity_Id gnat_copy_proc
2740 = copy_to
2741 ? Storage_Model_Copy_To (gnat_smo)
2742 : Storage_Model_Copy_From (gnat_smo);
2743 tree gnu_copy_proc = gnat_to_gnu (gnat_copy_proc);
2744 tree gnu_param_type_list = TYPE_ARG_TYPES (TREE_TYPE (gnu_copy_proc));
2745 tree t1 = TREE_VALUE (gnu_param_type_list);
2746 tree t2 = TREE_VALUE (TREE_CHAIN (gnu_param_type_list));
2747 tree t3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (gnu_param_type_list)));
2748 tree t4
2749 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (gnu_param_type_list))));
2750
2751 return
2752 build_call_n_expr (gnu_copy_proc,
2753 4,
2754 build_unary_op (ADDR_EXPR, t1, gnat_to_gnu (gnat_smo)),
2755 build_unary_op (ADDR_EXPR, t2, dest),
2756 build_unary_op (ADDR_EXPR, t3, src),
2757 convert (t4, size));
2758 }
2759
2760 /* Build a load of SRC using the storage model of GNAT_SMO. */
2761
2762 tree
2763 build_storage_model_load (Entity_Id gnat_smo, tree src)
2764 {
2765 tree ret = build2 (LOAD_EXPR, TREE_TYPE (src), src, NULL_TREE);
2766
2767 /* Unconstrained array references have no size so we need to store the
2768 storage object model for future processing by the machinery. */
2769 if (TREE_CODE (src) == UNCONSTRAINED_ARRAY_REF)
2770 TREE_OPERAND (ret, 1) = build_int_cst (integer_type_node, gnat_smo);
2771 else
2772 TREE_OPERAND (ret, 1) = build_storage_model_load (gnat_smo, src, src);
2773
2774 return ret;
2775 }
2776
2777 /* Build a load of SRC into DEST using the storage model of GNAT_SMO.
2778 If SIZE is specified, use it, otherwise use the size of SRC. */
2779
2780 tree
2781 build_storage_model_load (Entity_Id gnat_smo, tree dest, tree src, tree size)
2782 {
2783 gcc_assert (TREE_CODE (src) != LOAD_EXPR);
2784
2785 if (!size)
2786 {
2787 size = TYPE_SIZE_UNIT (TREE_TYPE (src));
2788 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, src);
2789 size = INSTANTIATE_LOAD_IN_EXPR (size, gnat_smo);
2790 }
2791
2792 return build_storage_model_copy (gnat_smo, dest, src, size, false);
2793 }
2794
2795 /* Build a store of SRC into DEST using the storage model of GNAT_SMO.
2796 If SIZE is specified, use it, otherwise use the size of DEST. */
2797
2798 tree
2799 build_storage_model_store (Entity_Id gnat_smo, tree dest, tree src, tree size)
2800 {
2801 gcc_assert (TREE_CODE (src) != LOAD_EXPR);
2802
2803 if (!size)
2804 {
2805 size = TYPE_SIZE_UNIT (TREE_TYPE (dest));
2806 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, dest);
2807 size = INSTANTIATE_LOAD_IN_EXPR (size, gnat_smo);
2808 }
2809
2810 return build_storage_model_copy (gnat_smo, dest, src, size, true);
2811 }
2812
2813 /* Given a tree EXP, instantiate occurrences of LOAD_EXPR in it and associate
2814 them with the storage model of GNAT_SMO. */
2815
2816 tree
2817 instantiate_load_in_expr (tree exp, Entity_Id gnat_smo)
2818 {
2819 const enum tree_code code = TREE_CODE (exp);
2820 tree type = TREE_TYPE (exp);
2821 tree op0, op1, op2, op3;
2822 tree new_tree;
2823
2824 /* We handle TREE_LIST and COMPONENT_REF separately. */
2825 if (code == TREE_LIST)
2826 {
2827 op0 = INSTANTIATE_LOAD_IN_EXPR (TREE_CHAIN (exp), gnat_smo);
2828 op1 = INSTANTIATE_LOAD_IN_EXPR (TREE_VALUE (exp), gnat_smo);
2829 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2830 return exp;
2831
2832 return tree_cons (TREE_PURPOSE (exp), op1, op0);
2833 }
2834 else if (code == COMPONENT_REF)
2835 {
2836 /* The field. */
2837 op1 = TREE_OPERAND (exp, 1);
2838
2839 /* If it is a discriminant or equivalent, a LOAD_EXPR is needed. */
2840 if (DECL_DISCRIMINANT_NUMBER (op1))
2841 return build_storage_model_load (gnat_smo, exp);
2842
2843 op0 = INSTANTIATE_LOAD_IN_EXPR (TREE_OPERAND (exp, 0), gnat_smo);
2844 if (op0 == TREE_OPERAND (exp, 0))
2845 return exp;
2846
2847 new_tree = fold_build3 (COMPONENT_REF, type, op0, op1, NULL_TREE);
2848 }
2849 else
2850 switch (TREE_CODE_CLASS (code))
2851 {
2852 case tcc_constant:
2853 case tcc_declaration:
2854 return exp;
2855
2856 case tcc_expression:
2857 if (code == LOAD_EXPR)
2858 return exp;
2859
2860 /* Fall through. */
2861
2862 case tcc_exceptional:
2863 case tcc_unary:
2864 case tcc_binary:
2865 case tcc_comparison:
2866 case tcc_reference:
2867 switch (TREE_CODE_LENGTH (code))
2868 {
2869 case 0:
2870 return exp;
2871
2872 case 1:
2873 op0 = INSTANTIATE_LOAD_IN_EXPR (TREE_OPERAND (exp, 0), gnat_smo);
2874 if (op0 == TREE_OPERAND (exp, 0))
2875 return exp;
2876
2877 new_tree = fold_build1 (code, type, op0);
2878 break;
2879
2880 case 2:
2881 op0 = INSTANTIATE_LOAD_IN_EXPR (TREE_OPERAND (exp, 0), gnat_smo);
2882 op1 = INSTANTIATE_LOAD_IN_EXPR (TREE_OPERAND (exp, 1), gnat_smo);
2883
2884 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2885 return exp;
2886
2887 new_tree = fold_build2 (code, type, op0, op1);
2888 break;
2889
2890 case 3:
2891 op0 = INSTANTIATE_LOAD_IN_EXPR (TREE_OPERAND (exp, 0), gnat_smo);
2892 op1 = INSTANTIATE_LOAD_IN_EXPR (TREE_OPERAND (exp, 1), gnat_smo);
2893 op2 = INSTANTIATE_LOAD_IN_EXPR (TREE_OPERAND (exp, 2), gnat_smo);
2894
2895 if (op0 == TREE_OPERAND (exp, 0)
2896 && op1 == TREE_OPERAND (exp, 1)
2897 && op2 == TREE_OPERAND (exp, 2))
2898 return exp;
2899
2900 new_tree = fold_build3 (code, type, op0, op1, op2);
2901 break;
2902
2903 case 4:
2904 op0 = INSTANTIATE_LOAD_IN_EXPR (TREE_OPERAND (exp, 0), gnat_smo);
2905 op1 = INSTANTIATE_LOAD_IN_EXPR (TREE_OPERAND (exp, 1), gnat_smo);
2906 op2 = INSTANTIATE_LOAD_IN_EXPR (TREE_OPERAND (exp, 2), gnat_smo);
2907 op3 = INSTANTIATE_LOAD_IN_EXPR (TREE_OPERAND (exp, 3), gnat_smo);
2908
2909 if (op0 == TREE_OPERAND (exp, 0)
2910 && op1 == TREE_OPERAND (exp, 1)
2911 && op2 == TREE_OPERAND (exp, 2)
2912 && op3 == TREE_OPERAND (exp, 3))
2913 return exp;
2914
2915 new_tree = fold (build4 (code, type, op0, op1, op2, op3));
2916 break;
2917
2918 default:
2919 gcc_unreachable ();
2920 }
2921 break;
2922
2923 case tcc_vl_exp:
2924 {
2925 gcc_assert (code == CALL_EXPR);
2926
2927 const int n = call_expr_nargs (exp);
2928 gcc_assert (n > 0);
2929 tree *argarray = XALLOCAVEC (tree, n);
2930 for (int i = 0; i < n; i++)
2931 argarray[i]
2932 = INSTANTIATE_LOAD_IN_EXPR (CALL_EXPR_ARG (exp, i), gnat_smo);
2933
2934 for (int i = 0; i < n; i++)
2935 if (argarray[i] != CALL_EXPR_ARG (exp, i))
2936 return build_call_array (type, CALL_EXPR_FN (exp), n, argarray);
2937
2938 return exp;
2939 }
2940
2941 default:
2942 gcc_unreachable ();
2943 }
2944
2945 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
2946
2947 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
2948 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
2949
2950 return new_tree;
2951 }
2952
2953 /* Given an array or slice reference, instantiate occurrences of LOAD_EXPR in
2954 it and associate them with the storage model of GNAT_SMO. */
2955
2956 void
2957 instantiate_load_in_array_ref (tree ref, Entity_Id gnat_smo)
2958 {
2959 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
2960 tree elem_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0)));
2961
2962 TREE_OPERAND (ref, 2)
2963 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), ref);
2964 TREE_OPERAND (ref, 2)
2965 = INSTANTIATE_LOAD_IN_EXPR (TREE_OPERAND (ref, 2), gnat_smo);
2966
2967 TREE_OPERAND (ref, 3)
2968 = size_binop (EXACT_DIV_EXPR,
2969 SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elem_type),
2970 ref),
2971 size_int (TYPE_ALIGN_UNIT (elem_type)));
2972 TREE_OPERAND (ref, 3)
2973 = INSTANTIATE_LOAD_IN_EXPR (TREE_OPERAND (ref, 3), gnat_smo);
2974 }
2975
2976 /* Indicate that we need to take the address of T and that it therefore
2977 should not be allocated in a register. Return true if successful. */
2978
2979 bool
2980 gnat_mark_addressable (tree t)
2981 {
2982 while (true)
2983 switch (TREE_CODE (t))
2984 {
2985 case ADDR_EXPR:
2986 case COMPONENT_REF:
2987 case ARRAY_REF:
2988 case ARRAY_RANGE_REF:
2989 case REALPART_EXPR:
2990 case IMAGPART_EXPR:
2991 case VIEW_CONVERT_EXPR:
2992 case NON_LVALUE_EXPR:
2993 CASE_CONVERT:
2994 t = TREE_OPERAND (t, 0);
2995 break;
2996
2997 case COMPOUND_EXPR:
2998 t = TREE_OPERAND (t, 1);
2999 break;
3000
3001 case CONSTRUCTOR:
3002 TREE_ADDRESSABLE (t) = 1;
3003 return true;
3004
3005 case VAR_DECL:
3006 case PARM_DECL:
3007 case RESULT_DECL:
3008 TREE_ADDRESSABLE (t) = 1;
3009 return true;
3010
3011 case FUNCTION_DECL:
3012 TREE_ADDRESSABLE (t) = 1;
3013 return true;
3014
3015 case CONST_DECL:
3016 return DECL_CONST_CORRESPONDING_VAR (t)
3017 && gnat_mark_addressable (DECL_CONST_CORRESPONDING_VAR (t));
3018
3019 default:
3020 return true;
3021 }
3022 }
3023
3024 /* Return true if EXP is a stable expression for the purpose of the functions
3025 below and, therefore, can be returned unmodified by them. We accept things
3026 that are actual constants or that have already been handled. */
3027
3028 static bool
3029 gnat_stable_expr_p (tree exp)
3030 {
3031 enum tree_code code = TREE_CODE (exp);
3032 return TREE_CONSTANT (exp) || code == NULL_EXPR || code == SAVE_EXPR;
3033 }
3034
3035 /* Save EXP for later use or reuse. This is equivalent to save_expr in tree.cc
3036 but we know how to handle our own nodes. */
3037
3038 tree
3039 gnat_save_expr (tree exp)
3040 {
3041 tree type = TREE_TYPE (exp);
3042 enum tree_code code = TREE_CODE (exp);
3043
3044 if (gnat_stable_expr_p (exp))
3045 return exp;
3046
3047 if (code == UNCONSTRAINED_ARRAY_REF)
3048 {
3049 tree t = build1 (code, type, gnat_save_expr (TREE_OPERAND (exp, 0)));
3050 TREE_READONLY (t) = TYPE_READONLY (type);
3051 return t;
3052 }
3053
3054 /* If this is a COMPONENT_REF of a fat pointer, save the entire fat pointer.
3055 This may be more efficient, but will also allow us to more easily find
3056 the match for the PLACEHOLDER_EXPR. */
3057 if (code == COMPONENT_REF
3058 && TYPE_IS_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
3059 return build3 (code, type, gnat_save_expr (TREE_OPERAND (exp, 0)),
3060 TREE_OPERAND (exp, 1), NULL_TREE);
3061
3062 return save_expr (exp);
3063 }
3064
3065 /* Protect EXP for immediate reuse. This is a variant of gnat_save_expr that
3066 is optimized under the assumption that EXP's value doesn't change before
3067 its subsequent reuse(s) except potentially through its reevaluation.
3068
3069 gnat_protect_expr guarantees that multiple evaluations of the expression
3070 will not generate multiple side effects, whereas gnat_save_expr further
3071 guarantees that all evaluations will yield the same result. */
3072
3073 tree
3074 gnat_protect_expr (tree exp)
3075 {
3076 tree type = TREE_TYPE (exp);
3077 enum tree_code code = TREE_CODE (exp);
3078
3079 if (gnat_stable_expr_p (exp))
3080 return exp;
3081
3082 /* If EXP has no side effects, we theoretically don't need to do anything.
3083 However, we may be recursively passed more and more complex expressions
3084 involving checks which will be reused multiple times and eventually be
3085 unshared for gimplification; in order to avoid a complexity explosion
3086 at that point, we protect any expressions more complex than a simple
3087 arithmetic expression. */
3088 if (!TREE_SIDE_EFFECTS (exp))
3089 {
3090 tree inner = skip_simple_arithmetic (exp);
3091 if (!EXPR_P (inner) || REFERENCE_CLASS_P (inner))
3092 return exp;
3093 }
3094
3095 /* If this is a conversion, protect what's inside the conversion. */
3096 if (code == NON_LVALUE_EXPR
3097 || CONVERT_EXPR_CODE_P (code)
3098 || code == VIEW_CONVERT_EXPR)
3099 return build1 (code, type, gnat_protect_expr (TREE_OPERAND (exp, 0)));
3100
3101 /* If we're indirectly referencing something, we only need to protect the
3102 address since the data itself can't change in these situations. */
3103 if (code == INDIRECT_REF || code == UNCONSTRAINED_ARRAY_REF)
3104 {
3105 tree t = build1 (code, type, gnat_protect_expr (TREE_OPERAND (exp, 0)));
3106 TREE_READONLY (t) = TYPE_READONLY (type);
3107 return t;
3108 }
3109
3110 /* Likewise if we're indirectly referencing part of something. */
3111 if (code == COMPONENT_REF
3112 && INDIRECT_REF_P (TREE_OPERAND (exp, 0)))
3113 return build3 (code, type, gnat_protect_expr (TREE_OPERAND (exp, 0)),
3114 TREE_OPERAND (exp, 1), NULL_TREE);
3115
3116 /* An atomic load is an INDIRECT_REF of its first argument, so apply the
3117 same transformation as in the INDIRECT_REF case above. */
3118 if (code == CALL_EXPR && call_is_atomic_load (exp))
3119 return build_call_expr (TREE_OPERAND (CALL_EXPR_FN (exp), 0), 2,
3120 gnat_protect_expr (CALL_EXPR_ARG (exp, 0)),
3121 CALL_EXPR_ARG (exp, 1));
3122
3123 /* If this is a COMPONENT_REF of a fat pointer, save the entire fat pointer.
3124 This may be more efficient, but will also allow us to more easily find
3125 the match for the PLACEHOLDER_EXPR. */
3126 if (code == COMPONENT_REF
3127 && TYPE_IS_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
3128 return build3 (code, type, gnat_protect_expr (TREE_OPERAND (exp, 0)),
3129 TREE_OPERAND (exp, 1), NULL_TREE);
3130
3131 /* If this is a fat pointer or a scalar, just make a SAVE_EXPR. Likewise
3132 for a CALL_EXPR as large objects are returned via invisible reference
3133 in most ABIs so the temporary will directly be filled by the callee. */
3134 if (TYPE_IS_FAT_POINTER_P (type)
3135 || !AGGREGATE_TYPE_P (type)
3136 || code == CALL_EXPR)
3137 return save_expr (exp);
3138
3139 /* Otherwise reference, protect the address and dereference. */
3140 return
3141 build_unary_op (INDIRECT_REF, type,
3142 save_expr (build_unary_op (ADDR_EXPR, NULL_TREE, exp)));
3143 }
3144
3145 /* This is equivalent to stabilize_reference_1 in tree.cc but we take an extra
3146 argument to force evaluation of everything. */
3147
3148 static tree
3149 gnat_stabilize_reference_1 (tree e, void *data)
3150 {
3151 const bool force = *(bool *)data;
3152 enum tree_code code = TREE_CODE (e);
3153 tree type = TREE_TYPE (e);
3154 tree result;
3155
3156 if (gnat_stable_expr_p (e))
3157 return e;
3158
3159 switch (TREE_CODE_CLASS (code))
3160 {
3161 case tcc_exceptional:
3162 case tcc_declaration:
3163 case tcc_comparison:
3164 case tcc_expression:
3165 case tcc_reference:
3166 case tcc_vl_exp:
3167 /* If this is a COMPONENT_REF of a fat pointer, save the entire
3168 fat pointer. This may be more efficient, but will also allow
3169 us to more easily find the match for the PLACEHOLDER_EXPR. */
3170 if (code == COMPONENT_REF
3171 && TYPE_IS_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (e, 0))))
3172 result
3173 = build3 (code, type,
3174 gnat_stabilize_reference_1 (TREE_OPERAND (e, 0), data),
3175 TREE_OPERAND (e, 1), NULL_TREE);
3176 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3177 so that it will only be evaluated once. */
3178 /* The tcc_reference and tcc_comparison classes could be handled as
3179 below, but it is generally faster to only evaluate them once. */
3180 else if (TREE_SIDE_EFFECTS (e) || force)
3181 return save_expr (e);
3182 else
3183 return e;
3184 break;
3185
3186 case tcc_binary:
3187 /* Recursively stabilize each operand. */
3188 result
3189 = build2 (code, type,
3190 gnat_stabilize_reference_1 (TREE_OPERAND (e, 0), data),
3191 gnat_stabilize_reference_1 (TREE_OPERAND (e, 1), data));
3192 break;
3193
3194 case tcc_unary:
3195 /* Recursively stabilize each operand. */
3196 result
3197 = build1 (code, type,
3198 gnat_stabilize_reference_1 (TREE_OPERAND (e, 0), data));
3199 break;
3200
3201 default:
3202 gcc_unreachable ();
3203 }
3204
3205 /* See gnat_rewrite_reference below for the rationale. */
3206 TREE_READONLY (result) = TREE_READONLY (e);
3207 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3208
3209 if (TREE_SIDE_EFFECTS (e))
3210 TREE_SIDE_EFFECTS (result) = 1;
3211
3212 return result;
3213 }
3214
3215 /* This is equivalent to stabilize_reference in tree.cc but we know how to
3216 handle our own nodes and we take extra arguments. FORCE says whether to
3217 force evaluation of everything in REF. INIT is set to the first arm of
3218 a COMPOUND_EXPR present in REF, if any. */
3219
3220 tree
3221 gnat_stabilize_reference (tree ref, bool force, tree *init)
3222 {
3223 return
3224 gnat_rewrite_reference (ref, gnat_stabilize_reference_1, &force, init);
3225 }
3226
3227 /* Rewrite reference REF and call FUNC on each expression within REF in the
3228 process. DATA is passed unmodified to FUNC. INIT is set to the first
3229 arm of a COMPOUND_EXPR present in REF, if any. */
3230
3231 tree
3232 gnat_rewrite_reference (tree ref, rewrite_fn func, void *data, tree *init)
3233 {
3234 tree type = TREE_TYPE (ref);
3235 enum tree_code code = TREE_CODE (ref);
3236 tree result;
3237
3238 switch (code)
3239 {
3240 case CONST_DECL:
3241 case VAR_DECL:
3242 case PARM_DECL:
3243 case RESULT_DECL:
3244 /* No action is needed in this case. */
3245 return ref;
3246
3247 CASE_CONVERT:
3248 case FLOAT_EXPR:
3249 case FIX_TRUNC_EXPR:
3250 case REALPART_EXPR:
3251 case IMAGPART_EXPR:
3252 case VIEW_CONVERT_EXPR:
3253 result
3254 = build1 (code, type,
3255 gnat_rewrite_reference (TREE_OPERAND (ref, 0), func, data,
3256 init));
3257 break;
3258
3259 case INDIRECT_REF:
3260 case UNCONSTRAINED_ARRAY_REF:
3261 result = build1 (code, type, func (TREE_OPERAND (ref, 0), data));
3262 break;
3263
3264 case COMPONENT_REF:
3265 result = build3 (COMPONENT_REF, type,
3266 gnat_rewrite_reference (TREE_OPERAND (ref, 0), func,
3267 data, init),
3268 TREE_OPERAND (ref, 1), NULL_TREE);
3269 break;
3270
3271 case BIT_FIELD_REF:
3272 result = build3 (BIT_FIELD_REF, type,
3273 gnat_rewrite_reference (TREE_OPERAND (ref, 0), func,
3274 data, init),
3275 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
3276 REF_REVERSE_STORAGE_ORDER (result) = REF_REVERSE_STORAGE_ORDER (ref);
3277 break;
3278
3279 case ARRAY_REF:
3280 case ARRAY_RANGE_REF:
3281 result
3282 = build4 (code, type,
3283 gnat_rewrite_reference (TREE_OPERAND (ref, 0), func, data,
3284 init),
3285 func (TREE_OPERAND (ref, 1), data),
3286 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3287 break;
3288
3289 case COMPOUND_EXPR:
3290 gcc_assert (!*init);
3291 *init = TREE_OPERAND (ref, 0);
3292 /* We expect only the pattern built in Call_to_gnu. */
3293 gcc_assert (DECL_P (TREE_OPERAND (ref, 1))
3294 || (TREE_CODE (TREE_OPERAND (ref, 1)) == COMPONENT_REF
3295 && DECL_P (TREE_OPERAND (TREE_OPERAND (ref, 1), 0))));
3296 return TREE_OPERAND (ref, 1);
3297
3298 case CALL_EXPR:
3299 {
3300 /* This can only be an atomic load. */
3301 gcc_assert (call_is_atomic_load (ref));
3302
3303 /* An atomic load is an INDIRECT_REF of its first argument. */
3304 tree t = CALL_EXPR_ARG (ref, 0);
3305 if (TREE_CODE (t) == NOP_EXPR)
3306 t = TREE_OPERAND (t, 0);
3307 if (TREE_CODE (t) == ADDR_EXPR)
3308 t = build1 (ADDR_EXPR, TREE_TYPE (t),
3309 gnat_rewrite_reference (TREE_OPERAND (t, 0), func, data,
3310 init));
3311 else
3312 t = func (t, data);
3313 t = fold_convert (TREE_TYPE (CALL_EXPR_ARG (ref, 0)), t);
3314
3315 result = build_call_expr (TREE_OPERAND (CALL_EXPR_FN (ref), 0), 2,
3316 t, CALL_EXPR_ARG (ref, 1));
3317 }
3318 break;
3319
3320 case ERROR_MARK:
3321 case NULL_EXPR:
3322 return ref;
3323
3324 default:
3325 gcc_unreachable ();
3326 }
3327
3328 /* TREE_READONLY and TREE_THIS_VOLATILE set on the initial expression may
3329 not be sustained across some paths, such as the one for INDIRECT_REF.
3330
3331 Special care should be taken regarding TREE_SIDE_EFFECTS, because some
3332 paths introduce side-effects where there was none initially (e.g. if a
3333 SAVE_EXPR is built) and we also want to keep track of that. */
3334 TREE_READONLY (result) = TREE_READONLY (ref);
3335 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3336
3337 if (TREE_SIDE_EFFECTS (ref))
3338 TREE_SIDE_EFFECTS (result) = 1;
3339
3340 if (code == INDIRECT_REF
3341 || code == UNCONSTRAINED_ARRAY_REF
3342 || code == ARRAY_REF
3343 || code == ARRAY_RANGE_REF)
3344 TREE_THIS_NOTRAP (result) = TREE_THIS_NOTRAP (ref);
3345
3346 return result;
3347 }
3348
3349 /* This is equivalent to get_inner_reference in expr.cc but it returns the
3350 ultimate containing object only if the reference (lvalue) is constant,
3351 i.e. if it doesn't depend on the context in which it is evaluated. */
3352
3353 tree
3354 get_inner_constant_reference (tree exp)
3355 {
3356 while (true)
3357 {
3358 switch (TREE_CODE (exp))
3359 {
3360 case BIT_FIELD_REF:
3361 break;
3362
3363 case COMPONENT_REF:
3364 if (!TREE_CONSTANT (DECL_FIELD_OFFSET (TREE_OPERAND (exp, 1))))
3365 return NULL_TREE;
3366 break;
3367
3368 case ARRAY_REF:
3369 case ARRAY_RANGE_REF:
3370 {
3371 tree array_type = TREE_TYPE (TREE_OPERAND (exp, 0));
3372 if (!TREE_CONSTANT (TREE_OPERAND (exp, 1))
3373 || !TREE_CONSTANT (TYPE_MIN_VALUE (TYPE_DOMAIN (array_type)))
3374 || !TREE_CONSTANT (TYPE_SIZE_UNIT (TREE_TYPE (array_type))))
3375 return NULL_TREE;
3376 }
3377 break;
3378
3379 case REALPART_EXPR:
3380 case IMAGPART_EXPR:
3381 case VIEW_CONVERT_EXPR:
3382 break;
3383
3384 default:
3385 goto done;
3386 }
3387
3388 exp = TREE_OPERAND (exp, 0);
3389 }
3390
3391 done:
3392 return exp;
3393 }
3394
3395 /* Return true if EXPR is the addition or the subtraction of a constant and,
3396 if so, set *ADD to the addend, *CST to the constant and *MINUS_P to true
3397 if this is a subtraction. */
3398
3399 bool
3400 is_simple_additive_expression (tree expr, tree *add, tree *cst, bool *minus_p)
3401 {
3402 /* Skip overflow checks. */
3403 if (TREE_CODE (expr) == COND_EXPR
3404 && TREE_CODE (COND_EXPR_THEN (expr)) == COMPOUND_EXPR
3405 && TREE_CODE (TREE_OPERAND (COND_EXPR_THEN (expr), 0)) == CALL_EXPR
3406 && get_callee_fndecl (TREE_OPERAND (COND_EXPR_THEN (expr), 0))
3407 == gnat_raise_decls[CE_Overflow_Check_Failed])
3408 expr = COND_EXPR_ELSE (expr);
3409
3410 if (TREE_CODE (expr) == PLUS_EXPR)
3411 {
3412 if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3413 {
3414 *add = TREE_OPERAND (expr, 1);
3415 *cst = TREE_OPERAND (expr, 0);
3416 *minus_p = false;
3417 return true;
3418 }
3419 else if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3420 {
3421 *add = TREE_OPERAND (expr, 0);
3422 *cst = TREE_OPERAND (expr, 1);
3423 *minus_p = false;
3424 return true;
3425 }
3426 }
3427 else if (TREE_CODE (expr) == MINUS_EXPR)
3428 {
3429 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3430 {
3431 *add = TREE_OPERAND (expr, 0);
3432 *cst = TREE_OPERAND (expr, 1);
3433 *minus_p = true;
3434 return true;
3435 }
3436 }
3437
3438 return false;
3439 }
3440
3441 /* If EXPR is an expression that is invariant in the current function, in the
3442 sense that it can be evaluated anywhere in the function and any number of
3443 times, return EXPR or an equivalent expression. Otherwise return NULL. */
3444
3445 tree
3446 gnat_invariant_expr (tree expr)
3447 {
3448 tree type = TREE_TYPE (expr);
3449 tree add, cst;
3450 bool minus_p;
3451
3452 expr = remove_conversions (expr, false);
3453
3454 /* Look through temporaries created to capture values. */
3455 while ((TREE_CODE (expr) == CONST_DECL
3456 || (VAR_P (expr) && TREE_READONLY (expr)))
3457 && decl_function_context (expr) == current_function_decl
3458 && DECL_INITIAL (expr))
3459 {
3460 expr = DECL_INITIAL (expr);
3461 /* Look into CONSTRUCTORs built to initialize padded types. */
3462 expr = maybe_padded_object (expr);
3463 expr = remove_conversions (expr, false);
3464 }
3465
3466 /* We are only interested in scalar types at the moment and, even if we may
3467 have gone through padding types in the above loop, we must be back to a
3468 scalar value at this point. */
3469 if (AGGREGATE_TYPE_P (TREE_TYPE (expr)))
3470 return NULL_TREE;
3471
3472 if (TREE_CONSTANT (expr))
3473 return fold_convert (type, expr);
3474
3475 /* Deal with aligning patterns. */
3476 if (TREE_CODE (expr) == BIT_AND_EXPR
3477 && TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3478 {
3479 tree op0 = gnat_invariant_expr (TREE_OPERAND (expr, 0));
3480 if (op0)
3481 return fold_build2 (BIT_AND_EXPR, type, op0, TREE_OPERAND (expr, 1));
3482 else
3483 return NULL_TREE;
3484 }
3485
3486 /* Deal with addition or subtraction of constants. */
3487 if (is_simple_additive_expression (expr, &add, &cst, &minus_p))
3488 {
3489 add = gnat_invariant_expr (add);
3490 if (add)
3491 return
3492 fold_build2 (minus_p ? MINUS_EXPR : PLUS_EXPR, type,
3493 fold_convert (type, add), fold_convert (type, cst));
3494 else
3495 return NULL_TREE;
3496 }
3497
3498 bool invariant_p = false;
3499 tree t = expr;
3500
3501 while (true)
3502 {
3503 switch (TREE_CODE (t))
3504 {
3505 case COMPONENT_REF:
3506 invariant_p |= DECL_INVARIANT_P (TREE_OPERAND (t, 1));
3507 break;
3508
3509 case ARRAY_REF:
3510 case ARRAY_RANGE_REF:
3511 {
3512 tree array_type = TREE_TYPE (TREE_OPERAND (t, 0));
3513 if (!TREE_CONSTANT (TREE_OPERAND (t, 1))
3514 || !TREE_CONSTANT (TYPE_MIN_VALUE (TYPE_DOMAIN (array_type)))
3515 || !TREE_CONSTANT (TYPE_SIZE_UNIT (TREE_TYPE (array_type))))
3516 return NULL_TREE;
3517 }
3518 break;
3519
3520 case BIT_FIELD_REF:
3521 case REALPART_EXPR:
3522 case IMAGPART_EXPR:
3523 case VIEW_CONVERT_EXPR:
3524 CASE_CONVERT:
3525 break;
3526
3527 case INDIRECT_REF:
3528 if ((!invariant_p && !TREE_READONLY (t)) || TREE_SIDE_EFFECTS (t))
3529 return NULL_TREE;
3530 invariant_p = false;
3531 break;
3532
3533 default:
3534 goto object;
3535 }
3536
3537 t = TREE_OPERAND (t, 0);
3538 }
3539
3540 object:
3541 if (TREE_SIDE_EFFECTS (t))
3542 return NULL_TREE;
3543
3544 if (TREE_CODE (t) == CONST_DECL
3545 && (DECL_EXTERNAL (t)
3546 || decl_function_context (t) != current_function_decl))
3547 return fold_convert (type, expr);
3548
3549 if (!invariant_p && !TREE_READONLY (t))
3550 return NULL_TREE;
3551
3552 if (TREE_CODE (t) == PARM_DECL)
3553 return fold_convert (type, expr);
3554
3555 if (VAR_P (t)
3556 && (DECL_EXTERNAL (t)
3557 || decl_function_context (t) != current_function_decl))
3558 return fold_convert (type, expr);
3559
3560 return NULL_TREE;
3561 }