xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/rtx-vector-builder.c (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1cef8759bSmrg /* A class for building vector rtx constants.
2*4c3eb207Smrg    Copyright (C) 2017-2020 Free Software Foundation, Inc.
3cef8759bSmrg 
4cef8759bSmrg This file is part of GCC.
5cef8759bSmrg 
6cef8759bSmrg GCC is free software; you can redistribute it and/or modify it under
7cef8759bSmrg the terms of the GNU General Public License as published by the Free
8cef8759bSmrg Software Foundation; either version 3, or (at your option) any later
9cef8759bSmrg version.
10cef8759bSmrg 
11cef8759bSmrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12cef8759bSmrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
13cef8759bSmrg FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14cef8759bSmrg for more details.
15cef8759bSmrg 
16cef8759bSmrg You should have received a copy of the GNU General Public License
17cef8759bSmrg along with GCC; see the file COPYING3.  If not see
18cef8759bSmrg <http://www.gnu.org/licenses/>.  */
19cef8759bSmrg 
20cef8759bSmrg #include "config.h"
21cef8759bSmrg #include "system.h"
22cef8759bSmrg #include "coretypes.h"
23cef8759bSmrg #include "tm.h"
24cef8759bSmrg #include "rtl.h"
25cef8759bSmrg #include "rtx-vector-builder.h"
26cef8759bSmrg 
27cef8759bSmrg /* Return a CONST_VECTOR for the current constant.  V is an existing
28cef8759bSmrg    rtvec that contains all the elements.  */
29cef8759bSmrg 
30cef8759bSmrg rtx
build(rtvec v)31cef8759bSmrg rtx_vector_builder::build (rtvec v)
32cef8759bSmrg {
33cef8759bSmrg   finalize ();
34cef8759bSmrg 
35cef8759bSmrg   rtx x = find_cached_value ();
36cef8759bSmrg   if (x)
37cef8759bSmrg     return x;
38cef8759bSmrg 
39cef8759bSmrg   x = gen_rtx_raw_CONST_VECTOR (m_mode, v);
40cef8759bSmrg   CONST_VECTOR_NPATTERNS (x) = npatterns ();
41cef8759bSmrg   CONST_VECTOR_NELTS_PER_PATTERN (x) = nelts_per_pattern ();
42cef8759bSmrg   return x;
43cef8759bSmrg }
44cef8759bSmrg 
45cef8759bSmrg /* Return a vector element with the value BASE + FACTOR * STEP.  */
46cef8759bSmrg 
47cef8759bSmrg rtx
apply_step(rtx base,unsigned int factor,const poly_wide_int & step)48cef8759bSmrg rtx_vector_builder::apply_step (rtx base, unsigned int factor,
49*4c3eb207Smrg 				const poly_wide_int &step) const
50cef8759bSmrg {
51cef8759bSmrg   scalar_int_mode int_mode = as_a <scalar_int_mode> (GET_MODE_INNER (m_mode));
52*4c3eb207Smrg   return immed_wide_int_const (wi::to_poly_wide (base, int_mode)
53*4c3eb207Smrg 			       + factor * step,
54cef8759bSmrg 			       int_mode);
55cef8759bSmrg }
56cef8759bSmrg 
57cef8759bSmrg /* Return a CONST_VECTOR for the current constant.  */
58cef8759bSmrg 
59cef8759bSmrg rtx
build()60cef8759bSmrg rtx_vector_builder::build ()
61cef8759bSmrg {
62cef8759bSmrg   finalize ();
63cef8759bSmrg 
64cef8759bSmrg   rtx x = find_cached_value ();
65cef8759bSmrg   if (x)
66cef8759bSmrg     return x;
67cef8759bSmrg 
68cef8759bSmrg   unsigned int nelts;
69cef8759bSmrg   if (!GET_MODE_NUNITS (m_mode).is_constant (&nelts))
70cef8759bSmrg     nelts = encoded_nelts ();
71cef8759bSmrg   rtvec v = rtvec_alloc (nelts);
72cef8759bSmrg   for (unsigned int i = 0; i < nelts; ++i)
73cef8759bSmrg     RTVEC_ELT (v, i) = elt (i);
74cef8759bSmrg   x = gen_rtx_raw_CONST_VECTOR (m_mode, v);
75cef8759bSmrg   CONST_VECTOR_NPATTERNS (x) = npatterns ();
76cef8759bSmrg   CONST_VECTOR_NELTS_PER_PATTERN (x) = nelts_per_pattern ();
77cef8759bSmrg   return x;
78cef8759bSmrg }
79cef8759bSmrg 
80cef8759bSmrg /* Check whether there is a global cached value for the vector.
81cef8759bSmrg    Return it if so, otherwise return null.  */
82cef8759bSmrg 
83cef8759bSmrg rtx
find_cached_value()84cef8759bSmrg rtx_vector_builder::find_cached_value ()
85cef8759bSmrg {
86cef8759bSmrg   if (encoded_nelts () != 1)
87cef8759bSmrg     return NULL_RTX;
88cef8759bSmrg 
89cef8759bSmrg   rtx elt = (*this)[0];
90cef8759bSmrg 
91cef8759bSmrg   if (GET_MODE_CLASS (m_mode) == MODE_VECTOR_BOOL)
92cef8759bSmrg     {
93cef8759bSmrg       if (elt == const1_rtx || elt == constm1_rtx)
94cef8759bSmrg 	return CONST1_RTX (m_mode);
95cef8759bSmrg       else if (elt == const0_rtx)
96cef8759bSmrg 	return CONST0_RTX (m_mode);
97cef8759bSmrg       else
98cef8759bSmrg 	gcc_unreachable ();
99cef8759bSmrg     }
100cef8759bSmrg 
101cef8759bSmrg   /* We can be called before the global vector constants are set up,
102cef8759bSmrg      but in that case we'll just return null.  */
103cef8759bSmrg   scalar_mode inner_mode = GET_MODE_INNER (m_mode);
104cef8759bSmrg   if (elt == CONST0_RTX (inner_mode))
105cef8759bSmrg     return CONST0_RTX (m_mode);
106cef8759bSmrg   else if (elt == CONST1_RTX (inner_mode))
107cef8759bSmrg     return CONST1_RTX (m_mode);
108cef8759bSmrg   else if (elt == CONSTM1_RTX (inner_mode))
109cef8759bSmrg     return CONSTM1_RTX (m_mode);
110cef8759bSmrg 
111cef8759bSmrg   return NULL_RTX;
112cef8759bSmrg }
113