genattrtab.c 120 KB
Newer Older
Tom Wood committed
1
/* Generate code from machine description to compute values of attributes.
Jeff Law committed
2
   Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998,
3
   1999, 2000, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4
   Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
Tom Wood committed
5

6
This file is part of GCC.
Tom Wood committed
7

8 9 10 11
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.
Tom Wood committed
12

13 14 15 16
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.
Tom Wood committed
17 18

You should have received a copy of the GNU General Public License
19
along with GCC; see the file COPYING.  If not, write to the Free
Kelley Cook committed
20 21
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.  */
Tom Wood committed
22

23
/* This program handles insn attributes and the DEFINE_DELAY and
24
   DEFINE_INSN_RESERVATION definitions.
Tom Wood committed
25

Tom Wood committed
26
   It produces a series of functions named `get_attr_...', one for each insn
Tom Wood committed
27 28 29 30 31 32 33 34 35 36 37
   attribute.  Each of these is given the rtx for an insn and returns a member
   of the enum for the attribute.

   These subroutines have the form of a `switch' on the INSN_CODE (via
   `recog_memoized').  Each case either returns a constant attribute value
   or a value that depends on tests on other attributes, the form of
   operands, or some random C expression (encoded with a SYMBOL_REF
   expression).

   If the attribute `alternative', or a random C expression is present,
   `constrain_operands' is called.  If either of these cases of a reference to
38
   an operand is found, `extract_insn' is called.
Tom Wood committed
39

40
   The special attribute `length' is also recognized.  For this operand,
Tom Wood committed
41 42 43 44 45 46 47 48
   expressions involving the address of an operand or the current insn,
   (address (pc)), are valid.  In this case, an initial pass is made to
   set all lengths that do not depend on address.  Those that do are set to
   the maximum length.  Then each insn that depends on an address is checked
   and possibly has its length changed.  The process repeats until no further
   changed are made.  The resulting lengths are saved for use by
   `get_attr_length'.

Tom Wood committed
49 50 51 52 53 54
   A special form of DEFINE_ATTR, where the expression for default value is a
   CONST expression, indicates an attribute that is constant for a given run
   of the compiler.  The subroutine generated for these attributes has no
   parameters as it does not depend on any particular insn.  Constant
   attributes are typically used to specify which variety of processor is
   used.
55

Tom Wood committed
56
   Internal attributes are defined to handle DEFINE_DELAY and
57
   DEFINE_INSN_RESERVATION.  Special routines are output for these cases.
Tom Wood committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

   This program works by keeping a list of possible values for each attribute.
   These include the basic attribute choices, default values for attribute, and
   all derived quantities.

   As the description file is read, the definition for each insn is saved in a
   `struct insn_def'.   When the file reading is complete, a `struct insn_ent'
   is created for each insn and chained to the corresponding attribute value,
   either that specified, or the default.

   An optimization phase is then run.  This simplifies expressions for each
   insn.  EQ_ATTR tests are resolved, whenever possible, to a test that
   indicates when the attribute has the specified value for the insn.  This
   avoids recursive calls during compilation.

73 74
   The strategy used when processing DEFINE_DELAY definitions is to create
   arbitrarily complex expressions and have the optimization simplify them.
Tom Wood committed
75 76

   Once optimization is complete, any required routines and definitions
Tom Wood committed
77 78 79 80 81 82 83 84
   will be written.

   An optimization that is not yet implemented is to hoist the constant
   expressions entirely out of the routines and definitions that are written.
   A way to do this is to iterate over all possible combinations of values
   for constant attributes and generate a set of functions for that given
   combination.  An initialization function would be written that evaluates
   the attributes and installs the corresponding set of routines and
85 86 87
   definitions (each would be accessed through a pointer).

   We use the flags in an RTX as follows:
88
   `unchanging' (ATTR_IND_SIMPLIFIED_P): This rtx is fully simplified
89
      independent of the insn code.
90
   `in_struct' (ATTR_CURR_SIMPLIFIED_P): This rtx is fully simplified
91
      for the insn code currently being processed (see optimize_attrs).
92
   `return_val' (ATTR_PERMANENT_P): This rtx is permanent and unique
93
      (see attr_rtx).  */
94

95 96
#define ATTR_IND_SIMPLIFIED_P(RTX) (RTX_FLAG((RTX), unchanging))
#define ATTR_CURR_SIMPLIFIED_P(RTX) (RTX_FLAG((RTX), in_struct))
97
#define ATTR_PERMANENT_P(RTX) (RTX_FLAG((RTX), return_val))
98

99
#if 0
100 101
#define strcmp_check(S1, S2) ((S1) == (S2)		\
			      ? 0			\
102
			      : (gcc_assert (strcmp ((S1), (S2))), 1))
103 104 105 106
#else
#define strcmp_check(S1, S2) ((S1) != (S2))
#endif

107
#include "bconfig.h"
108
#include "system.h"
109 110
#include "coretypes.h"
#include "tm.h"
Tom Wood committed
111
#include "rtl.h"
Clinton Popetz committed
112
#include "gensupport.h"
113
#include "obstack.h"
Zack Weinberg committed
114
#include "errors.h"
115

116 117 118
/* Flags for make_internal_attr's `special' parameter.  */
#define ATTR_NONE		0
#define ATTR_SPECIAL		(1 << 0)
119

120
static struct obstack obstack1, obstack2;
121 122
static struct obstack *hash_obstack = &obstack1;
static struct obstack *temp_obstack = &obstack2;
Tom Wood committed
123

124 125 126
/* enough space to reserve for printing out ints */
#define MAX_DIGITS (HOST_BITS_PER_INT * 3 / 10 + 3)

Tom Wood committed
127 128 129 130 131 132 133 134 135
/* Define structures used to record attributes and values.  */

/* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
   encountered, we store all the relevant information into a
   `struct insn_def'.  This is done to allow attribute definitions to occur
   anywhere in the file.  */

struct insn_def
{
Mike Stump committed
136 137
  struct insn_def *next;	/* Next insn in chain.  */
  rtx def;			/* The DEFINE_...  */
138 139 140
  int insn_code;		/* Instruction number.  */
  int insn_index;		/* Expression numer in file, for errors.  */
  int lineno;			/* Line number.  */
Tom Wood committed
141
  int num_alternatives;		/* Number of alternatives.  */
Mike Stump committed
142
  int vec_idx;			/* Index of attribute vector in `def'.  */
Tom Wood committed
143 144 145 146 147 148 149 150
};

/* Once everything has been read in, we store in each attribute value a list
   of insn codes that have that value.  Here is the structure used for the
   list.  */

struct insn_ent
{
151
  struct insn_ent *next;	/* Next in chain.  */
152
  struct insn_def *def;		/* Instruction definition.  */
Tom Wood committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
};

/* Each value of an attribute (either constant or computed) is assigned a
   structure which is used as the listhead of the insns that have that
   value.  */

struct attr_value
{
  rtx value;			/* Value of attribute.  */
  struct attr_value *next;	/* Next attribute value in chain.  */
  struct insn_ent *first_insn;	/* First insn with this value.  */
  int num_insns;		/* Number of insns with this value.  */
  int has_asm_insn;		/* True if this value used for `asm' insns */
};

/* Structure for each attribute.  */

struct attr_desc
{
Mike Stump committed
172 173
  char *name;			/* Name of attribute.  */
  struct attr_desc *next;	/* Next attribute.  */
174 175 176
  struct attr_value *first_value; /* First value of this attribute.  */
  struct attr_value *default_val; /* Default value for this attribute.  */
  int lineno : 24;		/* Line number.  */
177 178 179
  unsigned is_numeric	: 1;	/* Values of this attribute are numeric.  */
  unsigned is_const	: 1;	/* Attribute value constant for each run.  */
  unsigned is_special	: 1;	/* Don't call `write_attr_set'.  */
Tom Wood committed
180 181 182 183 184 185 186
};

/* Structure for each DEFINE_DELAY.  */

struct delay_desc
{
  rtx def;			/* DEFINE_DELAY expression.  */
Mike Stump committed
187
  struct delay_desc *next;	/* Next DEFINE_DELAY.  */
Tom Wood committed
188
  int num;			/* Number of DEFINE_DELAY, starting at 1.  */
189
  int lineno;			/* Line number.  */
Tom Wood committed
190 191
};

192 193 194 195 196 197 198 199
struct attr_value_list
{
  struct attr_value *av;
  struct insn_ent *ie;
  struct attr_desc *attr;
  struct attr_value_list *next;
};

Tom Wood committed
200 201
/* Listheads of above structures.  */

202 203 204
/* This one is indexed by the first character of the attribute name.  */
#define MAX_ATTRS_INDEX 256
static struct attr_desc *attrs[MAX_ATTRS_INDEX];
Tom Wood committed
205 206
static struct insn_def *defs;
static struct delay_desc *delays;
207
struct attr_value_list **insn_code_values;
208

Mike Stump committed
209
/* Other variables.  */
Tom Wood committed
210 211 212 213 214 215 216

static int insn_code_number;
static int insn_index_number;
static int got_define_asm_attributes;
static int must_extract;
static int must_constrain;
static int address_used;
217
static int length_used;
Tom Wood committed
218 219
static int num_delays;
static int have_annul_true, have_annul_false;
220
static int num_insn_ents;
Tom Wood committed
221

222 223 224 225
/* Stores, for each insn code, the number of constraint alternatives.  */

static int *insn_n_alternatives;

Tom Wood committed
226 227 228 229 230 231 232 233 234 235 236
/* Stores, for each insn code, a bitmap that has bits on for each possible
   alternative.  */

static int *insn_alternatives;

/* Used to simplify expressions.  */

static rtx true_rtx, false_rtx;

/* Used to reduce calls to `strcmp' */

237
static const char *alternative_name;
238 239 240 241
static const char *length_str;
static const char *delay_type_str;
static const char *delay_1_0_str;
static const char *num_delay_slots_str;
Tom Wood committed
242 243 244 245

/* Simplify an expression.  Only call the routine if there is something to
   simplify.  */
#define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX)	\
246
  (ATTR_IND_SIMPLIFIED_P (EXP) || ATTR_CURR_SIMPLIFIED_P (EXP) ? (EXP)	\
Tom Wood committed
247
   : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
248

249 250
#define DEF_ATTR_STRING(S) (attr_string ((S), strlen (S)))

251 252 253 254 255
/* Forward declarations of functions used before their definitions, only.  */
static char *attr_string           (const char *, int);
static char *attr_printf           (unsigned int, const char *, ...)
  ATTRIBUTE_PRINTF_2;
static rtx make_numeric_value      (int);
256
static struct attr_desc *find_attr (const char **, int);
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
static rtx mk_attr_alt             (int);
static char *next_comma_elt	   (const char **);
static rtx insert_right_side	   (enum rtx_code, rtx, rtx, int, int);
static rtx copy_boolean		   (rtx);
static int compares_alternatives_p (rtx);
static void make_internal_attr     (const char *, rtx, int);
static void insert_insn_ent        (struct attr_value *, struct insn_ent *);
static void walk_attr_value	   (rtx);
static int max_attr_value	   (rtx, int*);
static int min_attr_value	   (rtx, int*);
static int or_attr_value	   (rtx, int*);
static rtx simplify_test_exp	   (rtx, int, int);
static rtx simplify_test_exp_in_temp (rtx, int, int);
static rtx copy_rtx_unchanging	   (rtx);
static bool attr_alt_subset_p      (rtx, rtx);
272
static bool attr_alt_subset_of_compl_p (rtx, rtx);
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
static void clear_struct_flag      (rtx);
static void write_attr_valueq	   (struct attr_desc *, const char *);
static struct attr_value *find_most_used  (struct attr_desc *);
static void write_attr_set	   (struct attr_desc *, int, rtx,
				    const char *, const char *, rtx,
				    int, int);
static void write_attr_case	   (struct attr_desc *, struct attr_value *,
				    int, const char *, const char *, int, rtx);
static void write_attr_value	   (struct attr_desc *, rtx);
static void write_upcase	   (const char *);
static void write_indent	   (int);
static rtx identity_fn		   (rtx);
static rtx zero_fn		   (rtx);
static rtx one_fn		   (rtx);
static rtx max_fn		   (rtx);
static rtx min_fn		   (rtx);
289 290

#define oballoc(size) obstack_alloc (hash_obstack, size)
291

Tom Wood committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
/* Hash table for sharing RTL and strings.  */

/* Each hash table slot is a bucket containing a chain of these structures.
   Strings are given negative hash codes; RTL expressions are given positive
   hash codes.  */

struct attr_hash
{
  struct attr_hash *next;	/* Next structure in the bucket.  */
  int hashcode;			/* Hash code of this rtx or string.  */
  union
    {
      char *str;		/* The string (negative hash codes) */
      rtx rtl;			/* or the RTL recorded here.  */
    } u;
};

/* Now here is the hash table.  When recording an RTL, it is added to
   the slot whose index is the hash code mod the table size.  Note
   that the hash table is used for several kinds of RTL (see attr_rtx)
   and for strings.  While all these live in the same table, they are
   completely independent, and the hash code is computed differently
   for each.  */

#define RTL_HASH_SIZE 4093
317
static struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
Tom Wood committed
318 319 320

/* Here is how primitive or already-shared RTL's hash
   codes are made.  */
321
#define RTL_HASH(RTL) ((long) (RTL) & 0777777)
Tom Wood committed
322 323 324 325

/* Add an entry to the hash table for RTL with hash code HASHCODE.  */

static void
326
attr_hash_add_rtx (int hashcode, rtx rtl)
Tom Wood committed
327
{
328
  struct attr_hash *h;
Tom Wood committed
329

330
  h = obstack_alloc (hash_obstack, sizeof (struct attr_hash));
Tom Wood committed
331 332 333 334 335 336 337 338 339
  h->hashcode = hashcode;
  h->u.rtl = rtl;
  h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
  attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
}

/* Add an entry to the hash table for STRING with hash code HASHCODE.  */

static void
340
attr_hash_add_string (int hashcode, char *str)
Tom Wood committed
341
{
342
  struct attr_hash *h;
Tom Wood committed
343

344
  h = obstack_alloc (hash_obstack, sizeof (struct attr_hash));
Tom Wood committed
345 346 347 348 349 350
  h->hashcode = -hashcode;
  h->u.str = str;
  h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
  attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
}

351
/* Generate an RTL expression, but avoid duplicates.
352
   Set the ATTR_PERMANENT_P flag for these permanent objects.
353 354

   In some cases we cannot uniquify; then we return an ordinary
355
   impermanent rtx with ATTR_PERMANENT_P clear.
356

357
   Args are as follows:
Tom Wood committed
358 359 360 361

   rtx attr_rtx (code, [element1, ..., elementn])  */

static rtx
362
attr_rtx_1 (enum rtx_code code, va_list p)
Tom Wood committed
363
{
364
  rtx rt_val = NULL_RTX;/* RTX to return to caller...		*/
Tom Wood committed
365
  int hashcode;
366
  struct attr_hash *h;
367
  struct obstack *old_obstack = rtl_obstack;
Tom Wood committed
368 369 370 371 372

  /* For each of several cases, search the hash table for an existing entry.
     Use that entry if one is found; otherwise create a new RTL and add it
     to the table.  */

373
  if (GET_RTX_CLASS (code) == RTX_UNARY)
Tom Wood committed
374 375 376
    {
      rtx arg0 = va_arg (p, rtx);

377
      /* A permanent object cannot point to impermanent ones.  */
378
      if (! ATTR_PERMANENT_P (arg0))
379 380 381 382 383 384
	{
	  rt_val = rtx_alloc (code);
	  XEXP (rt_val, 0) = arg0;
	  return rt_val;
	}

385
      hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
Tom Wood committed
386 387 388 389
      for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
	if (h->hashcode == hashcode
	    && GET_CODE (h->u.rtl) == code
	    && XEXP (h->u.rtl, 0) == arg0)
390
	  return h->u.rtl;
Tom Wood committed
391 392 393

      if (h == 0)
	{
394
	  rtl_obstack = hash_obstack;
Tom Wood committed
395 396 397 398
	  rt_val = rtx_alloc (code);
	  XEXP (rt_val, 0) = arg0;
	}
    }
399 400 401 402
  else if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
  	   || GET_RTX_CLASS (code) == RTX_COMM_ARITH
  	   || GET_RTX_CLASS (code) == RTX_COMPARE
  	   || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
Tom Wood committed
403 404 405 406
    {
      rtx arg0 = va_arg (p, rtx);
      rtx arg1 = va_arg (p, rtx);

407
      /* A permanent object cannot point to impermanent ones.  */
408
      if (! ATTR_PERMANENT_P (arg0) || ! ATTR_PERMANENT_P (arg1))
409 410 411 412 413 414 415
	{
	  rt_val = rtx_alloc (code);
	  XEXP (rt_val, 0) = arg0;
	  XEXP (rt_val, 1) = arg1;
	  return rt_val;
	}

416
      hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
Tom Wood committed
417 418 419 420 421
      for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
	if (h->hashcode == hashcode
	    && GET_CODE (h->u.rtl) == code
	    && XEXP (h->u.rtl, 0) == arg0
	    && XEXP (h->u.rtl, 1) == arg1)
422
	  return h->u.rtl;
Tom Wood committed
423 424 425

      if (h == 0)
	{
426
	  rtl_obstack = hash_obstack;
Tom Wood committed
427 428 429 430 431 432 433 434
	  rt_val = rtx_alloc (code);
	  XEXP (rt_val, 0) = arg0;
	  XEXP (rt_val, 1) = arg1;
	}
    }
  else if (GET_RTX_LENGTH (code) == 1
	   && GET_RTX_FORMAT (code)[0] == 's')
    {
435
      char *arg0 = va_arg (p, char *);
Tom Wood committed
436

437
      arg0 = DEF_ATTR_STRING (arg0);
438

439
      hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
Tom Wood committed
440 441 442 443
      for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
	if (h->hashcode == hashcode
	    && GET_CODE (h->u.rtl) == code
	    && XSTR (h->u.rtl, 0) == arg0)
444
	  return h->u.rtl;
Tom Wood committed
445 446 447

      if (h == 0)
	{
448
	  rtl_obstack = hash_obstack;
Tom Wood committed
449 450 451 452 453 454 455 456
	  rt_val = rtx_alloc (code);
	  XSTR (rt_val, 0) = arg0;
	}
    }
  else if (GET_RTX_LENGTH (code) == 2
	   && GET_RTX_FORMAT (code)[0] == 's'
	   && GET_RTX_FORMAT (code)[1] == 's')
    {
457 458
      char *arg0 = va_arg (p, char *);
      char *arg1 = va_arg (p, char *);
Tom Wood committed
459

460
      hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
Tom Wood committed
461 462 463 464 465
      for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
	if (h->hashcode == hashcode
	    && GET_CODE (h->u.rtl) == code
	    && XSTR (h->u.rtl, 0) == arg0
	    && XSTR (h->u.rtl, 1) == arg1)
466
	  return h->u.rtl;
Tom Wood committed
467 468 469

      if (h == 0)
	{
470
	  rtl_obstack = hash_obstack;
Tom Wood committed
471 472 473 474 475
	  rt_val = rtx_alloc (code);
	  XSTR (rt_val, 0) = arg0;
	  XSTR (rt_val, 1) = arg1;
	}
    }
476 477
  else if (code == CONST_INT)
    {
Richard Stallman committed
478
      HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
479
      if (arg0 == 0)
480 481 482 483 484
	return false_rtx;
      else if (arg0 == 1)
	return true_rtx;
      else
	goto nohash;
485
    }
Tom Wood committed
486 487
  else
    {
488 489
      int i;		/* Array indices...			*/
      const char *fmt;	/* Current rtx's format...		*/
490
    nohash:
Tom Wood committed
491
      rt_val = rtx_alloc (code);	/* Allocate the storage space.  */
492

Tom Wood committed
493 494 495 496 497 498 499 500 501 502 503 504
      fmt = GET_RTX_FORMAT (code);	/* Find the right format...  */
      for (i = 0; i < GET_RTX_LENGTH (code); i++)
	{
	  switch (*fmt++)
	    {
	    case '0':		/* Unused field.  */
	      break;

	    case 'i':		/* An integer?  */
	      XINT (rt_val, i) = va_arg (p, int);
	      break;

Richard Stallman committed
505 506 507 508
	    case 'w':		/* A wide integer? */
	      XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
	      break;

Tom Wood committed
509 510 511 512 513 514 515 516 517 518 519 520 521 522
	    case 's':		/* A string?  */
	      XSTR (rt_val, i) = va_arg (p, char *);
	      break;

	    case 'e':		/* An expression?  */
	    case 'u':		/* An insn?  Same except when printing.  */
	      XEXP (rt_val, i) = va_arg (p, rtx);
	      break;

	    case 'E':		/* An RTX vector?  */
	      XVEC (rt_val, i) = va_arg (p, rtvec);
	      break;

	    default:
523
	      gcc_unreachable ();
Tom Wood committed
524 525 526 527 528
	    }
	}
      return rt_val;
    }

529
  rtl_obstack = old_obstack;
Tom Wood committed
530
  attr_hash_add_rtx (hashcode, rt_val);
531
  ATTR_PERMANENT_P (rt_val) = 1;
Tom Wood committed
532
  return rt_val;
533
}
Tom Wood committed
534

535
static rtx
536
attr_rtx (enum rtx_code code, ...)
537 538
{
  rtx result;
539
  va_list p;
540

541
  va_start (p, code);
542
  result = attr_rtx_1 (code, p);
543
  va_end (p);
544
  return result;
Tom Wood committed
545 546 547 548 549 550 551
}

/* Create a new string printed with the printf line arguments into a space
   of at most LEN bytes:

   rtx attr_printf (len, format, [arg1, ..., argn])  */

552
static char *
553
attr_printf (unsigned int len, const char *fmt, ...)
Tom Wood committed
554
{
555
  char str[256];
556
  va_list p;
557

558
  va_start (p, fmt);
559

560
  gcc_assert (len < sizeof str); /* Leave room for \0.  */
561

Tom Wood committed
562
  vsprintf (str, fmt, p);
563
  va_end (p);
Tom Wood committed
564

565
  return DEF_ATTR_STRING (str);
Tom Wood committed
566 567
}

568
static rtx
569
attr_eq (const char *name, const char *value)
570
{
571
  return attr_rtx (EQ_ATTR, DEF_ATTR_STRING (name), DEF_ATTR_STRING (value));
572 573
}

574
static const char *
575
attr_numeral (int n)
576 577 578 579
{
  return XSTR (make_numeric_value (n), 0);
}

Tom Wood committed
580 581 582 583
/* Return a permanent (possibly shared) copy of a string STR (not assumed
   to be null terminated) with LEN bytes.  */

static char *
584
attr_string (const char *str, int len)
Tom Wood committed
585
{
586
  struct attr_hash *h;
Tom Wood committed
587 588
  int hashcode;
  int i;
589
  char *new_str;
Tom Wood committed
590 591

  /* Compute the hash code.  */
592
  hashcode = (len + 1) * 613 + (unsigned) str[0];
593
  for (i = 1; i < len; i += 2)
594
    hashcode = ((hashcode * 613) + (unsigned) str[i]);
Tom Wood committed
595 596 597 598 599
  if (hashcode < 0)
    hashcode = -hashcode;

  /* Search the table for the string.  */
  for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
600
    if (h->hashcode == -hashcode && h->u.str[0] == str[0]
601
	&& !strncmp (h->u.str, str, len))
Tom Wood committed
602 603 604
      return h->u.str;			/* <-- return if found.  */

  /* Not found; create a permanent copy and add it to the hash table.  */
605
  new_str = obstack_alloc (hash_obstack, len + 1);
606
  memcpy (new_str, str, len);
Tom Wood committed
607 608 609 610 611
  new_str[len] = '\0';
  attr_hash_add_string (hashcode, new_str);

  return new_str;			/* Return the new string.  */
}
612 613 614 615 616

/* Check two rtx's for equality of contents,
   taking advantage of the fact that if both are hashed
   then they can't be equal unless they are the same object.  */

617
static int
618
attr_equal_p (rtx x, rtx y)
619
{
620
  return (x == y || (! (ATTR_PERMANENT_P (x) && ATTR_PERMANENT_P (y))
621 622
		     && rtx_equal_p (x, y)));
}
623

624 625 626 627
/* Copy an attribute value expression,
   descending to all depths, but not copying any
   permanent hashed subexpressions.  */

628
static rtx
629
attr_copy_rtx (rtx orig)
630
{
631 632 633 634
  rtx copy;
  int i, j;
  RTX_CODE code;
  const char *format_ptr;
635 636

  /* No need to copy a permanent object.  */
637
  if (ATTR_PERMANENT_P (orig))
638 639 640 641 642 643 644 645 646
    return orig;

  code = GET_CODE (orig);

  switch (code)
    {
    case REG:
    case CONST_INT:
    case CONST_DOUBLE:
647
    case CONST_VECTOR:
648 649 650 651 652
    case SYMBOL_REF:
    case CODE_LABEL:
    case PC:
    case CC0:
      return orig;
653 654 655

    default:
      break;
656 657 658 659
    }

  copy = rtx_alloc (code);
  PUT_MODE (copy, GET_MODE (orig));
660 661 662
  ATTR_IND_SIMPLIFIED_P (copy) = ATTR_IND_SIMPLIFIED_P (orig);
  ATTR_CURR_SIMPLIFIED_P (copy) = ATTR_CURR_SIMPLIFIED_P (orig);
  ATTR_PERMANENT_P (copy) = ATTR_PERMANENT_P (orig);
663

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
  format_ptr = GET_RTX_FORMAT (GET_CODE (copy));

  for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
    {
      switch (*format_ptr++)
	{
	case 'e':
	  XEXP (copy, i) = XEXP (orig, i);
	  if (XEXP (orig, i) != NULL)
	    XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
	  break;

	case 'E':
	case 'V':
	  XVEC (copy, i) = XVEC (orig, i);
	  if (XVEC (orig, i) != NULL)
	    {
	      XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
	      for (j = 0; j < XVECLEN (copy, i); j++)
		XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
	    }
	  break;

Richard Stallman committed
687 688
	case 'n':
	case 'i':
689 690
	  XINT (copy, i) = XINT (orig, i);
	  break;
Richard Stallman committed
691 692 693 694 695 696 697 698 699 700 701

	case 'w':
	  XWINT (copy, i) = XWINT (orig, i);
	  break;

	case 's':
	case 'S':
	  XSTR (copy, i) = XSTR (orig, i);
	  break;

	default:
702
	  gcc_unreachable ();
703 704 705 706
	}
    }
  return copy;
}
707

Tom Wood committed
708
/* Given a test expression for an attribute, ensure it is validly formed.
Tom Wood committed
709 710 711
   IS_CONST indicates whether the expression is constant for each compiler
   run (a constant expression may not test any particular insn).

Tom Wood committed
712 713 714 715 716 717 718 719
   Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
   and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")).  Do the latter
   test first so that (eq_attr "att" "!a1,a2,a3") works as expected.

   Update the string address in EQ_ATTR expression to be the same used
   in the attribute (or `alternative_name') to speed up subsequent
   `find_attr' calls and eliminate most `strcmp' calls.

Kazu Hirata committed
720
   Return the new expression, if any.  */
Tom Wood committed
721

722
static rtx
723
check_attr_test (rtx exp, int is_const, int lineno)
Tom Wood committed
724 725 726
{
  struct attr_desc *attr;
  struct attr_value *av;
727
  const char *name_ptr, *p;
Tom Wood committed
728 729 730 731 732 733 734
  rtx orexp, newexp;

  switch (GET_CODE (exp))
    {
    case EQ_ATTR:
      /* Handle negation test.  */
      if (XSTR (exp, 1)[0] == '!')
Tom Wood committed
735
	return check_attr_test (attr_rtx (NOT,
736 737
					  attr_eq (XSTR (exp, 0),
						   &XSTR (exp, 1)[1])),
738
				is_const, lineno);
Tom Wood committed
739 740 741

      else if (n_comma_elts (XSTR (exp, 1)) == 1)
	{
742
	  attr = find_attr (&XSTR (exp, 0), 0);
Tom Wood committed
743 744 745
	  if (attr == NULL)
	    {
	      if (! strcmp (XSTR (exp, 0), "alternative"))
746
		return mk_attr_alt (1 << atoi (XSTR (exp, 1)));
747
	      else
748
		fatal ("unknown attribute `%s' in EQ_ATTR", XSTR (exp, 0));
Tom Wood committed
749 750
	    }

Tom Wood committed
751
	  if (is_const && ! attr->is_const)
752
	    fatal ("constant expression uses insn attribute `%s' in EQ_ATTR",
Kaveh R. Ghazi committed
753
		   XSTR (exp, 0));
Tom Wood committed
754

755 756 757
	  /* Copy this just to make it permanent,
	     so expressions using it can be permanent too.  */
	  exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
Tom Wood committed
758

759
	  /* It shouldn't be possible to simplify the value given to a
760
	     constant attribute, so don't expand this until it's time to
761
	     write the test expression.  */
762
	  if (attr->is_const)
763
	    ATTR_IND_SIMPLIFIED_P (exp) = 1;
764

Tom Wood committed
765 766 767
	  if (attr->is_numeric)
	    {
	      for (p = XSTR (exp, 1); *p; p++)
768
		if (! ISDIGIT (*p))
769
		  fatal ("attribute `%s' takes only numeric values",
770
			 XSTR (exp, 0));
Tom Wood committed
771 772 773 774 775 776 777 778 779
	    }
	  else
	    {
	      for (av = attr->first_value; av; av = av->next)
		if (GET_CODE (av->value) == CONST_STRING
		    && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
		  break;

	      if (av == NULL)
780
		fatal ("unknown value `%s' for `%s' attribute",
Kaveh R. Ghazi committed
781
		       XSTR (exp, 1), XSTR (exp, 0));
Tom Wood committed
782 783 784 785
	    }
	}
      else
	{
786
	  if (! strcmp (XSTR (exp, 0), "alternative"))
Tom Wood committed
787
	    {
788 789 790 791 792 793 794
	      int set = 0;

	      name_ptr = XSTR (exp, 1);
	      while ((p = next_comma_elt (&name_ptr)) != NULL)
		set |= 1 << atoi (p);

	      return mk_attr_alt (set);
Tom Wood committed
795
	    }
796 797 798 799 800 801 802 803 804 805
	  else
	    {
	      /* Make an IOR tree of the possible values.  */
	      orexp = false_rtx;
	      name_ptr = XSTR (exp, 1);
	      while ((p = next_comma_elt (&name_ptr)) != NULL)
		{
		  newexp = attr_eq (XSTR (exp, 0), p);
		  orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
		}
Tom Wood committed
806

807 808
	      return check_attr_test (orexp, is_const, lineno);
	    }
Tom Wood committed
809 810 811
	}
      break;

812 813 814
    case ATTR_FLAG:
      break;

Tom Wood committed
815 816
    case CONST_INT:
      /* Either TRUE or FALSE.  */
Richard Stallman committed
817
      if (XWINT (exp, 0))
Tom Wood committed
818 819 820 821 822 823
	return true_rtx;
      else
	return false_rtx;

    case IOR:
    case AND:
824 825
      XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
      XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const, lineno);
Tom Wood committed
826 827 828
      break;

    case NOT:
829
      XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
Tom Wood committed
830 831 832
      break;

    case MATCH_OPERAND:
Tom Wood committed
833 834
      if (is_const)
	fatal ("RTL operator \"%s\" not valid in constant attribute test",
835
	       GET_RTX_NAME (GET_CODE (exp)));
836
      /* These cases can't be simplified.  */
837
      ATTR_IND_SIMPLIFIED_P (exp) = 1;
838
      break;
839

Tom Wood committed
840 841 842
    case LE:  case LT:  case GT:  case GE:
    case LEU: case LTU: case GTU: case GEU:
    case NE:  case EQ:
843 844 845 846 847
      if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
	  && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
	exp = attr_rtx (GET_CODE (exp),
			attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
			attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
Tom Wood committed
848
      /* These cases can't be simplified.  */
849
      ATTR_IND_SIMPLIFIED_P (exp) = 1;
Tom Wood committed
850 851
      break;

Tom Wood committed
852 853 854 855 856
    case SYMBOL_REF:
      if (is_const)
	{
	  /* These cases are valid for constant attributes, but can't be
	     simplified.  */
857
	  exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
858
	  ATTR_IND_SIMPLIFIED_P (exp) = 1;
Tom Wood committed
859 860
	  break;
	}
Tom Wood committed
861 862 863 864 865 866 867
    default:
      fatal ("RTL operator \"%s\" not valid in attribute test",
	     GET_RTX_NAME (GET_CODE (exp)));
    }

  return exp;
}
868

Tom Wood committed
869 870
/* Given an expression, ensure that it is validly formed and that all named
   attribute values are valid for the given attribute.  Issue a fatal error
871
   if not.  If no attribute is specified, assume a numeric attribute.
Tom Wood committed
872

873 874 875
   Return a perhaps modified replacement expression for the value.  */

static rtx
876
check_attr_value (rtx exp, struct attr_desc *attr)
Tom Wood committed
877 878
{
  struct attr_value *av;
879
  const char *p;
Tom Wood committed
880 881 882 883 884 885
  int i;

  switch (GET_CODE (exp))
    {
    case CONST_INT:
      if (attr && ! attr->is_numeric)
886 887 888 889 890 891 892
	{
	  message_with_line (attr->lineno,
			     "CONST_INT not valid for non-numeric attribute %s",
			     attr->name);
	  have_error = 1;
	  break;
	}
Tom Wood committed
893

894
      if (INTVAL (exp) < 0)
895 896
	{
	  message_with_line (attr->lineno,
897 898
			     "negative numeric value specified for attribute %s",
			     attr->name);
899 900 901
	  have_error = 1;
	  break;
	}
Tom Wood committed
902 903 904 905 906 907 908 909
      break;

    case CONST_STRING:
      if (! strcmp (XSTR (exp, 0), "*"))
	break;

      if (attr == 0 || attr->is_numeric)
	{
910 911
	  p = XSTR (exp, 0);
	  for (; *p; p++)
912
	    if (! ISDIGIT (*p))
913 914 915 916 917 918 919
	      {
		message_with_line (attr ? attr->lineno : 0,
				   "non-numeric value for numeric attribute %s",
				   attr ? attr->name : "internal");
		have_error = 1;
		break;
	      }
Tom Wood committed
920 921 922 923 924 925 926 927 928
	  break;
	}

      for (av = attr->first_value; av; av = av->next)
	if (GET_CODE (av->value) == CONST_STRING
	    && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
	  break;

      if (av == NULL)
929 930 931 932 933 934
	{
	  message_with_line (attr->lineno,
			     "unknown value `%s' for `%s' attribute",
			     XSTR (exp, 0), attr ? attr->name : "internal");
	  have_error = 1;
	}
935
      break;
Tom Wood committed
936 937

    case IF_THEN_ELSE:
Tom Wood committed
938
      XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
939 940
				       attr ? attr->is_const : 0,
				       attr ? attr->lineno : 0);
941 942 943
      XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
      XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
      break;
Tom Wood committed
944

945 946 947 948 949 950
    case PLUS:
    case MINUS:
    case MULT:
    case DIV:
    case MOD:
      if (attr && !attr->is_numeric)
951 952
	{
	  message_with_line (attr->lineno,
953 954
			     "invalid operation `%s' for non-numeric attribute value",
			     GET_RTX_NAME (GET_CODE (exp)));
955 956 957
	  have_error = 1;
	  break;
	}
958
      /* Fall through.  */
959

960 961 962 963 964 965 966
    case IOR:
    case AND:
      XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
      XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
      break;

    case FFS:
Richard Henderson committed
967 968 969 970
    case CLZ:
    case CTZ:
    case POPCOUNT:
    case PARITY:
971
    case BSWAP:
972 973 974
      XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
      break;

Tom Wood committed
975 976
    case COND:
      if (XVECLEN (exp, 0) % 2 != 0)
977 978 979 980 981 982
	{
	  message_with_line (attr->lineno,
			     "first operand of COND must have even length");
	  have_error = 1;
	  break;
	}
Tom Wood committed
983 984 985

      for (i = 0; i < XVECLEN (exp, 0); i += 2)
	{
Tom Wood committed
986
	  XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
987 988
						 attr ? attr->is_const : 0,
						 attr ? attr->lineno : 0);
989 990
	  XVECEXP (exp, 0, i + 1)
	    = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
Tom Wood committed
991 992
	}

993 994
      XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
      break;
Tom Wood committed
995

996 997
    case ATTR:
      {
998
	struct attr_desc *attr2 = find_attr (&XSTR (exp, 0), 0);
999
	if (attr2 == NULL)
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
	  {
	    message_with_line (attr ? attr->lineno : 0,
			       "unknown attribute `%s' in ATTR",
			       XSTR (exp, 0));
	    have_error = 1;
	  }
	else if (attr && attr->is_const && ! attr2->is_const)
	  {
	    message_with_line (attr->lineno,
		"non-constant attribute `%s' referenced from `%s'",
		XSTR (exp, 0), attr->name);
	    have_error = 1;
	  }
1013
	else if (attr
1014
		 && attr->is_numeric != attr2->is_numeric)
1015 1016 1017 1018 1019 1020
	  {
	    message_with_line (attr->lineno,
		"numeric attribute mismatch calling `%s' from `%s'",
		XSTR (exp, 0), attr->name);
	    have_error = 1;
	  }
1021 1022 1023
      }
      break;

Tom Wood committed
1024
    case SYMBOL_REF:
1025 1026 1027 1028
      /* A constant SYMBOL_REF is valid as a constant attribute test and
         is expanded later by make_canonical into a COND.  In a non-constant
         attribute test, it is left be.  */
      return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
Tom Wood committed
1029

Tom Wood committed
1030
    default:
1031 1032 1033 1034 1035
      message_with_line (attr ? attr->lineno : 0,
			 "invalid operation `%s' for attribute value",
			 GET_RTX_NAME (GET_CODE (exp)));
      have_error = 1;
      break;
Tom Wood committed
1036
    }
1037 1038

  return exp;
Tom Wood committed
1039
}
1040

Tom Wood committed
1041
/* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1042
   It becomes a COND with each test being (eq_attr "alternative" "n") */
Tom Wood committed
1043 1044

static rtx
1045
convert_set_attr_alternative (rtx exp, struct insn_def *id)
Tom Wood committed
1046
{
1047
  int num_alt = id->num_alternatives;
Tom Wood committed
1048 1049 1050 1051
  rtx condexp;
  int i;

  if (XVECLEN (exp, 1) != num_alt)
1052 1053
    {
      message_with_line (id->lineno,
1054
			 "bad number of entries in SET_ATTR_ALTERNATIVE");
1055 1056 1057
      have_error = 1;
      return NULL_RTX;
    }
Tom Wood committed
1058 1059 1060 1061 1062 1063 1064 1065

  /* Make a COND with all tests but the last.  Select the last value via the
     default.  */
  condexp = rtx_alloc (COND);
  XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);

  for (i = 0; i < num_alt - 1; i++)
    {
1066
      const char *p;
1067
      p = attr_numeral (i);
Tom Wood committed
1068

1069
      XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
Tom Wood committed
1070 1071 1072 1073 1074
      XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
    }

  XEXP (condexp, 1) = XVECEXP (exp, 1, i);

Tom Wood committed
1075
  return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
Tom Wood committed
1076
}
1077

Tom Wood committed
1078 1079 1080 1081
/* Given a SET_ATTR, convert to the appropriate SET.  If a comma-separated
   list of values is given, convert to SET_ATTR_ALTERNATIVE first.  */

static rtx
1082
convert_set_attr (rtx exp, struct insn_def *id)
Tom Wood committed
1083 1084
{
  rtx newexp;
1085
  const char *name_ptr;
Tom Wood committed
1086 1087 1088 1089 1090 1091
  char *p;
  int n;

  /* See how many alternative specified.  */
  n = n_comma_elts (XSTR (exp, 1));
  if (n == 1)
Tom Wood committed
1092 1093 1094
    return attr_rtx (SET,
		     attr_rtx (ATTR, XSTR (exp, 0)),
		     attr_rtx (CONST_STRING, XSTR (exp, 1)));
Tom Wood committed
1095 1096 1097 1098 1099 1100 1101 1102 1103

  newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
  XSTR (newexp, 0) = XSTR (exp, 0);
  XVEC (newexp, 1) = rtvec_alloc (n);

  /* Process each comma-separated name.  */
  name_ptr = XSTR (exp, 1);
  n = 0;
  while ((p = next_comma_elt (&name_ptr)) != NULL)
Tom Wood committed
1104
    XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
Tom Wood committed
1105

1106
  return convert_set_attr_alternative (newexp, id);
Tom Wood committed
1107
}
1108

Tom Wood committed
1109 1110
/* Scan all definitions, checking for validity.  Also, convert any SET_ATTR
   and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
Mike Stump committed
1111
   expressions.  */
Tom Wood committed
1112 1113

static void
1114
check_defs (void)
Tom Wood committed
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
{
  struct insn_def *id;
  struct attr_desc *attr;
  int i;
  rtx value;

  for (id = defs; id; id = id->next)
    {
      if (XVEC (id->def, id->vec_idx) == NULL)
	continue;

      for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
	{
	  value = XVECEXP (id->def, id->vec_idx, i);
	  switch (GET_CODE (value))
	    {
	    case SET:
	      if (GET_CODE (XEXP (value, 0)) != ATTR)
1133 1134 1135 1136 1137
		{
		  message_with_line (id->lineno, "bad attribute set");
		  have_error = 1;
		  value = NULL_RTX;
		}
Tom Wood committed
1138 1139 1140
	      break;

	    case SET_ATTR_ALTERNATIVE:
1141
	      value = convert_set_attr_alternative (value, id);
Tom Wood committed
1142 1143 1144
	      break;

	    case SET_ATTR:
1145
	      value = convert_set_attr (value, id);
Tom Wood committed
1146 1147 1148
	      break;

	    default:
1149 1150 1151 1152
	      message_with_line (id->lineno, "invalid attribute code %s",
				 GET_RTX_NAME (GET_CODE (value)));
	      have_error = 1;
	      value = NULL_RTX;
Tom Wood committed
1153
	    }
1154 1155
	  if (value == NULL_RTX)
	    continue;
Tom Wood committed
1156

1157
	  if ((attr = find_attr (&XSTR (XEXP (value, 0), 0), 0)) == NULL)
1158 1159 1160 1161 1162 1163
	    {
	      message_with_line (id->lineno, "unknown attribute %s",
				 XSTR (XEXP (value, 0), 0));
	      have_error = 1;
	      continue;
	    }
Tom Wood committed
1164 1165

	  XVECEXP (id->def, id->vec_idx, i) = value;
1166
	  XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
Tom Wood committed
1167 1168 1169
	}
    }
}
Tom Wood committed
1170

Tom Wood committed
1171 1172 1173 1174 1175 1176
/* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
   expressions by converting them into a COND.  This removes cases from this
   program.  Also, replace an attribute value of "*" with the default attribute
   value.  */

static rtx
1177
make_canonical (struct attr_desc *attr, rtx exp)
Tom Wood committed
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
{
  int i;
  rtx newexp;

  switch (GET_CODE (exp))
    {
    case CONST_INT:
      exp = make_numeric_value (INTVAL (exp));
      break;

    case CONST_STRING:
      if (! strcmp (XSTR (exp, 0), "*"))
	{
	  if (attr == 0 || attr->default_val == 0)
1192
	    fatal ("(attr_value \"*\") used in invalid context");
Tom Wood committed
1193 1194
	  exp = attr->default_val->value;
	}
1195 1196
      else
	XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
Tom Wood committed
1197 1198 1199

      break;

Tom Wood committed
1200
    case SYMBOL_REF:
1201
      if (!attr->is_const || ATTR_IND_SIMPLIFIED_P (exp))
Tom Wood committed
1202
	break;
1203 1204 1205
      /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
	 This makes the COND something that won't be considered an arbitrary
	 expression by walk_attr_value.  */
1206
      ATTR_IND_SIMPLIFIED_P (exp) = 1;
1207 1208
      exp = check_attr_value (exp, attr);
      break;
Tom Wood committed
1209

Tom Wood committed
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
    case IF_THEN_ELSE:
      newexp = rtx_alloc (COND);
      XVEC (newexp, 0) = rtvec_alloc (2);
      XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
      XVECEXP (newexp, 0, 1) = XEXP (exp, 1);

      XEXP (newexp, 1) = XEXP (exp, 2);

      exp = newexp;
      /* Fall through to COND case since this is now a COND.  */

    case COND:
1222 1223 1224
      {
	int allsame = 1;
	rtx defval;
Tom Wood committed
1225

Mike Stump committed
1226
	/* First, check for degenerate COND.  */
1227 1228 1229
	if (XVECLEN (exp, 0) == 0)
	  return make_canonical (attr, XEXP (exp, 1));
	defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
Tom Wood committed
1230

1231 1232
	for (i = 0; i < XVECLEN (exp, 0); i += 2)
	  {
1233
	    XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1234 1235 1236 1237 1238 1239 1240 1241
	    XVECEXP (exp, 0, i + 1)
	      = make_canonical (attr, XVECEXP (exp, 0, i + 1));
	    if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
	      allsame = 0;
	  }
	if (allsame)
	  return defval;
      }
1242 1243 1244 1245
      break;

    default:
      break;
Tom Wood committed
1246 1247 1248 1249
    }

  return exp;
}
1250 1251

static rtx
1252
copy_boolean (rtx exp)
1253 1254 1255 1256
{
  if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
    return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
		     copy_boolean (XEXP (exp, 1)));
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
  if (GET_CODE (exp) == MATCH_OPERAND)
    {
      XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
      XSTR (exp, 2) = DEF_ATTR_STRING (XSTR (exp, 2));
    }
  else if (GET_CODE (exp) == EQ_ATTR)
    {
      XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
      XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
    }

1268 1269
  return exp;
}
1270

Tom Wood committed
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
/* Given a value and an attribute description, return a `struct attr_value *'
   that represents that value.  This is either an existing structure, if the
   value has been previously encountered, or a newly-created structure.

   `insn_code' is the code of an insn whose attribute has the specified
   value (-2 if not processing an insn).  We ensure that all insns for
   a given value have the same number of alternatives if the value checks
   alternatives.  */

static struct attr_value *
1281
get_attr_value (rtx value, struct attr_desc *attr, int insn_code)
Tom Wood committed
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
{
  struct attr_value *av;
  int num_alt = 0;

  value = make_canonical (attr, value);
  if (compares_alternatives_p (value))
    {
      if (insn_code < 0 || insn_alternatives == NULL)
	fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
      else
	num_alt = insn_alternatives[insn_code];
    }

  for (av = attr->first_value; av; av = av->next)
    if (rtx_equal_p (value, av->value)
	&& (num_alt == 0 || av->first_insn == NULL
1298
	    || insn_alternatives[av->first_insn->def->insn_code]))
Tom Wood committed
1299 1300
      return av;

1301
  av = oballoc (sizeof (struct attr_value));
Tom Wood committed
1302 1303 1304 1305 1306 1307 1308 1309 1310
  av->value = value;
  av->next = attr->first_value;
  attr->first_value = av;
  av->first_insn = NULL;
  av->num_insns = 0;
  av->has_asm_insn = 0;

  return av;
}
1311

Tom Wood committed
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
/* After all DEFINE_DELAYs have been read in, create internal attributes
   to generate the required routines.

   First, we compute the number of delay slots for each insn (as a COND of
   each of the test expressions in DEFINE_DELAYs).  Then, if more than one
   delay type is specified, we compute a similar function giving the
   DEFINE_DELAY ordinal for each insn.

   Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
   tells whether a given insn can be in that delay slot.

1323
   Normal attribute filling and optimization expands these to contain the
Tom Wood committed
1324 1325 1326
   information needed to handle delay slots.  */

static void
1327
expand_delays (void)
Tom Wood committed
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
{
  struct delay_desc *delay;
  rtx condexp;
  rtx newexp;
  int i;
  char *p;

  /* First, generate data for `num_delay_slots' function.  */

  condexp = rtx_alloc (COND);
  XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
  XEXP (condexp, 1) = make_numeric_value (0);

  for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
    {
      XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
      XVECEXP (condexp, 0, i + 1)
	= make_numeric_value (XVECLEN (delay->def, 1) / 3);
    }

1348
  make_internal_attr (num_delay_slots_str, condexp, ATTR_NONE);
Tom Wood committed
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362

  /* If more than one delay type, do the same for computing the delay type.  */
  if (num_delays > 1)
    {
      condexp = rtx_alloc (COND);
      XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
      XEXP (condexp, 1) = make_numeric_value (0);

      for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
	{
	  XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
	  XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
	}

1363
      make_internal_attr (delay_type_str, condexp, ATTR_SPECIAL);
Tom Wood committed
1364 1365
    }

1366 1367
  /* For each delay possibility and delay slot, compute an eligibility
     attribute for non-annulled insns and for each type of annulled (annul
Tom Wood committed
1368
     if true and annul if false).  */
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
  for (delay = delays; delay; delay = delay->next)
    {
      for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
	{
	  condexp = XVECEXP (delay->def, 1, i);
	  if (condexp == 0)
	    condexp = false_rtx;
	  newexp = attr_rtx (IF_THEN_ELSE, condexp,
			     make_numeric_value (1), make_numeric_value (0));

1379 1380
	  p = attr_printf (sizeof "*delay__" + MAX_DIGITS * 2,
			   "*delay_%d_%d", delay->num, i / 3);
1381
	  make_internal_attr (p, newexp, ATTR_SPECIAL);
1382 1383 1384 1385 1386 1387 1388 1389

	  if (have_annul_true)
	    {
	      condexp = XVECEXP (delay->def, 1, i + 1);
	      if (condexp == 0) condexp = false_rtx;
	      newexp = attr_rtx (IF_THEN_ELSE, condexp,
				 make_numeric_value (1),
				 make_numeric_value (0));
1390
	      p = attr_printf (sizeof "*annul_true__" + MAX_DIGITS * 2,
1391
			       "*annul_true_%d_%d", delay->num, i / 3);
1392
	      make_internal_attr (p, newexp, ATTR_SPECIAL);
1393 1394 1395 1396 1397 1398 1399 1400 1401
	    }

	  if (have_annul_false)
	    {
	      condexp = XVECEXP (delay->def, 1, i + 2);
	      if (condexp == 0) condexp = false_rtx;
	      newexp = attr_rtx (IF_THEN_ELSE, condexp,
				 make_numeric_value (1),
				 make_numeric_value (0));
1402
	      p = attr_printf (sizeof "*annul_false__" + MAX_DIGITS * 2,
1403
			       "*annul_false_%d_%d", delay->num, i / 3);
1404
	      make_internal_attr (p, newexp, ATTR_SPECIAL);
1405 1406 1407
	    }
	}
    }
Tom Wood committed
1408
}
1409

Tom Wood committed
1410 1411 1412 1413 1414
/* Once all attributes and insns have been read and checked, we construct for
   each attribute value a list of all the insns that have that value for
   the attribute.  */

static void
1415
fill_attr (struct attr_desc *attr)
Tom Wood committed
1416 1417 1418 1419 1420 1421 1422
{
  struct attr_value *av;
  struct insn_ent *ie;
  struct insn_def *id;
  int i;
  rtx value;

1423 1424 1425 1426 1427
  /* Don't fill constant attributes.  The value is independent of
     any particular insn.  */
  if (attr->is_const)
    return;

Tom Wood committed
1428 1429 1430 1431 1432 1433 1434
  for (id = defs; id; id = id->next)
    {
      /* If no value is specified for this insn for this attribute, use the
	 default.  */
      value = NULL;
      if (XVEC (id->def, id->vec_idx))
	for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1435 1436
	  if (! strcmp_check (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
			      attr->name))
Tom Wood committed
1437 1438 1439 1440 1441 1442 1443
	    value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);

      if (value == NULL)
	av = attr->default_val;
      else
	av = get_attr_value (value, attr, id->insn_code);

1444
      ie = oballoc (sizeof (struct insn_ent));
1445
      ie->def = id;
Tom Wood committed
1446 1447 1448
      insert_insn_ent (av, ie);
    }
}
1449

1450 1451 1452 1453 1454 1455
/* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
   test that checks relative positions of insns (uses MATCH_DUP or PC).
   If so, replace it with what is obtained by passing the expression to
   ADDRESS_FN.  If not but it is a COND or IF_THEN_ELSE, call this routine
   recursively on each value (including the default value).  Otherwise,
   return the value returned by NO_ADDRESS_FN applied to EXP.  */
Tom Wood committed
1456 1457

static rtx
1458 1459
substitute_address (rtx exp, rtx (*no_address_fn) (rtx),
		    rtx (*address_fn) (rtx))
Tom Wood committed
1460 1461 1462 1463
{
  int i;
  rtx newexp;

1464 1465 1466 1467 1468 1469
  if (GET_CODE (exp) == COND)
    {
      /* See if any tests use addresses.  */
      address_used = 0;
      for (i = 0; i < XVECLEN (exp, 0); i += 2)
	walk_attr_value (XVECEXP (exp, 0, i));
Tom Wood committed
1470

1471 1472
      if (address_used)
	return (*address_fn) (exp);
Tom Wood committed
1473

1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
      /* Make a new copy of this COND, replacing each element.  */
      newexp = rtx_alloc (COND);
      XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
      for (i = 0; i < XVECLEN (exp, 0); i += 2)
	{
	  XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
	  XVECEXP (newexp, 0, i + 1)
	    = substitute_address (XVECEXP (exp, 0, i + 1),
				  no_address_fn, address_fn);
	}
Tom Wood committed
1484

1485 1486 1487 1488
      XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
					     no_address_fn, address_fn);

      return newexp;
Tom Wood committed
1489 1490
    }

1491 1492 1493 1494 1495 1496
  else if (GET_CODE (exp) == IF_THEN_ELSE)
    {
      address_used = 0;
      walk_attr_value (XEXP (exp, 0));
      if (address_used)
	return (*address_fn) (exp);
Tom Wood committed
1497

Tom Wood committed
1498 1499 1500 1501 1502 1503 1504
      return attr_rtx (IF_THEN_ELSE,
		       substitute_address (XEXP (exp, 0),
					   no_address_fn, address_fn),
		       substitute_address (XEXP (exp, 1),
					   no_address_fn, address_fn),
		       substitute_address (XEXP (exp, 2),
					   no_address_fn, address_fn));
1505 1506 1507
    }

  return (*no_address_fn) (exp);
Tom Wood committed
1508
}
1509

Tom Wood committed
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
/* Make new attributes from the `length' attribute.  The following are made,
   each corresponding to a function called from `shorten_branches' or
   `get_attr_length':

   *insn_default_length		This is the length of the insn to be returned
				by `get_attr_length' before `shorten_branches'
				has been called.  In each case where the length
				depends on relative addresses, the largest
				possible is used.  This routine is also used
				to compute the initial size of the insn.

   *insn_variable_length_p	This returns 1 if the insn's length depends
				on relative addresses, zero otherwise.

   *insn_current_length		This is only called when it is known that the
				insn has a variable length and returns the
				current length, based on relative addresses.
  */

static void
1530
make_length_attrs (void)
Tom Wood committed
1531
{
1532 1533 1534
  static const char *new_names[] =
    {
      "*insn_default_length",
1535
      "*insn_min_length",
1536 1537 1538
      "*insn_variable_length_p",
      "*insn_current_length"
    };
1539 1540 1541 1542
  static rtx (*const no_address_fn[]) (rtx)
    = {identity_fn,identity_fn, zero_fn, zero_fn};
  static rtx (*const address_fn[]) (rtx)
    = {max_fn, min_fn, one_fn, identity_fn};
Kaveh R. Ghazi committed
1543
  size_t i;
Tom Wood committed
1544 1545 1546 1547 1548 1549
  struct attr_desc *length_attr, *new_attr;
  struct attr_value *av, *new_av;
  struct insn_ent *ie, *new_ie;

  /* See if length attribute is defined.  If so, it must be numeric.  Make
     it special so we don't output anything for it.  */
1550
  length_attr = find_attr (&length_str, 0);
Tom Wood committed
1551 1552 1553 1554
  if (length_attr == 0)
    return;

  if (! length_attr->is_numeric)
1555
    fatal ("length attribute must be numeric");
Tom Wood committed
1556

Tom Wood committed
1557
  length_attr->is_const = 0;
Tom Wood committed
1558 1559 1560
  length_attr->is_special = 1;

  /* Make each new attribute, in turn.  */
1561
  for (i = 0; i < ARRAY_SIZE (new_names); i++)
Tom Wood committed
1562 1563 1564 1565
    {
      make_internal_attr (new_names[i],
			  substitute_address (length_attr->default_val->value,
					      no_address_fn[i], address_fn[i]),
1566
			  ATTR_NONE);
1567
      new_attr = find_attr (&new_names[i], 0);
Tom Wood committed
1568 1569 1570 1571 1572 1573
      for (av = length_attr->first_value; av; av = av->next)
	for (ie = av->first_insn; ie; ie = ie->next)
	  {
	    new_av = get_attr_value (substitute_address (av->value,
							 no_address_fn[i],
							 address_fn[i]),
1574
				     new_attr, ie->def->insn_code);
1575
	    new_ie = oballoc (sizeof (struct insn_ent));
1576
	    new_ie->def = ie->def;
Tom Wood committed
1577 1578 1579 1580 1581 1582 1583 1584
	    insert_insn_ent (new_av, new_ie);
	  }
    }
}

/* Utility functions called from above routine.  */

static rtx
1585
identity_fn (rtx exp)
Tom Wood committed
1586 1587 1588 1589 1590
{
  return exp;
}

static rtx
1591
zero_fn (rtx exp ATTRIBUTE_UNUSED)
Tom Wood committed
1592 1593 1594 1595 1596
{
  return make_numeric_value (0);
}

static rtx
1597
one_fn (rtx exp ATTRIBUTE_UNUSED)
Tom Wood committed
1598 1599 1600 1601 1602
{
  return make_numeric_value (1);
}

static rtx
1603
max_fn (rtx exp)
Tom Wood committed
1604
{
1605 1606
  int unknown;
  return make_numeric_value (max_attr_value (exp, &unknown));
Tom Wood committed
1607
}
1608

1609 1610 1611 1612 1613 1614 1615
static rtx
min_fn (rtx exp)
{
  int unknown;
  return make_numeric_value (min_attr_value (exp, &unknown));
}

1616
static void
1617
write_length_unit_log (void)
1618
{
1619
  struct attr_desc *length_attr = find_attr (&length_str, 0);
1620 1621 1622
  struct attr_value *av;
  struct insn_ent *ie;
  unsigned int length_unit_log, length_or;
1623
  int unknown = 0;
1624 1625 1626

  if (length_attr == 0)
    return;
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
  length_or = or_attr_value (length_attr->default_val->value, &unknown);
  for (av = length_attr->first_value; av; av = av->next)
    for (ie = av->first_insn; ie; ie = ie->next)
      length_or |= or_attr_value (av->value, &unknown);

  if (unknown)
    length_unit_log = 0;
  else
    {
      length_or = ~length_or;
      for (length_unit_log = 0; length_or & 1; length_or >>= 1)
1638
	length_unit_log++;
1639
    }
1640
  printf ("const int length_unit_log = %u;\n", length_unit_log);
1641
}
1642

Tom Wood committed
1643 1644 1645 1646 1647 1648
/* Take a COND expression and see if any of the conditions in it can be
   simplified.  If any are known true or known false for the particular insn
   code, the COND can be further simplified.

   Also call ourselves on any COND operations that are values of this COND.

1649
   We do not modify EXP; rather, we make and return a new rtx.  */
Tom Wood committed
1650 1651

static rtx
1652
simplify_cond (rtx exp, int insn_code, int insn_index)
Tom Wood committed
1653 1654
{
  int i, j;
1655 1656 1657
  /* We store the desired contents here,
     then build a new expression if they don't match EXP.  */
  rtx defval = XEXP (exp, 1);
1658
  rtx new_defval = XEXP (exp, 1);
1659
  int len = XVECLEN (exp, 0);
1660
  rtx *tests = XNEWVEC (rtx, len);
1661
  int allsame = 1;
1662
  rtx ret;
Tom Wood committed
1663

1664
  /* This lets us free all storage allocated below, if appropriate.  */
1665
  obstack_finish (rtl_obstack);
Tom Wood committed
1666

1667
  memcpy (tests, XVEC (exp, 0)->elem, len * sizeof (rtx));
Tom Wood committed
1668

1669 1670
  /* See if default value needs simplification.  */
  if (GET_CODE (defval) == COND)
1671
    new_defval = simplify_cond (defval, insn_code, insn_index);
Tom Wood committed
1672

1673
  /* Simplify the subexpressions, and see what tests we can get rid of.  */
Tom Wood committed
1674

1675
  for (i = 0; i < len; i += 2)
1676 1677
    {
      rtx newtest, newval;
Tom Wood committed
1678

1679
      /* Simplify this test.  */
1680
      newtest = simplify_test_exp_in_temp (tests[i], insn_code, insn_index);
1681
      tests[i] = newtest;
Tom Wood committed
1682

1683
      newval = tests[i + 1];
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
      /* See if this value may need simplification.  */
      if (GET_CODE (newval) == COND)
	newval = simplify_cond (newval, insn_code, insn_index);

      /* Look for ways to delete or combine this test.  */
      if (newtest == true_rtx)
	{
	  /* If test is true, make this value the default
	     and discard this + any following tests.  */
	  len = i;
1694
	  defval = tests[i + 1];
1695
	  new_defval = newval;
Tom Wood committed
1696 1697
	}

1698
      else if (newtest == false_rtx)
Tom Wood committed
1699
	{
1700 1701
	  /* If test is false, discard it and its value.  */
	  for (j = i; j < len - 2; j++)
1702
	    tests[j] = tests[j + 2];
1703
	  i -= 2;
1704 1705
	  len -= 2;
	}
Tom Wood committed
1706

1707
      else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
1708 1709 1710 1711
	{
	  /* If this value and the value for the prev test are the same,
	     merge the tests.  */

1712 1713
	  tests[i - 2]
	    = insert_right_side (IOR, tests[i - 2], newtest,
1714 1715 1716 1717
				 insn_code, insn_index);

	  /* Delete this test/value.  */
	  for (j = i; j < len - 2; j++)
1718
	    tests[j] = tests[j + 2];
1719
	  len -= 2;
1720
	  i -= 2;
Tom Wood committed
1721 1722
	}

1723
      else
1724
	tests[i + 1] = newval;
1725
    }
Tom Wood committed
1726

1727 1728 1729
  /* If the last test in a COND has the same value
     as the default value, that test isn't needed.  */

1730
  while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
1731 1732 1733 1734 1735 1736 1737
    len -= 2;

  /* See if we changed anything.  */
  if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
    allsame = 0;
  else
    for (i = 0; i < len; i++)
1738
      if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
1739 1740 1741 1742
	{
	  allsame = 0;
	  break;
	}
Tom Wood committed
1743

1744 1745 1746
  if (len == 0)
    {
      if (GET_CODE (defval) == COND)
1747 1748 1749
	ret = simplify_cond (defval, insn_code, insn_index);
      else
	ret = defval;
1750
    }
1751
  else if (allsame)
1752
    ret = exp;
1753 1754
  else
    {
1755
      rtx newexp = rtx_alloc (COND);
1756 1757

      XVEC (newexp, 0) = rtvec_alloc (len);
1758
      memcpy (XVEC (newexp, 0)->elem, tests, len * sizeof (rtx));
1759
      XEXP (newexp, 1) = new_defval;
1760
      ret = newexp;
Tom Wood committed
1761
    }
1762 1763
  free (tests);
  return ret;
Tom Wood committed
1764
}
1765

Tom Wood committed
1766 1767 1768
/* Remove an insn entry from an attribute value.  */

static void
1769
remove_insn_ent (struct attr_value *av, struct insn_ent *ie)
Tom Wood committed
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
{
  struct insn_ent *previe;

  if (av->first_insn == ie)
    av->first_insn = ie->next;
  else
    {
      for (previe = av->first_insn; previe->next != ie; previe = previe->next)
	;
      previe->next = ie->next;
    }

  av->num_insns--;
1783
  if (ie->def->insn_code == -1)
Tom Wood committed
1784
    av->has_asm_insn = 0;
1785 1786

  num_insn_ents--;
Tom Wood committed
1787 1788 1789 1790 1791
}

/* Insert an insn entry in an attribute value list.  */

static void
1792
insert_insn_ent (struct attr_value *av, struct insn_ent *ie)
Tom Wood committed
1793 1794 1795 1796
{
  ie->next = av->first_insn;
  av->first_insn = ie;
  av->num_insns++;
1797
  if (ie->def->insn_code == -1)
Tom Wood committed
1798
    av->has_asm_insn = 1;
1799 1800

  num_insn_ents++;
Tom Wood committed
1801
}
1802

Tom Wood committed
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
/* This is a utility routine to take an expression that is a tree of either
   AND or IOR expressions and insert a new term.  The new term will be
   inserted at the right side of the first node whose code does not match
   the root.  A new node will be created with the root's code.  Its left
   side will be the old right side and its right side will be the new
   term.

   If the `term' is itself a tree, all its leaves will be inserted.  */

static rtx
1813
insert_right_side (enum rtx_code code, rtx exp, rtx term, int insn_code, int insn_index)
Tom Wood committed
1814 1815 1816
{
  rtx newexp;

1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
  /* Avoid consing in some special cases.  */
  if (code == AND && term == true_rtx)
    return exp;
  if (code == AND && term == false_rtx)
    return false_rtx;
  if (code == AND && exp == true_rtx)
    return term;
  if (code == AND && exp == false_rtx)
    return false_rtx;
  if (code == IOR && term == true_rtx)
    return true_rtx;
  if (code == IOR && term == false_rtx)
    return exp;
  if (code == IOR && exp == true_rtx)
    return true_rtx;
  if (code == IOR && exp == false_rtx)
    return term;
1834
  if (attr_equal_p (exp, term))
1835 1836
    return exp;

Tom Wood committed
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
  if (GET_CODE (term) == code)
    {
      exp = insert_right_side (code, exp, XEXP (term, 0),
			       insn_code, insn_index);
      exp = insert_right_side (code, exp, XEXP (term, 1),
			       insn_code, insn_index);

      return exp;
    }

  if (GET_CODE (exp) == code)
    {
1849 1850 1851 1852 1853 1854 1855
      rtx new = insert_right_side (code, XEXP (exp, 1),
				   term, insn_code, insn_index);
      if (new != XEXP (exp, 1))
	/* Make a copy of this expression and call recursively.  */
	newexp = attr_rtx (code, XEXP (exp, 0), new);
      else
	newexp = exp;
Tom Wood committed
1856 1857 1858 1859
    }
  else
    {
      /* Insert the new term.  */
Tom Wood committed
1860
      newexp = attr_rtx (code, exp, term);
1861
    }
Tom Wood committed
1862

1863
  return simplify_test_exp_in_temp (newexp, insn_code, insn_index);
Tom Wood committed
1864
}
1865

Tom Wood committed
1866 1867 1868 1869 1870 1871
/* If we have an expression which AND's a bunch of
	(not (eq_attrq "alternative" "n"))
   terms, we may have covered all or all but one of the possible alternatives.
   If so, we can optimize.  Similarly for IOR's of EQ_ATTR.

   This routine is passed an expression and either AND or IOR.  It returns a
1872
   bitmask indicating which alternatives are mentioned within EXP.  */
Tom Wood committed
1873 1874

static int
1875
compute_alternative_mask (rtx exp, enum rtx_code code)
Tom Wood committed
1876
{
1877
  const char *string;
Tom Wood committed
1878 1879 1880 1881 1882 1883 1884
  if (GET_CODE (exp) == code)
    return compute_alternative_mask (XEXP (exp, 0), code)
	   | compute_alternative_mask (XEXP (exp, 1), code);

  else if (code == AND && GET_CODE (exp) == NOT
	   && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
	   && XSTR (XEXP (exp, 0), 0) == alternative_name)
1885
    string = XSTR (XEXP (exp, 0), 1);
Tom Wood committed
1886 1887 1888

  else if (code == IOR && GET_CODE (exp) == EQ_ATTR
	   && XSTR (exp, 0) == alternative_name)
1889
    string = XSTR (exp, 1);
Tom Wood committed
1890

1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
  else if (GET_CODE (exp) == EQ_ATTR_ALT)
    {
      if (code == AND && XINT (exp, 1))
	return XINT (exp, 0);

      if (code == IOR && !XINT (exp, 1))
	return XINT (exp, 0);

      return 0;
    }
Tom Wood committed
1901 1902
  else
    return 0;
1903 1904 1905 1906

  if (string[1] == 0)
    return 1 << (string[0] - '0');
  return 1 << atoi (string);
Tom Wood committed
1907 1908 1909 1910 1911 1912
}

/* Given I, a single-bit mask, return RTX to compare the `alternative'
   attribute with the value represented by that bit.  */

static rtx
1913
make_alternative_compare (int mask)
Tom Wood committed
1914
{
1915
  return mk_attr_alt (mask);
Tom Wood committed
1916
}
1917

Tom Wood committed
1918 1919 1920 1921
/* If we are processing an (eq_attr "attr" "value") test, we find the value
   of "attr" for this insn code.  From that value, we can compute a test
   showing when the EQ_ATTR will be true.  This routine performs that
   computation.  If a test condition involves an address, we leave the EQ_ATTR
1922
   intact because addresses are only valid for the `length' attribute.
Tom Wood committed
1923

1924 1925
   EXP is the EQ_ATTR expression and VALUE is the value of that attribute
   for the insn corresponding to INSN_CODE and INSN_INDEX.  */
1926

Tom Wood committed
1927
static rtx
1928
evaluate_eq_attr (rtx exp, rtx value, int insn_code, int insn_index)
Tom Wood committed
1929 1930 1931 1932 1933 1934
{
  rtx orexp, andexp;
  rtx right;
  rtx newexp;
  int i;

1935
  switch (GET_CODE (value))
Tom Wood committed
1936
    {
1937
    case CONST_STRING:
1938
      if (! strcmp_check (XSTR (value, 0), XSTR (exp, 1)))
Tom Wood committed
1939 1940 1941
	newexp = true_rtx;
      else
	newexp = false_rtx;
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
      break;
      
    case SYMBOL_REF:
      {
	char *p;
	char string[256];
	
	gcc_assert (GET_CODE (exp) == EQ_ATTR);
	gcc_assert (strlen (XSTR (exp, 0)) + strlen (XSTR (exp, 1)) + 2
		    <= 256);
	
	strcpy (string, XSTR (exp, 0));
	strcat (string, "_");
	strcat (string, XSTR (exp, 1));
	for (p = string; *p; p++)
	  *p = TOUPPER (*p);
	
	newexp = attr_rtx (EQ, value,
			   attr_rtx (SYMBOL_REF,
				     DEF_ATTR_STRING (string)));
	break;
      }
Tom Wood committed
1964

1965 1966 1967 1968 1969
    case COND:
      /* We construct an IOR of all the cases for which the
	 requested attribute value is present.  Since we start with
	 FALSE, if it is not present, FALSE will be returned.
	  
Tom Wood committed
1970
	 Each case is the AND of the NOT's of the previous conditions with the
1971
	 current condition; in the default case the current condition is TRUE.
1972
	  
Tom Wood committed
1973
	 For each possible COND value, call ourselves recursively.
1974
	  
Tom Wood committed
1975
	 The extra TRUE and FALSE expressions will be eliminated by another
Mike Stump committed
1976
	 call to the simplification routine.  */
Tom Wood committed
1977 1978 1979 1980 1981 1982

      orexp = false_rtx;
      andexp = true_rtx;

      for (i = 0; i < XVECLEN (value, 0); i += 2)
	{
1983 1984
	  rtx this = simplify_test_exp_in_temp (XVECEXP (value, 0, i),
						insn_code, insn_index);
1985 1986

	  right = insert_right_side (AND, andexp, this,
Tom Wood committed
1987 1988
				     insn_code, insn_index);
	  right = insert_right_side (AND, right,
1989 1990 1991 1992
				     evaluate_eq_attr (exp,
						       XVECEXP (value, 0,
								i + 1),
						       insn_code, insn_index),
Tom Wood committed
1993 1994 1995 1996 1997
				     insn_code, insn_index);
	  orexp = insert_right_side (IOR, orexp, right,
				     insn_code, insn_index);

	  /* Add this condition into the AND expression.  */
1998
	  newexp = attr_rtx (NOT, this);
Tom Wood committed
1999 2000 2001 2002 2003 2004 2005
	  andexp = insert_right_side (AND, andexp, newexp,
				      insn_code, insn_index);
	}

      /* Handle the default case.  */
      right = insert_right_side (AND, andexp,
				 evaluate_eq_attr (exp, XEXP (value, 1),
2006
						   insn_code, insn_index),
Tom Wood committed
2007 2008
				 insn_code, insn_index);
      newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2009 2010 2011 2012
      break;

    default:
      gcc_unreachable ();
Tom Wood committed
2013 2014
    }

2015
  /* If uses an address, must return original expression.  But set the
2016
     ATTR_IND_SIMPLIFIED_P bit so we don't try to simplify it again.  */
Tom Wood committed
2017 2018 2019 2020 2021

  address_used = 0;
  walk_attr_value (newexp);

  if (address_used)
2022
    {
2023
      if (! ATTR_IND_SIMPLIFIED_P (exp))
2024
	return copy_rtx_unchanging (exp);
2025 2026
      return exp;
    }
Tom Wood committed
2027 2028 2029
  else
    return newexp;
}
2030

Tom Wood committed
2031 2032 2033 2034 2035
/* This routine is called when an AND of a term with a tree of AND's is
   encountered.  If the term or its complement is present in the tree, it
   can be replaced with TRUE or FALSE, respectively.

   Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2036
   be true and hence are complementary.
Tom Wood committed
2037 2038 2039 2040 2041 2042 2043 2044 2045

   There is one special case:  If we see
	(and (not (eq_attr "att" "v1"))
	     (eq_attr "att" "v2"))
   this can be replaced by (eq_attr "att" "v2").  To do this we need to
   replace the term, not anything in the AND tree.  So we pass a pointer to
   the term.  */

static rtx
2046
simplify_and_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
Tom Wood committed
2047 2048 2049 2050 2051 2052 2053 2054
{
  rtx left, right;
  rtx newexp;
  rtx temp;
  int left_eliminates_term, right_eliminates_term;

  if (GET_CODE (exp) == AND)
    {
2055
      left  = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
Tom Wood committed
2056 2057 2058
      right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
      if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
	{
2059
	  newexp = attr_rtx (AND, left, right);
Tom Wood committed
2060

2061
	  exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
Tom Wood committed
2062 2063 2064 2065 2066 2067 2068 2069
	}
    }

  else if (GET_CODE (exp) == IOR)
    {
      /* For the IOR case, we do the same as above, except that we can
         only eliminate `term' if both sides of the IOR would do so.  */
      temp = *pterm;
2070
      left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
Tom Wood committed
2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081
      left_eliminates_term = (temp == true_rtx);

      temp = *pterm;
      right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
      right_eliminates_term = (temp == true_rtx);

      if (left_eliminates_term && right_eliminates_term)
	*pterm = true_rtx;

      if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
	{
2082
	  newexp = attr_rtx (IOR, left, right);
Tom Wood committed
2083

2084
	  exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
Tom Wood committed
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
	}
    }

  /* Check for simplifications.  Do some extra checking here since this
     routine is called so many times.  */

  if (exp == *pterm)
    return true_rtx;

  else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
    return false_rtx;

  else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
    return false_rtx;

2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
  else if (GET_CODE (exp) == EQ_ATTR_ALT && GET_CODE (*pterm) == EQ_ATTR_ALT)
    {
      if (attr_alt_subset_p (*pterm, exp))
	return true_rtx;

      if (attr_alt_subset_of_compl_p (*pterm, exp))
	return false_rtx;

      if (attr_alt_subset_p (exp, *pterm))
	*pterm = true_rtx;
	
      return exp;
    }

Tom Wood committed
2114 2115 2116 2117 2118
  else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
    {
      if (XSTR (exp, 0) != XSTR (*pterm, 0))
	return exp;

2119
      if (! strcmp_check (XSTR (exp, 1), XSTR (*pterm, 1)))
Tom Wood committed
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
	return true_rtx;
      else
	return false_rtx;
    }

  else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
	   && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
    {
      if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
	return exp;

2131
      if (! strcmp_check (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
Tom Wood committed
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142
	return false_rtx;
      else
	return true_rtx;
    }

  else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
	   && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
    {
      if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
	return exp;

2143
      if (! strcmp_check (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
Tom Wood committed
2144 2145 2146 2147 2148 2149 2150
	return false_rtx;
      else
	*pterm = true_rtx;
    }

  else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
    {
2151
      if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
Tom Wood committed
2152 2153 2154 2155 2156
	return true_rtx;
    }

  else if (GET_CODE (exp) == NOT)
    {
2157
      if (attr_equal_p (XEXP (exp, 0), *pterm))
Tom Wood committed
2158 2159 2160 2161 2162
	return false_rtx;
    }

  else if (GET_CODE (*pterm) == NOT)
    {
2163
      if (attr_equal_p (XEXP (*pterm, 0), exp))
Tom Wood committed
2164 2165 2166
	return false_rtx;
    }

2167
  else if (attr_equal_p (exp, *pterm))
Tom Wood committed
2168 2169 2170 2171
    return true_rtx;

  return exp;
}
2172

2173
/* Similar to `simplify_and_tree', but for IOR trees.  */
Tom Wood committed
2174 2175

static rtx
2176
simplify_or_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
Tom Wood committed
2177 2178 2179 2180 2181 2182 2183 2184
{
  rtx left, right;
  rtx newexp;
  rtx temp;
  int left_eliminates_term, right_eliminates_term;

  if (GET_CODE (exp) == IOR)
    {
2185
      left  = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
Tom Wood committed
2186 2187 2188
      right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
      if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
	{
Tom Wood committed
2189
	  newexp = attr_rtx (GET_CODE (exp), left, right);
Tom Wood committed
2190

2191
	  exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
Tom Wood committed
2192 2193 2194 2195 2196 2197 2198 2199
	}
    }

  else if (GET_CODE (exp) == AND)
    {
      /* For the AND case, we do the same as above, except that we can
         only eliminate `term' if both sides of the AND would do so.  */
      temp = *pterm;
2200
      left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
Tom Wood committed
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
      left_eliminates_term = (temp == false_rtx);

      temp = *pterm;
      right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
      right_eliminates_term = (temp == false_rtx);

      if (left_eliminates_term && right_eliminates_term)
	*pterm = false_rtx;

      if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
	{
Tom Wood committed
2212
	  newexp = attr_rtx (GET_CODE (exp), left, right);
Tom Wood committed
2213

2214
	  exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
Tom Wood committed
2215 2216 2217
	}
    }

2218
  if (attr_equal_p (exp, *pterm))
Tom Wood committed
2219 2220
    return false_rtx;

2221
  else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
Tom Wood committed
2222 2223
    return true_rtx;

2224
  else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
Tom Wood committed
2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238
    return true_rtx;

  else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
	   && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
	   && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
    *pterm = false_rtx;

  else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
	   && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
	   && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
    return false_rtx;

  return exp;
}
2239

2240
/* Compute approximate cost of the expression.  Used to decide whether
2241
   expression is cheap enough for inline.  */
2242
static int
2243
attr_rtx_cost (rtx x)
2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
{
  int cost = 0;
  enum rtx_code code;
  if (!x)
    return 0;
  code = GET_CODE (x);
  switch (code)
    {
    case MATCH_OPERAND:
      if (XSTR (x, 1)[0])
	return 10;
      else
	return 0;
2257

2258 2259 2260
    case EQ_ATTR_ALT:
      return 0;

2261 2262
    case EQ_ATTR:
      /* Alternatives don't result into function call.  */
2263
      if (!strcmp_check (XSTR (x, 0), alternative_name))
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
	return 0;
      else
	return 5;
    default:
      {
	int i, j;
	const char *fmt = GET_RTX_FORMAT (code);
	for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
	  {
	    switch (fmt[i])
	      {
	      case 'V':
	      case 'E':
		for (j = 0; j < XVECLEN (x, i); j++)
		  cost += attr_rtx_cost (XVECEXP (x, i, j));
		break;
	      case 'e':
		cost += attr_rtx_cost (XEXP (x, i));
		break;
	      }
	  }
      }
      break;
    }
  return cost;
}
2290 2291

/* Simplify test expression and use temporary obstack in order to avoid
2292 2293
   memory bloat.  Use ATTR_IND_SIMPLIFIED to avoid unnecessary simplifications
   and avoid unnecessary copying if possible.  */
2294 2295

static rtx
2296
simplify_test_exp_in_temp (rtx exp, int insn_code, int insn_index)
2297 2298 2299
{
  rtx x;
  struct obstack *old;
2300
  if (ATTR_IND_SIMPLIFIED_P (exp))
2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
    return exp;
  old = rtl_obstack;
  rtl_obstack = temp_obstack;
  x = simplify_test_exp (exp, insn_code, insn_index);
  rtl_obstack = old;
  if (x == exp || rtl_obstack == temp_obstack)
    return x;
  return attr_copy_rtx (x);
}

2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
/* Returns true if S1 is a subset of S2.  */

static bool
attr_alt_subset_p (rtx s1, rtx s2)
{
  switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
    {
    case (0 << 1) | 0:
      return !(XINT (s1, 0) &~ XINT (s2, 0));

    case (0 << 1) | 1:
      return !(XINT (s1, 0) & XINT (s2, 0));

    case (1 << 1) | 0:
      return false;

    case (1 << 1) | 1:
      return !(XINT (s2, 0) &~ XINT (s1, 0));

    default:
2331
      gcc_unreachable ();
2332 2333 2334 2335 2336
    }
}

/* Returns true if S1 is a subset of complement of S2.  */

2337 2338
static bool
attr_alt_subset_of_compl_p (rtx s1, rtx s2)
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
{
  switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
    {
    case (0 << 1) | 0:
      return !(XINT (s1, 0) & XINT (s2, 0));

    case (0 << 1) | 1:
      return !(XINT (s1, 0) & ~XINT (s2, 0));

    case (1 << 1) | 0:
      return !(XINT (s2, 0) &~ XINT (s1, 0));

    case (1 << 1) | 1:
      return false;

    default:
2355
      gcc_unreachable ();
2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
    }
}

/* Return EQ_ATTR_ALT expression representing intersection of S1 and S2.  */

static rtx
attr_alt_intersection (rtx s1, rtx s2)
{
  rtx result = rtx_alloc (EQ_ATTR_ALT);

  switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
    {
    case (0 << 1) | 0:
      XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
      break;
    case (0 << 1) | 1:
      XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
      break;
    case (1 << 1) | 0:
      XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
      break;
    case (1 << 1) | 1:
      XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
      break;
    default:
2381
      gcc_unreachable ();
2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409
    }
  XINT (result, 1) = XINT (s1, 1) & XINT (s2, 1);

  return result;
}

/* Return EQ_ATTR_ALT expression representing union of S1 and S2.  */

static rtx
attr_alt_union (rtx s1, rtx s2)
{
  rtx result = rtx_alloc (EQ_ATTR_ALT);

  switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
    {
    case (0 << 1) | 0:
      XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
      break;
    case (0 << 1) | 1:
      XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
      break;
    case (1 << 1) | 0:
      XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
      break;
    case (1 << 1) | 1:
      XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
      break;
    default:
2410
      gcc_unreachable ();
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443
    }

  XINT (result, 1) = XINT (s1, 1) | XINT (s2, 1);
  return result;
}

/* Return EQ_ATTR_ALT expression representing complement of S.  */

static rtx
attr_alt_complement (rtx s)
{
  rtx result = rtx_alloc (EQ_ATTR_ALT);

  XINT (result, 0) = XINT (s, 0);
  XINT (result, 1) = 1 - XINT (s, 1);

  return result;
}

/* Return EQ_ATTR_ALT expression representing set containing elements set
   in E.  */

static rtx
mk_attr_alt (int e)
{
  rtx result = rtx_alloc (EQ_ATTR_ALT);

  XINT (result, 0) = e;
  XINT (result, 1) = 0;

  return result;
}

Tom Wood committed
2444 2445 2446 2447
/* Given an expression, see if it can be simplified for a particular insn
   code based on the values of other attributes being tested.  This can
   eliminate nested get_attr_... calls.

2448
   Note that if an endless recursion is specified in the patterns, the
Tom Wood committed
2449 2450 2451 2452 2453
   optimization will loop.  However, it will do so in precisely the cases where
   an infinite recursion loop could occur during compilation.  It's better that
   it occurs here!  */

static rtx
2454
simplify_test_exp (rtx exp, int insn_code, int insn_index)
Tom Wood committed
2455 2456 2457 2458 2459
{
  rtx left, right;
  struct attr_desc *attr;
  struct attr_value *av;
  struct insn_ent *ie;
2460
  struct attr_value_list *iv;
Tom Wood committed
2461 2462
  int i;
  rtx newexp = exp;
2463
  bool left_alt, right_alt;
2464 2465

  /* Don't re-simplify something we already simplified.  */
2466
  if (ATTR_IND_SIMPLIFIED_P (exp) || ATTR_CURR_SIMPLIFIED_P (exp))
2467
    return exp;
Tom Wood committed
2468 2469 2470 2471

  switch (GET_CODE (exp))
    {
    case AND:
2472
      left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2473
      if (left == false_rtx)
Mark Mitchell committed
2474
	return false_rtx;
2475
      right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2476
      if (right == false_rtx)
Mark Mitchell committed
2477
	return false_rtx;
2478

2479 2480 2481 2482 2483 2484 2485
      if (GET_CODE (left) == EQ_ATTR_ALT
	  && GET_CODE (right) == EQ_ATTR_ALT)
	{
	  exp = attr_alt_intersection (left, right);
	  return simplify_test_exp (exp, insn_code, insn_index);
	}

2486 2487 2488 2489 2490 2491
      /* If either side is an IOR and we have (eq_attr "alternative" ..")
	 present on both sides, apply the distributive law since this will
	 yield simplifications.  */
      if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
	  && compute_alternative_mask (left, IOR)
	  && compute_alternative_mask (right, IOR))
Tom Wood committed
2492
	{
2493
	  if (GET_CODE (left) == IOR)
Tom Wood committed
2494
	    {
2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512
	      rtx tem = left;
	      left = right;
	      right = tem;
	    }

	  newexp = attr_rtx (IOR,
			     attr_rtx (AND, left, XEXP (right, 0)),
			     attr_rtx (AND, left, XEXP (right, 1)));

	  return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
	}

      /* Try with the term on both sides.  */
      right = simplify_and_tree (right, &left, insn_code, insn_index);
      if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
	left = simplify_and_tree (left, &right, insn_code, insn_index);

      if (left == false_rtx || right == false_rtx)
Mark Mitchell committed
2513
	return false_rtx;
2514 2515
      else if (left == true_rtx)
	{
2516
	  return right;
2517 2518 2519
	}
      else if (right == true_rtx)
	{
2520
	  return left;
2521 2522 2523 2524
	}
      /* See if all or all but one of the insn's alternatives are specified
	 in this tree.  Optimize if so.  */

2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543
      if (GET_CODE (left) == NOT)
	left_alt = (GET_CODE (XEXP (left, 0)) == EQ_ATTR
		    && XSTR (XEXP (left, 0), 0) == alternative_name);
      else
	left_alt = (GET_CODE (left) == EQ_ATTR_ALT
		    && XINT (left, 1));

      if (GET_CODE (right) == NOT)
	right_alt = (GET_CODE (XEXP (right, 0)) == EQ_ATTR
		     && XSTR (XEXP (right, 0), 0) == alternative_name);
      else
	right_alt = (GET_CODE (right) == EQ_ATTR_ALT
		     && XINT (right, 1));

      if (insn_code >= 0
	  && (GET_CODE (left) == AND
	      || left_alt
	      || GET_CODE (right) == AND
	      || right_alt))
2544 2545 2546
	{
	  i = compute_alternative_mask (exp, AND);
	  if (i & ~insn_alternatives[insn_code])
2547
	    fatal ("invalid alternative specified for pattern number %d",
2548 2549
		   insn_index);

Mike Stump committed
2550
	  /* If all alternatives are excluded, this is false.  */
2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562
	  i ^= insn_alternatives[insn_code];
	  if (i == 0)
	    return false_rtx;
	  else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
	    {
	      /* If just one excluded, AND a comparison with that one to the
		 front of the tree.  The others will be eliminated by
		 optimization.  We do not want to do this if the insn has one
		 alternative and we have tested none of them!  */
	      left = make_alternative_compare (i);
	      right = simplify_and_tree (exp, &left, insn_code, insn_index);
	      newexp = attr_rtx (AND, left, right);
Tom Wood committed
2563 2564 2565 2566

	      return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
	    }
	}
2567 2568 2569 2570 2571 2572 2573

      if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
	{
	  newexp = attr_rtx (AND, left, right);
	  return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
	}
      break;
Tom Wood committed
2574 2575

    case IOR:
2576
      left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2577
      if (left == true_rtx)
Mark Mitchell committed
2578
	return true_rtx;
2579
      right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2580
      if (right == true_rtx)
Mark Mitchell committed
2581
	return true_rtx;
2582

2583 2584 2585 2586 2587 2588 2589
      if (GET_CODE (left) == EQ_ATTR_ALT
	  && GET_CODE (right) == EQ_ATTR_ALT)
	{
	  exp = attr_alt_union (left, right);
	  return simplify_test_exp (exp, insn_code, insn_index);
	}

2590 2591 2592 2593 2594
      right = simplify_or_tree (right, &left, insn_code, insn_index);
      if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
	left = simplify_or_tree (left, &right, insn_code, insn_index);

      if (right == true_rtx || left == true_rtx)
Mark Mitchell committed
2595
	return true_rtx;
2596 2597
      else if (left == false_rtx)
	{
2598
	  return right;
2599 2600 2601
	}
      else if (right == false_rtx)
	{
2602
	  return left;
2603 2604 2605 2606 2607 2608 2609 2610 2611 2612
	}

      /* Test for simple cases where the distributive law is useful.  I.e.,
	    convert (ior (and (x) (y))
			 (and (x) (z)))
	    to      (and (x)
			 (ior (y) (z)))
       */

      else if (GET_CODE (left) == AND && GET_CODE (right) == AND
2613
	       && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626
	{
	  newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));

	  left = XEXP (left, 0);
	  right = newexp;
	  newexp = attr_rtx (AND, left, right);
	  return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
	}

      /* See if all or all but one of the insn's alternatives are specified
	 in this tree.  Optimize if so.  */

      else if (insn_code >= 0
2627
	       && (GET_CODE (left) == IOR
2628 2629
		   || (GET_CODE (left) == EQ_ATTR_ALT
		       && !XINT (left, 1))
2630 2631 2632
		   || (GET_CODE (left) == EQ_ATTR
		       && XSTR (left, 0) == alternative_name)
		   || GET_CODE (right) == IOR
2633 2634
		   || (GET_CODE (right) == EQ_ATTR_ALT
		       && !XINT (right, 1))
2635 2636
		   || (GET_CODE (right) == EQ_ATTR
		       && XSTR (right, 0) == alternative_name)))
2637 2638 2639
	{
	  i = compute_alternative_mask (exp, IOR);
	  if (i & ~insn_alternatives[insn_code])
2640
	    fatal ("invalid alternative specified for pattern number %d",
2641 2642
		   insn_index);

Mike Stump committed
2643
	  /* If all alternatives are included, this is true.  */
2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666
	  i ^= insn_alternatives[insn_code];
	  if (i == 0)
	    return true_rtx;
	  else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
	    {
	      /* If just one excluded, IOR a comparison with that one to the
		 front of the tree.  The others will be eliminated by
		 optimization.  We do not want to do this if the insn has one
		 alternative and we have tested none of them!  */
	      left = make_alternative_compare (i);
	      right = simplify_and_tree (exp, &left, insn_code, insn_index);
	      newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);

	      return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
	    }
	}

      if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
	{
	  newexp = attr_rtx (IOR, left, right);
	  return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
	}
      break;
Tom Wood committed
2667 2668

    case NOT:
2669
      if (GET_CODE (XEXP (exp, 0)) == NOT)
2670 2671 2672 2673 2674 2675
	{
	  left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
				    insn_code, insn_index);
	  return left;
	}

Tom Wood committed
2676 2677 2678 2679 2680
      left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
      if (GET_CODE (left) == NOT)
	return XEXP (left, 0);

      if (left == false_rtx)
Mark Mitchell committed
2681
	return true_rtx;
2682
      if (left == true_rtx)
Mark Mitchell committed
2683
	return false_rtx;
Tom Wood committed
2684

2685 2686 2687 2688 2689 2690
      if (GET_CODE (left) == EQ_ATTR_ALT)
	{
	  exp = attr_alt_complement (left);
	  return simplify_test_exp (exp, insn_code, insn_index);
	}

Tom Wood committed
2691
      /* Try to apply De`Morgan's laws.  */
2692
      if (GET_CODE (left) == IOR)
Tom Wood committed
2693
	{
Tom Wood committed
2694 2695 2696
	  newexp = attr_rtx (AND,
			     attr_rtx (NOT, XEXP (left, 0)),
			     attr_rtx (NOT, XEXP (left, 1)));
Tom Wood committed
2697 2698 2699 2700 2701

	  newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
	}
      else if (GET_CODE (left) == AND)
	{
Tom Wood committed
2702 2703 2704
	  newexp = attr_rtx (IOR,
			     attr_rtx (NOT, XEXP (left, 0)),
			     attr_rtx (NOT, XEXP (left, 1)));
Tom Wood committed
2705 2706 2707 2708 2709

	  newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
	}
      else if (left != XEXP (exp, 0))
	{
Tom Wood committed
2710
	  newexp = attr_rtx (NOT, left);
Tom Wood committed
2711 2712 2713
	}
      break;

2714 2715 2716 2717 2718
    case EQ_ATTR_ALT:
      if (!XINT (exp, 0))
	return XINT (exp, 1) ? true_rtx : false_rtx;
      break;

Tom Wood committed
2719
    case EQ_ATTR:
2720 2721 2722 2723 2724 2725
      if (XSTR (exp, 0) == alternative_name)
	{
	  newexp = mk_attr_alt (1 << atoi (XSTR (exp, 1)));
	  break;
	}

Tom Wood committed
2726 2727
      /* Look at the value for this insn code in the specified attribute.
	 We normally can replace this comparison with the condition that
Kazu Hirata committed
2728
	 would give this insn the values being tested for.  */
2729
      if (insn_code >= 0
2730
	  && (attr = find_attr (&XSTR (exp, 0), 0)) != NULL)
2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760
	{
	  rtx x;

	  av = NULL;
	  if (insn_code_values)
	    {
	      for (iv = insn_code_values[insn_code]; iv; iv = iv->next)
		if (iv->attr == attr)
		  {
		    av = iv->av;
		    break;
		  }
	    }
	  else
	    {
	      for (av = attr->first_value; av; av = av->next)
		for (ie = av->first_insn; ie; ie = ie->next)
		  if (ie->def->insn_code == insn_code)
		    goto got_av;
	    }

	  if (av)
	    {
	    got_av:
	      x = evaluate_eq_attr (exp, av->value, insn_code, insn_index);
	      x = SIMPLIFY_TEST_EXP (x, insn_code, insn_index);
	      if (attr_rtx_cost(x) < 20)
		return x;
	    }
	}
2761
      break;
2762

2763 2764
    default:
      break;
Tom Wood committed
2765 2766 2767 2768 2769
    }

  /* We have already simplified this expression.  Simplifying it again
     won't buy anything unless we weren't given a valid insn code
     to process (i.e., we are canonicalizing something.).  */
2770
  if (insn_code != -2
2771
      && ! ATTR_IND_SIMPLIFIED_P (newexp))
2772
    return copy_rtx_unchanging (newexp);
Tom Wood committed
2773 2774 2775

  return newexp;
}
2776

Tom Wood committed
2777 2778 2779 2780 2781
/* Optimize the attribute lists by seeing if we can determine conditional
   values from the known values of other attributes.  This will save subroutine
   calls during the compilation.  */

static void
2782
optimize_attrs (void)
Tom Wood committed
2783 2784 2785
{
  struct attr_desc *attr;
  struct attr_value *av;
2786
  struct insn_ent *ie;
Tom Wood committed
2787
  rtx newexp;
2788
  int i;
2789
  struct attr_value_list *ivbuf;
2790 2791
  struct attr_value_list *iv;

2792 2793
  /* For each insn code, make a list of all the insn_ent's for it,
     for all values for all attributes.  */
2794

2795 2796
  if (num_insn_ents == 0)
    return;
2797

2798
  /* Make 2 extra elements, for "code" values -2 and -1.  */
2799
  insn_code_values = XCNEWVEC (struct attr_value_list *, insn_code_number + 2);
2800

2801 2802
  /* Offset the table address so we can index by -2 or -1.  */
  insn_code_values += 2;
2803

2804
  iv = ivbuf = XNEWVEC (struct attr_value_list, num_insn_ents);
2805

2806 2807 2808 2809 2810 2811 2812 2813
  for (i = 0; i < MAX_ATTRS_INDEX; i++)
    for (attr = attrs[i]; attr; attr = attr->next)
      for (av = attr->first_value; av; av = av->next)
	for (ie = av->first_insn; ie; ie = ie->next)
	  {
	    iv->attr = attr;
	    iv->av = av;
	    iv->ie = ie;
2814 2815
	    iv->next = insn_code_values[ie->def->insn_code];
	    insn_code_values[ie->def->insn_code] = iv;
2816 2817
	    iv++;
	  }
2818

2819
  /* Sanity check on num_insn_ents.  */
2820
  gcc_assert (iv == ivbuf + num_insn_ents);
2821

2822 2823 2824 2825 2826 2827 2828
  /* Process one insn code at a time.  */
  for (i = -2; i < insn_code_number; i++)
    {
      /* Clear the ATTR_CURR_SIMPLIFIED_P flag everywhere relevant.
	 We use it to mean "already simplified for this insn".  */
      for (iv = insn_code_values[i]; iv; iv = iv->next)
	clear_struct_flag (iv->av->value);
2829

2830
      for (iv = insn_code_values[i]; iv; iv = iv->next)
2831
	{
2832 2833 2834 2835 2836 2837
	  struct obstack *old = rtl_obstack;

	  attr = iv->attr;
	  av = iv->av;
	  ie = iv->ie;
	  if (GET_CODE (av->value) != COND)
2838
	    continue;
2839

2840 2841 2842 2843
	  rtl_obstack = temp_obstack;
	  newexp = av->value;
	  while (GET_CODE (newexp) == COND)
	    {
2844 2845
	      rtx newexp2 = simplify_cond (newexp, ie->def->insn_code,
					   ie->def->insn_index);
2846 2847 2848 2849 2850 2851 2852 2853 2854 2855
	      if (newexp2 == newexp)
		break;
	      newexp = newexp2;
	    }

	  rtl_obstack = old;
	  if (newexp != av->value)
	    {
	      newexp = attr_copy_rtx (newexp);
	      remove_insn_ent (av, ie);
2856
	      av = get_attr_value (newexp, attr, ie->def->insn_code);
2857 2858 2859 2860
	      iv->av = av;
	      insert_insn_ent (av, ie);
	    }
	}
2861
    }
2862 2863 2864

  free (ivbuf);
  free (insn_code_values - 2);
2865
  insn_code_values = NULL;
2866
}
2867

2868
/* Clear the ATTR_CURR_SIMPLIFIED_P flag in EXP and its subexpressions.  */
2869

2870
static void
2871
clear_struct_flag (rtx x)
2872
{
2873 2874 2875 2876
  int i;
  int j;
  enum rtx_code code;
  const char *fmt;
2877

2878 2879
  ATTR_CURR_SIMPLIFIED_P (x) = 0;
  if (ATTR_IND_SIMPLIFIED_P (x))
2880 2881 2882 2883 2884 2885 2886 2887 2888
    return;

  code = GET_CODE (x);

  switch (code)
    {
    case REG:
    case CONST_INT:
    case CONST_DOUBLE:
2889
    case CONST_VECTOR:
2890 2891 2892 2893 2894
    case SYMBOL_REF:
    case CODE_LABEL:
    case PC:
    case CC0:
    case EQ_ATTR:
2895
    case ATTR_FLAG:
2896
      return;
2897

2898 2899
    default:
      break;
2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919
    }

  /* Compare the elements.  If any pair of corresponding elements
     fail to match, return 0 for the whole things.  */

  fmt = GET_RTX_FORMAT (code);
  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
    {
      switch (fmt[i])
	{
	case 'V':
	case 'E':
	  for (j = 0; j < XVECLEN (x, i); j++)
	    clear_struct_flag (XVECEXP (x, i, j));
	  break;

	case 'e':
	  clear_struct_flag (XEXP (x, i));
	  break;
	}
Tom Wood committed
2920 2921
    }
}
2922

Tom Wood committed
2923 2924 2925
/* Create table entries for DEFINE_ATTR.  */

static void
2926
gen_attr (rtx exp, int lineno)
Tom Wood committed
2927 2928 2929
{
  struct attr_desc *attr;
  struct attr_value *av;
2930
  const char *name_ptr;
Tom Wood committed
2931 2932 2933 2934
  char *p;

  /* Make a new attribute structure.  Check for duplicate by looking at
     attr->default_val, since it is initialized by this routine.  */
2935
  attr = find_attr (&XSTR (exp, 0), 1);
Tom Wood committed
2936
  if (attr->default_val)
2937 2938 2939 2940 2941 2942 2943 2944
    {
      message_with_line (lineno, "duplicate definition for attribute %s",
			 attr->name);
      message_with_line (attr->lineno, "previous definition");
      have_error = 1;
      return;
    }
  attr->lineno = lineno;
Tom Wood committed
2945 2946

  if (*XSTR (exp, 1) == '\0')
2947
    attr->is_numeric = 1;
Tom Wood committed
2948 2949 2950 2951 2952
  else
    {
      name_ptr = XSTR (exp, 1);
      while ((p = next_comma_elt (&name_ptr)) != NULL)
	{
2953
	  av = oballoc (sizeof (struct attr_value));
Tom Wood committed
2954
	  av->value = attr_rtx (CONST_STRING, p);
Tom Wood committed
2955 2956 2957 2958 2959 2960 2961 2962
	  av->next = attr->first_value;
	  attr->first_value = av;
	  av->first_insn = NULL;
	  av->num_insns = 0;
	  av->has_asm_insn = 0;
	}
    }

Tom Wood committed
2963 2964 2965 2966
  if (GET_CODE (XEXP (exp, 2)) == CONST)
    {
      attr->is_const = 1;
      if (attr->is_numeric)
2967 2968 2969 2970 2971 2972
	{
	  message_with_line (lineno,
			     "constant attributes may not take numeric values");
	  have_error = 1;
	}

Tom Wood committed
2973 2974 2975 2976
      /* Get rid of the CONST node.  It is allowed only at top-level.  */
      XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
    }

2977
  if (! strcmp_check (attr->name, length_str) && ! attr->is_numeric)
2978
    {
2979 2980
      message_with_line (lineno,
			 "`length' attribute must take numeric values");
2981 2982
      have_error = 1;
    }
Tom Wood committed
2983

Mike Stump committed
2984
  /* Set up the default value.  */
2985
  XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
Tom Wood committed
2986 2987
  attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
}
2988

Tom Wood committed
2989 2990 2991 2992 2993
/* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
   alternatives in the constraints.  Assume all MATCH_OPERANDs have the same
   number of alternatives as this should be checked elsewhere.  */

static int
2994
count_alternatives (rtx exp)
Tom Wood committed
2995 2996
{
  int i, j, n;
2997
  const char *fmt;
2998

Tom Wood committed
2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025
  if (GET_CODE (exp) == MATCH_OPERAND)
    return n_comma_elts (XSTR (exp, 2));

  for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
       i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
    switch (*fmt++)
      {
      case 'e':
      case 'u':
	n = count_alternatives (XEXP (exp, i));
	if (n)
	  return n;
	break;

      case 'E':
      case 'V':
	if (XVEC (exp, i) != NULL)
	  for (j = 0; j < XVECLEN (exp, i); j++)
	    {
	      n = count_alternatives (XVECEXP (exp, i, j));
	      if (n)
		return n;
	    }
      }

  return 0;
}
3026

3027
/* Returns nonzero if the given expression contains an EQ_ATTR with the
Tom Wood committed
3028 3029 3030
   `alternative' attribute.  */

static int
3031
compares_alternatives_p (rtx exp)
Tom Wood committed
3032 3033
{
  int i, j;
3034
  const char *fmt;
Tom Wood committed
3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057

  if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
    return 1;

  for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
       i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
    switch (*fmt++)
      {
      case 'e':
      case 'u':
	if (compares_alternatives_p (XEXP (exp, i)))
	  return 1;
	break;

      case 'E':
	for (j = 0; j < XVECLEN (exp, i); j++)
	  if (compares_alternatives_p (XVECEXP (exp, i, j)))
	    return 1;
	break;
      }

  return 0;
}
3058

Tom Wood committed
3059 3060 3061
/* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES.  */

static void
3062
gen_insn (rtx exp, int lineno)
Tom Wood committed
3063 3064 3065
{
  struct insn_def *id;

3066
  id = oballoc (sizeof (struct insn_def));
Tom Wood committed
3067 3068 3069
  id->next = defs;
  defs = id;
  id->def = exp;
3070
  id->lineno = lineno;
Tom Wood committed
3071 3072 3073 3074

  switch (GET_CODE (exp))
    {
    case DEFINE_INSN:
Clinton Popetz committed
3075 3076
      id->insn_code = insn_code_number;
      id->insn_index = insn_index_number;
Tom Wood committed
3077 3078 3079 3080 3081 3082 3083
      id->num_alternatives = count_alternatives (exp);
      if (id->num_alternatives == 0)
	id->num_alternatives = 1;
      id->vec_idx = 4;
      break;

    case DEFINE_PEEPHOLE:
Clinton Popetz committed
3084 3085
      id->insn_code = insn_code_number;
      id->insn_index = insn_index_number;
Tom Wood committed
3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098
      id->num_alternatives = count_alternatives (exp);
      if (id->num_alternatives == 0)
	id->num_alternatives = 1;
      id->vec_idx = 3;
      break;

    case DEFINE_ASM_ATTRIBUTES:
      id->insn_code = -1;
      id->insn_index = -1;
      id->num_alternatives = 1;
      id->vec_idx = 0;
      got_define_asm_attributes = 1;
      break;
3099

3100
    default:
3101
      gcc_unreachable ();
Tom Wood committed
3102 3103
    }
}
3104

Tom Wood committed
3105 3106 3107 3108
/* Process a DEFINE_DELAY.  Validate the vector length, check if annul
   true or annul false is specified, and make a `struct delay_desc'.  */

static void
3109
gen_delay (rtx def, int lineno)
Tom Wood committed
3110 3111 3112 3113 3114
{
  struct delay_desc *delay;
  int i;

  if (XVECLEN (def, 1) % 3 != 0)
3115 3116
    {
      message_with_line (lineno,
3117
			 "number of elements in DEFINE_DELAY must be multiple of three");
3118 3119 3120
      have_error = 1;
      return;
    }
Tom Wood committed
3121 3122 3123 3124 3125 3126 3127 3128

  for (i = 0; i < XVECLEN (def, 1); i += 3)
    {
      if (XVECEXP (def, 1, i + 1))
	have_annul_true = 1;
      if (XVECEXP (def, 1, i + 2))
	have_annul_false = 1;
    }
3129

3130
  delay = oballoc (sizeof (struct delay_desc));
Tom Wood committed
3131 3132 3133
  delay->def = def;
  delay->num = ++num_delays;
  delay->next = delays;
3134
  delay->lineno = lineno;
Tom Wood committed
3135 3136
  delays = delay;
}
3137

3138
/* Given a piece of RTX, print a C expression to test its truth value.
3139
   We use AND and IOR both for logical and bit-wise operations, so
Tom Wood committed
3140
   interpret them as logical unless they are inside a comparison expression.
3141
   The first bit of FLAGS will be nonzero in that case.
3142 3143 3144

   Set the second bit of FLAGS to make references to attribute values use
   a cached local variable instead of calling a function.  */
Tom Wood committed
3145 3146

static void
3147
write_test_expr (rtx exp, int flags)
Tom Wood committed
3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160
{
  int comparison_operator = 0;
  RTX_CODE code;
  struct attr_desc *attr;

  /* In order not to worry about operator precedence, surround our part of
     the expression with parentheses.  */

  printf ("(");
  code = GET_CODE (exp);
  switch (code)
    {
    /* Binary operators.  */
3161 3162 3163 3164 3165
    case GEU: case GTU:
    case LEU: case LTU:
      printf ("(unsigned) ");
      /* Fall through.  */

Tom Wood committed
3166
    case EQ: case NE:
3167 3168
    case GE: case GT:
    case LE: case LT:
Tom Wood committed
3169 3170 3171 3172
      comparison_operator = 1;

    case PLUS:   case MINUS:  case MULT:     case DIV:      case MOD:
    case AND:    case IOR:    case XOR:
3173
    case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3174
      write_test_expr (XEXP (exp, 0), flags | comparison_operator);
Tom Wood committed
3175
      switch (code)
3176
	{
Tom Wood committed
3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219
	case EQ:
	  printf (" == ");
	  break;
	case NE:
	  printf (" != ");
	  break;
	case GE:
	  printf (" >= ");
	  break;
	case GT:
	  printf (" > ");
	  break;
	case GEU:
	  printf (" >= (unsigned) ");
	  break;
	case GTU:
	  printf (" > (unsigned) ");
	  break;
	case LE:
	  printf (" <= ");
	  break;
	case LT:
	  printf (" < ");
	  break;
	case LEU:
	  printf (" <= (unsigned) ");
	  break;
	case LTU:
	  printf (" < (unsigned) ");
	  break;
	case PLUS:
	  printf (" + ");
	  break;
	case MINUS:
	  printf (" - ");
	  break;
	case MULT:
	  printf (" * ");
	  break;
	case DIV:
	  printf (" / ");
	  break;
	case MOD:
3220
	  printf (" %% ");
Tom Wood committed
3221 3222
	  break;
	case AND:
3223
	  if (flags & 1)
Tom Wood committed
3224 3225 3226 3227 3228
	    printf (" & ");
	  else
	    printf (" && ");
	  break;
	case IOR:
3229
	  if (flags & 1)
Tom Wood committed
3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243
	    printf (" | ");
	  else
	    printf (" || ");
	  break;
	case XOR:
	  printf (" ^ ");
	  break;
	case ASHIFT:
	  printf (" << ");
	  break;
	case LSHIFTRT:
	case ASHIFTRT:
	  printf (" >> ");
	  break;
3244
	default:
3245
	  gcc_unreachable ();
3246
	}
Tom Wood committed
3247

3248
      write_test_expr (XEXP (exp, 1), flags | comparison_operator);
Tom Wood committed
3249 3250 3251 3252
      break;

    case NOT:
      /* Special-case (not (eq_attrq "alternative" "x")) */
3253
      if (! (flags & 1) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
Tom Wood committed
3254 3255 3256 3257 3258 3259 3260 3261
	  && XSTR (XEXP (exp, 0), 0) == alternative_name)
	{
	  printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
	  break;
	}

      /* Otherwise, fall through to normal unary operator.  */

3262
    /* Unary operators.  */
Tom Wood committed
3263 3264 3265 3266
    case ABS:  case NEG:
      switch (code)
	{
	case NOT:
3267
	  if (flags & 1)
Tom Wood committed
3268 3269 3270 3271 3272 3273 3274 3275 3276 3277
	    printf ("~ ");
	  else
	    printf ("! ");
	  break;
	case ABS:
	  printf ("abs ");
	  break;
	case NEG:
	  printf ("-");
	  break;
3278
	default:
3279
	  gcc_unreachable ();
Tom Wood committed
3280 3281
	}

3282
      write_test_expr (XEXP (exp, 0), flags);
Tom Wood committed
3283 3284
      break;

3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330
    case EQ_ATTR_ALT:
	{
	  int set = XINT (exp, 0), bit = 0;

	  if (flags & 1)
	    fatal ("EQ_ATTR_ALT not valid inside comparison");

	  if (!set)
	    fatal ("Empty EQ_ATTR_ALT should be optimized out");

	  if (!(set & (set - 1)))
	    {
	      if (!(set & 0xffff))
		{
		  bit += 16;
		  set >>= 16;
		}
	      if (!(set & 0xff))
		{
		  bit += 8;
		  set >>= 8;
		}
	      if (!(set & 0xf))
		{
		  bit += 4;
		  set >>= 4;
		}
	      if (!(set & 0x3))
		{
		  bit += 2;
		  set >>= 2;
		}
	      if (!(set & 1))
		bit++;

	      printf ("which_alternative %s= %d",
		      XINT (exp, 1) ? "!" : "=", bit);
	    }
	  else
	    {
	      printf ("%s((1 << which_alternative) & 0x%x)",
		      XINT (exp, 1) ? "!" : "", set);
	    }
	}
      break;

Tom Wood committed
3331 3332 3333 3334
    /* Comparison test of an attribute with a value.  Most of these will
       have been removed by optimization.   Handle "alternative"
       specially and give error if EQ_ATTR present inside a comparison.  */
    case EQ_ATTR:
3335
      if (flags & 1)
Tom Wood committed
3336 3337 3338 3339 3340 3341 3342 3343
	fatal ("EQ_ATTR not valid inside comparison");

      if (XSTR (exp, 0) == alternative_name)
	{
	  printf ("which_alternative == %s", XSTR (exp, 1));
	  break;
	}

3344
      attr = find_attr (&XSTR (exp, 0), 0);
3345
      gcc_assert (attr);
3346 3347 3348 3349 3350

      /* Now is the time to expand the value of a constant attribute.  */
      if (attr->is_const)
	{
	  write_test_expr (evaluate_eq_attr (exp, attr->default_val->value,
3351
					     -2, -2),
3352
			   flags);
3353 3354 3355
	}
      else
	{
3356 3357 3358 3359 3360 3361
	  if (flags & 2)
	    printf ("attr_%s", attr->name);
	  else
	    printf ("get_attr_%s (insn)", attr->name);
	  printf (" == ");
	  write_attr_valueq (attr, XSTR (exp, 1));
3362
	}
Tom Wood committed
3363 3364
      break;

3365 3366
    /* Comparison test of flags for define_delays.  */
    case ATTR_FLAG:
3367
      if (flags & 1)
3368 3369 3370 3371
	fatal ("ATTR_FLAG not valid inside comparison");
      printf ("(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
      break;

Tom Wood committed
3372 3373 3374 3375
    /* See if an operand matches a predicate.  */
    case MATCH_OPERAND:
      /* If only a mode is given, just ensure the mode matches the operand.
	 If neither a mode nor predicate is given, error.  */
3376
      if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
Tom Wood committed
3377 3378
	{
	  if (GET_MODE (exp) == VOIDmode)
3379
	    fatal ("null MATCH_OPERAND specified as test");
Tom Wood committed
3380 3381 3382 3383 3384 3385 3386 3387 3388
	  else
	    printf ("GET_MODE (operands[%d]) == %smode",
		    XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
	}
      else
	printf ("%s (operands[%d], %smode)",
		XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
      break;

Mike Stump committed
3389
    /* Constant integer.  */
Tom Wood committed
3390
    case CONST_INT:
3391
      printf (HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
Tom Wood committed
3392 3393
      break;

Mike Stump committed
3394
    /* A random C expression.  */
Tom Wood committed
3395
    case SYMBOL_REF:
3396
      print_c_condition (XSTR (exp, 0));
Tom Wood committed
3397 3398 3399 3400
      break;

    /* The address of the branch target.  */
    case MATCH_DUP:
3401
      printf ("INSN_ADDRESSES_SET_P () ? INSN_ADDRESSES (INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])) : 0",
3402
	      XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
Tom Wood committed
3403 3404 3405
      break;

    case PC:
3406 3407 3408 3409 3410
      /* The address of the current insn.  We implement this actually as the
	 address of the current insn for backward branches, but the last
	 address of the next insn for forward branches, and both with
	 adjustments that account for the worst-case possible stretching of
	 intervening alignments between this insn and its destination.  */
3411
      printf ("insn_current_reference_address (insn)");
Tom Wood committed
3412 3413
      break;

3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425
    case CONST_STRING:
      printf ("%s", XSTR (exp, 0));
      break;

    case IF_THEN_ELSE:
      write_test_expr (XEXP (exp, 0), flags & 2);
      printf (" ? ");
      write_test_expr (XEXP (exp, 1), flags | 1);
      printf (" : ");
      write_test_expr (XEXP (exp, 2), flags | 1);
      break;

Tom Wood committed
3426 3427 3428 3429 3430 3431 3432
    default:
      fatal ("bad RTX code `%s' in attribute calculation\n",
	     GET_RTX_NAME (code));
    }

  printf (")");
}
3433

Tom Wood committed
3434
/* Given an attribute value, return the maximum CONST_STRING argument
3435
   encountered.  Set *UNKNOWNP and return INT_MAX if the value is unknown.  */
Tom Wood committed
3436 3437

static int
3438
max_attr_value (rtx exp, int *unknownp)
Tom Wood committed
3439
{
3440 3441
  int current_max;
  int i, n;
Tom Wood committed
3442

3443
  switch (GET_CODE (exp))
Tom Wood committed
3444
    {
3445 3446 3447 3448 3449 3450
    case CONST_STRING:
      current_max = atoi (XSTR (exp, 0));
      break;

    case COND:
      current_max = max_attr_value (XEXP (exp, 1), unknownp);
Tom Wood committed
3451 3452
      for (i = 0; i < XVECLEN (exp, 0); i += 2)
	{
3453
	  n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
Tom Wood committed
3454 3455 3456
	  if (n > current_max)
	    current_max = n;
	}
3457
      break;
Tom Wood committed
3458

3459 3460 3461
    case IF_THEN_ELSE:
      current_max = max_attr_value (XEXP (exp, 1), unknownp);
      n = max_attr_value (XEXP (exp, 2), unknownp);
Tom Wood committed
3462 3463
      if (n > current_max)
	current_max = n;
3464
      break;
Tom Wood committed
3465

3466 3467 3468 3469
    default:
      *unknownp = 1;
      current_max = INT_MAX;
      break;
3470 3471
    }

Tom Wood committed
3472 3473
  return current_max;
}
3474

3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515
/* Given an attribute value, return the minimum CONST_STRING argument
   encountered.  Set *UNKNOWNP and return 0 if the value is unknown.  */

static int
min_attr_value (rtx exp, int *unknownp)
{
  int current_min;
  int i, n;

  switch (GET_CODE (exp))
    {
    case CONST_STRING:
      current_min = atoi (XSTR (exp, 0));
      break;

    case COND:
      current_min = min_attr_value (XEXP (exp, 1), unknownp);
      for (i = 0; i < XVECLEN (exp, 0); i += 2)
	{
	  n = min_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
	  if (n < current_min)
	    current_min = n;
	}
      break;

    case IF_THEN_ELSE:
      current_min = min_attr_value (XEXP (exp, 1), unknownp);
      n = min_attr_value (XEXP (exp, 2), unknownp);
      if (n < current_min)
	current_min = n;
      break;

    default:
      *unknownp = 1;
      current_min = INT_MAX;
      break;
    }

  return current_min;
}

3516
/* Given an attribute value, return the result of ORing together all
3517 3518
   CONST_STRING arguments encountered.  Set *UNKNOWNP and return -1
   if the numeric value is not known.  */
3519 3520

static int
3521
or_attr_value (rtx exp, int *unknownp)
3522
{
3523
  int current_or;
3524 3525
  int i;

3526
  switch (GET_CODE (exp))
3527
    {
3528 3529 3530 3531 3532 3533
    case CONST_STRING:
      current_or = atoi (XSTR (exp, 0));
      break;

    case COND:
      current_or = or_attr_value (XEXP (exp, 1), unknownp);
3534
      for (i = 0; i < XVECLEN (exp, 0); i += 2)
3535 3536
	current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
      break;
3537

3538 3539 3540 3541
    case IF_THEN_ELSE:
      current_or = or_attr_value (XEXP (exp, 1), unknownp);
      current_or |= or_attr_value (XEXP (exp, 2), unknownp);
      break;
3542

3543 3544 3545 3546
    default:
      *unknownp = 1;
      current_or = -1;
      break;
3547 3548 3549 3550
    }

  return current_or;
}
3551

Tom Wood committed
3552 3553 3554 3555 3556 3557 3558
/* Scan an attribute value, possibly a conditional, and record what actions
   will be required to do any conditional tests in it.

   Specifically, set
	`must_extract'	  if we need to extract the insn operands
	`must_constrain'  if we must compute `which_alternative'
	`address_used'	  if an address expression was used
3559
	`length_used'	  if an (eq_attr "length" ...) was used
Tom Wood committed
3560 3561 3562
 */

static void
3563
walk_attr_value (rtx exp)
Tom Wood committed
3564
{
3565 3566
  int i, j;
  const char *fmt;
Tom Wood committed
3567 3568 3569 3570 3571 3572 3573 3574 3575
  RTX_CODE code;

  if (exp == NULL)
    return;

  code = GET_CODE (exp);
  switch (code)
    {
    case SYMBOL_REF:
3576
      if (! ATTR_IND_SIMPLIFIED_P (exp))
Tom Wood committed
3577 3578 3579 3580
	/* Since this is an arbitrary expression, it can look at anything.
	   However, constant expressions do not depend on any particular
	   insn.  */
	must_extract = must_constrain = 1;
Tom Wood committed
3581 3582 3583 3584 3585 3586
      return;

    case MATCH_OPERAND:
      must_extract = 1;
      return;

3587 3588 3589 3590
    case EQ_ATTR_ALT:
      must_extract = must_constrain = 1;
      break;

Tom Wood committed
3591 3592 3593
    case EQ_ATTR:
      if (XSTR (exp, 0) == alternative_name)
	must_extract = must_constrain = 1;
3594
      else if (strcmp_check (XSTR (exp, 0), length_str) == 0)
3595
	length_used = 1;
Tom Wood committed
3596 3597 3598
      return;

    case MATCH_DUP:
3599 3600 3601 3602
      must_extract = 1;
      address_used = 1;
      return;

Tom Wood committed
3603 3604 3605
    case PC:
      address_used = 1;
      return;
3606 3607 3608

    case ATTR_FLAG:
      return;
3609 3610 3611

    default:
      break;
Tom Wood committed
3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628
    }

  for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
    switch (*fmt++)
      {
      case 'e':
      case 'u':
	walk_attr_value (XEXP (exp, i));
	break;

      case 'E':
	if (XVEC (exp, i) != NULL)
	  for (j = 0; j < XVECLEN (exp, i); j++)
	    walk_attr_value (XVECEXP (exp, i, j));
	break;
      }
}
3629

Tom Wood committed
3630 3631 3632
/* Write out a function to obtain the attribute for a given INSN.  */

static void
3633
write_attr_get (struct attr_desc *attr)
Tom Wood committed
3634 3635 3636 3637
{
  struct attr_value *av, *common_av;

  /* Find the most used attribute value.  Handle that as the `default' of the
Mike Stump committed
3638
     switch we will generate.  */
Tom Wood committed
3639 3640 3641 3642
  common_av = find_most_used (attr);

  /* Write out start of function, then all values with explicit `case' lines,
     then a `default', then the value with the most uses.  */
3643
  if (!attr->is_numeric)
Tom Wood committed
3644
    printf ("enum attr_%s\n", attr->name);
3645 3646
  else
    printf ("int\n");
Tom Wood committed
3647 3648 3649 3650

  /* If the attribute name starts with a star, the remainder is the name of
     the subroutine to use, instead of `get_attr_...'.  */
  if (attr->name[0] == '*')
3651
    printf ("%s (rtx insn ATTRIBUTE_UNUSED)\n", &attr->name[1]);
Tom Wood committed
3652
  else if (attr->is_const == 0)
3653
    printf ("get_attr_%s (rtx insn ATTRIBUTE_UNUSED)\n", attr->name);
Tom Wood committed
3654 3655
  else
    {
3656
      printf ("get_attr_%s (void)\n", attr->name);
Tom Wood committed
3657 3658 3659
      printf ("{\n");

      for (av = attr->first_value; av; av = av->next)
3660
	if (av->num_insns == 1)
Tom Wood committed
3661
	  write_attr_set (attr, 2, av->value, "return", ";",
3662 3663
			  true_rtx, av->first_insn->def->insn_code,
			  av->first_insn->def->insn_index);
3664 3665 3666
	else if (av->num_insns != 0)
	  write_attr_set (attr, 2, av->value, "return", ";",
			  true_rtx, -2, 0);
Tom Wood committed
3667 3668 3669 3670

      printf ("}\n\n");
      return;
    }
3671

Tom Wood committed
3672
  printf ("{\n");
3673 3674
  printf ("  switch (recog_memoized (insn))\n");
  printf ("    {\n");
Tom Wood committed
3675

3676 3677 3678
  for (av = attr->first_value; av; av = av->next)
    if (av != common_av)
      write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
3679

3680 3681
  write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
  printf ("    }\n}\n\n");
Tom Wood committed
3682
}
3683

Tom Wood committed
3684 3685 3686 3687 3688 3689
/* Given an AND tree of known true terms (because we are inside an `if' with
   that as the condition or are in an `else' clause) and an expression,
   replace any known true terms with TRUE.  Use `simplify_and_tree' to do
   the bulk of the work.  */

static rtx
3690
eliminate_known_true (rtx known_true, rtx exp, int insn_code, int insn_index)
Tom Wood committed
3691 3692 3693
{
  rtx term;

3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709
  known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);

  if (GET_CODE (known_true) == AND)
    {
      exp = eliminate_known_true (XEXP (known_true, 0), exp,
				  insn_code, insn_index);
      exp = eliminate_known_true (XEXP (known_true, 1), exp,
				  insn_code, insn_index);
    }
  else
    {
      term = known_true;
      exp = simplify_and_tree (exp, &term, insn_code, insn_index);
    }

  return exp;
Tom Wood committed
3710
}
3711

Tom Wood committed
3712 3713 3714 3715 3716 3717
/* Write out a series of tests and assignment statements to perform tests and
   sets of an attribute value.  We are passed an indentation amount and prefix
   and suffix strings to write around each attribute value (e.g., "return"
   and ";").  */

static void
3718 3719 3720
write_attr_set (struct attr_desc *attr, int indent, rtx value,
		const char *prefix, const char *suffix, rtx known_true,
		int insn_code, int insn_index)
Tom Wood committed
3721
{
3722
  if (GET_CODE (value) == COND)
Tom Wood committed
3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739
    {
      /* Assume the default value will be the default of the COND unless we
	 find an always true expression.  */
      rtx default_val = XEXP (value, 1);
      rtx our_known_true = known_true;
      rtx newexp;
      int first_if = 1;
      int i;

      for (i = 0; i < XVECLEN (value, 0); i += 2)
	{
	  rtx testexp;
	  rtx inner_true;

	  testexp = eliminate_known_true (our_known_true,
					  XVECEXP (value, 0, i),
					  insn_code, insn_index);
Tom Wood committed
3740
	  newexp = attr_rtx (NOT, testexp);
3741 3742
	  newexp = insert_right_side (AND, our_known_true, newexp,
				      insn_code, insn_index);
Tom Wood committed
3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769

	  /* If the test expression is always true or if the next `known_true'
	     expression is always false, this is the last case, so break
	     out and let this value be the `else' case.  */
	  if (testexp == true_rtx || newexp == false_rtx)
	    {
	      default_val = XVECEXP (value, 0, i + 1);
	      break;
	    }

	  /* Compute the expression to pass to our recursive call as being
	     known true.  */
	  inner_true = insert_right_side (AND, our_known_true,
					  testexp, insn_code, insn_index);

	  /* If this is always false, skip it.  */
	  if (inner_true == false_rtx)
	    continue;

	  write_indent (indent);
	  printf ("%sif ", first_if ? "" : "else ");
	  first_if = 0;
	  write_test_expr (testexp, 0);
	  printf ("\n");
	  write_indent (indent + 2);
	  printf ("{\n");

3770
	  write_attr_set (attr, indent + 4,
Tom Wood committed
3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795
			  XVECEXP (value, 0, i + 1), prefix, suffix,
			  inner_true, insn_code, insn_index);
	  write_indent (indent + 2);
	  printf ("}\n");
	  our_known_true = newexp;
	}

      if (! first_if)
	{
	  write_indent (indent);
	  printf ("else\n");
	  write_indent (indent + 2);
	  printf ("{\n");
	}

      write_attr_set (attr, first_if ? indent : indent + 4, default_val,
		      prefix, suffix, our_known_true, insn_code, insn_index);

      if (! first_if)
	{
	  write_indent (indent + 2);
	  printf ("}\n");
	}
    }
  else
3796 3797 3798 3799 3800 3801
    {
      write_indent (indent);
      printf ("%s ", prefix);
      write_attr_value (attr, value);
      printf ("%s\n", suffix);
    }
Tom Wood committed
3802
}
3803

3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822
/* Write a series of case statements for every instruction in list IE.
   INDENT is the amount of indentation to write before each case.  */

static void
write_insn_cases (struct insn_ent *ie, int indent)
{
  for (; ie != 0; ie = ie->next)
    if (ie->def->insn_code != -1)
      {
	write_indent (indent);
	if (GET_CODE (ie->def->def) == DEFINE_PEEPHOLE)
	  printf ("case %d:  /* define_peephole, line %d */\n",
		  ie->def->insn_code, ie->def->lineno);
	else
	  printf ("case %d:  /* %s */\n",
		  ie->def->insn_code, XSTR (ie->def->def, 0));
      }
}

Tom Wood committed
3823 3824 3825
/* Write out the computation for one attribute value.  */

static void
3826 3827 3828
write_attr_case (struct attr_desc *attr, struct attr_value *av,
		 int write_case_lines, const char *prefix, const char *suffix,
		 int indent, rtx known_true)
Tom Wood committed
3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845
{
  if (av->num_insns == 0)
    return;

  if (av->has_asm_insn)
    {
      write_indent (indent);
      printf ("case -1:\n");
      write_indent (indent + 2);
      printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
      write_indent (indent + 2);
      printf ("    && asm_noperands (PATTERN (insn)) < 0)\n");
      write_indent (indent + 2);
      printf ("  fatal_insn_not_found (insn);\n");
    }

  if (write_case_lines)
3846
    write_insn_cases (av->first_insn, indent);
Tom Wood committed
3847 3848 3849 3850 3851 3852
  else
    {
      write_indent (indent);
      printf ("default:\n");
    }

3853
  /* See what we have to do to output this value.  */
Tom Wood committed
3854 3855 3856
  must_extract = must_constrain = address_used = 0;
  walk_attr_value (av->value);

3857
  if (must_constrain)
Tom Wood committed
3858 3859
    {
      write_indent (indent + 2);
3860
      printf ("extract_constrain_insn_cached (insn);\n");
Tom Wood committed
3861
    }
3862
  else if (must_extract)
Tom Wood committed
3863 3864
    {
      write_indent (indent + 2);
3865
      printf ("extract_insn_cached (insn);\n");
Tom Wood committed
3866 3867
    }

3868 3869 3870 3871 3872 3873 3874
  if (av->num_insns == 1)
    write_attr_set (attr, indent + 2, av->value, prefix, suffix,
		    known_true, av->first_insn->def->insn_code,
		    av->first_insn->def->insn_index);
  else
    write_attr_set (attr, indent + 2, av->value, prefix, suffix,
		    known_true, -2, 0);
Tom Wood committed
3875 3876 3877 3878 3879 3880 3881 3882

  if (strncmp (prefix, "return", 6))
    {
      write_indent (indent + 2);
      printf ("break;\n");
    }
  printf ("\n");
}
3883

3884
/* Utilities to write in various forms.  */
3885 3886

static void
3887
write_attr_valueq (struct attr_desc *attr, const char *s)
Tom Wood committed
3888 3889
{
  if (attr->is_numeric)
3890
    {
3891 3892 3893 3894
      int num = atoi (s);

      printf ("%d", num);

3895
      if (num > 9 || num < 0)
3896
	printf (" /* 0x%x */", num);
3897
    }
Tom Wood committed
3898 3899 3900 3901 3902 3903 3904 3905 3906
  else
    {
      write_upcase (attr->name);
      printf ("_");
      write_upcase (s);
    }
}

static void
3907
write_attr_value (struct attr_desc *attr, rtx value)
Tom Wood committed
3908
{
3909 3910 3911 3912 3913 3914 3915 3916
  int op;

  switch (GET_CODE (value))
    {
    case CONST_STRING:
      write_attr_valueq (attr, XSTR (value, 0));
      break;

3917 3918 3919 3920
    case CONST_INT:
      printf (HOST_WIDE_INT_PRINT_DEC, INTVAL (value));
      break;

3921
    case SYMBOL_REF:
3922
      print_c_condition (XSTR (value, 0));
3923 3924 3925 3926
      break;

    case ATTR:
      {
3927
	struct attr_desc *attr2 = find_attr (&XSTR (value, 0), 0);
3928
	printf ("get_attr_%s (%s)", attr2->name,
3929 3930 3931
		(attr2->is_const ? "" : "insn"));
      }
      break;
Tom Wood committed
3932

3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957
    case PLUS:
      op = '+';
      goto do_operator;
    case MINUS:
      op = '-';
      goto do_operator;
    case MULT:
      op = '*';
      goto do_operator;
    case DIV:
      op = '/';
      goto do_operator;
    case MOD:
      op = '%';
      goto do_operator;

    do_operator:
      write_attr_value (attr, XEXP (value, 0));
      putchar (' ');
      putchar (op);
      putchar (' ');
      write_attr_value (attr, XEXP (value, 1));
      break;

    default:
3958
      gcc_unreachable ();
3959
    }
Tom Wood committed
3960 3961 3962
}

static void
3963
write_upcase (const char *str)
Tom Wood committed
3964 3965
{
  while (*str)
3966 3967 3968 3969 3970
    {
      /* The argument of TOUPPER should not have side effects.  */
      putchar (TOUPPER(*str));
      str++;
    }
Tom Wood committed
3971 3972 3973
}

static void
3974
write_indent (int indent)
Tom Wood committed
3975 3976 3977 3978 3979 3980 3981
{
  for (; indent > 8; indent -= 8)
    printf ("\t");

  for (; indent; indent--)
    printf (" ");
}
3982

Tom Wood committed
3983
/* Write a subroutine that is given an insn that requires a delay slot, a
3984
   delay slot ordinal, and a candidate insn.  It returns nonzero if the
Tom Wood committed
3985 3986 3987 3988 3989 3990 3991
   candidate can be placed in the specified delay slot of the insn.

   We can write as many as three subroutines.  `eligible_for_delay'
   handles normal delay slots, `eligible_for_annul_true' indicates that
   the specified insn can be annulled if the branch is true, and likewise
   for `eligible_for_annul_false'.

3992
   KIND is a string distinguishing these three cases ("delay", "annul_true",
Tom Wood committed
3993 3994 3995
   or "annul_false").  */

static void
3996
write_eligible_delay (const char *kind)
Tom Wood committed
3997 3998 3999 4000
{
  struct delay_desc *delay;
  int max_slots;
  char str[50];
4001
  const char *pstr;
Tom Wood committed
4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016
  struct attr_desc *attr;
  struct attr_value *av, *common_av;
  int i;

  /* Compute the maximum number of delay slots required.  We use the delay
     ordinal times this number plus one, plus the slot number as an index into
     the appropriate predicate to test.  */

  for (delay = delays, max_slots = 0; delay; delay = delay->next)
    if (XVECLEN (delay->def, 1) / 3 > max_slots)
      max_slots = XVECLEN (delay->def, 1) / 3;

  /* Write function prelude.  */

  printf ("int\n");
4017
  printf ("eligible_for_%s (rtx delay_insn ATTRIBUTE_UNUSED, int slot, rtx candidate_insn, int flags ATTRIBUTE_UNUSED)\n",
4018
	  kind);
Tom Wood committed
4019 4020 4021
  printf ("{\n");
  printf ("  rtx insn;\n");
  printf ("\n");
4022
  printf ("  gcc_assert (slot < %d);\n", max_slots);
Tom Wood committed
4023
  printf ("\n");
4024 4025 4026 4027 4028
  /* Allow dbr_schedule to pass labels, etc.  This can happen if try_split
     converts a compound instruction into a loop.  */
  printf ("  if (!INSN_P (candidate_insn))\n");
  printf ("    return 0;\n");
  printf ("\n");
Tom Wood committed
4029 4030 4031 4032 4033

  /* If more than one delay type, find out which type the delay insn is.  */

  if (num_delays > 1)
    {
4034
      attr = find_attr (&delay_type_str, 0);
4035
      gcc_assert (attr);
Tom Wood committed
4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050
      common_av = find_most_used (attr);

      printf ("  insn = delay_insn;\n");
      printf ("  switch (recog_memoized (insn))\n");
      printf ("    {\n");

      sprintf (str, " * %d;\n      break;", max_slots);
      for (av = attr->first_value; av; av = av->next)
	if (av != common_av)
	  write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);

      write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
      printf ("    }\n\n");

      /* Ensure matched.  Otherwise, shouldn't have been called.  */
4051
      printf ("  gcc_assert (slot >= %d);\n\n", max_slots);
Tom Wood committed
4052 4053 4054 4055 4056 4057 4058 4059 4060
    }

  /* If just one type of delay slot, write simple switch.  */
  if (num_delays == 1 && max_slots == 1)
    {
      printf ("  insn = candidate_insn;\n");
      printf ("  switch (recog_memoized (insn))\n");
      printf ("    {\n");

4061
      attr = find_attr (&delay_1_0_str, 0);
4062
      gcc_assert (attr);
Tom Wood committed
4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089
      common_av = find_most_used (attr);

      for (av = attr->first_value; av; av = av->next)
	if (av != common_av)
	  write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);

      write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
      printf ("    }\n");
    }

  else
    {
      /* Write a nested CASE.  The first indicates which condition we need to
	 test, and the inner CASE tests the condition.  */
      printf ("  insn = candidate_insn;\n");
      printf ("  switch (slot)\n");
      printf ("    {\n");

      for (delay = delays; delay; delay = delay->next)
	for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
	  {
	    printf ("    case %d:\n",
		    (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
	    printf ("      switch (recog_memoized (insn))\n");
	    printf ("\t{\n");

	    sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
4090 4091
	    pstr = str;
	    attr = find_attr (&pstr, 0);
4092
	    gcc_assert (attr);
Tom Wood committed
4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103
	    common_av = find_most_used (attr);

	    for (av = attr->first_value; av; av = av->next)
	      if (av != common_av)
		write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);

	    write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
	    printf ("      }\n");
	  }

      printf ("    default:\n");
4104
      printf ("      gcc_unreachable ();\n");
Tom Wood committed
4105 4106 4107 4108 4109
      printf ("    }\n");
    }

  printf ("}\n\n");
}
4110

Tom Wood committed
4111 4112 4113 4114 4115 4116 4117
/* This page contains miscellaneous utility routines.  */

/* Given a pointer to a (char *), return a malloc'ed string containing the
   next comma-separated element.  Advance the pointer to after the string
   scanned, or the end-of-string.  Return NULL if at end of string.  */

static char *
4118
next_comma_elt (const char **pstr)
Tom Wood committed
4119
{
4120
  const char *start;
Tom Wood committed
4121

4122
  start = scan_comma_elt (pstr);
Tom Wood committed
4123

4124 4125
  if (start == NULL)
    return NULL;
Tom Wood committed
4126

4127
  return attr_string (start, *pstr - start);
Tom Wood committed
4128 4129 4130
}

/* Return a `struct attr_desc' pointer for a given named attribute.  If CREATE
4131 4132
   is nonzero, build a new attribute, if one does not exist.  *NAME_P is
   replaced by a pointer to a canonical copy of the string.  */
Tom Wood committed
4133 4134

static struct attr_desc *
4135
find_attr (const char **name_p, int create)
Tom Wood committed
4136 4137
{
  struct attr_desc *attr;
4138
  int index;
4139
  const char *name = *name_p;
Tom Wood committed
4140 4141 4142 4143 4144 4145

  /* Before we resort to using `strcmp', see if the string address matches
     anywhere.  In most cases, it should have been canonicalized to do so.  */
  if (name == alternative_name)
    return NULL;

4146 4147
  index = name[0] & (MAX_ATTRS_INDEX - 1);
  for (attr = attrs[index]; attr; attr = attr->next)
Tom Wood committed
4148 4149 4150 4151
    if (name == attr->name)
      return attr;

  /* Otherwise, do it the slow way.  */
4152
  for (attr = attrs[index]; attr; attr = attr->next)
4153
    if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
4154 4155 4156 4157
      {
	*name_p = attr->name;
	return attr;
      }
Tom Wood committed
4158 4159 4160 4161

  if (! create)
    return NULL;

4162
  attr = oballoc (sizeof (struct attr_desc));
4163
  attr->name = DEF_ATTR_STRING (name);
Tom Wood committed
4164
  attr->first_value = attr->default_val = NULL;
4165
  attr->is_numeric = attr->is_const = attr->is_special = 0;
4166 4167
  attr->next = attrs[index];
  attrs[index] = attr;
Tom Wood committed
4168

4169 4170
  *name_p = attr->name;

Tom Wood committed
4171 4172 4173 4174 4175
  return attr;
}

/* Create internal attribute with the given default value.  */

4176
static void
4177
make_internal_attr (const char *name, rtx value, int special)
Tom Wood committed
4178 4179 4180
{
  struct attr_desc *attr;

4181
  attr = find_attr (&name, 1);
4182
  gcc_assert (!attr->default_val);
Tom Wood committed
4183 4184

  attr->is_numeric = 1;
Tom Wood committed
4185
  attr->is_const = 0;
4186
  attr->is_special = (special & ATTR_SPECIAL) != 0;
Tom Wood committed
4187 4188 4189 4190 4191 4192
  attr->default_val = get_attr_value (value, attr, -2);
}

/* Find the most used value of an attribute.  */

static struct attr_value *
4193
find_most_used (struct attr_desc *attr)
Tom Wood committed
4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210
{
  struct attr_value *av;
  struct attr_value *most_used;
  int nuses;

  most_used = NULL;
  nuses = -1;

  for (av = attr->first_value; av; av = av->next)
    if (av->num_insns > nuses)
      nuses = av->num_insns, most_used = av;

  return most_used;
}

/* Return (attr_value "n") */

4211
static rtx
4212
make_numeric_value (int n)
Tom Wood committed
4213 4214 4215
{
  static rtx int_values[20];
  rtx exp;
Tom Wood committed
4216
  char *p;
Tom Wood committed
4217

4218
  gcc_assert (n >= 0);
Tom Wood committed
4219 4220 4221 4222

  if (n < 20 && int_values[n])
    return int_values[n];

4223
  p = attr_printf (MAX_DIGITS, "%d", n);
Tom Wood committed
4224
  exp = attr_rtx (CONST_STRING, p);
Tom Wood committed
4225 4226 4227 4228 4229 4230

  if (n < 20)
    int_values[n] = exp;

  return exp;
}
4231

4232
static rtx
4233
copy_rtx_unchanging (rtx orig)
4234
{
4235
  if (ATTR_IND_SIMPLIFIED_P (orig) || ATTR_CURR_SIMPLIFIED_P (orig))
4236 4237
    return orig;

4238
  ATTR_CURR_SIMPLIFIED_P (orig) = 1;
4239
  return orig;
4240 4241
}

4242 4243 4244
/* Determine if an insn has a constant number of delay slots, i.e., the
   number of delay slots is not a function of the length of the insn.  */

4245
static void
4246
write_const_num_delay_slots (void)
4247
{
4248
  struct attr_desc *attr = find_attr (&num_delay_slots_str, 0);
4249 4250 4251 4252
  struct attr_value *av;

  if (attr)
    {
4253
      printf ("int\nconst_num_delay_slots (rtx insn)\n");
4254 4255 4256 4257 4258
      printf ("{\n");
      printf ("  switch (recog_memoized (insn))\n");
      printf ("    {\n");

      for (av = attr->first_value; av; av = av->next)
4259 4260 4261 4262
	{
	  length_used = 0;
	  walk_attr_value (av->value);
	  if (length_used)
4263
	    write_insn_cases (av->first_insn, 4);
4264 4265
	}

4266 4267
      printf ("    default:\n");
      printf ("      return 1;\n");
4268
      printf ("    }\n}\n\n");
4269 4270
    }
}
4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357

/* Synthetic attributes used by insn-automata.c and the scheduler.
   These are primarily concerned with (define_insn_reservation)
   patterns.  */

struct insn_reserv
{
  struct insn_reserv *next;

  const char *name;
  int default_latency;
  rtx condexp;

  /* Sequence number of this insn.  */
  int insn_num;

  /* Whether a (define_bypass) construct names this insn in its
     output list.  */
  bool bypassed;
};

static struct insn_reserv *all_insn_reservs = 0;
static struct insn_reserv **last_insn_reserv_p = &all_insn_reservs;
static size_t n_insn_reservs;

/* Store information from a DEFINE_INSN_RESERVATION for future
   attribute generation.  */
static void
gen_insn_reserv (rtx def)
{
  struct insn_reserv *decl = oballoc (sizeof (struct insn_reserv));

  decl->name            = DEF_ATTR_STRING (XSTR (def, 0));
  decl->default_latency = XINT (def, 1);
  decl->condexp         = check_attr_test (XEXP (def, 2), 0, 0);
  decl->insn_num        = n_insn_reservs;
  decl->bypassed	= false;
  decl->next            = 0;
  
  *last_insn_reserv_p = decl;
  last_insn_reserv_p  = &decl->next;
  n_insn_reservs++;
}

/* Store information from a DEFINE_BYPASS for future attribute
   generation.  The only thing we care about is the list of output
   insns, which will later be used to tag reservation structures with
   a 'bypassed' bit.  */

struct bypass_list
{
  struct bypass_list *next;
  const char *insn;
};

static struct bypass_list *all_bypasses;
static size_t n_bypasses;

static void
gen_bypass_1 (const char *s, size_t len)
{
  struct bypass_list *b;

  if (len == 0)
    return;

  s = attr_string (s, len);
  for (b = all_bypasses; b; b = b->next)
    if (s == b->insn)
      return;  /* already got that one */

  b = oballoc (sizeof (struct bypass_list));
  b->insn = s;
  b->next = all_bypasses;
  all_bypasses = b;
  n_bypasses++;
}

static void
gen_bypass (rtx def)
{
  const char *p, *base;

  for (p = base = XSTR (def, 1); *p; p++)
    if (*p == ',')
      {
	gen_bypass_1 (base, p - base);
4358 4359 4360 4361
	do
	  p++;
	while (ISSPACE (*p));
	base = p;
4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435
      }
  gen_bypass_1 (base, p - base);
}

/* Find and mark all of the bypassed insns.  */
static void
process_bypasses (void)
{
  struct bypass_list *b;
  struct insn_reserv *r;

  /* The reservation list is likely to be much longer than the bypass
     list.  */
  for (r = all_insn_reservs; r; r = r->next)
    for (b = all_bypasses; b; b = b->next)
      if (r->name == b->insn)
	r->bypassed = true;
}

/* Create all of the attributes that describe automaton properties.  */
static void
make_automaton_attrs (void)
{
  int i;
  struct insn_reserv *decl;
  rtx code_exp, lats_exp, byps_exp;

  if (n_insn_reservs == 0)
    return;

  code_exp = rtx_alloc (COND);
  lats_exp = rtx_alloc (COND);
  
  XVEC (code_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
  XVEC (lats_exp, 0) = rtvec_alloc (n_insn_reservs * 2);

  XEXP (code_exp, 1) = make_numeric_value (n_insn_reservs + 1);
  XEXP (lats_exp, 1) = make_numeric_value (0);

  for (decl = all_insn_reservs, i = 0;
       decl;
       decl = decl->next, i += 2)
    {
      XVECEXP (code_exp, 0, i)   = decl->condexp;
      XVECEXP (lats_exp, 0, i)   = decl->condexp;
      
      XVECEXP (code_exp, 0, i+1) = make_numeric_value (decl->insn_num);
      XVECEXP (lats_exp, 0, i+1) = make_numeric_value (decl->default_latency);
    }

  if (n_bypasses == 0)
    byps_exp = make_numeric_value (0);
  else
    {
      process_bypasses ();

      byps_exp = rtx_alloc (COND);
      XVEC (byps_exp, 0) = rtvec_alloc (n_bypasses * 2);
      XEXP (byps_exp, 1) = make_numeric_value (0);
      for (decl = all_insn_reservs, i = 0;
	   decl;
	   decl = decl->next)
	if (decl->bypassed)
	  {
	    XVECEXP (byps_exp, 0, i)   = decl->condexp;
	    XVECEXP (byps_exp, 0, i+1) = make_numeric_value (1);
	    i += 2;
	  }
    }

  make_internal_attr ("*internal_dfa_insn_code", code_exp, ATTR_NONE);
  make_internal_attr ("*insn_default_latency",   lats_exp, ATTR_NONE);
  make_internal_attr ("*bypass_p",               byps_exp, ATTR_NONE);
}
4436

Tom Wood committed
4437
int
4438
main (int argc, char **argv)
Tom Wood committed
4439 4440 4441 4442 4443
{
  rtx desc;
  struct attr_desc *attr;
  struct insn_def *id;
  rtx tem;
4444
  int i;
Tom Wood committed
4445

4446 4447
  progname = "genattrtab";

4448
  if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
4449 4450
    return (FATAL_EXIT_CODE);

4451
  obstack_init (hash_obstack);
4452
  obstack_init (temp_obstack);
Tom Wood committed
4453 4454

  /* Set up true and false rtx's */
4455
  true_rtx = rtx_alloc (CONST_INT);
Richard Stallman committed
4456
  XWINT (true_rtx, 0) = 1;
4457
  false_rtx = rtx_alloc (CONST_INT);
Richard Stallman committed
4458
  XWINT (false_rtx, 0) = 0;
4459 4460
  ATTR_IND_SIMPLIFIED_P (true_rtx) = ATTR_IND_SIMPLIFIED_P (false_rtx) = 1;
  ATTR_PERMANENT_P (true_rtx) = ATTR_PERMANENT_P (false_rtx) = 1;
4461

4462 4463 4464 4465 4466
  alternative_name = DEF_ATTR_STRING ("alternative");
  length_str = DEF_ATTR_STRING ("length");
  delay_type_str = DEF_ATTR_STRING ("*delay_type");
  delay_1_0_str = DEF_ATTR_STRING ("*delay_1_0");
  num_delay_slots_str = DEF_ATTR_STRING ("*num_delay_slots");
Tom Wood committed
4467 4468 4469 4470 4471 4472 4473 4474

  printf ("/* Generated automatically by the program `genattrtab'\n\
from the machine description file `md'.  */\n\n");

  /* Read the machine description.  */

  while (1)
    {
4475
      int lineno;
Tom Wood committed
4476

4477
      desc = read_md_rtx (&lineno, &insn_code_number);
Clinton Popetz committed
4478 4479
      if (desc == NULL)
	break;
4480

Clinton Popetz committed
4481
      switch (GET_CODE (desc))
Tom Wood committed
4482
	{
4483 4484 4485 4486 4487
	case DEFINE_INSN:
	case DEFINE_PEEPHOLE:
	case DEFINE_ASM_ATTRIBUTES:
	  gen_insn (desc, lineno);
	  break;
Tom Wood committed
4488

4489 4490 4491
	case DEFINE_ATTR:
	  gen_attr (desc, lineno);
	  break;
Tom Wood committed
4492

4493 4494 4495 4496
	case DEFINE_DELAY:
	  gen_delay (desc, lineno);
	  break;

4497 4498
	case DEFINE_INSN_RESERVATION:
	  gen_insn_reserv (desc);
4499
	  break;
4500

4501 4502 4503
	case DEFINE_BYPASS:
	  gen_bypass (desc);
	  break;
4504

4505 4506
	default:
	  break;
Tom Wood committed
4507
	}
Clinton Popetz committed
4508
      if (GET_CODE (desc) != DEFINE_ASM_ATTRIBUTES)
4509
	insn_index_number++;
Tom Wood committed
4510 4511
    }

4512 4513 4514
  if (have_error)
    return FATAL_EXIT_CODE;

Clinton Popetz committed
4515 4516
  insn_code_number++;

Tom Wood committed
4517 4518 4519 4520 4521
  /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one.  */
  if (! got_define_asm_attributes)
    {
      tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
      XVEC (tem, 0) = rtvec_alloc (0);
4522
      gen_insn (tem, 0);
Tom Wood committed
4523 4524 4525 4526 4527 4528 4529
    }

  /* Expand DEFINE_DELAY information into new attribute.  */
  if (num_delays)
    expand_delays ();

  printf ("#include \"config.h\"\n");
4530
  printf ("#include \"system.h\"\n");
4531 4532
  printf ("#include \"coretypes.h\"\n");
  printf ("#include \"tm.h\"\n");
Tom Wood committed
4533
  printf ("#include \"rtl.h\"\n");
4534
  printf ("#include \"tm_p.h\"\n");
Tom Wood committed
4535 4536 4537 4538 4539 4540
  printf ("#include \"insn-config.h\"\n");
  printf ("#include \"recog.h\"\n");
  printf ("#include \"regs.h\"\n");
  printf ("#include \"real.h\"\n");
  printf ("#include \"output.h\"\n");
  printf ("#include \"insn-attr.h\"\n");
4541
  printf ("#include \"toplev.h\"\n");
4542
  printf ("#include \"flags.h\"\n");
4543
  printf ("#include \"function.h\"\n");
4544
  printf ("\n");
4545
  printf ("#define operands recog_data.operand\n\n");
Tom Wood committed
4546 4547

  /* Make `insn_alternatives'.  */
4548
  insn_alternatives = oballoc (insn_code_number * sizeof (int));
Tom Wood committed
4549 4550 4551 4552
  for (id = defs; id; id = id->next)
    if (id->insn_code >= 0)
      insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;

4553
  /* Make `insn_n_alternatives'.  */
4554
  insn_n_alternatives = oballoc (insn_code_number * sizeof (int));
4555 4556 4557 4558
  for (id = defs; id; id = id->next)
    if (id->insn_code >= 0)
      insn_n_alternatives[id->insn_code] = id->num_alternatives;

4559 4560 4561
  /* Construct extra attributes for automata.  */
  make_automaton_attrs ();

Tom Wood committed
4562 4563 4564 4565
  /* Prepare to write out attribute subroutines by checking everything stored
     away and building the attribute cases.  */

  check_defs ();
4566

4567 4568
  for (i = 0; i < MAX_ATTRS_INDEX; i++)
    for (attr = attrs[i]; attr; attr = attr->next)
4569 4570 4571 4572 4573 4574 4575 4576 4577
      attr->default_val->value
	= check_attr_value (attr->default_val->value, attr);

  if (have_error)
    return FATAL_EXIT_CODE;

  for (i = 0; i < MAX_ATTRS_INDEX; i++)
    for (attr = attrs[i]; attr; attr = attr->next)
      fill_attr (attr);
Tom Wood committed
4578 4579 4580 4581

  /* Construct extra attributes for `length'.  */
  make_length_attrs ();

Mike Stump committed
4582
  /* Perform any possible optimizations to speed up compilation.  */
Tom Wood committed
4583 4584 4585
  optimize_attrs ();

  /* Now write out all the `gen_attr_...' routines.  Do these before the
4586
     special routines so that they get defined before they are used.  */
Tom Wood committed
4587

4588 4589 4590
  for (i = 0; i < MAX_ATTRS_INDEX; i++)
    for (attr = attrs[i]; attr; attr = attr->next)
      {
4591
	if (! attr->is_special && ! attr->is_const)
4592
	  write_attr_get (attr);
4593
      }
Tom Wood committed
4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606

  /* Write out delay eligibility information, if DEFINE_DELAY present.
     (The function to compute the number of delay slots will be written
     below.)  */
  if (num_delays)
    {
      write_eligible_delay ("delay");
      if (have_annul_true)
	write_eligible_delay ("annul_true");
      if (have_annul_false)
	write_eligible_delay ("annul_false");
    }

4607
  /* Write out constant delay slot info.  */
4608 4609
  write_const_num_delay_slots ();

4610 4611
  write_length_unit_log ();

Tom Wood committed
4612
  fflush (stdout);
4613
  return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
Tom Wood committed
4614
}