Update change log
[platform/upstream/gcc48.git] / gcc / convert.c
1 /* Utility routines for data type conversion for GCC.
2    Copyright (C) 1987-2013 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20
21 /* These routines are somewhat language-independent utility function
22    intended to be called by the language-specific convert () functions.  */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "flags.h"
30 #include "convert.h"
31 #include "diagnostic-core.h"
32 #include "langhooks.h"
33
34 /* Convert EXPR to some pointer or reference type TYPE.
35    EXPR must be pointer, reference, integer, enumeral, or literal zero;
36    in other cases error is called.  */
37
38 tree
39 convert_to_pointer (tree type, tree expr)
40 {
41   location_t loc = EXPR_LOCATION (expr);
42   if (TREE_TYPE (expr) == type)
43     return expr;
44
45   switch (TREE_CODE (TREE_TYPE (expr)))
46     {
47     case POINTER_TYPE:
48     case REFERENCE_TYPE:
49       {
50         /* If the pointers point to different address spaces, conversion needs
51            to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR.  */
52         addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
53         addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
54
55         if (to_as == from_as)
56           return fold_build1_loc (loc, NOP_EXPR, type, expr);
57         else
58           return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
59       }
60
61     case INTEGER_TYPE:
62     case ENUMERAL_TYPE:
63     case BOOLEAN_TYPE:
64       {
65         /* If the input precision differs from the target pointer type
66            precision, first convert the input expression to an integer type of
67            the target precision.  Some targets, e.g. VMS, need several pointer
68            sizes to coexist so the latter isn't necessarily POINTER_SIZE.  */
69         unsigned int pprec = TYPE_PRECISION (type);
70         unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
71
72         if (eprec != pprec)
73           expr = fold_build1_loc (loc, NOP_EXPR,
74                               lang_hooks.types.type_for_size (pprec, 0),
75                               expr);
76       }
77
78       return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
79
80     default:
81       error ("cannot convert to a pointer type");
82       return convert_to_pointer (type, integer_zero_node);
83     }
84 }
85
86
87 /* Convert EXPR to some floating-point type TYPE.
88
89    EXPR must be float, fixed-point, integer, or enumeral;
90    in other cases error is called.  */
91
92 tree
93 convert_to_real (tree type, tree expr)
94 {
95   enum built_in_function fcode = builtin_mathfn_code (expr);
96   tree itype = TREE_TYPE (expr);
97
98   /* Disable until we figure out how to decide whether the functions are
99      present in runtime.  */
100   /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
101   if (optimize
102       && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
103           || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
104     {
105       switch (fcode)
106         {
107 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
108           CASE_MATHFN (COSH)
109           CASE_MATHFN (EXP)
110           CASE_MATHFN (EXP10)
111           CASE_MATHFN (EXP2)
112           CASE_MATHFN (EXPM1)
113           CASE_MATHFN (GAMMA)
114           CASE_MATHFN (J0)
115           CASE_MATHFN (J1)
116           CASE_MATHFN (LGAMMA)
117           CASE_MATHFN (POW10)
118           CASE_MATHFN (SINH)
119           CASE_MATHFN (TGAMMA)
120           CASE_MATHFN (Y0)
121           CASE_MATHFN (Y1)
122             /* The above functions may set errno differently with float
123                input or output so this transformation is not safe with
124                -fmath-errno.  */
125             if (flag_errno_math)
126               break;
127           CASE_MATHFN (ACOS)
128           CASE_MATHFN (ACOSH)
129           CASE_MATHFN (ASIN)
130           CASE_MATHFN (ASINH)
131           CASE_MATHFN (ATAN)
132           CASE_MATHFN (ATANH)
133           CASE_MATHFN (CBRT)
134           CASE_MATHFN (COS)
135           CASE_MATHFN (ERF)
136           CASE_MATHFN (ERFC)
137           CASE_MATHFN (FABS)
138           CASE_MATHFN (LOG)
139           CASE_MATHFN (LOG10)
140           CASE_MATHFN (LOG2)
141           CASE_MATHFN (LOG1P)
142           CASE_MATHFN (LOGB)
143           CASE_MATHFN (SIN)
144           CASE_MATHFN (SQRT)
145           CASE_MATHFN (TAN)
146           CASE_MATHFN (TANH)
147 #undef CASE_MATHFN
148             {
149               tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
150               tree newtype = type;
151
152               /* We have (outertype)sqrt((innertype)x).  Choose the wider mode from
153                  the both as the safe type for operation.  */
154               if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
155                 newtype = TREE_TYPE (arg0);
156
157               /* Be careful about integer to fp conversions.
158                  These may overflow still.  */
159               if (FLOAT_TYPE_P (TREE_TYPE (arg0))
160                   && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
161                   && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
162                       || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
163                 {
164                   tree fn = mathfn_built_in (newtype, fcode);
165
166                   if (fn)
167                   {
168                     tree arg = fold (convert_to_real (newtype, arg0));
169                     expr = build_call_expr (fn, 1, arg);
170                     if (newtype == type)
171                       return expr;
172                   }
173                 }
174             }
175         default:
176           break;
177         }
178     }
179   if (optimize
180       && (((fcode == BUILT_IN_FLOORL
181            || fcode == BUILT_IN_CEILL
182            || fcode == BUILT_IN_ROUNDL
183            || fcode == BUILT_IN_RINTL
184            || fcode == BUILT_IN_TRUNCL
185            || fcode == BUILT_IN_NEARBYINTL)
186           && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
187               || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
188           || ((fcode == BUILT_IN_FLOOR
189                || fcode == BUILT_IN_CEIL
190                || fcode == BUILT_IN_ROUND
191                || fcode == BUILT_IN_RINT
192                || fcode == BUILT_IN_TRUNC
193                || fcode == BUILT_IN_NEARBYINT)
194               && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
195     {
196       tree fn = mathfn_built_in (type, fcode);
197
198       if (fn)
199         {
200           tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
201
202           /* Make sure (type)arg0 is an extension, otherwise we could end up
203              changing (float)floor(double d) into floorf((float)d), which is
204              incorrect because (float)d uses round-to-nearest and can round
205              up to the next integer.  */
206           if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
207             return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
208         }
209     }
210
211   /* Propagate the cast into the operation.  */
212   if (itype != type && FLOAT_TYPE_P (type))
213     switch (TREE_CODE (expr))
214       {
215         /* Convert (float)-x into -(float)x.  This is safe for
216            round-to-nearest rounding mode.  */
217         case ABS_EXPR:
218         case NEGATE_EXPR:
219           if (!flag_rounding_math
220               && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
221             return build1 (TREE_CODE (expr), type,
222                            fold (convert_to_real (type,
223                                                   TREE_OPERAND (expr, 0))));
224           break;
225         /* Convert (outertype)((innertype0)a+(innertype1)b)
226            into ((newtype)a+(newtype)b) where newtype
227            is the widest mode from all of these.  */
228         case PLUS_EXPR:
229         case MINUS_EXPR:
230         case MULT_EXPR:
231         case RDIV_EXPR:
232            {
233              tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
234              tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
235
236              if (FLOAT_TYPE_P (TREE_TYPE (arg0))
237                  && FLOAT_TYPE_P (TREE_TYPE (arg1))
238                  && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
239                {
240                   tree newtype = type;
241
242                   if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
243                       || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
244                       || TYPE_MODE (type) == SDmode)
245                     newtype = dfloat32_type_node;
246                   if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
247                       || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
248                       || TYPE_MODE (type) == DDmode)
249                     newtype = dfloat64_type_node;
250                   if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
251                       || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
252                       || TYPE_MODE (type) == TDmode)
253                     newtype = dfloat128_type_node;
254                   if (newtype == dfloat32_type_node
255                       || newtype == dfloat64_type_node
256                       || newtype == dfloat128_type_node)
257                     {
258                       expr = build2 (TREE_CODE (expr), newtype,
259                                      fold (convert_to_real (newtype, arg0)),
260                                      fold (convert_to_real (newtype, arg1)));
261                       if (newtype == type)
262                         return expr;
263                       break;
264                     }
265
266                   if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
267                     newtype = TREE_TYPE (arg0);
268                   if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
269                     newtype = TREE_TYPE (arg1);
270                   /* Sometimes this transformation is safe (cannot
271                      change results through affecting double rounding
272                      cases) and sometimes it is not.  If NEWTYPE is
273                      wider than TYPE, e.g. (float)((long double)double
274                      + (long double)double) converted to
275                      (float)(double + double), the transformation is
276                      unsafe regardless of the details of the types
277                      involved; double rounding can arise if the result
278                      of NEWTYPE arithmetic is a NEWTYPE value half way
279                      between two representable TYPE values but the
280                      exact value is sufficiently different (in the
281                      right direction) for this difference to be
282                      visible in ITYPE arithmetic.  If NEWTYPE is the
283                      same as TYPE, however, the transformation may be
284                      safe depending on the types involved: it is safe
285                      if the ITYPE has strictly more than twice as many
286                      mantissa bits as TYPE, can represent infinities
287                      and NaNs if the TYPE can, and has sufficient
288                      exponent range for the product or ratio of two
289                      values representable in the TYPE to be within the
290                      range of normal values of ITYPE.  */
291                   if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
292                       && (flag_unsafe_math_optimizations
293                           || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
294                               && real_can_shorten_arithmetic (TYPE_MODE (itype),
295                                                               TYPE_MODE (type))
296                               && !excess_precision_type (newtype))))
297                     {
298                       expr = build2 (TREE_CODE (expr), newtype,
299                                      fold (convert_to_real (newtype, arg0)),
300                                      fold (convert_to_real (newtype, arg1)));
301                       if (newtype == type)
302                         return expr;
303                     }
304                }
305            }
306           break;
307         default:
308           break;
309       }
310
311   switch (TREE_CODE (TREE_TYPE (expr)))
312     {
313     case REAL_TYPE:
314       /* Ignore the conversion if we don't need to store intermediate
315          results and neither type is a decimal float.  */
316       return build1 ((flag_float_store
317                      || DECIMAL_FLOAT_TYPE_P (type)
318                      || DECIMAL_FLOAT_TYPE_P (itype))
319                      ? CONVERT_EXPR : NOP_EXPR, type, expr);
320
321     case INTEGER_TYPE:
322     case ENUMERAL_TYPE:
323     case BOOLEAN_TYPE:
324       return build1 (FLOAT_EXPR, type, expr);
325
326     case FIXED_POINT_TYPE:
327       return build1 (FIXED_CONVERT_EXPR, type, expr);
328
329     case COMPLEX_TYPE:
330       return convert (type,
331                       fold_build1 (REALPART_EXPR,
332                                    TREE_TYPE (TREE_TYPE (expr)), expr));
333
334     case POINTER_TYPE:
335     case REFERENCE_TYPE:
336       error ("pointer value used where a floating point value was expected");
337       return convert_to_real (type, integer_zero_node);
338
339     default:
340       error ("aggregate value used where a float was expected");
341       return convert_to_real (type, integer_zero_node);
342     }
343 }
344
345 /* Convert EXPR to some integer (or enum) type TYPE.
346
347    EXPR must be pointer, integer, discrete (enum, char, or bool), float,
348    fixed-point or vector; in other cases error is called.
349
350    The result of this is always supposed to be a newly created tree node
351    not in use in any existing structure.  */
352
353 tree
354 convert_to_integer (tree type, tree expr)
355 {
356   enum tree_code ex_form = TREE_CODE (expr);
357   tree intype = TREE_TYPE (expr);
358   unsigned int inprec = TYPE_PRECISION (intype);
359   unsigned int outprec = TYPE_PRECISION (type);
360
361   /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
362      be.  Consider `enum E = { a, b = (enum E) 3 };'.  */
363   if (!COMPLETE_TYPE_P (type))
364     {
365       error ("conversion to incomplete type");
366       return error_mark_node;
367     }
368
369   /* Convert e.g. (long)round(d) -> lround(d).  */
370   /* If we're converting to char, we may encounter differing behavior
371      between converting from double->char vs double->long->char.
372      We're in "undefined" territory but we prefer to be conservative,
373      so only proceed in "unsafe" math mode.  */
374   if (optimize
375       && (flag_unsafe_math_optimizations
376           || (long_integer_type_node
377               && outprec >= TYPE_PRECISION (long_integer_type_node))))
378     {
379       tree s_expr = strip_float_extensions (expr);
380       tree s_intype = TREE_TYPE (s_expr);
381       const enum built_in_function fcode = builtin_mathfn_code (s_expr);
382       tree fn = 0;
383
384       switch (fcode)
385         {
386         CASE_FLT_FN (BUILT_IN_CEIL):
387           /* Only convert in ISO C99 mode.  */
388           if (!TARGET_C99_FUNCTIONS)
389             break;
390           if (outprec < TYPE_PRECISION (integer_type_node)
391               || (outprec == TYPE_PRECISION (integer_type_node)
392                   && !TYPE_UNSIGNED (type)))
393             fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
394           else if (outprec == TYPE_PRECISION (long_integer_type_node)
395                    && !TYPE_UNSIGNED (type))
396             fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
397           else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
398                    && !TYPE_UNSIGNED (type))
399             fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
400           break;
401
402         CASE_FLT_FN (BUILT_IN_FLOOR):
403           /* Only convert in ISO C99 mode.  */
404           if (!TARGET_C99_FUNCTIONS)
405             break;
406           if (outprec < TYPE_PRECISION (integer_type_node)
407               || (outprec == TYPE_PRECISION (integer_type_node)
408                   && !TYPE_UNSIGNED (type)))
409             fn = mathfn_built_in (s_intype, BUILT_IN_IFLOOR);
410           else if (outprec == TYPE_PRECISION (long_integer_type_node)
411                    && !TYPE_UNSIGNED (type))
412             fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
413           else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
414                    && !TYPE_UNSIGNED (type))
415             fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
416           break;
417
418         CASE_FLT_FN (BUILT_IN_ROUND):
419           /* Only convert in ISO C99 mode.  */
420           if (!TARGET_C99_FUNCTIONS)
421             break;
422           if (outprec < TYPE_PRECISION (integer_type_node)
423               || (outprec == TYPE_PRECISION (integer_type_node)
424                   && !TYPE_UNSIGNED (type)))
425             fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
426           else if (outprec == TYPE_PRECISION (long_integer_type_node)
427                    && !TYPE_UNSIGNED (type))
428             fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
429           else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
430                    && !TYPE_UNSIGNED (type))
431             fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
432           break;
433
434         CASE_FLT_FN (BUILT_IN_NEARBYINT):
435           /* Only convert nearbyint* if we can ignore math exceptions.  */
436           if (flag_trapping_math)
437             break;
438           /* ... Fall through ...  */
439         CASE_FLT_FN (BUILT_IN_RINT):
440           /* Only convert in ISO C99 mode.  */
441           if (!TARGET_C99_FUNCTIONS)
442             break;
443           if (outprec < TYPE_PRECISION (integer_type_node)
444               || (outprec == TYPE_PRECISION (integer_type_node)
445                   && !TYPE_UNSIGNED (type)))
446             fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
447           else if (outprec == TYPE_PRECISION (long_integer_type_node)
448                    && !TYPE_UNSIGNED (type))
449             fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
450           else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
451                    && !TYPE_UNSIGNED (type))
452             fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
453           break;
454
455         CASE_FLT_FN (BUILT_IN_TRUNC):
456           return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
457
458         default:
459           break;
460         }
461
462       if (fn)
463         {
464           tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
465           return convert_to_integer (type, newexpr);
466         }
467     }
468
469   /* Convert (int)logb(d) -> ilogb(d).  */
470   if (optimize
471       && flag_unsafe_math_optimizations
472       && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
473       && integer_type_node
474       && (outprec > TYPE_PRECISION (integer_type_node)
475           || (outprec == TYPE_PRECISION (integer_type_node)
476               && !TYPE_UNSIGNED (type))))
477     {
478       tree s_expr = strip_float_extensions (expr);
479       tree s_intype = TREE_TYPE (s_expr);
480       const enum built_in_function fcode = builtin_mathfn_code (s_expr);
481       tree fn = 0;
482
483       switch (fcode)
484         {
485         CASE_FLT_FN (BUILT_IN_LOGB):
486           fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
487           break;
488
489         default:
490           break;
491         }
492
493       if (fn)
494         {
495           tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
496           return convert_to_integer (type, newexpr);
497         }
498     }
499
500   switch (TREE_CODE (intype))
501     {
502     case POINTER_TYPE:
503     case REFERENCE_TYPE:
504       if (integer_zerop (expr))
505         return build_int_cst (type, 0);
506
507       /* Convert to an unsigned integer of the correct width first, and from
508          there widen/truncate to the required type.  Some targets support the
509          coexistence of multiple valid pointer sizes, so fetch the one we need
510          from the type.  */
511       expr = fold_build1 (CONVERT_EXPR,
512                           lang_hooks.types.type_for_size
513                             (TYPE_PRECISION (intype), 0),
514                           expr);
515       return fold_convert (type, expr);
516
517     case INTEGER_TYPE:
518     case ENUMERAL_TYPE:
519     case BOOLEAN_TYPE:
520     case OFFSET_TYPE:
521       /* If this is a logical operation, which just returns 0 or 1, we can
522          change the type of the expression.  */
523
524       if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
525         {
526           expr = copy_node (expr);
527           TREE_TYPE (expr) = type;
528           return expr;
529         }
530
531       /* If we are widening the type, put in an explicit conversion.
532          Similarly if we are not changing the width.  After this, we know
533          we are truncating EXPR.  */
534
535       else if (outprec >= inprec)
536         {
537           enum tree_code code;
538
539           /* If the precision of the EXPR's type is K bits and the
540              destination mode has more bits, and the sign is changing,
541              it is not safe to use a NOP_EXPR.  For example, suppose
542              that EXPR's type is a 3-bit unsigned integer type, the
543              TYPE is a 3-bit signed integer type, and the machine mode
544              for the types is 8-bit QImode.  In that case, the
545              conversion necessitates an explicit sign-extension.  In
546              the signed-to-unsigned case the high-order bits have to
547              be cleared.  */
548           if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
549               && (TYPE_PRECISION (TREE_TYPE (expr))
550                   != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr)))))
551             code = CONVERT_EXPR;
552           else
553             code = NOP_EXPR;
554
555           return fold_build1 (code, type, expr);
556         }
557
558       /* If TYPE is an enumeral type or a type with a precision less
559          than the number of bits in its mode, do the conversion to the
560          type corresponding to its mode, then do a nop conversion
561          to TYPE.  */
562       else if (TREE_CODE (type) == ENUMERAL_TYPE
563                || outprec != GET_MODE_PRECISION (TYPE_MODE (type)))
564         return build1 (NOP_EXPR, type,
565                        convert (lang_hooks.types.type_for_mode
566                                 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
567                                 expr));
568
569       /* Here detect when we can distribute the truncation down past some
570          arithmetic.  For example, if adding two longs and converting to an
571          int, we can equally well convert both to ints and then add.
572          For the operations handled here, such truncation distribution
573          is always safe.
574          It is desirable in these cases:
575          1) when truncating down to full-word from a larger size
576          2) when truncating takes no work.
577          3) when at least one operand of the arithmetic has been extended
578          (as by C's default conversions).  In this case we need two conversions
579          if we do the arithmetic as already requested, so we might as well
580          truncate both and then combine.  Perhaps that way we need only one.
581
582          Note that in general we cannot do the arithmetic in a type
583          shorter than the desired result of conversion, even if the operands
584          are both extended from a shorter type, because they might overflow
585          if combined in that type.  The exceptions to this--the times when
586          two narrow values can be combined in their narrow type even to
587          make a wider result--are handled by "shorten" in build_binary_op.  */
588
589       switch (ex_form)
590         {
591         case RSHIFT_EXPR:
592           /* We can pass truncation down through right shifting
593              when the shift count is a nonpositive constant.  */
594           if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
595               && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
596             goto trunc1;
597           break;
598
599         case LSHIFT_EXPR:
600           /* We can pass truncation down through left shifting
601              when the shift count is a nonnegative constant and
602              the target type is unsigned.  */
603           if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
604               && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
605               && TYPE_UNSIGNED (type)
606               && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
607             {
608               /* If shift count is less than the width of the truncated type,
609                  really shift.  */
610               if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
611                 /* In this case, shifting is like multiplication.  */
612                 goto trunc1;
613               else
614                 {
615                   /* If it is >= that width, result is zero.
616                      Handling this with trunc1 would give the wrong result:
617                      (int) ((long long) a << 32) is well defined (as 0)
618                      but (int) a << 32 is undefined and would get a
619                      warning.  */
620
621                   tree t = build_int_cst (type, 0);
622
623                   /* If the original expression had side-effects, we must
624                      preserve it.  */
625                   if (TREE_SIDE_EFFECTS (expr))
626                     return build2 (COMPOUND_EXPR, type, expr, t);
627                   else
628                     return t;
629                 }
630             }
631           break;
632
633         case TRUNC_DIV_EXPR:
634           {
635             tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
636             tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
637
638             /* Don't distribute unless the output precision is at least as big
639                as the actual inputs and it has the same signedness.  */
640             if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
641                 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
642                 /* If signedness of arg0 and arg1 don't match,
643                    we can't necessarily find a type to compare them in.  */
644                 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
645                     == TYPE_UNSIGNED (TREE_TYPE (arg1)))
646                 /* Do not change the sign of the division.  */
647                 && (TYPE_UNSIGNED (TREE_TYPE (expr))
648                     == TYPE_UNSIGNED (TREE_TYPE (arg0)))
649                 /* Either require unsigned division or a division by
650                    a constant that is not -1.  */
651                 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
652                     || (TREE_CODE (arg1) == INTEGER_CST
653                         && !integer_all_onesp (arg1))))
654               goto trunc1;
655             break;
656           }
657
658         case MAX_EXPR:
659         case MIN_EXPR:
660         case MULT_EXPR:
661           {
662             tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
663             tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
664
665             /* Don't distribute unless the output precision is at least as big
666                as the actual inputs.  Otherwise, the comparison of the
667                truncated values will be wrong.  */
668             if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
669                 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
670                 /* If signedness of arg0 and arg1 don't match,
671                    we can't necessarily find a type to compare them in.  */
672                 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
673                     == TYPE_UNSIGNED (TREE_TYPE (arg1))))
674               goto trunc1;
675             break;
676           }
677
678         case PLUS_EXPR:
679         case MINUS_EXPR:
680         case BIT_AND_EXPR:
681         case BIT_IOR_EXPR:
682         case BIT_XOR_EXPR:
683         trunc1:
684           {
685             tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
686             tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
687
688             /* Do not try to narrow operands of pointer subtraction;
689                that will interfere with other folding.  */
690             if (ex_form == MINUS_EXPR
691                 && CONVERT_EXPR_P (arg0)
692                 && CONVERT_EXPR_P (arg1)
693                 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
694                 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
695               break;
696
697             if (outprec >= BITS_PER_WORD
698                 || TRULY_NOOP_TRUNCATION (outprec, inprec)
699                 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
700                 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
701               {
702                 /* Do the arithmetic in type TYPEX,
703                    then convert result to TYPE.  */
704                 tree typex = type;
705
706                 /* Can't do arithmetic in enumeral types
707                    so use an integer type that will hold the values.  */
708                 if (TREE_CODE (typex) == ENUMERAL_TYPE)
709                   typex = lang_hooks.types.type_for_size
710                     (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
711
712                 /* But now perhaps TYPEX is as wide as INPREC.
713                    In that case, do nothing special here.
714                    (Otherwise would recurse infinitely in convert.  */
715                 if (TYPE_PRECISION (typex) != inprec)
716                   {
717                     /* Don't do unsigned arithmetic where signed was wanted,
718                        or vice versa.
719                        Exception: if both of the original operands were
720                        unsigned then we can safely do the work as unsigned.
721                        Exception: shift operations take their type solely
722                        from the first argument.
723                        Exception: the LSHIFT_EXPR case above requires that
724                        we perform this operation unsigned lest we produce
725                        signed-overflow undefinedness.
726                        And we may need to do it as unsigned
727                        if we truncate to the original size.  */
728                     if (TYPE_UNSIGNED (TREE_TYPE (expr))
729                         || (TYPE_UNSIGNED (TREE_TYPE (arg0))
730                             && (TYPE_UNSIGNED (TREE_TYPE (arg1))
731                                 || ex_form == LSHIFT_EXPR
732                                 || ex_form == RSHIFT_EXPR
733                                 || ex_form == LROTATE_EXPR
734                                 || ex_form == RROTATE_EXPR))
735                         || ex_form == LSHIFT_EXPR
736                         /* If we have !flag_wrapv, and either ARG0 or
737                            ARG1 is of a signed type, we have to do
738                            PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
739                            type in case the operation in outprec precision
740                            could overflow.  Otherwise, we would introduce
741                            signed-overflow undefinedness.  */
742                         || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
743                              || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
744                             && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
745                                  > outprec)
746                                 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
747                                     > outprec))
748                             && (ex_form == PLUS_EXPR
749                                 || ex_form == MINUS_EXPR
750                                 || ex_form == MULT_EXPR)))
751                       typex = unsigned_type_for (typex);
752                     else
753                       typex = signed_type_for (typex);
754                     return convert (type,
755                                     fold_build2 (ex_form, typex,
756                                                  convert (typex, arg0),
757                                                  convert (typex, arg1)));
758                   }
759               }
760           }
761           break;
762
763         case NEGATE_EXPR:
764         case BIT_NOT_EXPR:
765           /* This is not correct for ABS_EXPR,
766              since we must test the sign before truncation.  */
767           {
768             tree typex = unsigned_type_for (type);
769             return convert (type,
770                             fold_build1 (ex_form, typex,
771                                          convert (typex,
772                                                   TREE_OPERAND (expr, 0))));
773           }
774
775         case NOP_EXPR:
776           /* Don't introduce a
777              "can't convert between vector values of different size" error.  */
778           if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
779               && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
780                   != GET_MODE_SIZE (TYPE_MODE (type))))
781             break;
782           /* If truncating after truncating, might as well do all at once.
783              If truncating after extending, we may get rid of wasted work.  */
784           return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
785
786         case COND_EXPR:
787           /* It is sometimes worthwhile to push the narrowing down through
788              the conditional and never loses.  A COND_EXPR may have a throw
789              as one operand, which then has void type.  Just leave void
790              operands as they are.  */
791           return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
792                               VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
793                               ? TREE_OPERAND (expr, 1)
794                               : convert (type, TREE_OPERAND (expr, 1)),
795                               VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
796                               ? TREE_OPERAND (expr, 2)
797                               : convert (type, TREE_OPERAND (expr, 2)));
798
799         default:
800           break;
801         }
802
803       /* When parsing long initializers, we might end up with a lot of casts.
804          Shortcut this.  */
805       if (TREE_CODE (expr) == INTEGER_CST)
806         return fold_convert (type, expr);
807       return build1 (CONVERT_EXPR, type, expr);
808
809     case REAL_TYPE:
810       return build1 (FIX_TRUNC_EXPR, type, expr);
811
812     case FIXED_POINT_TYPE:
813       return build1 (FIXED_CONVERT_EXPR, type, expr);
814
815     case COMPLEX_TYPE:
816       return convert (type,
817                       fold_build1 (REALPART_EXPR,
818                                    TREE_TYPE (TREE_TYPE (expr)), expr));
819
820     case VECTOR_TYPE:
821       if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
822         {
823           error ("can%'t convert between vector values of different size");
824           return error_mark_node;
825         }
826       return build1 (VIEW_CONVERT_EXPR, type, expr);
827
828     default:
829       error ("aggregate value used where an integer was expected");
830       return convert (type, integer_zero_node);
831     }
832 }
833
834 /* Convert EXPR to the complex type TYPE in the usual ways.  */
835
836 tree
837 convert_to_complex (tree type, tree expr)
838 {
839   tree subtype = TREE_TYPE (type);
840
841   switch (TREE_CODE (TREE_TYPE (expr)))
842     {
843     case REAL_TYPE:
844     case FIXED_POINT_TYPE:
845     case INTEGER_TYPE:
846     case ENUMERAL_TYPE:
847     case BOOLEAN_TYPE:
848       return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
849                      convert (subtype, integer_zero_node));
850
851     case COMPLEX_TYPE:
852       {
853         tree elt_type = TREE_TYPE (TREE_TYPE (expr));
854
855         if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
856           return expr;
857         else if (TREE_CODE (expr) == COMPLEX_EXPR)
858           return fold_build2 (COMPLEX_EXPR, type,
859                               convert (subtype, TREE_OPERAND (expr, 0)),
860                               convert (subtype, TREE_OPERAND (expr, 1)));
861         else
862           {
863             expr = save_expr (expr);
864             return
865               fold_build2 (COMPLEX_EXPR, type,
866                            convert (subtype,
867                                     fold_build1 (REALPART_EXPR,
868                                                  TREE_TYPE (TREE_TYPE (expr)),
869                                                  expr)),
870                            convert (subtype,
871                                     fold_build1 (IMAGPART_EXPR,
872                                                  TREE_TYPE (TREE_TYPE (expr)),
873                                                  expr)));
874           }
875       }
876
877     case POINTER_TYPE:
878     case REFERENCE_TYPE:
879       error ("pointer value used where a complex was expected");
880       return convert_to_complex (type, integer_zero_node);
881
882     default:
883       error ("aggregate value used where a complex was expected");
884       return convert_to_complex (type, integer_zero_node);
885     }
886 }
887
888 /* Convert EXPR to the vector type TYPE in the usual ways.  */
889
890 tree
891 convert_to_vector (tree type, tree expr)
892 {
893   switch (TREE_CODE (TREE_TYPE (expr)))
894     {
895     case INTEGER_TYPE:
896     case VECTOR_TYPE:
897       if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
898         {
899           error ("can%'t convert between vector values of different size");
900           return error_mark_node;
901         }
902       return build1 (VIEW_CONVERT_EXPR, type, expr);
903
904     default:
905       error ("can%'t convert value to a vector");
906       return error_mark_node;
907     }
908 }
909
910 /* Convert EXPR to some fixed-point type TYPE.
911
912    EXPR must be fixed-point, float, integer, or enumeral;
913    in other cases error is called.  */
914
915 tree
916 convert_to_fixed (tree type, tree expr)
917 {
918   if (integer_zerop (expr))
919     {
920       tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
921       return fixed_zero_node;
922     }
923   else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
924     {
925       tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
926       return fixed_one_node;
927     }
928
929   switch (TREE_CODE (TREE_TYPE (expr)))
930     {
931     case FIXED_POINT_TYPE:
932     case INTEGER_TYPE:
933     case ENUMERAL_TYPE:
934     case BOOLEAN_TYPE:
935     case REAL_TYPE:
936       return build1 (FIXED_CONVERT_EXPR, type, expr);
937
938     case COMPLEX_TYPE:
939       return convert (type,
940                       fold_build1 (REALPART_EXPR,
941                                    TREE_TYPE (TREE_TYPE (expr)), expr));
942
943     default:
944       error ("aggregate value used where a fixed-point was expected");
945       return error_mark_node;
946     }
947 }