* except.c (expand_start_all_catch): One more do_pending_stack_adjust.
[official-gcc.git] / gcc / md.texi
blob629929bf9782b98e1c370cc90e9b06e02c176665
1 @c Copyright (C) 1988,89,92,93,94,96 Free Software Foundation, Inc.
2 @c This is part of the GCC manual.
3 @c For copying conditions, see the file gcc.texi.
5 @ifset INTERNALS
6 @node Machine Desc
7 @chapter Machine Descriptions
8 @cindex machine descriptions
10 A machine description has two parts: a file of instruction patterns
11 (@file{.md} file) and a C header file of macro definitions.
13 The @file{.md} file for a target machine contains a pattern for each
14 instruction that the target machine supports (or at least each instruction
15 that is worth telling the compiler about).  It may also contain comments.
16 A semicolon causes the rest of the line to be a comment, unless the semicolon
17 is inside a quoted string.
19 See the next chapter for information on the C header file.
21 @menu
22 * Patterns::            How to write instruction patterns.
23 * Example::             An explained example of a @code{define_insn} pattern.
24 * RTL Template::        The RTL template defines what insns match a pattern.
25 * Output Template::     The output template says how to make assembler code
26                           from such an insn.
27 * Output Statement::    For more generality, write C code to output
28                           the assembler code.
29 * Constraints::         When not all operands are general operands.
30 * Standard Names::      Names mark patterns to use for code generation.
31 * Pattern Ordering::    When the order of patterns makes a difference.
32 * Dependent Patterns::  Having one pattern may make you need another.
33 * Jump Patterns::       Special considerations for patterns for jump insns.
34 * Insn Canonicalizations::Canonicalization of Instructions
35 * Peephole Definitions::Defining machine-specific peephole optimizations.
36 * Expander Definitions::Generating a sequence of several RTL insns
37                          for a standard operation.
38 * Insn Splitting::    Splitting Instructions into Multiple Instructions
39 * Insn Attributes::     Specifying the value of attributes for generated insns.
40 @end menu
42 @node Patterns
43 @section Everything about Instruction Patterns
44 @cindex patterns
45 @cindex instruction patterns
47 @findex define_insn
48 Each instruction pattern contains an incomplete RTL expression, with pieces
49 to be filled in later, operand constraints that restrict how the pieces can
50 be filled in, and an output pattern or C code to generate the assembler
51 output, all wrapped up in a @code{define_insn} expression.
53 A @code{define_insn} is an RTL expression containing four or five operands:
55 @enumerate
56 @item
57 An optional name.  The presence of a name indicate that this instruction
58 pattern can perform a certain standard job for the RTL-generation
59 pass of the compiler.  This pass knows certain names and will use
60 the instruction patterns with those names, if the names are defined
61 in the machine description.
63 The absence of a name is indicated by writing an empty string
64 where the name should go.  Nameless instruction patterns are never
65 used for generating RTL code, but they may permit several simpler insns
66 to be combined later on.
68 Names that are not thus known and used in RTL-generation have no
69 effect; they are equivalent to no name at all.
71 @item
72 The @dfn{RTL template} (@pxref{RTL Template}) is a vector of incomplete
73 RTL expressions which show what the instruction should look like.  It is
74 incomplete because it may contain @code{match_operand},
75 @code{match_operator}, and @code{match_dup} expressions that stand for
76 operands of the instruction.
78 If the vector has only one element, that element is the template for the
79 instruction pattern.  If the vector has multiple elements, then the
80 instruction pattern is a @code{parallel} expression containing the
81 elements described.
83 @item
84 @cindex pattern conditions
85 @cindex conditions, in patterns
86 A condition.  This is a string which contains a C expression that is
87 the final test to decide whether an insn body matches this pattern.
89 @cindex named patterns and conditions
90 For a named pattern, the condition (if present) may not depend on
91 the data in the insn being matched, but only the target-machine-type
92 flags.  The compiler needs to test these conditions during
93 initialization in order to learn exactly which named instructions are
94 available in a particular run.
96 @findex operands
97 For nameless patterns, the condition is applied only when matching an
98 individual insn, and only after the insn has matched the pattern's
99 recognition template.  The insn's operands may be found in the vector
100 @code{operands}.
102 @item
103 The @dfn{output template}: a string that says how to output matching
104 insns as assembler code.  @samp{%} in this string specifies where
105 to substitute the value of an operand.  @xref{Output Template}.
107 When simple substitution isn't general enough, you can specify a piece
108 of C code to compute the output.  @xref{Output Statement}.
110 @item
111 Optionally, a vector containing the values of attributes for insns matching
112 this pattern.  @xref{Insn Attributes}.
113 @end enumerate
115 @node Example
116 @section Example of @code{define_insn}
117 @cindex @code{define_insn} example
119 Here is an actual example of an instruction pattern, for the 68000/68020.
121 @example
122 (define_insn "tstsi"
123   [(set (cc0)
124         (match_operand:SI 0 "general_operand" "rm"))]
125   ""
126   "*
127 @{ if (TARGET_68020 || ! ADDRESS_REG_P (operands[0]))
128     return \"tstl %0\";
129   return \"cmpl #0,%0\"; @}")
130 @end example
132 This is an instruction that sets the condition codes based on the value of
133 a general operand.  It has no condition, so any insn whose RTL description
134 has the form shown may be handled according to this pattern.  The name
135 @samp{tstsi} means ``test a @code{SImode} value'' and tells the RTL generation
136 pass that, when it is necessary to test such a value, an insn to do so
137 can be constructed using this pattern.
139 The output control string is a piece of C code which chooses which
140 output template to return based on the kind of operand and the specific
141 type of CPU for which code is being generated.
143 @samp{"rm"} is an operand constraint.  Its meaning is explained below.
145 @node RTL Template
146 @section RTL Template
147 @cindex RTL insn template
148 @cindex generating insns
149 @cindex insns, generating
150 @cindex recognizing insns
151 @cindex insns, recognizing
153 The RTL template is used to define which insns match the particular pattern
154 and how to find their operands.  For named patterns, the RTL template also
155 says how to construct an insn from specified operands.
157 Construction involves substituting specified operands into a copy of the
158 template.  Matching involves determining the values that serve as the
159 operands in the insn being matched.  Both of these activities are
160 controlled by special expression types that direct matching and
161 substitution of the operands.
163 @table @code
164 @findex match_operand
165 @item (match_operand:@var{m} @var{n} @var{predicate} @var{constraint})
166 This expression is a placeholder for operand number @var{n} of
167 the insn.  When constructing an insn, operand number @var{n}
168 will be substituted at this point.  When matching an insn, whatever
169 appears at this position in the insn will be taken as operand
170 number @var{n}; but it must satisfy @var{predicate} or this instruction
171 pattern will not match at all.
173 Operand numbers must be chosen consecutively counting from zero in
174 each instruction pattern.  There may be only one @code{match_operand}
175 expression in the pattern for each operand number.  Usually operands
176 are numbered in the order of appearance in @code{match_operand}
177 expressions.
179 @var{predicate} is a string that is the name of a C function that accepts two
180 arguments, an expression and a machine mode.  During matching, the
181 function will be called with the putative operand as the expression and
182 @var{m} as the mode argument (if @var{m} is not specified,
183 @code{VOIDmode} will be used, which normally causes @var{predicate} to accept
184 any mode).  If it returns zero, this instruction pattern fails to match.
185 @var{predicate} may be an empty string; then it means no test is to be done
186 on the operand, so anything which occurs in this position is valid.
188 Most of the time, @var{predicate} will reject modes other than @var{m}---but
189 not always.  For example, the predicate @code{address_operand} uses
190 @var{m} as the mode of memory ref that the address should be valid for.
191 Many predicates accept @code{const_int} nodes even though their mode is
192 @code{VOIDmode}.
194 @var{constraint} controls reloading and the choice of the best register
195 class to use for a value, as explained later (@pxref{Constraints}).
197 People are often unclear on the difference between the constraint and the
198 predicate.  The predicate helps decide whether a given insn matches the
199 pattern.  The constraint plays no role in this decision; instead, it
200 controls various decisions in the case of an insn which does match.
202 @findex general_operand
203 On CISC machines, the most common @var{predicate} is
204 @code{"general_operand"}.  This function checks that the putative
205 operand is either a constant, a register or a memory reference, and that
206 it is valid for mode @var{m}.
208 @findex register_operand
209 For an operand that must be a register, @var{predicate} should be
210 @code{"register_operand"}.  Using @code{"general_operand"} would be
211 valid, since the reload pass would copy any non-register operands
212 through registers, but this would make GNU CC do extra work, it would
213 prevent invariant operands (such as constant) from being removed from
214 loops, and it would prevent the register allocator from doing the best
215 possible job.  On RISC machines, it is usually most efficient to allow
216 @var{predicate} to accept only objects that the constraints allow.
218 @findex immediate_operand
219 For an operand that must be a constant, you must be sure to either use
220 @code{"immediate_operand"} for @var{predicate}, or make the instruction
221 pattern's extra condition require a constant, or both.  You cannot
222 expect the constraints to do this work!  If the constraints allow only
223 constants, but the predicate allows something else, the compiler will
224 crash when that case arises.
226 @findex match_scratch
227 @item (match_scratch:@var{m} @var{n} @var{constraint})
228 This expression is also a placeholder for operand number @var{n}
229 and indicates that operand must be a @code{scratch} or @code{reg}
230 expression.
232 When matching patterns, this is equivalent to
234 @smallexample
235 (match_operand:@var{m} @var{n} "scratch_operand" @var{pred})
236 @end smallexample
238 but, when generating RTL, it produces a (@code{scratch}:@var{m})
239 expression.
241 If the last few expressions in a @code{parallel} are @code{clobber}
242 expressions whose operands are either a hard register or
243 @code{match_scratch}, the combiner can add or delete them when
244 necessary.  @xref{Side Effects}.
246 @findex match_dup
247 @item (match_dup @var{n})
248 This expression is also a placeholder for operand number @var{n}.
249 It is used when the operand needs to appear more than once in the
250 insn.
252 In construction, @code{match_dup} acts just like @code{match_operand}:
253 the operand is substituted into the insn being constructed.  But in
254 matching, @code{match_dup} behaves differently.  It assumes that operand
255 number @var{n} has already been determined by a @code{match_operand}
256 appearing earlier in the recognition template, and it matches only an
257 identical-looking expression.
259 @findex match_operator
260 @item (match_operator:@var{m} @var{n} @var{predicate} [@var{operands}@dots{}])
261 This pattern is a kind of placeholder for a variable RTL expression
262 code.
264 When constructing an insn, it stands for an RTL expression whose
265 expression code is taken from that of operand @var{n}, and whose
266 operands are constructed from the patterns @var{operands}.
268 When matching an expression, it matches an expression if the function
269 @var{predicate} returns nonzero on that expression @emph{and} the
270 patterns @var{operands} match the operands of the expression.
272 Suppose that the function @code{commutative_operator} is defined as
273 follows, to match any expression whose operator is one of the
274 commutative arithmetic operators of RTL and whose mode is @var{mode}:
276 @smallexample
278 commutative_operator (x, mode)
279      rtx x;
280      enum machine_mode mode;
282   enum rtx_code code = GET_CODE (x);
283   if (GET_MODE (x) != mode)
284     return 0;
285   return (GET_RTX_CLASS (code) == 'c'
286           || code == EQ || code == NE);
288 @end smallexample
290 Then the following pattern will match any RTL expression consisting
291 of a commutative operator applied to two general operands:
293 @smallexample
294 (match_operator:SI 3 "commutative_operator"
295   [(match_operand:SI 1 "general_operand" "g")
296    (match_operand:SI 2 "general_operand" "g")])
297 @end smallexample
299 Here the vector @code{[@var{operands}@dots{}]} contains two patterns
300 because the expressions to be matched all contain two operands.
302 When this pattern does match, the two operands of the commutative
303 operator are recorded as operands 1 and 2 of the insn.  (This is done
304 by the two instances of @code{match_operand}.)  Operand 3 of the insn
305 will be the entire commutative expression: use @code{GET_CODE
306 (operands[3])} to see which commutative operator was used.
308 The machine mode @var{m} of @code{match_operator} works like that of
309 @code{match_operand}: it is passed as the second argument to the
310 predicate function, and that function is solely responsible for
311 deciding whether the expression to be matched ``has'' that mode.
313 When constructing an insn, argument 3 of the gen-function will specify
314 the operation (i.e. the expression code) for the expression to be
315 made.  It should be an RTL expression, whose expression code is copied
316 into a new expression whose operands are arguments 1 and 2 of the
317 gen-function.  The subexpressions of argument 3 are not used;
318 only its expression code matters.
320 When @code{match_operator} is used in a pattern for matching an insn,
321 it usually best if the operand number of the @code{match_operator}
322 is higher than that of the actual operands of the insn.  This improves
323 register allocation because the register allocator often looks at
324 operands 1 and 2 of insns to see if it can do register tying.
326 There is no way to specify constraints in @code{match_operator}.  The
327 operand of the insn which corresponds to the @code{match_operator}
328 never has any constraints because it is never reloaded as a whole.
329 However, if parts of its @var{operands} are matched by
330 @code{match_operand} patterns, those parts may have constraints of
331 their own.
333 @findex match_op_dup
334 @item (match_op_dup:@var{m} @var{n}[@var{operands}@dots{}])
335 Like @code{match_dup}, except that it applies to operators instead of
336 operands.  When constructing an insn, operand number @var{n} will be
337 substituted at this point.  But in matching, @code{match_op_dup} behaves
338 differently.  It assumes that operand number @var{n} has already been
339 determined by a @code{match_operator} appearing earlier in the
340 recognition template, and it matches only an identical-looking
341 expression.
343 @findex match_parallel
344 @item (match_parallel @var{n} @var{predicate} [@var{subpat}@dots{}])
345 This pattern is a placeholder for an insn that consists of a
346 @code{parallel} expression with a variable number of elements.  This
347 expression should only appear at the top level of an insn pattern.
349 When constructing an insn, operand number @var{n} will be substituted at
350 this point.  When matching an insn, it matches if the body of the insn
351 is a @code{parallel} expression with at least as many elements as the
352 vector of @var{subpat} expressions in the @code{match_parallel}, if each
353 @var{subpat} matches the corresponding element of the @code{parallel},
354 @emph{and} the function @var{predicate} returns nonzero on the
355 @code{parallel} that is the body of the insn.  It is the responsibility
356 of the predicate to validate elements of the @code{parallel} beyond
357 those listed in the @code{match_parallel}.@refill
359 A typical use of @code{match_parallel} is to match load and store
360 multiple expressions, which can contain a variable number of elements
361 in a @code{parallel}.  For example,
362 @c the following is *still* going over.  need to change the code.
363 @c also need to work on grouping of this example.  --mew 1feb93
365 @smallexample
366 (define_insn ""
367   [(match_parallel 0 "load_multiple_operation"
368      [(set (match_operand:SI 1 "gpc_reg_operand" "=r")
369            (match_operand:SI 2 "memory_operand" "m"))
370       (use (reg:SI 179))
371       (clobber (reg:SI 179))])]
372   ""
373   "loadm 0,0,%1,%2")
374 @end smallexample
376 This example comes from @file{a29k.md}.  The function
377 @code{load_multiple_operations} is defined in @file{a29k.c} and checks
378 that subsequent elements in the @code{parallel} are the same as the
379 @code{set} in the pattern, except that they are referencing subsequent
380 registers and memory locations.
382 An insn that matches this pattern might look like:
384 @smallexample
385 (parallel
386  [(set (reg:SI 20) (mem:SI (reg:SI 100)))
387   (use (reg:SI 179))
388   (clobber (reg:SI 179))
389   (set (reg:SI 21)
390        (mem:SI (plus:SI (reg:SI 100)
391                         (const_int 4))))
392   (set (reg:SI 22)
393        (mem:SI (plus:SI (reg:SI 100)
394                         (const_int 8))))])
395 @end smallexample
397 @findex match_par_dup
398 @item (match_par_dup @var{n} [@var{subpat}@dots{}])
399 Like @code{match_op_dup}, but for @code{match_parallel} instead of
400 @code{match_operator}.
402 @findex address
403 @item (address (match_operand:@var{m} @var{n} "address_operand" ""))
404 This complex of expressions is a placeholder for an operand number
405 @var{n} in a ``load address'' instruction: an operand which specifies
406 a memory location in the usual way, but for which the actual operand
407 value used is the address of the location, not the contents of the
408 location.
410 @code{address} expressions never appear in RTL code, only in machine
411 descriptions.  And they are used only in machine descriptions that do
412 not use the operand constraint feature.  When operand constraints are
413 in use, the letter @samp{p} in the constraint serves this purpose.
415 @var{m} is the machine mode of the @emph{memory location being
416 addressed}, not the machine mode of the address itself.  That mode is
417 always the same on a given target machine (it is @code{Pmode}, which
418 normally is @code{SImode}), so there is no point in mentioning it;
419 thus, no machine mode is written in the @code{address} expression.  If
420 some day support is added for machines in which addresses of different
421 kinds of objects appear differently or are used differently (such as
422 the PDP-10), different formats would perhaps need different machine
423 modes and these modes might be written in the @code{address}
424 expression.
425 @end table
427 @node Output Template
428 @section Output Templates and Operand Substitution
429 @cindex output templates
430 @cindex operand substitution
432 @cindex @samp{%} in template
433 @cindex percent sign
434 The @dfn{output template} is a string which specifies how to output the
435 assembler code for an instruction pattern.  Most of the template is a
436 fixed string which is output literally.  The character @samp{%} is used
437 to specify where to substitute an operand; it can also be used to
438 identify places where different variants of the assembler require
439 different syntax.
441 In the simplest case, a @samp{%} followed by a digit @var{n} says to output
442 operand @var{n} at that point in the string.
444 @samp{%} followed by a letter and a digit says to output an operand in an
445 alternate fashion.  Four letters have standard, built-in meanings described
446 below.  The machine description macro @code{PRINT_OPERAND} can define
447 additional letters with nonstandard meanings.
449 @samp{%c@var{digit}} can be used to substitute an operand that is a
450 constant value without the syntax that normally indicates an immediate
451 operand.
453 @samp{%n@var{digit}} is like @samp{%c@var{digit}} except that the value of
454 the constant is negated before printing.
456 @samp{%a@var{digit}} can be used to substitute an operand as if it were a
457 memory reference, with the actual operand treated as the address.  This may
458 be useful when outputting a ``load address'' instruction, because often the
459 assembler syntax for such an instruction requires you to write the operand
460 as if it were a memory reference.
462 @samp{%l@var{digit}} is used to substitute a @code{label_ref} into a jump
463 instruction.
465 @samp{%=} outputs a number which is unique to each instruction in the
466 entire compilation.  This is useful for making local labels to be
467 referred to more than once in a single template that generates multiple
468 assembler instructions.
470 @samp{%} followed by a punctuation character specifies a substitution that
471 does not use an operand.  Only one case is standard: @samp{%%} outputs a
472 @samp{%} into the assembler code.  Other nonstandard cases can be
473 defined in the @code{PRINT_OPERAND} macro.  You must also define
474 which punctuation characters are valid with the
475 @code{PRINT_OPERAND_PUNCT_VALID_P} macro.
477 @cindex \
478 @cindex backslash
479 The template may generate multiple assembler instructions.  Write the text
480 for the instructions, with @samp{\;} between them.
482 @cindex matching operands
483 When the RTL contains two operands which are required by constraint to match
484 each other, the output template must refer only to the lower-numbered operand.
485 Matching operands are not always identical, and the rest of the compiler
486 arranges to put the proper RTL expression for printing into the lower-numbered
487 operand.
489 One use of nonstandard letters or punctuation following @samp{%} is to
490 distinguish between different assembler languages for the same machine; for
491 example, Motorola syntax versus MIT syntax for the 68000.  Motorola syntax
492 requires periods in most opcode names, while MIT syntax does not.  For
493 example, the opcode @samp{movel} in MIT syntax is @samp{move.l} in Motorola
494 syntax.  The same file of patterns is used for both kinds of output syntax,
495 but the character sequence @samp{%.} is used in each place where Motorola
496 syntax wants a period.  The @code{PRINT_OPERAND} macro for Motorola syntax
497 defines the sequence to output a period; the macro for MIT syntax defines
498 it to do nothing.
500 @cindex @code{#} in template
501 As a special case, a template consisting of the single character @code{#}
502 instructs the compiler to first split the insn, and then output the
503 resulting instructions separately.  This helps eliminate redundancy in the
504 output templates.   If you have a @code{define_insn} that needs to emit
505 multiple assembler instructions, and there is an matching @code{define_split}
506 already defined, then you can simply use @code{#} as the output template
507 instead of writing an output template that emits the multiple assembler
508 instructions.
510 If the macro @code{ASSEMBLER_DIALECT} is defined, you can use construct
511 of the form @samp{@{option0|option1|option2@}} in the templates.  These
512 describe multiple variants of assembler language syntax.
513 @xref{Instruction Output}.
515 @node Output Statement
516 @section C Statements for Assembler Output
517 @cindex output statements
518 @cindex C statements for assembler output
519 @cindex generating assembler output
521 Often a single fixed template string cannot produce correct and efficient
522 assembler code for all the cases that are recognized by a single
523 instruction pattern.  For example, the opcodes may depend on the kinds of
524 operands; or some unfortunate combinations of operands may require extra
525 machine instructions.
527 If the output control string starts with a @samp{@@}, then it is actually
528 a series of templates, each on a separate line.  (Blank lines and
529 leading spaces and tabs are ignored.)  The templates correspond to the
530 pattern's constraint alternatives (@pxref{Multi-Alternative}).  For example,
531 if a target machine has a two-address add instruction @samp{addr} to add
532 into a register and another @samp{addm} to add a register to memory, you
533 might write this pattern:
535 @smallexample
536 (define_insn "addsi3"
537   [(set (match_operand:SI 0 "general_operand" "=r,m")
538         (plus:SI (match_operand:SI 1 "general_operand" "0,0")
539                  (match_operand:SI 2 "general_operand" "g,r")))]
540   ""
541   "@@
542    addr %2,%0
543    addm %2,%0")
544 @end smallexample
546 @cindex @code{*} in template
547 @cindex asterisk in template
548 If the output control string starts with a @samp{*}, then it is not an
549 output template but rather a piece of C program that should compute a
550 template.  It should execute a @code{return} statement to return the
551 template-string you want.  Most such templates use C string literals, which
552 require doublequote characters to delimit them.  To include these
553 doublequote characters in the string, prefix each one with @samp{\}.
555 The operands may be found in the array @code{operands}, whose C data type
556 is @code{rtx []}.
558 It is very common to select different ways of generating assembler code
559 based on whether an immediate operand is within a certain range.  Be
560 careful when doing this, because the result of @code{INTVAL} is an
561 integer on the host machine.  If the host machine has more bits in an
562 @code{int} than the target machine has in the mode in which the constant
563 will be used, then some of the bits you get from @code{INTVAL} will be
564 superfluous.  For proper results, you must carefully disregard the
565 values of those bits.
567 @findex output_asm_insn
568 It is possible to output an assembler instruction and then go on to output
569 or compute more of them, using the subroutine @code{output_asm_insn}.  This
570 receives two arguments: a template-string and a vector of operands.  The
571 vector may be @code{operands}, or it may be another array of @code{rtx}
572 that you declare locally and initialize yourself.
574 @findex which_alternative
575 When an insn pattern has multiple alternatives in its constraints, often
576 the appearance of the assembler code is determined mostly by which alternative
577 was matched.  When this is so, the C code can test the variable
578 @code{which_alternative}, which is the ordinal number of the alternative
579 that was actually satisfied (0 for the first, 1 for the second alternative,
580 etc.).
582 For example, suppose there are two opcodes for storing zero, @samp{clrreg}
583 for registers and @samp{clrmem} for memory locations.  Here is how
584 a pattern could use @code{which_alternative} to choose between them:
586 @smallexample
587 (define_insn ""
588   [(set (match_operand:SI 0 "general_operand" "=r,m")
589         (const_int 0))]
590   ""
591   "*
592   return (which_alternative == 0
593           ? \"clrreg %0\" : \"clrmem %0\");
594   ")
595 @end smallexample
597 The example above, where the assembler code to generate was
598 @emph{solely} determined by the alternative, could also have been specified
599 as follows, having the output control string start with a @samp{@@}:
601 @smallexample
602 @group
603 (define_insn ""
604   [(set (match_operand:SI 0 "general_operand" "=r,m")
605         (const_int 0))]
606   ""
607   "@@
608    clrreg %0
609    clrmem %0")
610 @end group
611 @end smallexample
612 @end ifset
614 @c Most of this node appears by itself (in a different place) even
615 @c when the INTERNALS flag is clear.  Passages that require the full
616 @c manual's context are conditionalized to appear only in the full manual.
617 @ifset INTERNALS
618 @node Constraints
619 @section Operand Constraints
620 @cindex operand constraints
621 @cindex constraints
623 Each @code{match_operand} in an instruction pattern can specify a
624 constraint for the type of operands allowed.
625 @end ifset
626 @ifclear INTERNALS
627 @node Constraints
628 @section Constraints for @code{asm} Operands
629 @cindex operand constraints, @code{asm}
630 @cindex constraints, @code{asm}
631 @cindex @code{asm} constraints
633 Here are specific details on what constraint letters you can use with
634 @code{asm} operands.
635 @end ifclear
636 Constraints can say whether
637 an operand may be in a register, and which kinds of register; whether the
638 operand can be a memory reference, and which kinds of address; whether the
639 operand may be an immediate constant, and which possible values it may
640 have.  Constraints can also require two operands to match.
642 @ifset INTERNALS
643 @menu
644 * Simple Constraints::  Basic use of constraints.
645 * Multi-Alternative::   When an insn has two alternative constraint-patterns.
646 * Class Preferences::   Constraints guide which hard register to put things in.
647 * Modifiers::           More precise control over effects of constraints.
648 * Machine Constraints:: Existing constraints for some particular machines.
649 * No Constraints::      Describing a clean machine without constraints.
650 @end menu
651 @end ifset
653 @ifclear INTERNALS
654 @menu
655 * Simple Constraints::  Basic use of constraints.
656 * Multi-Alternative::   When an insn has two alternative constraint-patterns.
657 * Modifiers::           More precise control over effects of constraints.
658 * Machine Constraints:: Special constraints for some particular machines.
659 @end menu
660 @end ifclear
662 @node Simple Constraints
663 @subsection Simple Constraints
664 @cindex simple constraints
666 The simplest kind of constraint is a string full of letters, each of
667 which describes one kind of operand that is permitted.  Here are
668 the letters that are allowed:
670 @table @asis
671 @cindex @samp{m} in constraint
672 @cindex memory references in constraints
673 @item @samp{m}
674 A memory operand is allowed, with any kind of address that the machine
675 supports in general.
677 @cindex offsettable address
678 @cindex @samp{o} in constraint
679 @item @samp{o}
680 A memory operand is allowed, but only if the address is
681 @dfn{offsettable}.  This means that adding a small integer (actually,
682 the width in bytes of the operand, as determined by its machine mode)
683 may be added to the address and the result is also a valid memory
684 address.
686 @cindex autoincrement/decrement addressing
687 For example, an address which is constant is offsettable; so is an
688 address that is the sum of a register and a constant (as long as a
689 slightly larger constant is also within the range of address-offsets
690 supported by the machine); but an autoincrement or autodecrement
691 address is not offsettable.  More complicated indirect/indexed
692 addresses may or may not be offsettable depending on the other
693 addressing modes that the machine supports.
695 Note that in an output operand which can be matched by another
696 operand, the constraint letter @samp{o} is valid only when accompanied
697 by both @samp{<} (if the target machine has predecrement addressing)
698 and @samp{>} (if the target machine has preincrement addressing).
700 @cindex @samp{V} in constraint
701 @item @samp{V}
702 A memory operand that is not offsettable.  In other words, anything that
703 would fit the @samp{m} constraint but not the @samp{o} constraint.
705 @cindex @samp{<} in constraint
706 @item @samp{<}
707 A memory operand with autodecrement addressing (either predecrement or
708 postdecrement) is allowed.
710 @cindex @samp{>} in constraint
711 @item @samp{>}
712 A memory operand with autoincrement addressing (either preincrement or
713 postincrement) is allowed.
715 @cindex @samp{r} in constraint
716 @cindex registers in constraints
717 @item @samp{r}
718 A register operand is allowed provided that it is in a general
719 register.
721 @cindex @samp{d} in constraint
722 @item @samp{d}, @samp{a}, @samp{f}, @dots{}
723 Other letters can be defined in machine-dependent fashion to stand for
724 particular classes of registers.  @samp{d}, @samp{a} and @samp{f} are
725 defined on the 68000/68020 to stand for data, address and floating
726 point registers.
728 @cindex constants in constraints
729 @cindex @samp{i} in constraint
730 @item @samp{i}
731 An immediate integer operand (one with constant value) is allowed.
732 This includes symbolic constants whose values will be known only at
733 assembly time.
735 @cindex @samp{n} in constraint
736 @item @samp{n}
737 An immediate integer operand with a known numeric value is allowed.
738 Many systems cannot support assembly-time constants for operands less
739 than a word wide.  Constraints for these operands should use @samp{n}
740 rather than @samp{i}.
742 @cindex @samp{I} in constraint
743 @item @samp{I}, @samp{J}, @samp{K}, @dots{} @samp{P}
744 Other letters in the range @samp{I} through @samp{P} may be defined in
745 a machine-dependent fashion to permit immediate integer operands with
746 explicit integer values in specified ranges.  For example, on the
747 68000, @samp{I} is defined to stand for the range of values 1 to 8.
748 This is the range permitted as a shift count in the shift
749 instructions.
751 @cindex @samp{E} in constraint
752 @item @samp{E}
753 An immediate floating operand (expression code @code{const_double}) is
754 allowed, but only if the target floating point format is the same as
755 that of the host machine (on which the compiler is running).
757 @cindex @samp{F} in constraint
758 @item @samp{F}
759 An immediate floating operand (expression code @code{const_double}) is
760 allowed.
762 @cindex @samp{G} in constraint
763 @cindex @samp{H} in constraint
764 @item @samp{G}, @samp{H}
765 @samp{G} and @samp{H} may be defined in a machine-dependent fashion to
766 permit immediate floating operands in particular ranges of values.
768 @cindex @samp{s} in constraint
769 @item @samp{s}
770 An immediate integer operand whose value is not an explicit integer is
771 allowed.
773 This might appear strange; if an insn allows a constant operand with a
774 value not known at compile time, it certainly must allow any known
775 value.  So why use @samp{s} instead of @samp{i}?  Sometimes it allows
776 better code to be generated.
778 For example, on the 68000 in a fullword instruction it is possible to
779 use an immediate operand; but if the immediate value is between -128
780 and 127, better code results from loading the value into a register and
781 using the register.  This is because the load into the register can be
782 done with a @samp{moveq} instruction.  We arrange for this to happen
783 by defining the letter @samp{K} to mean ``any integer outside the
784 range -128 to 127'', and then specifying @samp{Ks} in the operand
785 constraints.
787 @cindex @samp{g} in constraint
788 @item @samp{g}
789 Any register, memory or immediate integer operand is allowed, except for
790 registers that are not general registers.
792 @cindex @samp{X} in constraint
793 @item @samp{X}
794 @ifset INTERNALS
795 Any operand whatsoever is allowed, even if it does not satisfy
796 @code{general_operand}.  This is normally used in the constraint of
797 a @code{match_scratch} when certain alternatives will not actually
798 require a scratch register.
799 @end ifset
800 @ifclear INTERNALS
801 Any operand whatsoever is allowed.
802 @end ifclear
804 @cindex @samp{0} in constraint
805 @cindex digits in constraint
806 @item @samp{0}, @samp{1}, @samp{2}, @dots{} @samp{9}
807 An operand that matches the specified operand number is allowed.  If a
808 digit is used together with letters within the same alternative, the
809 digit should come last.
811 @cindex matching constraint
812 @cindex constraint, matching
813 This is called a @dfn{matching constraint} and what it really means is
814 that the assembler has only a single operand that fills two roles
815 @ifset INTERNALS
816 considered separate in the RTL insn.  For example, an add insn has two
817 input operands and one output operand in the RTL, but on most CISC
818 @end ifset
819 @ifclear INTERNALS
820 which @code{asm} distinguishes.  For example, an add instruction uses
821 two input operands and an output operand, but on most CISC
822 @end ifclear
823 machines an add instruction really has only two operands, one of them an
824 input-output operand:
826 @smallexample
827 addl #35,r12
828 @end smallexample
830 Matching constraints are used in these circumstances.
831 More precisely, the two operands that match must include one input-only
832 operand and one output-only operand.  Moreover, the digit must be a
833 smaller number than the number of the operand that uses it in the
834 constraint.
836 @ifset INTERNALS
837 For operands to match in a particular case usually means that they
838 are identical-looking RTL expressions.  But in a few special cases
839 specific kinds of dissimilarity are allowed.  For example, @code{*x}
840 as an input operand will match @code{*x++} as an output operand.
841 For proper results in such cases, the output template should always
842 use the output-operand's number when printing the operand.
843 @end ifset
845 @cindex load address instruction
846 @cindex push address instruction
847 @cindex address constraints
848 @cindex @samp{p} in constraint
849 @item @samp{p}
850 An operand that is a valid memory address is allowed.  This is
851 for ``load address'' and ``push address'' instructions.
853 @findex address_operand
854 @samp{p} in the constraint must be accompanied by @code{address_operand}
855 as the predicate in the @code{match_operand}.  This predicate interprets
856 the mode specified in the @code{match_operand} as the mode of the memory
857 reference for which the address would be valid.
859 @cindex extensible constraints
860 @cindex @samp{Q}, in constraint
861 @item @samp{Q}, @samp{R}, @samp{S}, @dots{} @samp{U}
862 Letters in the range @samp{Q} through @samp{U} may be defined in a
863 machine-dependent fashion to stand for arbitrary operand types.
864 @ifset INTERNALS
865 The machine description macro @code{EXTRA_CONSTRAINT} is passed the
866 operand as its first argument and the constraint letter as its
867 second operand.
869 A typical use for this would be to distinguish certain types of
870 memory references that affect other insn operands.
872 Do not define these constraint letters to accept register references
873 (@code{reg}); the reload pass does not expect this and would not handle
874 it properly.
875 @end ifset
876 @end table
878 @ifset INTERNALS
879 In order to have valid assembler code, each operand must satisfy
880 its constraint.  But a failure to do so does not prevent the pattern
881 from applying to an insn.  Instead, it directs the compiler to modify
882 the code so that the constraint will be satisfied.  Usually this is
883 done by copying an operand into a register.
885 Contrast, therefore, the two instruction patterns that follow:
887 @smallexample
888 (define_insn ""
889   [(set (match_operand:SI 0 "general_operand" "=r")
890         (plus:SI (match_dup 0)
891                  (match_operand:SI 1 "general_operand" "r")))]
892   ""
893   "@dots{}")
894 @end smallexample
896 @noindent
897 which has two operands, one of which must appear in two places, and
899 @smallexample
900 (define_insn ""
901   [(set (match_operand:SI 0 "general_operand" "=r")
902         (plus:SI (match_operand:SI 1 "general_operand" "0")
903                  (match_operand:SI 2 "general_operand" "r")))]
904   ""
905   "@dots{}")
906 @end smallexample
908 @noindent
909 which has three operands, two of which are required by a constraint to be
910 identical.  If we are considering an insn of the form
912 @smallexample
913 (insn @var{n} @var{prev} @var{next}
914   (set (reg:SI 3)
915        (plus:SI (reg:SI 6) (reg:SI 109)))
916   @dots{})
917 @end smallexample
919 @noindent
920 the first pattern would not apply at all, because this insn does not
921 contain two identical subexpressions in the right place.  The pattern would
922 say, ``That does not look like an add instruction; try other patterns.''
923 The second pattern would say, ``Yes, that's an add instruction, but there
924 is something wrong with it.''  It would direct the reload pass of the
925 compiler to generate additional insns to make the constraint true.  The
926 results might look like this:
928 @smallexample
929 (insn @var{n2} @var{prev} @var{n}
930   (set (reg:SI 3) (reg:SI 6))
931   @dots{})
933 (insn @var{n} @var{n2} @var{next}
934   (set (reg:SI 3)
935        (plus:SI (reg:SI 3) (reg:SI 109)))
936   @dots{})
937 @end smallexample
939 It is up to you to make sure that each operand, in each pattern, has
940 constraints that can handle any RTL expression that could be present for
941 that operand.  (When multiple alternatives are in use, each pattern must,
942 for each possible combination of operand expressions, have at least one
943 alternative which can handle that combination of operands.)  The
944 constraints don't need to @emph{allow} any possible operand---when this is
945 the case, they do not constrain---but they must at least point the way to
946 reloading any possible operand so that it will fit.
948 @itemize @bullet
949 @item
950 If the constraint accepts whatever operands the predicate permits,
951 there is no problem: reloading is never necessary for this operand.
953 For example, an operand whose constraints permit everything except
954 registers is safe provided its predicate rejects registers.
956 An operand whose predicate accepts only constant values is safe
957 provided its constraints include the letter @samp{i}.  If any possible
958 constant value is accepted, then nothing less than @samp{i} will do;
959 if the predicate is more selective, then the constraints may also be
960 more selective.
962 @item
963 Any operand expression can be reloaded by copying it into a register.
964 So if an operand's constraints allow some kind of register, it is
965 certain to be safe.  It need not permit all classes of registers; the
966 compiler knows how to copy a register into another register of the
967 proper class in order to make an instruction valid.
969 @cindex nonoffsettable memory reference
970 @cindex memory reference, nonoffsettable
971 @item
972 A nonoffsettable memory reference can be reloaded by copying the
973 address into a register.  So if the constraint uses the letter
974 @samp{o}, all memory references are taken care of.
976 @item
977 A constant operand can be reloaded by allocating space in memory to
978 hold it as preinitialized data.  Then the memory reference can be used
979 in place of the constant.  So if the constraint uses the letters
980 @samp{o} or @samp{m}, constant operands are not a problem.
982 @item
983 If the constraint permits a constant and a pseudo register used in an insn
984 was not allocated to a hard register and is equivalent to a constant,
985 the register will be replaced with the constant.  If the predicate does
986 not permit a constant and the insn is re-recognized for some reason, the
987 compiler will crash.  Thus the predicate must always recognize any
988 objects allowed by the constraint.
989 @end itemize
991 If the operand's predicate can recognize registers, but the constraint does
992 not permit them, it can make the compiler crash.  When this operand happens
993 to be a register, the reload pass will be stymied, because it does not know
994 how to copy a register temporarily into memory.
996 If the predicate accepts a unary operator, the constraint applies to the
997 operand.  For example, the MIPS processor at ISA level 3 supports an
998 instruction which adds two registers in @code{SImode} to produce a
999 @code{DImode} result, but only if the registers are correctly sign
1000 extended.  This predicate for the input operands accepts a
1001 @code{sign_extend} of an @code{SImode} register.  Write the constraint
1002 to indicate the type of register that is required for the operand of the
1003 @code{sign_extend}.
1004 @end ifset
1006 @node Multi-Alternative
1007 @subsection Multiple Alternative Constraints
1008 @cindex multiple alternative constraints
1010 Sometimes a single instruction has multiple alternative sets of possible
1011 operands.  For example, on the 68000, a logical-or instruction can combine
1012 register or an immediate value into memory, or it can combine any kind of
1013 operand into a register; but it cannot combine one memory location into
1014 another.
1016 These constraints are represented as multiple alternatives.  An alternative
1017 can be described by a series of letters for each operand.  The overall
1018 constraint for an operand is made from the letters for this operand
1019 from the first alternative, a comma, the letters for this operand from
1020 the second alternative, a comma, and so on until the last alternative.
1021 @ifset INTERNALS
1022 Here is how it is done for fullword logical-or on the 68000:
1024 @smallexample
1025 (define_insn "iorsi3"
1026   [(set (match_operand:SI 0 "general_operand" "=m,d")
1027         (ior:SI (match_operand:SI 1 "general_operand" "%0,0")
1028                 (match_operand:SI 2 "general_operand" "dKs,dmKs")))]
1029   @dots{})
1030 @end smallexample
1032 The first alternative has @samp{m} (memory) for operand 0, @samp{0} for
1033 operand 1 (meaning it must match operand 0), and @samp{dKs} for operand
1034 2.  The second alternative has @samp{d} (data register) for operand 0,
1035 @samp{0} for operand 1, and @samp{dmKs} for operand 2.  The @samp{=} and
1036 @samp{%} in the constraints apply to all the alternatives; their
1037 meaning is explained in the next section (@pxref{Class Preferences}).
1038 @end ifset
1040 @c FIXME Is this ? and ! stuff of use in asm()?  If not, hide unless INTERNAL
1041 If all the operands fit any one alternative, the instruction is valid.
1042 Otherwise, for each alternative, the compiler counts how many instructions
1043 must be added to copy the operands so that that alternative applies.
1044 The alternative requiring the least copying is chosen.  If two alternatives
1045 need the same amount of copying, the one that comes first is chosen.
1046 These choices can be altered with the @samp{?} and @samp{!} characters:
1048 @table @code
1049 @cindex @samp{?} in constraint
1050 @cindex question mark
1051 @item ?
1052 Disparage slightly the alternative that the @samp{?} appears in,
1053 as a choice when no alternative applies exactly.  The compiler regards
1054 this alternative as one unit more costly for each @samp{?} that appears
1055 in it.
1057 @cindex @samp{!} in constraint
1058 @cindex exclamation point
1059 @item !
1060 Disparage severely the alternative that the @samp{!} appears in.
1061 This alternative can still be used if it fits without reloading,
1062 but if reloading is needed, some other alternative will be used.
1063 @end table
1065 @ifset INTERNALS
1066 When an insn pattern has multiple alternatives in its constraints, often
1067 the appearance of the assembler code is determined mostly by which
1068 alternative was matched.  When this is so, the C code for writing the
1069 assembler code can use the variable @code{which_alternative}, which is
1070 the ordinal number of the alternative that was actually satisfied (0 for
1071 the first, 1 for the second alternative, etc.).  @xref{Output Statement}.
1072 @end ifset
1074 @ifset INTERNALS
1075 @node Class Preferences
1076 @subsection Register Class Preferences
1077 @cindex class preference constraints
1078 @cindex register class preference constraints
1080 @cindex voting between constraint alternatives
1081 The operand constraints have another function: they enable the compiler
1082 to decide which kind of hardware register a pseudo register is best
1083 allocated to.  The compiler examines the constraints that apply to the
1084 insns that use the pseudo register, looking for the machine-dependent
1085 letters such as @samp{d} and @samp{a} that specify classes of registers.
1086 The pseudo register is put in whichever class gets the most ``votes''.
1087 The constraint letters @samp{g} and @samp{r} also vote: they vote in
1088 favor of a general register.  The machine description says which registers
1089 are considered general.
1091 Of course, on some machines all registers are equivalent, and no register
1092 classes are defined.  Then none of this complexity is relevant.
1093 @end ifset
1095 @node Modifiers
1096 @subsection Constraint Modifier Characters
1097 @cindex modifiers in constraints
1098 @cindex constraint modifier characters
1100 @c prevent bad page break with this line
1101 Here are constraint modifier characters.
1103 @table @samp
1104 @cindex @samp{=} in constraint
1105 @item =
1106 Means that this operand is write-only for this instruction: the previous
1107 value is discarded and replaced by output data.
1109 @cindex @samp{+} in constraint
1110 @item +
1111 Means that this operand is both read and written by the instruction.
1113 When the compiler fixes up the operands to satisfy the constraints,
1114 it needs to know which operands are inputs to the instruction and
1115 which are outputs from it.  @samp{=} identifies an output; @samp{+}
1116 identifies an operand that is both input and output; all other operands
1117 are assumed to be input only.
1119 @cindex @samp{&} in constraint
1120 @cindex earlyclobber operand
1121 @item &
1122 Means (in a particular alternative) that this operand is an
1123 @dfn{earlyclobber} operand, which is modified before the instruction is
1124 finished using the input operands.  Therefore, this operand may not lie
1125 in a register that is used as an input operand or as part of any memory
1126 address.
1128 @samp{&} applies only to the alternative in which it is written.  In
1129 constraints with multiple alternatives, sometimes one alternative
1130 requires @samp{&} while others do not.  See, for example, the
1131 @samp{movdf} insn of the 68000.
1133 An input operand can be tied to an earlyclobber operand if its only 
1134 use as an input occurs before the early result is written.  Adding
1135 alternatives of this form often allows GCC to produce better code
1136 when only some of the inputs can be affected by the earlyclobber. 
1137 See, for example, the @samp{mulsi3} insn of the ARM.
1139 @samp{&} does not obviate the need to write @samp{=}.
1141 @cindex @samp{%} in constraint
1142 @item %
1143 Declares the instruction to be commutative for this operand and the
1144 following operand.  This means that the compiler may interchange the
1145 two operands if that is the cheapest way to make all operands fit the
1146 constraints.
1147 @ifset INTERNALS
1148 This is often used in patterns for addition instructions
1149 that really have only two operands: the result must go in one of the
1150 arguments.  Here for example, is how the 68000 halfword-add
1151 instruction is defined:
1153 @smallexample
1154 (define_insn "addhi3"
1155   [(set (match_operand:HI 0 "general_operand" "=m,r")
1156      (plus:HI (match_operand:HI 1 "general_operand" "%0,0")
1157               (match_operand:HI 2 "general_operand" "di,g")))]
1158   @dots{})
1159 @end smallexample
1160 @end ifset
1162 @cindex @samp{#} in constraint
1163 @item #
1164 Says that all following characters, up to the next comma, are to be
1165 ignored as a constraint.  They are significant only for choosing
1166 register preferences.
1168 @ifset INTERNALS
1169 @cindex @samp{*} in constraint
1170 @item *
1171 Says that the following character should be ignored when choosing
1172 register preferences.  @samp{*} has no effect on the meaning of the
1173 constraint as a constraint, and no effect on reloading.
1175 Here is an example: the 68000 has an instruction to sign-extend a
1176 halfword in a data register, and can also sign-extend a value by
1177 copying it into an address register.  While either kind of register is
1178 acceptable, the constraints on an address-register destination are
1179 less strict, so it is best if register allocation makes an address
1180 register its goal.  Therefore, @samp{*} is used so that the @samp{d}
1181 constraint letter (for data register) is ignored when computing
1182 register preferences.
1184 @smallexample
1185 (define_insn "extendhisi2"
1186   [(set (match_operand:SI 0 "general_operand" "=*d,a")
1187         (sign_extend:SI
1188          (match_operand:HI 1 "general_operand" "0,g")))]
1189   @dots{})
1190 @end smallexample
1191 @end ifset
1192 @end table
1194 @node Machine Constraints
1195 @subsection Constraints for Particular Machines
1196 @cindex machine specific constraints
1197 @cindex constraints, machine specific
1199 Whenever possible, you should use the general-purpose constraint letters
1200 in @code{asm} arguments, since they will convey meaning more readily to
1201 people reading your code.  Failing that, use the constraint letters
1202 that usually have very similar meanings across architectures.  The most
1203 commonly used constraints are @samp{m} and @samp{r} (for memory and
1204 general-purpose registers respectively; @pxref{Simple Constraints}), and
1205 @samp{I}, usually the letter indicating the most common
1206 immediate-constant format.
1208 For each machine architecture, the @file{config/@var{machine}.h} file
1209 defines additional constraints.  These constraints are used by the
1210 compiler itself for instruction generation, as well as for @code{asm}
1211 statements; therefore, some of the constraints are not particularly
1212 interesting for @code{asm}.  The constraints are defined through these
1213 macros:
1215 @table @code
1216 @item REG_CLASS_FROM_LETTER
1217 Register class constraints (usually lower case).
1219 @item CONST_OK_FOR_LETTER_P
1220 Immediate constant constraints, for non-floating point constants of
1221 word size or smaller precision (usually upper case).
1223 @item CONST_DOUBLE_OK_FOR_LETTER_P
1224 Immediate constant constraints, for all floating point constants and for
1225 constants of greater than word size precision (usually upper case).
1227 @item EXTRA_CONSTRAINT
1228 Special cases of registers or memory.  This macro is not required, and
1229 is only defined for some machines.
1230 @end table
1232 Inspecting these macro definitions in the compiler source for your
1233 machine is the best way to be certain you have the right constraints.
1234 However, here is a summary of the machine-dependent constraints
1235 available on some particular machines.
1237 @table @emph
1238 @item ARM family---@file{arm.h}
1239 @table @code
1240 @item f
1241 Floating-point register
1243 @item F
1244 One of the floating-point constants 0.0, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0
1245 or 10.0
1247 @item G
1248 Floating-point constant that would satisfy the constraint @samp{F} if it
1249 were negated
1251 @item I
1252 Integer that is valid as an immediate operand in a data processing
1253 instruction.  That is, an integer in the range 0 to 255 rotated by a
1254 multiple of 2
1256 @item J
1257 Integer in the range -4095 to 4095
1259 @item K
1260 Integer that satisfies constraint @samp{I} when inverted (ones complement)
1262 @item L
1263 Integer that satisfies constraint @samp{I} when negated (twos complement)
1265 @item M
1266 Integer in the range 0 to 32
1268 @item Q
1269 A memory reference where the exact address is in a single register
1270 (`@samp{m}' is preferable for @code{asm} statements)
1272 @item R
1273 An item in the constant pool
1275 @item S
1276 A symbol in the text segment of the current file
1277 @end table
1279 @item AMD 29000 family---@file{a29k.h}
1280 @table @code
1281 @item l
1282 Local register 0
1284 @item b
1285 Byte Pointer (@samp{BP}) register
1287 @item q
1288 @samp{Q} register
1290 @item h
1291 Special purpose register
1293 @item A
1294 First accumulator register
1296 @item a
1297 Other accumulator register
1299 @item f
1300 Floating point register
1302 @item I
1303 Constant greater than 0, less than 0x100
1305 @item J
1306 Constant greater than 0, less than 0x10000
1308 @item K
1309 Constant whose high 24 bits are on (1)
1311 @item L
1312 16 bit constant whose high 8 bits are on (1)
1314 @item M
1315 32 bit constant whose high 16 bits are on (1)
1317 @item N
1318 32 bit negative constant that fits in 8 bits
1320 @item O
1321 The constant 0x80000000 or, on the 29050, any 32 bit constant
1322 whose low 16 bits are 0.
1324 @item P
1325 16 bit negative constant that fits in 8 bits
1327 @item G
1328 @itemx H
1329 A floating point constant (in @code{asm} statements, use the machine
1330 independent @samp{E} or @samp{F} instead)
1331 @end table
1333 @item IBM RS6000---@file{rs6000.h}
1334 @table @code
1335 @item b
1336 Address base register
1338 @item f
1339 Floating point register
1341 @item h
1342 @samp{MQ}, @samp{CTR}, or @samp{LINK} register
1344 @item q
1345 @samp{MQ} register
1347 @item c
1348 @samp{CTR} register
1350 @item l
1351 @samp{LINK} register
1353 @item x
1354 @samp{CR} register (condition register) number 0
1356 @item y
1357 @samp{CR} register (condition register)
1359 @item I
1360 Signed 16 bit constant
1362 @item J
1363 Constant whose low 16 bits are 0
1365 @item K
1366 Constant whose high 16 bits are 0
1368 @item L
1369 Constant suitable as a mask operand
1371 @item M
1372 Constant larger than 31
1374 @item N
1375 Exact power of 2
1377 @item O
1378 Zero
1380 @item P
1381 Constant whose negation is a signed 16 bit constant
1383 @item G
1384 Floating point constant that can be loaded into a register with one
1385 instruction per word
1387 @item Q
1388 Memory operand that is an offset from a register (@samp{m} is preferable
1389 for @code{asm} statements)
1391 @item R
1392 AIX TOC entry
1394 @item S
1395 Windows NT SYMBOL_REF
1397 @item T
1398 Windows NT LABEL_REF
1400 @item U
1401 System V Release 4 small data area reference
1402 @end table
1404 @item Intel 386---@file{i386.h}
1405 @table @code
1406 @item q
1407 @samp{a}, @code{b}, @code{c}, or @code{d} register
1409 @item A
1410 @samp{a}, or @code{d} register (for 64-bit ints)
1412 @item f
1413 Floating point register
1415 @item t
1416 First (top of stack) floating point register
1418 @item u
1419 Second floating point register
1421 @item a
1422 @samp{a} register
1424 @item b
1425 @samp{b} register
1427 @item c
1428 @samp{c} register
1430 @item d
1431 @samp{d} register
1433 @item D
1434 @samp{di} register
1436 @item S
1437 @samp{si} register
1439 @item I
1440 Constant in range 0 to 31 (for 32 bit shifts)
1442 @item J
1443 Constant in range 0 to 63 (for 64 bit shifts)
1445 @item K
1446 @samp{0xff}
1448 @item L
1449 @samp{0xffff}
1451 @item M
1452 0, 1, 2, or 3 (shifts for @code{lea} instruction)
1454 @item N
1455 Constant in range 0 to 255 (for @code{out} instruction)
1457 @item G
1458 Standard 80387 floating point constant
1459 @end table
1461 @item Intel 960---@file{i960.h}
1462 @table @code
1463 @item f
1464 Floating point register (@code{fp0} to @code{fp3})
1466 @item l
1467 Local register (@code{r0} to @code{r15})
1469 @item b
1470 Global register (@code{g0} to @code{g15})
1472 @item d
1473 Any local or global register
1475 @item I
1476 Integers from 0 to 31
1478 @item J
1481 @item K
1482 Integers from -31 to 0
1484 @item G
1485 Floating point 0
1487 @item H
1488 Floating point 1
1489 @end table
1491 @item MIPS---@file{mips.h}
1492 @table @code
1493 @item d
1494 General-purpose integer register
1496 @item f
1497 Floating-point register (if available)
1499 @item h
1500 @samp{Hi} register
1502 @item l
1503 @samp{Lo} register
1505 @item x
1506 @samp{Hi} or @samp{Lo} register
1508 @item y
1509 General-purpose integer register
1511 @item z
1512 Floating-point status register
1514 @item I
1515 Signed 16 bit constant (for arithmetic instructions)
1517 @item J
1518 Zero
1520 @item K
1521 Zero-extended 16-bit constant (for logic instructions)
1523 @item L
1524 Constant with low 16 bits zero (can be loaded with @code{lui})
1526 @item M
1527 32 bit constant which requires two instructions to load (a constant
1528 which is not @samp{I}, @samp{K}, or @samp{L})
1530 @item N
1531 Negative 16 bit constant
1533 @item O
1534 Exact power of two
1536 @item P
1537 Positive 16 bit constant
1539 @item G
1540 Floating point zero
1542 @item Q
1543 Memory reference that can be loaded with more than one instruction
1544 (@samp{m} is preferable for @code{asm} statements)
1546 @item R
1547 Memory reference that can be loaded with one instruction
1548 (@samp{m} is preferable for @code{asm} statements)
1550 @item S
1551 Memory reference in external OSF/rose PIC format
1552 (@samp{m} is preferable for @code{asm} statements)
1553 @end table
1555 @item Motorola 680x0---@file{m68k.h}
1556 @table @code
1557 @item a
1558 Address register
1560 @item d
1561 Data register
1563 @item f
1564 68881 floating-point register, if available
1566 @item x
1567 Sun FPA (floating-point) register, if available
1569 @item y
1570 First 16 Sun FPA registers, if available
1572 @item I
1573 Integer in the range 1 to 8
1575 @item J
1576 16 bit signed number
1578 @item K
1579 Signed number whose magnitude is greater than 0x80
1581 @item L
1582 Integer in the range -8 to -1
1584 @item M
1585 Signed number whose magnitude is greater than 0x100
1587 @item G
1588 Floating point constant that is not a 68881 constant
1590 @item H
1591 Floating point constant that can be used by Sun FPA
1592 @end table
1594 @need 1000
1595 @item SPARC---@file{sparc.h}
1596 @table @code
1597 @item f
1598 Floating-point register that can hold 32 or 64 bit values.
1600 @item e
1601 Floating-point register that can hold 64 or 128 bit values.
1603 @item I
1604 Signed 13 bit constant
1606 @item J
1607 Zero
1609 @item K
1610 32 bit constant with the low 12 bits clear (a constant that can be
1611 loaded with the @code{sethi} instruction)
1613 @item G
1614 Floating-point zero
1616 @item H
1617 Signed 13 bit constant, sign-extended to 32 or 64 bits
1619 @item Q
1620 Memory reference that can be loaded with one instruction  (@samp{m} is
1621 more appropriate for @code{asm} statements)
1623 @item S
1624 Constant, or memory address
1626 @item T
1627 Memory address aligned to an 8-byte boundary
1629 @item U
1630 Even register
1631 @end table
1632 @end table
1634 @ifset INTERNALS
1635 @node No Constraints
1636 @subsection Not Using Constraints
1637 @cindex no constraints
1638 @cindex not using constraints
1640 Some machines are so clean that operand constraints are not required.  For
1641 example, on the Vax, an operand valid in one context is valid in any other
1642 context.  On such a machine, every operand constraint would be @samp{g},
1643 excepting only operands of ``load address'' instructions which are
1644 written as if they referred to a memory location's contents but actual
1645 refer to its address.  They would have constraint @samp{p}.
1647 @cindex empty constraints
1648 For such machines, instead of writing @samp{g} and @samp{p} for all
1649 the constraints, you can choose to write a description with empty constraints.
1650 Then you write @samp{""} for the constraint in every @code{match_operand}.
1651 Address operands are identified by writing an @code{address} expression
1652 around the @code{match_operand}, not by their constraints.
1654 When the machine description has just empty constraints, certain parts
1655 of compilation are skipped, making the compiler faster.  However,
1656 few machines actually do not need constraints; all machine descriptions
1657 now in existence use constraints.
1658 @end ifset
1660 @ifset INTERNALS
1661 @node Standard Names
1662 @section Standard Pattern Names For Generation
1663 @cindex standard pattern names
1664 @cindex pattern names
1665 @cindex names, pattern
1667 Here is a table of the instruction names that are meaningful in the RTL
1668 generation pass of the compiler.  Giving one of these names to an
1669 instruction pattern tells the RTL generation pass that it can use the
1670 pattern in to accomplish a certain task.
1672 @table @asis
1673 @cindex @code{mov@var{m}} instruction pattern
1674 @item @samp{mov@var{m}}
1675 Here @var{m} stands for a two-letter machine mode name, in lower case.
1676 This instruction pattern moves data with that machine mode from operand
1677 1 to operand 0.  For example, @samp{movsi} moves full-word data.
1679 If operand 0 is a @code{subreg} with mode @var{m} of a register whose
1680 own mode is wider than @var{m}, the effect of this instruction is
1681 to store the specified value in the part of the register that corresponds
1682 to mode @var{m}.  The effect on the rest of the register is undefined.
1684 This class of patterns is special in several ways.  First of all, each
1685 of these names @emph{must} be defined, because there is no other way
1686 to copy a datum from one place to another.
1688 Second, these patterns are not used solely in the RTL generation pass.
1689 Even the reload pass can generate move insns to copy values from stack
1690 slots into temporary registers.  When it does so, one of the operands is
1691 a hard register and the other is an operand that can need to be reloaded
1692 into a register.
1694 @findex force_reg
1695 Therefore, when given such a pair of operands, the pattern must generate
1696 RTL which needs no reloading and needs no temporary registers---no
1697 registers other than the operands.  For example, if you support the
1698 pattern with a @code{define_expand}, then in such a case the
1699 @code{define_expand} mustn't call @code{force_reg} or any other such
1700 function which might generate new pseudo registers.
1702 This requirement exists even for subword modes on a RISC machine where
1703 fetching those modes from memory normally requires several insns and
1704 some temporary registers.  Look in @file{spur.md} to see how the
1705 requirement can be satisfied.
1707 @findex change_address
1708 During reload a memory reference with an invalid address may be passed
1709 as an operand.  Such an address will be replaced with a valid address
1710 later in the reload pass.  In this case, nothing may be done with the
1711 address except to use it as it stands.  If it is copied, it will not be
1712 replaced with a valid address.  No attempt should be made to make such
1713 an address into a valid address and no routine (such as
1714 @code{change_address}) that will do so may be called.  Note that
1715 @code{general_operand} will fail when applied to such an address.
1717 @findex reload_in_progress
1718 The global variable @code{reload_in_progress} (which must be explicitly
1719 declared if required) can be used to determine whether such special
1720 handling is required.
1722 The variety of operands that have reloads depends on the rest of the
1723 machine description, but typically on a RISC machine these can only be
1724 pseudo registers that did not get hard registers, while on other
1725 machines explicit memory references will get optional reloads.
1727 If a scratch register is required to move an object to or from memory,
1728 it can be allocated using @code{gen_reg_rtx} prior to reload.  But this
1729 is impossible during and after reload.  If there are cases needing
1730 scratch registers after reload, you must define
1731 @code{SECONDARY_INPUT_RELOAD_CLASS} and perhaps also
1732 @code{SECONDARY_OUTPUT_RELOAD_CLASS} to detect them, and provide
1733 patterns @samp{reload_in@var{m}} or @samp{reload_out@var{m}} to handle
1734 them.  @xref{Register Classes}.
1736 The constraints on a @samp{move@var{m}} must permit moving any hard
1737 register to any other hard register provided that
1738 @code{HARD_REGNO_MODE_OK} permits mode @var{m} in both registers and
1739 @code{REGISTER_MOVE_COST} applied to their classes returns a value of 2.
1741 It is obligatory to support floating point @samp{move@var{m}}
1742 instructions into and out of any registers that can hold fixed point
1743 values, because unions and structures (which have modes @code{SImode} or
1744 @code{DImode}) can be in those registers and they may have floating
1745 point members.
1747 There may also be a need to support fixed point @samp{move@var{m}}
1748 instructions in and out of floating point registers.  Unfortunately, I
1749 have forgotten why this was so, and I don't know whether it is still
1750 true.  If @code{HARD_REGNO_MODE_OK} rejects fixed point values in
1751 floating point registers, then the constraints of the fixed point
1752 @samp{move@var{m}} instructions must be designed to avoid ever trying to
1753 reload into a floating point register.
1755 @cindex @code{reload_in} instruction pattern
1756 @cindex @code{reload_out} instruction pattern
1757 @item @samp{reload_in@var{m}}
1758 @itemx @samp{reload_out@var{m}}
1759 Like @samp{mov@var{m}}, but used when a scratch register is required to
1760 move between operand 0 and operand 1.  Operand 2 describes the scratch
1761 register.  See the discussion of the @code{SECONDARY_RELOAD_CLASS}
1762 macro in @pxref{Register Classes}.
1764 @cindex @code{movstrict@var{m}} instruction pattern
1765 @item @samp{movstrict@var{m}}
1766 Like @samp{mov@var{m}} except that if operand 0 is a @code{subreg}
1767 with mode @var{m} of a register whose natural mode is wider,
1768 the @samp{movstrict@var{m}} instruction is guaranteed not to alter
1769 any of the register except the part which belongs to mode @var{m}.
1771 @cindex @code{load_multiple} instruction pattern
1772 @item @samp{load_multiple}
1773 Load several consecutive memory locations into consecutive registers.
1774 Operand 0 is the first of the consecutive registers, operand 1
1775 is the first memory location, and operand 2 is a constant: the
1776 number of consecutive registers.
1778 Define this only if the target machine really has such an instruction;
1779 do not define this if the most efficient way of loading consecutive
1780 registers from memory is to do them one at a time.
1782 On some machines, there are restrictions as to which consecutive
1783 registers can be stored into memory, such as particular starting or
1784 ending register numbers or only a range of valid counts.  For those
1785 machines, use a @code{define_expand} (@pxref{Expander Definitions})
1786 and make the pattern fail if the restrictions are not met.
1788 Write the generated insn as a @code{parallel} with elements being a
1789 @code{set} of one register from the appropriate memory location (you may
1790 also need @code{use} or @code{clobber} elements).  Use a
1791 @code{match_parallel} (@pxref{RTL Template}) to recognize the insn.  See
1792 @file{a29k.md} and @file{rs6000.md} for examples of the use of this insn
1793 pattern.
1795 @cindex @samp{store_multiple} instruction pattern
1796 @item @samp{store_multiple}
1797 Similar to @samp{load_multiple}, but store several consecutive registers
1798 into consecutive memory locations.  Operand 0 is the first of the
1799 consecutive memory locations, operand 1 is the first register, and
1800 operand 2 is a constant: the number of consecutive registers.
1802 @cindex @code{add@var{m}3} instruction pattern
1803 @item @samp{add@var{m}3}
1804 Add operand 2 and operand 1, storing the result in operand 0.  All operands
1805 must have mode @var{m}.  This can be used even on two-address machines, by
1806 means of constraints requiring operands 1 and 0 to be the same location.
1808 @cindex @code{sub@var{m}3} instruction pattern
1809 @cindex @code{mul@var{m}3} instruction pattern
1810 @cindex @code{div@var{m}3} instruction pattern
1811 @cindex @code{udiv@var{m}3} instruction pattern
1812 @cindex @code{mod@var{m}3} instruction pattern
1813 @cindex @code{umod@var{m}3} instruction pattern
1814 @cindex @code{smin@var{m}3} instruction pattern
1815 @cindex @code{smax@var{m}3} instruction pattern
1816 @cindex @code{umin@var{m}3} instruction pattern
1817 @cindex @code{umax@var{m}3} instruction pattern
1818 @cindex @code{and@var{m}3} instruction pattern
1819 @cindex @code{ior@var{m}3} instruction pattern
1820 @cindex @code{xor@var{m}3} instruction pattern
1821 @item @samp{sub@var{m}3}, @samp{mul@var{m}3}
1822 @itemx @samp{div@var{m}3}, @samp{udiv@var{m}3}, @samp{mod@var{m}3}, @samp{umod@var{m}3}
1823 @itemx @samp{smin@var{m}3}, @samp{smax@var{m}3}, @samp{umin@var{m}3}, @samp{umax@var{m}3}
1824 @itemx @samp{and@var{m}3}, @samp{ior@var{m}3}, @samp{xor@var{m}3}
1825 Similar, for other arithmetic operations.
1827 @cindex @code{mulhisi3} instruction pattern
1828 @item @samp{mulhisi3}
1829 Multiply operands 1 and 2, which have mode @code{HImode}, and store
1830 a @code{SImode} product in operand 0.
1832 @cindex @code{mulqihi3} instruction pattern
1833 @cindex @code{mulsidi3} instruction pattern
1834 @item @samp{mulqihi3}, @samp{mulsidi3}
1835 Similar widening-multiplication instructions of other widths.
1837 @cindex @code{umulqihi3} instruction pattern
1838 @cindex @code{umulhisi3} instruction pattern
1839 @cindex @code{umulsidi3} instruction pattern
1840 @item @samp{umulqihi3}, @samp{umulhisi3}, @samp{umulsidi3}
1841 Similar widening-multiplication instructions that do unsigned
1842 multiplication.
1844 @cindex @code{smul@var{m}3_highpart} instruction pattern
1845 @item @samp{mul@var{m}3_highpart}
1846 Perform a signed multiplication of operands 1 and 2, which have mode
1847 @var{m}, and store the most significant half of the product in operand 0.
1848 The least significant half of the product is discarded.
1850 @cindex @code{umul@var{m}3_highpart} instruction pattern
1851 @item @samp{umul@var{m}3_highpart}
1852 Similar, but the multiplication is unsigned.
1854 @cindex @code{divmod@var{m}4} instruction pattern
1855 @item @samp{divmod@var{m}4}
1856 Signed division that produces both a quotient and a remainder.
1857 Operand 1 is divided by operand 2 to produce a quotient stored
1858 in operand 0 and a remainder stored in operand 3.
1860 For machines with an instruction that produces both a quotient and a
1861 remainder, provide a pattern for @samp{divmod@var{m}4} but do not
1862 provide patterns for @samp{div@var{m}3} and @samp{mod@var{m}3}.  This
1863 allows optimization in the relatively common case when both the quotient
1864 and remainder are computed.
1866 If an instruction that just produces a quotient or just a remainder
1867 exists and is more efficient than the instruction that produces both,
1868 write the output routine of @samp{divmod@var{m}4} to call
1869 @code{find_reg_note} and look for a @code{REG_UNUSED} note on the
1870 quotient or remainder and generate the appropriate instruction.
1872 @cindex @code{udivmod@var{m}4} instruction pattern
1873 @item @samp{udivmod@var{m}4}
1874 Similar, but does unsigned division.
1876 @cindex @code{ashl@var{m}3} instruction pattern
1877 @item @samp{ashl@var{m}3}
1878 Arithmetic-shift operand 1 left by a number of bits specified by operand
1879 2, and store the result in operand 0.  Here @var{m} is the mode of
1880 operand 0 and operand 1; operand 2's mode is specified by the
1881 instruction pattern, and the compiler will convert the operand to that
1882 mode before generating the instruction.
1884 @cindex @code{ashr@var{m}3} instruction pattern
1885 @cindex @code{lshr@var{m}3} instruction pattern
1886 @cindex @code{rotl@var{m}3} instruction pattern
1887 @cindex @code{rotr@var{m}3} instruction pattern
1888 @item @samp{ashr@var{m}3}, @samp{lshr@var{m}3}, @samp{rotl@var{m}3}, @samp{rotr@var{m}3}
1889 Other shift and rotate instructions, analogous to the
1890 @code{ashl@var{m}3} instructions.
1892 @cindex @code{neg@var{m}2} instruction pattern
1893 @item @samp{neg@var{m}2}
1894 Negate operand 1 and store the result in operand 0.
1896 @cindex @code{abs@var{m}2} instruction pattern
1897 @item @samp{abs@var{m}2}
1898 Store the absolute value of operand 1 into operand 0.
1900 @cindex @code{sqrt@var{m}2} instruction pattern
1901 @item @samp{sqrt@var{m}2}
1902 Store the square root of operand 1 into operand 0.
1904 The @code{sqrt} built-in function of C always uses the mode which
1905 corresponds to the C data type @code{double}.
1907 @cindex @code{ffs@var{m}2} instruction pattern
1908 @item @samp{ffs@var{m}2}
1909 Store into operand 0 one plus the index of the least significant 1-bit
1910 of operand 1.  If operand 1 is zero, store zero.  @var{m} is the mode
1911 of operand 0; operand 1's mode is specified by the instruction
1912 pattern, and the compiler will convert the operand to that mode before
1913 generating the instruction.
1915 The @code{ffs} built-in function of C always uses the mode which
1916 corresponds to the C data type @code{int}.
1918 @cindex @code{one_cmpl@var{m}2} instruction pattern
1919 @item @samp{one_cmpl@var{m}2}
1920 Store the bitwise-complement of operand 1 into operand 0.
1922 @cindex @code{cmp@var{m}} instruction pattern
1923 @item @samp{cmp@var{m}}
1924 Compare operand 0 and operand 1, and set the condition codes.
1925 The RTL pattern should look like this:
1927 @smallexample
1928 (set (cc0) (compare (match_operand:@var{m} 0 @dots{})
1929                     (match_operand:@var{m} 1 @dots{})))
1930 @end smallexample
1932 @cindex @code{tst@var{m}} instruction pattern
1933 @item @samp{tst@var{m}}
1934 Compare operand 0 against zero, and set the condition codes.
1935 The RTL pattern should look like this:
1937 @smallexample
1938 (set (cc0) (match_operand:@var{m} 0 @dots{}))
1939 @end smallexample
1941 @samp{tst@var{m}} patterns should not be defined for machines that do
1942 not use @code{(cc0)}.  Doing so would confuse the optimizer since it
1943 would no longer be clear which @code{set} operations were comparisons.
1944 The @samp{cmp@var{m}} patterns should be used instead.
1946 @cindex @code{movstr@var{m}} instruction pattern
1947 @item @samp{movstr@var{m}}
1948 Block move instruction.  The addresses of the destination and source
1949 strings are the first two operands, and both are in mode @code{Pmode}.
1950 The number of bytes to move is the third operand, in mode @var{m}.
1952 The fourth operand is the known shared alignment of the source and
1953 destination, in the form of a @code{const_int} rtx.  Thus, if the
1954 compiler knows that both source and destination are word-aligned,
1955 it may provide the value 4 for this operand.
1957 These patterns need not give special consideration to the possibility
1958 that the source and destination strings might overlap.
1960 @cindex @code{clrstr@var{m}} instruction pattern
1961 @item @samp{clrstr@var{m}}
1962 Block clear instruction.  The addresses of the destination string is the
1963 first operand, in mode @code{Pmode}.  The number of bytes to clear is
1964 the second operand, in mode @var{m}.
1966 The third operand is the known alignment of the destination, in the form
1967 of a @code{const_int} rtx.  Thus, if the compiler knows that the
1968 destination is word-aligned, it may provide the value 4 for this
1969 operand.
1971 @cindex @code{cmpstr@var{m}} instruction pattern
1972 @item @samp{cmpstr@var{m}}
1973 Block compare instruction, with five operands.  Operand 0 is the output;
1974 it has mode @var{m}.  The remaining four operands are like the operands
1975 of @samp{movstr@var{m}}.  The two memory blocks specified are compared
1976 byte by byte in lexicographic order.  The effect of the instruction is
1977 to store a value in operand 0 whose sign indicates the result of the
1978 comparison.
1980 @cindex @code{strlen@var{m}} instruction pattern
1981 @item @samp{strlen@var{m}}
1982 Compute the length of a string, with three operands.
1983 Operand 0 is the result (of mode @var{m}), operand 1 is
1984 a @code{mem} referring to the first character of the string,
1985 operand 2 is the character to search for (normally zero),
1986 and operand 3 is a constant describing the known alignment
1987 of the beginning of the string.
1989 @cindex @code{float@var{mn}2} instruction pattern
1990 @item @samp{float@var{m}@var{n}2}
1991 Convert signed integer operand 1 (valid for fixed point mode @var{m}) to
1992 floating point mode @var{n} and store in operand 0 (which has mode
1993 @var{n}).
1995 @cindex @code{floatuns@var{mn}2} instruction pattern
1996 @item @samp{floatuns@var{m}@var{n}2}
1997 Convert unsigned integer operand 1 (valid for fixed point mode @var{m})
1998 to floating point mode @var{n} and store in operand 0 (which has mode
1999 @var{n}).
2001 @cindex @code{fix@var{mn}2} instruction pattern
2002 @item @samp{fix@var{m}@var{n}2}
2003 Convert operand 1 (valid for floating point mode @var{m}) to fixed
2004 point mode @var{n} as a signed number and store in operand 0 (which
2005 has mode @var{n}).  This instruction's result is defined only when
2006 the value of operand 1 is an integer.
2008 @cindex @code{fixuns@var{mn}2} instruction pattern
2009 @item @samp{fixuns@var{m}@var{n}2}
2010 Convert operand 1 (valid for floating point mode @var{m}) to fixed
2011 point mode @var{n} as an unsigned number and store in operand 0 (which
2012 has mode @var{n}).  This instruction's result is defined only when the
2013 value of operand 1 is an integer.
2015 @cindex @code{ftrunc@var{m}2} instruction pattern
2016 @item @samp{ftrunc@var{m}2}
2017 Convert operand 1 (valid for floating point mode @var{m}) to an
2018 integer value, still represented in floating point mode @var{m}, and
2019 store it in operand 0 (valid for floating point mode @var{m}).
2021 @cindex @code{fix_trunc@var{mn}2} instruction pattern
2022 @item @samp{fix_trunc@var{m}@var{n}2}
2023 Like @samp{fix@var{m}@var{n}2} but works for any floating point value
2024 of mode @var{m} by converting the value to an integer.
2026 @cindex @code{fixuns_trunc@var{mn}2} instruction pattern
2027 @item @samp{fixuns_trunc@var{m}@var{n}2}
2028 Like @samp{fixuns@var{m}@var{n}2} but works for any floating point
2029 value of mode @var{m} by converting the value to an integer.
2031 @cindex @code{trunc@var{mn}2} instruction pattern
2032 @item @samp{trunc@var{m}@var{n}2}
2033 Truncate operand 1 (valid for mode @var{m}) to mode @var{n} and
2034 store in operand 0 (which has mode @var{n}).  Both modes must be fixed
2035 point or both floating point.
2037 @cindex @code{extend@var{mn}2} instruction pattern
2038 @item @samp{extend@var{m}@var{n}2}
2039 Sign-extend operand 1 (valid for mode @var{m}) to mode @var{n} and
2040 store in operand 0 (which has mode @var{n}).  Both modes must be fixed
2041 point or both floating point.
2043 @cindex @code{zero_extend@var{mn}2} instruction pattern
2044 @item @samp{zero_extend@var{m}@var{n}2}
2045 Zero-extend operand 1 (valid for mode @var{m}) to mode @var{n} and
2046 store in operand 0 (which has mode @var{n}).  Both modes must be fixed
2047 point.
2049 @cindex @code{extv} instruction pattern
2050 @item @samp{extv}
2051 Extract a bit field from operand 1 (a register or memory operand), where
2052 operand 2 specifies the width in bits and operand 3 the starting bit,
2053 and store it in operand 0.  Operand 0 must have mode @code{word_mode}.
2054 Operand 1 may have mode @code{byte_mode} or @code{word_mode}; often
2055 @code{word_mode} is allowed only for registers.  Operands 2 and 3 must
2056 be valid for @code{word_mode}.
2058 The RTL generation pass generates this instruction only with constants
2059 for operands 2 and 3.
2061 The bit-field value is sign-extended to a full word integer
2062 before it is stored in operand 0.
2064 @cindex @code{extzv} instruction pattern
2065 @item @samp{extzv}
2066 Like @samp{extv} except that the bit-field value is zero-extended.
2068 @cindex @code{insv} instruction pattern
2069 @item @samp{insv}
2070 Store operand 3 (which must be valid for @code{word_mode}) into a bit
2071 field in operand 0, where operand 1 specifies the width in bits and
2072 operand 2 the starting bit.  Operand 0 may have mode @code{byte_mode} or
2073 @code{word_mode}; often @code{word_mode} is allowed only for registers.
2074 Operands 1 and 2 must be valid for @code{word_mode}.
2076 The RTL generation pass generates this instruction only with constants
2077 for operands 1 and 2.
2079 @cindex @code{mov@var{mode}cc} instruction pattern
2080 @item @samp{mov@var{mode}cc}
2081 Conditionally move operand 2 or operand 3 into operand 0 according to the
2082 comparison in operand 1.  If the comparison is true, operand 2 is moved
2083 into operand 0, otherwise operand 3 is moved.
2085 The mode of the operands being compared need not be the same as the operands
2086 being moved.  Some machines, sparc64 for example, have instructions that
2087 conditionally move an integer value based on the floating point condition
2088 codes and vice versa.
2090 If the machine does not have conditional move instructions, do not
2091 define these patterns.
2093 @cindex @code{s@var{cond}} instruction pattern
2094 @item @samp{s@var{cond}}
2095 Store zero or nonzero in the operand according to the condition codes.
2096 Value stored is nonzero iff the condition @var{cond} is true.
2097 @var{cond} is the name of a comparison operation expression code, such
2098 as @code{eq}, @code{lt} or @code{leu}.
2100 You specify the mode that the operand must have when you write the
2101 @code{match_operand} expression.  The compiler automatically sees
2102 which mode you have used and supplies an operand of that mode.
2104 The value stored for a true condition must have 1 as its low bit, or
2105 else must be negative.  Otherwise the instruction is not suitable and
2106 you should omit it from the machine description.  You describe to the
2107 compiler exactly which value is stored by defining the macro
2108 @code{STORE_FLAG_VALUE} (@pxref{Misc}).  If a description cannot be
2109 found that can be used for all the @samp{s@var{cond}} patterns, you
2110 should omit those operations from the machine description.
2112 These operations may fail, but should do so only in relatively
2113 uncommon cases; if they would fail for common cases involving
2114 integer comparisons, it is best to omit these patterns.
2116 If these operations are omitted, the compiler will usually generate code
2117 that copies the constant one to the target and branches around an
2118 assignment of zero to the target.  If this code is more efficient than
2119 the potential instructions used for the @samp{s@var{cond}} pattern
2120 followed by those required to convert the result into a 1 or a zero in
2121 @code{SImode}, you should omit the @samp{s@var{cond}} operations from
2122 the machine description.
2124 @cindex @code{b@var{cond}} instruction pattern
2125 @item @samp{b@var{cond}}
2126 Conditional branch instruction.  Operand 0 is a @code{label_ref} that
2127 refers to the label to jump to.  Jump if the condition codes meet
2128 condition @var{cond}.
2130 Some machines do not follow the model assumed here where a comparison
2131 instruction is followed by a conditional branch instruction.  In that
2132 case, the @samp{cmp@var{m}} (and @samp{tst@var{m}}) patterns should
2133 simply store the operands away and generate all the required insns in a
2134 @code{define_expand} (@pxref{Expander Definitions}) for the conditional
2135 branch operations.  All calls to expand @samp{b@var{cond}} patterns are
2136 immediately preceded by calls to expand either a @samp{cmp@var{m}}
2137 pattern or a @samp{tst@var{m}} pattern.
2139 Machines that use a pseudo register for the condition code value, or
2140 where the mode used for the comparison depends on the condition being
2141 tested, should also use the above mechanism.  @xref{Jump Patterns}
2143 The above discussion also applies to the @samp{mov@var{mode}cc} and
2144 @samp{s@var{cond}} patterns.
2146 @cindex @code{call} instruction pattern
2147 @item @samp{call}
2148 Subroutine call instruction returning no value.  Operand 0 is the
2149 function to call; operand 1 is the number of bytes of arguments pushed
2150 (in mode @code{SImode}, except it is normally a @code{const_int});
2151 operand 2 is the number of registers used as operands.
2153 On most machines, operand 2 is not actually stored into the RTL
2154 pattern.  It is supplied for the sake of some RISC machines which need
2155 to put this information into the assembler code; they can put it in
2156 the RTL instead of operand 1.
2158 Operand 0 should be a @code{mem} RTX whose address is the address of the
2159 function.  Note, however, that this address can be a @code{symbol_ref}
2160 expression even if it would not be a legitimate memory address on the
2161 target machine.  If it is also not a valid argument for a call
2162 instruction, the pattern for this operation should be a
2163 @code{define_expand} (@pxref{Expander Definitions}) that places the
2164 address into a register and uses that register in the call instruction.
2166 @cindex @code{call_value} instruction pattern
2167 @item @samp{call_value}
2168 Subroutine call instruction returning a value.  Operand 0 is the hard
2169 register in which the value is returned.  There are three more
2170 operands, the same as the three operands of the @samp{call}
2171 instruction (but with numbers increased by one).
2173 Subroutines that return @code{BLKmode} objects use the @samp{call}
2174 insn.
2176 @cindex @code{call_pop} instruction pattern
2177 @cindex @code{call_value_pop} instruction pattern
2178 @item @samp{call_pop}, @samp{call_value_pop}
2179 Similar to @samp{call} and @samp{call_value}, except used if defined and
2180 if @code{RETURN_POPS_ARGS} is non-zero.  They should emit a @code{parallel}
2181 that contains both the function call and a @code{set} to indicate the
2182 adjustment made to the frame pointer.
2184 For machines where @code{RETURN_POPS_ARGS} can be non-zero, the use of these
2185 patterns increases the number of functions for which the frame pointer
2186 can be eliminated, if desired.
2188 @cindex @code{untyped_call} instruction pattern
2189 @item @samp{untyped_call}
2190 Subroutine call instruction returning a value of any type.  Operand 0 is
2191 the function to call; operand 1 is a memory location where the result of
2192 calling the function is to be stored; operand 2 is a @code{parallel}
2193 expression where each element is a @code{set} expression that indicates
2194 the saving of a function return value into the result block.
2196 This instruction pattern should be defined to support
2197 @code{__builtin_apply} on machines where special instructions are needed
2198 to call a subroutine with arbitrary arguments or to save the value
2199 returned.  This instruction pattern is required on machines that have
2200 multiple registers that can hold a return value (i.e.
2201 @code{FUNCTION_VALUE_REGNO_P} is true for more than one register).
2203 @cindex @code{return} instruction pattern
2204 @item @samp{return}
2205 Subroutine return instruction.  This instruction pattern name should be
2206 defined only if a single instruction can do all the work of returning
2207 from a function.
2209 Like the @samp{mov@var{m}} patterns, this pattern is also used after the
2210 RTL generation phase.  In this case it is to support machines where
2211 multiple instructions are usually needed to return from a function, but
2212 some class of functions only requires one instruction to implement a
2213 return.  Normally, the applicable functions are those which do not need
2214 to save any registers or allocate stack space.
2216 @findex reload_completed
2217 @findex leaf_function_p
2218 For such machines, the condition specified in this pattern should only
2219 be true when @code{reload_completed} is non-zero and the function's
2220 epilogue would only be a single instruction.  For machines with register
2221 windows, the routine @code{leaf_function_p} may be used to determine if
2222 a register window push is required.
2224 Machines that have conditional return instructions should define patterns
2225 such as
2227 @smallexample
2228 (define_insn ""
2229   [(set (pc)
2230         (if_then_else (match_operator
2231                          0 "comparison_operator"
2232                          [(cc0) (const_int 0)])
2233                       (return)
2234                       (pc)))]
2235   "@var{condition}"
2236   "@dots{}")
2237 @end smallexample
2239 where @var{condition} would normally be the same condition specified on the
2240 named @samp{return} pattern.
2242 @cindex @code{untyped_return} instruction pattern
2243 @item @samp{untyped_return}
2244 Untyped subroutine return instruction.  This instruction pattern should
2245 be defined to support @code{__builtin_return} on machines where special
2246 instructions are needed to return a value of any type.
2248 Operand 0 is a memory location where the result of calling a function
2249 with @code{__builtin_apply} is stored; operand 1 is a @code{parallel}
2250 expression where each element is a @code{set} expression that indicates
2251 the restoring of a function return value from the result block.
2253 @cindex @code{nop} instruction pattern
2254 @item @samp{nop}
2255 No-op instruction.  This instruction pattern name should always be defined
2256 to output a no-op in assembler code.  @code{(const_int 0)} will do as an
2257 RTL pattern.
2259 @cindex @code{indirect_jump} instruction pattern
2260 @item @samp{indirect_jump}
2261 An instruction to jump to an address which is operand zero.
2262 This pattern name is mandatory on all machines.
2264 @cindex @code{casesi} instruction pattern
2265 @item @samp{casesi}
2266 Instruction to jump through a dispatch table, including bounds checking.
2267 This instruction takes five operands:
2269 @enumerate
2270 @item
2271 The index to dispatch on, which has mode @code{SImode}.
2273 @item
2274 The lower bound for indices in the table, an integer constant.
2276 @item
2277 The total range of indices in the table---the largest index
2278 minus the smallest one (both inclusive).
2280 @item
2281 A label that precedes the table itself.
2283 @item
2284 A label to jump to if the index has a value outside the bounds.
2285 (If the machine-description macro @code{CASE_DROPS_THROUGH} is defined,
2286 then an out-of-bounds index drops through to the code following
2287 the jump table instead of jumping to this label.  In that case,
2288 this label is not actually used by the @samp{casesi} instruction,
2289 but it is always provided as an operand.)
2290 @end enumerate
2292 The table is a @code{addr_vec} or @code{addr_diff_vec} inside of a
2293 @code{jump_insn}.  The number of elements in the table is one plus the
2294 difference between the upper bound and the lower bound.
2296 @cindex @code{tablejump} instruction pattern
2297 @item @samp{tablejump}
2298 Instruction to jump to a variable address.  This is a low-level
2299 capability which can be used to implement a dispatch table when there
2300 is no @samp{casesi} pattern.
2302 This pattern requires two operands: the address or offset, and a label
2303 which should immediately precede the jump table.  If the macro
2304 @code{CASE_VECTOR_PC_RELATIVE} is defined then the first operand is an
2305 offset which counts from the address of the table; otherwise, it is an
2306 absolute address to jump to.  In either case, the first operand has
2307 mode @code{Pmode}.
2309 The @samp{tablejump} insn is always the last insn before the jump
2310 table it uses.  Its assembler code normally has no need to use the
2311 second operand, but you should incorporate it in the RTL pattern so
2312 that the jump optimizer will not delete the table as unreachable code.
2314 @cindex @code{canonicalize_funcptr_for_compare} instruction pattern
2315 @item @samp{canonicalize_funcptr_for_compare}
2316 Canonicalize the function pointer in operand 1 and store the result
2317 into operand 0.
2319 Operand 0 is always a @code{reg} and has mode @code{Pmode}; operand 1
2320 may be a @code{reg}, @code{mem}, @code{symbol_ref}, @code{const_int}, etc
2321 and also has mode @code{Pmode}.
2323 Canonicalization of a function pointer usually involves computing
2324 the address of the function which would be called if the function
2325 pointer were used in an indirect call.
2327 Only define this pattern if function pointers on the target machine
2328 can have different values but still call the same function when
2329 used in an indirect call.
2331 @cindex @code{save_stack_block} instruction pattern
2332 @cindex @code{save_stack_function} instruction pattern
2333 @cindex @code{save_stack_nonlocal} instruction pattern
2334 @cindex @code{restore_stack_block} instruction pattern
2335 @cindex @code{restore_stack_function} instruction pattern
2336 @cindex @code{restore_stack_nonlocal} instruction pattern
2337 @item @samp{save_stack_block}
2338 @itemx @samp{save_stack_function}
2339 @itemx @samp{save_stack_nonlocal}
2340 @itemx @samp{restore_stack_block}
2341 @itemx @samp{restore_stack_function}
2342 @itemx @samp{restore_stack_nonlocal}
2343 Most machines save and restore the stack pointer by copying it to or
2344 from an object of mode @code{Pmode}.  Do not define these patterns on
2345 such machines.
2347 Some machines require special handling for stack pointer saves and
2348 restores.  On those machines, define the patterns corresponding to the
2349 non-standard cases by using a @code{define_expand} (@pxref{Expander
2350 Definitions}) that produces the required insns.  The three types of
2351 saves and restores are:
2353 @enumerate
2354 @item
2355 @samp{save_stack_block} saves the stack pointer at the start of a block
2356 that allocates a variable-sized object, and @samp{restore_stack_block}
2357 restores the stack pointer when the block is exited.
2359 @item
2360 @samp{save_stack_function} and @samp{restore_stack_function} do a
2361 similar job for the outermost block of a function and are used when the
2362 function allocates variable-sized objects or calls @code{alloca}.  Only
2363 the epilogue uses the restored stack pointer, allowing a simpler save or
2364 restore sequence on some machines.
2366 @item
2367 @samp{save_stack_nonlocal} is used in functions that contain labels
2368 branched to by nested functions.  It saves the stack pointer in such a
2369 way that the inner function can use @samp{restore_stack_nonlocal} to
2370 restore the stack pointer.  The compiler generates code to restore the
2371 frame and argument pointer registers, but some machines require saving
2372 and restoring additional data such as register window information or
2373 stack backchains.  Place insns in these patterns to save and restore any
2374 such required data.
2375 @end enumerate
2377 When saving the stack pointer, operand 0 is the save area and operand 1
2378 is the stack pointer.  The mode used to allocate the save area is the
2379 mode of operand 0.  You must specify an integral mode, or
2380 @code{VOIDmode} if no save area is needed for a particular type of save
2381 (either because no save is needed or because a machine-specific save
2382 area can be used).  Operand 0 is the stack pointer and operand 1 is the
2383 save area for restore operations.  If @samp{save_stack_block} is
2384 defined, operand 0 must not be @code{VOIDmode} since these saves can be
2385 arbitrarily nested.
2387 A save area is a @code{mem} that is at a constant offset from
2388 @code{virtual_stack_vars_rtx} when the stack pointer is saved for use by
2389 nonlocal gotos and a @code{reg} in the other two cases.
2391 @cindex @code{allocate_stack} instruction pattern
2392 @item @samp{allocate_stack}
2393 Subtract (or add if @code{STACK_GROWS_DOWNWARD} is undefined) operand 0 from
2394 the stack pointer to create space for dynamically allocated data.
2396 Do not define this pattern if all that must be done is the subtraction.
2397 Some machines require other operations such as stack probes or
2398 maintaining the back chain.  Define this pattern to emit those
2399 operations in addition to updating the stack pointer.
2401 @cindex @code{probe} instruction pattern
2402 @item @samp{probe}
2403 Some machines require instructions to be executed after space is
2404 allocated from the stack, for example to generate a reference at
2405 the bottom of the stack.
2407 If you need to emit instructions before the stack has been adjusted,
2408 put them into the @samp{allocate_stack} pattern.  Otherwise, define
2409 this pattern to emit the required instructions.
2411 No operands are provided.
2413 @cindex @code{check_stack} instruction pattern
2414 @item @samp{check_stack}
2415 If stack checking cannot be done on your system by probing the stack with
2416 a load or store instruction (@pxref{Stack Checking}), define this pattern
2417 to perform the needed check and signaling an error if the stack
2418 has overflowed.  The single operand is the location in the stack furthest
2419 from the current stack pointer that you need to validate.  Normally,
2420 on machines where this pattern is needed, you would obtain the stack
2421 limit from a global or thread-specific variable or register.
2423 @cindex @code{nonlocal_goto} instruction pattern
2424 @item @samp{nonlocal_goto}
2425 Emit code to generate a non-local goto, e.g., a jump from one function
2426 to a label in an outer function.  This pattern has four arguments,
2427 each representing a value to be used in the jump.  The first
2428 argument is to be loadedd into the frame pointer, the second is
2429 the address to branch to (code to dispatch to the actual label),
2430 the third is the address of a location where the stack is saved,
2431 and the last is the address of the label, to be placed in the
2432 location for the incoming static chain.
2434 On most machines you need not define this pattern, since GNU CC will
2435 already generate the correct code, which is to load the frame pointer
2436 and static chain, restore the stack (using the
2437 @samp{restore_stack_nonlocal} pattern, if defined), and jump indirectly
2438 to the dispatcher.  You need only define this pattern if this code will
2439 not work on your machine.
2441 @cindex @code{nonlocal_goto_receiver} instruction pattern
2442 @item @samp{nonlocal_goto_receiver}
2443 This pattern, if defined, contains code needed at the target of a
2444 nonlocal goto after the code already generated by GNU CC.  You will not
2445 normally need to define this pattern.  A typical reason why you might
2446 need this pattern is if some value, such as a pointer to a global table,
2447 must be restored when the frame pointer is restored.  There are no
2448 arguments.
2450 @cindex @code{exception_receiver} instruction pattern
2451 @item @samp{exception_receiver}
2452 This pattern, if defined, contains code needed at the site of an
2453 exception handler that isn't needed at the site of a nonlocal goto.  You
2454 will not normally need to define this pattern.  A typical reason why you
2455 might need this pattern is if some value, such as a pointer to a global
2456 table, must be restored after control flow is branched to the handler of
2457 an exception.  There are no arguments.
2458 @end table
2460 @node Pattern Ordering
2461 @section When the Order of Patterns Matters
2462 @cindex Pattern Ordering
2463 @cindex Ordering of Patterns
2465 Sometimes an insn can match more than one instruction pattern.  Then the
2466 pattern that appears first in the machine description is the one used.
2467 Therefore, more specific patterns (patterns that will match fewer things)
2468 and faster instructions (those that will produce better code when they
2469 do match) should usually go first in the description.
2471 In some cases the effect of ordering the patterns can be used to hide
2472 a pattern when it is not valid.  For example, the 68000 has an
2473 instruction for converting a fullword to floating point and another
2474 for converting a byte to floating point.  An instruction converting
2475 an integer to floating point could match either one.  We put the
2476 pattern to convert the fullword first to make sure that one will
2477 be used rather than the other.  (Otherwise a large integer might
2478 be generated as a single-byte immediate quantity, which would not work.)
2479 Instead of using this pattern ordering it would be possible to make the
2480 pattern for convert-a-byte smart enough to deal properly with any
2481 constant value.
2483 @node Dependent Patterns
2484 @section Interdependence of Patterns
2485 @cindex Dependent Patterns
2486 @cindex Interdependence of Patterns
2488 Every machine description must have a named pattern for each of the
2489 conditional branch names @samp{b@var{cond}}.  The recognition template
2490 must always have the form
2492 @example
2493 (set (pc)
2494      (if_then_else (@var{cond} (cc0) (const_int 0))
2495                    (label_ref (match_operand 0 "" ""))
2496                    (pc)))
2497 @end example
2499 @noindent
2500 In addition, every machine description must have an anonymous pattern
2501 for each of the possible reverse-conditional branches.  Their templates
2502 look like
2504 @example
2505 (set (pc)
2506      (if_then_else (@var{cond} (cc0) (const_int 0))
2507                    (pc)
2508                    (label_ref (match_operand 0 "" ""))))
2509 @end example
2511 @noindent
2512 They are necessary because jump optimization can turn direct-conditional
2513 branches into reverse-conditional branches.
2515 It is often convenient to use the @code{match_operator} construct to
2516 reduce the number of patterns that must be specified for branches.  For
2517 example,
2519 @example
2520 (define_insn ""
2521   [(set (pc)
2522         (if_then_else (match_operator 0 "comparison_operator"
2523                                       [(cc0) (const_int 0)])
2524                       (pc)
2525                       (label_ref (match_operand 1 "" ""))))]
2526   "@var{condition}"
2527   "@dots{}")
2528 @end example
2530 In some cases machines support instructions identical except for the
2531 machine mode of one or more operands.  For example, there may be
2532 ``sign-extend halfword'' and ``sign-extend byte'' instructions whose
2533 patterns are
2535 @example
2536 (set (match_operand:SI 0 @dots{})
2537      (extend:SI (match_operand:HI 1 @dots{})))
2539 (set (match_operand:SI 0 @dots{})
2540      (extend:SI (match_operand:QI 1 @dots{})))
2541 @end example
2543 @noindent
2544 Constant integers do not specify a machine mode, so an instruction to
2545 extend a constant value could match either pattern.  The pattern it
2546 actually will match is the one that appears first in the file.  For correct
2547 results, this must be the one for the widest possible mode (@code{HImode},
2548 here).  If the pattern matches the @code{QImode} instruction, the results
2549 will be incorrect if the constant value does not actually fit that mode.
2551 Such instructions to extend constants are rarely generated because they are
2552 optimized away, but they do occasionally happen in nonoptimized
2553 compilations.
2555 If a constraint in a pattern allows a constant, the reload pass may
2556 replace a register with a constant permitted by the constraint in some
2557 cases.  Similarly for memory references.  Because of this substitution,
2558 you should not provide separate patterns for increment and decrement
2559 instructions.  Instead, they should be generated from the same pattern
2560 that supports register-register add insns by examining the operands and
2561 generating the appropriate machine instruction.
2563 @node Jump Patterns
2564 @section Defining Jump Instruction Patterns
2565 @cindex jump instruction patterns
2566 @cindex defining jump instruction patterns
2568 For most machines, GNU CC assumes that the machine has a condition code.
2569 A comparison insn sets the condition code, recording the results of both
2570 signed and unsigned comparison of the given operands.  A separate branch
2571 insn tests the condition code and branches or not according its value.
2572 The branch insns come in distinct signed and unsigned flavors.  Many
2573 common machines, such as the Vax, the 68000 and the 32000, work this
2574 way.
2576 Some machines have distinct signed and unsigned compare instructions, and
2577 only one set of conditional branch instructions.  The easiest way to handle
2578 these machines is to treat them just like the others until the final stage
2579 where assembly code is written.  At this time, when outputting code for the
2580 compare instruction, peek ahead at the following branch using
2581 @code{next_cc0_user (insn)}.  (The variable @code{insn} refers to the insn
2582 being output, in the output-writing code in an instruction pattern.)  If
2583 the RTL says that is an unsigned branch, output an unsigned compare;
2584 otherwise output a signed compare.  When the branch itself is output, you
2585 can treat signed and unsigned branches identically.
2587 The reason you can do this is that GNU CC always generates a pair of
2588 consecutive RTL insns, possibly separated by @code{note} insns, one to
2589 set the condition code and one to test it, and keeps the pair inviolate
2590 until the end.
2592 To go with this technique, you must define the machine-description macro
2593 @code{NOTICE_UPDATE_CC} to do @code{CC_STATUS_INIT}; in other words, no
2594 compare instruction is superfluous.
2596 Some machines have compare-and-branch instructions and no condition code.
2597 A similar technique works for them.  When it is time to ``output'' a
2598 compare instruction, record its operands in two static variables.  When
2599 outputting the branch-on-condition-code instruction that follows, actually
2600 output a compare-and-branch instruction that uses the remembered operands.
2602 It also works to define patterns for compare-and-branch instructions.
2603 In optimizing compilation, the pair of compare and branch instructions
2604 will be combined according to these patterns.  But this does not happen
2605 if optimization is not requested.  So you must use one of the solutions
2606 above in addition to any special patterns you define.
2608 In many RISC machines, most instructions do not affect the condition
2609 code and there may not even be a separate condition code register.  On
2610 these machines, the restriction that the definition and use of the
2611 condition code be adjacent insns is not necessary and can prevent
2612 important optimizations.  For example, on the IBM RS/6000, there is a
2613 delay for taken branches unless the condition code register is set three
2614 instructions earlier than the conditional branch.  The instruction
2615 scheduler cannot perform this optimization if it is not permitted to
2616 separate the definition and use of the condition code register.
2618 On these machines, do not use @code{(cc0)}, but instead use a register
2619 to represent the condition code.  If there is a specific condition code
2620 register in the machine, use a hard register.  If the condition code or
2621 comparison result can be placed in any general register, or if there are
2622 multiple condition registers, use a pseudo register.
2624 @findex prev_cc0_setter
2625 @findex next_cc0_user
2626 On some machines, the type of branch instruction generated may depend on
2627 the way the condition code was produced; for example, on the 68k and
2628 Sparc, setting the condition code directly from an add or subtract
2629 instruction does not clear the overflow bit the way that a test
2630 instruction does, so a different branch instruction must be used for
2631 some conditional branches.  For machines that use @code{(cc0)}, the set
2632 and use of the condition code must be adjacent (separated only by
2633 @code{note} insns) allowing flags in @code{cc_status} to be used.
2634 (@xref{Condition Code}.)  Also, the comparison and branch insns can be
2635 located from each other by using the functions @code{prev_cc0_setter}
2636 and @code{next_cc0_user}.
2638 However, this is not true on machines that do not use @code{(cc0)}.  On
2639 those machines, no assumptions can be made about the adjacency of the
2640 compare and branch insns and the above methods cannot be used.  Instead,
2641 we use the machine mode of the condition code register to record
2642 different formats of the condition code register.
2644 Registers used to store the condition code value should have a mode that
2645 is in class @code{MODE_CC}.  Normally, it will be @code{CCmode}.  If
2646 additional modes are required (as for the add example mentioned above in
2647 the Sparc), define the macro @code{EXTRA_CC_MODES} to list the
2648 additional modes required (@pxref{Condition Code}).  Also define
2649 @code{EXTRA_CC_NAMES} to list the names of those modes and
2650 @code{SELECT_CC_MODE} to choose a mode given an operand of a compare.
2652 If it is known during RTL generation that a different mode will be
2653 required (for example, if the machine has separate compare instructions
2654 for signed and unsigned quantities, like most IBM processors), they can
2655 be specified at that time.
2657 If the cases that require different modes would be made by instruction
2658 combination, the macro @code{SELECT_CC_MODE} determines which machine
2659 mode should be used for the comparison result.  The patterns should be
2660 written using that mode.  To support the case of the add on the Sparc
2661 discussed above, we have the pattern
2663 @smallexample
2664 (define_insn ""
2665   [(set (reg:CC_NOOV 0)
2666         (compare:CC_NOOV
2667           (plus:SI (match_operand:SI 0 "register_operand" "%r")
2668                    (match_operand:SI 1 "arith_operand" "rI"))
2669           (const_int 0)))]
2670   ""
2671   "@dots{}")
2672 @end smallexample
2674 The @code{SELECT_CC_MODE} macro on the Sparc returns @code{CC_NOOVmode}
2675 for comparisons whose argument is a @code{plus}.
2677 @node Insn Canonicalizations
2678 @section Canonicalization of Instructions
2679 @cindex canonicalization of instructions
2680 @cindex insn canonicalization
2682 There are often cases where multiple RTL expressions could represent an
2683 operation performed by a single machine instruction.  This situation is
2684 most commonly encountered with logical, branch, and multiply-accumulate
2685 instructions.  In such cases, the compiler attempts to convert these
2686 multiple RTL expressions into a single canonical form to reduce the
2687 number of insn patterns required.
2689 In addition to algebraic simplifications, following canonicalizations
2690 are performed:
2692 @itemize @bullet
2693 @item
2694 For commutative and comparison operators, a constant is always made the
2695 second operand.  If a machine only supports a constant as the second
2696 operand, only patterns that match a constant in the second operand need
2697 be supplied.
2699 @cindex @code{neg}, canonicalization of
2700 @cindex @code{not}, canonicalization of
2701 @cindex @code{mult}, canonicalization of
2702 @cindex @code{plus}, canonicalization of
2703 @cindex @code{minus}, canonicalization of
2704 For these operators, if only one operand is a @code{neg}, @code{not},
2705 @code{mult}, @code{plus}, or @code{minus} expression, it will be the
2706 first operand.
2708 @cindex @code{compare}, canonicalization of
2709 @item
2710 For the @code{compare} operator, a constant is always the second operand
2711 on machines where @code{cc0} is used (@pxref{Jump Patterns}).  On other
2712 machines, there are rare cases where the compiler might want to construct
2713 a @code{compare} with a constant as the first operand.  However, these
2714 cases are not common enough for it to be worthwhile to provide a pattern
2715 matching a constant as the first operand unless the machine actually has
2716 such an instruction.
2718 An operand of @code{neg}, @code{not}, @code{mult}, @code{plus}, or
2719 @code{minus} is made the first operand under the same conditions as
2720 above.
2722 @item
2723 @code{(minus @var{x} (const_int @var{n}))} is converted to
2724 @code{(plus @var{x} (const_int @var{-n}))}.
2726 @item
2727 Within address computations (i.e., inside @code{mem}), a left shift is
2728 converted into the appropriate multiplication by a power of two.
2730 @cindex @code{ior}, canonicalization of
2731 @cindex @code{and}, canonicalization of
2732 @cindex De Morgan's law
2733 @item
2734 De`Morgan's Law is used to move bitwise negation inside a bitwise
2735 logical-and or logical-or operation.  If this results in only one
2736 operand being a @code{not} expression, it will be the first one.
2738 A machine that has an instruction that performs a bitwise logical-and of one
2739 operand with the bitwise negation of the other should specify the pattern
2740 for that instruction as
2742 @example
2743 (define_insn ""
2744   [(set (match_operand:@var{m} 0 @dots{})
2745         (and:@var{m} (not:@var{m} (match_operand:@var{m} 1 @dots{}))
2746                      (match_operand:@var{m} 2 @dots{})))]
2747   "@dots{}"
2748   "@dots{}")
2749 @end example
2751 @noindent
2752 Similarly, a pattern for a ``NAND'' instruction should be written
2754 @example
2755 (define_insn ""
2756   [(set (match_operand:@var{m} 0 @dots{})
2757         (ior:@var{m} (not:@var{m} (match_operand:@var{m} 1 @dots{}))
2758                      (not:@var{m} (match_operand:@var{m} 2 @dots{}))))]
2759   "@dots{}"
2760   "@dots{}")
2761 @end example
2763 In both cases, it is not necessary to include patterns for the many
2764 logically equivalent RTL expressions.
2766 @cindex @code{xor}, canonicalization of
2767 @item
2768 The only possible RTL expressions involving both bitwise exclusive-or
2769 and bitwise negation are @code{(xor:@var{m} @var{x} @var{y})}
2770 and @code{(not:@var{m} (xor:@var{m} @var{x} @var{y}))}.@refill
2772 @item
2773 The sum of three items, one of which is a constant, will only appear in
2774 the form
2776 @example
2777 (plus:@var{m} (plus:@var{m} @var{x} @var{y}) @var{constant})
2778 @end example
2780 @item
2781 On machines that do not use @code{cc0},
2782 @code{(compare @var{x} (const_int 0))} will be converted to
2783 @var{x}.@refill
2785 @cindex @code{zero_extract}, canonicalization of
2786 @cindex @code{sign_extract}, canonicalization of
2787 @item
2788 Equality comparisons of a group of bits (usually a single bit) with zero
2789 will be written using @code{zero_extract} rather than the equivalent
2790 @code{and} or @code{sign_extract} operations.
2792 @end itemize
2794 @node Peephole Definitions
2795 @section Machine-Specific Peephole Optimizers
2796 @cindex peephole optimizer definitions
2797 @cindex defining peephole optimizers
2799 In addition to instruction patterns the @file{md} file may contain
2800 definitions of machine-specific peephole optimizations.
2802 The combiner does not notice certain peephole optimizations when the data
2803 flow in the program does not suggest that it should try them.  For example,
2804 sometimes two consecutive insns related in purpose can be combined even
2805 though the second one does not appear to use a register computed in the
2806 first one.  A machine-specific peephole optimizer can detect such
2807 opportunities.
2809 @need 1000
2810 A definition looks like this:
2812 @smallexample
2813 (define_peephole
2814   [@var{insn-pattern-1}
2815    @var{insn-pattern-2}
2816    @dots{}]
2817   "@var{condition}"
2818   "@var{template}"
2819   "@var{optional insn-attributes}")
2820 @end smallexample
2822 @noindent
2823 The last string operand may be omitted if you are not using any
2824 machine-specific information in this machine description.  If present,
2825 it must obey the same rules as in a @code{define_insn}.
2827 In this skeleton, @var{insn-pattern-1} and so on are patterns to match
2828 consecutive insns.  The optimization applies to a sequence of insns when
2829 @var{insn-pattern-1} matches the first one, @var{insn-pattern-2} matches
2830 the next, and so on.@refill
2832 Each of the insns matched by a peephole must also match a
2833 @code{define_insn}.  Peepholes are checked only at the last stage just
2834 before code generation, and only optionally.  Therefore, any insn which
2835 would match a peephole but no @code{define_insn} will cause a crash in code
2836 generation in an unoptimized compilation, or at various optimization
2837 stages.
2839 The operands of the insns are matched with @code{match_operands},
2840 @code{match_operator}, and @code{match_dup}, as usual.  What is not
2841 usual is that the operand numbers apply to all the insn patterns in the
2842 definition.  So, you can check for identical operands in two insns by
2843 using @code{match_operand} in one insn and @code{match_dup} in the
2844 other.
2846 The operand constraints used in @code{match_operand} patterns do not have
2847 any direct effect on the applicability of the peephole, but they will
2848 be validated afterward, so make sure your constraints are general enough
2849 to apply whenever the peephole matches.  If the peephole matches
2850 but the constraints are not satisfied, the compiler will crash.
2852 It is safe to omit constraints in all the operands of the peephole; or
2853 you can write constraints which serve as a double-check on the criteria
2854 previously tested.
2856 Once a sequence of insns matches the patterns, the @var{condition} is
2857 checked.  This is a C expression which makes the final decision whether to
2858 perform the optimization (we do so if the expression is nonzero).  If
2859 @var{condition} is omitted (in other words, the string is empty) then the
2860 optimization is applied to every sequence of insns that matches the
2861 patterns.
2863 The defined peephole optimizations are applied after register allocation
2864 is complete.  Therefore, the peephole definition can check which
2865 operands have ended up in which kinds of registers, just by looking at
2866 the operands.
2868 @findex prev_active_insn
2869 The way to refer to the operands in @var{condition} is to write
2870 @code{operands[@var{i}]} for operand number @var{i} (as matched by
2871 @code{(match_operand @var{i} @dots{})}).  Use the variable @code{insn}
2872 to refer to the last of the insns being matched; use
2873 @code{prev_active_insn} to find the preceding insns.
2875 @findex dead_or_set_p
2876 When optimizing computations with intermediate results, you can use
2877 @var{condition} to match only when the intermediate results are not used
2878 elsewhere.  Use the C expression @code{dead_or_set_p (@var{insn},
2879 @var{op})}, where @var{insn} is the insn in which you expect the value
2880 to be used for the last time (from the value of @code{insn}, together
2881 with use of @code{prev_nonnote_insn}), and @var{op} is the intermediate
2882 value (from @code{operands[@var{i}]}).@refill
2884 Applying the optimization means replacing the sequence of insns with one
2885 new insn.  The @var{template} controls ultimate output of assembler code
2886 for this combined insn.  It works exactly like the template of a
2887 @code{define_insn}.  Operand numbers in this template are the same ones
2888 used in matching the original sequence of insns.
2890 The result of a defined peephole optimizer does not need to match any of
2891 the insn patterns in the machine description; it does not even have an
2892 opportunity to match them.  The peephole optimizer definition itself serves
2893 as the insn pattern to control how the insn is output.
2895 Defined peephole optimizers are run as assembler code is being output,
2896 so the insns they produce are never combined or rearranged in any way.
2898 Here is an example, taken from the 68000 machine description:
2900 @smallexample
2901 (define_peephole
2902   [(set (reg:SI 15) (plus:SI (reg:SI 15) (const_int 4)))
2903    (set (match_operand:DF 0 "register_operand" "=f")
2904         (match_operand:DF 1 "register_operand" "ad"))]
2905   "FP_REG_P (operands[0]) && ! FP_REG_P (operands[1])"
2906   "*
2908   rtx xoperands[2];
2909   xoperands[1] = gen_rtx (REG, SImode, REGNO (operands[1]) + 1);
2910 #ifdef MOTOROLA
2911   output_asm_insn (\"move.l %1,(sp)\", xoperands);
2912   output_asm_insn (\"move.l %1,-(sp)\", operands);
2913   return \"fmove.d (sp)+,%0\";
2914 #else
2915   output_asm_insn (\"movel %1,sp@@\", xoperands);
2916   output_asm_insn (\"movel %1,sp@@-\", operands);
2917   return \"fmoved sp@@+,%0\";
2918 #endif
2921 @end smallexample
2923 @need 1000
2924 The effect of this optimization is to change
2926 @smallexample
2927 @group
2928 jbsr _foobar
2929 addql #4,sp
2930 movel d1,sp@@-
2931 movel d0,sp@@-
2932 fmoved sp@@+,fp0
2933 @end group
2934 @end smallexample
2936 @noindent
2937 into
2939 @smallexample
2940 @group
2941 jbsr _foobar
2942 movel d1,sp@@
2943 movel d0,sp@@-
2944 fmoved sp@@+,fp0
2945 @end group
2946 @end smallexample
2948 @ignore
2949 @findex CC_REVERSED
2950 If a peephole matches a sequence including one or more jump insns, you must
2951 take account of the flags such as @code{CC_REVERSED} which specify that the
2952 condition codes are represented in an unusual manner.  The compiler
2953 automatically alters any ordinary conditional jumps which occur in such
2954 situations, but the compiler cannot alter jumps which have been replaced by
2955 peephole optimizations.  So it is up to you to alter the assembler code
2956 that the peephole produces.  Supply C code to write the assembler output,
2957 and in this C code check the condition code status flags and change the
2958 assembler code as appropriate.
2959 @end ignore
2961 @var{insn-pattern-1} and so on look @emph{almost} like the second
2962 operand of @code{define_insn}.  There is one important difference: the
2963 second operand of @code{define_insn} consists of one or more RTX's
2964 enclosed in square brackets.  Usually, there is only one: then the same
2965 action can be written as an element of a @code{define_peephole}.  But
2966 when there are multiple actions in a @code{define_insn}, they are
2967 implicitly enclosed in a @code{parallel}.  Then you must explicitly
2968 write the @code{parallel}, and the square brackets within it, in the
2969 @code{define_peephole}.  Thus, if an insn pattern looks like this,
2971 @smallexample
2972 (define_insn "divmodsi4"
2973   [(set (match_operand:SI 0 "general_operand" "=d")
2974         (div:SI (match_operand:SI 1 "general_operand" "0")
2975                 (match_operand:SI 2 "general_operand" "dmsK")))
2976    (set (match_operand:SI 3 "general_operand" "=d")
2977         (mod:SI (match_dup 1) (match_dup 2)))]
2978   "TARGET_68020"
2979   "divsl%.l %2,%3:%0")
2980 @end smallexample
2982 @noindent
2983 then the way to mention this insn in a peephole is as follows:
2985 @smallexample
2986 (define_peephole
2987   [@dots{}
2988    (parallel
2989     [(set (match_operand:SI 0 "general_operand" "=d")
2990           (div:SI (match_operand:SI 1 "general_operand" "0")
2991                   (match_operand:SI 2 "general_operand" "dmsK")))
2992      (set (match_operand:SI 3 "general_operand" "=d")
2993           (mod:SI (match_dup 1) (match_dup 2)))])
2994    @dots{}]
2995   @dots{})
2996 @end smallexample
2998 @node Expander Definitions
2999 @section Defining RTL Sequences for Code Generation
3000 @cindex expander definitions
3001 @cindex code generation RTL sequences
3002 @cindex defining RTL sequences for code generation
3004 On some target machines, some standard pattern names for RTL generation
3005 cannot be handled with single insn, but a sequence of RTL insns can
3006 represent them.  For these target machines, you can write a
3007 @code{define_expand} to specify how to generate the sequence of RTL.
3009 @findex define_expand
3010 A @code{define_expand} is an RTL expression that looks almost like a
3011 @code{define_insn}; but, unlike the latter, a @code{define_expand} is used
3012 only for RTL generation and it can produce more than one RTL insn.
3014 A @code{define_expand} RTX has four operands:
3016 @itemize @bullet
3017 @item
3018 The name.  Each @code{define_expand} must have a name, since the only
3019 use for it is to refer to it by name.
3021 @findex define_peephole
3022 @item
3023 The RTL template.  This is just like the RTL template for a
3024 @code{define_peephole} in that it is a vector of RTL expressions
3025 each being one insn.
3027 @item
3028 The condition, a string containing a C expression.  This expression is
3029 used to express how the availability of this pattern depends on
3030 subclasses of target machine, selected by command-line options when GNU
3031 CC is run.  This is just like the condition of a @code{define_insn} that
3032 has a standard name.  Therefore, the condition (if present) may not
3033 depend on the data in the insn being matched, but only the
3034 target-machine-type flags.  The compiler needs to test these conditions
3035 during initialization in order to learn exactly which named instructions
3036 are available in a particular run.
3038 @item
3039 The preparation statements, a string containing zero or more C
3040 statements which are to be executed before RTL code is generated from
3041 the RTL template.
3043 Usually these statements prepare temporary registers for use as
3044 internal operands in the RTL template, but they can also generate RTL
3045 insns directly by calling routines such as @code{emit_insn}, etc.
3046 Any such insns precede the ones that come from the RTL template.
3047 @end itemize
3049 Every RTL insn emitted by a @code{define_expand} must match some
3050 @code{define_insn} in the machine description.  Otherwise, the compiler
3051 will crash when trying to generate code for the insn or trying to optimize
3054 The RTL template, in addition to controlling generation of RTL insns,
3055 also describes the operands that need to be specified when this pattern
3056 is used.  In particular, it gives a predicate for each operand.
3058 A true operand, which needs to be specified in order to generate RTL from
3059 the pattern, should be described with a @code{match_operand} in its first
3060 occurrence in the RTL template.  This enters information on the operand's
3061 predicate into the tables that record such things.  GNU CC uses the
3062 information to preload the operand into a register if that is required for
3063 valid RTL code.  If the operand is referred to more than once, subsequent
3064 references should use @code{match_dup}.
3066 The RTL template may also refer to internal ``operands'' which are
3067 temporary registers or labels used only within the sequence made by the
3068 @code{define_expand}.  Internal operands are substituted into the RTL
3069 template with @code{match_dup}, never with @code{match_operand}.  The
3070 values of the internal operands are not passed in as arguments by the
3071 compiler when it requests use of this pattern.  Instead, they are computed
3072 within the pattern, in the preparation statements.  These statements
3073 compute the values and store them into the appropriate elements of
3074 @code{operands} so that @code{match_dup} can find them.
3076 There are two special macros defined for use in the preparation statements:
3077 @code{DONE} and @code{FAIL}.  Use them with a following semicolon,
3078 as a statement.
3080 @table @code
3082 @findex DONE
3083 @item DONE
3084 Use the @code{DONE} macro to end RTL generation for the pattern.  The
3085 only RTL insns resulting from the pattern on this occasion will be
3086 those already emitted by explicit calls to @code{emit_insn} within the
3087 preparation statements; the RTL template will not be generated.
3089 @findex FAIL
3090 @item FAIL
3091 Make the pattern fail on this occasion.  When a pattern fails, it means
3092 that the pattern was not truly available.  The calling routines in the
3093 compiler will try other strategies for code generation using other patterns.
3095 Failure is currently supported only for binary (addition, multiplication,
3096 shifting, etc.) and bitfield (@code{extv}, @code{extzv}, and @code{insv})
3097 operations.
3098 @end table
3100 Here is an example, the definition of left-shift for the SPUR chip:
3102 @smallexample
3103 @group
3104 (define_expand "ashlsi3"
3105   [(set (match_operand:SI 0 "register_operand" "")
3106         (ashift:SI
3107 @end group
3108 @group
3109           (match_operand:SI 1 "register_operand" "")
3110           (match_operand:SI 2 "nonmemory_operand" "")))]
3111   ""
3112   "
3113 @end group
3114 @end smallexample
3116 @smallexample
3117 @group
3119   if (GET_CODE (operands[2]) != CONST_INT
3120       || (unsigned) INTVAL (operands[2]) > 3)
3121     FAIL;
3122 @}")
3123 @end group
3124 @end smallexample
3126 @noindent
3127 This example uses @code{define_expand} so that it can generate an RTL insn
3128 for shifting when the shift-count is in the supported range of 0 to 3 but
3129 fail in other cases where machine insns aren't available.  When it fails,
3130 the compiler tries another strategy using different patterns (such as, a
3131 library call).
3133 If the compiler were able to handle nontrivial condition-strings in
3134 patterns with names, then it would be possible to use a
3135 @code{define_insn} in that case.  Here is another case (zero-extension
3136 on the 68000) which makes more use of the power of @code{define_expand}:
3138 @smallexample
3139 (define_expand "zero_extendhisi2"
3140   [(set (match_operand:SI 0 "general_operand" "")
3141         (const_int 0))
3142    (set (strict_low_part
3143           (subreg:HI
3144             (match_dup 0)
3145             0))
3146         (match_operand:HI 1 "general_operand" ""))]
3147   ""
3148   "operands[1] = make_safe_from (operands[1], operands[0]);")
3149 @end smallexample
3151 @noindent
3152 @findex make_safe_from
3153 Here two RTL insns are generated, one to clear the entire output operand
3154 and the other to copy the input operand into its low half.  This sequence
3155 is incorrect if the input operand refers to [the old value of] the output
3156 operand, so the preparation statement makes sure this isn't so.  The
3157 function @code{make_safe_from} copies the @code{operands[1]} into a
3158 temporary register if it refers to @code{operands[0]}.  It does this
3159 by emitting another RTL insn.
3161 Finally, a third example shows the use of an internal operand.
3162 Zero-extension on the SPUR chip is done by @code{and}-ing the result
3163 against a halfword mask.  But this mask cannot be represented by a
3164 @code{const_int} because the constant value is too large to be legitimate
3165 on this machine.  So it must be copied into a register with
3166 @code{force_reg} and then the register used in the @code{and}.
3168 @smallexample
3169 (define_expand "zero_extendhisi2"
3170   [(set (match_operand:SI 0 "register_operand" "")
3171         (and:SI (subreg:SI
3172                   (match_operand:HI 1 "register_operand" "")
3173                   0)
3174                 (match_dup 2)))]
3175   ""
3176   "operands[2]
3177      = force_reg (SImode, gen_rtx (CONST_INT,
3178                                    VOIDmode, 65535)); ")
3179 @end smallexample
3181 @strong{Note:} If the @code{define_expand} is used to serve a
3182 standard binary or unary arithmetic operation or a bitfield operation,
3183 then the last insn it generates must not be a @code{code_label},
3184 @code{barrier} or @code{note}.  It must be an @code{insn},
3185 @code{jump_insn} or @code{call_insn}.  If you don't need a real insn
3186 at the end, emit an insn to copy the result of the operation into
3187 itself.  Such an insn will generate no code, but it can avoid problems
3188 in the compiler.@refill
3190 @node Insn Splitting
3191 @section Defining How to Split Instructions
3192 @cindex insn splitting
3193 @cindex instruction splitting
3194 @cindex splitting instructions
3196 There are two cases where you should specify how to split a pattern into
3197 multiple insns.  On machines that have instructions requiring delay
3198 slots (@pxref{Delay Slots}) or that have instructions whose output is
3199 not available for multiple cycles (@pxref{Function Units}), the compiler
3200 phases that optimize these cases need to be able to move insns into
3201 one-instruction delay slots.  However, some insns may generate more than one
3202 machine instruction.  These insns cannot be placed into a delay slot.
3204 Often you can rewrite the single insn as a list of individual insns,
3205 each corresponding to one machine instruction.  The disadvantage of
3206 doing so is that it will cause the compilation to be slower and require
3207 more space.  If the resulting insns are too complex, it may also
3208 suppress some optimizations.  The compiler splits the insn if there is a
3209 reason to believe that it might improve instruction or delay slot
3210 scheduling.
3212 The insn combiner phase also splits putative insns.  If three insns are
3213 merged into one insn with a complex expression that cannot be matched by
3214 some @code{define_insn} pattern, the combiner phase attempts to split
3215 the complex pattern into two insns that are recognized.  Usually it can
3216 break the complex pattern into two patterns by splitting out some
3217 subexpression.  However, in some other cases, such as performing an
3218 addition of a large constant in two insns on a RISC machine, the way to
3219 split the addition into two insns is machine-dependent.
3221 @cindex define_split
3222 The @code{define_split} definition tells the compiler how to split a
3223 complex insn into several simpler insns.  It looks like this:
3225 @smallexample
3226 (define_split
3227   [@var{insn-pattern}]
3228   "@var{condition}"
3229   [@var{new-insn-pattern-1}
3230    @var{new-insn-pattern-2}
3231    @dots{}]
3232   "@var{preparation statements}")
3233 @end smallexample
3235 @var{insn-pattern} is a pattern that needs to be split and
3236 @var{condition} is the final condition to be tested, as in a
3237 @code{define_insn}.  When an insn matching @var{insn-pattern} and
3238 satisfying @var{condition} is found, it is replaced in the insn list
3239 with the insns given by @var{new-insn-pattern-1},
3240 @var{new-insn-pattern-2}, etc.
3242 The @var{preparation statements} are similar to those statements that
3243 are specified for @code{define_expand} (@pxref{Expander Definitions})
3244 and are executed before the new RTL is generated to prepare for the
3245 generated code or emit some insns whose pattern is not fixed.  Unlike
3246 those in @code{define_expand}, however, these statements must not
3247 generate any new pseudo-registers.  Once reload has completed, they also
3248 must not allocate any space in the stack frame.
3250 Patterns are matched against @var{insn-pattern} in two different
3251 circumstances.  If an insn needs to be split for delay slot scheduling
3252 or insn scheduling, the insn is already known to be valid, which means
3253 that it must have been matched by some @code{define_insn} and, if
3254 @code{reload_completed} is non-zero, is known to satisfy the constraints
3255 of that @code{define_insn}.  In that case, the new insn patterns must
3256 also be insns that are matched by some @code{define_insn} and, if
3257 @code{reload_completed} is non-zero, must also satisfy the constraints
3258 of those definitions.
3260 As an example of this usage of @code{define_split}, consider the following
3261 example from @file{a29k.md}, which splits a @code{sign_extend} from
3262 @code{HImode} to @code{SImode} into a pair of shift insns:
3264 @smallexample
3265 (define_split
3266   [(set (match_operand:SI 0 "gen_reg_operand" "")
3267         (sign_extend:SI (match_operand:HI 1 "gen_reg_operand" "")))]
3268   ""
3269   [(set (match_dup 0)
3270         (ashift:SI (match_dup 1)
3271                    (const_int 16)))
3272    (set (match_dup 0)
3273         (ashiftrt:SI (match_dup 0)
3274                      (const_int 16)))]
3275   "
3276 @{ operands[1] = gen_lowpart (SImode, operands[1]); @}")
3277 @end smallexample
3279 When the combiner phase tries to split an insn pattern, it is always the
3280 case that the pattern is @emph{not} matched by any @code{define_insn}.
3281 The combiner pass first tries to split a single @code{set} expression
3282 and then the same @code{set} expression inside a @code{parallel}, but
3283 followed by a @code{clobber} of a pseudo-reg to use as a scratch
3284 register.  In these cases, the combiner expects exactly two new insn
3285 patterns to be generated.  It will verify that these patterns match some
3286 @code{define_insn} definitions, so you need not do this test in the
3287 @code{define_split} (of course, there is no point in writing a
3288 @code{define_split} that will never produce insns that match).
3290 Here is an example of this use of @code{define_split}, taken from
3291 @file{rs6000.md}:
3293 @smallexample
3294 (define_split
3295   [(set (match_operand:SI 0 "gen_reg_operand" "")
3296         (plus:SI (match_operand:SI 1 "gen_reg_operand" "")
3297                  (match_operand:SI 2 "non_add_cint_operand" "")))]
3298   ""
3299   [(set (match_dup 0) (plus:SI (match_dup 1) (match_dup 3)))
3300    (set (match_dup 0) (plus:SI (match_dup 0) (match_dup 4)))]
3303   int low = INTVAL (operands[2]) & 0xffff;
3304   int high = (unsigned) INTVAL (operands[2]) >> 16;
3306   if (low & 0x8000)
3307     high++, low |= 0xffff0000;
3309   operands[3] = gen_rtx (CONST_INT, VOIDmode, high << 16);
3310   operands[4] = gen_rtx (CONST_INT, VOIDmode, low);
3311 @}")
3312 @end smallexample
3314 Here the predicate @code{non_add_cint_operand} matches any
3315 @code{const_int} that is @emph{not} a valid operand of a single add
3316 insn.  The add with the smaller displacement is written so that it
3317 can be substituted into the address of a subsequent operation.
3319 An example that uses a scratch register, from the same file, generates
3320 an equality comparison of a register and a large constant:
3322 @smallexample
3323 (define_split
3324   [(set (match_operand:CC 0 "cc_reg_operand" "")
3325         (compare:CC (match_operand:SI 1 "gen_reg_operand" "")
3326                     (match_operand:SI 2 "non_short_cint_operand" "")))
3327    (clobber (match_operand:SI 3 "gen_reg_operand" ""))]
3328   "find_single_use (operands[0], insn, 0)
3329    && (GET_CODE (*find_single_use (operands[0], insn, 0)) == EQ
3330        || GET_CODE (*find_single_use (operands[0], insn, 0)) == NE)"
3331   [(set (match_dup 3) (xor:SI (match_dup 1) (match_dup 4)))
3332    (set (match_dup 0) (compare:CC (match_dup 3) (match_dup 5)))]
3333   "
3335   /* Get the constant we are comparing against, C, and see what it
3336      looks like sign-extended to 16 bits.  Then see what constant
3337      could be XOR'ed with C to get the sign-extended value.  */
3339   int c = INTVAL (operands[2]);
3340   int sextc = (c << 16) >> 16;
3341   int xorv = c ^ sextc;
3343   operands[4] = gen_rtx (CONST_INT, VOIDmode, xorv);
3344   operands[5] = gen_rtx (CONST_INT, VOIDmode, sextc);
3345 @}")
3346 @end smallexample
3348 To avoid confusion, don't write a single @code{define_split} that
3349 accepts some insns that match some @code{define_insn} as well as some
3350 insns that don't.  Instead, write two separate @code{define_split}
3351 definitions, one for the insns that are valid and one for the insns that
3352 are not valid.
3354 @node Insn Attributes
3355 @section Instruction Attributes
3356 @cindex insn attributes
3357 @cindex instruction attributes
3359 In addition to describing the instruction supported by the target machine,
3360 the @file{md} file also defines a group of @dfn{attributes} and a set of
3361 values for each.  Every generated insn is assigned a value for each attribute.
3362 One possible attribute would be the effect that the insn has on the machine's
3363 condition code.  This attribute can then be used by @code{NOTICE_UPDATE_CC}
3364 to track the condition codes.
3366 @menu
3367 * Defining Attributes:: Specifying attributes and their values.
3368 * Expressions::         Valid expressions for attribute values.
3369 * Tagging Insns::       Assigning attribute values to insns.
3370 * Attr Example::        An example of assigning attributes.
3371 * Insn Lengths::        Computing the length of insns.
3372 * Constant Attributes:: Defining attributes that are constant.
3373 * Delay Slots::         Defining delay slots required for a machine.
3374 * Function Units::      Specifying information for insn scheduling.
3375 @end menu
3377 @node Defining Attributes
3378 @subsection Defining Attributes and their Values
3379 @cindex defining attributes and their values
3380 @cindex attributes, defining
3382 @findex define_attr
3383 The @code{define_attr} expression is used to define each attribute required
3384 by the target machine.  It looks like:
3386 @smallexample
3387 (define_attr @var{name} @var{list-of-values} @var{default})
3388 @end smallexample
3390 @var{name} is a string specifying the name of the attribute being defined.
3392 @var{list-of-values} is either a string that specifies a comma-separated
3393 list of values that can be assigned to the attribute, or a null string to
3394 indicate that the attribute takes numeric values.
3396 @var{default} is an attribute expression that gives the value of this
3397 attribute for insns that match patterns whose definition does not include
3398 an explicit value for this attribute.  @xref{Attr Example}, for more
3399 information on the handling of defaults.  @xref{Constant Attributes},
3400 for information on attributes that do not depend on any particular insn.
3402 @findex insn-attr.h
3403 For each defined attribute, a number of definitions are written to the
3404 @file{insn-attr.h} file.  For cases where an explicit set of values is
3405 specified for an attribute, the following are defined:
3407 @itemize @bullet
3408 @item
3409 A @samp{#define} is written for the symbol @samp{HAVE_ATTR_@var{name}}.
3411 @item
3412 An enumeral class is defined for @samp{attr_@var{name}} with
3413 elements of the form @samp{@var{upper-name}_@var{upper-value}} where
3414 the attribute name and value are first converted to upper case.
3416 @item
3417 A function @samp{get_attr_@var{name}} is defined that is passed an insn and
3418 returns the attribute value for that insn.
3419 @end itemize
3421 For example, if the following is present in the @file{md} file:
3423 @smallexample
3424 (define_attr "type" "branch,fp,load,store,arith" @dots{})
3425 @end smallexample
3427 @noindent
3428 the following lines will be written to the file @file{insn-attr.h}.
3430 @smallexample
3431 #define HAVE_ATTR_type
3432 enum attr_type @{TYPE_BRANCH, TYPE_FP, TYPE_LOAD,
3433                  TYPE_STORE, TYPE_ARITH@};
3434 extern enum attr_type get_attr_type ();
3435 @end smallexample
3437 If the attribute takes numeric values, no @code{enum} type will be
3438 defined and the function to obtain the attribute's value will return
3439 @code{int}.
3441 @node Expressions
3442 @subsection Attribute Expressions
3443 @cindex attribute expressions
3445 RTL expressions used to define attributes use the codes described above
3446 plus a few specific to attribute definitions, to be discussed below.
3447 Attribute value expressions must have one of the following forms:
3449 @table @code
3450 @cindex @code{const_int} and attributes
3451 @item (const_int @var{i})
3452 The integer @var{i} specifies the value of a numeric attribute.  @var{i}
3453 must be non-negative.
3455 The value of a numeric attribute can be specified either with a
3456 @code{const_int} or as an integer represented as a string in
3457 @code{const_string}, @code{eq_attr} (see below), and @code{set_attr}
3458 (@pxref{Tagging Insns}) expressions.
3460 @cindex @code{const_string} and attributes
3461 @item (const_string @var{value})
3462 The string @var{value} specifies a constant attribute value.
3463 If @var{value} is specified as @samp{"*"}, it means that the default value of
3464 the attribute is to be used for the insn containing this expression.
3465 @samp{"*"} obviously cannot be used in the @var{default} expression
3466 of a @code{define_attr}.@refill
3468 If the attribute whose value is being specified is numeric, @var{value}
3469 must be a string containing a non-negative integer (normally
3470 @code{const_int} would be used in this case).  Otherwise, it must
3471 contain one of the valid values for the attribute.
3473 @cindex @code{if_then_else} and attributes
3474 @item (if_then_else @var{test} @var{true-value} @var{false-value})
3475 @var{test} specifies an attribute test, whose format is defined below.
3476 The value of this expression is @var{true-value} if @var{test} is true,
3477 otherwise it is @var{false-value}.
3479 @cindex @code{cond} and attributes
3480 @item (cond [@var{test1} @var{value1} @dots{}] @var{default})
3481 The first operand of this expression is a vector containing an even
3482 number of expressions and consisting of pairs of @var{test} and @var{value}
3483 expressions.  The value of the @code{cond} expression is that of the
3484 @var{value} corresponding to the first true @var{test} expression.  If
3485 none of the @var{test} expressions are true, the value of the @code{cond}
3486 expression is that of the @var{default} expression.
3487 @end table
3489 @var{test} expressions can have one of the following forms:
3491 @table @code
3492 @cindex @code{const_int} and attribute tests
3493 @item (const_int @var{i})
3494 This test is true if @var{i} is non-zero and false otherwise.
3496 @cindex @code{not} and attributes
3497 @cindex @code{ior} and attributes
3498 @cindex @code{and} and attributes
3499 @item (not @var{test})
3500 @itemx (ior @var{test1} @var{test2})
3501 @itemx (and @var{test1} @var{test2})
3502 These tests are true if the indicated logical function is true.
3504 @cindex @code{match_operand} and attributes
3505 @item (match_operand:@var{m} @var{n} @var{pred} @var{constraints})
3506 This test is true if operand @var{n} of the insn whose attribute value
3507 is being determined has mode @var{m} (this part of the test is ignored
3508 if @var{m} is @code{VOIDmode}) and the function specified by the string
3509 @var{pred} returns a non-zero value when passed operand @var{n} and mode
3510 @var{m} (this part of the test is ignored if @var{pred} is the null
3511 string).
3513 The @var{constraints} operand is ignored and should be the null string.
3515 @cindex @code{le} and attributes
3516 @cindex @code{leu} and attributes
3517 @cindex @code{lt} and attributes
3518 @cindex @code{gt} and attributes
3519 @cindex @code{gtu} and attributes
3520 @cindex @code{ge} and attributes
3521 @cindex @code{geu} and attributes
3522 @cindex @code{ne} and attributes
3523 @cindex @code{eq} and attributes
3524 @cindex @code{plus} and attributes
3525 @cindex @code{minus} and attributes
3526 @cindex @code{mult} and attributes
3527 @cindex @code{div} and attributes
3528 @cindex @code{mod} and attributes
3529 @cindex @code{abs} and attributes
3530 @cindex @code{neg} and attributes
3531 @cindex @code{ashift} and attributes
3532 @cindex @code{lshiftrt} and attributes
3533 @cindex @code{ashiftrt} and attributes
3534 @item (le @var{arith1} @var{arith2})
3535 @itemx (leu @var{arith1} @var{arith2})
3536 @itemx (lt @var{arith1} @var{arith2})
3537 @itemx (ltu @var{arith1} @var{arith2})
3538 @itemx (gt @var{arith1} @var{arith2})
3539 @itemx (gtu @var{arith1} @var{arith2})
3540 @itemx (ge @var{arith1} @var{arith2})
3541 @itemx (geu @var{arith1} @var{arith2})
3542 @itemx (ne @var{arith1} @var{arith2})
3543 @itemx (eq @var{arith1} @var{arith2})
3544 These tests are true if the indicated comparison of the two arithmetic
3545 expressions is true.  Arithmetic expressions are formed with
3546 @code{plus}, @code{minus}, @code{mult}, @code{div}, @code{mod},
3547 @code{abs}, @code{neg}, @code{and}, @code{ior}, @code{xor}, @code{not},
3548 @code{ashift}, @code{lshiftrt}, and @code{ashiftrt} expressions.@refill
3550 @findex get_attr
3551 @code{const_int} and @code{symbol_ref} are always valid terms (@pxref{Insn
3552 Lengths},for additional forms).  @code{symbol_ref} is a string
3553 denoting a C expression that yields an @code{int} when evaluated by the
3554 @samp{get_attr_@dots{}} routine.  It should normally be a global
3555 variable.@refill
3557 @findex eq_attr
3558 @item (eq_attr @var{name} @var{value})
3559 @var{name} is a string specifying the name of an attribute.
3561 @var{value} is a string that is either a valid value for attribute
3562 @var{name}, a comma-separated list of values, or @samp{!} followed by a
3563 value or list.  If @var{value} does not begin with a @samp{!}, this
3564 test is true if the value of the @var{name} attribute of the current
3565 insn is in the list specified by @var{value}.  If @var{value} begins
3566 with a @samp{!}, this test is true if the attribute's value is
3567 @emph{not} in the specified list.
3569 For example,
3571 @smallexample
3572 (eq_attr "type" "load,store")
3573 @end smallexample
3575 @noindent
3576 is equivalent to
3578 @smallexample
3579 (ior (eq_attr "type" "load") (eq_attr "type" "store"))
3580 @end smallexample
3582 If @var{name} specifies an attribute of @samp{alternative}, it refers to the
3583 value of the compiler variable @code{which_alternative}
3584 (@pxref{Output Statement}) and the values must be small integers.  For
3585 example,@refill
3587 @smallexample
3588 (eq_attr "alternative" "2,3")
3589 @end smallexample
3591 @noindent
3592 is equivalent to
3594 @smallexample
3595 (ior (eq (symbol_ref "which_alternative") (const_int 2))
3596      (eq (symbol_ref "which_alternative") (const_int 3)))
3597 @end smallexample
3599 Note that, for most attributes, an @code{eq_attr} test is simplified in cases
3600 where the value of the attribute being tested is known for all insns matching
3601 a particular pattern.  This is by far the most common case.@refill
3603 @findex attr_flag
3604 @item (attr_flag @var{name})
3605 The value of an @code{attr_flag} expression is true if the flag
3606 specified by @var{name} is true for the @code{insn} currently being
3607 scheduled.
3609 @var{name} is a string specifying one of a fixed set of flags to test.
3610 Test the flags @code{forward} and @code{backward} to determine the
3611 direction of a conditional branch.  Test the flags @code{very_likely},
3612 @code{likely}, @code{very_unlikely}, and @code{unlikely} to determine
3613 if a conditional branch is expected to be taken.
3615 If the @code{very_likely} flag is true, then the @code{likely} flag is also
3616 true.  Likewise for the @code{very_unlikely} and @code{unlikely} flags.
3618 This example describes a conditional branch delay slot which
3619 can be nullified for forward branches that are taken (annul-true) or
3620 for backward branches which are not taken (annul-false).
3622 @smallexample
3623 (define_delay (eq_attr "type" "cbranch")
3624   [(eq_attr "in_branch_delay" "true")
3625    (and (eq_attr "in_branch_delay" "true")
3626         (attr_flag "forward"))
3627    (and (eq_attr "in_branch_delay" "true")
3628         (attr_flag "backward"))])
3629 @end smallexample
3631 The @code{forward} and @code{backward} flags are false if the current
3632 @code{insn} being scheduled is not a conditional branch.
3634 The @code{very_likely} and @code{likely} flags are true if the
3635 @code{insn} being scheduled is not a conditional branch.
3636 The @code{very_unlikely} and @code{unlikely} flags are false if the
3637 @code{insn} being scheduled is not a conditional branch.
3639 @code{attr_flag} is only used during delay slot scheduling and has no
3640 meaning to other passes of the compiler.
3641 @end table
3643 @node Tagging Insns
3644 @subsection Assigning Attribute Values to Insns
3645 @cindex tagging insns
3646 @cindex assigning attribute values to insns
3648 The value assigned to an attribute of an insn is primarily determined by
3649 which pattern is matched by that insn (or which @code{define_peephole}
3650 generated it).  Every @code{define_insn} and @code{define_peephole} can
3651 have an optional last argument to specify the values of attributes for
3652 matching insns.  The value of any attribute not specified in a particular
3653 insn is set to the default value for that attribute, as specified in its
3654 @code{define_attr}.  Extensive use of default values for attributes
3655 permits the specification of the values for only one or two attributes
3656 in the definition of most insn patterns, as seen in the example in the
3657 next section.@refill
3659 The optional last argument of @code{define_insn} and
3660 @code{define_peephole} is a vector of expressions, each of which defines
3661 the value for a single attribute.  The most general way of assigning an
3662 attribute's value is to use a @code{set} expression whose first operand is an
3663 @code{attr} expression giving the name of the attribute being set.  The
3664 second operand of the @code{set} is an attribute expression
3665 (@pxref{Expressions}) giving the value of the attribute.@refill
3667 When the attribute value depends on the @samp{alternative} attribute
3668 (i.e., which is the applicable alternative in the constraint of the
3669 insn), the @code{set_attr_alternative} expression can be used.  It
3670 allows the specification of a vector of attribute expressions, one for
3671 each alternative.
3673 @findex set_attr
3674 When the generality of arbitrary attribute expressions is not required,
3675 the simpler @code{set_attr} expression can be used, which allows
3676 specifying a string giving either a single attribute value or a list
3677 of attribute values, one for each alternative.
3679 The form of each of the above specifications is shown below.  In each case,
3680 @var{name} is a string specifying the attribute to be set.
3682 @table @code
3683 @item (set_attr @var{name} @var{value-string})
3684 @var{value-string} is either a string giving the desired attribute value,
3685 or a string containing a comma-separated list giving the values for
3686 succeeding alternatives.  The number of elements must match the number
3687 of alternatives in the constraint of the insn pattern.
3689 Note that it may be useful to specify @samp{*} for some alternative, in
3690 which case the attribute will assume its default value for insns matching
3691 that alternative.
3693 @findex set_attr_alternative
3694 @item (set_attr_alternative @var{name} [@var{value1} @var{value2} @dots{}])
3695 Depending on the alternative of the insn, the value will be one of the
3696 specified values.  This is a shorthand for using a @code{cond} with
3697 tests on the @samp{alternative} attribute.
3699 @findex attr
3700 @item (set (attr @var{name}) @var{value})
3701 The first operand of this @code{set} must be the special RTL expression
3702 @code{attr}, whose sole operand is a string giving the name of the
3703 attribute being set.  @var{value} is the value of the attribute.
3704 @end table
3706 The following shows three different ways of representing the same
3707 attribute value specification:
3709 @smallexample
3710 (set_attr "type" "load,store,arith")
3712 (set_attr_alternative "type"
3713                       [(const_string "load") (const_string "store")
3714                        (const_string "arith")])
3716 (set (attr "type")
3717      (cond [(eq_attr "alternative" "1") (const_string "load")
3718             (eq_attr "alternative" "2") (const_string "store")]
3719            (const_string "arith")))
3720 @end smallexample
3722 @need 1000
3723 @findex define_asm_attributes
3724 The @code{define_asm_attributes} expression provides a mechanism to
3725 specify the attributes assigned to insns produced from an @code{asm}
3726 statement.  It has the form:
3728 @smallexample
3729 (define_asm_attributes [@var{attr-sets}])
3730 @end smallexample
3732 @noindent
3733 where @var{attr-sets} is specified the same as for both the
3734 @code{define_insn} and the @code{define_peephole} expressions.
3736 These values will typically be the ``worst case'' attribute values.  For
3737 example, they might indicate that the condition code will be clobbered.
3739 A specification for a @code{length} attribute is handled specially.  The
3740 way to compute the length of an @code{asm} insn is to multiply the
3741 length specified in the expression @code{define_asm_attributes} by the
3742 number of machine instructions specified in the @code{asm} statement,
3743 determined by counting the number of semicolons and newlines in the
3744 string.  Therefore, the value of the @code{length} attribute specified
3745 in a @code{define_asm_attributes} should be the maximum possible length
3746 of a single machine instruction.
3748 @node Attr Example
3749 @subsection Example of Attribute Specifications
3750 @cindex attribute specifications example
3751 @cindex attribute specifications
3753 The judicious use of defaulting is important in the efficient use of
3754 insn attributes.  Typically, insns are divided into @dfn{types} and an
3755 attribute, customarily called @code{type}, is used to represent this
3756 value.  This attribute is normally used only to define the default value
3757 for other attributes.  An example will clarify this usage.
3759 Assume we have a RISC machine with a condition code and in which only
3760 full-word operations are performed in registers.  Let us assume that we
3761 can divide all insns into loads, stores, (integer) arithmetic
3762 operations, floating point operations, and branches.
3764 Here we will concern ourselves with determining the effect of an insn on
3765 the condition code and will limit ourselves to the following possible
3766 effects:  The condition code can be set unpredictably (clobbered), not
3767 be changed, be set to agree with the results of the operation, or only
3768 changed if the item previously set into the condition code has been
3769 modified.
3771 Here is part of a sample @file{md} file for such a machine:
3773 @smallexample
3774 (define_attr "type" "load,store,arith,fp,branch" (const_string "arith"))
3776 (define_attr "cc" "clobber,unchanged,set,change0"
3777              (cond [(eq_attr "type" "load")
3778                         (const_string "change0")
3779                     (eq_attr "type" "store,branch")
3780                         (const_string "unchanged")
3781                     (eq_attr "type" "arith")
3782                         (if_then_else (match_operand:SI 0 "" "")
3783                                       (const_string "set")
3784                                       (const_string "clobber"))]
3785                    (const_string "clobber")))
3787 (define_insn ""
3788   [(set (match_operand:SI 0 "general_operand" "=r,r,m")
3789         (match_operand:SI 1 "general_operand" "r,m,r"))]
3790   ""
3791   "@@
3792    move %0,%1
3793    load %0,%1
3794    store %0,%1"
3795   [(set_attr "type" "arith,load,store")])
3796 @end smallexample
3798 Note that we assume in the above example that arithmetic operations
3799 performed on quantities smaller than a machine word clobber the condition
3800 code since they will set the condition code to a value corresponding to the
3801 full-word result.
3803 @node Insn Lengths
3804 @subsection Computing the Length of an Insn
3805 @cindex insn lengths, computing
3806 @cindex computing the length of an insn
3808 For many machines, multiple types of branch instructions are provided, each
3809 for different length branch displacements.  In most cases, the assembler
3810 will choose the correct instruction to use.  However, when the assembler
3811 cannot do so, GCC can when a special attribute, the @samp{length}
3812 attribute, is defined.  This attribute must be defined to have numeric
3813 values by specifying a null string in its @code{define_attr}.
3815 In the case of the @samp{length} attribute, two additional forms of
3816 arithmetic terms are allowed in test expressions:
3818 @table @code
3819 @cindex @code{match_dup} and attributes
3820 @item (match_dup @var{n})
3821 This refers to the address of operand @var{n} of the current insn, which
3822 must be a @code{label_ref}.
3824 @cindex @code{pc} and attributes
3825 @item (pc)
3826 This refers to the address of the @emph{current} insn.  It might have
3827 been more consistent with other usage to make this the address of the
3828 @emph{next} insn but this would be confusing because the length of the
3829 current insn is to be computed.
3830 @end table
3832 @cindex @code{addr_vec}, length of
3833 @cindex @code{addr_diff_vec}, length of
3834 For normal insns, the length will be determined by value of the
3835 @samp{length} attribute.  In the case of @code{addr_vec} and
3836 @code{addr_diff_vec} insn patterns, the length is computed as
3837 the number of vectors multiplied by the size of each vector.
3839 Lengths are measured in addressable storage units (bytes).
3841 The following macros can be used to refine the length computation:
3843 @table @code
3844 @findex FIRST_INSN_ADDRESS
3845 @item FIRST_INSN_ADDRESS
3846 When the @code{length} insn attribute is used, this macro specifies the
3847 value to be assigned to the address of the first insn in a function.  If
3848 not specified, 0 is used.
3850 @findex ADJUST_INSN_LENGTH
3851 @item ADJUST_INSN_LENGTH (@var{insn}, @var{length})
3852 If defined, modifies the length assigned to instruction @var{insn} as a
3853 function of the context in which it is used.  @var{length} is an lvalue
3854 that contains the initially computed length of the insn and should be
3855 updated with the correct length of the insn.  If updating is required,
3856 @var{insn} must not be a varying-length insn.
3858 This macro will normally not be required.  A case in which it is
3859 required is the ROMP.  On this machine, the size of an @code{addr_vec}
3860 insn must be increased by two to compensate for the fact that alignment
3861 may be required.
3862 @end table
3864 @findex get_attr_length
3865 The routine that returns @code{get_attr_length} (the value of the
3866 @code{length} attribute) can be used by the output routine to
3867 determine the form of the branch instruction to be written, as the
3868 example below illustrates.
3870 As an example of the specification of variable-length branches, consider
3871 the IBM 360.  If we adopt the convention that a register will be set to
3872 the starting address of a function, we can jump to labels within 4k of
3873 the start using a four-byte instruction.  Otherwise, we need a six-byte
3874 sequence to load the address from memory and then branch to it.
3876 On such a machine, a pattern for a branch instruction might be specified
3877 as follows:
3879 @smallexample
3880 (define_insn "jump"
3881   [(set (pc)
3882         (label_ref (match_operand 0 "" "")))]
3883   ""
3884   "*
3886    return (get_attr_length (insn) == 4
3887            ? \"b %l0\" : \"l r15,=a(%l0); br r15\");
3889   [(set (attr "length") (if_then_else (lt (match_dup 0) (const_int 4096))
3890                                       (const_int 4)
3891                                       (const_int 6)))])
3892 @end smallexample
3894 @node Constant Attributes
3895 @subsection Constant Attributes
3896 @cindex constant attributes
3898 A special form of @code{define_attr}, where the expression for the
3899 default value is a @code{const} expression, indicates an attribute that
3900 is constant for a given run of the compiler.  Constant attributes may be
3901 used to specify which variety of processor is used.  For example,
3903 @smallexample
3904 (define_attr "cpu" "m88100,m88110,m88000"
3905  (const
3906   (cond [(symbol_ref "TARGET_88100") (const_string "m88100")
3907          (symbol_ref "TARGET_88110") (const_string "m88110")]
3908         (const_string "m88000"))))
3910 (define_attr "memory" "fast,slow"
3911  (const
3912   (if_then_else (symbol_ref "TARGET_FAST_MEM")
3913                 (const_string "fast")
3914                 (const_string "slow"))))
3915 @end smallexample
3917 The routine generated for constant attributes has no parameters as it
3918 does not depend on any particular insn.  RTL expressions used to define
3919 the value of a constant attribute may use the @code{symbol_ref} form,
3920 but may not use either the @code{match_operand} form or @code{eq_attr}
3921 forms involving insn attributes.
3923 @node Delay Slots
3924 @subsection Delay Slot Scheduling
3925 @cindex delay slots, defining
3927 The insn attribute mechanism can be used to specify the requirements for
3928 delay slots, if any, on a target machine.  An instruction is said to
3929 require a @dfn{delay slot} if some instructions that are physically
3930 after the instruction are executed as if they were located before it.
3931 Classic examples are branch and call instructions, which often execute
3932 the following instruction before the branch or call is performed.
3934 On some machines, conditional branch instructions can optionally
3935 @dfn{annul} instructions in the delay slot.  This means that the
3936 instruction will not be executed for certain branch outcomes.  Both
3937 instructions that annul if the branch is true and instructions that
3938 annul if the branch is false are supported.
3940 Delay slot scheduling differs from instruction scheduling in that
3941 determining whether an instruction needs a delay slot is dependent only
3942 on the type of instruction being generated, not on data flow between the
3943 instructions.  See the next section for a discussion of data-dependent
3944 instruction scheduling.
3946 @findex define_delay
3947 The requirement of an insn needing one or more delay slots is indicated
3948 via the @code{define_delay} expression.  It has the following form:
3950 @smallexample
3951 (define_delay @var{test}
3952               [@var{delay-1} @var{annul-true-1} @var{annul-false-1}
3953                @var{delay-2} @var{annul-true-2} @var{annul-false-2}
3954                @dots{}])
3955 @end smallexample
3957 @var{test} is an attribute test that indicates whether this
3958 @code{define_delay} applies to a particular insn.  If so, the number of
3959 required delay slots is determined by the length of the vector specified
3960 as the second argument.  An insn placed in delay slot @var{n} must
3961 satisfy attribute test @var{delay-n}.  @var{annul-true-n} is an
3962 attribute test that specifies which insns may be annulled if the branch
3963 is true.  Similarly, @var{annul-false-n} specifies which insns in the
3964 delay slot may be annulled if the branch is false.  If annulling is not
3965 supported for that delay slot, @code{(nil)} should be coded.@refill
3967 For example, in the common case where branch and call insns require
3968 a single delay slot, which may contain any insn other than a branch or
3969 call, the following would be placed in the @file{md} file:
3971 @smallexample
3972 (define_delay (eq_attr "type" "branch,call")
3973               [(eq_attr "type" "!branch,call") (nil) (nil)])
3974 @end smallexample
3976 Multiple @code{define_delay} expressions may be specified.  In this
3977 case, each such expression specifies different delay slot requirements
3978 and there must be no insn for which tests in two @code{define_delay}
3979 expressions are both true.
3981 For example, if we have a machine that requires one delay slot for branches
3982 but two for calls,  no delay slot can contain a branch or call insn,
3983 and any valid insn in the delay slot for the branch can be annulled if the
3984 branch is true, we might represent this as follows:
3986 @smallexample
3987 (define_delay (eq_attr "type" "branch")
3988    [(eq_attr "type" "!branch,call")
3989     (eq_attr "type" "!branch,call")
3990     (nil)])
3992 (define_delay (eq_attr "type" "call")
3993               [(eq_attr "type" "!branch,call") (nil) (nil)
3994                (eq_attr "type" "!branch,call") (nil) (nil)])
3995 @end smallexample
3996 @c the above is *still* too long.  --mew 4feb93
3998 @node Function Units
3999 @subsection Specifying Function Units
4000 @cindex function units, for scheduling
4002 On most RISC machines, there are instructions whose results are not
4003 available for a specific number of cycles.  Common cases are instructions
4004 that load data from memory.  On many machines, a pipeline stall will result
4005 if the data is referenced too soon after the load instruction.
4007 In addition, many newer microprocessors have multiple function units, usually
4008 one for integer and one for floating point, and often will incur pipeline
4009 stalls when a result that is needed is not yet ready.
4011 The descriptions in this section allow the specification of how much
4012 time must elapse between the execution of an instruction and the time
4013 when its result is used.  It also allows specification of when the
4014 execution of an instruction will delay execution of similar instructions
4015 due to function unit conflicts.
4017 For the purposes of the specifications in this section, a machine is
4018 divided into @dfn{function units}, each of which execute a specific
4019 class of instructions in first-in-first-out order.  Function units that
4020 accept one instruction each cycle and allow a result to be used in the
4021 succeeding instruction (usually via forwarding) need not be specified.
4022 Classic RISC microprocessors will normally have a single function unit,
4023 which we can call @samp{memory}.  The newer ``superscalar'' processors
4024 will often have function units for floating point operations, usually at
4025 least a floating point adder and multiplier.
4027 @findex define_function_unit
4028 Each usage of a function units by a class of insns is specified with a
4029 @code{define_function_unit} expression, which looks like this:
4031 @smallexample
4032 (define_function_unit @var{name} @var{multiplicity} @var{simultaneity}
4033                       @var{test} @var{ready-delay} @var{issue-delay}
4034                      [@var{conflict-list}])
4035 @end smallexample
4037 @var{name} is a string giving the name of the function unit.
4039 @var{multiplicity} is an integer specifying the number of identical
4040 units in the processor.  If more than one unit is specified, they will
4041 be scheduled independently.  Only truly independent units should be
4042 counted; a pipelined unit should be specified as a single unit.  (The
4043 only common example of a machine that has multiple function units for a
4044 single instruction class that are truly independent and not pipelined
4045 are the two multiply and two increment units of the CDC 6600.)
4047 @var{simultaneity} specifies the maximum number of insns that can be
4048 executing in each instance of the function unit simultaneously or zero
4049 if the unit is pipelined and has no limit.
4051 All @code{define_function_unit} definitions referring to function unit
4052 @var{name} must have the same name and values for @var{multiplicity} and
4053 @var{simultaneity}.
4055 @var{test} is an attribute test that selects the insns we are describing
4056 in this definition.  Note that an insn may use more than one function
4057 unit and a function unit may be specified in more than one
4058 @code{define_function_unit}.
4060 @var{ready-delay} is an integer that specifies the number of cycles
4061 after which the result of the instruction can be used without
4062 introducing any stalls.
4064 @var{issue-delay} is an integer that specifies the number of cycles
4065 after the instruction matching the @var{test} expression begins using
4066 this unit until a subsequent instruction can begin.  A cost of @var{N}
4067 indicates an @var{N-1} cycle delay.  A subsequent instruction may also
4068 be delayed if an earlier instruction has a longer @var{ready-delay}
4069 value.  This blocking effect is computed using the @var{simultaneity},
4070 @var{ready-delay}, @var{issue-delay}, and @var{conflict-list} terms.
4071 For a normal non-pipelined function unit, @var{simultaneity} is one, the
4072 unit is taken to block for the @var{ready-delay} cycles of the executing
4073 insn, and smaller values of @var{issue-delay} are ignored.
4075 @var{conflict-list} is an optional list giving detailed conflict costs
4076 for this unit.  If specified, it is a list of condition test expressions
4077 to be applied to insns chosen to execute in @var{name} following the
4078 particular insn matching @var{test} that is already executing in
4079 @var{name}.  For each insn in the list, @var{issue-delay} specifies the
4080 conflict cost; for insns not in the list, the cost is zero.  If not
4081 specified, @var{conflict-list} defaults to all instructions that use the
4082 function unit.
4084 Typical uses of this vector are where a floating point function unit can
4085 pipeline either single- or double-precision operations, but not both, or
4086 where a memory unit can pipeline loads, but not stores, etc.
4088 As an example, consider a classic RISC machine where the result of a
4089 load instruction is not available for two cycles (a single ``delay''
4090 instruction is required) and where only one load instruction can be executed
4091 simultaneously.  This would be specified as:
4093 @smallexample
4094 (define_function_unit "memory" 1 1 (eq_attr "type" "load") 2 0)
4095 @end smallexample
4097 For the case of a floating point function unit that can pipeline either
4098 single or double precision, but not both, the following could be specified:
4100 @smallexample
4101 (define_function_unit
4102    "fp" 1 0 (eq_attr "type" "sp_fp") 4 4 [(eq_attr "type" "dp_fp")])
4103 (define_function_unit
4104    "fp" 1 0 (eq_attr "type" "dp_fp") 4 4 [(eq_attr "type" "sp_fp")])
4105 @end smallexample
4107 @strong{Note:} The scheduler attempts to avoid function unit conflicts
4108 and uses all the specifications in the @code{define_function_unit}
4109 expression.  It has recently come to our attention that these
4110 specifications may not allow modeling of some of the newer
4111 ``superscalar'' processors that have insns using multiple pipelined
4112 units.  These insns will cause a potential conflict for the second unit
4113 used during their execution and there is no way of representing that
4114 conflict.  We welcome any examples of how function unit conflicts work
4115 in such processors and suggestions for their representation.
4116 @end ifset