]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/doc/gccint/target-macros/register-usage.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / doc / gccint / target-macros / register-usage.rst
1 ..
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.
5
6 .. index:: register usage
7
8 .. _registers:
9
10 Register Usage
11 **************
12
13 This section explains how to describe what registers the target machine
14 has, and how (in general) they can be used.
15
16 The description of which registers a specific instruction can use is
17 done with register classes; see :ref:`register-classes`. For information
18 on using registers to access a stack frame, see :ref:`frame-registers`.
19 For passing values in registers, see :ref:`register-arguments`.
20 For returning values in registers, see :ref:`scalar-return`.
21
22 .. toctree::
23 :maxdepth: 2
24
25
26 .. _register-basics:
27
28 Basic Characteristics of Registers
29 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30
31 .. prevent bad page break with this line
32
33 Registers have various characteristics.
34
35 .. c:macro:: FIRST_PSEUDO_REGISTER
36
37 Number of hardware registers known to the compiler. They receive
38 numbers 0 through ``FIRST_PSEUDO_REGISTER-1`` ; thus, the first
39 pseudo register's number really is assigned the number
40 ``FIRST_PSEUDO_REGISTER``.
41
42 .. c:macro:: FIXED_REGISTERS
43
44 .. index:: fixed register
45
46 An initializer that says which registers are used for fixed purposes
47 all throughout the compiled code and are therefore not available for
48 general allocation. These would include the stack pointer, the frame
49 pointer (except on machines where that can be used as a general
50 register when no frame pointer is needed), the program counter on
51 machines where that is considered one of the addressable registers,
52 and any other numbered register with a standard use.
53
54 This information is expressed as a sequence of numbers, separated by
55 commas and surrounded by braces. The :samp:`{n}` th number is 1 if
56 register :samp:`{n}` is fixed, 0 otherwise.
57
58 The table initialized from this macro, and the table initialized by
59 the following one, may be overridden at run time either automatically,
60 by the actions of the macro ``CONDITIONAL_REGISTER_USAGE``, or by
61 the user with the command options :option:`-ffixed-reg`,
62 :option:`-fcall-used-reg` and :option:`-fcall-saved-reg`.
63
64 .. c:macro:: CALL_USED_REGISTERS
65
66 .. index:: call-used register, call-clobbered register, call-saved register
67
68 Like ``FIXED_REGISTERS`` but has 1 for each register that is
69 clobbered (in general) by function calls as well as for fixed
70 registers. This macro therefore identifies the registers that are not
71 available for general allocation of values that must live across
72 function calls.
73
74 If a register has 0 in ``CALL_USED_REGISTERS``, the compiler
75 automatically saves it on function entry and restores it on function
76 exit, if the register is used within the function.
77
78 Exactly one of ``CALL_USED_REGISTERS`` and ``CALL_REALLY_USED_REGISTERS``
79 must be defined. Modern ports should define ``CALL_REALLY_USED_REGISTERS``.
80
81 .. c:macro:: CALL_REALLY_USED_REGISTERS
82
83 .. index:: call-used register, call-clobbered register, call-saved register
84
85 Like ``CALL_USED_REGISTERS`` except this macro doesn't require
86 that the entire set of ``FIXED_REGISTERS`` be included.
87 (``CALL_USED_REGISTERS`` must be a superset of ``FIXED_REGISTERS``).
88
89 Exactly one of ``CALL_USED_REGISTERS`` and ``CALL_REALLY_USED_REGISTERS``
90 must be defined. Modern ports should define ``CALL_REALLY_USED_REGISTERS``.
91
92 .. index:: call-used register, call-clobbered register, call-saved register
93
94 .. include:: tm.rst.in
95 :start-after: [TARGET_FNTYPE_ABI]
96 :end-before: [TARGET_FNTYPE_ABI]
97
98
99 .. include:: tm.rst.in
100 :start-after: [TARGET_INSN_CALLEE_ABI]
101 :end-before: [TARGET_INSN_CALLEE_ABI]
102
103
104 .. index:: call-used register, call-clobbered register, call-saved register
105
106 .. include:: tm.rst.in
107 :start-after: [TARGET_HARD_REGNO_CALL_PART_CLOBBERED]
108 :end-before: [TARGET_HARD_REGNO_CALL_PART_CLOBBERED]
109
110
111 .. include:: tm.rst.in
112 :start-after: [TARGET_GET_MULTILIB_ABI_NAME]
113 :end-before: [TARGET_GET_MULTILIB_ABI_NAME]
114
115
116 .. index:: fixed_regs, call_used_regs, global_regs, reg_names, reg_class_contents
117
118 .. include:: tm.rst.in
119 :start-after: [TARGET_CONDITIONAL_REGISTER_USAGE]
120 :end-before: [TARGET_CONDITIONAL_REGISTER_USAGE]
121
122
123 .. c:macro:: INCOMING_REGNO (out)
124
125 Define this macro if the target machine has register windows. This C
126 expression returns the register number as seen by the called function
127 corresponding to the register number :samp:`{out}` as seen by the calling
128 function. Return :samp:`{out}` if register number :samp:`{out}` is not an
129 outbound register.
130
131 .. c:macro:: OUTGOING_REGNO (in)
132
133 Define this macro if the target machine has register windows. This C
134 expression returns the register number as seen by the calling function
135 corresponding to the register number :samp:`{in}` as seen by the called
136 function. Return :samp:`{in}` if register number :samp:`{in}` is not an inbound
137 register.
138
139 .. c:macro:: LOCAL_REGNO (regno)
140
141 Define this macro if the target machine has register windows. This C
142 expression returns true if the register is call-saved but is in the
143 register window. Unlike most call-saved registers, such registers
144 need not be explicitly restored on function exit or during non-local
145 gotos.
146
147 .. c:macro:: PC_REGNUM
148
149 If the program counter has a register number, define this as that
150 register number. Otherwise, do not define it.
151
152 .. index:: order of register allocation, register allocation order
153
154 .. _allocation-order:
155
156 Order of Allocation of Registers
157 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
158
159 .. prevent bad page break with this line
160
161 Registers are allocated in order.
162
163 .. c:macro:: REG_ALLOC_ORDER
164
165 If defined, an initializer for a vector of integers, containing the
166 numbers of hard registers in the order in which GCC should prefer
167 to use them (from most preferred to least).
168
169 If this macro is not defined, registers are used lowest numbered first
170 (all else being equal).
171
172 One use of this macro is on machines where the highest numbered
173 registers must always be saved and the save-multiple-registers
174 instruction supports only sequences of consecutive registers. On such
175 machines, define ``REG_ALLOC_ORDER`` to be an initializer that lists
176 the highest numbered allocable register first.
177
178 .. c:macro:: ADJUST_REG_ALLOC_ORDER
179
180 A C statement (sans semicolon) to choose the order in which to allocate
181 hard registers for pseudo-registers local to a basic block.
182
183 Store the desired register order in the array ``reg_alloc_order``.
184 Element 0 should be the register to allocate first; element 1, the next
185 register; and so on.
186
187 The macro body should not assume anything about the contents of
188 ``reg_alloc_order`` before execution of the macro.
189
190 On most machines, it is not necessary to define this macro.
191
192 .. c:macro:: HONOR_REG_ALLOC_ORDER
193
194 Normally, IRA tries to estimate the costs for saving a register in the
195 prologue and restoring it in the epilogue. This discourages it from
196 using call-saved registers. If a machine wants to ensure that IRA
197 allocates registers in the order given by REG_ALLOC_ORDER even if some
198 call-saved registers appear earlier than call-used ones, then define this
199 macro as a C expression to nonzero. Default is 0.
200
201 .. c:macro:: IRA_HARD_REGNO_ADD_COST_MULTIPLIER (regno)
202
203 In some case register allocation order is not enough for the
204 Integrated Register Allocator (IRA) to generate a good code.
205 If this macro is defined, it should return a floating point value
206 based on :samp:`{regno}`. The cost of using :samp:`{regno}` for a pseudo will
207 be increased by approximately the pseudo's usage frequency times the
208 value returned by this macro. Not defining this macro is equivalent
209 to having it always return ``0.0``.
210
211 On most machines, it is not necessary to define this macro.
212
213 .. _values-in-registers:
214
215 How Values Fit in Registers
216 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
217
218 This section discusses the macros that describe which kinds of values
219 (specifically, which machine modes) each register can hold, and how many
220 consecutive registers are needed for a given mode.
221
222 .. include:: tm.rst.in
223 :start-after: [TARGET_HARD_REGNO_NREGS]
224 :end-before: [TARGET_HARD_REGNO_NREGS]
225
226
227 .. c:macro:: HARD_REGNO_NREGS_HAS_PADDING (regno, mode)
228
229 A C expression that is nonzero if a value of mode :samp:`{mode}`, stored
230 in memory, ends with padding that causes it to take up more space than
231 in registers starting at register number :samp:`{regno}` (as determined by
232 multiplying GCC's notion of the size of the register when containing
233 this mode by the number of registers returned by
234 ``TARGET_HARD_REGNO_NREGS``). By default this is zero.
235
236 For example, if a floating-point value is stored in three 32-bit
237 registers but takes up 128 bits in memory, then this would be
238 nonzero.
239
240 This macros only needs to be defined if there are cases where
241 ``subreg_get_info``
242 would otherwise wrongly determine that a ``subreg`` can be
243 represented by an offset to the register number, when in fact such a
244 ``subreg`` would contain some of the padding not stored in
245 registers and so not be representable.
246
247 .. c:macro:: HARD_REGNO_NREGS_WITH_PADDING (regno, mode)
248
249 For values of :samp:`{regno}` and :samp:`{mode}` for which
250 ``HARD_REGNO_NREGS_HAS_PADDING`` returns nonzero, a C expression
251 returning the greater number of registers required to hold the value
252 including any padding. In the example above, the value would be four.
253
254 .. c:macro:: REGMODE_NATURAL_SIZE (mode)
255
256 Define this macro if the natural size of registers that hold values
257 of mode :samp:`{mode}` is not the word size. It is a C expression that
258 should give the natural size in bytes for the specified mode. It is
259 used by the register allocator to try to optimize its results. This
260 happens for example on SPARC 64-bit where the natural size of
261 floating-point registers is still 32-bit.
262
263 .. include:: tm.rst.in
264 :start-after: [TARGET_HARD_REGNO_MODE_OK]
265 :end-before: [TARGET_HARD_REGNO_MODE_OK]
266
267
268 .. c:macro:: HARD_REGNO_RENAME_OK (from, to)
269
270 A C expression that is nonzero if it is OK to rename a hard register
271 :samp:`{from}` to another hard register :samp:`{to}`.
272
273 One common use of this macro is to prevent renaming of a register to
274 another register that is not saved by a prologue in an interrupt
275 handler.
276
277 The default is always nonzero.
278
279 .. include:: tm.rst.in
280 :start-after: [TARGET_MODES_TIEABLE_P]
281 :end-before: [TARGET_MODES_TIEABLE_P]
282
283
284 .. include:: tm.rst.in
285 :start-after: [TARGET_HARD_REGNO_SCRATCH_OK]
286 :end-before: [TARGET_HARD_REGNO_SCRATCH_OK]
287
288
289 .. c:macro:: AVOID_CCMODE_COPIES
290
291 Define this macro if the compiler should avoid copies to/from ``CCmode``
292 registers. You should only define this macro if support for copying to/from
293 ``CCmode`` is incomplete.
294
295 .. index:: leaf functions, functions, leaf
296
297 .. _leaf-functions:
298
299 Handling Leaf Functions
300 ^^^^^^^^^^^^^^^^^^^^^^^
301
302 On some machines, a leaf function (i.e., one which makes no calls) can run
303 more efficiently if it does not make its own register window. Often this
304 means it is required to receive its arguments in the registers where they
305 are passed by the caller, instead of the registers where they would
306 normally arrive.
307
308 The special treatment for leaf functions generally applies only when
309 other conditions are met; for example, often they may use only those
310 registers for its own variables and temporaries. We use the term 'leaf
311 function' to mean a function that is suitable for this special
312 handling, so that functions with no calls are not necessarily 'leaf
313 functions'.
314
315 GCC assigns register numbers before it knows whether the function is
316 suitable for leaf function treatment. So it needs to renumber the
317 registers in order to output a leaf function. The following macros
318 accomplish this.
319
320 .. c:macro:: LEAF_REGISTERS
321
322 Name of a char vector, indexed by hard register number, which
323 contains 1 for a register that is allowable in a candidate for leaf
324 function treatment.
325
326 If leaf function treatment involves renumbering the registers, then the
327 registers marked here should be the ones before renumbering---those that
328 GCC would ordinarily allocate. The registers which will actually be
329 used in the assembler code, after renumbering, should not be marked with 1
330 in this vector.
331
332 Define this macro only if the target machine offers a way to optimize
333 the treatment of leaf functions.
334
335 .. c:macro:: LEAF_REG_REMAP (regno)
336
337 A C expression whose value is the register number to which :samp:`{regno}`
338 should be renumbered, when a function is treated as a leaf function.
339
340 If :samp:`{regno}` is a register number which should not appear in a leaf
341 function before renumbering, then the expression should yield -1, which
342 will cause the compiler to abort.
343
344 Define this macro only if the target machine offers a way to optimize the
345 treatment of leaf functions, and registers need to be renumbered to do
346 this.
347
348 .. index:: current_function_is_leaf, current_function_uses_only_leaf_regs
349
350 ``TARGET_ASM_FUNCTION_PROLOGUE`` and
351 ``TARGET_ASM_FUNCTION_EPILOGUE`` must usually treat leaf functions
352 specially. They can test the C variable ``current_function_is_leaf``
353 which is nonzero for leaf functions. ``current_function_is_leaf`` is
354 set prior to local register allocation and is valid for the remaining
355 compiler passes. They can also test the C variable
356 ``current_function_uses_only_leaf_regs`` which is nonzero for leaf
357 functions which only use leaf registers.
358 ``current_function_uses_only_leaf_regs`` is valid after all passes
359 that modify the instructions have been run and is only useful if
360 ``LEAF_REGISTERS`` is defined.
361
362 .. changed this to fix overfull. ALSO: why the "it" at the beginning
363
364 .. of the next paragraph?! -mew 2feb93
365
366 .. _stack-registers:
367
368 Registers That Form a Stack
369 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
370
371 There are special features to handle computers where some of the
372 'registers' form a stack. Stack registers are normally written by
373 pushing onto the stack, and are numbered relative to the top of the
374 stack.
375
376 Currently, GCC can only handle one group of stack-like registers, and
377 they must be consecutively numbered. Furthermore, the existing
378 support for stack-like registers is specific to the 80387 floating
379 point coprocessor. If you have a new architecture that uses
380 stack-like registers, you will need to do substantial work on
381 :samp:`reg-stack.cc` and write your machine description to cooperate
382 with it, as well as defining these macros.
383
384 .. c:macro:: STACK_REGS
385
386 Define this if the machine has any stack-like registers.
387
388 .. c:macro:: STACK_REG_COVER_CLASS
389
390 This is a cover class containing the stack registers. Define this if
391 the machine has any stack-like registers.
392
393 .. c:macro:: FIRST_STACK_REG
394
395 The number of the first stack-like register. This one is the top
396 of the stack.
397
398 .. c:macro:: LAST_STACK_REG
399
400 The number of the last stack-like register. This one is the bottom of
401 the stack.