#### 17.8.8 Testing constraints from C

It is occasionally useful to test a constraint from C code rather than implicitly via the constraint string in a `match_operand`. The generated file `tm_p.h` declares a few interfaces for working with constraints. At present these are defined for all constraints except `g` (which is equivalent to `general_operand`).

Some valid constraint names are not valid C identifiers, so there is a mangling scheme for referring to them from C. Constraint names that do not contain angle brackets or underscores are left unchanged. Underscores are doubled, each ‘`<`’ is replaced with ‘`_l`’, and each ‘`>`’ with ‘`_g`’. Here are some examples:

    Original

    Mangled

    x

    x

    P42x

    P42x

    P4_x

    P4__x

    P4>x

    P4_gx

    P4>>

    P4_g_g

    P4_g>

    P4__g_g

Throughout this section, the variable c is either a constraint in the abstract sense, or a constant from `enum constraint_num`; the variable m is a mangled constraint name (usually as part of a larger identifier).

Enum: constraint_num

  For each constraint except `g`, there is a corresponding enumeration constant: ‘`CONSTRAINT_`’ plus the mangled name of the constraint. Functions that take an `enum constraint_num` as an argument expect one of these constants.

Function: inline bool satisfies_constraint_ m (rtx exp)

  For each non-register constraint m except `g`, there is one of these functions; it returns `true` if exp satisfies the constraint. These functions are only visible if `rtl.h` was included before `tm_p.h`.

Function: bool constraint_satisfied_p (rtx exp, enum constraint_num c)

  Like the `satisfies_constraint_m` functions, but the constraint to test is given as an argument, c. If c specifies a register constraint, this function will always return `false`.

Function: enum reg_class reg_class_for_constraint (enum constraint_num c)

  Returns the register class associated with c. If c is not a register constraint, or those registers are not available for the currently selected subtarget, returns `NO_REGS`.

Here is an example use of `satisfies_constraint_m`. In peephole optimizations (see Peephole Definitions), operand constraint strings are ignored, so if there are relevant constraints, they must be tested in the C condition. In the example, the optimization is applied if operand 2 does not satisfy the ‘`K`’ constraint. (This is a simplified version of a peephole definition from the i386 machine description.)

    (define_peephole2
      [(match_scratch:SI 3 "r")
       (set (match_operand:SI 0 "register_operand" "")
            (mult:SI (match_operand:SI 1 "memory_operand" "")
                     (match_operand:SI 2 "immediate_operand" "")))]

      "!satisfies_constraint_K (operands[2])"

      [(set (match_dup 3) (match_dup 1))
       (set (match_dup 0) (mult:SI (match_dup 3) (match_dup 2)))]

      "")