| 1 |
/* Description of target passes for AVR 8-bit microcontrollers. |
| 2 |
Copyright (C) 2016-2025 Free Software Foundation, Inc. */ |
| 3 |
|
| 4 |
/* This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify it under |
| 7 |
the terms of the GNU General Public License as published by the Free |
| 8 |
Software Foundation; either version 3, or (at your option) any later |
| 9 |
version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 |
for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with GCC; see the file COPYING3. If not see |
| 18 |
<http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
/* A post reload optimization pass that fuses PLUS insns with CONST_INT |
| 21 |
addend with a load or store insn to get POST_INC or PRE_DEC addressing. |
| 22 |
It can also fuse two PLUSes to a single one, which may occur due to |
| 23 |
splits from `avr_split_fake_addressing_move'. We do this in an own |
| 24 |
pass because it can find more cases than peephole2, for example when |
| 25 |
there are unrelated insns between the interesting ones. */ |
| 26 |
|
| 27 |
INSERT_PASS_BEFORE (pass_peephole2, 1, avr_pass_fuse_add); |
| 28 |
|
| 29 |
/* There are cases where avr-fuse-add doesn't find POST_INC cases because |
| 30 |
the RTL code at that time is too long-winded, and moves registers back and |
| 31 |
forth (which seems to be the same reason for why pass auto_inc_dec cannot |
| 32 |
find POST_INC, either). Some of that long-windedness is cleaned up very |
| 33 |
late in pass cprop_hardreg, which opens up new opportunities to find post |
| 34 |
increments. An example is the following function from AVR-LibC's qsort: |
| 35 |
|
| 36 |
void swapfunc (char *a, char *b, int n) |
| 37 |
{ |
| 38 |
do |
| 39 |
{ |
| 40 |
char tmp = *a; |
| 41 |
*a++ = *b; |
| 42 |
*b++ = tmp; |
| 43 |
} while (--n > 0); |
| 44 |
} |
| 45 |
|
| 46 |
Hence, run avr-fuse-add twice; the second time after cprop_hardreg. */ |
| 47 |
|
| 48 |
INSERT_PASS_AFTER (pass_cprop_hardreg, 1, avr_pass_fuse_add); |
| 49 |
|
| 50 |
/* An analysis pass that runs prior to prologue / epilogue generation. |
| 51 |
Computes cfun->machine->gasisr.maybe which is used in prologue and |
| 52 |
epilogue generation provided -mgas-isr-prologues is on. */ |
| 53 |
|
| 54 |
INSERT_PASS_BEFORE (pass_thread_prologue_and_epilogue, 1, avr_pass_pre_proep); |
| 55 |
|
| 56 |
/* This avr-specific pass (re)computes insn notes, in particular REG_DEAD |
| 57 |
notes which are used by `avr.cc::reg_unused_after' and branch offset |
| 58 |
computations. These notes must be correct, i.e. there must be no |
| 59 |
dangling REG_DEAD notes; otherwise wrong code might result, cf. PR64331. |
| 60 |
|
| 61 |
DF needs (correct) CFG, hence right before free_cfg is the last |
| 62 |
opportunity to rectify notes. */ |
| 63 |
|
| 64 |
INSERT_PASS_BEFORE (pass_free_cfg, 1, avr_pass_recompute_notes); |
| 65 |
|
| 66 |
/* casesi uses a SImode switch index which is quite costly as most code will |
| 67 |
work on HImode or QImode. The following pass runs right after .expand and |
| 68 |
tries to fix such situations by operating on the original mode. This |
| 69 |
reduces code size and register pressure. |
| 70 |
|
| 71 |
The assertion is that the code generated by casesi is unaltered and |
| 72 |
a sign-extend or zero-extend from QImode or HImode precedes the casesi |
| 73 |
insns without any insns in between. */ |
| 74 |
|
| 75 |
INSERT_PASS_AFTER (pass_expand, 1, avr_pass_casesi); |
| 76 |
|
| 77 |
/* Insn combine may come up with superfluous reg-reg moves, where the combine |
| 78 |
people say that these are no problem since reg-alloc is supposed to optimize |
| 79 |
them. The issue is that the lower-subreg pass sitting between combine and |
| 80 |
reg-alloc may split such moves, coming up with a zoo of subregs which are |
| 81 |
only handled poorly by the register allocator. */ |
| 82 |
|
| 83 |
INSERT_PASS_AFTER (pass_combine, 1, avr_pass_2moves); |
| 84 |
|
| 85 |
/* Some combine insns have nonzero_bits() in their condition, though insns |
| 86 |
should not use such stuff in their condition. Therefore, we split such |
| 87 |
insn into something without nonzero_bits() in their condition right after |
| 88 |
insn combine. |
| 89 |
|
| 90 |
Since neither split_all_insns() nor split_all_insns_noflow() work at that |
| 91 |
point (presumably since there are splits involving branches), we split |
| 92 |
respective insns (and only such insns) by hand. Respective insns are |
| 93 |
tagged with insn attribute nzb = "yes" so that they are easy to spot. */ |
| 94 |
|
| 95 |
INSERT_PASS_AFTER (pass_combine, 1, avr_pass_split_nzb); |
| 96 |
|
| 97 |
/* If-else decision trees generated for switch / case may produce sequences |
| 98 |
like |
| 99 |
|
| 100 |
SREG = compare (reg, val); |
| 101 |
if (SREG == 0) goto label1; |
| 102 |
SREG = compare (reg, 1 + val); |
| 103 |
if (SREG >= 0) goto label2; |
| 104 |
|
| 105 |
which can be optimized to |
| 106 |
|
| 107 |
SREG = compare (reg, val); |
| 108 |
if (SREG == 0) goto label1; |
| 109 |
if (SREG >= 0) goto label2; |
| 110 |
|
| 111 |
The optimal place for such a pass would be directly after expand, but |
| 112 |
it's not possible for a jump insn to target more than one code label. |
| 113 |
Hence, run a mini pass right before split2 which introduces REG_CC. */ |
| 114 |
|
| 115 |
INSERT_PASS_BEFORE (pass_split_after_reload, 1, avr_pass_ifelse); |
| 116 |
|
| 117 |
/* A post reload pass that tracks known values held in registers |
| 118 |
and performs optimizations based on that knowledge. |
| 119 |
It also splits non-memory insns that can be represented in |
| 120 |
terms of byte operations. |
| 121 |
|
| 122 |
It runs between the two instances of the RTL peephole pass because |
| 123 |
- The RTL peepholer may provide a scratch reg for *reload_in<mode>. |
| 124 |
- The RTL peepholer may optimize insns involving lower registers. */ |
| 125 |
|
| 126 |
INSERT_PASS_AFTER (pass_peephole2, 1, avr_pass_fuse_move); |
| 127 |
|
| 128 |
/* Run an instance of post-reload split prior to avr-fuse-move. |
| 129 |
Purpose is to split the `3op' alternative (which allows 3 operands) |
| 130 |
of shift insns into a 3-operand shift with a byte offset, and |
| 131 |
a 2-operand residual shift. This additional split pass runs after |
| 132 |
the 1st RTL peephole pass but prior to avr-fuse-move. |
| 133 |
The respective define_split patterns have a `n_avr_fuse_add_executed' |
| 134 |
condition (amongst others) so that split passes that run before |
| 135 |
the 1st RTL peephole pass won't split them. Shifts with a constant |
| 136 |
offset that is a multiple of 8 are split by avr-fuse-move. */ |
| 137 |
|
| 138 |
INSERT_PASS_AFTER (pass_peephole2, 1, avr_pass_split_after_peephole2); |