2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
6 .. Most of this node appears by itself (in a different place) even
7 when the INTERNALS flag is clear. Passages that require the internals
8 manual's context are conditionalized to appear only in the internals manual.
10 .. index:: operand constraints, constraints
17 Each ``match_operand`` in an instruction pattern can specify
18 constraints for the operands allowed. The constraints allow you to
19 fine-tune matching within the set of operands allowed by the
22 Constraints can say whether
23 an operand may be in a register, and which kinds of register; whether the
24 operand can be a memory reference, and which kinds of address; whether the
25 operand may be an immediate constant, and which possible values it may
26 have. Constraints can also require two operands to match.
27 Side-effects aren't allowed in operands of inline ``asm``, unless
28 :samp:`<` or :samp:`>` constraints are used, because there is no guarantee
29 that the side effects will happen exactly once in an instruction that can update
30 the addressing register.
36 .. include:: ../../../../doc/md.rst
41 .. _disable-insn-alternatives:
43 Disable insn alternatives using the enabled attribute
44 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46 There are three insn attributes that may be used to selectively disable
47 instruction alternatives:
50 Says whether an alternative is available on the current subtarget.
52 ``preferred_for_size``
53 Says whether an enabled alternative should be used in code that is
56 ``preferred_for_speed``
57 Says whether an enabled alternative should be used in code that is
60 All these attributes should use ``(const_int 1)`` to allow an alternative
61 or ``(const_int 0)`` to disallow it. The attributes must be a static
62 property of the subtarget; they cannot for example depend on the
63 current operands, on the current optimization level, on the location
64 of the insn within the body of a loop, on whether register allocation
65 has finished, or on the current compiler pass.
67 The ``enabled`` attribute is a correctness property. It tells GCC to act
68 as though the disabled alternatives were never defined in the first place.
69 This is useful when adding new instructions to an existing pattern in
70 cases where the new instructions are only available for certain cpu
71 architecture levels (typically mapped to the ``-march=`` command-line
74 In contrast, the ``preferred_for_size`` and ``preferred_for_speed``
75 attributes are strong optimization hints rather than correctness properties.
76 ``preferred_for_size`` tells GCC which alternatives to consider when
77 adding or modifying an instruction that GCC wants to optimize for size.
78 ``preferred_for_speed`` does the same thing for speed. Note that things
79 like code motion can lead to cases where code optimized for size uses
80 alternatives that are not preferred for size, and similarly for speed.
82 Although ``define_insn`` s can in principle specify the ``enabled``
83 attribute directly, it is often clearer to have subsiduary attributes
84 for each architectural feature of interest. The ``define_insn`` s
85 can then use these subsiduary attributes to say which alternatives
86 require which features. The example below does this for ``cpu_facility``.
88 E.g. the following two patterns could easily be merged using the ``enabled``
93 (define_insn "*movdi_old"
94 [(set (match_operand:DI 0 "register_operand" "=d")
95 (match_operand:DI 1 "register_operand" " d"))]
99 (define_insn "*movdi_new"
100 [(set (match_operand:DI 0 "register_operand" "=d,f,d")
101 (match_operand:DI 1 "register_operand" " d,d,f"))]
112 (define_insn "*movdi_combined"
113 [(set (match_operand:DI 0 "register_operand" "=d,f,d")
114 (match_operand:DI 1 "register_operand" " d,d,f"))]
120 [(set_attr "cpu_facility" "*,new,new")])
122 with the ``enabled`` attribute defined like this:
126 (define_attr "cpu_facility" "standard,new" (const_string "standard"))
128 (define_attr "enabled" ""
129 (cond [(eq_attr "cpu_facility" "standard") (const_int 1)
130 (and (eq_attr "cpu_facility" "new")
131 (ne (symbol_ref "TARGET_NEW") (const_int 0)))
135 .. index:: defining constraints, constraints, defining
137 .. _define-constraints:
139 Defining Machine-Specific Constraints
140 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
142 Machine-specific constraints fall into two categories: register and
143 non-register constraints. Within the latter category, constraints
144 which allow subsets of all possible memory or address operands should
145 be specially marked, to give ``reload`` more information.
147 Machine-specific constraints can be given names of arbitrary length,
148 but they must be entirely composed of letters, digits, underscores
149 (:samp:`_`), and angle brackets (:samp:`< >`). Like C identifiers, they
150 must begin with a letter or underscore.
152 In order to avoid ambiguity in operand constraint strings, no
153 constraint can have a name that begins with any other constraint's
154 name. For example, if ``x`` is defined as a constraint name,
155 ``xy`` may not be, and vice versa. As a consequence of this rule,
156 no constraint may begin with one of the generic constraint letters:
157 :samp:`E F V X g i m n o p r s`.
159 Register constraints correspond directly to register classes.
160 See :ref:`register-classes`. There is thus not much flexibility in their
163 .. index:: define_register_constraint
165 MD Expression define_register_constraint name regclass docstringAll three arguments are string constants.
166 :samp:`{name}` is the name of the constraint, as it will appear in
167 ``match_operand`` expressions. If :samp:`{name}` is a multi-letter
168 constraint its length shall be the same for all constraints starting
169 with the same letter. :samp:`{regclass}` can be either the
170 name of the corresponding register class (see :ref:`register-classes`),
171 or a C expression which evaluates to the appropriate register class.
172 If it is an expression, it must have no side effects, and it cannot
173 look at the operand. The usual use of expressions is to map some
174 register constraints to ``NO_REGS`` when the register class
175 is not available on a given subarchitecture.
177 :samp:`{docstring}` is a sentence documenting the meaning of the
178 constraint. Docstrings are explained further below.
180 Non-register constraints are more like predicates: the constraint
181 definition gives a boolean expression which indicates whether the
184 .. index:: define_constraint
186 MD Expression define_constraint name docstring expThe :samp:`{name}` and :samp:`{docstring}` arguments are the same as for
187 ``define_register_constraint``, but note that the docstring comes
188 immediately after the name for these expressions. :samp:`{exp}` is an RTL
189 expression, obeying the same rules as the RTL expressions in predicate
190 definitions. See :ref:`defining-predicates`, for details. If it
191 evaluates true, the constraint matches; if it evaluates false, it
192 doesn't. Constraint expressions should indicate which RTL codes they
193 might match, just like predicate expressions.
195 ``match_test`` C expressions have access to the
199 The RTL object defining the operand.
202 The machine mode of :samp:`{op}`.
205 :samp:`INTVAL ({op})`, if :samp:`{op}` is a ``const_int``.
208 :samp:`CONST_DOUBLE_HIGH ({op})`, if :samp:`{op}` is an integer
212 :samp:`CONST_DOUBLE_LOW ({op})`, if :samp:`{op}` is an integer
216 :samp:`CONST_DOUBLE_REAL_VALUE ({op})`, if :samp:`{op}` is a floating-point
219 The :samp:`{*val}` variables should only be used once another piece of the
220 expression has verified that :samp:`{op}` is the appropriate kind of RTL
223 Most non-register constraints should be defined with
224 ``define_constraint``. The remaining two definition expressions
225 are only appropriate for constraints that should be handled specially
226 by ``reload`` if they fail to match.
228 .. index:: define_memory_constraint
230 MD Expression define_memory_constraint name docstring expUse this expression for constraints that match a subset of all memory
231 operands: that is, ``reload`` can make them match by converting the
232 operand to the form :samp:`(mem (reg {X} ))`, where :samp:`{X}` is a
233 base register (from the register class specified by
234 ``BASE_REG_CLASS``, see :ref:`register-classes`).
236 For example, on the S/390, some instructions do not accept arbitrary
237 memory references, but only those that do not make use of an index
238 register. The constraint letter :samp:`Q` is defined to represent a
239 memory address of this type. If :samp:`Q` is defined with
240 ``define_memory_constraint``, a :samp:`Q` constraint can handle any
241 memory operand, because ``reload`` knows it can simply copy the
242 memory address into a base register if required. This is analogous to
243 the way an :samp:`o` constraint can handle any memory operand.
245 The syntax and semantics are otherwise identical to
246 ``define_constraint``.
248 .. index:: define_special_memory_constraint
250 MD Expression define_special_memory_constraint name docstring expUse this expression for constraints that match a subset of all memory
251 operands: that is, ``reload`` cannot make them match by reloading
252 the address as it is described for ``define_memory_constraint`` or
253 such address reload is undesirable with the performance point of view.
255 For example, ``define_special_memory_constraint`` can be useful if
256 specifically aligned memory is necessary or desirable for some insn
259 The syntax and semantics are otherwise identical to
260 ``define_memory_constraint``.
262 .. index:: define_relaxed_memory_constraint
264 MD Expression define_relaxed_memory_constraint name docstring expThe test expression in a ``define_memory_constraint`` can assume
265 that ``TARGET_LEGITIMATE_ADDRESS_P`` holds for the address inside
266 a ``mem`` rtx and so it does not need to test this condition itself.
267 In other words, a ``define_memory_constraint`` test of the form:
273 is enough to test whether an rtx is a ``mem`` *and* whether
274 its address satisfies ``TARGET_MEM_CONSTRAINT`` (which is usually
275 :samp:`'m'`). Thus the conditions imposed by a ``define_memory_constraint``
276 always apply on top of the conditions imposed by ``TARGET_MEM_CONSTRAINT``.
278 However, it is sometimes useful to define memory constraints that allow
279 addresses beyond those accepted by ``TARGET_LEGITIMATE_ADDRESS_P``.
280 ``define_relaxed_memory_constraint`` exists for this case.
281 The test expression in a ``define_relaxed_memory_constraint`` is
282 applied with no preconditions, so that the expression can determine
283 'from scratch' exactly which addresses are valid and which are not.
285 The syntax and semantics are otherwise identical to
286 ``define_memory_constraint``.
288 .. index:: define_address_constraint
290 MD Expression define_address_constraint name docstring expUse this expression for constraints that match a subset of all address
291 operands: that is, ``reload`` can make the constraint match by
292 converting the operand to the form :samp:`(reg {X} )`, again
293 with :samp:`{X}` a base register.
295 Constraints defined with ``define_address_constraint`` can only be
296 used with the ``address_operand`` predicate, or machine-specific
297 predicates that work the same way. They are treated analogously to
298 the generic :samp:`p` constraint.
300 The syntax and semantics are otherwise identical to
301 ``define_constraint``.
303 For historical reasons, names beginning with the letters :samp:`G H`
304 are reserved for constraints that match only ``const_double`` s, and
305 names beginning with the letters :samp:`I J K L M N O P` are reserved
306 for constraints that match only ``const_int`` s. This may change in
307 the future. For the time being, constraints with these names must be
308 written in a stylized form, so that ``genpreds`` can tell you did
313 (define_constraint "[GHIJKLMNOP]..."
315 (and (match_code "const_int") ; const_double for G/H
316 condition...)) ; usually a match_test
318 .. the semicolons line up in the formatted manual
320 It is fine to use names beginning with other letters for constraints
321 that match ``const_double`` s or ``const_int`` s.
323 Each docstring in a constraint definition should be one or more complete
324 sentences, marked up in Texinfo format. *They are currently unused.*
325 In the future they will be copied into the GCC manual, in :ref:`machine-constraints`, replacing the hand-maintained tables currently found in
326 that section. Also, in the future the compiler may use this to give
327 more helpful diagnostics when poor choice of ``asm`` constraints
328 causes a reload failure.
330 If you put the pseudo-Texinfo directive :samp:`@internal` at the
331 beginning of a docstring, then (in the future) it will appear only in
332 the internals manual's version of the machine-specific constraint tables.
333 Use this for constraints that should not appear in ``asm`` statements.
335 .. index:: testing constraints, constraints, testing
337 .. _c-constraint-interface:
339 Testing constraints from C
340 ^^^^^^^^^^^^^^^^^^^^^^^^^^
342 It is occasionally useful to test a constraint from C code rather than
343 implicitly via the constraint string in a ``match_operand``. The
344 generated file :samp:`tm_p.h` declares a few interfaces for working
345 with constraints. At present these are defined for all constraints
346 except ``g`` (which is equivalent to ``general_operand``).
348 Some valid constraint names are not valid C identifiers, so there is a
349 mangling scheme for referring to them from C. Constraint names that
350 do not contain angle brackets or underscores are left unchanged.
351 Underscores are doubled, each :samp:`<` is replaced with :samp:`_l`, and
352 each :samp:`>` with :samp:`_g`. Here are some examples:
354 .. the @c's prevent double blank lines in the printed manual.
373 Throughout this section, the variable :samp:`{c}` is either a constraint
374 in the abstract sense, or a constant from ``enum constraint_num`` ;
375 the variable :samp:`{m}` is a mangled constraint name (usually as part of
376 a larger identifier).
378 .. index:: constraint_num
380 Enum constraint_numFor each constraint except ``g``, there is a corresponding
381 enumeration constant: :samp:`CONSTRAINT_` plus the mangled name of the
382 constraint. Functions that take an ``enum constraint_num`` as an
383 argument expect one of these constants.
385 .. function:: inline bool satisfies_constraint_m (rtx exp)
387 For each non-register constraint :samp:`{m}` except ``g``, there is
388 one of these functions; it returns ``true`` if :samp:`{exp}` satisfies the
389 constraint. These functions are only visible if :samp:`rtl.h` was included
390 before :samp:`tm_p.h`.
392 .. function:: bool constraint_satisfied_p (rtx exp, enum constraint_num c)
394 Like the ``satisfies_constraint_m`` functions, but the
395 constraint to test is given as an argument, :samp:`{c}`. If :samp:`{c}`
396 specifies a register constraint, this function will always return
399 .. function:: enum reg_class reg_class_for_constraint (enum constraint_num c)
401 Returns the register class associated with :samp:`{c}`. If :samp:`{c}` is not
402 a register constraint, or those registers are not available for the
403 currently selected subtarget, returns ``NO_REGS``.
405 Here is an example use of ``satisfies_constraint_m``. In
406 peephole optimizations (see :ref:`peephole-definitions`), operand
407 constraint strings are ignored, so if there are relevant constraints,
408 they must be tested in the C condition. In the example, the
409 optimization is applied if operand 2 does *not* satisfy the
410 :samp:`K` constraint. (This is a simplified version of a peephole
411 definition from the i386 machine description.)
416 [(match_scratch:SI 3 "r")
417 (set (match_operand:SI 0 "register_operand" "")
418 (mult:SI (match_operand:SI 1 "memory_operand" "")
419 (match_operand:SI 2 "immediate_operand" "")))]
421 "!satisfies_constraint_K (operands[2])"
423 [(set (match_dup 3) (match_dup 1))
424 (set (match_dup 0) (mult:SI (match_dup 3) (match_dup 2)))]