99ffbb7833d36ca0b0976e4de8ddde492df3c57d
[platform/upstream/gcc.git] / gcc / machmode.h
1 /* Machine mode definitions for GCC; included by rtl.h and tree.h.
2    Copyright (C) 1991-2018 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 #ifndef HAVE_MACHINE_MODES
21 #define HAVE_MACHINE_MODES
22
23 typedef opt_mode<machine_mode> opt_machine_mode;
24
25 extern CONST_MODE_SIZE unsigned short mode_size[NUM_MACHINE_MODES];
26 extern const poly_uint16_pod mode_precision[NUM_MACHINE_MODES];
27 extern const unsigned char mode_inner[NUM_MACHINE_MODES];
28 extern const poly_uint16_pod mode_nunits[NUM_MACHINE_MODES];
29 extern CONST_MODE_UNIT_SIZE unsigned char mode_unit_size[NUM_MACHINE_MODES];
30 extern const unsigned short mode_unit_precision[NUM_MACHINE_MODES];
31 extern const unsigned char mode_wider[NUM_MACHINE_MODES];
32 extern const unsigned char mode_2xwider[NUM_MACHINE_MODES];
33
34 template<typename T>
35 struct mode_traits
36 {
37   /* For use by the machmode support code only.
38
39      There are cases in which the machmode support code needs to forcibly
40      convert a machine_mode to a specific mode class T, and in which the
41      context guarantees that this is valid without the need for an assert.
42      This can be done using:
43
44        return typename mode_traits<T>::from_int (mode);
45
46      when returning a T and:
47
48        res = T (typename mode_traits<T>::from_int (mode));
49
50      when assigning to a value RES that must be assignment-compatible
51      with (but possibly not the same as) T.  */
52 #ifdef USE_ENUM_MODES
53   /* Allow direct conversion of enums to specific mode classes only
54      when USE_ENUM_MODES is defined.  This is only intended for use
55      by gencondmd, so that it can tell more easily when .md conditions
56      are always false.  */
57   typedef machine_mode from_int;
58 #else
59   /* Here we use an enum type distinct from machine_mode but with the
60      same range as machine_mode.  T should have a constructor that
61      accepts this enum type; it should not have a constructor that
62      accepts machine_mode.
63
64      We use this somewhat indirect approach to avoid too many constructor
65      calls when the compiler is built with -O0.  For example, even in
66      unoptimized code, the return statement above would construct the
67      returned T directly from the numerical value of MODE.  */
68   enum from_int { dummy = MAX_MACHINE_MODE };
69 #endif
70 };
71
72 template<>
73 struct mode_traits<machine_mode>
74 {
75   /* machine_mode itself needs no conversion.  */
76   typedef machine_mode from_int;
77 };
78
79 /* Always treat machine modes as fixed-size while compiling code specific
80    to targets that have no variable-size modes.  */
81 #if defined (IN_TARGET_CODE) && NUM_POLY_INT_COEFFS == 1
82 #define ONLY_FIXED_SIZE_MODES 1
83 #else
84 #define ONLY_FIXED_SIZE_MODES 0
85 #endif
86
87 /* Get the name of mode MODE as a string.  */
88
89 extern const char * const mode_name[NUM_MACHINE_MODES];
90 #define GET_MODE_NAME(MODE)  mode_name[MODE]
91
92 /* Mode classes.  */
93
94 #include "mode-classes.def"
95 #define DEF_MODE_CLASS(M) M
96 enum mode_class { MODE_CLASSES, MAX_MODE_CLASS };
97 #undef DEF_MODE_CLASS
98 #undef MODE_CLASSES
99
100 /* Get the general kind of object that mode MODE represents
101    (integer, floating, complex, etc.)  */
102
103 extern const unsigned char mode_class[NUM_MACHINE_MODES];
104 #define GET_MODE_CLASS(MODE)  ((enum mode_class) mode_class[MODE])
105
106 /* Nonzero if MODE is an integral mode.  */
107 #define INTEGRAL_MODE_P(MODE)                   \
108   (GET_MODE_CLASS (MODE) == MODE_INT            \
109    || GET_MODE_CLASS (MODE) == MODE_PARTIAL_INT \
110    || GET_MODE_CLASS (MODE) == MODE_COMPLEX_INT \
111    || GET_MODE_CLASS (MODE) == MODE_VECTOR_INT)
112
113 /* Nonzero if MODE is a floating-point mode.  */
114 #define FLOAT_MODE_P(MODE)              \
115   (GET_MODE_CLASS (MODE) == MODE_FLOAT  \
116    || GET_MODE_CLASS (MODE) == MODE_DECIMAL_FLOAT \
117    || GET_MODE_CLASS (MODE) == MODE_COMPLEX_FLOAT \
118    || GET_MODE_CLASS (MODE) == MODE_VECTOR_FLOAT)
119
120 /* Nonzero if MODE is a complex mode.  */
121 #define COMPLEX_MODE_P(MODE)                    \
122   (GET_MODE_CLASS (MODE) == MODE_COMPLEX_INT    \
123    || GET_MODE_CLASS (MODE) == MODE_COMPLEX_FLOAT)
124
125 /* Nonzero if MODE is a vector mode.  */
126 #define VECTOR_MODE_P(MODE)                     \
127   (GET_MODE_CLASS (MODE) == MODE_VECTOR_INT     \
128    || GET_MODE_CLASS (MODE) == MODE_VECTOR_FLOAT        \
129    || GET_MODE_CLASS (MODE) == MODE_VECTOR_FRACT        \
130    || GET_MODE_CLASS (MODE) == MODE_VECTOR_UFRACT       \
131    || GET_MODE_CLASS (MODE) == MODE_VECTOR_ACCUM        \
132    || GET_MODE_CLASS (MODE) == MODE_VECTOR_UACCUM)
133
134 /* Nonzero if MODE is a scalar integral mode.  */
135 #define SCALAR_INT_MODE_P(MODE)                 \
136   (GET_MODE_CLASS (MODE) == MODE_INT            \
137    || GET_MODE_CLASS (MODE) == MODE_PARTIAL_INT)
138
139 /* Nonzero if MODE is a scalar floating point mode.  */
140 #define SCALAR_FLOAT_MODE_P(MODE)               \
141   (GET_MODE_CLASS (MODE) == MODE_FLOAT          \
142    || GET_MODE_CLASS (MODE) == MODE_DECIMAL_FLOAT)
143
144 /* Nonzero if MODE is a decimal floating point mode.  */
145 #define DECIMAL_FLOAT_MODE_P(MODE)              \
146   (GET_MODE_CLASS (MODE) == MODE_DECIMAL_FLOAT)
147
148 /* Nonzero if MODE is a scalar fract mode.  */
149 #define SCALAR_FRACT_MODE_P(MODE)       \
150   (GET_MODE_CLASS (MODE) == MODE_FRACT)
151
152 /* Nonzero if MODE is a scalar ufract mode.  */
153 #define SCALAR_UFRACT_MODE_P(MODE)      \
154   (GET_MODE_CLASS (MODE) == MODE_UFRACT)
155
156 /* Nonzero if MODE is a scalar fract or ufract mode.  */
157 #define ALL_SCALAR_FRACT_MODE_P(MODE)   \
158   (SCALAR_FRACT_MODE_P (MODE) || SCALAR_UFRACT_MODE_P (MODE))
159
160 /* Nonzero if MODE is a scalar accum mode.  */
161 #define SCALAR_ACCUM_MODE_P(MODE)       \
162   (GET_MODE_CLASS (MODE) == MODE_ACCUM)
163
164 /* Nonzero if MODE is a scalar uaccum mode.  */
165 #define SCALAR_UACCUM_MODE_P(MODE)      \
166   (GET_MODE_CLASS (MODE) == MODE_UACCUM)
167
168 /* Nonzero if MODE is a scalar accum or uaccum mode.  */
169 #define ALL_SCALAR_ACCUM_MODE_P(MODE)   \
170   (SCALAR_ACCUM_MODE_P (MODE) || SCALAR_UACCUM_MODE_P (MODE))
171
172 /* Nonzero if MODE is a scalar fract or accum mode.  */
173 #define SIGNED_SCALAR_FIXED_POINT_MODE_P(MODE)  \
174   (SCALAR_FRACT_MODE_P (MODE) || SCALAR_ACCUM_MODE_P (MODE))
175
176 /* Nonzero if MODE is a scalar ufract or uaccum mode.  */
177 #define UNSIGNED_SCALAR_FIXED_POINT_MODE_P(MODE)        \
178   (SCALAR_UFRACT_MODE_P (MODE) || SCALAR_UACCUM_MODE_P (MODE))
179
180 /* Nonzero if MODE is a scalar fract, ufract, accum or uaccum mode.  */
181 #define ALL_SCALAR_FIXED_POINT_MODE_P(MODE)     \
182   (SIGNED_SCALAR_FIXED_POINT_MODE_P (MODE)      \
183    || UNSIGNED_SCALAR_FIXED_POINT_MODE_P (MODE))
184
185 /* Nonzero if MODE is a scalar/vector fract mode.  */
186 #define FRACT_MODE_P(MODE)              \
187   (GET_MODE_CLASS (MODE) == MODE_FRACT  \
188    || GET_MODE_CLASS (MODE) == MODE_VECTOR_FRACT)
189
190 /* Nonzero if MODE is a scalar/vector ufract mode.  */
191 #define UFRACT_MODE_P(MODE)             \
192   (GET_MODE_CLASS (MODE) == MODE_UFRACT \
193    || GET_MODE_CLASS (MODE) == MODE_VECTOR_UFRACT)
194
195 /* Nonzero if MODE is a scalar/vector fract or ufract mode.  */
196 #define ALL_FRACT_MODE_P(MODE)          \
197   (FRACT_MODE_P (MODE) || UFRACT_MODE_P (MODE))
198
199 /* Nonzero if MODE is a scalar/vector accum mode.  */
200 #define ACCUM_MODE_P(MODE)              \
201   (GET_MODE_CLASS (MODE) == MODE_ACCUM  \
202    || GET_MODE_CLASS (MODE) == MODE_VECTOR_ACCUM)
203
204 /* Nonzero if MODE is a scalar/vector uaccum mode.  */
205 #define UACCUM_MODE_P(MODE)             \
206   (GET_MODE_CLASS (MODE) == MODE_UACCUM \
207    || GET_MODE_CLASS (MODE) == MODE_VECTOR_UACCUM)
208
209 /* Nonzero if MODE is a scalar/vector accum or uaccum mode.  */
210 #define ALL_ACCUM_MODE_P(MODE)          \
211   (ACCUM_MODE_P (MODE) || UACCUM_MODE_P (MODE))
212
213 /* Nonzero if MODE is a scalar/vector fract or accum mode.  */
214 #define SIGNED_FIXED_POINT_MODE_P(MODE)         \
215   (FRACT_MODE_P (MODE) || ACCUM_MODE_P (MODE))
216
217 /* Nonzero if MODE is a scalar/vector ufract or uaccum mode.  */
218 #define UNSIGNED_FIXED_POINT_MODE_P(MODE)       \
219   (UFRACT_MODE_P (MODE) || UACCUM_MODE_P (MODE))
220
221 /* Nonzero if MODE is a scalar/vector fract, ufract, accum or uaccum mode.  */
222 #define ALL_FIXED_POINT_MODE_P(MODE)            \
223   (SIGNED_FIXED_POINT_MODE_P (MODE)             \
224    || UNSIGNED_FIXED_POINT_MODE_P (MODE))
225
226 /* Nonzero if CLASS modes can be widened.  */
227 #define CLASS_HAS_WIDER_MODES_P(CLASS)         \
228   (CLASS == MODE_INT                           \
229    || CLASS == MODE_PARTIAL_INT                \
230    || CLASS == MODE_FLOAT                      \
231    || CLASS == MODE_DECIMAL_FLOAT              \
232    || CLASS == MODE_COMPLEX_FLOAT              \
233    || CLASS == MODE_FRACT                      \
234    || CLASS == MODE_UFRACT                     \
235    || CLASS == MODE_ACCUM                      \
236    || CLASS == MODE_UACCUM)
237
238 #define POINTER_BOUNDS_MODE_P(MODE)      \
239   (GET_MODE_CLASS (MODE) == MODE_POINTER_BOUNDS)
240
241 /* An optional T (i.e. a T or nothing), where T is some form of mode class.  */
242 template<typename T>
243 class opt_mode
244 {
245 public:
246   enum from_int { dummy = MAX_MACHINE_MODE };
247
248   ALWAYS_INLINE opt_mode () : m_mode (E_VOIDmode) {}
249   ALWAYS_INLINE opt_mode (const T &m) : m_mode (m) {}
250   template<typename U>
251   ALWAYS_INLINE opt_mode (const U &m) : m_mode (T (m)) {}
252   ALWAYS_INLINE opt_mode (from_int m) : m_mode (machine_mode (m)) {}
253
254   machine_mode else_void () const;
255   machine_mode else_blk () const;
256   T require () const;
257
258   bool exists () const;
259   template<typename U> bool exists (U *) const;
260
261 private:
262   machine_mode m_mode;
263 };
264
265 /* If the object contains a T, return its enum value, otherwise return
266    E_VOIDmode.  */
267
268 template<typename T>
269 ALWAYS_INLINE machine_mode
270 opt_mode<T>::else_void () const
271 {
272   return m_mode;
273 }
274
275 /* If the T exists, return its enum value, otherwise return E_BLKmode.  */
276
277 template<typename T>
278 inline machine_mode
279 opt_mode<T>::else_blk () const
280 {
281   return m_mode == E_VOIDmode ? E_BLKmode : m_mode;
282 }
283
284 /* Assert that the object contains a T and return it.  */
285
286 template<typename T>
287 inline T
288 opt_mode<T>::require () const
289 {
290   gcc_checking_assert (m_mode != E_VOIDmode);
291   return typename mode_traits<T>::from_int (m_mode);
292 }
293
294 /* Return true if the object contains a T rather than nothing.  */
295
296 template<typename T>
297 ALWAYS_INLINE bool
298 opt_mode<T>::exists () const
299 {
300   return m_mode != E_VOIDmode;
301 }
302
303 /* Return true if the object contains a T, storing it in *MODE if so.  */
304
305 template<typename T>
306 template<typename U>
307 inline bool
308 opt_mode<T>::exists (U *mode) const
309 {
310   if (m_mode != E_VOIDmode)
311     {
312       *mode = T (typename mode_traits<T>::from_int (m_mode));
313       return true;
314     }
315   return false;
316 }
317
318 /* A POD version of mode class T.  */
319
320 template<typename T>
321 struct pod_mode
322 {
323   typedef typename mode_traits<T>::from_int from_int;
324   typedef typename T::measurement_type measurement_type;
325
326   machine_mode m_mode;
327   ALWAYS_INLINE operator machine_mode () const { return m_mode; }
328   ALWAYS_INLINE operator T () const { return from_int (m_mode); }
329   ALWAYS_INLINE pod_mode &operator = (const T &m) { m_mode = m; return *this; }
330 };
331
332 /* Return true if mode M has type T.  */
333
334 template<typename T>
335 inline bool
336 is_a (machine_mode m)
337 {
338   return T::includes_p (m);
339 }
340
341 template<typename T, typename U>
342 inline bool
343 is_a (const opt_mode<U> &m)
344 {
345   return T::includes_p (m.else_void ());
346 }
347
348 /* Assert that mode M has type T, and return it in that form.  */
349
350 template<typename T>
351 inline T
352 as_a (machine_mode m)
353 {
354   gcc_checking_assert (T::includes_p (m));
355   return typename mode_traits<T>::from_int (m);
356 }
357
358 template<typename T, typename U>
359 inline T
360 as_a (const opt_mode<U> &m)
361 {
362   return as_a <T> (m.else_void ());
363 }
364
365 /* Convert M to an opt_mode<T>.  */
366
367 template<typename T>
368 inline opt_mode<T>
369 dyn_cast (machine_mode m)
370 {
371   if (T::includes_p (m))
372     return T (typename mode_traits<T>::from_int (m));
373   return opt_mode<T> ();
374 }
375
376 template<typename T, typename U>
377 inline opt_mode<T>
378 dyn_cast (const opt_mode<U> &m)
379 {
380   return dyn_cast <T> (m.else_void ());
381 }
382
383 /* Return true if mode M has type T, storing it as a T in *RESULT
384    if so.  */
385
386 template<typename T, typename U>
387 inline bool
388 is_a (machine_mode m, U *result)
389 {
390   if (T::includes_p (m))
391     {
392       *result = T (typename mode_traits<T>::from_int (m));
393       return true;
394     }
395   return false;
396 }
397
398 /* Represents a machine mode that is known to be a SCALAR_INT_MODE_P.  */
399 class scalar_int_mode
400 {
401 public:
402   typedef mode_traits<scalar_int_mode>::from_int from_int;
403   typedef unsigned short measurement_type;
404
405   ALWAYS_INLINE scalar_int_mode () {}
406   ALWAYS_INLINE scalar_int_mode (from_int m) : m_mode (machine_mode (m)) {}
407   ALWAYS_INLINE operator machine_mode () const { return m_mode; }
408
409   static bool includes_p (machine_mode);
410
411 protected:
412   machine_mode m_mode;
413 };
414
415 /* Return true if M is a scalar_int_mode.  */
416
417 inline bool
418 scalar_int_mode::includes_p (machine_mode m)
419 {
420   return SCALAR_INT_MODE_P (m);
421 }
422
423 /* Represents a machine mode that is known to be a SCALAR_FLOAT_MODE_P.  */
424 class scalar_float_mode
425 {
426 public:
427   typedef mode_traits<scalar_float_mode>::from_int from_int;
428   typedef unsigned short measurement_type;
429
430   ALWAYS_INLINE scalar_float_mode () {}
431   ALWAYS_INLINE scalar_float_mode (from_int m) : m_mode (machine_mode (m)) {}
432   ALWAYS_INLINE operator machine_mode () const { return m_mode; }
433
434   static bool includes_p (machine_mode);
435
436 protected:
437   machine_mode m_mode;
438 };
439
440 /* Return true if M is a scalar_float_mode.  */
441
442 inline bool
443 scalar_float_mode::includes_p (machine_mode m)
444 {
445   return SCALAR_FLOAT_MODE_P (m);
446 }
447
448 /* Represents a machine mode that is known to be scalar.  */
449 class scalar_mode
450 {
451 public:
452   typedef mode_traits<scalar_mode>::from_int from_int;
453   typedef unsigned short measurement_type;
454
455   ALWAYS_INLINE scalar_mode () {}
456   ALWAYS_INLINE scalar_mode (from_int m) : m_mode (machine_mode (m)) {}
457   ALWAYS_INLINE scalar_mode (const scalar_int_mode &m) : m_mode (m) {}
458   ALWAYS_INLINE scalar_mode (const scalar_float_mode &m) : m_mode (m) {}
459   ALWAYS_INLINE scalar_mode (const scalar_int_mode_pod &m) : m_mode (m) {}
460   ALWAYS_INLINE operator machine_mode () const { return m_mode; }
461
462   static bool includes_p (machine_mode);
463
464 protected:
465   machine_mode m_mode;
466 };
467
468 /* Return true if M represents some kind of scalar value.  */
469
470 inline bool
471 scalar_mode::includes_p (machine_mode m)
472 {
473   switch (GET_MODE_CLASS (m))
474     {
475     case MODE_INT:
476     case MODE_PARTIAL_INT:
477     case MODE_FRACT:
478     case MODE_UFRACT:
479     case MODE_ACCUM:
480     case MODE_UACCUM:
481     case MODE_FLOAT:
482     case MODE_DECIMAL_FLOAT:
483     case MODE_POINTER_BOUNDS:
484       return true;
485     default:
486       return false;
487     }
488 }
489
490 /* Represents a machine mode that is known to be a COMPLEX_MODE_P.  */
491 class complex_mode
492 {
493 public:
494   typedef mode_traits<complex_mode>::from_int from_int;
495   typedef unsigned short measurement_type;
496
497   ALWAYS_INLINE complex_mode () {}
498   ALWAYS_INLINE complex_mode (from_int m) : m_mode (machine_mode (m)) {}
499   ALWAYS_INLINE operator machine_mode () const { return m_mode; }
500
501   static bool includes_p (machine_mode);
502
503 protected:
504   machine_mode m_mode;
505 };
506
507 /* Return true if M is a complex_mode.  */
508
509 inline bool
510 complex_mode::includes_p (machine_mode m)
511 {
512   return COMPLEX_MODE_P (m);
513 }
514
515 /* Return the base GET_MODE_SIZE value for MODE.  */
516
517 ALWAYS_INLINE unsigned short
518 mode_to_bytes (machine_mode mode)
519 {
520 #if GCC_VERSION >= 4001
521   return (__builtin_constant_p (mode)
522           ? mode_size_inline (mode) : mode_size[mode]);
523 #else
524   return mode_size[mode];
525 #endif
526 }
527
528 /* Return the base GET_MODE_BITSIZE value for MODE.  */
529
530 ALWAYS_INLINE unsigned short
531 mode_to_bits (machine_mode mode)
532 {
533   return mode_to_bytes (mode) * BITS_PER_UNIT;
534 }
535
536 /* Return the base GET_MODE_PRECISION value for MODE.  */
537
538 ALWAYS_INLINE poly_uint16
539 mode_to_precision (machine_mode mode)
540 {
541   return mode_precision[mode];
542 }
543
544 /* Return the base GET_MODE_INNER value for MODE.  */
545
546 ALWAYS_INLINE scalar_mode
547 mode_to_inner (machine_mode mode)
548 {
549 #if GCC_VERSION >= 4001
550   return scalar_mode::from_int (__builtin_constant_p (mode)
551                                 ? mode_inner_inline (mode)
552                                 : mode_inner[mode]);
553 #else
554   return scalar_mode::from_int (mode_inner[mode]);
555 #endif
556 }
557
558 /* Return the base GET_MODE_UNIT_SIZE value for MODE.  */
559
560 ALWAYS_INLINE unsigned char
561 mode_to_unit_size (machine_mode mode)
562 {
563 #if GCC_VERSION >= 4001
564   return (__builtin_constant_p (mode)
565           ? mode_unit_size_inline (mode) : mode_unit_size[mode]);
566 #else
567   return mode_unit_size[mode];
568 #endif
569 }
570
571 /* Return the base GET_MODE_UNIT_PRECISION value for MODE.  */
572
573 ALWAYS_INLINE unsigned short
574 mode_to_unit_precision (machine_mode mode)
575 {
576 #if GCC_VERSION >= 4001
577   return (__builtin_constant_p (mode)
578           ? mode_unit_precision_inline (mode) : mode_unit_precision[mode]);
579 #else
580   return mode_unit_precision[mode];
581 #endif
582 }
583
584 /* Return the base GET_MODE_NUNITS value for MODE.  */
585
586 ALWAYS_INLINE poly_uint16
587 mode_to_nunits (machine_mode mode)
588 {
589 #if GCC_VERSION >= 4001
590   return (__builtin_constant_p (mode)
591           ? mode_nunits_inline (mode) : mode_nunits[mode]);
592 #else
593   return mode_nunits[mode];
594 #endif
595 }
596
597 /* Get the size in bytes of an object of mode MODE.  */
598
599 #define GET_MODE_SIZE(MODE) (mode_to_bytes (MODE))
600
601 /* Get the size in bits of an object of mode MODE.  */
602
603 #define GET_MODE_BITSIZE(MODE) (mode_to_bits (MODE))
604
605 /* Get the number of value bits of an object of mode MODE.  */
606
607 #if ONLY_FIXED_SIZE_MODES
608 #define GET_MODE_PRECISION(MODE) \
609   ((unsigned short) mode_to_precision (MODE).coeffs[0])
610 #else
611 ALWAYS_INLINE poly_uint16
612 GET_MODE_PRECISION (machine_mode mode)
613 {
614   return mode_to_precision (mode);
615 }
616
617 template<typename T>
618 ALWAYS_INLINE typename if_poly<typename T::measurement_type>::type
619 GET_MODE_PRECISION (const T &mode)
620 {
621   return mode_to_precision (mode);
622 }
623
624 template<typename T>
625 ALWAYS_INLINE typename if_nonpoly<typename T::measurement_type>::type
626 GET_MODE_PRECISION (const T &mode)
627 {
628   return mode_to_precision (mode).coeffs[0];
629 }
630 #endif
631
632 /* Get the number of integral bits of an object of mode MODE.  */
633 extern CONST_MODE_IBIT unsigned char mode_ibit[NUM_MACHINE_MODES];
634 #define GET_MODE_IBIT(MODE) mode_ibit[MODE]
635
636 /* Get the number of fractional bits of an object of mode MODE.  */
637 extern CONST_MODE_FBIT unsigned char mode_fbit[NUM_MACHINE_MODES];
638 #define GET_MODE_FBIT(MODE) mode_fbit[MODE]
639
640 /* Get a bitmask containing 1 for all bits in a word
641    that fit within mode MODE.  */
642
643 extern const unsigned HOST_WIDE_INT mode_mask_array[NUM_MACHINE_MODES];
644
645 #define GET_MODE_MASK(MODE) mode_mask_array[MODE]
646
647 /* Return the mode of the basic parts of MODE.  For vector modes this is the
648    mode of the vector elements.  For complex modes it is the mode of the real
649    and imaginary parts.  For other modes it is MODE itself.  */
650
651 #define GET_MODE_INNER(MODE) (mode_to_inner (MODE))
652
653 /* Get the size in bytes or bits of the basic parts of an
654    object of mode MODE.  */
655
656 #define GET_MODE_UNIT_SIZE(MODE) mode_to_unit_size (MODE)
657
658 #define GET_MODE_UNIT_BITSIZE(MODE) \
659   ((unsigned short) (GET_MODE_UNIT_SIZE (MODE) * BITS_PER_UNIT))
660
661 #define GET_MODE_UNIT_PRECISION(MODE) (mode_to_unit_precision (MODE))
662
663 /* Get the number of units in an object of mode MODE.  This is 2 for
664    complex modes and the number of elements for vector modes.  */
665
666 #if ONLY_FIXED_SIZE_MODES
667 #define GET_MODE_NUNITS(MODE) (mode_to_nunits (MODE).coeffs[0])
668 #else
669 ALWAYS_INLINE poly_uint16
670 GET_MODE_NUNITS (machine_mode mode)
671 {
672   return mode_to_nunits (mode);
673 }
674
675 template<typename T>
676 ALWAYS_INLINE typename if_poly<typename T::measurement_type>::type
677 GET_MODE_NUNITS (const T &mode)
678 {
679   return mode_to_nunits (mode);
680 }
681
682 template<typename T>
683 ALWAYS_INLINE typename if_nonpoly<typename T::measurement_type>::type
684 GET_MODE_NUNITS (const T &mode)
685 {
686   return mode_to_nunits (mode).coeffs[0];
687 }
688 #endif
689
690 /* Get the next wider natural mode (eg, QI -> HI -> SI -> DI -> TI).  */
691
692 template<typename T>
693 ALWAYS_INLINE opt_mode<T>
694 GET_MODE_WIDER_MODE (const T &m)
695 {
696   return typename opt_mode<T>::from_int (mode_wider[m]);
697 }
698
699 /* For scalars, this is a mode with twice the precision.  For vectors,
700    this is a mode with the same inner mode but with twice the elements.  */
701
702 template<typename T>
703 ALWAYS_INLINE opt_mode<T>
704 GET_MODE_2XWIDER_MODE (const T &m)
705 {
706   return typename opt_mode<T>::from_int (mode_2xwider[m]);
707 }
708
709 /* Get the complex mode from the component mode.  */
710 extern const unsigned char mode_complex[NUM_MACHINE_MODES];
711 #define GET_MODE_COMPLEX_MODE(MODE) ((machine_mode) mode_complex[MODE])
712
713 /* Represents a machine mode that must have a fixed size.  The main
714    use of this class is to represent the modes of objects that always
715    have static storage duration, such as constant pool entries.
716    (No current target supports the concept of variable-size static data.)  */
717 class fixed_size_mode
718 {
719 public:
720   typedef mode_traits<fixed_size_mode>::from_int from_int;
721   typedef unsigned short measurement_type;
722
723   ALWAYS_INLINE fixed_size_mode () {}
724   ALWAYS_INLINE fixed_size_mode (from_int m) : m_mode (machine_mode (m)) {}
725   ALWAYS_INLINE fixed_size_mode (const scalar_mode &m) : m_mode (m) {}
726   ALWAYS_INLINE fixed_size_mode (const scalar_int_mode &m) : m_mode (m) {}
727   ALWAYS_INLINE fixed_size_mode (const scalar_float_mode &m) : m_mode (m) {}
728   ALWAYS_INLINE fixed_size_mode (const scalar_mode_pod &m) : m_mode (m) {}
729   ALWAYS_INLINE fixed_size_mode (const scalar_int_mode_pod &m) : m_mode (m) {}
730   ALWAYS_INLINE fixed_size_mode (const complex_mode &m) : m_mode (m) {}
731   ALWAYS_INLINE operator machine_mode () const { return m_mode; }
732
733   static bool includes_p (machine_mode);
734
735 protected:
736   machine_mode m_mode;
737 };
738
739 /* Return true if MODE has a fixed size.  */
740
741 inline bool
742 fixed_size_mode::includes_p (machine_mode)
743 {
744   return true;
745 }
746
747 /* Wrapper for mode arguments to target macros, so that if a target
748    doesn't need polynomial-sized modes, its header file can continue
749    to treat everything as fixed_size_mode.  This should go away once
750    macros are moved to target hooks.  It shouldn't be used in other
751    contexts.  */
752 #if NUM_POLY_INT_COEFFS == 1
753 #define MACRO_MODE(MODE) (as_a <fixed_size_mode> (MODE))
754 #else
755 #define MACRO_MODE(MODE) (MODE)
756 #endif
757
758 extern opt_machine_mode mode_for_size (poly_uint64, enum mode_class, int);
759
760 /* Return the machine mode to use for a MODE_INT of SIZE bits, if one
761    exists.  If LIMIT is nonzero, modes wider than MAX_FIXED_MODE_SIZE
762    will not be used.  */
763
764 inline opt_scalar_int_mode
765 int_mode_for_size (poly_uint64 size, int limit)
766 {
767   return dyn_cast <scalar_int_mode> (mode_for_size (size, MODE_INT, limit));
768 }
769
770 /* Return the machine mode to use for a MODE_FLOAT of SIZE bits, if one
771    exists.  */
772
773 inline opt_scalar_float_mode
774 float_mode_for_size (poly_uint64 size)
775 {
776   return dyn_cast <scalar_float_mode> (mode_for_size (size, MODE_FLOAT, 0));
777 }
778
779 /* Likewise for MODE_DECIMAL_FLOAT.  */
780
781 inline opt_scalar_float_mode
782 decimal_float_mode_for_size (unsigned int size)
783 {
784   return dyn_cast <scalar_float_mode>
785     (mode_for_size (size, MODE_DECIMAL_FLOAT, 0));
786 }
787
788 extern machine_mode smallest_mode_for_size (poly_uint64, enum mode_class);
789
790 /* Find the narrowest integer mode that contains at least SIZE bits.
791    Such a mode must exist.  */
792
793 inline scalar_int_mode
794 smallest_int_mode_for_size (poly_uint64 size)
795 {
796   return as_a <scalar_int_mode> (smallest_mode_for_size (size, MODE_INT));
797 }
798
799 extern opt_scalar_int_mode int_mode_for_mode (machine_mode);
800 extern opt_machine_mode bitwise_mode_for_mode (machine_mode);
801 extern opt_machine_mode mode_for_vector (scalar_mode, poly_uint64);
802 extern opt_machine_mode mode_for_int_vector (unsigned int, poly_uint64);
803
804 /* Return the integer vector equivalent of MODE, if one exists.  In other
805    words, return the mode for an integer vector that has the same number
806    of bits as MODE and the same number of elements as MODE, with the
807    latter being 1 if MODE is scalar.  The returned mode can be either
808    an integer mode or a vector mode.  */
809
810 inline opt_machine_mode
811 mode_for_int_vector (machine_mode mode)
812 {
813   return mode_for_int_vector (GET_MODE_UNIT_BITSIZE (mode),
814                               GET_MODE_NUNITS (mode));
815 }
816
817 /* A class for iterating through possible bitfield modes.  */
818 class bit_field_mode_iterator
819 {
820 public:
821   bit_field_mode_iterator (HOST_WIDE_INT, HOST_WIDE_INT,
822                            poly_int64, poly_int64,
823                            unsigned int, bool);
824   bool next_mode (scalar_int_mode *);
825   bool prefer_smaller_modes ();
826
827 private:
828   opt_scalar_int_mode m_mode;
829   /* We use signed values here because the bit position can be negative
830      for invalid input such as gcc.dg/pr48335-8.c.  */
831   HOST_WIDE_INT m_bitsize;
832   HOST_WIDE_INT m_bitpos;
833   poly_int64 m_bitregion_start;
834   poly_int64 m_bitregion_end;
835   unsigned int m_align;
836   bool m_volatilep;
837   int m_count;
838 };
839
840 /* Find the best mode to use to access a bit field.  */
841
842 extern bool get_best_mode (int, int, poly_uint64, poly_uint64, unsigned int,
843                            unsigned HOST_WIDE_INT, bool, scalar_int_mode *);
844
845 /* Determine alignment, 1<=result<=BIGGEST_ALIGNMENT.  */
846
847 extern CONST_MODE_BASE_ALIGN unsigned short mode_base_align[NUM_MACHINE_MODES];
848
849 extern unsigned get_mode_alignment (machine_mode);
850
851 #define GET_MODE_ALIGNMENT(MODE) get_mode_alignment (MODE)
852
853 /* For each class, get the narrowest mode in that class.  */
854
855 extern const unsigned char class_narrowest_mode[MAX_MODE_CLASS];
856 #define GET_CLASS_NARROWEST_MODE(CLASS) \
857   ((machine_mode) class_narrowest_mode[CLASS])
858
859 /* The narrowest full integer mode available on the target.  */
860
861 #define NARROWEST_INT_MODE \
862   (scalar_int_mode \
863    (scalar_int_mode::from_int (class_narrowest_mode[MODE_INT])))
864
865 /* Return the narrowest mode in T's class.  */
866
867 template<typename T>
868 inline T
869 get_narrowest_mode (T mode)
870 {
871   return typename mode_traits<T>::from_int
872     (class_narrowest_mode[GET_MODE_CLASS (mode)]);
873 }
874
875 /* Define the integer modes whose sizes are BITS_PER_UNIT and BITS_PER_WORD
876    and the mode whose class is Pmode and whose size is POINTER_SIZE.  */
877
878 extern scalar_int_mode byte_mode;
879 extern scalar_int_mode word_mode;
880 extern scalar_int_mode ptr_mode;
881
882 /* Target-dependent machine mode initialization - in insn-modes.c.  */
883 extern void init_adjust_machine_modes (void);
884
885 #define TRULY_NOOP_TRUNCATION_MODES_P(MODE1, MODE2) \
886   (targetm.truly_noop_truncation (GET_MODE_PRECISION (MODE1), \
887                                   GET_MODE_PRECISION (MODE2)))
888
889 /* Return true if MODE is a scalar integer mode that fits in a
890    HOST_WIDE_INT.  */
891
892 inline bool
893 HWI_COMPUTABLE_MODE_P (machine_mode mode)
894 {
895   machine_mode mme = mode;
896   return (SCALAR_INT_MODE_P (mme)
897           && mode_to_precision (mme).coeffs[0] <= HOST_BITS_PER_WIDE_INT);
898 }
899
900 inline bool
901 HWI_COMPUTABLE_MODE_P (scalar_int_mode mode)
902 {
903   return GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT;
904 }
905
906 struct int_n_data_t {
907   /* These parts are initailized by genmodes output */
908   unsigned int bitsize;
909   scalar_int_mode_pod m;
910   /* RID_* is RID_INTN_BASE + index into this array */
911 };
912
913 /* This is also in tree.h.  genmodes.c guarantees the're sorted from
914    smallest bitsize to largest bitsize. */
915 extern bool int_n_enabled_p[NUM_INT_N_ENTS];
916 extern const int_n_data_t int_n_data[NUM_INT_N_ENTS];
917
918 /* Return true if MODE has class MODE_INT, storing it as a scalar_int_mode
919    in *INT_MODE if so.  */
920
921 template<typename T>
922 inline bool
923 is_int_mode (machine_mode mode, T *int_mode)
924 {
925   if (GET_MODE_CLASS (mode) == MODE_INT)
926     {
927       *int_mode = scalar_int_mode (scalar_int_mode::from_int (mode));
928       return true;
929     }
930   return false;
931 }
932
933 /* Return true if MODE has class MODE_FLOAT, storing it as a
934    scalar_float_mode in *FLOAT_MODE if so.  */
935
936 template<typename T>
937 inline bool
938 is_float_mode (machine_mode mode, T *float_mode)
939 {
940   if (GET_MODE_CLASS (mode) == MODE_FLOAT)
941     {
942       *float_mode = scalar_float_mode (scalar_float_mode::from_int (mode));
943       return true;
944     }
945   return false;
946 }
947
948 /* Return true if MODE has class MODE_COMPLEX_INT, storing it as
949    a complex_mode in *CMODE if so.  */
950
951 template<typename T>
952 inline bool
953 is_complex_int_mode (machine_mode mode, T *cmode)
954 {
955   if (GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
956     {
957       *cmode = complex_mode (complex_mode::from_int (mode));
958       return true;
959     }
960   return false;
961 }
962
963 /* Return true if MODE has class MODE_COMPLEX_FLOAT, storing it as
964    a complex_mode in *CMODE if so.  */
965
966 template<typename T>
967 inline bool
968 is_complex_float_mode (machine_mode mode, T *cmode)
969 {
970   if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
971     {
972       *cmode = complex_mode (complex_mode::from_int (mode));
973       return true;
974     }
975   return false;
976 }
977
978 /* Return true if MODE is a scalar integer mode with a precision
979    smaller than LIMIT's precision.  */
980
981 inline bool
982 is_narrower_int_mode (machine_mode mode, scalar_int_mode limit)
983 {
984   scalar_int_mode int_mode;
985   return (is_a <scalar_int_mode> (mode, &int_mode)
986           && GET_MODE_PRECISION (int_mode) < GET_MODE_PRECISION (limit));
987 }
988
989 namespace mode_iterator
990 {
991   /* Start mode iterator *ITER at the first mode in class MCLASS, if any.  */
992
993   template<typename T>
994   inline void
995   start (opt_mode<T> *iter, enum mode_class mclass)
996   {
997     if (GET_CLASS_NARROWEST_MODE (mclass) == E_VOIDmode)
998       *iter = opt_mode<T> ();
999     else
1000       *iter = as_a<T> (GET_CLASS_NARROWEST_MODE (mclass));
1001   }
1002
1003   inline void
1004   start (machine_mode *iter, enum mode_class mclass)
1005   {
1006     *iter = GET_CLASS_NARROWEST_MODE (mclass);
1007   }
1008
1009   /* Return true if mode iterator *ITER has not reached the end.  */
1010
1011   template<typename T>
1012   inline bool
1013   iterate_p (opt_mode<T> *iter)
1014   {
1015     return iter->exists ();
1016   }
1017
1018   inline bool
1019   iterate_p (machine_mode *iter)
1020   {
1021     return *iter != E_VOIDmode;
1022   }
1023
1024   /* Set mode iterator *ITER to the next widest mode in the same class,
1025      if any.  */
1026
1027   template<typename T>
1028   inline void
1029   get_wider (opt_mode<T> *iter)
1030   {
1031     *iter = GET_MODE_WIDER_MODE (iter->require ());
1032   }
1033
1034   inline void
1035   get_wider (machine_mode *iter)
1036   {
1037     *iter = GET_MODE_WIDER_MODE (*iter).else_void ();
1038   }
1039
1040   /* Set mode iterator *ITER to the next widest mode in the same class.
1041      Such a mode is known to exist.  */
1042
1043   template<typename T>
1044   inline void
1045   get_known_wider (T *iter)
1046   {
1047     *iter = GET_MODE_WIDER_MODE (*iter).require ();
1048   }
1049
1050   /* Set mode iterator *ITER to the mode that is two times wider than the
1051      current one, if such a mode exists.  */
1052
1053   template<typename T>
1054   inline void
1055   get_2xwider (opt_mode<T> *iter)
1056   {
1057     *iter = GET_MODE_2XWIDER_MODE (iter->require ());
1058   }
1059
1060   inline void
1061   get_2xwider (machine_mode *iter)
1062   {
1063     *iter = GET_MODE_2XWIDER_MODE (*iter).else_void ();
1064   }
1065 }
1066
1067 /* Make ITERATOR iterate over all the modes in mode class CLASS,
1068    from narrowest to widest.  */
1069 #define FOR_EACH_MODE_IN_CLASS(ITERATOR, CLASS)  \
1070   for (mode_iterator::start (&(ITERATOR), CLASS); \
1071        mode_iterator::iterate_p (&(ITERATOR)); \
1072        mode_iterator::get_wider (&(ITERATOR)))
1073
1074 /* Make ITERATOR iterate over all the modes in the range [START, END),
1075    in order of increasing width.  */
1076 #define FOR_EACH_MODE(ITERATOR, START, END) \
1077   for ((ITERATOR) = (START); \
1078        (ITERATOR) != (END); \
1079        mode_iterator::get_known_wider (&(ITERATOR)))
1080
1081 /* Make ITERATOR iterate over START and all wider modes in the same
1082    class, in order of increasing width.  */
1083 #define FOR_EACH_MODE_FROM(ITERATOR, START) \
1084   for ((ITERATOR) = (START); \
1085        mode_iterator::iterate_p (&(ITERATOR)); \
1086        mode_iterator::get_wider (&(ITERATOR)))
1087
1088 /* Make ITERATOR iterate over modes in the range [NARROWEST, END)
1089    in order of increasing width, where NARROWEST is the narrowest mode
1090    in END's class.  */
1091 #define FOR_EACH_MODE_UNTIL(ITERATOR, END) \
1092   FOR_EACH_MODE (ITERATOR, get_narrowest_mode (END), END)
1093
1094 /* Make ITERATOR iterate over modes in the same class as MODE, in order
1095    of increasing width.  Start at the first mode wider than START,
1096    or don't iterate at all if there is no wider mode.  */
1097 #define FOR_EACH_WIDER_MODE(ITERATOR, START) \
1098   for ((ITERATOR) = (START), mode_iterator::get_wider (&(ITERATOR)); \
1099        mode_iterator::iterate_p (&(ITERATOR)); \
1100        mode_iterator::get_wider (&(ITERATOR)))
1101
1102 /* Make ITERATOR iterate over modes in the same class as MODE, in order
1103    of increasing width, and with each mode being twice the width of the
1104    previous mode.  Start at the mode that is two times wider than START,
1105    or don't iterate at all if there is no such mode.  */
1106 #define FOR_EACH_2XWIDER_MODE(ITERATOR, START) \
1107   for ((ITERATOR) = (START), mode_iterator::get_2xwider (&(ITERATOR)); \
1108        mode_iterator::iterate_p (&(ITERATOR)); \
1109        mode_iterator::get_2xwider (&(ITERATOR)))
1110
1111 template<typename T>
1112 void
1113 gt_ggc_mx (pod_mode<T> *)
1114 {
1115 }
1116
1117 template<typename T>
1118 void
1119 gt_pch_nx (pod_mode<T> *)
1120 {
1121 }
1122
1123 template<typename T>
1124 void
1125 gt_pch_nx (pod_mode<T> *, void (*) (void *, void *), void *)
1126 {
1127 }
1128
1129 #endif /* not HAVE_MACHINE_MODES */