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