1e1fd32794897afe2d8463777afcd3985f4efe8e
[platform/upstream/gcc.git] / gcc / lists.c
1 /* List management for the GCC expander.
2    Copyright (C) 1987-2015 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 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "diagnostic-core.h"
25 #include "rtl.h"
26
27 static void free_list (rtx *, rtx *);
28
29 /* Functions for maintaining cache-able lists of EXPR_LIST and INSN_LISTs.  */
30
31 /* An INSN_LIST containing all INSN_LISTs allocated but currently unused.  */
32 static GTY ((deletable)) rtx unused_insn_list;
33
34 /* An EXPR_LIST containing all EXPR_LISTs allocated but currently unused.  */
35 static GTY ((deletable)) rtx unused_expr_list;
36
37 /* This function will free an entire list of either EXPR_LIST, INSN_LIST
38    or DEPS_LIST nodes.  This is to be used only on lists that consist
39    exclusively of nodes of one type only.  This is only called by
40    free_EXPR_LIST_list, free_INSN_LIST_list and free_DEPS_LIST_list.  */
41 static void
42 free_list (rtx *listp, rtx *unused_listp)
43 {
44   rtx link, prev_link;
45
46   prev_link = *listp;
47   link = XEXP (prev_link, 1);
48
49   gcc_assert (unused_listp != &unused_insn_list
50               || GET_CODE (prev_link) == INSN_LIST);
51
52   while (link)
53     {
54       gcc_assert (unused_listp != &unused_insn_list
55                   || GET_CODE (prev_link) == INSN_LIST);
56
57       prev_link = link;
58       link = XEXP (link, 1);
59     }
60
61   XEXP (prev_link, 1) = *unused_listp;
62   *unused_listp = *listp;
63   *listp = 0;
64 }
65
66 /* Find corresponding to ELEM node in the list pointed to by LISTP.
67    This node must exist in the list.  Returns pointer to that node.  */
68 static rtx *
69 find_list_elem (rtx elem, rtx *listp)
70 {
71   while (XEXP (*listp, 0) != elem)
72     listp = &XEXP (*listp, 1);
73   return listp;
74 }
75
76 /* Remove the node pointed to by LISTP from the list.  */
77 static void
78 remove_list_node (rtx *listp)
79 {
80   rtx node;
81
82   node = *listp;
83   *listp = XEXP (node, 1);
84   XEXP (node, 1) = 0;
85 }
86
87 /* Removes corresponding to ELEM node from the list pointed to by LISTP.
88    Returns that node.  */
89 rtx
90 remove_list_elem (rtx elem, rtx *listp)
91 {
92   rtx node;
93
94   listp = find_list_elem (elem, listp);
95   node = *listp;
96   remove_list_node (listp);
97   return node;
98 }
99
100 /* This call is used in place of a gen_rtx_INSN_LIST. If there is a cached
101    node available, we'll use it, otherwise a call to gen_rtx_INSN_LIST
102    is made.  */
103 rtx_insn_list *
104 alloc_INSN_LIST (rtx val, rtx next)
105 {
106   rtx_insn_list *r;
107
108   if (unused_insn_list)
109     {
110       r = as_a <rtx_insn_list *> (unused_insn_list);
111       unused_insn_list = r->next ();
112       XEXP (r, 0) = val;
113       XEXP (r, 1) = next;
114       PUT_REG_NOTE_KIND (r, VOIDmode);
115
116       gcc_assert (GET_CODE (r) == INSN_LIST);
117     }
118   else
119     r = gen_rtx_INSN_LIST (VOIDmode, val, next);
120
121   return r;
122 }
123
124 /* This call is used in place of a gen_rtx_EXPR_LIST. If there is a cached
125    node available, we'll use it, otherwise a call to gen_rtx_EXPR_LIST
126    is made.  */
127 rtx_expr_list *
128 alloc_EXPR_LIST (int kind, rtx val, rtx next)
129 {
130   rtx_expr_list *r;
131
132   if (unused_expr_list)
133     {
134       r = as_a <rtx_expr_list *> (unused_expr_list);
135       unused_expr_list = XEXP (r, 1);
136       XEXP (r, 0) = val;
137       XEXP (r, 1) = next;
138       PUT_REG_NOTE_KIND (r, kind);
139     }
140   else
141     r = gen_rtx_EXPR_LIST ((machine_mode) kind, val, next);
142
143   return r;
144 }
145
146 /* This function will free up an entire list of EXPR_LIST nodes.  */
147 void
148 free_EXPR_LIST_list (rtx_expr_list **listp)
149 {
150   if (*listp == 0)
151     return;
152   free_list ((rtx *)listp, &unused_expr_list);
153 }
154
155 /* This function will free up an entire list of INSN_LIST nodes.  */
156 void
157 free_INSN_LIST_list (rtx_insn_list **listp)
158 {
159   if (*listp == 0)
160     return;
161   free_list ((rtx *)listp, &unused_insn_list);
162 }
163
164 /* Make a copy of the INSN_LIST list LINK and return it.  */
165 rtx_insn_list *
166 copy_INSN_LIST (rtx_insn_list *link)
167 {
168   rtx_insn_list *new_queue;
169   rtx_insn_list **pqueue = &new_queue;
170
171   for (; link; link = link->next ())
172     {
173       rtx_insn *x = link->insn ();
174       rtx_insn_list *newlink = alloc_INSN_LIST (x, NULL);
175       *pqueue = newlink;
176       pqueue = (rtx_insn_list **)&XEXP (newlink, 1);
177     }
178   *pqueue = NULL;
179   return new_queue;
180 }
181
182 /* Duplicate the INSN_LIST elements of COPY and prepend them to OLD.  */
183 rtx_insn_list *
184 concat_INSN_LIST (rtx_insn_list *copy, rtx_insn_list *old)
185 {
186   rtx_insn_list *new_rtx = old;
187   for (; copy ; copy = copy->next ())
188     {
189       new_rtx = alloc_INSN_LIST (copy->insn (), new_rtx);
190       PUT_REG_NOTE_KIND (new_rtx, REG_NOTE_KIND (copy));
191     }
192   return new_rtx;
193 }
194
195 /* This function will free up an individual EXPR_LIST node.  */
196 void
197 free_EXPR_LIST_node (rtx ptr)
198 {
199   XEXP (ptr, 1) = unused_expr_list;
200   unused_expr_list = ptr;
201 }
202
203 /* This function will free up an individual INSN_LIST node.  */
204 void
205 free_INSN_LIST_node (rtx ptr)
206 {
207   gcc_assert (GET_CODE (ptr) == INSN_LIST);
208   XEXP (ptr, 1) = unused_insn_list;
209   unused_insn_list = ptr;
210 }
211
212 /* Remove and free corresponding to ELEM node in the INSN_LIST pointed to
213    by LISTP.  */
214 void
215 remove_free_INSN_LIST_elem (rtx_insn *elem, rtx_insn_list **listp)
216 {
217   free_INSN_LIST_node (remove_list_elem (elem, (rtx *)listp));
218 }
219
220 /* Remove and free the first node in the INSN_LIST pointed to by LISTP.  */
221 rtx_insn *
222 remove_free_INSN_LIST_node (rtx_insn_list **listp)
223 {
224   rtx_insn_list *node = *listp;
225   rtx_insn *elem = node->insn ();
226
227   remove_list_node ((rtx *)listp);
228   free_INSN_LIST_node (node);
229
230   return elem;
231 }
232
233 /* Remove and free the first node in the EXPR_LIST pointed to by LISTP.  */
234 rtx
235 remove_free_EXPR_LIST_node (rtx_expr_list **listp)
236 {
237   rtx_expr_list *node = *listp;
238   rtx elem = XEXP (node, 0);
239
240   remove_list_node ((rtx *)listp);
241   free_EXPR_LIST_node (node);
242
243   return elem;
244 }
245
246 #include "gt-lists.h"