]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gensupport.c
re PR rtl-optimization/77425 (Pointer test follows dereference in sched-int.h)
[thirdparty/gcc.git] / gcc / gensupport.c
CommitLineData
3916d6d8 1/* Support routines for the various generation passes.
818ab71a 2 Copyright (C) 2000-2016 Free Software Foundation, Inc.
c88c0d42 3
1322177d 4 This file is part of GCC.
c88c0d42 5
1322177d
LB
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
9dcd6f09 8 the Free Software Foundation; either version 3, or (at your option)
c88c0d42
CP
9 any later version.
10
1322177d
LB
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
c88c0d42
CP
15
16 You should have received a copy of the GNU General Public License
9dcd6f09
NC
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
c88c0d42 19
4977bab6 20#include "bconfig.h"
c88c0d42 21#include "system.h"
4977bab6
ZW
22#include "coretypes.h"
23#include "tm.h"
c88c0d42 24#include "rtl.h"
3916d6d8 25#include "obstack.h"
c88c0d42 26#include "errors.h"
10692477 27#include "read-md.h"
c88c0d42 28#include "gensupport.h"
6b8068d6 29#include "vec.h"
c88c0d42 30
477c104e
MK
31#define MAX_OPERANDS 40
32
33static rtx operand_data[MAX_OPERANDS];
34static rtx match_operand_entries_in_pattern[MAX_OPERANDS];
35static char used_operands_numbers[MAX_OPERANDS];
36
3916d6d8 37
c8cf201f
RK
38/* In case some macros used by files we include need it, define this here. */
39int target_flags;
40
2199e5fa
ZW
41int insn_elision = 1;
42
3916d6d8
RH
43static struct obstack obstack;
44struct obstack *rtl_obstack = &obstack;
45
72a35f93
RS
46/* Counter for named patterns and INSN_CODEs. */
47static int insn_sequence_num;
48
49/* Counter for define_splits. */
50static int split_sequence_num;
51
52/* Counter for define_peephole2s. */
53static int peephole2_sequence_num;
3262c1f5
RH
54
55static int predicable_default;
56static const char *predicable_true;
57static const char *predicable_false;
58
477c104e
MK
59static const char *subst_true = "yes";
60static const char *subst_false = "no";
61
2199e5fa
ZW
62static htab_t condition_table;
63
477c104e
MK
64/* We initially queue all patterns, process the define_insn,
65 define_cond_exec and define_subst patterns, then return
66 them one at a time. */
c88c0d42 67
3262c1f5
RH
68struct queue_elem
69{
70 rtx data;
cc472607 71 file_location loc;
3262c1f5 72 struct queue_elem *next;
a406f566
MM
73 /* In a DEFINE_INSN that came from a DEFINE_INSN_AND_SPLIT, SPLIT
74 points to the generated DEFINE_SPLIT. */
75 struct queue_elem *split;
c88c0d42
CP
76};
77
64aad689
AK
78#define MNEMONIC_ATTR_NAME "mnemonic"
79#define MNEMONIC_HTAB_SIZE 1024
80
3262c1f5
RH
81static struct queue_elem *define_attr_queue;
82static struct queue_elem **define_attr_tail = &define_attr_queue;
e543e219
ZW
83static struct queue_elem *define_pred_queue;
84static struct queue_elem **define_pred_tail = &define_pred_queue;
3262c1f5
RH
85static struct queue_elem *define_insn_queue;
86static struct queue_elem **define_insn_tail = &define_insn_queue;
87static struct queue_elem *define_cond_exec_queue;
88static struct queue_elem **define_cond_exec_tail = &define_cond_exec_queue;
477c104e
MK
89static struct queue_elem *define_subst_queue;
90static struct queue_elem **define_subst_tail = &define_subst_queue;
3262c1f5
RH
91static struct queue_elem *other_queue;
92static struct queue_elem **other_tail = &other_queue;
477c104e
MK
93static struct queue_elem *define_subst_attr_queue;
94static struct queue_elem **define_subst_attr_tail = &define_subst_attr_queue;
c88c0d42 95
58d745ec
RS
96/* Mapping from DEFINE_* rtxes to their location in the source file. */
97static hash_map <rtx, file_location> *rtx_locs;
98
3d7aafde 99static void remove_constraints (rtx);
3d7aafde
AJ
100
101static int is_predicable (struct queue_elem *);
102static void identify_predicable_attribute (void);
103static int n_alternatives (const char *);
104static void collect_insn_data (rtx, int *, int *);
3d7aafde
AJ
105static const char *alter_test_for_insn (struct queue_elem *,
106 struct queue_elem *);
107static char *shift_output_template (char *, const char *, int);
108static const char *alter_output_for_insn (struct queue_elem *,
109 struct queue_elem *,
110 int, int);
111static void process_one_cond_exec (struct queue_elem *);
112static void process_define_cond_exec (void);
e543e219 113static void init_predicate_table (void);
0458fe77 114static void record_insn_name (int, const char *);
477c104e
MK
115
116static bool has_subst_attribute (struct queue_elem *, struct queue_elem *);
477c104e
MK
117static const char * alter_output_for_subst_insn (rtx, int);
118static void alter_attrs_for_subst_insn (struct queue_elem *, int);
119static void process_substs_on_one_elem (struct queue_elem *,
120 struct queue_elem *);
121static rtx subst_dup (rtx, int, int);
122static void process_define_subst (void);
123
124static const char * duplicate_alternatives (const char *, int);
125static const char * duplicate_each_alternative (const char * str, int n_dup);
126
127typedef const char * (*constraints_handler_t) (const char *, int);
128static rtx alter_constraints (rtx, int, constraints_handler_t);
a7ba15ca 129static rtx adjust_operands_numbers (rtx);
477c104e 130static rtx replace_duplicating_operands_in_pattern (rtx);
3916d6d8 131\f
10b76d73
RK
132/* Make a version of gen_rtx_CONST_INT so that GEN_INT can be used in
133 the gensupport programs. */
134
135rtx
ef4bddc2 136gen_rtx_CONST_INT (machine_mode ARG_UNUSED (mode),
3d7aafde 137 HOST_WIDE_INT arg)
10b76d73
RK
138{
139 rtx rt = rtx_alloc (CONST_INT);
140
141 XWINT (rt, 0) = arg;
142 return rt;
143}
313d38e3
RS
144
145/* Return the rtx pattern specified by the list of rtxes in a
146 define_insn or define_split. */
147
148rtx
149add_implicit_parallel (rtvec vec)
150{
151 if (GET_NUM_ELEM (vec) == 1)
152 return RTVEC_ELT (vec, 0);
153 else
154 {
155 rtx pattern = rtx_alloc (PARALLEL);
156 XVEC (pattern, 0) = vec;
157 return pattern;
158 }
159}
3916d6d8 160\f
77059241
RS
161/* Predicate handling.
162
163 We construct from the machine description a table mapping each
164 predicate to a list of the rtl codes it can possibly match. The
165 function 'maybe_both_true' uses it to deduce that there are no
166 expressions that can be matches by certain pairs of tree nodes.
167 Also, if a predicate can match only one code, we can hardwire that
168 code into the node testing the predicate.
169
170 Some predicates are flagged as special. validate_pattern will not
171 warn about modeless match_operand expressions if they have a
172 special predicate. Predicates that allow only constants are also
173 treated as special, for this purpose.
174
175 validate_pattern will warn about predicates that allow non-lvalues
176 when they appear in destination operands.
177
178 Calculating the set of rtx codes that can possibly be accepted by a
179 predicate expression EXP requires a three-state logic: any given
180 subexpression may definitively accept a code C (Y), definitively
181 reject a code C (N), or may have an indeterminate effect (I). N
182 and I is N; Y or I is Y; Y and I, N or I are both I. Here are full
183 truth tables.
184
185 a b a&b a|b
186 Y Y Y Y
187 N Y N Y
188 N N N N
189 I Y I Y
190 I N N I
191 I I I I
192
193 We represent Y with 1, N with 0, I with 2. If any code is left in
194 an I state by the complete expression, we must assume that that
195 code can be accepted. */
196
197#define N 0
198#define Y 1
199#define I 2
200
201#define TRISTATE_AND(a,b) \
202 ((a) == I ? ((b) == N ? N : I) : \
203 (b) == I ? ((a) == N ? N : I) : \
204 (a) && (b))
205
206#define TRISTATE_OR(a,b) \
207 ((a) == I ? ((b) == Y ? Y : I) : \
208 (b) == I ? ((a) == Y ? Y : I) : \
209 (a) || (b))
210
211#define TRISTATE_NOT(a) \
212 ((a) == I ? I : !(a))
213
214/* 0 means no warning about that code yet, 1 means warned. */
215static char did_you_mean_codes[NUM_RTX_CODE];
216
217/* Recursively calculate the set of rtx codes accepted by the
c9f84f2e
RS
218 predicate expression EXP, writing the result to CODES. LOC is
219 the .md file location of the directive containing EXP. */
77059241 220
851ee5f4 221void
c9f84f2e 222compute_test_codes (rtx exp, file_location loc, char *codes)
77059241
RS
223{
224 char op0_codes[NUM_RTX_CODE];
225 char op1_codes[NUM_RTX_CODE];
226 char op2_codes[NUM_RTX_CODE];
227 int i;
228
229 switch (GET_CODE (exp))
230 {
231 case AND:
c9f84f2e
RS
232 compute_test_codes (XEXP (exp, 0), loc, op0_codes);
233 compute_test_codes (XEXP (exp, 1), loc, op1_codes);
77059241
RS
234 for (i = 0; i < NUM_RTX_CODE; i++)
235 codes[i] = TRISTATE_AND (op0_codes[i], op1_codes[i]);
236 break;
237
238 case IOR:
c9f84f2e
RS
239 compute_test_codes (XEXP (exp, 0), loc, op0_codes);
240 compute_test_codes (XEXP (exp, 1), loc, op1_codes);
77059241
RS
241 for (i = 0; i < NUM_RTX_CODE; i++)
242 codes[i] = TRISTATE_OR (op0_codes[i], op1_codes[i]);
243 break;
244 case NOT:
c9f84f2e 245 compute_test_codes (XEXP (exp, 0), loc, op0_codes);
77059241
RS
246 for (i = 0; i < NUM_RTX_CODE; i++)
247 codes[i] = TRISTATE_NOT (op0_codes[i]);
248 break;
249
250 case IF_THEN_ELSE:
251 /* a ? b : c accepts the same codes as (a & b) | (!a & c). */
c9f84f2e
RS
252 compute_test_codes (XEXP (exp, 0), loc, op0_codes);
253 compute_test_codes (XEXP (exp, 1), loc, op1_codes);
254 compute_test_codes (XEXP (exp, 2), loc, op2_codes);
77059241
RS
255 for (i = 0; i < NUM_RTX_CODE; i++)
256 codes[i] = TRISTATE_OR (TRISTATE_AND (op0_codes[i], op1_codes[i]),
257 TRISTATE_AND (TRISTATE_NOT (op0_codes[i]),
258 op2_codes[i]));
259 break;
260
261 case MATCH_CODE:
262 /* MATCH_CODE allows a specified list of codes. However, if it
263 does not apply to the top level of the expression, it does not
264 constrain the set of codes for the top level. */
265 if (XSTR (exp, 1)[0] != '\0')
266 {
267 memset (codes, Y, NUM_RTX_CODE);
268 break;
269 }
270
271 memset (codes, N, NUM_RTX_CODE);
272 {
273 const char *next_code = XSTR (exp, 0);
274 const char *code;
275
276 if (*next_code == '\0')
277 {
c9f84f2e 278 error_at (loc, "empty match_code expression");
77059241
RS
279 break;
280 }
281
282 while ((code = scan_comma_elt (&next_code)) != 0)
283 {
284 size_t n = next_code - code;
285 int found_it = 0;
286
287 for (i = 0; i < NUM_RTX_CODE; i++)
288 if (!strncmp (code, GET_RTX_NAME (i), n)
289 && GET_RTX_NAME (i)[n] == '\0')
290 {
291 codes[i] = Y;
292 found_it = 1;
293 break;
294 }
295 if (!found_it)
296 {
c9f84f2e
RS
297 error_at (loc, "match_code \"%.*s\" matches nothing",
298 (int) n, code);
77059241
RS
299 for (i = 0; i < NUM_RTX_CODE; i++)
300 if (!strncasecmp (code, GET_RTX_NAME (i), n)
301 && GET_RTX_NAME (i)[n] == '\0'
302 && !did_you_mean_codes[i])
303 {
304 did_you_mean_codes[i] = 1;
c9f84f2e
RS
305 message_at (loc, "(did you mean \"%s\"?)",
306 GET_RTX_NAME (i));
77059241
RS
307 }
308 }
309 }
310 }
311 break;
312
313 case MATCH_OPERAND:
314 /* MATCH_OPERAND disallows the set of codes that the named predicate
315 disallows, and is indeterminate for the codes that it does allow. */
316 {
317 struct pred_data *p = lookup_predicate (XSTR (exp, 1));
318 if (!p)
319 {
c9f84f2e
RS
320 error_at (loc, "reference to unknown predicate '%s'",
321 XSTR (exp, 1));
77059241
RS
322 break;
323 }
324 for (i = 0; i < NUM_RTX_CODE; i++)
325 codes[i] = p->codes[i] ? I : N;
326 }
327 break;
328
329
330 case MATCH_TEST:
331 /* (match_test WHATEVER) is completely indeterminate. */
332 memset (codes, I, NUM_RTX_CODE);
333 break;
334
335 default:
c9f84f2e
RS
336 error_at (loc, "'%s' cannot be used in predicates or constraints",
337 GET_RTX_NAME (GET_CODE (exp)));
77059241
RS
338 memset (codes, I, NUM_RTX_CODE);
339 break;
340 }
341}
342
343#undef TRISTATE_OR
344#undef TRISTATE_AND
345#undef TRISTATE_NOT
346
347/* Return true if NAME is a valid predicate name. */
348
349static bool
350valid_predicate_name_p (const char *name)
351{
352 const char *p;
353
354 if (!ISALPHA (name[0]) && name[0] != '_')
355 return false;
356 for (p = name + 1; *p; p++)
357 if (!ISALNUM (*p) && *p != '_')
358 return false;
359 return true;
360}
361
cc472607
RS
362/* Process define_predicate directive DESC, which appears at location LOC.
363 Compute the set of codes that can be matched, and record this as a known
364 predicate. */
77059241
RS
365
366static void
cc472607 367process_define_predicate (rtx desc, file_location loc)
77059241
RS
368{
369 struct pred_data *pred;
370 char codes[NUM_RTX_CODE];
371 int i;
372
373 if (!valid_predicate_name_p (XSTR (desc, 0)))
374 {
cc472607
RS
375 error_at (loc, "%s: predicate name must be a valid C function name",
376 XSTR (desc, 0));
77059241
RS
377 return;
378 }
379
380 pred = XCNEW (struct pred_data);
381 pred->name = XSTR (desc, 0);
382 pred->exp = XEXP (desc, 1);
383 pred->c_block = XSTR (desc, 2);
384 if (GET_CODE (desc) == DEFINE_SPECIAL_PREDICATE)
385 pred->special = true;
386
c9f84f2e 387 compute_test_codes (XEXP (desc, 1), loc, codes);
77059241
RS
388
389 for (i = 0; i < NUM_RTX_CODE; i++)
390 if (codes[i] != N)
391 add_predicate_code (pred, (enum rtx_code) i);
392
393 add_predicate (pred);
394}
395#undef I
396#undef N
397#undef Y
398\f
a406f566
MM
399/* Queue PATTERN on LIST_TAIL. Return the address of the new queue
400 element. */
3262c1f5 401
a406f566 402static struct queue_elem *
3d7aafde 403queue_pattern (rtx pattern, struct queue_elem ***list_tail,
cc472607 404 file_location loc)
3262c1f5 405{
c3284718 406 struct queue_elem *e = XNEW (struct queue_elem);
3262c1f5 407 e->data = pattern;
cc472607 408 e->loc = loc;
3262c1f5 409 e->next = NULL;
a406f566 410 e->split = NULL;
3262c1f5
RH
411 **list_tail = e;
412 *list_tail = &e->next;
a406f566 413 return e;
3262c1f5
RH
414}
415
477c104e
MK
416/* Remove element ELEM from QUEUE. */
417static void
418remove_from_queue (struct queue_elem *elem, struct queue_elem **queue)
419{
420 struct queue_elem *prev, *e;
421 prev = NULL;
422 for (e = *queue; e ; e = e->next)
423 {
424 if (e == elem)
425 break;
426 prev = e;
427 }
428 if (e == NULL)
429 return;
430
431 if (prev)
432 prev->next = elem->next;
433 else
434 *queue = elem->next;
435}
436
0bddee8e
BS
437/* Build a define_attr for an binary attribute with name NAME and
438 possible values "yes" and "no", and queue it. */
439static void
440add_define_attr (const char *name)
441{
c3284718 442 struct queue_elem *e = XNEW (struct queue_elem);
0bddee8e
BS
443 rtx t1 = rtx_alloc (DEFINE_ATTR);
444 XSTR (t1, 0) = name;
445 XSTR (t1, 1) = "no,yes";
446 XEXP (t1, 2) = rtx_alloc (CONST_STRING);
447 XSTR (XEXP (t1, 2), 0) = "yes";
448 e->data = t1;
cc472607 449 e->loc = file_location ("built-in", -1);
0bddee8e
BS
450 e->next = define_attr_queue;
451 define_attr_queue = e;
452
453}
454
c88c0d42
CP
455/* Recursively remove constraints from an rtx. */
456
457static void
3d7aafde 458remove_constraints (rtx part)
c88c0d42 459{
b3694847
SS
460 int i, j;
461 const char *format_ptr;
c88c0d42
CP
462
463 if (part == 0)
464 return;
465
466 if (GET_CODE (part) == MATCH_OPERAND)
467 XSTR (part, 2) = "";
468 else if (GET_CODE (part) == MATCH_SCRATCH)
469 XSTR (part, 1) = "";
470
471 format_ptr = GET_RTX_FORMAT (GET_CODE (part));
472
473 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
474 switch (*format_ptr++)
475 {
476 case 'e':
477 case 'u':
478 remove_constraints (XEXP (part, i));
479 break;
480 case 'E':
481 if (XVEC (part, i) != NULL)
482 for (j = 0; j < XVECLEN (part, i); j++)
483 remove_constraints (XVECEXP (part, i, j));
484 break;
485 }
486}
487
d91edf86 488/* Process a top level rtx in some way, queuing as appropriate. */
c88c0d42
CP
489
490static void
cc472607 491process_rtx (rtx desc, file_location loc)
3262c1f5
RH
492{
493 switch (GET_CODE (desc))
494 {
495 case DEFINE_INSN:
cc472607 496 queue_pattern (desc, &define_insn_tail, loc);
3262c1f5
RH
497 break;
498
499 case DEFINE_COND_EXEC:
cc472607 500 queue_pattern (desc, &define_cond_exec_tail, loc);
3262c1f5
RH
501 break;
502
477c104e 503 case DEFINE_SUBST:
cc472607 504 queue_pattern (desc, &define_subst_tail, loc);
477c104e
MK
505 break;
506
507 case DEFINE_SUBST_ATTR:
cc472607 508 queue_pattern (desc, &define_subst_attr_tail, loc);
477c104e
MK
509 break;
510
3262c1f5 511 case DEFINE_ATTR:
8f4fe86c 512 case DEFINE_ENUM_ATTR:
cc472607 513 queue_pattern (desc, &define_attr_tail, loc);
3262c1f5
RH
514 break;
515
e543e219
ZW
516 case DEFINE_PREDICATE:
517 case DEFINE_SPECIAL_PREDICATE:
cc472607 518 process_define_predicate (desc, loc);
77059241
RS
519 /* Fall through. */
520
f38840db
ZW
521 case DEFINE_CONSTRAINT:
522 case DEFINE_REGISTER_CONSTRAINT:
523 case DEFINE_MEMORY_CONSTRAINT:
9eb1ca69 524 case DEFINE_SPECIAL_MEMORY_CONSTRAINT:
f38840db 525 case DEFINE_ADDRESS_CONSTRAINT:
cc472607 526 queue_pattern (desc, &define_pred_tail, loc);
e543e219
ZW
527 break;
528
3262c1f5
RH
529 case DEFINE_INSN_AND_SPLIT:
530 {
531 const char *split_cond;
20217ac1
KG
532 rtx split;
533 rtvec attr;
de4bfbcb 534 int i;
a406f566
MM
535 struct queue_elem *insn_elem;
536 struct queue_elem *split_elem;
3262c1f5 537
dc297297 538 /* Create a split with values from the insn_and_split. */
3262c1f5 539 split = rtx_alloc (DEFINE_SPLIT);
de4bfbcb
RH
540
541 i = XVECLEN (desc, 1);
fbd40359 542 XVEC (split, 0) = rtvec_alloc (i);
de4bfbcb
RH
543 while (--i >= 0)
544 {
545 XVECEXP (split, 0, i) = copy_rtx (XVECEXP (desc, 1, i));
546 remove_constraints (XVECEXP (split, 0, i));
547 }
3262c1f5
RH
548
549 /* If the split condition starts with "&&", append it to the
550 insn condition to create the new split condition. */
551 split_cond = XSTR (desc, 4);
552 if (split_cond[0] == '&' && split_cond[1] == '&')
7445392c 553 {
d2a3ce4e 554 copy_md_ptr_loc (split_cond + 2, split_cond);
7445392c
RS
555 split_cond = join_c_conditions (XSTR (desc, 2), split_cond + 2);
556 }
3262c1f5
RH
557 XSTR (split, 1) = split_cond;
558 XVEC (split, 2) = XVEC (desc, 5);
559 XSTR (split, 3) = XSTR (desc, 6);
560
561 /* Fix up the DEFINE_INSN. */
ee138cf8 562 attr = XVEC (desc, 7);
3262c1f5 563 PUT_CODE (desc, DEFINE_INSN);
ee138cf8 564 XVEC (desc, 4) = attr;
3262c1f5
RH
565
566 /* Queue them. */
cc472607
RS
567 insn_elem = queue_pattern (desc, &define_insn_tail, loc);
568 split_elem = queue_pattern (split, &other_tail, loc);
a406f566 569 insn_elem->split = split_elem;
3262c1f5
RH
570 break;
571 }
572
573 default:
cc472607 574 queue_pattern (desc, &other_tail, loc);
3262c1f5 575 break;
c88c0d42
CP
576 }
577}
3916d6d8 578\f
3262c1f5
RH
579/* Return true if attribute PREDICABLE is true for ELEM, which holds
580 a DEFINE_INSN. */
581
582static int
3d7aafde 583is_predicable (struct queue_elem *elem)
3262c1f5
RH
584{
585 rtvec vec = XVEC (elem->data, 4);
586 const char *value;
587 int i;
588
589 if (! vec)
590 return predicable_default;
591
592 for (i = GET_NUM_ELEM (vec) - 1; i >= 0; --i)
593 {
594 rtx sub = RTVEC_ELT (vec, i);
595 switch (GET_CODE (sub))
596 {
597 case SET_ATTR:
598 if (strcmp (XSTR (sub, 0), "predicable") == 0)
599 {
600 value = XSTR (sub, 1);
601 goto found;
602 }
603 break;
604
605 case SET_ATTR_ALTERNATIVE:
606 if (strcmp (XSTR (sub, 0), "predicable") == 0)
607 {
cc472607 608 error_at (elem->loc, "multiple alternatives for `predicable'");
3262c1f5
RH
609 return 0;
610 }
611 break;
612
613 case SET:
614 if (GET_CODE (SET_DEST (sub)) != ATTR
615 || strcmp (XSTR (SET_DEST (sub), 0), "predicable") != 0)
616 break;
617 sub = SET_SRC (sub);
618 if (GET_CODE (sub) == CONST_STRING)
619 {
620 value = XSTR (sub, 0);
621 goto found;
622 }
623
624 /* ??? It would be possible to handle this if we really tried.
625 It's not easy though, and I'm not going to bother until it
626 really proves necessary. */
cc472607 627 error_at (elem->loc, "non-constant value for `predicable'");
3262c1f5
RH
628 return 0;
629
630 default:
b2d59f6f 631 gcc_unreachable ();
3262c1f5
RH
632 }
633 }
634
635 return predicable_default;
636
637 found:
0bddee8e
BS
638 /* Find out which value we're looking at. Multiple alternatives means at
639 least one is predicable. */
3262c1f5 640 if (strchr (value, ',') != NULL)
0bddee8e 641 return 1;
3262c1f5
RH
642 if (strcmp (value, predicable_true) == 0)
643 return 1;
644 if (strcmp (value, predicable_false) == 0)
645 return 0;
646
cc472607 647 error_at (elem->loc, "unknown value `%s' for `predicable' attribute", value);
3262c1f5
RH
648 return 0;
649}
650
477c104e
MK
651/* Find attribute SUBST in ELEM and assign NEW_VALUE to it. */
652static void
653change_subst_attribute (struct queue_elem *elem,
654 struct queue_elem *subst_elem,
655 const char *new_value)
656{
657 rtvec attrs_vec = XVEC (elem->data, 4);
658 const char *subst_name = XSTR (subst_elem->data, 0);
659 int i;
660
661 if (! attrs_vec)
662 return;
663
664 for (i = GET_NUM_ELEM (attrs_vec) - 1; i >= 0; --i)
665 {
666 rtx cur_attr = RTVEC_ELT (attrs_vec, i);
667 if (GET_CODE (cur_attr) != SET_ATTR)
668 continue;
669 if (strcmp (XSTR (cur_attr, 0), subst_name) == 0)
670 {
671 XSTR (cur_attr, 1) = new_value;
672 return;
673 }
674 }
675}
676
677/* Return true if ELEM has the attribute with the name of DEFINE_SUBST
678 represented by SUBST_ELEM and this attribute has value SUBST_TRUE.
679 DEFINE_SUBST isn't applied to patterns without such attribute. In other
680 words, we suppose the default value of the attribute to be 'no' since it is
681 always generated automaticaly in read-rtl.c. */
682static bool
683has_subst_attribute (struct queue_elem *elem, struct queue_elem *subst_elem)
684{
685 rtvec attrs_vec = XVEC (elem->data, 4);
686 const char *value, *subst_name = XSTR (subst_elem->data, 0);
687 int i;
688
689 if (! attrs_vec)
690 return false;
691
692 for (i = GET_NUM_ELEM (attrs_vec) - 1; i >= 0; --i)
693 {
694 rtx cur_attr = RTVEC_ELT (attrs_vec, i);
695 switch (GET_CODE (cur_attr))
696 {
697 case SET_ATTR:
698 if (strcmp (XSTR (cur_attr, 0), subst_name) == 0)
699 {
700 value = XSTR (cur_attr, 1);
701 goto found;
702 }
703 break;
704
705 case SET:
706 if (GET_CODE (SET_DEST (cur_attr)) != ATTR
707 || strcmp (XSTR (SET_DEST (cur_attr), 0), subst_name) != 0)
708 break;
709 cur_attr = SET_SRC (cur_attr);
710 if (GET_CODE (cur_attr) == CONST_STRING)
711 {
712 value = XSTR (cur_attr, 0);
713 goto found;
714 }
715
716 /* Only (set_attr "subst" "yes/no") and
717 (set (attr "subst" (const_string "yes/no")))
718 are currently allowed. */
cc472607 719 error_at (elem->loc, "unsupported value for `%s'", subst_name);
477c104e
MK
720 return false;
721
722 case SET_ATTR_ALTERNATIVE:
cc472607
RS
723 error_at (elem->loc,
724 "%s: `set_attr_alternative' is unsupported by "
725 "`define_subst'", XSTR (elem->data, 0));
477c104e
MK
726 return false;
727
728
729 default:
730 gcc_unreachable ();
731 }
732 }
733
734 return false;
735
736 found:
737 if (strcmp (value, subst_true) == 0)
738 return true;
739 if (strcmp (value, subst_false) == 0)
740 return false;
741
cc472607
RS
742 error_at (elem->loc, "unknown value `%s' for `%s' attribute",
743 value, subst_name);
477c104e
MK
744 return false;
745}
746
747/* Compare RTL-template of original define_insn X to input RTL-template of
748 define_subst PT. Return 1 if the templates match, 0 otherwise.
749 During the comparison, the routine also fills global_array OPERAND_DATA. */
750static bool
cc472607 751subst_pattern_match (rtx x, rtx pt, file_location loc)
477c104e
MK
752{
753 RTX_CODE code, code_pt;
754 int i, j, len;
755 const char *fmt, *pred_name;
756
757 code = GET_CODE (x);
758 code_pt = GET_CODE (pt);
759
760 if (code_pt == MATCH_OPERAND)
761 {
762 /* MATCH_DUP, and MATCH_OP_DUP don't have a specified mode, so we
763 always accept them. */
764 if (GET_MODE (pt) != VOIDmode && GET_MODE (x) != GET_MODE (pt)
765 && (code != MATCH_DUP && code != MATCH_OP_DUP))
766 return false; /* Modes don't match. */
767
768 if (code == MATCH_OPERAND)
769 {
770 pred_name = XSTR (pt, 1);
771 if (pred_name[0] != 0)
772 {
773 const struct pred_data *pred_pt = lookup_predicate (pred_name);
774 if (!pred_pt || pred_pt != lookup_predicate (XSTR (x, 1)))
775 return false; /* Predicates don't match. */
776 }
777 }
778
779 gcc_assert (XINT (pt, 0) >= 0 && XINT (pt, 0) < MAX_OPERANDS);
780 operand_data[XINT (pt, 0)] = x;
781 return true;
782 }
783
784 if (code_pt == MATCH_OPERATOR)
785 {
786 int x_vecexp_pos = -1;
787
788 /* Compare modes. */
789 if (GET_MODE (pt) != VOIDmode && GET_MODE (x) != GET_MODE (pt))
790 return false;
791
792 /* In case X is also match_operator, compare predicates. */
793 if (code == MATCH_OPERATOR)
794 {
795 pred_name = XSTR (pt, 1);
796 if (pred_name[0] != 0)
797 {
798 const struct pred_data *pred_pt = lookup_predicate (pred_name);
799 if (!pred_pt || pred_pt != lookup_predicate (XSTR (x, 1)))
800 return false;
801 }
802 }
803
804 /* Compare operands.
805 MATCH_OPERATOR in input template could match in original template
806 either 1) MATCH_OPERAND, 2) UNSPEC, 3) ordinary operation (like PLUS).
807 In the first case operands are at (XVECEXP (x, 2, j)), in the second
808 - at (XVECEXP (x, 0, j)), in the last one - (XEXP (x, j)).
809 X_VECEXP_POS variable shows, where to look for these operands. */
810 if (code == UNSPEC
811 || code == UNSPEC_VOLATILE)
812 x_vecexp_pos = 0;
813 else if (code == MATCH_OPERATOR)
814 x_vecexp_pos = 2;
815 else
816 x_vecexp_pos = -1;
817
818 /* MATCH_OPERATOR or UNSPEC case. */
819 if (x_vecexp_pos >= 0)
820 {
821 /* Compare operands number in X and PT. */
822 if (XVECLEN (x, x_vecexp_pos) != XVECLEN (pt, 2))
823 return false;
824 for (j = 0; j < XVECLEN (pt, 2); j++)
825 if (!subst_pattern_match (XVECEXP (x, x_vecexp_pos, j),
cc472607 826 XVECEXP (pt, 2, j), loc))
477c104e
MK
827 return false;
828 }
829
830 /* Ordinary operator. */
831 else
832 {
833 /* Compare operands number in X and PT.
834 We count operands differently for X and PT since we compare
835 an operator (with operands directly in RTX) and MATCH_OPERATOR
836 (that has a vector with operands). */
837 if (GET_RTX_LENGTH (code) != XVECLEN (pt, 2))
838 return false;
839 for (j = 0; j < XVECLEN (pt, 2); j++)
cc472607 840 if (!subst_pattern_match (XEXP (x, j), XVECEXP (pt, 2, j), loc))
477c104e
MK
841 return false;
842 }
843
844 /* Store the operand to OPERAND_DATA array. */
845 gcc_assert (XINT (pt, 0) >= 0 && XINT (pt, 0) < MAX_OPERANDS);
846 operand_data[XINT (pt, 0)] = x;
847 return true;
848 }
849
850 if (code_pt == MATCH_PAR_DUP
851 || code_pt == MATCH_DUP
852 || code_pt == MATCH_OP_DUP
853 || code_pt == MATCH_SCRATCH
854 || code_pt == MATCH_PARALLEL)
855 {
856 /* Currently interface for these constructions isn't defined -
857 probably they aren't needed in input template of define_subst at all.
858 So, for now their usage in define_subst is forbidden. */
cc472607
RS
859 error_at (loc, "%s cannot be used in define_subst",
860 GET_RTX_NAME (code_pt));
477c104e
MK
861 }
862
863 gcc_assert (code != MATCH_PAR_DUP
864 && code_pt != MATCH_DUP
865 && code_pt != MATCH_OP_DUP
866 && code_pt != MATCH_SCRATCH
867 && code_pt != MATCH_PARALLEL
868 && code_pt != MATCH_OPERAND
869 && code_pt != MATCH_OPERATOR);
870 /* If PT is none of the handled above, then we match only expressions with
871 the same code in X. */
872 if (code != code_pt)
873 return false;
874
875 fmt = GET_RTX_FORMAT (code_pt);
876 len = GET_RTX_LENGTH (code_pt);
877
878 for (i = 0; i < len; i++)
879 {
880 if (fmt[i] == '0')
881 break;
882
883 switch (fmt[i])
884 {
9fccb335 885 case 'i': case 'r': case 'w': case 's':
477c104e
MK
886 continue;
887
888 case 'e': case 'u':
cc472607 889 if (!subst_pattern_match (XEXP (x, i), XEXP (pt, i), loc))
477c104e
MK
890 return false;
891 break;
892 case 'E':
893 {
894 if (XVECLEN (x, i) != XVECLEN (pt, i))
895 return false;
896 for (j = 0; j < XVECLEN (pt, i); j++)
cc472607
RS
897 if (!subst_pattern_match (XVECEXP (x, i, j),
898 XVECEXP (pt, i, j), loc))
477c104e
MK
899 return false;
900 break;
901 }
902 default:
903 gcc_unreachable ();
904 }
905 }
906
907 return true;
908}
909
3262c1f5
RH
910/* Examine the attribute "predicable"; discover its boolean values
911 and its default. */
912
913static void
3d7aafde 914identify_predicable_attribute (void)
3262c1f5
RH
915{
916 struct queue_elem *elem;
d6edb99e 917 char *p_true, *p_false;
3262c1f5 918 const char *value;
3262c1f5
RH
919
920 /* Look for the DEFINE_ATTR for `predicable', which must exist. */
921 for (elem = define_attr_queue; elem ; elem = elem->next)
922 if (strcmp (XSTR (elem->data, 0), "predicable") == 0)
923 goto found;
924
cc472607
RS
925 error_at (define_cond_exec_queue->loc,
926 "attribute `predicable' not defined");
3262c1f5
RH
927 return;
928
929 found:
930 value = XSTR (elem->data, 1);
1dcd444b 931 p_false = xstrdup (value);
d6edb99e
ZW
932 p_true = strchr (p_false, ',');
933 if (p_true == NULL || strchr (++p_true, ',') != NULL)
3262c1f5 934 {
cc472607 935 error_at (elem->loc, "attribute `predicable' is not a boolean");
04695783 936 free (p_false);
3262c1f5
RH
937 return;
938 }
d6edb99e 939 p_true[-1] = '\0';
3262c1f5 940
d6edb99e
ZW
941 predicable_true = p_true;
942 predicable_false = p_false;
3262c1f5
RH
943
944 switch (GET_CODE (XEXP (elem->data, 2)))
945 {
946 case CONST_STRING:
947 value = XSTR (XEXP (elem->data, 2), 0);
948 break;
949
950 case CONST:
cc472607 951 error_at (elem->loc, "attribute `predicable' cannot be const");
04695783 952 free (p_false);
3262c1f5
RH
953 return;
954
955 default:
cc472607
RS
956 error_at (elem->loc,
957 "attribute `predicable' must have a constant default");
04695783 958 free (p_false);
3262c1f5
RH
959 return;
960 }
961
d6edb99e 962 if (strcmp (value, p_true) == 0)
3262c1f5 963 predicable_default = 1;
d6edb99e 964 else if (strcmp (value, p_false) == 0)
3262c1f5
RH
965 predicable_default = 0;
966 else
967 {
cc472607
RS
968 error_at (elem->loc, "unknown value `%s' for `predicable' attribute",
969 value);
04695783 970 free (p_false);
3262c1f5
RH
971 }
972}
973
974/* Return the number of alternatives in constraint S. */
975
976static int
3d7aafde 977n_alternatives (const char *s)
3262c1f5
RH
978{
979 int n = 1;
980
981 if (s)
982 while (*s)
983 n += (*s++ == ',');
984
985 return n;
986}
987
477c104e
MK
988/* The routine scans rtl PATTERN, find match_operand in it and counts
989 number of alternatives. If PATTERN contains several match_operands
990 with different number of alternatives, error is emitted, and the
991 routine returns 0. If all match_operands in PATTERN have the same
992 number of alternatives, it's stored in N_ALT, and the routine returns 1.
cc472607 993 LOC is the location of PATTERN, for error reporting. */
477c104e 994static int
cc472607 995get_alternatives_number (rtx pattern, int *n_alt, file_location loc)
477c104e
MK
996{
997 const char *fmt;
998 enum rtx_code code;
999 int i, j, len;
1000
1001 if (!n_alt)
1002 return 0;
1003
1004 code = GET_CODE (pattern);
1005 switch (code)
1006 {
1007 case MATCH_OPERAND:
1008 i = n_alternatives (XSTR (pattern, 2));
1009 /* n_alternatives returns 1 if constraint string is empty -
1010 here we fix it up. */
1011 if (!*(XSTR (pattern, 2)))
1012 i = 0;
1013 if (*n_alt <= 0)
1014 *n_alt = i;
1015
1016 else if (i && i != *n_alt)
1017 {
cc472607
RS
1018 error_at (loc, "wrong number of alternatives in operand %d",
1019 XINT (pattern, 0));
477c104e
MK
1020 return 0;
1021 }
1022
1023 default:
1024 break;
1025 }
1026
1027 fmt = GET_RTX_FORMAT (code);
1028 len = GET_RTX_LENGTH (code);
1029 for (i = 0; i < len; i++)
1030 {
1031 switch (fmt[i])
1032 {
1033 case 'e': case 'u':
cc472607
RS
1034 if (!get_alternatives_number (XEXP (pattern, i), n_alt, loc))
1035 return 0;
477c104e
MK
1036 break;
1037
1038 case 'V':
1039 if (XVEC (pattern, i) == NULL)
1040 break;
191816a3 1041 /* FALLTHRU */
477c104e
MK
1042
1043 case 'E':
1044 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
cc472607
RS
1045 if (!get_alternatives_number (XVECEXP (pattern, i, j), n_alt, loc))
1046 return 0;
477c104e
MK
1047 break;
1048
9fccb335 1049 case 'i': case 'r': case 'w': case '0': case 's': case 'S': case 'T':
477c104e
MK
1050 break;
1051
1052 default:
1053 gcc_unreachable ();
1054 }
1055 }
1056 return 1;
1057}
1058
3262c1f5
RH
1059/* Determine how many alternatives there are in INSN, and how many
1060 operands. */
1061
1062static void
3d7aafde 1063collect_insn_data (rtx pattern, int *palt, int *pmax)
3262c1f5
RH
1064{
1065 const char *fmt;
1066 enum rtx_code code;
1067 int i, j, len;
1068
1069 code = GET_CODE (pattern);
1070 switch (code)
1071 {
1072 case MATCH_OPERAND:
b7e2dd6f
BS
1073 case MATCH_SCRATCH:
1074 i = n_alternatives (XSTR (pattern, code == MATCH_SCRATCH ? 1 : 2));
892ecf92 1075 *palt = (i > *palt ? i : *palt);
5d3cc252 1076 /* Fall through. */
3262c1f5
RH
1077
1078 case MATCH_OPERATOR:
3262c1f5 1079 case MATCH_PARALLEL:
3262c1f5
RH
1080 i = XINT (pattern, 0);
1081 if (i > *pmax)
1082 *pmax = i;
1083 break;
1084
1085 default:
1086 break;
1087 }
1088
1089 fmt = GET_RTX_FORMAT (code);
1090 len = GET_RTX_LENGTH (code);
1091 for (i = 0; i < len; i++)
1092 {
1093 switch (fmt[i])
1094 {
1095 case 'e': case 'u':
1096 collect_insn_data (XEXP (pattern, i), palt, pmax);
1097 break;
1098
1099 case 'V':
1100 if (XVEC (pattern, i) == NULL)
1101 break;
5d3cc252 1102 /* Fall through. */
3262c1f5
RH
1103 case 'E':
1104 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
1105 collect_insn_data (XVECEXP (pattern, i, j), palt, pmax);
1106 break;
1107
9fccb335 1108 case 'i': case 'r': case 'w': case '0': case 's': case 'S': case 'T':
3262c1f5
RH
1109 break;
1110
1111 default:
b2d59f6f 1112 gcc_unreachable ();
3262c1f5
RH
1113 }
1114 }
1115}
1116
1117static rtx
cc472607
RS
1118alter_predicate_for_insn (rtx pattern, int alt, int max_op,
1119 file_location loc)
3262c1f5
RH
1120{
1121 const char *fmt;
1122 enum rtx_code code;
1123 int i, j, len;
1124
1125 code = GET_CODE (pattern);
1126 switch (code)
1127 {
1128 case MATCH_OPERAND:
1129 {
1130 const char *c = XSTR (pattern, 2);
1131
1132 if (n_alternatives (c) != 1)
1133 {
cc472607
RS
1134 error_at (loc, "too many alternatives for operand %d",
1135 XINT (pattern, 0));
3262c1f5
RH
1136 return NULL;
1137 }
1138
1139 /* Replicate C as needed to fill out ALT alternatives. */
1140 if (c && *c && alt > 1)
1141 {
1142 size_t c_len = strlen (c);
1143 size_t len = alt * (c_len + 1);
477c104e 1144 char *new_c = XNEWVEC (char, len);
3262c1f5
RH
1145
1146 memcpy (new_c, c, c_len);
1147 for (i = 1; i < alt; ++i)
1148 {
1149 new_c[i * (c_len + 1) - 1] = ',';
1150 memcpy (&new_c[i * (c_len + 1)], c, c_len);
1151 }
1152 new_c[len - 1] = '\0';
1153 XSTR (pattern, 2) = new_c;
1154 }
1155 }
5d3cc252 1156 /* Fall through. */
3262c1f5
RH
1157
1158 case MATCH_OPERATOR:
1159 case MATCH_SCRATCH:
1160 case MATCH_PARALLEL:
3262c1f5
RH
1161 XINT (pattern, 0) += max_op;
1162 break;
1163
1164 default:
1165 break;
1166 }
1167
1168 fmt = GET_RTX_FORMAT (code);
1169 len = GET_RTX_LENGTH (code);
1170 for (i = 0; i < len; i++)
1171 {
1172 rtx r;
1173
1174 switch (fmt[i])
1175 {
1176 case 'e': case 'u':
cc472607 1177 r = alter_predicate_for_insn (XEXP (pattern, i), alt, max_op, loc);
3262c1f5
RH
1178 if (r == NULL)
1179 return r;
1180 break;
1181
1182 case 'E':
1183 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
1184 {
1185 r = alter_predicate_for_insn (XVECEXP (pattern, i, j),
cc472607 1186 alt, max_op, loc);
3262c1f5
RH
1187 if (r == NULL)
1188 return r;
1189 }
1190 break;
1191
9fccb335 1192 case 'i': case 'r': case 'w': case '0': case 's':
3262c1f5
RH
1193 break;
1194
1195 default:
b2d59f6f 1196 gcc_unreachable ();
3262c1f5
RH
1197 }
1198 }
1199
1200 return pattern;
1201}
1202
477c104e
MK
1203/* Duplicate constraints in PATTERN. If pattern is from original
1204 rtl-template, we need to duplicate each alternative - for that we
1205 need to use duplicate_each_alternative () as a functor ALTER.
1206 If pattern is from output-pattern of define_subst, we need to
1207 duplicate constraints in another way - with duplicate_alternatives ().
1208 N_DUP is multiplication factor. */
1209static rtx
1210alter_constraints (rtx pattern, int n_dup, constraints_handler_t alter)
1211{
1212 const char *fmt;
1213 enum rtx_code code;
1214 int i, j, len;
1215
1216 code = GET_CODE (pattern);
1217 switch (code)
1218 {
1219 case MATCH_OPERAND:
1220 XSTR (pattern, 2) = alter (XSTR (pattern, 2), n_dup);
1221 break;
1222
1223 default:
1224 break;
1225 }
1226
1227 fmt = GET_RTX_FORMAT (code);
1228 len = GET_RTX_LENGTH (code);
1229 for (i = 0; i < len; i++)
1230 {
1231 rtx r;
1232
1233 switch (fmt[i])
1234 {
1235 case 'e': case 'u':
1236 r = alter_constraints (XEXP (pattern, i), n_dup, alter);
1237 if (r == NULL)
1238 return r;
1239 break;
1240
1241 case 'E':
1242 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
1243 {
1244 r = alter_constraints (XVECEXP (pattern, i, j), n_dup, alter);
1245 if (r == NULL)
1246 return r;
1247 }
1248 break;
1249
9fccb335 1250 case 'i': case 'r': case 'w': case '0': case 's':
477c104e
MK
1251 break;
1252
1253 default:
1254 break;
1255 }
1256 }
1257
1258 return pattern;
1259}
1260
3262c1f5 1261static const char *
3d7aafde
AJ
1262alter_test_for_insn (struct queue_elem *ce_elem,
1263 struct queue_elem *insn_elem)
3262c1f5 1264{
7445392c
RS
1265 return join_c_conditions (XSTR (ce_elem->data, 1),
1266 XSTR (insn_elem->data, 2));
3262c1f5
RH
1267}
1268
0bddee8e
BS
1269/* Modify VAL, which is an attribute expression for the "enabled" attribute,
1270 to take "ce_enabled" into account. Return the new expression. */
1271static rtx
1272modify_attr_enabled_ce (rtx val)
1273{
1274 rtx eq_attr, str;
1275 rtx ite;
1276 eq_attr = rtx_alloc (EQ_ATTR);
1277 ite = rtx_alloc (IF_THEN_ELSE);
1278 str = rtx_alloc (CONST_STRING);
1279
1280 XSTR (eq_attr, 0) = "ce_enabled";
1281 XSTR (eq_attr, 1) = "yes";
1282 XSTR (str, 0) = "no";
1283 XEXP (ite, 0) = eq_attr;
1284 XEXP (ite, 1) = val;
1285 XEXP (ite, 2) = str;
1286
1287 return ite;
1288}
1289
1290/* Alter the attribute vector of INSN, which is a COND_EXEC variant created
1291 from a define_insn pattern. We must modify the "predicable" attribute
1292 to be named "ce_enabled", and also change any "enabled" attribute that's
1293 present so that it takes ce_enabled into account.
1294 We rely on the fact that INSN was created with copy_rtx, and modify data
1295 in-place. */
1296
1297static void
1298alter_attrs_for_insn (rtx insn)
1299{
1300 static bool global_changes_made = false;
1301 rtvec vec = XVEC (insn, 4);
1302 rtvec new_vec;
1303 rtx val, set;
1304 int num_elem;
1305 int predicable_idx = -1;
1306 int enabled_idx = -1;
1307 int i;
1308
1309 if (! vec)
1310 return;
1311
1312 num_elem = GET_NUM_ELEM (vec);
1313 for (i = num_elem - 1; i >= 0; --i)
1314 {
1315 rtx sub = RTVEC_ELT (vec, i);
1316 switch (GET_CODE (sub))
1317 {
1318 case SET_ATTR:
1319 if (strcmp (XSTR (sub, 0), "predicable") == 0)
1320 {
1321 predicable_idx = i;
1322 XSTR (sub, 0) = "ce_enabled";
1323 }
1324 else if (strcmp (XSTR (sub, 0), "enabled") == 0)
1325 {
1326 enabled_idx = i;
1327 XSTR (sub, 0) = "nonce_enabled";
1328 }
1329 break;
1330
1331 case SET_ATTR_ALTERNATIVE:
1332 if (strcmp (XSTR (sub, 0), "predicable") == 0)
1333 /* We already give an error elsewhere. */
1334 return;
1335 else if (strcmp (XSTR (sub, 0), "enabled") == 0)
1336 {
1337 enabled_idx = i;
1338 XSTR (sub, 0) = "nonce_enabled";
1339 }
1340 break;
1341
1342 case SET:
1343 if (GET_CODE (SET_DEST (sub)) != ATTR)
1344 break;
1345 if (strcmp (XSTR (SET_DEST (sub), 0), "predicable") == 0)
1346 {
1347 sub = SET_SRC (sub);
1348 if (GET_CODE (sub) == CONST_STRING)
1349 {
1350 predicable_idx = i;
1351 XSTR (sub, 0) = "ce_enabled";
1352 }
1353 else
1354 /* We already give an error elsewhere. */
1355 return;
1356 break;
1357 }
1358 if (strcmp (XSTR (SET_DEST (sub), 0), "enabled") == 0)
1359 {
1360 enabled_idx = i;
1361 XSTR (SET_DEST (sub), 0) = "nonce_enabled";
1362 }
1363 break;
1364
1365 default:
1366 gcc_unreachable ();
1367 }
1368 }
1369 if (predicable_idx == -1)
1370 return;
1371
1372 if (!global_changes_made)
1373 {
1374 struct queue_elem *elem;
477c104e 1375
0bddee8e
BS
1376 global_changes_made = true;
1377 add_define_attr ("ce_enabled");
1378 add_define_attr ("nonce_enabled");
1379
1380 for (elem = define_attr_queue; elem ; elem = elem->next)
1381 if (strcmp (XSTR (elem->data, 0), "enabled") == 0)
1382 {
1383 XEXP (elem->data, 2)
1384 = modify_attr_enabled_ce (XEXP (elem->data, 2));
1385 }
1386 }
1387 if (enabled_idx == -1)
1388 return;
1389
1390 new_vec = rtvec_alloc (num_elem + 1);
1391 for (i = 0; i < num_elem; i++)
1392 RTVEC_ELT (new_vec, i) = RTVEC_ELT (vec, i);
1393 val = rtx_alloc (IF_THEN_ELSE);
1394 XEXP (val, 0) = rtx_alloc (EQ_ATTR);
1395 XEXP (val, 1) = rtx_alloc (CONST_STRING);
1396 XEXP (val, 2) = rtx_alloc (CONST_STRING);
1397 XSTR (XEXP (val, 0), 0) = "nonce_enabled";
1398 XSTR (XEXP (val, 0), 1) = "yes";
1399 XSTR (XEXP (val, 1), 0) = "yes";
1400 XSTR (XEXP (val, 2), 0) = "no";
1401 set = rtx_alloc (SET);
1402 SET_DEST (set) = rtx_alloc (ATTR);
1403 XSTR (SET_DEST (set), 0) = "enabled";
1404 SET_SRC (set) = modify_attr_enabled_ce (val);
1405 RTVEC_ELT (new_vec, i) = set;
1406 XVEC (insn, 4) = new_vec;
1407}
1408
477c104e
MK
1409/* As number of constraints is changed after define_subst, we need to
1410 process attributes as well - we need to duplicate them the same way
1411 that we duplicated constraints in original pattern
1412 ELEM is a queue element, containing our rtl-template,
1413 N_DUP - multiplication factor. */
1414static void
1415alter_attrs_for_subst_insn (struct queue_elem * elem, int n_dup)
3262c1f5 1416{
477c104e
MK
1417 rtvec vec = XVEC (elem->data, 4);
1418 int num_elem;
1419 int i;
1420
1421 if (n_dup < 2 || ! vec)
1422 return;
1423
1424 num_elem = GET_NUM_ELEM (vec);
1425 for (i = num_elem - 1; i >= 0; --i)
3262c1f5 1426 {
477c104e
MK
1427 rtx sub = RTVEC_ELT (vec, i);
1428 switch (GET_CODE (sub))
3262c1f5 1429 {
477c104e
MK
1430 case SET_ATTR:
1431 if (strchr (XSTR (sub, 1), ',') != NULL)
1432 XSTR (sub, 1) = duplicate_alternatives (XSTR (sub, 1), n_dup);
1433 break;
1434
1435 case SET_ATTR_ALTERNATIVE:
1436 case SET:
cc472607
RS
1437 error_at (elem->loc,
1438 "%s: `define_subst' does not support attributes "
1439 "assigned by `set' and `set_attr_alternative'",
1440 XSTR (elem->data, 0));
477c104e
MK
1441 return;
1442
1443 default:
1444 gcc_unreachable ();
1445 }
1446 }
1447}
1448
1449/* Adjust all of the operand numbers in SRC to match the shift they'll
1450 get from an operand displacement of DISP. Return a pointer after the
1451 adjusted string. */
1452
1453static char *
1454shift_output_template (char *dest, const char *src, int disp)
1455{
1456 while (*src)
1457 {
1458 char c = *src++;
1459 *dest++ = c;
1460 if (c == '%')
1461 {
1462 c = *src++;
1463 if (ISDIGIT ((unsigned char) c))
1464 c += disp;
1465 else if (ISALPHA (c))
1466 {
53ed1a12 1467 *dest++ = c;
1ad463f4 1468 c = *src++ + disp;
3262c1f5 1469 }
53ed1a12 1470 *dest++ = c;
3262c1f5
RH
1471 }
1472 }
1473
53ed1a12 1474 return dest;
3262c1f5
RH
1475}
1476
1477static const char *
3d7aafde
AJ
1478alter_output_for_insn (struct queue_elem *ce_elem,
1479 struct queue_elem *insn_elem,
1480 int alt, int max_op)
3262c1f5
RH
1481{
1482 const char *ce_out, *insn_out;
53ed1a12 1483 char *result, *p;
3262c1f5
RH
1484 size_t len, ce_len, insn_len;
1485
1486 /* ??? Could coordinate with genoutput to not duplicate code here. */
1487
1488 ce_out = XSTR (ce_elem->data, 2);
66621f9e 1489 insn_out = XTMPL (insn_elem->data, 3);
3262c1f5
RH
1490 if (!ce_out || *ce_out == '\0')
1491 return insn_out;
1492
1493 ce_len = strlen (ce_out);
1494 insn_len = strlen (insn_out);
1495
1496 if (*insn_out == '*')
1497 /* You must take care of the predicate yourself. */
1498 return insn_out;
1499
1500 if (*insn_out == '@')
1501 {
1502 len = (ce_len + 1) * alt + insn_len + 1;
477c104e 1503 p = result = XNEWVEC (char, len);
3262c1f5
RH
1504
1505 do
1506 {
1507 do
1508 *p++ = *insn_out++;
1509 while (ISSPACE ((unsigned char) *insn_out));
1510
1511 if (*insn_out != '#')
1512 {
1513 p = shift_output_template (p, ce_out, max_op);
1514 *p++ = ' ';
1515 }
1516
1517 do
1518 *p++ = *insn_out++;
1519 while (*insn_out && *insn_out != '\n');
1520 }
1521 while (*insn_out);
1522 *p = '\0';
1523 }
1524 else
1525 {
1526 len = ce_len + 1 + insn_len + 1;
53ed1a12 1527 result = XNEWVEC (char, len);
3262c1f5 1528
53ed1a12 1529 p = shift_output_template (result, ce_out, max_op);
3262c1f5
RH
1530 *p++ = ' ';
1531 memcpy (p, insn_out, insn_len + 1);
1532 }
1533
53ed1a12 1534 return result;
3262c1f5
RH
1535}
1536
477c104e
MK
1537/* From string STR "a,b,c" produce "a,b,c,a,b,c,a,b,c", i.e. original
1538 string, duplicated N_DUP times. */
1539
1540static const char *
1541duplicate_alternatives (const char * str, int n_dup)
1542{
1543 int i, len, new_len;
1544 char *result, *sp;
1545 const char *cp;
1546
1547 if (n_dup < 2)
1548 return str;
1549
1550 while (ISSPACE (*str))
1551 str++;
1552
1553 if (*str == '\0')
1554 return str;
1555
1556 cp = str;
1557 len = strlen (str);
1558 new_len = (len + 1) * n_dup;
1559
1560 sp = result = XNEWVEC (char, new_len);
1561
1562 /* Global modifier characters mustn't be duplicated: skip if found. */
1563 if (*cp == '=' || *cp == '+' || *cp == '%')
1564 {
1565 *sp++ = *cp++;
1566 len--;
1567 }
1568
1569 /* Copy original constraints N_DUP times. */
1570 for (i = 0; i < n_dup; i++, sp += len+1)
1571 {
1572 memcpy (sp, cp, len);
1573 *(sp+len) = (i == n_dup - 1) ? '\0' : ',';
1574 }
1575
1576 return result;
1577}
1578
1579/* From string STR "a,b,c" produce "a,a,a,b,b,b,c,c,c", i.e. string where
1580 each alternative from the original string is duplicated N_DUP times. */
1581static const char *
1582duplicate_each_alternative (const char * str, int n_dup)
1583{
1584 int i, len, new_len;
1585 char *result, *sp, *ep, *cp;
1586
1587 if (n_dup < 2)
1588 return str;
1589
1590 while (ISSPACE (*str))
1591 str++;
1592
1593 if (*str == '\0')
1594 return str;
1595
1596 cp = xstrdup (str);
1597
1598 new_len = (strlen (cp) + 1) * n_dup;
1599
1600 sp = result = XNEWVEC (char, new_len);
1601
1602 /* Global modifier characters mustn't be duplicated: skip if found. */
1603 if (*cp == '=' || *cp == '+' || *cp == '%')
1604 *sp++ = *cp++;
1605
1606 do
1607 {
1608 if ((ep = strchr (cp, ',')) != NULL)
1609 *ep++ = '\0';
1610 len = strlen (cp);
1611
1612 /* Copy a constraint N_DUP times. */
1613 for (i = 0; i < n_dup; i++, sp += len + 1)
1614 {
1615 memcpy (sp, cp, len);
1616 *(sp+len) = (ep == NULL && i == n_dup - 1) ? '\0' : ',';
1617 }
1618
1619 cp = ep;
1620 }
1621 while (cp != NULL);
1622
1623 return result;
1624}
1625
1626/* Alter the output of INSN whose pattern was modified by
1627 DEFINE_SUBST. We must replicate output strings according
1628 to the new number of alternatives ALT in substituted pattern.
1629 If ALT equals 1, output has one alternative or defined by C
1630 code, then output is returned without any changes. */
1631
1632static const char *
1633alter_output_for_subst_insn (rtx insn, int alt)
1634{
1635 const char *insn_out, *sp ;
1636 char *old_out, *new_out, *cp;
1637 int i, j, new_len;
1638
1639 insn_out = XTMPL (insn, 3);
1640
1641 if (alt < 2 || *insn_out == '*' || *insn_out != '@')
1642 return insn_out;
1643
1644 old_out = XNEWVEC (char, strlen (insn_out)),
1645 sp = insn_out;
1646
1647 while (ISSPACE (*sp) || *sp == '@')
1648 sp++;
1649
1650 for (i = 0; *sp;)
1651 old_out[i++] = *sp++;
1652
1653 new_len = alt * (i + 1) + 1;
1654
1655 new_out = XNEWVEC (char, new_len);
1656 new_out[0] = '@';
1657
1658 for (j = 0, cp = new_out + 1; j < alt; j++, cp += i + 1)
1659 {
1660 memcpy (cp, old_out, i);
1661 *(cp+i) = (j == alt - 1) ? '\0' : '\n';
1662 }
1663
1664 return new_out;
1665}
1666
3262c1f5
RH
1667/* Replicate insns as appropriate for the given DEFINE_COND_EXEC. */
1668
1669static void
3d7aafde 1670process_one_cond_exec (struct queue_elem *ce_elem)
3262c1f5
RH
1671{
1672 struct queue_elem *insn_elem;
1673 for (insn_elem = define_insn_queue; insn_elem ; insn_elem = insn_elem->next)
1674 {
1675 int alternatives, max_operand;
a406f566 1676 rtx pred, insn, pattern, split;
2f6c5b27 1677 char *new_name;
a406f566 1678 int i;
3262c1f5
RH
1679
1680 if (! is_predicable (insn_elem))
1681 continue;
1682
1683 alternatives = 1;
1684 max_operand = -1;
1685 collect_insn_data (insn_elem->data, &alternatives, &max_operand);
1686 max_operand += 1;
1687
1688 if (XVECLEN (ce_elem->data, 0) != 1)
1689 {
cc472607 1690 error_at (ce_elem->loc, "too many patterns in predicate");
3262c1f5
RH
1691 return;
1692 }
1693
1694 pred = copy_rtx (XVECEXP (ce_elem->data, 0, 0));
1695 pred = alter_predicate_for_insn (pred, alternatives, max_operand,
cc472607 1696 ce_elem->loc);
3262c1f5
RH
1697 if (pred == NULL)
1698 return;
1699
1700 /* Construct a new pattern for the new insn. */
1701 insn = copy_rtx (insn_elem->data);
2f6c5b27
SB
1702 new_name = XNEWVAR (char, strlen XSTR (insn_elem->data, 0) + 4);
1703 sprintf (new_name, "*p %s", XSTR (insn_elem->data, 0));
1704 XSTR (insn, 0) = new_name;
3262c1f5
RH
1705 pattern = rtx_alloc (COND_EXEC);
1706 XEXP (pattern, 0) = pred;
313d38e3
RS
1707 XEXP (pattern, 1) = add_implicit_parallel (XVEC (insn, 1));
1708 XVEC (insn, 1) = rtvec_alloc (1);
1709 XVECEXP (insn, 1, 0) = pattern;
3262c1f5 1710
aadaf24e
KT
1711 if (XVEC (ce_elem->data, 3) != NULL)
1712 {
1713 rtvec attributes = rtvec_alloc (XVECLEN (insn, 4)
1714 + XVECLEN (ce_elem->data, 3));
1715 int i = 0;
1716 int j = 0;
1717 for (i = 0; i < XVECLEN (insn, 4); i++)
1718 RTVEC_ELT (attributes, i) = XVECEXP (insn, 4, i);
1719
1720 for (j = 0; j < XVECLEN (ce_elem->data, 3); j++, i++)
1721 RTVEC_ELT (attributes, i) = XVECEXP (ce_elem->data, 3, j);
1722
1723 XVEC (insn, 4) = attributes;
1724 }
1725
3262c1f5 1726 XSTR (insn, 2) = alter_test_for_insn (ce_elem, insn_elem);
66621f9e 1727 XTMPL (insn, 3) = alter_output_for_insn (ce_elem, insn_elem,
3262c1f5 1728 alternatives, max_operand);
0bddee8e 1729 alter_attrs_for_insn (insn);
3262c1f5
RH
1730
1731 /* Put the new pattern on the `other' list so that it
1732 (a) is not reprocessed by other define_cond_exec patterns
1733 (b) appears after all normal define_insn patterns.
1734
1735 ??? B is debatable. If one has normal insns that match
1736 cond_exec patterns, they will be preferred over these
1737 generated patterns. Whether this matters in practice, or if
1738 it's a good thing, or whether we should thread these new
1739 patterns into the define_insn chain just after their generator
1740 is something we'll have to experiment with. */
1741
cc472607 1742 queue_pattern (insn, &other_tail, insn_elem->loc);
a406f566
MM
1743
1744 if (!insn_elem->split)
1745 continue;
1746
1747 /* If the original insn came from a define_insn_and_split,
9cf737f8 1748 generate a new split to handle the predicated insn. */
a406f566
MM
1749 split = copy_rtx (insn_elem->split->data);
1750 /* Predicate the pattern matched by the split. */
1751 pattern = rtx_alloc (COND_EXEC);
1752 XEXP (pattern, 0) = pred;
313d38e3
RS
1753 XEXP (pattern, 1) = add_implicit_parallel (XVEC (split, 0));
1754 XVEC (split, 0) = rtvec_alloc (1);
1755 XVECEXP (split, 0, 0) = pattern;
1756
a406f566
MM
1757 /* Predicate all of the insns generated by the split. */
1758 for (i = 0; i < XVECLEN (split, 2); i++)
1759 {
1760 pattern = rtx_alloc (COND_EXEC);
1761 XEXP (pattern, 0) = pred;
1762 XEXP (pattern, 1) = XVECEXP (split, 2, i);
1763 XVECEXP (split, 2, i) = pattern;
1764 }
1765 /* Add the new split to the queue. */
cc472607 1766 queue_pattern (split, &other_tail, insn_elem->split->loc);
3262c1f5
RH
1767 }
1768}
1769
477c104e
MK
1770/* Try to apply define_substs to the given ELEM.
1771 Only define_substs, specified via attributes would be applied.
1772 If attribute, requiring define_subst, is set, but no define_subst
1773 was applied, ELEM would be deleted. */
1774
1775static void
1776process_substs_on_one_elem (struct queue_elem *elem,
1777 struct queue_elem *queue)
1778{
1779 struct queue_elem *subst_elem;
1780 int i, j, patterns_match;
1781
1782 for (subst_elem = define_subst_queue;
1783 subst_elem; subst_elem = subst_elem->next)
1784 {
1785 int alternatives, alternatives_subst;
1786 rtx subst_pattern;
1787 rtvec subst_pattern_vec;
1788
1789 if (!has_subst_attribute (elem, subst_elem))
1790 continue;
1791
1792 /* Compare original rtl-pattern from define_insn with input
1793 pattern from define_subst.
1794 Also, check if numbers of alternatives are the same in all
1795 match_operands. */
1796 if (XVECLEN (elem->data, 1) != XVECLEN (subst_elem->data, 1))
1797 continue;
1798 patterns_match = 1;
1799 alternatives = -1;
1800 alternatives_subst = -1;
1801 for (j = 0; j < XVECLEN (elem->data, 1); j++)
1802 {
1803 if (!subst_pattern_match (XVECEXP (elem->data, 1, j),
1804 XVECEXP (subst_elem->data, 1, j),
cc472607 1805 subst_elem->loc))
477c104e
MK
1806 {
1807 patterns_match = 0;
1808 break;
1809 }
1810
1811 if (!get_alternatives_number (XVECEXP (elem->data, 1, j),
cc472607 1812 &alternatives, subst_elem->loc))
477c104e
MK
1813 {
1814 patterns_match = 0;
1815 break;
1816 }
1817 }
1818
1819 /* Check if numbers of alternatives are the same in all
1820 match_operands in output template of define_subst. */
1821 for (j = 0; j < XVECLEN (subst_elem->data, 3); j++)
1822 {
1823 if (!get_alternatives_number (XVECEXP (subst_elem->data, 3, j),
1824 &alternatives_subst,
cc472607 1825 subst_elem->loc))
477c104e
MK
1826 {
1827 patterns_match = 0;
1828 break;
1829 }
1830 }
1831
1832 if (!patterns_match)
1833 continue;
1834
1835 /* Clear array in which we save occupied indexes of operands. */
1836 memset (used_operands_numbers, 0, sizeof (used_operands_numbers));
1837
1838 /* Create a pattern, based on the output one from define_subst. */
1839 subst_pattern_vec = rtvec_alloc (XVECLEN (subst_elem->data, 3));
1840 for (j = 0; j < XVECLEN (subst_elem->data, 3); j++)
1841 {
1842 subst_pattern = copy_rtx (XVECEXP (subst_elem->data, 3, j));
1843
1844 /* Duplicate constraints in substitute-pattern. */
1845 subst_pattern = alter_constraints (subst_pattern, alternatives,
1846 duplicate_each_alternative);
1847
a7ba15ca 1848 subst_pattern = adjust_operands_numbers (subst_pattern);
477c104e
MK
1849
1850 /* Substitute match_dup and match_op_dup in the new pattern and
1851 duplicate constraints. */
1852 subst_pattern = subst_dup (subst_pattern, alternatives,
1853 alternatives_subst);
1854
1855 replace_duplicating_operands_in_pattern (subst_pattern);
1856
1857 /* We don't need any constraints in DEFINE_EXPAND. */
1858 if (GET_CODE (elem->data) == DEFINE_EXPAND)
1859 remove_constraints (subst_pattern);
1860
a7ba15ca 1861 RTVEC_ELT (subst_pattern_vec, j) = subst_pattern;
477c104e
MK
1862 }
1863 XVEC (elem->data, 1) = subst_pattern_vec;
1864
1865 for (i = 0; i < MAX_OPERANDS; i++)
1866 match_operand_entries_in_pattern[i] = NULL;
1867
1868 if (GET_CODE (elem->data) == DEFINE_INSN)
1869 {
1870 XTMPL (elem->data, 3) =
1871 alter_output_for_subst_insn (elem->data, alternatives_subst);
1872 alter_attrs_for_subst_insn (elem, alternatives_subst);
1873 }
1874
1875 /* Recalculate condition, joining conditions from original and
1876 DEFINE_SUBST input patterns. */
1877 XSTR (elem->data, 2) = join_c_conditions (XSTR (subst_elem->data, 2),
1878 XSTR (elem->data, 2));
1879 /* Mark that subst was applied by changing attribute from "yes"
1880 to "no". */
1881 change_subst_attribute (elem, subst_elem, subst_false);
1882 }
1883
1884 /* If ELEM contains a subst attribute with value "yes", then we
1885 expected that a subst would be applied, but it wasn't - so,
1886 we need to remove that elementto avoid duplicating. */
1887 for (subst_elem = define_subst_queue;
1888 subst_elem; subst_elem = subst_elem->next)
1889 {
1890 if (has_subst_attribute (elem, subst_elem))
1891 {
1892 remove_from_queue (elem, &queue);
1893 return;
1894 }
1895 }
1896}
1897
1898/* This is a subroutine of mark_operands_used_in_match_dup.
1899 This routine is marks all MATCH_OPERANDs inside PATTERN as occupied. */
1900static void
1901mark_operands_from_match_dup (rtx pattern)
1902{
1903 const char *fmt;
1904 int i, j, len, opno;
1905
1906 if (GET_CODE (pattern) == MATCH_OPERAND
1907 || GET_CODE (pattern) == MATCH_OPERATOR
1908 || GET_CODE (pattern) == MATCH_PARALLEL)
1909 {
1910 opno = XINT (pattern, 0);
1911 gcc_assert (opno >= 0 && opno < MAX_OPERANDS);
1912 used_operands_numbers [opno] = 1;
1913 }
1914 fmt = GET_RTX_FORMAT (GET_CODE (pattern));
1915 len = GET_RTX_LENGTH (GET_CODE (pattern));
1916 for (i = 0; i < len; i++)
1917 {
1918 switch (fmt[i])
1919 {
1920 case 'e': case 'u':
1921 mark_operands_from_match_dup (XEXP (pattern, i));
1922 break;
1923 case 'E':
1924 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
1925 mark_operands_from_match_dup (XVECEXP (pattern, i, j));
1926 break;
1927 }
1928 }
1929}
1930
a7ba15ca 1931/* This is a subroutine of adjust_operands_numbers.
477c104e
MK
1932 It goes through all expressions in PATTERN and when MATCH_DUP is
1933 met, all MATCH_OPERANDs inside it is marked as occupied. The
1934 process of marking is done by routin mark_operands_from_match_dup. */
1935static void
1936mark_operands_used_in_match_dup (rtx pattern)
1937{
1938 const char *fmt;
1939 int i, j, len, opno;
1940
1941 if (GET_CODE (pattern) == MATCH_DUP)
1942 {
1943 opno = XINT (pattern, 0);
1944 gcc_assert (opno >= 0 && opno < MAX_OPERANDS);
1945 mark_operands_from_match_dup (operand_data[opno]);
1946 return;
1947 }
1948 fmt = GET_RTX_FORMAT (GET_CODE (pattern));
1949 len = GET_RTX_LENGTH (GET_CODE (pattern));
1950 for (i = 0; i < len; i++)
1951 {
1952 switch (fmt[i])
1953 {
1954 case 'e': case 'u':
1955 mark_operands_used_in_match_dup (XEXP (pattern, i));
1956 break;
1957 case 'E':
1958 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
1959 mark_operands_used_in_match_dup (XVECEXP (pattern, i, j));
1960 break;
1961 }
1962 }
1963}
1964
1965/* This is subroutine of renumerate_operands_in_pattern.
1966 It finds first not-occupied operand-index. */
1967static int
1968find_first_unused_number_of_operand ()
1969{
1970 int i;
1971 for (i = 0; i < MAX_OPERANDS; i++)
1972 if (!used_operands_numbers[i])
1973 return i;
1974 return MAX_OPERANDS;
1975}
1976
a7ba15ca
AK
1977/* This is subroutine of adjust_operands_numbers.
1978 It visits all expressions in PATTERN and assigns not-occupied
1979 operand indexes to MATCH_OPERANDs and MATCH_OPERATORs of this
1980 PATTERN. */
477c104e
MK
1981static void
1982renumerate_operands_in_pattern (rtx pattern)
1983{
1984 const char *fmt;
1985 enum rtx_code code;
1986 int i, j, len, new_opno;
1987 code = GET_CODE (pattern);
1988
1989 if (code == MATCH_OPERAND
1990 || code == MATCH_OPERATOR)
1991 {
1992 new_opno = find_first_unused_number_of_operand ();
1993 gcc_assert (new_opno >= 0 && new_opno < MAX_OPERANDS);
1994 XINT (pattern, 0) = new_opno;
1995 used_operands_numbers [new_opno] = 1;
1996 }
1997
1998 fmt = GET_RTX_FORMAT (GET_CODE (pattern));
1999 len = GET_RTX_LENGTH (GET_CODE (pattern));
2000 for (i = 0; i < len; i++)
2001 {
2002 switch (fmt[i])
2003 {
2004 case 'e': case 'u':
2005 renumerate_operands_in_pattern (XEXP (pattern, i));
2006 break;
2007 case 'E':
2008 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
2009 renumerate_operands_in_pattern (XVECEXP (pattern, i, j));
2010 break;
2011 }
2012 }
2013}
2014
a7ba15ca
AK
2015/* If output pattern of define_subst contains MATCH_DUP, then this
2016 expression would be replaced with the pattern, matched with
2017 MATCH_OPERAND from input pattern. This pattern could contain any
2018 number of MATCH_OPERANDs, MATCH_OPERATORs etc., so it's possible
2019 that a MATCH_OPERAND from output_pattern (if any) would have the
2020 same number, as MATCH_OPERAND from copied pattern. To avoid such
2021 indexes overlapping, we assign new indexes to MATCH_OPERANDs,
2022 laying in the output pattern outside of MATCH_DUPs. */
2023static rtx
2024adjust_operands_numbers (rtx pattern)
2025{
2026 mark_operands_used_in_match_dup (pattern);
2027
2028 renumerate_operands_in_pattern (pattern);
2029
2030 return pattern;
2031}
477c104e
MK
2032
2033/* Generate RTL expression
2034 (match_dup OPNO)
2035 */
2036static rtx
2037generate_match_dup (int opno)
2038{
2039 rtx return_rtx = rtx_alloc (MATCH_DUP);
2040 PUT_CODE (return_rtx, MATCH_DUP);
2041 XINT (return_rtx, 0) = opno;
2042 return return_rtx;
2043}
2044
2045/* This routine checks all match_operands in PATTERN and if some of
2046 have the same index, it replaces all of them except the first one to
2047 match_dup.
2048 Usually, match_operands with the same indexes are forbidden, but
2049 after define_subst copy an RTL-expression from original template,
2050 indexes of existed and just-copied match_operands could coincide.
2051 To fix it, we replace one of them with match_dup. */
2052static rtx
2053replace_duplicating_operands_in_pattern (rtx pattern)
2054{
2055 const char *fmt;
2056 int i, j, len, opno;
2057 rtx mdup;
2058
2059 if (GET_CODE (pattern) == MATCH_OPERAND)
2060 {
2061 opno = XINT (pattern, 0);
2062 gcc_assert (opno >= 0 && opno < MAX_OPERANDS);
2063 if (match_operand_entries_in_pattern[opno] == NULL)
2064 {
2065 match_operand_entries_in_pattern[opno] = pattern;
2066 return NULL;
2067 }
2068 else
2069 {
2070 /* Compare predicates before replacing with match_dup. */
2071 if (strcmp (XSTR (pattern, 1),
2072 XSTR (match_operand_entries_in_pattern[opno], 1)))
2073 {
2074 error ("duplicated match_operands with different predicates were"
2075 " found.");
2076 return NULL;
2077 }
2078 return generate_match_dup (opno);
2079 }
2080 }
2081 fmt = GET_RTX_FORMAT (GET_CODE (pattern));
2082 len = GET_RTX_LENGTH (GET_CODE (pattern));
2083 for (i = 0; i < len; i++)
2084 {
2085 switch (fmt[i])
2086 {
2087 case 'e': case 'u':
2088 mdup = replace_duplicating_operands_in_pattern (XEXP (pattern, i));
2089 if (mdup)
2090 XEXP (pattern, i) = mdup;
2091 break;
2092 case 'E':
2093 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
2094 {
2095 mdup =
2096 replace_duplicating_operands_in_pattern (XVECEXP
2097 (pattern, i, j));
2098 if (mdup)
2099 XVECEXP (pattern, i, j) = mdup;
2100 }
2101 break;
2102 }
2103 }
2104 return NULL;
2105}
2106
2107/* The routine modifies given input PATTERN of define_subst, replacing
2108 MATCH_DUP and MATCH_OP_DUP with operands from define_insn original
2109 pattern, whose operands are stored in OPERAND_DATA array.
2110 It also duplicates constraints in operands - constraints from
2111 define_insn operands are duplicated N_SUBST_ALT times, constraints
2112 from define_subst operands are duplicated N_ALT times.
2113 After the duplication, returned output rtl-pattern contains every
2114 combination of input constraints Vs constraints from define_subst
2115 output. */
2116static rtx
2117subst_dup (rtx pattern, int n_alt, int n_subst_alt)
2118{
2119 const char *fmt;
2120 enum rtx_code code;
2121 int i, j, len, opno;
2122
2123 code = GET_CODE (pattern);
2124 switch (code)
2125 {
2126 case MATCH_DUP:
2127 case MATCH_OP_DUP:
2128 opno = XINT (pattern, 0);
2129
2130 gcc_assert (opno >= 0 && opno < MAX_OPERANDS);
2131
2132 if (operand_data[opno])
2133 {
2134 pattern = copy_rtx (operand_data[opno]);
2135
2136 /* Duplicate constraints. */
2137 pattern = alter_constraints (pattern, n_subst_alt,
2138 duplicate_alternatives);
2139 }
2140 break;
2141
2142 default:
2143 break;
2144 }
2145
2146 fmt = GET_RTX_FORMAT (GET_CODE (pattern));
2147 len = GET_RTX_LENGTH (GET_CODE (pattern));
2148 for (i = 0; i < len; i++)
2149 {
2150 switch (fmt[i])
2151 {
2152 case 'e': case 'u':
2153 if (code != MATCH_DUP && code != MATCH_OP_DUP)
2154 XEXP (pattern, i) = subst_dup (XEXP (pattern, i),
2155 n_alt, n_subst_alt);
2156 break;
2157 case 'V':
2158 if (XVEC (pattern, i) == NULL)
2159 break;
191816a3 2160 /* FALLTHRU */
477c104e 2161 case 'E':
c7b3b99f
PCC
2162 if (code != MATCH_DUP && code != MATCH_OP_DUP)
2163 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
477c104e
MK
2164 XVECEXP (pattern, i, j) = subst_dup (XVECEXP (pattern, i, j),
2165 n_alt, n_subst_alt);
2166 break;
2167
9fccb335 2168 case 'i': case 'r': case 'w': case '0': case 's': case 'S': case 'T':
477c104e
MK
2169 break;
2170
2171 default:
2172 gcc_unreachable ();
2173 }
2174 }
2175 return pattern;
2176}
2177
3262c1f5
RH
2178/* If we have any DEFINE_COND_EXEC patterns, expand the DEFINE_INSN
2179 patterns appropriately. */
2180
2181static void
3d7aafde 2182process_define_cond_exec (void)
3262c1f5
RH
2183{
2184 struct queue_elem *elem;
2185
2186 identify_predicable_attribute ();
bb933490 2187 if (have_error)
3262c1f5
RH
2188 return;
2189
2190 for (elem = define_cond_exec_queue; elem ; elem = elem->next)
2191 process_one_cond_exec (elem);
2192}
477c104e
MK
2193
2194/* If we have any DEFINE_SUBST patterns, expand DEFINE_INSN and
2195 DEFINE_EXPAND patterns appropriately. */
2196
2197static void
2198process_define_subst (void)
2199{
2200 struct queue_elem *elem, *elem_attr;
2201
2202 /* Check if each define_subst has corresponding define_subst_attr. */
2203 for (elem = define_subst_queue; elem ; elem = elem->next)
2204 {
2205 for (elem_attr = define_subst_attr_queue;
2206 elem_attr;
2207 elem_attr = elem_attr->next)
2208 if (strcmp (XSTR (elem->data, 0), XSTR (elem_attr->data, 1)) == 0)
2209 goto found;
2210
cc472607
RS
2211 error_at (elem->loc,
2212 "%s: `define_subst' must have at least one "
2213 "corresponding `define_subst_attr'",
2214 XSTR (elem->data, 0));
21c0a521
DM
2215 return;
2216
477c104e
MK
2217 found:
2218 continue;
2219 }
2220
2221 for (elem = define_insn_queue; elem ; elem = elem->next)
2222 process_substs_on_one_elem (elem, define_insn_queue);
2223 for (elem = other_queue; elem ; elem = elem->next)
2224 {
2225 if (GET_CODE (elem->data) != DEFINE_EXPAND)
2226 continue;
2227 process_substs_on_one_elem (elem, other_queue);
2228 }
2229}
600ab3fc
RS
2230\f
2231/* A read_md_files callback for reading an rtx. */
04d8aa70 2232
600ab3fc 2233static void
cc472607 2234rtx_handle_directive (file_location loc, const char *rtx_name)
04d8aa70 2235{
6b8068d6
TS
2236 auto_vec<rtx, 32> subrtxs;
2237 if (!read_rtx (rtx_name, &subrtxs))
2238 return;
04d8aa70 2239
6b8068d6
TS
2240 rtx x;
2241 unsigned int i;
2242 FOR_EACH_VEC_ELT (subrtxs, i, x)
cc472607 2243 process_rtx (x, loc);
04d8aa70
AM
2244}
2245
64aad689
AK
2246/* Comparison function for the mnemonic hash table. */
2247
2248static int
2249htab_eq_string (const void *s1, const void *s2)
2250{
2251 return strcmp ((const char*)s1, (const char*)s2) == 0;
2252}
2253
2254/* Add mnemonic STR with length LEN to the mnemonic hash table
2255 MNEMONIC_HTAB. A trailing zero end character is appendend to STR
2256 and a permanent heap copy of STR is created. */
2257
2258static void
c240b3e0 2259add_mnemonic_string (htab_t mnemonic_htab, const char *str, size_t len)
64aad689
AK
2260{
2261 char *new_str;
2262 void **slot;
2263 char *str_zero = (char*)alloca (len + 1);
2264
2265 memcpy (str_zero, str, len);
2266 str_zero[len] = '\0';
2267
2268 slot = htab_find_slot (mnemonic_htab, str_zero, INSERT);
2269
2270 if (*slot)
2271 return;
2272
2273 /* Not found; create a permanent copy and add it to the hash table. */
2274 new_str = XNEWVAR (char, len + 1);
2275 memcpy (new_str, str_zero, len + 1);
2276 *slot = new_str;
2277}
2278
2279/* Scan INSN for mnemonic strings and add them to the mnemonic hash
2280 table in MNEMONIC_HTAB.
2281
2282 The mnemonics cannot be found if they are emitted using C code.
2283
2284 If a mnemonic string contains ';' or a newline the string assumed
2285 to consist of more than a single instruction. The attribute value
2286 will then be set to the user defined default value. */
2287
2288static void
2289gen_mnemonic_setattr (htab_t mnemonic_htab, rtx insn)
2290{
2291 const char *template_code, *cp;
2292 int i;
2293 int vec_len;
2294 rtx set_attr;
2295 char *attr_name;
2296 rtvec new_vec;
2297
2298 template_code = XTMPL (insn, 3);
2299
2300 /* Skip patterns which use C code to emit the template. */
2301 if (template_code[0] == '*')
2302 return;
2303
2304 if (template_code[0] == '@')
2305 cp = &template_code[1];
2306 else
2307 cp = &template_code[0];
2308
2309 for (i = 0; *cp; )
2310 {
2311 const char *ep, *sp;
c240b3e0 2312 size_t size = 0;
64aad689
AK
2313
2314 while (ISSPACE (*cp))
2315 cp++;
2316
2317 for (ep = sp = cp; !IS_VSPACE (*ep) && *ep != '\0'; ++ep)
2318 if (!ISSPACE (*ep))
2319 sp = ep + 1;
2320
2321 if (i > 0)
2322 obstack_1grow (&string_obstack, ',');
2323
2324 while (cp < sp && ((*cp >= '0' && *cp <= '9')
2325 || (*cp >= 'a' && *cp <= 'z')))
2326
2327 {
2328 obstack_1grow (&string_obstack, *cp);
2329 cp++;
2330 size++;
2331 }
2332
2333 while (cp < sp)
2334 {
2335 if (*cp == ';' || (*cp == '\\' && cp[1] == 'n'))
2336 {
2337 /* Don't set a value if there are more than one
2338 instruction in the string. */
c240b3e0 2339 obstack_blank_fast (&string_obstack, -size);
64aad689
AK
2340 size = 0;
2341
2342 cp = sp;
2343 break;
2344 }
2345 cp++;
2346 }
2347 if (size == 0)
2348 obstack_1grow (&string_obstack, '*');
2349 else
2350 add_mnemonic_string (mnemonic_htab,
c240b3e0 2351 (char *) obstack_next_free (&string_obstack) - size,
64aad689
AK
2352 size);
2353 i++;
2354 }
2355
2356 /* An insn definition might emit an empty string. */
2357 if (obstack_object_size (&string_obstack) == 0)
2358 return;
2359
2360 obstack_1grow (&string_obstack, '\0');
2361
2362 set_attr = rtx_alloc (SET_ATTR);
2363 XSTR (set_attr, 1) = XOBFINISH (&string_obstack, char *);
2364 attr_name = XNEWVAR (char, strlen (MNEMONIC_ATTR_NAME) + 1);
2365 strcpy (attr_name, MNEMONIC_ATTR_NAME);
2366 XSTR (set_attr, 0) = attr_name;
2367
2368 if (!XVEC (insn, 4))
2369 vec_len = 0;
2370 else
2371 vec_len = XVECLEN (insn, 4);
2372
2373 new_vec = rtvec_alloc (vec_len + 1);
2374 for (i = 0; i < vec_len; i++)
2375 RTVEC_ELT (new_vec, i) = XVECEXP (insn, 4, i);
2376 RTVEC_ELT (new_vec, vec_len) = set_attr;
2377 XVEC (insn, 4) = new_vec;
2378}
2379
2380/* This function is called for the elements in the mnemonic hashtable
2381 and generates a comma separated list of the mnemonics. */
2382
2383static int
2384mnemonic_htab_callback (void **slot, void *info ATTRIBUTE_UNUSED)
2385{
2386 obstack_grow (&string_obstack, (char*)*slot, strlen ((char*)*slot));
2387 obstack_1grow (&string_obstack, ',');
2388 return 1;
2389}
2390
2391/* Generate (set_attr "mnemonic" "..") RTXs and append them to every
2392 insn definition in case the back end requests it by defining the
2393 mnemonic attribute. The values for the attribute will be extracted
2394 from the output patterns of the insn definitions as far as
2395 possible. */
2396
2397static void
2398gen_mnemonic_attr (void)
2399{
2400 struct queue_elem *elem;
2401 rtx mnemonic_attr = NULL;
2402 htab_t mnemonic_htab;
2403 const char *str, *p;
2404 int i;
2405
2406 if (have_error)
2407 return;
2408
2409 /* Look for the DEFINE_ATTR for `mnemonic'. */
2410 for (elem = define_attr_queue; elem != *define_attr_tail; elem = elem->next)
2411 if (GET_CODE (elem->data) == DEFINE_ATTR
2412 && strcmp (XSTR (elem->data, 0), MNEMONIC_ATTR_NAME) == 0)
2413 {
2414 mnemonic_attr = elem->data;
2415 break;
2416 }
2417
2418 /* A (define_attr "mnemonic" "...") indicates that the back-end
2419 wants a mnemonic attribute to be generated. */
2420 if (!mnemonic_attr)
2421 return;
2422
2423 mnemonic_htab = htab_create_alloc (MNEMONIC_HTAB_SIZE, htab_hash_string,
2424 htab_eq_string, 0, xcalloc, free);
2425
2426 for (elem = define_insn_queue; elem; elem = elem->next)
2427 {
2428 rtx insn = elem->data;
2429 bool found = false;
2430
2431 /* Check if the insn definition already has
5db40447 2432 (set_attr "mnemonic" ...) or (set (attr "mnemonic") ...). */
64aad689
AK
2433 if (XVEC (insn, 4))
2434 for (i = 0; i < XVECLEN (insn, 4); i++)
5db40447
AK
2435 {
2436 rtx set_attr = XVECEXP (insn, 4, i);
2437
2438 switch (GET_CODE (set_attr))
2439 {
2440 case SET_ATTR:
2441 case SET_ATTR_ALTERNATIVE:
2442 if (strcmp (XSTR (set_attr, 0), MNEMONIC_ATTR_NAME) == 0)
2443 found = true;
2444 break;
2445 case SET:
2446 if (GET_CODE (SET_DEST (set_attr)) == ATTR
2447 && strcmp (XSTR (SET_DEST (set_attr), 0),
2448 MNEMONIC_ATTR_NAME) == 0)
2449 found = true;
2450 break;
2451 default:
2452 break;
2453 }
2454 }
64aad689
AK
2455
2456 if (!found)
2457 gen_mnemonic_setattr (mnemonic_htab, insn);
2458 }
2459
2460 /* Add the user defined values to the hash table. */
2461 str = XSTR (mnemonic_attr, 1);
2462 while ((p = scan_comma_elt (&str)) != NULL)
2463 add_mnemonic_string (mnemonic_htab, p, str - p);
2464
2465 htab_traverse (mnemonic_htab, mnemonic_htab_callback, NULL);
2466
2467 /* Replace the last ',' with the zero end character. */
2468 *((char *)obstack_next_free (&string_obstack) - 1) = '\0';
2469 XSTR (mnemonic_attr, 1) = XOBFINISH (&string_obstack, char *);
2470}
2471
477c104e
MK
2472/* Check if there are DEFINE_ATTRs with the same name. */
2473static void
2474check_define_attr_duplicates ()
2475{
2476 struct queue_elem *elem;
2477 htab_t attr_htab;
2478 char * attr_name;
2479 void **slot;
2480
2481 attr_htab = htab_create (500, htab_hash_string, htab_eq_string, NULL);
2482
2483 for (elem = define_attr_queue; elem; elem = elem->next)
2484 {
2485 attr_name = xstrdup (XSTR (elem->data, 0));
2486
2487 slot = htab_find_slot (attr_htab, attr_name, INSERT);
2488
2489 /* Duplicate. */
2490 if (*slot)
2491 {
cc472607 2492 error_at (elem->loc, "redefinition of attribute '%s'", attr_name);
477c104e
MK
2493 htab_delete (attr_htab);
2494 return;
2495 }
2496
2497 *slot = attr_name;
2498 }
2499
2500 htab_delete (attr_htab);
2501}
2502
04d8aa70
AM
2503/* The entry point for initializing the reader. */
2504
600ab3fc 2505bool
66b0fe8f 2506init_rtx_reader_args_cb (int argc, const char **argv,
600ab3fc 2507 bool (*parse_opt) (const char *))
04d8aa70 2508{
1c7352cd
ZW
2509 /* Prepare to read input. */
2510 condition_table = htab_create (500, hash_c_test, cmp_c_test, NULL);
e543e219 2511 init_predicate_table ();
3916d6d8 2512 obstack_init (rtl_obstack);
e714561a
SB
2513
2514 /* Start at 1, to make 0 available for CODE_FOR_nothing. */
72a35f93
RS
2515 insn_sequence_num = 1;
2516
2517 /* These sequences are not used as indices, so can start at 1 also. */
2518 split_sequence_num = 1;
2519 peephole2_sequence_num = 1;
1c7352cd 2520
600ab3fc 2521 read_md_files (argc, argv, parse_opt, rtx_handle_directive);
3262c1f5 2522
477c104e
MK
2523 if (define_attr_queue != NULL)
2524 check_define_attr_duplicates ();
2525
3262c1f5
RH
2526 /* Process define_cond_exec patterns. */
2527 if (define_cond_exec_queue != NULL)
2528 process_define_cond_exec ();
2529
477c104e
MK
2530 /* Process define_subst patterns. */
2531 if (define_subst_queue != NULL)
2532 process_define_subst ();
2533
64aad689
AK
2534 if (define_attr_queue != NULL)
2535 gen_mnemonic_attr ();
2536
600ab3fc 2537 return !have_error;
3262c1f5
RH
2538}
2539
f9942f4e
ZW
2540/* Programs that don't have their own options can use this entry point
2541 instead. */
600ab3fc 2542bool
66b0fe8f 2543init_rtx_reader_args (int argc, const char **argv)
f9942f4e 2544{
600ab3fc 2545 return init_rtx_reader_args_cb (argc, argv, 0);
f9942f4e
ZW
2546}
2547\f
5d2d3e43
RS
2548/* Try to read a single rtx from the file. Return true on success,
2549 describing it in *INFO. */
3262c1f5 2550
5d2d3e43
RS
2551bool
2552read_md_rtx (md_rtx_info *info)
3262c1f5 2553{
72a35f93
RS
2554 int truth, *counter;
2555 rtx def;
3262c1f5 2556
2199e5fa
ZW
2557 /* Discard insn patterns which we know can never match (because
2558 their C test is provably always false). If insn_elision is
2559 false, our caller needs to see all the patterns. Note that the
2560 elided patterns are never counted by the sequence numbering; it
1a84c183 2561 is the caller's responsibility, when insn_elision is false, not
2199e5fa 2562 to use elided pattern numbers for anything. */
72a35f93
RS
2563 do
2564 {
2565 struct queue_elem **queue, *elem;
2566
2567 /* Read all patterns from a given queue before moving on to the next. */
2568 if (define_attr_queue != NULL)
2569 queue = &define_attr_queue;
2570 else if (define_pred_queue != NULL)
2571 queue = &define_pred_queue;
2572 else if (define_insn_queue != NULL)
2573 queue = &define_insn_queue;
2574 else if (other_queue != NULL)
2575 queue = &other_queue;
2576 else
2577 return false;
2578
2579 elem = *queue;
2580 *queue = elem->next;
2581 def = elem->data;
2582 info->def = def;
2583 info->loc = elem->loc;
2584 free (elem);
2585
2586 truth = maybe_eval_c_test (get_c_test (def));
2587 }
2588 while (truth == 0 && insn_elision);
2589
2590 /* Perform code-specific processing and pick the appropriate sequence
2591 number counter. */
2592 switch (GET_CODE (def))
c88c0d42 2593 {
3262c1f5
RH
2594 case DEFINE_INSN:
2595 case DEFINE_EXPAND:
72a35f93 2596 /* insn_sequence_num is used here so the name table will match caller's
0458fe77 2597 idea of insn numbering, whether or not elision is active. */
72a35f93
RS
2598 record_insn_name (insn_sequence_num, XSTR (def, 0));
2599
2600 /* Fall through. */
2601 case DEFINE_PEEPHOLE:
2602 counter = &insn_sequence_num;
2199e5fa
ZW
2603 break;
2604
3262c1f5 2605 case DEFINE_SPLIT:
72a35f93
RS
2606 counter = &split_sequence_num;
2607 break;
2608
3262c1f5 2609 case DEFINE_PEEPHOLE2:
72a35f93 2610 counter = &peephole2_sequence_num;
3262c1f5
RH
2611 break;
2612
2613 default:
72a35f93 2614 counter = NULL;
3262c1f5 2615 break;
c88c0d42
CP
2616 }
2617
72a35f93
RS
2618 if (counter)
2619 {
2620 info->index = *counter;
2621 if (truth != 0)
2622 *counter += 1;
2623 }
2624 else
2625 info->index = -1;
2626
58d745ec
RS
2627 if (!rtx_locs)
2628 rtx_locs = new hash_map <rtx, file_location>;
2629 rtx_locs->put (info->def, info->loc);
2630
5d2d3e43 2631 return true;
c88c0d42 2632}
9a5834ae 2633
58d745ec
RS
2634/* Return the file location of DEFINE_* rtx X, which was previously
2635 returned by read_md_rtx. */
2636file_location
2637get_file_location (rtx x)
2638{
2639 gcc_assert (rtx_locs);
2640 file_location *entry = rtx_locs->get (x);
2641 gcc_assert (entry);
2642 return *entry;
2643}
2644
ba0ee63d
RS
2645/* Return the number of possible INSN_CODEs. Only meaningful once the
2646 whole file has been processed. */
2647unsigned int
2648get_num_insn_codes ()
2649{
72a35f93 2650 return insn_sequence_num;
ba0ee63d
RS
2651}
2652
d1427a17
RS
2653/* Return the C test that says whether definition rtx DEF can be used,
2654 or "" if it can be used unconditionally. */
2655
2656const char *
2657get_c_test (rtx x)
2658{
2659 switch (GET_CODE (x))
2660 {
2661 case DEFINE_INSN:
2662 case DEFINE_EXPAND:
2663 case DEFINE_SUBST:
2664 return XSTR (x, 2);
2665
2666 case DEFINE_SPLIT:
2667 case DEFINE_PEEPHOLE:
2668 case DEFINE_PEEPHOLE2:
2669 return XSTR (x, 1);
2670
2671 default:
2672 return "";
2673 }
2674}
2675
2199e5fa
ZW
2676/* Helper functions for insn elision. */
2677
2678/* Compute a hash function of a c_test structure, which is keyed
2679 by its ->expr field. */
2680hashval_t
3d7aafde 2681hash_c_test (const void *x)
2199e5fa
ZW
2682{
2683 const struct c_test *a = (const struct c_test *) x;
2684 const unsigned char *base, *s = (const unsigned char *) a->expr;
2685 hashval_t hash;
2686 unsigned char c;
2687 unsigned int len;
2688
2689 base = s;
2690 hash = 0;
2691
2692 while ((c = *s++) != '\0')
2693 {
2694 hash += c + (c << 17);
2695 hash ^= hash >> 2;
2696 }
2697
2698 len = s - base;
2699 hash += len + (len << 17);
2700 hash ^= hash >> 2;
2701
2702 return hash;
2703}
2704
2705/* Compare two c_test expression structures. */
2706int
3d7aafde 2707cmp_c_test (const void *x, const void *y)
2199e5fa
ZW
2708{
2709 const struct c_test *a = (const struct c_test *) x;
2710 const struct c_test *b = (const struct c_test *) y;
2711
2712 return !strcmp (a->expr, b->expr);
2713}
2714
2715/* Given a string representing a C test expression, look it up in the
2716 condition_table and report whether or not its value is known
2717 at compile time. Returns a tristate: 1 for known true, 0 for
2718 known false, -1 for unknown. */
2719int
3d7aafde 2720maybe_eval_c_test (const char *expr)
2199e5fa
ZW
2721{
2722 const struct c_test *test;
2723 struct c_test dummy;
2724
52831d13 2725 if (expr[0] == 0)
2199e5fa
ZW
2726 return 1;
2727
2199e5fa 2728 dummy.expr = expr;
5d038c4c 2729 test = (const struct c_test *)htab_find (condition_table, &dummy);
1c7352cd
ZW
2730 if (!test)
2731 return -1;
2199e5fa
ZW
2732 return test->value;
2733}
2734
1c7352cd
ZW
2735/* Record the C test expression EXPR in the condition_table, with
2736 value VAL. Duplicates clobber previous entries. */
2737
2738void
2739add_c_test (const char *expr, int value)
2740{
2741 struct c_test *test;
2742
2743 if (expr[0] == 0)
2744 return;
2745
2746 test = XNEW (struct c_test);
2747 test->expr = expr;
2748 test->value = value;
2749
2750 *(htab_find_slot (condition_table, test, INSERT)) = test;
2751}
2752
2753/* For every C test, call CALLBACK with two arguments: a pointer to
2754 the condition structure and INFO. Stops when CALLBACK returns zero. */
2755void
2756traverse_c_tests (htab_trav callback, void *info)
2757{
2758 if (condition_table)
2759 htab_traverse (condition_table, callback, info);
2760}
2761
e543e219
ZW
2762/* Helper functions for define_predicate and define_special_predicate
2763 processing. Shared between genrecog.c and genpreds.c. */
2764
2765static htab_t predicate_table;
2766struct pred_data *first_predicate;
2767static struct pred_data **last_predicate = &first_predicate;
2768
2769static hashval_t
2770hash_struct_pred_data (const void *ptr)
2771{
2772 return htab_hash_string (((const struct pred_data *)ptr)->name);
2773}
2774
2775static int
2776eq_struct_pred_data (const void *a, const void *b)
2777{
2778 return !strcmp (((const struct pred_data *)a)->name,
2779 ((const struct pred_data *)b)->name);
2780}
2781
2782struct pred_data *
2783lookup_predicate (const char *name)
2784{
2785 struct pred_data key;
2786 key.name = name;
cceb1885 2787 return (struct pred_data *) htab_find (predicate_table, &key);
e543e219
ZW
2788}
2789
e663da80
RS
2790/* Record that predicate PRED can accept CODE. */
2791
2792void
2793add_predicate_code (struct pred_data *pred, enum rtx_code code)
2794{
2795 if (!pred->codes[code])
2796 {
2797 pred->num_codes++;
2798 pred->codes[code] = true;
2799
2800 if (GET_RTX_CLASS (code) != RTX_CONST_OBJ)
2801 pred->allows_non_const = true;
2802
2803 if (code != REG
2804 && code != SUBREG
2805 && code != MEM
2806 && code != CONCAT
2807 && code != PARALLEL
a8fd4225
GS
2808 && code != STRICT_LOW_PART
2809 && code != SCRATCH)
e663da80
RS
2810 pred->allows_non_lvalue = true;
2811
2812 if (pred->num_codes == 1)
2813 pred->singleton = code;
2814 else if (pred->num_codes == 2)
2815 pred->singleton = UNKNOWN;
2816 }
2817}
2818
e543e219
ZW
2819void
2820add_predicate (struct pred_data *pred)
2821{
2822 void **slot = htab_find_slot (predicate_table, pred, INSERT);
2823 if (*slot)
2824 {
2825 error ("duplicate predicate definition for '%s'", pred->name);
2826 return;
2827 }
2828 *slot = pred;
2829 *last_predicate = pred;
2830 last_predicate = &pred->next;
2831}
2832
2833/* This array gives the initial content of the predicate table. It
c2acaf06 2834 has entries for all predicates defined in recog.c. */
e543e219 2835
ebce9df7 2836struct std_pred_table
e543e219
ZW
2837{
2838 const char *name;
ebce9df7 2839 bool special;
e663da80 2840 bool allows_const_p;
e543e219
ZW
2841 RTX_CODE codes[NUM_RTX_CODE];
2842};
2843
ebce9df7 2844static const struct std_pred_table std_preds[] = {
e663da80 2845 {"general_operand", false, true, {SUBREG, REG, MEM}},
0060d7d7
RS
2846 {"address_operand", true, true, {SUBREG, REG, MEM, PLUS, MINUS, MULT,
2847 ZERO_EXTEND, SIGN_EXTEND, AND}},
e663da80
RS
2848 {"register_operand", false, false, {SUBREG, REG}},
2849 {"pmode_register_operand", true, false, {SUBREG, REG}},
2850 {"scratch_operand", false, false, {SCRATCH, REG}},
81f40b79 2851 {"immediate_operand", false, true, {UNKNOWN}},
e663da80 2852 {"const_int_operand", false, false, {CONST_INT}},
807e902e
KZ
2853#if TARGET_SUPPORTS_WIDE_INT
2854 {"const_scalar_int_operand", false, false, {CONST_INT, CONST_WIDE_INT}},
2855 {"const_double_operand", false, false, {CONST_DOUBLE}},
2856#else
e663da80 2857 {"const_double_operand", false, false, {CONST_INT, CONST_DOUBLE}},
807e902e 2858#endif
e663da80
RS
2859 {"nonimmediate_operand", false, false, {SUBREG, REG, MEM}},
2860 {"nonmemory_operand", false, true, {SUBREG, REG}},
2861 {"push_operand", false, false, {MEM}},
2862 {"pop_operand", false, false, {MEM}},
2863 {"memory_operand", false, false, {SUBREG, MEM}},
2864 {"indirect_operand", false, false, {SUBREG, MEM}},
c6963675
PB
2865 {"ordered_comparison_operator", false, false, {EQ, NE,
2866 LE, LT, GE, GT,
2867 LEU, LTU, GEU, GTU}},
e663da80
RS
2868 {"comparison_operator", false, false, {EQ, NE,
2869 LE, LT, GE, GT,
2870 LEU, LTU, GEU, GTU,
2871 UNORDERED, ORDERED,
2872 UNEQ, UNGE, UNGT,
2873 UNLE, UNLT, LTGT}}
e543e219 2874};
ebce9df7 2875#define NUM_KNOWN_STD_PREDS ARRAY_SIZE (std_preds)
e543e219
ZW
2876
2877/* Initialize the table of predicate definitions, starting with
c2acaf06 2878 the information we have on generic predicates. */
e543e219
ZW
2879
2880static void
2881init_predicate_table (void)
2882{
2883 size_t i, j;
2884 struct pred_data *pred;
2885
2886 predicate_table = htab_create_alloc (37, hash_struct_pred_data,
2887 eq_struct_pred_data, 0,
2888 xcalloc, free);
2889
ebce9df7 2890 for (i = 0; i < NUM_KNOWN_STD_PREDS; i++)
e543e219 2891 {
cceb1885 2892 pred = XCNEW (struct pred_data);
ebce9df7
PB
2893 pred->name = std_preds[i].name;
2894 pred->special = std_preds[i].special;
e543e219 2895
ebce9df7 2896 for (j = 0; std_preds[i].codes[j] != 0; j++)
e663da80
RS
2897 add_predicate_code (pred, std_preds[i].codes[j]);
2898
2899 if (std_preds[i].allows_const_p)
2900 for (j = 0; j < NUM_RTX_CODE; j++)
2901 if (GET_RTX_CLASS (j) == RTX_CONST_OBJ)
bbbbb16a 2902 add_predicate_code (pred, (enum rtx_code) j);
b8698a0f 2903
e543e219
ZW
2904 add_predicate (pred);
2905 }
e543e219 2906}
0458fe77
ZW
2907\f
2908/* These functions allow linkage with print-rtl.c. Also, some generators
2909 like to annotate their output with insn names. */
2910
2911/* Holds an array of names indexed by insn_code_number. */
2912static char **insn_name_ptr = 0;
2913static int insn_name_ptr_size = 0;
2914
2915const char *
2916get_insn_name (int code)
2917{
2918 if (code < insn_name_ptr_size)
2919 return insn_name_ptr[code];
2920 else
2921 return NULL;
2922}
2923
2924static void
2925record_insn_name (int code, const char *name)
2926{
2927 static const char *last_real_name = "insn";
2928 static int last_real_code = 0;
8ad97cfc 2929 char *new_name;
0458fe77
ZW
2930
2931 if (insn_name_ptr_size <= code)
2932 {
2933 int new_size;
2934 new_size = (insn_name_ptr_size ? insn_name_ptr_size * 2 : 512);
7cbb2a85 2935 insn_name_ptr = XRESIZEVEC (char *, insn_name_ptr, new_size);
0458fe77 2936 memset (insn_name_ptr + insn_name_ptr_size, 0,
c3284718 2937 sizeof (char *) * (new_size - insn_name_ptr_size));
0458fe77
ZW
2938 insn_name_ptr_size = new_size;
2939 }
2940
2941 if (!name || name[0] == '\0')
2942 {
8ad97cfc
KG
2943 new_name = XNEWVAR (char, strlen (last_real_name) + 10);
2944 sprintf (new_name, "%s+%d", last_real_name, code - last_real_code);
0458fe77
ZW
2945 }
2946 else
2947 {
8ad97cfc 2948 last_real_name = new_name = xstrdup (name);
0458fe77
ZW
2949 last_real_code = code;
2950 }
2951
8ad97cfc 2952 insn_name_ptr[code] = new_name;
0458fe77 2953}
e792559a
RS
2954\f
2955/* Make STATS describe the operands that appear in rtx X. */
2956
2957static void
2958get_pattern_stats_1 (struct pattern_stats *stats, rtx x)
2959{
2960 RTX_CODE code;
2961 int i;
2962 int len;
2963 const char *fmt;
2964
2965 if (x == NULL_RTX)
2966 return;
2967
2968 code = GET_CODE (x);
2969 switch (code)
2970 {
2971 case MATCH_OPERAND:
2972 case MATCH_OPERATOR:
2973 case MATCH_PARALLEL:
2974 stats->max_opno = MAX (stats->max_opno, XINT (x, 0));
2975 break;
2976
2977 case MATCH_DUP:
2978 case MATCH_OP_DUP:
2979 case MATCH_PAR_DUP:
2980 stats->num_dups++;
2981 stats->max_dup_opno = MAX (stats->max_dup_opno, XINT (x, 0));
2982 break;
2983
2984 case MATCH_SCRATCH:
2985 stats->max_scratch_opno = MAX (stats->max_scratch_opno, XINT (x, 0));
2986 break;
2987
2988 default:
2989 break;
2990 }
2991
2992 fmt = GET_RTX_FORMAT (code);
2993 len = GET_RTX_LENGTH (code);
2994 for (i = 0; i < len; i++)
2995 {
2996 if (fmt[i] == 'e' || fmt[i] == 'u')
2997 get_pattern_stats_1 (stats, XEXP (x, i));
2998 else if (fmt[i] == 'E')
2999 {
3000 int j;
3001 for (j = 0; j < XVECLEN (x, i); j++)
3002 get_pattern_stats_1 (stats, XVECEXP (x, i, j));
3003 }
3004 }
3005}
3006
3007/* Make STATS describe the operands that appear in instruction pattern
3008 PATTERN. */
3009
3010void
3011get_pattern_stats (struct pattern_stats *stats, rtvec pattern)
3012{
3013 int i, len;
3014
3015 stats->max_opno = -1;
3016 stats->max_dup_opno = -1;
3017 stats->max_scratch_opno = -1;
3018 stats->num_dups = 0;
3019
3020 len = GET_NUM_ELEM (pattern);
3021 for (i = 0; i < len; i++)
3022 get_pattern_stats_1 (stats, RTVEC_ELT (pattern, i));
3023
3024 stats->num_generator_args = stats->max_opno + 1;
3025 stats->num_insn_operands = MAX (stats->max_opno,
3026 stats->max_scratch_opno) + 1;
3027 stats->num_operand_vars = MAX (stats->max_opno,
3028 MAX (stats->max_dup_opno,
3029 stats->max_scratch_opno)) + 1;
3030}
3beaff21 3031
9d8895c9
RS
3032/* Return the emit_* function that should be used for pattern X, or NULL
3033 if we can't pick a particular type at compile time and should instead
3034 fall back to "emit". */
3beaff21
RS
3035
3036const char *
3037get_emit_function (rtx x)
3038{
3039 switch (classify_insn (x))
3040 {
3041 case INSN:
3042 return "emit_insn";
3043
3044 case CALL_INSN:
3045 return "emit_call_insn";
3046
3047 case JUMP_INSN:
3048 return "emit_jump_insn";
3049
3050 case UNKNOWN:
9d8895c9 3051 return NULL;
3beaff21
RS
3052
3053 default:
3054 gcc_unreachable ();
3055 }
3056}
3057
3058/* Return true if we must emit a barrier after pattern X. */
3059
3060bool
3061needs_barrier_p (rtx x)
3062{
3063 return (GET_CODE (x) == SET
3064 && GET_CODE (SET_DEST (x)) == PC
3065 && GET_CODE (SET_SRC (x)) == LABEL_REF);
3066}
886456e2
RS
3067
3068#define NS "NULL"
3069#define ZS "'\\0'"
3070#define OPTAB_CL(o, p, c, b, l) { #o, p, #b, ZS, #l, o, c, UNKNOWN, 1 },
3071#define OPTAB_CX(o, p) { #o, p, NULL, NULL, NULL, o, UNKNOWN, UNKNOWN, 1 },
3072#define OPTAB_CD(o, p) { #o, p, NS, ZS, NS, o, UNKNOWN, UNKNOWN, 2 },
3073#define OPTAB_NL(o, p, c, b, s, l) { #o, p, #b, #s, #l, o, c, c, 3 },
3074#define OPTAB_NC(o, p, c) { #o, p, NS, ZS, NS, o, c, c, 3 },
3075#define OPTAB_NX(o, p) { #o, p, NULL, NULL, NULL, o, UNKNOWN, UNKNOWN, 3 },
3076#define OPTAB_VL(o, p, c, b, s, l) { #o, p, #b, #s, #l, o, c, UNKNOWN, 3 },
3077#define OPTAB_VC(o, p, c) { #o, p, NS, ZS, NS, o, c, UNKNOWN, 3 },
3078#define OPTAB_VX(o, p) { #o, p, NULL, NULL, NULL, o, UNKNOWN, UNKNOWN, 3 },
3079#define OPTAB_DC(o, p, c) { #o, p, NS, ZS, NS, o, c, c, 4 },
3080#define OPTAB_D(o, p) { #o, p, NS, ZS, NS, o, UNKNOWN, UNKNOWN, 4 },
3081
3082/* An array of all optabs. Note that the same optab can appear more
3083 than once, with a different pattern. */
3084optab_def optabs[] = {
3085 { "unknown_optab", NULL, NS, ZS, NS, unknown_optab, UNKNOWN, UNKNOWN, 0 },
3086#include "optabs.def"
3087};
3088
3089/* The number of entries in optabs[]. */
3090unsigned int num_optabs = ARRAY_SIZE (optabs);
3091
3092#undef OPTAB_CL
3093#undef OPTAB_CX
3094#undef OPTAB_CD
3095#undef OPTAB_NL
3096#undef OPTAB_NC
3097#undef OPTAB_NX
3098#undef OPTAB_VL
3099#undef OPTAB_VC
3100#undef OPTAB_VX
3101#undef OPTAB_DC
3102#undef OPTAB_D
3103
3104/* Return true if instruction NAME matches pattern PAT, storing information
3105 about the match in P if so. */
3106
3107static bool
3108match_pattern (optab_pattern *p, const char *name, const char *pat)
3109{
3110 bool force_float = false;
3111 bool force_int = false;
3112 bool force_partial_int = false;
3113 bool force_fixed = false;
3114
3115 if (pat == NULL)
3116 return false;
3117 for (; ; ++pat)
3118 {
3119 if (*pat != '$')
3120 {
3121 if (*pat != *name++)
3122 return false;
3123 if (*pat == '\0')
3124 return true;
3125 continue;
3126 }
3127 switch (*++pat)
3128 {
3129 case 'I':
3130 force_int = 1;
3131 break;
3132 case 'P':
3133 force_partial_int = 1;
3134 break;
3135 case 'F':
3136 force_float = 1;
3137 break;
3138 case 'Q':
3139 force_fixed = 1;
3140 break;
3141
3142 case 'a':
3143 case 'b':
3144 {
3145 int i;
3146
3147 /* This loop will stop at the first prefix match, so
3148 look through the modes in reverse order, in case
3149 there are extra CC modes and CC is a prefix of the
3150 CC modes (as it should be). */
3151 for (i = (MAX_MACHINE_MODE) - 1; i >= 0; i--)
3152 {
3153 const char *p, *q;
3154 for (p = GET_MODE_NAME (i), q = name; *p; p++, q++)
3155 if (TOLOWER (*p) != *q)
3156 break;
3157 if (*p == 0
3158 && (! force_int || mode_class[i] == MODE_INT
3159 || mode_class[i] == MODE_VECTOR_INT)
3160 && (! force_partial_int
3161 || mode_class[i] == MODE_INT
3162 || mode_class[i] == MODE_PARTIAL_INT
3163 || mode_class[i] == MODE_VECTOR_INT)
3164 && (! force_float
3165 || mode_class[i] == MODE_FLOAT
3166 || mode_class[i] == MODE_DECIMAL_FLOAT
3167 || mode_class[i] == MODE_COMPLEX_FLOAT
3168 || mode_class[i] == MODE_VECTOR_FLOAT)
3169 && (! force_fixed
3170 || mode_class[i] == MODE_FRACT
3171 || mode_class[i] == MODE_UFRACT
3172 || mode_class[i] == MODE_ACCUM
3173 || mode_class[i] == MODE_UACCUM
3174 || mode_class[i] == MODE_VECTOR_FRACT
3175 || mode_class[i] == MODE_VECTOR_UFRACT
3176 || mode_class[i] == MODE_VECTOR_ACCUM
3177 || mode_class[i] == MODE_VECTOR_UACCUM))
3178 break;
3179 }
3180
3181 if (i < 0)
3182 return false;
3183 name += strlen (GET_MODE_NAME (i));
3184 if (*pat == 'a')
3185 p->m1 = i;
3186 else
3187 p->m2 = i;
3188
3189 force_int = false;
3190 force_partial_int = false;
3191 force_float = false;
3192 force_fixed = false;
3193 }
3194 break;
3195
3196 default:
3197 gcc_unreachable ();
3198 }
3199 }
3200}
3201
3202/* Return true if NAME is the name of an optab, describing it in P if so. */
3203
3204bool
3205find_optab (optab_pattern *p, const char *name)
3206{
3207 if (*name == 0 || *name == '*')
3208 return false;
3209
3210 /* See if NAME matches one of the patterns we have for the optabs
3211 we know about. */
3212 for (unsigned int pindex = 0; pindex < ARRAY_SIZE (optabs); pindex++)
3213 {
3214 p->m1 = p->m2 = 0;
3215 if (match_pattern (p, name, optabs[pindex].pattern))
3216 {
3217 p->name = name;
3218 p->op = optabs[pindex].op;
3219 p->sort_num = (p->op << 16) | (p->m2 << 8) | p->m1;
3220 return true;
3221 }
3222 }
3223 return false;
3224}