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