10.8 Registers and Memory
- For small values of the integer n (those that are less than
FIRST_PSEUDO_REGISTER), this stands for a reference to machine register number n: a hard register. For larger values of n, it stands for a temporary value or pseudo register. The compiler's strategy is to generate code assuming an unlimited number of such pseudo registers, and later convert them into hard registers or into memory references.
m is the machine mode of the reference. It is necessary because machines can generally refer to each register in more than one mode. For example, a register may contain a full word but there may be instructions to refer to it as a half word or as a single byte, as well as instructions to refer to it as a floating point number of various precisions.
Even for a register that the machine can access in only one mode, the mode must always be specified.
FIRST_PSEUDO_REGISTERis defined by the machine description, since the number of hard registers on the machine is an invariant characteristic of the machine. Note, however, that not all of the machine registers must be general registers. All the machine registers that can be used for storage of data are given hard register numbers, even those that can be used only in certain instructions or can hold only certain types of data.
A hard register may be accessed in various modes throughout one function, but each pseudo register is given a natural mode and is accessed only in that mode. When it is necessary to describe an access to a pseudo register using a nonnatural mode, a
subregexpression is used.
regexpression with a machine mode that specifies more than one word of data may actually stand for several consecutive registers. If in addition the register number specifies a hardware register, then it actually represents several consecutive hardware registers starting with the specified one.
Each pseudo register number used in a function's RTL code is represented by a unique
Some pseudo register numbers, those within the range of
LAST_VIRTUAL_REGISTERonly appear during the RTL generation phase and are eliminated before the optimization phases. These represent locations in the stack frame that cannot be determined until RTL generation for the function has been completed. The following virtual register numbers are defined:
- This points to the first word of the incoming arguments passed on the stack. Normally these arguments are placed there by the caller, but the callee may have pushed some arguments that were previously passed in registers.
FRAME_GROWS_DOWNWARDis defined to a nonzero value, this points to immediately above the first variable on the stack. Otherwise, it points to the first variable on the stack.
- This points to the location of dynamically allocated memory on the stack immediately after the stack pointer has been adjusted by the amount of memory desired.
- This points to the location in the stack at which outgoing arguments
should be written when the stack is pre-pushed (arguments pushed using
push insns should always use
(subreg:m1 reg:m2 bytenum
subregexpressions are used to refer to a register in a machine mode other than its natural one, or to refer to one register of a multi-part
regthat actually refers to several registers.
Each pseudo register has a natural mode. If it is necessary to operate on it in a different mode, the register must be enclosed in a
There are currently three supported types for the first operand of a
- pseudo registers
This is the most common case. Most
subregs have pseudo
regs as their first operand.
memwere common in earlier versions of GCC and are still supported. During the reload pass these are replaced by plain
mems. On machines that do not do instruction scheduling, use of
memare still used, but this is no longer recommended. Such
subregs are considered to be
register_operands rather than
memory_operands before and during reload. Because of this, the scheduling passes cannot properly schedule instructions with
mem, so for machines that do scheduling,
memshould never be used. To support this, the combine and recog passes have explicit code to inhibit the creation of
The use of
memafter the reload pass is an area that is not well understood and should be avoided. There is still some code in the compiler to support this, but this code has possibly rotted. This use of
subregs is discouraged and will most likely not be supported in the future.
- hard registers
It is seldom necessary to wrap hard registers in
subregs; such registers would normally reduce to a single
regrtx. This use of
subregs is discouraged and may not be supported in the future.
subregs are not supported. Using
simplify_gen_subregis the recommended way to avoid this problem.
subregs come in two distinct flavors, each having its own usage and rules:
- Paradoxical subregs
- When m1 is strictly wider than m2, the
subregexpression is called paradoxical. The canonical test for this class of
GET_MODE_SIZE (m1) > GET_MODE_SIZE (m2)
subregs can be used as both lvalues and rvalues. When used as an lvalue, the low-order bits of the source value are stored in reg and the high-order bits are discarded. When used as an rvalue, the low-order bits of the
subregare taken from reg while the high-order bits may or may not be defined.
The high-order bits of rvalues are in the following circumstances:
memWhen m2 is smaller than a word, the macro
LOAD_EXTEND_OP, can control how the high-order bits are defined.
regs The upper bits are defined when
SUBREG_PROMOTED_UNSIGNED_Pdescribes what the upper bits hold. Such subregs usually represent local variables, register variables and parameter pseudo variables that have been promoted to a wider mode.
bytenum is always zero for a paradoxical
subreg, even on big-endian targets.
For example, the paradoxical
(set (subreg:SI (reg:HI x) 0) y)
stores the lower 2 bytes of y in x and discards the upper 2 bytes. A subsequent:
(set z (subreg:SI (reg:HI x) 0))
would set the lower two bytes of z to y and set the upper two bytes to an unknown value assuming
- Normal subregs
- When m1 is at least as narrow as m2 the
subregexpression is called normal.
subregs restrict consideration to certain bits of reg. There are two cases. If m1 is smaller than a word, the
subregrefers to the least-significant part (or lowpart) of one word of reg. If m1 is word-sized or greater, the
subregrefers to one or more complete words.
When used as an lvalue,
subregis a word-based accessor. Storing to a
subregmodifies all the words of reg that overlap the
subreg, but it leaves the other words of reg alone.
When storing to a normal
subregthat is smaller than a word, the other bits of the referenced word are usually left in an undefined state. This laxity makes it easier to generate efficient code for such instructions. To represent an instruction that preserves all the bits outside of those in the
bytenum must identify the offset of the first byte of the
subregfrom the start of reg, assuming that reg is laid out in memory order. The memory order of bytes is defined by two target macros,
WORDS_BIG_ENDIAN, if set to 1, says that byte number zero is part of the most significant word; otherwise, it is part of the least significant word.
BYTES_BIG_ENDIAN, if set to 1, says that byte number zero is the most significant byte within a word; otherwise, it is the least significant byte within a word.
On a few targets,
WORDS_BIG_ENDIAN. However, most parts of the compiler treat floating point values as if they had the same endianness as integer values. This works because they handle them solely as a collection of integer values, with no particular numerical value. Only real.c and the runtime libraries care about
(subreg:HI (reg:SI x) 2)
BYTES_BIG_ENDIAN, UNITS_PER_WORD == 4 target is the same as
(subreg:HI (reg:SI x) 0)
on a little-endian, UNITS_PER_WORD == 4 target. Both
subregs access the lower two bytes of register x.
MODE_PARTIAL_INTmode behaves as if it were as wide as the corresponding
MODE_INTmode, except that it has an unknown number of undefined bits. For example:
(subreg:PSI (reg:SI 0) 0)
accesses the whole of (reg:SI 0), but the exact relationship between the
PSImodevalue and the
SImodevalue is not defined. If we assume UNITS_PER_WORD <= 4, then the following two
(subreg:PSI (reg:DI 0) 0) (subreg:PSI (reg:DI 0) 4)
represent independent 4-byte accesses to the two halves of (reg:DI 0). Both
subregs have an unknown number of undefined bits.
If UNITS_PER_WORD <= 2 then these two
(subreg:HI (reg:PSI 0) 0) (subreg:HI (reg:PSI 0) 2)
represent independent 2-byte accesses that together span the whole of (reg:PSI 0). Storing to the first
subregdoes not affect the value of the second, and vice versa. (reg:PSI 0) has an unknown number of undefined bits, so the assignment:
(set (subreg:HI (reg:PSI 0) 0) (reg:HI 4))
does not guarantee that (subreg:HI (reg:PSI 0) 0) has the value (reg:HI 4).
The rules above apply to both pseudo regs and hard regs. If the semantics are not correct for particular combinations of m1, m2 and hard reg, the target-specific code must ensure that those combinations are never used. For example:
CANNOT_CHANGE_MODE_CLASS (m2, m1, class)
must be true for every class class that includes reg.
It has been several years since a platform in which
BYTES_BIG_ENDIANnot equal to
WORDS_BIG_ENDIANhas been tested. Anyone wishing to support such a platform in the future may be confronted with code rot.
- pseudo registers This is the most common case. Most
- This represents a scratch register that will be required for the
execution of a single instruction and not used subsequently. It is
converted into a
regby either the local register allocator or the reload pass.
scratchis usually present inside a
clobberoperation (see Side Effects).
- This refers to the machine's condition code register. It has no
operands and may not have a machine mode. There are two ways to use it:
- To stand for a complete set of condition code flags. This is best on
most machines, where each comparison sets the entire series of flags.
With this technique,
(cc0)may be validly used in only two contexts: as the destination of an assignment (in test and compare instructions) and in comparison operators comparing against zero (
const_intwith value zero; that is to say,
- To stand for a single flag that is the result of a single condition.
This is useful on machines that have only a single flag bit, and in
which comparison instructions must specify the condition to test.
With this technique,
(cc0)may be validly used in only two contexts: as the destination of an assignment (in test and compare instructions) where the source is a comparison operator, and as the first operand of
if_then_else(in a conditional branch).
Instructions can set the condition code implicitly. On many machines, nearly all instructions set the condition code based on the value that they compute or store. It is not necessary to record these actions explicitly in the RTL because the machine description includes a prescription for recognizing the instructions that do so (by means of the macro
NOTICE_UPDATE_CC). See Condition Code. Only instructions whose sole purpose is to set the condition code, and instructions that use the condition code, need mention
On some machines, the condition code register is given a register number and a
regis used instead of
(cc0). This is usually the preferable approach if only a small subset of instructions modify the condition code. Other machines store condition codes in general registers; in such cases a pseudo register should be used.
Some machines, such as the SPARC and RS/6000, have two sets of arithmetic instructions, one that sets and one that does not set the condition code. This is best handled by normally generating the instruction that does not set the condition code, and making a pattern that both performs the arithmetic and sets the condition code register (which would not be
(cc0)in this case). For examples, search for addcc and andcc in sparc.md.
- To stand for a complete set of condition code flags. This is best on most machines, where each comparison sets the entire series of flags.
- This represents the machine's program counter. It has no operands and
may not have a machine mode.
(pc)may be validly used only in certain specific contexts in jump instructions.
All instructions that do not jump alter the program counter implicitly by incrementing it, but there is no need to mention this in the RTL.
(mem:m addr alias
- This RTX represents a reference to main memory at an address
represented by the expression addr. m specifies how large
a unit of memory is accessed. alias specifies an alias set for the
reference. In general two items are in different alias sets if they cannot
reference the same memory address.
(mem:BLK (scratch))is considered to alias all other memories. Thus it may be used as a memory barrier in epilogue stack deallocation patterns.
(concatm rtx rtx
- This RTX represents the concatenation of two other RTXs. This is used for complex values. It should only appear in the RTL attached to declarations and during RTL generation. It should not appear in the ordinary insn chain.
- This RTX represents the concatenation of all the rtx to make a
single value. Like
concat, this should only appear in declarations, and not in the insn chain.