1/* Generic hooks for the RTL middle-end.
2   Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 2, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING.  If not, write to the Free
18Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
1902110-1301, USA.  */
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tm.h"
25#include "rtl.h"
26#include "rtlhooks-def.h"
27#include "expr.h"
28#include "recog.h"
29
30
31/* For speed, we will copy the RTX hooks struct member-by-member
32   instead of doing indirect calls.  For these reason, we initialize
33   *two* struct rtl_hooks globals: rtl_hooks is the one that is used
34   to actually call the hooks, while general_rtl_hooks is used
35   to restore the hooks by passes that modify them.  */
36
37const struct rtl_hooks general_rtl_hooks = RTL_HOOKS_INITIALIZER;
38struct rtl_hooks rtl_hooks = RTL_HOOKS_INITIALIZER;
39
40rtx
41gen_lowpart_general (enum machine_mode mode, rtx x)
42{
43  rtx result = gen_lowpart_common (mode, x);
44
45  if (result)
46    return result;
47  /* If it's a REG, it must be a hard reg that's not valid in MODE.  */
48  else if (REG_P (x)
49	   /* Or we could have a subreg of a floating point value.  */
50	   || (GET_CODE (x) == SUBREG
51	       && FLOAT_MODE_P (GET_MODE (SUBREG_REG (x)))))
52    {
53      result = gen_lowpart_common (mode, copy_to_reg (x));
54      gcc_assert (result != 0);
55      return result;
56    }
57  else
58    {
59      int offset = 0;
60
61      /* The only additional case we can do is MEM.  */
62      gcc_assert (MEM_P (x));
63
64      /* The following exposes the use of "x" to CSE.  */
65      if (GET_MODE_SIZE (GET_MODE (x)) <= UNITS_PER_WORD
66	  && SCALAR_INT_MODE_P (GET_MODE (x))
67	  && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
68				    GET_MODE_BITSIZE (GET_MODE (x)))
69	  && ! no_new_pseudos)
70	return gen_lowpart_general (mode, force_reg (GET_MODE (x), x));
71
72      if (WORDS_BIG_ENDIAN)
73	offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
74		  - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
75
76      if (BYTES_BIG_ENDIAN)
77	/* Adjust the address so that the address-after-the-data
78	   is unchanged.  */
79	offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
80		   - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
81
82      return adjust_address (x, mode, offset);
83    }
84}
85
86/* Similar to gen_lowpart, but cannot emit any instruction via
87   copy_to_reg or force_reg.  Mainly used in simplify-rtx.c.  */
88rtx
89gen_lowpart_no_emit_general (enum machine_mode mode, rtx x)
90{
91  rtx result = gen_lowpart_if_possible (mode, x);
92  if (result)
93    return result;
94  else
95    return x;
96}
97
98rtx
99reg_num_sign_bit_copies_general (rtx x ATTRIBUTE_UNUSED,
100				 enum machine_mode mode ATTRIBUTE_UNUSED,
101                                 rtx known_x ATTRIBUTE_UNUSED,
102				 enum machine_mode known_mode ATTRIBUTE_UNUSED,
103                                 unsigned int known_ret ATTRIBUTE_UNUSED,
104                                 unsigned int *result ATTRIBUTE_UNUSED)
105{
106  return NULL;
107}
108
109rtx
110reg_nonzero_bits_general (rtx x ATTRIBUTE_UNUSED,
111			  enum machine_mode mode ATTRIBUTE_UNUSED,
112			  rtx known_x ATTRIBUTE_UNUSED,
113                          enum machine_mode known_mode ATTRIBUTE_UNUSED,
114                          unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
115                          unsigned HOST_WIDE_INT *nonzero ATTRIBUTE_UNUSED)
116{
117  return NULL;
118}
119
120bool
121reg_truncated_to_mode_general (enum machine_mode mode ATTRIBUTE_UNUSED,
122			       rtx x ATTRIBUTE_UNUSED)
123{
124  return false;
125}
126
127/* Assuming that X is an rtx (e.g., MEM, REG or SUBREG) for a fixed-point
128   number, return an rtx (MEM, SUBREG, or CONST_INT) that refers to the
129   least-significant part of X.
130   MODE specifies how big a part of X to return.
131
132   If the requested operation cannot be done, 0 is returned.
133
134   This is similar to gen_lowpart_general.  */
135
136rtx
137gen_lowpart_if_possible (enum machine_mode mode, rtx x)
138{
139  rtx result = gen_lowpart_common (mode, x);
140
141  if (result)
142    return result;
143  else if (MEM_P (x))
144    {
145      /* This is the only other case we handle.  */
146      int offset = 0;
147      rtx new;
148
149      if (WORDS_BIG_ENDIAN)
150	offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
151		  - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
152      if (BYTES_BIG_ENDIAN)
153	/* Adjust the address so that the address-after-the-data is
154	   unchanged.  */
155	offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
156		   - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
157
158      new = adjust_address_nv (x, mode, offset);
159      if (! memory_address_p (mode, XEXP (new, 0)))
160	return 0;
161
162      return new;
163    }
164  else if (mode != GET_MODE (x) && GET_MODE (x) != VOIDmode)
165    return gen_lowpart_SUBREG (mode, x);
166  else
167    return 0;
168}
169
170