Debian Codegraph @

gcc/config/avr/avr-passes.cc

raw
1 /* Support for avr-passes.def for AVR 8-bit microcontrollers.
2 Copyright (C) 2024-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
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License 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 #define IN_TARGET_CODE 1
21
22 #define INCLUDE_ARRAY
23 #define INCLUDE_VECTOR
24 #include "config.h"
25 #include "system.h"
26 #include "intl.h"
27 #include "coretypes.h"
28 #include "backend.h"
29 #include "target.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "diagnostic-core.h"
33 #include "cfghooks.h"
34 #include "cfganal.h"
35 #include "df.h"
36 #include "memmodel.h"
37 #include "tm_p.h"
38 #include "optabs.h"
39 #include "regs.h"
40 #include "emit-rtl.h"
41 #include "recog.h"
42 #include "explow.h"
43 #include "cfgrtl.h"
44 #include "context.h"
45 #include "tree-pass.h"
46 #include "insn-attr.h"
47 #include "tm-constrs.h"
48
49
50 #define CONST_INT_OR_FIXED_P(X) (CONST_INT_P (X) || CONST_FIXED_P (X))
51
52 #define FIRST_GPR (AVR_TINY ? REG_18 : REG_2)
53
54
55 // Emit pattern PAT, and ICE when the insn is not valid / not recognized.
56
57 static rtx_insn *
58 emit_valid_insn (rtx pat)
59 {
60 rtx_insn *insn = emit_insn (pat);
61
62 if (! valid_insn_p (insn)) // Also runs recog().
63 fatal_insn ("emit unrecognizable insn", insn);
64
65 return insn;
66 }
67
68 // Emit a single_set with an optional scratch operand. This function
69 // asserts that the new insn is valid and recognized.
70
71 static rtx_insn *
72 emit_valid_move_clobbercc (rtx dest, rtx src, rtx scratch = NULL_RTX)
73 {
74 rtx pat = scratch
75 ? gen_gen_move_clobbercc_scratch (dest, src, scratch)
76 : gen_gen_move_clobbercc (dest, src);
77
78 return emit_valid_insn (pat);
79 }
80
81
82 namespace
83 {
84
85 /////////////////////////////////////////////////////////////////////////////
86 // Before we start with the very code, introduce some helpers that are
87 // quite generic, though up to now only avr-fuse-add makes use of them.
88
89 /* Get the next / previous NONDEBUG_INSN_P after INSN in basic block BB.
90 This assumes we are in CFG layout mode so that BLOCK_FOR_INSN()
91 can be used. */
92
93 static rtx_insn *
94 next_nondebug_insn_bb (basic_block bb, rtx_insn *insn, bool forward = true)
95 {
96 while (insn)
97 {
98 insn = forward ? NEXT_INSN (insn) : PREV_INSN (insn);
99
100 if (insn && NONDEBUG_INSN_P (insn))
101 return BLOCK_FOR_INSN (insn) == bb ? insn : nullptr;
102 }
103
104 return insn;
105 }
106
107 static rtx_insn *
108 prev_nondebug_insn_bb (basic_block bb, rtx_insn *insn)
109 {
110 return next_nondebug_insn_bb (bb, insn, false);
111 }
112
113
114 /* Like `single_set' with the addition that it sets REGNO_SCRATCH when the
115 insn is a single_set with a QImode scratch register. When the insn has
116 no QImode scratch or just a scratch:QI, then set REGNO_SCRATCH = 0.
117 The assumption is that the function is only used after the splits for
118 REG_CC so that the pattern is a parallel with 2 elements (INSN has no
119 scratch operand), or 3 elements (INSN does have a scratch operand). */
120
121 static rtx
122 single_set_with_scratch (rtx_insn *insn, int &regno_scratch)
123 {
124 regno_scratch = 0;
125
126 if (! INSN_P (insn))
127 return NULL_RTX;
128
129 rtx set, clo, reg, pat = PATTERN (insn);
130
131 // Search for SET + CLOBBER(QI) + CLOBBER(CC).
132 if (GET_CODE (pat) == PARALLEL
133 && XVECLEN (pat, 0) == 3
134 && GET_CODE (set = XVECEXP (pat, 0, 0)) == SET
135 // At this pass, all insn are endowed with clobber(CC).
136 && GET_CODE (clo = XVECEXP (pat, 0, 2)) == CLOBBER
137 && GET_MODE (XEXP (clo, 0)) == CCmode
138 && GET_CODE (clo = XVECEXP (pat, 0, 1)) == CLOBBER
139 && REG_P (reg = XEXP (clo, 0))
140 && GET_MODE (reg) == QImode)
141 {
142 regno_scratch = REGNO (reg);
143 return set;
144 }
145
146 return single_set (insn);
147 }
148
149
150 // One bit for each GRP in REG_0 ... REG_31.
151 using gprmask_t = uint32_t;
152
153 // True when this is a valid GPR number for ordinary code, e.g.
154 // registers wider than 2 bytes have to start at an exven regno.
155 // TMP_REG and ZERO_REG are not considered valid, even though
156 // the C source can use register vars with them.
157 static inline bool
158 gpr_regno_p (int regno, int n_bytes = 1)
159 {
160 return (IN_RANGE (regno, FIRST_GPR, REG_32 - n_bytes)
161 // Size in { 1, 2, 3, 4, 8 } bytes.
162 && ((1u << n_bytes) & 0x11e)
163 // Registers >= 2 bytes start at an even regno.
164 && (n_bytes == 1 || regno % 2 == 0));
165 }
166
167 // There are cases where the C source defines local reg vars
168 // for R1 etc. The assumption is that this is handled before
169 // calling this function, e.g. by skipping code when a register
170 // overlaps with a fixed register.
171 static inline gprmask_t
172 regmask (int regno, int size)
173 {
174 gcc_checking_assert (gpr_regno_p (regno, size));
175 gprmask_t bits = (1u << size) - 1;
176
177 return bits << regno;
178 }
179
180 // Mask for hard register X that's some GPR, including fixed regs like R0.
181 static gprmask_t
182 regmask (rtx x)
183 {
184 gcc_assert (REG_P (x));
185 gprmask_t bits = (1u << GET_MODE_SIZE (GET_MODE (x))) - 1;
186
187 return bits << REGNO (x);
188 }
189
190
191 // Whether X has bits in the range [B0 ... B1]
192 static inline bool
193 has_bits_in (gprmask_t x, int b0, int b1)
194 {
195 if (b0 > b1 || b0 > 31 || b1 < 0)
196 return false;
197
198 const gprmask_t m = (2u << (b1 - b0)) - 1;
199 return x & (m << b0);
200 }
201
202
203 template<typename T>
204 T bad_case ()
205 {
206 gcc_unreachable ();
207 }
208
209 #define select false ? bad_case
210
211
212 namespace AVRasm
213 {
214 // Returns true when we a scratch reg is needed in order to get
215 // (siged or unsigned) 8-bit value VAL in some GPR.
216 // When it's about costs rather than the sheer requirement for a
217 // scratch, see also AVRasm::constant_cost.
218 static inline bool ldi_needs_scratch (int regno, int val)
219 {
220 return regno < REG_16 && IN_RANGE (val & 0xff, 2, 254);
221 }
222
223 // Return a byte value x >= 0 such that x <code> y == x for all y, or -1.
224 static inline int neutral_val (rtx_code code)
225 {
226 return select<int>()
227 : code == AND ? 0xff
228 : code == IOR ? 0x00
229 : code == XOR ? 0x00
230 : code == PLUS ? 0
231 : -1;
232 }
233
234 // When there exists a value x such that the image of the function
235 // y -> y <code> x has order 1, then return that x. Else return -1.
236 static inline int image1_val (rtx_code code)
237 {
238 return select<int>()
239 : code == AND ? 0x00
240 : code == IOR ? 0xff
241 : -1;
242 }
243
244 // Cost of 8-bit binary operation x o= VAL provided a scratch is
245 // available as needed.
246 static int constant_cost (rtx_code code, int regno, uint8_t val)
247 {
248 bool needs_scratch_p = select<bool>()
249 : code == PLUS ? regno < REG_16 && val != 1 && val != 0xff
250 : code == XOR ? val != 0xff && (regno < REG_16 || val != 0x80)
251 : code == IOR ? regno < REG_16
252 : code == AND ? regno < REG_16 && val != 0
253 : code == SET ? regno < REG_16 && val != 0
254 : bad_case<bool> ();
255
256 return val == AVRasm::neutral_val (code)
257 ? 0
258 : 1 + needs_scratch_p;
259 }
260 }; // AVRasm
261
262
263 // Returns the mode mask for a mode size of SIZE bytes.
264 static uint64_t size_to_mask (int size)
265 {
266 return ((uint64_t) 2 << (8 * size - 1)) - 1;
267 }
268
269 // Return the scalar int mode for a modesize of 1, 2, 3, 4 or 8 bytes.
270 static machine_mode size_to_mode (int size)
271 {
272 return select<machine_mode>()
273 : size == 1 ? QImode
274 : size == 2 ? HImode
275 : size == 3 ? PSImode
276 : size == 4 ? SImode
277 : size == 8 ? DImode
278 : bad_case<machine_mode> ();
279 }
280
281
282 //////////////////////////////////////////////////////////////////////////////
283 // Optimize moves after reload: -mfuse-move=<0,23>
284
285 /* The purpose of this pass is to perform optimizations after reload
286 like the following ones:
287
288 Without optimization | With optimization
289 ==================== | =================
290
291 long long fn_zero (void) (1)
292 {
293 return 0;
294 }
295
296 ldi r18, 0 ; movqi_insn | ldi r18, 0 ; movqi_insn
297 ldi r19, 0 ; movqi_insn | ldi r19, 0 ; movqi_insn
298 ldi r20, 0 ; movqi_insn | movw r20, r18 ; *movhi
299 ldi r21, 0 ; movqi_insn |
300 ldi r22, 0 ; movqi_insn | movw r22, r18 ; *movhi
301 ldi r23, 0 ; movqi_insn |
302 ldi r24, 0 ; movqi_insn | movw r24, r18 ; *movhi
303 ldi r25, 0 ; movqi_insn |
304 ret | ret
305
306 int fn_eq0 (char c) (2)
307 {
308 return c == 0;
309 }
310
311 mov r18, r24 ; movqi_insn | mov r18, r24 ; movqi_insn
312 ldi r24, 1 ; *movhi | ldi r24, 1 ; *movhi
313 ldi r25, 0 | ldi r25, 0
314 cp r18, ZERO ; cmpqi3 | cpse r18, ZERO ; peephole
315 breq .+4 ; branch |
316 ldi r24, 0 ; *movhi | ldi r24, 0 ; movqi_insn
317 ldi r25, 0 |
318 ret | ret
319
320 int a, b; (3)
321
322 void fn_store_ab (void)
323 {
324 a = 1;
325 b = -1;
326 }
327
328 ldi r24, 1 ; *movhi | ldi r24, 1 ; *movhi
329 ldi r25, 0 | ldi r25, 0
330 sts a+1, r25 ; *movhi | sts a+1, r25 ; *movhi
331 sts a, r24 | sts a, r24
332 ldi r24, -1 ; *movhi | sbiw r24, 2 ; *addhi3
333 ldi r25, -1 |
334 sts b+1, r25 ; *movhi | sts b+1, r25 ; *movhi
335 sts b, r24 | sts b, r24
336 ret | ret
337
338 unsigned fn_crc (unsigned x, unsigned y) (4)
339 {
340 for (char i = 8; i--; x <<= 1)
341 y ^= (x ^ y) & 0x80 ? 79U : 0U;
342 return y;
343 }
344
345 movw r18, r24 ; *movhi | movw r18, r24 ; *movhi
346 movw r24, r22 ; *movhi | movw r24, r22 ; *movhi
347 ldi r22, 8 ; movqi_insn | ldi r22, 8 ; movqi_insn
348 .L13: | .L13:
349 movw r30, r18 ; *movhi | movw r30, r18 ; *movhi
350 eor r30, r24 ; *xorqi3 | eor r30, r24 ; *xorqi3
351 eor r31, r25 ; *xorqi3 | eor r31, r25 ; *xorqi3
352 mov r20, r30 ; *andhi3 | mov r20, r30 ; *andqi3
353 andi r20, 1<<7 | andi r20, 1<<7
354 clr r21 |
355 sbrs r30, 7 ; *sbrx_branchhi | sbrc r30, 7 ; *sbrx_branchhi
356 rjmp .+4 |
357 ldi r20, 79 ; movqi_insn | ldi r20, 79 ; movqi_insn
358 ldi r21, 0 ; movqi_insn |
359 eor r24, r20 ; *xorqi3 | eor r24, r20 ; *xorqi3
360 eor r25, r21 ; *xorqi3 |
361 lsl r18 ; *ashlhi3_const | lsl r18 ; *ashlhi3_const
362 rol r19 | rol r19
363 subi r22, 1 ; *op8.for.cczn.p| subi r22, 1 ; *op8.for.cczn.plus
364 brne .L13 ; branch_ZN | brne .L13 ; branch_ZN
365 ret | ret
366
367 #define SPDR (*(uint8_t volatile*) 0x2c) (5)
368
369 void fn_PR49807 (long big)
370 {
371 SPDR = big >> 24;
372 SPDR = big >> 16;
373 SPDR = big >> 8;
374 SPDR = big;
375 }
376
377 movw r20, r22 ; *movhi | movw r20, r22 ; *movhi
378 movw r22, r24 ; *movhi | movw r22, r24 ; *movhi
379 mov r24, r23 ; *ashrsi3_const |
380 clr r27 |
381 sbrc r24,7 |
382 com r27 |
383 mov r25, r27 |
384 mov r26, r27 |
385 out 0xc, r24 ; movqi_insn | out 0xc, r23 ; movqi_insn
386 movw r24, r22 ; *ashrsi3_const |
387 clr r27 |
388 sbrc r25, 7 |
389 com r27 |
390 mov r26, r27 |
391 out 0xc, r24 ; movqi_insn | out 0xc, r24 ; movqi_insn
392 clr r27 ; *ashrsi3_const |
393 sbrc r23, 7 |
394 dec r27 |
395 mov r26, r23 |
396 mov r25, r22 |
397 mov r24, r21 |
398 out 0xc, r24 ; movqi_insn | out 0xc, r21 ; movqi_insn
399 out 0xc, r20 ; movqi_insn | out 0xc, r20 ; movqi_insn
400 ret | ret
401
402 The insns of each basic block are traversed from first to last.
403 Each insn is optimized on its own, or may be fused with the
404 previous insn like in example (1).
405 As the insns are traversed, memento_t keeps track of known values
406 held in the GPRs (general purpse registers) R2 ... R31 by simulating
407 the effect of the current insn in memento_t.apply_insn().
408 The basic blocks are traversed in reverse post order so as to
409 maximize the chance that GPRs from all preceding blocks are known,
410 which is the case in example (2). The traversal of the basic block
411 is performed by bbinfo_t.optimize_one_function().
412 bbinfo_t.optimize_one_block() traverses the insns of a BB and tries
413 the following optimizations:
414
415 bbinfo_t::try_fuse_p
416 Try to fuse two 8-bit insns to one MOVW like in (1).
417
418 bbinfo_t::try_simplify_p
419 Only perform the simplest optimizations that don't impede the
420 traceability of the generated code, which are:
421 - Transform operations like Rn = Rn=0 ^ Rm to Rn = Rm.
422 - Remove insns that are no-ops like Rn = Rn ^ Rm=0.
423
424 bbinfo_t::try_bin_arg1_p
425 In insns like EOR Rn, arg1 where arg1 is known or is a reg that
426 dies in the insn, *and* there is a different register Rm that's
427 known to contain the same value, then arg1 is replaced with Rm.
428
429 bbinfo_t::try_split_ldi_p
430 Tries to simplify loads of constants like in examples (1), (2) and (3).
431 It may use arithmetic instructions like AND with registers that
432 are holding known values when this is profitable.
433
434 bbinfo_t::try_split_any_p
435 Split all insns where the operation can be performed on individual
436 bytes, like andsi3. In example (4) the andhi3 can be optimized
437 to an andqi3.
438
439 bbinfo_t::try_mem0_p
440 Try to fuse a mem = reg insn to mem = __zero_reg__.
441 This should only occur when -msplit-ldst is on, but may
442 also occur with pushes since push<mode>1 splits them.
443 */
444
445
446 // A basic block with additional information like the GPR state.
447 // The main entry point for the pass. Runs various strategies
448 // like try_fuse, try_simplify, try_bin_arg1, try_split_ldi, try_split_any
449 // depending on -mfuse-add=<0,11>.
450 struct bbinfo_t;
451
452 // Additional insn information on a REG = non-memory single_set insn
453 // for quick access. Only valid when the m_size member is non-zero.
454 struct insninfo_t;
455
456 // Helper classes with data needed by the try_xxx optimizers.
457 struct optimize_data_t;
458 struct insn_optimize_data_t;
459
460 // Records which GPRs R0 ... R31 are holding a known value,
461 // and which values these are.
462 struct memento_t;
463
464 // Abstract Interpretation of expressions.
465 // absint_val_t represents an 8-bit value that equals the content of
466 // some GPR, or equals some known value (or both, or none of them).
467 // absint_byte_t represents an 8-bit entity that is equivalent to
468 // an absint_val_t, or is equivalent to some (unary or binary) operation
469 // on absint_val_t's like NOT, AND, IOR, XOR that operate bit-wise (and
470 // hence also byte-wise).
471 // absint_t represents an array of absint_byte_t's. When some insn is applied
472 // to a GPR state, then memento_t.apply_insn() represents the RHS of
473 // a single_set as an absint_t, and then applies that result to the GPRs.
474 // For example, in int y = x << 8 the representation is x = [r25; r24]
475 // and RHS = [r24; 00].
476 struct absint_val_t;
477 class absint_byte_t;
478 struct absint_t;
479
480 // A ply_t is a potential step towards an optimal sequence to load a constant
481 // value into a multi-byte register. A ply_t loosely relates to one AVR
482 // instruction, but it may also represent a sequence of instructions.
483 // For example, loading a constant into a lower register when no sratch reg
484 // is available may take up to 4 instructions. There is no 1:1 correspondence
485 // to insns, either.
486 // try_split_ldi determines the best sequence of ply_t's by means of a
487 // brute-force search with tree pruning: It's much too complicated to
488 // construct a good sequence directly, but there are many conditions that
489 // good sequence will satisfy, implemented in bbinfo_t::find_plies.
490 struct ply_t;
491 struct plies_t;
492
493 // The maximal number of ply_t's in any conceivable optimal solution
494 // that is better than what a vanilla mov<mode> generates.
495 // This is 6 for modes <= 4 and 8 for modes == 8.
496 static constexpr int N_BEST_PLYS = 8;
497
498 #define FUSE_MOVE_MAX_MODESIZE 8
499
500 #include "avr-passes-fuse-move.h"
501
502 // Static members.
503
504 gprmask_t memento_t::fixed_regs_mask;
505
506 // Statistics.
507 int ply_t::n_ply_ts;
508 int ply_t::max_n_ply_ts;
509 int plies_t::max_n_plies;
510
511 bbinfo_t *bbinfo_t::bb_info;
512 int bbinfo_t::tick;
513 bbinfo_t::find_plies_data_t *bbinfo_t::fpd;
514
515 // Which optimizations should be performed.
516 bool bbinfo_t::try_fuse_p;
517 bool bbinfo_t::try_bin_arg1_p;
518 bool bbinfo_t::try_split_ldi_p;
519 bool bbinfo_t::try_split_any_p;
520 bool bbinfo_t::try_simplify_p;
521 bool bbinfo_t::use_arith_p;
522 bool bbinfo_t::use_set_some_p;
523 bool bbinfo_t::try_mem0_p;
524
525
526 // Abstract Interpretation of expressions.
527 // A bunch of absint_byte_t's.
528
529 struct absint_t
530 {
531 static constexpr int eq_size = FUSE_MOVE_MAX_MODESIZE;
532 std::array<absint_byte_t, eq_size> eq;
533
534 rtx xexp = NULL_RTX;
535 rtx xexp_new = NULL_RTX;
536
537 absint_byte_t &operator[] (int i)
538 {
539 gcc_assert (IN_RANGE (i, 0, absint_t::eq_size - 1));
540 return eq[i];
541 }
542
543 const absint_byte_t &operator[] (int i) const
544 {
545 gcc_assert (IN_RANGE (i, 0, absint_t::eq_size - 1));
546 return eq[i];
547 }
548
549 absint_t () {}
550
551 absint_t (rtx xold)
552 : xexp(xold)
553 {}
554
555 absint_t (rtx xold, rtx xnew, int n_bytes)
556 : xexp(xold), xexp_new(xnew)
557 {
558 gcc_assert (n_bytes <= eq_size);
559 if (xnew)
560 for (int i = 0; i < n_bytes; ++i)
561 eq[i].learn_val8 (avr_uint8 (xnew, i));
562 }
563
564 // CODE != UNKNOWN: Maximal index of a byte with code CODE, or -1.
565 // CODE == UNKNOWN: Maximal index of a byte with known CODE, or -1.
566 int max_knows (rtx_code code = UNKNOWN) const
567 {
568 for (int i = eq_size - 1; i >= 0; --i)
569 if ((code == UNKNOWN && ! eq[i].can (UNKNOWN))
570 || (code != UNKNOWN && eq[i].can (code)))
571 return i;
572 return -1;
573 }
574
575 // CODE != UNKNOWN: Maximal i such that all bytes < i have code CODE.
576 // CODE == UNKNOWN: Maximal i such that all bytes < i have code != UNKNOWN.
577 int end_knows (rtx_code code = UNKNOWN) const
578 {
579 for (int i = 0; i < eq_size; ++i)
580 if ((code == UNKNOWN && eq[i].can (UNKNOWN))
581 || (code != UNKNOWN && ! eq[i].can (code)))
582 return i;
583 return eq_size;
584 }
585
586 // Number of bytes for which there is usable information.
587 int popcount () const
588 {
589 int pop = 0;
590 for (int i = 0; i < eq_size; ++i)
591 pop += ! eq[i].can (UNKNOWN);
592 return pop;
593 }
594
595 // Get the value under the assumption that all eq[].val8 are known.
596 uint64_t get_value (int n_bytes, bool strict = true) const
597 {
598 gcc_assert (IN_RANGE (n_bytes, 1, eq_size));
599 gcc_assert (! strict || end_knows (CONST_INT) >= n_bytes);
600
601 uint64_t val = 0;
602 for (int i = n_bytes - 1; i >= 0; --i)
603 val = 256 * val + eq[i].val8 (strict);
604 return val;
605 }
606
607 // Get n-byte value as a const_int, or NULL_RTX when (partially) unknown.
608 rtx get_value_as_const_int (int n_bytes) const
609 {
610 gcc_checking_assert (gpr_regno_p (REG_24, n_bytes));
611
612 if (end_knows (CONST_INT) < n_bytes)
613 return NULL_RTX;
614
615 const uint64_t val = get_value (n_bytes);
616 const machine_mode mode = size_to_mode (n_bytes);
617
618 return gen_int_mode (val, mode);
619 }
620
621 // Find a 16-bit register that contains the same value like held
622 // in positions I1 and I2 (if any). Return 0 when nothing appropriate
623 // for a MOVW is found.
624 int reg16_with_value (int i1, int i2, const memento_t &memo) const
625 {
626 if (i1 == (i2 ^ 1))
627 {
628 const int lo8 = eq[i1 & ~1].val8 (false);
629 const int hi8 = eq[i1 | 1].val8 (false);
630 if (lo8 >= 0 && hi8 >= 0)
631 return memo.reg16_with_value (lo8, hi8, 0);
632 }
633 return 0;
634 }
635
636 // When X is a REG rtx with a known content as of MEMO, then return
637 // the respective value as a constant for mode MODE.
638 // If X is NULL_RTX, or not a REG, or not known, then return NULL_RTX.
639 static rtx maybe_fold (rtx x, const memento_t &memo)
640 {
641 int n_bytes;
642
643 if (x != NULL_RTX
644 && REG_P (x)
645 && (n_bytes = GET_MODE_SIZE (GET_MODE (x))) <= FUSE_MOVE_MAX_MODESIZE
646 && gpr_regno_p (REGNO (x), n_bytes))
647 {
648 rtx xval = memo.get_value_as_const_int (REGNO (x), n_bytes);
649 if (xval)
650 return avr_chunk (GET_MODE (x), xval, 0);
651 }
652
653 return NULL_RTX;
654 }
655
656 // Try to conclude about the bytes that comprise X. DEST_MODE is the
657 // context mode that is used when X is CONST_INT and has VOIDmode.
658 static absint_t explore (rtx x, const memento_t &memo,
659 machine_mode dest_mode = VOIDmode)
660 {
661 const rtx_code code = GET_CODE (x);
662 bool worth_dumping = dump_file && (dump_flags & TDF_FOLDING);
663
664 const machine_mode mode = GET_MODE (x) == VOIDmode
665 ? dest_mode
666 : GET_MODE (x);
667
668 const int n_bytes = mode == VOIDmode && CONST_INT_P (x)
669 ? absint_t::eq_size
670 : GET_MODE_SIZE (mode);
671
672 if (! IN_RANGE (n_bytes, 1, absint_t::eq_size))
673 return absint_t (x);
674
675 // Eat our own dog food as produced by try_plit_ldi.
676
677 rtx xop0 = BINARY_P (x) || UNARY_P (x) ? XEXP (x, 0) : NULL_RTX;
678 rtx xval0 = xop0 && CONST_INT_OR_FIXED_P (xop0)
679 ? xop0
680 : absint_t::maybe_fold (xop0, memo);
681
682 if (UNARY_P (x)
683 && REG_P (xop0)
684 && GET_MODE (xop0) == mode
685 && xval0)
686 {
687 rtx y = simplify_unary_operation (code, mode, xval0, mode);
688 if (y && CONST_INT_OR_FIXED_P (y))
689 return absint_t (x, y, n_bytes);
690 }
691
692 rtx xop1 = BINARY_P (x) ? XEXP (x, 1) : NULL_RTX;
693 rtx xval1 = xop1 && CONST_INT_OR_FIXED_P (xop1)
694 ? xop1
695 : absint_t::maybe_fold (xop1, memo);
696
697 if (BINARY_P (x)
698 && xval0 && xval1)
699 {
700 rtx y = simplify_binary_operation (code, mode, xval0, xval1);
701 if (y && CONST_INT_OR_FIXED_P (y))
702 return absint_t (x, y, n_bytes);
703 }
704
705 // No fold to a constant value was found:
706 // Look at the individual bytes more closely.
707
708 absint_t ai (x);
709
710 switch (code)
711 {
712 default:
713 worth_dumping = false;
714 break;
715
716 case REG:
717 if (END_REGNO (x) <= REG_32
718 && ! (regmask (x) & memento_t::fixed_regs_mask))
719 for (unsigned r = REGNO (x); r < END_REGNO (x); ++r)
720 {
721 ai[r - REGNO (x)].learn_regno (r);
722 if (memo.knows (r))
723 ai[r - REGNO (x)].learn_val8 (memo.value (r));
724 }
725 break;
726
727 CASE_CONST_UNIQUE:
728 ai = absint_t (x, x, n_bytes);
729 break;
730
731 case ASHIFT:
732 case ASHIFTRT:
733 case LSHIFTRT:
734 case ROTATE:
735 case ROTATERT:
736 if ((CONST_INT_P (xop1) && INTVAL (xop1) >= 8)
737 // DImode shift offsets for transparent calls are shipped in R16.
738 || n_bytes == 8)
739 ai = explore_shift (x, memo);
740 break;
741
742 case AND:
743 case IOR:
744 case XOR:
745 {
746 const absint_t ai0 = absint_t::explore (xop0, memo, mode);
747 const absint_t ai1 = absint_t::explore (xop1, memo, mode);
748 for (int i = 0; i < n_bytes; ++i)
749 ai[i] = absint_byte_t (code, ai0[i], ai1[i]);
750 }
751 break;
752
753 case NOT:
754 {
755 const absint_t ai0 = absint_t::explore (xop0, memo);
756 for (int i = 0; i < n_bytes; ++i)
757 ai[i] = absint_byte_t (NOT, ai0[i]);
758 }
759 break;
760
761 case ZERO_EXTEND:
762 case SIGN_EXTEND:
763 {
764 const absint_t ai0 = absint_t::explore (xop0, memo);
765 const int ai0_size = GET_MODE_SIZE (GET_MODE (xop0));
766 const absint_byte_t b_signs = ai0[ai0_size - 1].get_signs (code);
767 for (int i = 0; i < n_bytes; ++i)
768 ai[i] = i < ai0_size ? ai0[i] : b_signs;
769 }
770 break;
771
772 case PLUS:
773 case MINUS:
774 if (SCALAR_INT_MODE_P (mode)
775 || ALL_SCALAR_FIXED_POINT_MODE_P (mode))
776 {
777 const absint_t ai0 = absint_t::explore (xop0, memo, mode);
778 const absint_t ai1 = absint_t::explore (xop1, memo, mode);
779 if (code == MINUS)
780 for (int i = 0; i < n_bytes && ai1[i].val8 (false) == 0; ++i)
781 ai[i] = ai0[i];
782
783 if (code == PLUS)
784 for (int i = 0; i < n_bytes; ++i)
785 {
786 if (ai0[i].val8 (false) == 0)
787 ai[i] = ai1[i];
788 else if (ai1[i].val8 (false) == 0)
789 ai[i] = ai0[i];
790 else
791 {
792 ai[i] = absint_byte_t (code, ai0[i], ai1[i]);
793 break;
794 }
795 }
796
797 if (code == PLUS
798 && GET_CODE (xop0) == ZERO_EXTEND
799 && CONST_INT_P (xop1))
800 {
801 rtx exop = XEXP (xop0, 0);
802 int exsize = GET_MODE_SIZE (GET_MODE (exop));
803 rtx lo_xop1 = avr_chunk (GET_MODE (exop), xop1, 0);
804 if (lo_xop1 == const0_rtx)
805 for (int i = exsize; i < n_bytes; ++i)
806 ai[i] = ai1[i];
807 }
808 }
809 break; // PLUS, MINUS
810
811 case MULT:
812 if (GET_MODE (xop0) == mode
813 && SCALAR_INT_MODE_P (mode))
814 {
815 // The constant may be located in xop0's zero_extend...
816 const absint_t ai0 = absint_t::explore (xop0, memo, mode);
817 const absint_t ai1 = absint_t::explore (xop1, memo, mode);
818 const int end0 = ai0.end_knows (CONST_INT);
819 const int end1 = ai1.end_knows (CONST_INT);
820 const uint64_t mul0 = end0 > 0 ? ai0.get_value (end0) : 1;
821 const uint64_t mul1 = end1 > 0 ? ai1.get_value (end1) : 1;
822 // Shifting in off/8 zero bytes from the right.
823 const int off = mul0 * mul1 != 0 ? ctz_hwi (mul0 * mul1) : 0;
824 for (int i = 0; i < off / 8; ++i)
825 ai[i].learn_val8 (0);
826 }
827 break; // MULT
828
829 case BSWAP:
830 if (GET_MODE (xop0) == mode)
831 {
832 const absint_t ai0 = absint_t::explore (xop0, memo);
833 for (int i = 0; i < n_bytes; ++i)
834 ai[i] = ai0[n_bytes - 1 - i];
835 }
836 break;
837 } // switch code
838
839 if (worth_dumping)
840 {
841 avr_dump (";; AI.explore %C:%m ", code, mode);
842 ai.dump ();
843 }
844
845 for (int i = 0; i < n_bytes; ++i)
846 gcc_assert (ai[i].check ());
847
848 return ai;
849 }
850
851 // Helper for the method above.
852 static absint_t explore_shift (rtx x, const memento_t &memo)
853 {
854 absint_t ai (x);
855
856 const rtx_code code = GET_CODE (x);
857 const machine_mode mode = GET_MODE (x);
858 const int n_bytes = GET_MODE_SIZE (mode);
859
860 if (! BINARY_P (x))
861 return ai;
862
863 rtx xop0 = XEXP (x, 0);
864 rtx xop1 = XEXP (x, 1);
865
866 // Look at shift offsets of DImode more closely;
867 // they are in R16 for __lshrdi3 etc. Patch xop1 on success.
868 if (n_bytes == 8
869 && ! CONST_INT_P (xop1)
870 && GET_MODE (xop0) == mode)
871 {
872 const int n_off = GET_MODE_SIZE (GET_MODE (xop1));
873 const absint_t aoff = absint_t::explore (xop1, memo);
874 xop1 = aoff.get_value_as_const_int (n_off);
875 }
876
877 if (! xop1
878 || GET_MODE (xop0) != mode
879 || ! IN_RANGE (n_bytes, 1, FUSE_MOVE_MAX_MODESIZE)
880 || ! CONST_INT_P (xop1)
881 || ! IN_RANGE (INTVAL (xop1), 8, 8 * n_bytes - 1))
882 return ai;
883
884 const int off = INTVAL (xop1);
885 const absint_t ai0 = absint_t::explore (xop0, memo);
886
887 switch (GET_CODE (x))
888 {
889 default:
890 break;
891
892 case ASHIFT:
893 // Shifting in 0x00's from the right.
894 for (int i = 0; i < off / 8; ++i)
895 ai[i].learn_val8 (0);
896 break;
897
898 case LSHIFTRT:
899 case ASHIFTRT:
900 {
901 // Shifting in 0x00's or signs from the left.
902 absint_byte_t b_signs = ai0[n_bytes - 1].get_signs (GET_CODE (x));
903 for (int i = n_bytes - off / 8; i < n_bytes; ++i)
904 ai[i] = b_signs;
905 if (off == 8 * n_bytes - 1)
906 if (code == ASHIFTRT)
907 ai[0] = b_signs;
908 }
909 break;
910 }
911
912 if (off % 8 != 0
913 || ai0.popcount () == 0)
914 return ai;
915
916 // For shift offsets that are a multiple of 8, record the
917 // action on the constituent bytes.
918
919 // Bytes are moving left by this offset (or zero for "none").
920 const int boffL = select<int>()
921 : code == ROTATE || code == ASHIFT ? off / 8
922 : code == ROTATERT ? n_bytes - off / 8
923 : 0;
924
925 // Bytes are moving right by this offset (or zero for "none").
926 const int boffR = select<int>()
927 : code == ROTATERT || code == ASHIFTRT || code == LSHIFTRT ? off / 8
928 : code == ROTATE ? n_bytes - off / 8
929 : 0;
930
931 if (dump_flags & TDF_FOLDING)
932 {
933 avr_dump (";; AI.explore_shift %C:%m ", code, mode);
934 if (boffL)
935 avr_dump ("<< %d%s", 8 * boffL, boffL && boffR ? ", " : "");
936 if (boffR)
937 avr_dump (">> %d", 8 * boffR);
938 avr_dump ("\n");
939 }
940
941 if (boffL)
942 for (int i = 0; i < n_bytes - boffL; ++i)
943 ai[i + boffL] = ai0[i];
944
945 if (boffR)
946 for (int i = boffR; i < n_bytes; ++i)
947 ai[i - boffR] = ai0[i];
948
949 return ai;
950 }
951
952 void dump (const char *msg = nullptr, FILE *f = dump_file) const
953 {
954 if (f)
955 dump (NULL_RTX, msg, f);
956 }
957
958 void dump (rtx dest, const char *msg = nullptr, FILE *f = dump_file) const
959 {
960 if (f)
961 {
962 int regno = dest && REG_P (dest) ? REGNO (dest) : 0;
963
964 msg = msg && msg[0] ? msg : "AI=[%s]\n";
965 const char *const xs = strstr (msg, "%s");
966 gcc_assert (xs);
967
968 fprintf (f, "%.*s", (int) (xs - msg), msg);
969 for (int i = max_knows (); i >= 0; --i)
970 {
971 const int sub_regno = eq[i].regno (false /*nonstrict*/);
972 const bool nop = regno && sub_regno == regno + i;
973 eq[i].dump (nop ? "%s=nop" : "%s", f);
974 fprintf (f, "%s", i ? "; " : "");
975 }
976 fprintf (f, "%s", xs + strlen ("%s"));
977 }
978 }
979 }; // absint_t
980
981
982 // Information for a REG = non-memory single_set.
983
984 struct insninfo_t
985 {
986 // This is an insn that sets the m_size bytes of m_regno to either
987 // - A compile time constant m_isrc (m_code = CONST_INT), or
988 // - The contents of register number m_rsrc (m_code = REG).
989 int m_size = 0;
990 int m_regno;
991 int m_rsrc;
992 rtx_code m_code;
993 uint64_t m_isrc;
994 rtx_insn *m_insn = nullptr;
995 rtx m_set = NULL_RTX;
996 rtx m_src = NULL_RTX;
997 int m_scratch = 0; // 0 or the register number of a QImode scratch.
998 rtx_code m_old_code = UNKNOWN;
999
1000 // Knowledge about the bytes of the SET_SRC: A byte may have a known
1001 // value, may be known to equal some register (e.g. with BSWAP),
1002 // or both, or may be unknown.
1003 absint_t m_ai;
1004
1005 // May be set for binary operations.
1006 absint_byte_t m_new_src;
1007
1008 bool init1 (insn_optimize_data_t &, int max_size, const char *purpose);
1009
1010 // Upper bound for the cost (in words) of a move<mode> insn that
1011 // performs a REG = CONST_XXX = .m_isrc move of modesize .m_size.
1012 int cost () const;
1013 bool combine (const insninfo_t &prev, const insninfo_t &curr);
1014 int emit_insn () const;
1015
1016 bool needs_scratch () const
1017 {
1018 gcc_assert (m_code == CONST_INT);
1019
1020 for (int i = 0; i < m_size; ++i)
1021 if (AVRasm::ldi_needs_scratch (m_regno, m_isrc >> (8 * i)))
1022 return true;
1023
1024 return false;
1025 }
1026
1027 int hamming (const memento_t &memo) const
1028 {
1029 gcc_assert (m_code == CONST_INT);
1030
1031 int h = 0;
1032 for (int i = 0; i < m_size; ++i)
1033 h += ! memo.have_value (m_regno + i, 1, 0xff & (m_isrc >> (8 * i)));
1034
1035 return h;
1036 }
1037
1038 // Upper bound for the number of ply_t's of a solution, given Hamming
1039 // distance of HAMM (-1 for unknown).
1040 int n_best_plys (int hamm = -1) const
1041 {
1042 gcc_assert (m_code == CONST_INT);
1043
1044 if (m_size == 8)
1045 return (hamm >= 0 ? hamm : m_size);
1046 else if (hamm <= 4)
1047 return (hamm >= 0 ? hamm : m_size)
1048 // The following terms is the max number of MOVWs with a
1049 // Hamming difference of less than 2.
1050 + (AVR_HAVE_MOVW && m_regno < REG_14) * m_size / 2
1051 + (AVR_HAVE_MOVW && m_regno == REG_14) * std::max (0, m_size - 2)
1052 - (AVR_HAVE_MOVW && hamm == 4 && (uint32_t) m_isrc % 0x10001 == 0);
1053 else
1054 gcc_unreachable ();
1055 }
1056 }; // insninfo_t
1057
1058
1059 struct insn_optimize_data_t
1060 {
1061 // Known values held in GPRs prior to the action of .insn / .ii,
1062 memento_t &regs;
1063 rtx_insn *insn;
1064 insninfo_t ii;
1065 bool unused;
1066
1067 insn_optimize_data_t () = delete;
1068
1069 insn_optimize_data_t (memento_t &memo)
1070 : regs(memo)
1071 {}
1072 }; // insn_optimize_data_t
1073
1074 struct optimize_data_t
1075 {
1076 insn_optimize_data_t prev;
1077 insn_optimize_data_t curr;
1078
1079 // Number >= 0 of new insns that replace the curr insn and maybe also the
1080 // prev insn. -1 when no replacement has been found.
1081 int n_new_insns = -1;
1082
1083 // .prev will be removed provided we have (potentially zero) new insns.
1084 bool delete_prev_p = false;
1085
1086 // Ignore these GPRs when comparing the simulation results of
1087 // old and new insn sequences. Usually some scratch reg(s).
1088 gprmask_t ignore_mask = 0;
1089
1090 optimize_data_t () = delete;
1091
1092 optimize_data_t (memento_t &prev_regs, memento_t &curr_regs)
1093 : prev(prev_regs), curr(curr_regs)
1094 {}
1095
1096 bool try_fuse (bbinfo_t *);
1097 bool try_mem0 (bbinfo_t *);
1098 bool try_bin_arg1 (bbinfo_t *);
1099 bool try_simplify (bbinfo_t *);
1100 bool try_split_ldi (bbinfo_t *);
1101 bool try_split_any (bbinfo_t *);
1102 bool fail (const char *reason);
1103 bool emit_signs (int r_sign, gprmask_t);
1104 void emit_move_mask (int dest, int src, int n_bytes, gprmask_t &);
1105 rtx_insn *emit_sequence (basic_block, rtx_insn *);
1106 bool get_2ary_operands (rtx_code &, const absint_byte_t &,
1107 insn_optimize_data_t &, int r_dest,
1108 absint_val_t &, absint_val_t &, int &ex_cost);
1109 rtx_insn *emit_and_apply_move (memento_t &, rtx dest, rtx src);
1110
1111 // M2 is the state of GPRs as the sequence starts; M1 is the state one before.
1112 static void apply_sequence (const std::vector<rtx_insn *> &insns,
1113 memento_t &m1, memento_t &m2)
1114 {
1115 gcc_assert (insns.size () >= 1);
1116
1117 for (auto &i : insns)
1118 {
1119 m1 = m2;
1120 m2.apply_insn (i, false);
1121 }
1122 }
1123 }; // optimize_data_t
1124
1125
1126 // Emit INSNS before .curr.insn, replacing .curr.insn and also .prev.insn when
1127 // .delete_prev_p is on. Adjusts .curr.regs and .prev.regs accordingly.
1128 rtx_insn *
1129 optimize_data_t::emit_sequence (basic_block bb, rtx_insn *insns)
1130 {
1131 gcc_assert (n_new_insns >= 0);
1132
1133 // The old insns will be replaced by and simulated...
1134 const std::vector<rtx_insn *> old_insns = delete_prev_p
1135 ? std::vector<rtx_insn *> { prev.insn, curr.insn }
1136 : std::vector<rtx_insn *> { curr.insn };
1137
1138 // ...against the new insns.
1139 std::vector<rtx_insn *> new_insns;
1140 for (rtx_insn *i = insns; i; i = NEXT_INSN (i))
1141 new_insns.push_back (i);
1142
1143 rtx_insn *new_curr_insn;
1144
1145 memento_t &m1 = prev.regs;
1146 memento_t &m2 = curr.regs;
1147
1148 if (new_insns.empty ())
1149 {
1150 if (delete_prev_p)
1151 {
1152 m2 = m1;
1153 m1.known = 0;
1154 new_curr_insn = prev_nondebug_insn_bb (bb, prev.insn);
1155 }
1156 else
1157 new_curr_insn = prev.insn;
1158 }
1159 else
1160 {
1161 // We are going to emit at least one new insn. Simulate the effect of
1162 // the new sequence and compare it against the effect of the old one.
1163 // Both effects must be the same (modulo scratch regs).
1164
1165 memento_t n1 = m1;
1166 memento_t n2 = m2;
1167
1168 if (delete_prev_p)
1169 {
1170 m2 = m1, m1.known = 0;
1171 n2 = n1, n1.known = 0;
1172 }
1173
1174 avr_dump (";; Applying new route...\n");
1175 optimize_data_t::apply_sequence (new_insns, n1, n2);
1176
1177 avr_dump (";; Applying old route...\n");
1178 optimize_data_t::apply_sequence (old_insns, m1, m2);
1179 avr_dump ("\n");
1180
1181 if (! m2.equals (n2, ignore_mask))
1182 {
1183 // When we come here, then
1184 // - We have a genuine bug, and/or
1185 // - We did produce insns that are opaque to absint_t's explore().
1186 avr_dump ("INCOMPLETE APPLICATION:\n");
1187 m2.dump ("regs old route=%s\n\n");
1188 n2.dump ("regs new route=%s\n\n");
1189 avr_dump ("The new insns are:\n%L", insns);
1190
1191 fatal_insn ("incomplete application of insn", insns);
1192 }
1193
1194 // Use N1 and N2 as the new GPR states. Even though they are equal
1195 // modulo ignore_mask, N2 may know more about GPRs when it doesn't
1196 // clobber the scratch reg.
1197 m1 = n1;
1198 m2 = n2;
1199
1200 emit_insn_before (insns, curr.insn);
1201
1202 new_curr_insn = new_insns.back ();
1203 }
1204
1205 if (delete_prev_p)
1206 SET_INSN_DELETED (prev.insn);
1207
1208 SET_INSN_DELETED (curr.insn);
1209
1210 return new_curr_insn;
1211 }
1212
1213
1214 const pass_data avr_pass_data_fuse_move =
1215 {
1216 RTL_PASS, // type
1217 "", // name (will be patched)
1218 OPTGROUP_NONE, // optinfo_flags
1219 TV_MACH_DEP, // tv_id
1220 0, // properties_required
1221 0, // properties_provided
1222 0, // properties_destroyed
1223 0, // todo_flags_start
1224 TODO_df_finish | TODO_df_verify // todo_flags_finish
1225 };
1226
1227
1228 class avr_pass_fuse_move : public rtl_opt_pass
1229 {
1230 public:
1231 avr_pass_fuse_move (gcc::context *ctxt, const char *name)
1232 : rtl_opt_pass (avr_pass_data_fuse_move, ctxt)
1233 {
1234 this->name = name;
1235 }
1236
1237 unsigned int execute (function *func) final override
1238 {
1239 if (optimize > 0 && avropt_fuse_move > 0)
1240 {
1241 df_note_add_problem ();
1242 df_analyze ();
1243
1244 bbinfo_t::optimize_one_function (func);
1245 }
1246
1247 return 0;
1248 }
1249 }; // avr_pass_fuse_move
1250
1251
1252 // Append PLY to .plies[]. A SET or BLD ply may start a new sequence of
1253 // SETs or BLDs and gets assigned the overhead of the sequence like for an
1254 // initial SET or CLT instruction. A SET ply my be added in two flavours:
1255 // One that starts a sequence of single_sets, and one that represents the
1256 // payload of a set_some insn. MEMO is the GPR state prior to PLY.
1257 void
1258 plies_t::add (ply_t ply, const ply_t *prev, const memento_t &memo,
1259 bool maybe_set_some)
1260 {
1261 if (ply.code == SET)
1262 {
1263 if (prev && prev->code == SET)
1264 {
1265 // Proceed with the SET sequence flavour.
1266 ply.in_set_some = prev->in_set_some;
1267
1268 if (ply.in_set_some)
1269 ply.scratch = 0;
1270 else if (! ply.scratch && ply.needs_scratch ())
1271 ply.cost += 2;
1272 }
1273 else
1274 {
1275 // The 1st SET in a sequence. May use set_some to set
1276 // all bytes in one insn, or a bunch of single_sets.
1277
1278 // Route1: Bunch of single_sets.
1279 const int ply_cost = ply.cost;
1280 if (! ply.scratch && ply.needs_scratch ())
1281 ply.cost += 2;
1282 ply.in_set_some = false;
1283
1284 add (ply);
1285
1286 if (maybe_set_some)
1287 {
1288 // Route 2: One set_some: The 1st SET gets all the overhead.
1289 ply.scratch = 0;
1290 ply.cost = ply_cost + 1 + ! memo.known_dregno ();
1291 ply.in_set_some = true;
1292 }
1293 }
1294 } // SET
1295 else if (ply.is_bld ())
1296 {
1297 // The first BLD in a series of BLDs gets the extra costs
1298 // for the SET / CLT that precedes the BLDs.
1299 ply.cost += ! ply.is_same_bld (prev);
1300 }
1301
1302 add (ply);
1303 }
1304
1305
1306 // Emit insns for .plies[] and return the number of emitted insns.
1307 // The emitted insns represent the effect of II with MEMO, which
1308 // is the GPR knowledge before II is executed.
1309 int
1310 plies_t::emit_insns (const insninfo_t &ii, const memento_t &memo) const
1311 {
1312 int n_insns = 0;
1313
1314 for (int i = 0; i < n_plies; ++i)
1315 {
1316 const ply_t &p = plies[i];
1317
1318 // SETs and BLDs are dumped by their emit_xxxs().
1319 if (p.code != SET && ! p.is_bld ())
1320 p.dump ();
1321
1322 rtx src1 = NULL_RTX;
1323 rtx src2 = NULL_RTX;
1324 rtx dest = NULL_RTX;
1325 rtx xscratch = NULL_RTX;
1326 rtx_code code = p.code;
1327
1328 switch (p.code)
1329 {
1330 default:
1331 avr_dump ("\n\n;; Bad ply_t:\n");
1332 p.dump (i + 1);
1333 gcc_unreachable ();
1334 break;
1335
1336 case REG: // *movhi = MOVW; movqi_insn = MOV
1337 dest = gen_rtx_REG (p.size == 1 ? QImode : HImode, p.regno);
1338 src1 = gen_rtx_REG (p.size == 1 ? QImode : HImode, p.arg);
1339 break;
1340
1341 case SET: // movqi_insn = LDI, CLR; set_some = (LDI + MOV) ** size.
1342 i += emit_sets (ii, n_insns, memo, i) - 1;
1343 continue;
1344
1345 case MOD: // *ior<mode>3, *and<mode>3 = SET + BLD... / CLT + BLD...
1346 i += emit_blds (ii, n_insns, i) - 1;
1347 continue;
1348
1349 case MINUS: // *subqi3 = SUB
1350 case PLUS: // *addqi3 = ADD
1351 case AND: // *andqi3 = AND
1352 case IOR: // *iorqi3 = OR
1353 case XOR: // *xorqi3 = EOR
1354 dest = gen_rtx_REG (QImode, p.regno);
1355 src2 = gen_rtx_REG (QImode, p.arg);
1356 break;
1357
1358 case PRE_INC: // *addqi3 = INC
1359 case PRE_DEC: // *addqi3 = DEC
1360 code = PLUS;
1361 dest = gen_rtx_REG (QImode, p.regno);
1362 src2 = p.code == PRE_INC ? const1_rtx : constm1_rtx;
1363 break;
1364
1365 case NEG: // *negqi2 = NEG
1366 case NOT: // *one_cmplqi2 = COM
1367 dest = gen_rtx_REG (QImode, p.regno);
1368 src1 = dest;
1369 break;
1370
1371 case ROTATE: // *rotlqi3 = SWAP
1372 case ASHIFT: // *ashlqi3 = LSL
1373 case ASHIFTRT: // *ashrqi3 = ASR
1374 case LSHIFTRT: // *lshrqi3 = LSR
1375 dest = gen_rtx_REG (QImode, p.regno);
1376 src2 = GEN_INT (code == ROTATE ? 4 : 1);
1377 break;
1378
1379 case SS_PLUS: // *addhi3 = ADIW, SBIW
1380 code = PLUS;
1381 dest = gen_rtx_REG (HImode, p.regno);
1382 src2 = gen_int_mode (p.arg, HImode);
1383 break;
1384 } // switch p.code
1385
1386 gcc_assert (dest && (! src1) + (! src2) == 1);
1387
1388 rtx src = code == REG || code == SET
1389 ? src1
1390 : (src2
1391 ? gen_rtx_fmt_ee (code, GET_MODE (dest), dest, src2)
1392 : gen_rtx_fmt_e (code, GET_MODE (dest), src1));
1393
1394 emit_valid_move_clobbercc (dest, src, xscratch);
1395 n_insns += 1;
1396 }
1397
1398 return n_insns;
1399 }
1400
1401
1402 // Helper for .emit_insns(). Emit an ior<mode>3 or and<mode>3 insn
1403 // that's equivalent to a sequence of contiguous BLDs starting at
1404 // .plies[ISTART]. Updates N_INSNS according to the number of insns
1405 // emitted and returns the number of consumed plys in .plies[].
1406 int
1407 plies_t::emit_blds (const insninfo_t &ii, int &n_insns, int istart) const
1408 {
1409 const ply_t &first = plies[istart];
1410
1411 gcc_assert (ii.m_size <= 4);
1412 gcc_assert (first.is_bld ());
1413
1414 const rtx_code code = first.is_setbld () ? IOR : AND;
1415 const machine_mode mode = size_to_mode (ii.m_size);
1416
1417 // Determine mask and number of BLDs.
1418
1419 uint32_t mask = 0;
1420 int n_blds = 0;
1421
1422 for (int i = istart; i < n_plies; ++i, ++n_blds)
1423 {
1424 const ply_t &p = plies[i];
1425 if (! p.is_bld () || ! p.is_same_bld (& first))
1426 break;
1427
1428 // For AND, work on the 1-complement of the mask,
1429 // i.e. 1's specify which bits to clear.
1430 uint8_t mask8 = code == IOR ? p.arg : ~p.arg;
1431 mask |= mask8 << (8 * (p.regno - ii.m_regno));
1432 }
1433
1434 mask = GET_MODE_MASK (mode) & (code == IOR ? mask : ~mask);
1435
1436 if (dump_file)
1437 {
1438 fprintf (dump_file, ";; emit_blds[%d...%d] R%d[%d]%s=%0*x\n",
1439 istart, istart + n_blds - 1, ii.m_regno, ii.m_size,
1440 code == IOR ? "|" : "&", 2 * ii.m_size, (int) mask);
1441 }
1442
1443 for (int i = 0; i < n_blds; ++i)
1444 plies[i + istart].dump ();
1445
1446 rtx dest = gen_rtx_REG (mode, ii.m_regno);
1447 rtx src = gen_rtx_fmt_ee (code, mode, dest, gen_int_mode (mask, mode));
1448 rtx xscratch = mode == QImode ? NULL_RTX : gen_rtx_SCRATCH (QImode);
1449
1450 emit_valid_move_clobbercc (dest, src, xscratch);
1451 n_insns += 1;
1452
1453 return n_blds;
1454 }
1455
1456
1457 // Emit insns for a contiguous sequence of SET ply_t's starting at
1458 // .plies[ISTART]. Advances N_INSNS by the number of emitted insns.
1459 // MEMO ist the state of the GPRs before II is executed, where II
1460 // represents the insn under optimization.
1461 // The emitted insns are "movqi_insn" or "*reload_inqi"
1462 // when .plies[ISTART].in_set_some is not set, and one "set_some" insn
1463 // when .plies[ISTART].in_set_some is set.
1464 int
1465 plies_t::emit_sets (const insninfo_t &ii, int &n_insns, const memento_t &memo,
1466 int istart) const
1467 {
1468 gcc_assert (plies[istart].code == SET);
1469
1470 const bool in_set_some = plies[istart].in_set_some;
1471
1472 // Some d-regno that holds a compile-time constant, or 0.
1473 const int known_dregno = memo.known_dregno ();
1474
1475 // Determine number of contiguous SETs,
1476 // and sort them in ps[] such that smaller regnos come first.
1477
1478 const ply_t *ps[FUSE_MOVE_MAX_MODESIZE];
1479 int n_sets = 0;
1480
1481 for (int i = istart; i < n_plies && plies[i].code == SET; ++i)
1482 ps[n_sets++] = & plies[i];
1483
1484 if (dump_file)
1485 {
1486 fprintf (dump_file, ";; emit_sets[%d...%d] R%d[%d]=%0*" PRIx64,
1487 istart, istart + n_sets - 1, ii.m_regno, ii.m_size,
1488 2 * ii.m_size, ii.m_isrc);
1489 fprintf (dump_file, ", scratch=%s%d", "R" + ! ii.m_scratch, ii.m_scratch);
1490 fprintf (dump_file, ", known_dreg=%s%d, set_some=%d\n",
1491 "R" + ! known_dregno, known_dregno, in_set_some);
1492 }
1493
1494 for (int i = 0; i < n_sets; ++i)
1495 ps[i]->dump ();
1496
1497 // Sort. This is most useful on regs like (reg:SI REG_14).
1498 for (int i = 0; i < n_sets - 1; ++i)
1499 for (int j = i + 1; j < n_sets; ++j)
1500 if (ps[i]->regno > ps[j]->regno)
1501 std::swap (ps[i], ps[j]);
1502
1503 // Prepare operands.
1504 rtx dst[FUSE_MOVE_MAX_MODESIZE];
1505 rtx src[FUSE_MOVE_MAX_MODESIZE];
1506 for (int i = 0; i < n_sets; ++i)
1507 {
1508 dst[i] = gen_rtx_REG (QImode, ps[i]->regno);
1509 src[i] = gen_int_mode (ps[i]->arg, QImode);
1510 }
1511
1512 if (in_set_some)
1513 {
1514 // Emit a "set_some" insn that sets all of the collected 8-bit SETs.
1515 // This is a parallel with n_sets QImode SETs as payload.
1516
1517 gcc_assert (! known_dregno || memo.knows (known_dregno));
1518
1519 // A scratch reg...
1520 rtx op1 = known_dregno
1521 ? gen_rtx_REG (QImode, known_dregno)
1522 : const0_rtx;
1523 // ...with a known content, so it can be restored without saving.
1524 rtx op2 = known_dregno
1525 ? gen_int_mode (memo.values[known_dregno], QImode)
1526 : const0_rtx;
1527 // Target register envelope.
1528 rtx op3 = GEN_INT (ii.m_regno);
1529 rtx op4 = GEN_INT (ii.m_size);
1530
1531 // Payload.
1532 for (int i = 0; i < n_sets; ++i)
1533 dst[i] = gen_rtx_SET (dst[i], src[i]);
1534
1535 rtvec vec = gen_rtvec (5 + n_sets,
1536 gen_rtx_USE (VOIDmode, op1),
1537 gen_rtx_USE (VOIDmode, op2),
1538 gen_rtx_USE (VOIDmode, op3),
1539 gen_rtx_USE (VOIDmode, op4),
1540 gen_rtx_CLOBBER (VOIDmode, cc_reg_rtx),
1541 dst[0], dst[1], dst[2], dst[3]);
1542 rtx pattern = gen_rtx_PARALLEL (VOIDmode, vec);
1543
1544 emit_valid_insn (pattern);
1545 n_insns += 1;
1546 }
1547 else
1548 {
1549 // Emit a bunch of movqi_insn / *reload_inqi insns.
1550
1551 for (int i = 0; i < n_sets; ++i)
1552 if (ii.m_scratch
1553 && AVRasm::constant_cost (SET, ps[i]->regno, ps[i]->arg) > 1)
1554 {
1555 rtx scratch = gen_rtx_REG (QImode, ii.m_scratch);
1556 bool use_reload_inqi = true;
1557 if (use_reload_inqi)
1558 {
1559 emit_valid_move_clobbercc (dst[i], src[i], scratch);
1560 n_insns += 1;
1561 }
1562 else
1563 {
1564 emit_valid_move_clobbercc (scratch, src[i]);
1565 emit_valid_move_clobbercc (dst[i], scratch);
1566 n_insns += 2;
1567 }
1568 }
1569 else
1570 {
1571 emit_valid_move_clobbercc (dst[i], src[i]);
1572 n_insns += 1;
1573 }
1574 }
1575
1576 return n_sets;
1577 }
1578
1579
1580 // Try to find an operation such that Y = op (X).
1581 // Shifts and rotates are regarded as unary operaions with
1582 // an implied 2nd operand or 1 or 4, respectively.
1583 static rtx_code
1584 find_arith (uint8_t y, uint8_t x)
1585 {
1586 #define RETIF(ex, code) y == (0xff & (ex)) ? code
1587 return select<rtx_code>()
1588 : RETIF (x + 1, PRE_INC)
1589 : RETIF (x - 1, PRE_DEC)
1590 : RETIF ((x << 4) | (x >> 4), ROTATE)
1591 : RETIF (-x, NEG)
1592 : RETIF (~x, NOT)
1593 : RETIF (x >> 1, LSHIFTRT)
1594 : RETIF (x << 1, ASHIFT)
1595 : RETIF ((x >> 1) | (x & 0x80), ASHIFTRT)
1596 : UNKNOWN;
1597 #undef RETIF
1598 }
1599
1600
1601 // Try to find an operation such that Z = X op X.
1602 static rtx_code
1603 find_arith2 (uint8_t z, uint8_t x, uint8_t y)
1604 {
1605 #define RETIF(ex, code) z == (0xff & (ex)) ? code
1606 return select<rtx_code>()
1607 : RETIF (x + y, PLUS)
1608 : RETIF (x - y, MINUS)
1609 : RETIF (x & y, AND)
1610 : RETIF (x | y, IOR)
1611 : RETIF (x ^ y, XOR)
1612 : UNKNOWN;
1613 #undef RETIF
1614 }
1615
1616
1617 // Add plies to .plies[] that represent a MOVW, but only ones that reduce
1618 // the Hamming distance from REGNO[SIZE] to VAL by exactly DHAMM.
1619 void
1620 plies_t::add_plies_movw (int regno, int size, uint64_t val,
1621 int dhamm, const memento_t &memo)
1622 {
1623 if (! AVR_HAVE_MOVW || size < 2)
1624 return;
1625
1626 for (int i = 0; i < size - 1; i += 2)
1627 {
1628 // MOVW that sets less than 2 regs to the target value is
1629 // not needed for the upper regs.
1630 if (dhamm != 2 && regno + i >= REG_16)
1631 continue;
1632
1633 const uint16_t val16 = val >> (8 * i);
1634 const uint8_t lo8 = val16;
1635 const uint8_t hi8 = val16 >> 8;
1636
1637 // When one of the target bytes is already as expected, then
1638 // no MOVW is needed for an optimal sequence.
1639 if (memo.have_value (regno + i, 1, lo8)
1640 || memo.have_value (regno + i + 1, 1, hi8))
1641 continue;
1642
1643 const int h_old = memo.hamming (regno + i, 2, val16);
1644
1645 // Record MOVWs that reduce the Hamming distance by DHAMM as requested.
1646 for (int j = FIRST_GPR; j < REG_32; j += 2)
1647 if (j != regno + i
1648 && memo.knows (j, 2))
1649 {
1650 const int h_new = memo.hamming (j, 2, val16);
1651 if (h_new == h_old - dhamm)
1652 add (ply_t { regno + i, 2, REG, j, 1, dhamm });
1653 }
1654 }
1655 }
1656
1657
1658 // Set PS to plys that reduce the Hamming distance from II.m_regno to
1659 // compile-time constant II.m_isrc by 2, 1 or 0. PREV is NULL or points
1660 // to a previous ply_t. MEMO is the GPR state after PREV and prior to the
1661 // added plys.
1662 void
1663 bbinfo_t::get_plies (plies_t &ps, const insninfo_t &ii, const memento_t &memo,
1664 const ply_t *prev)
1665 {
1666 ps.reset ();
1667
1668 fpd->n_get_plies += 1;
1669
1670 const bool maybe_set_some = (bbinfo_t::use_set_some_p && ii.needs_scratch ());
1671
1672 // Start with cheap plies, then continue to more expensive ones.
1673 const int regno = ii.m_regno;
1674 const int size = ii.m_size;
1675 const uint64_t val = ii.m_isrc;
1676
1677 // Find MOVW with a Hamming delta of 2.
1678 ps.add_plies_movw (regno, size, val, 2, memo);
1679
1680 // Find ADIW / SBIW
1681 if (AVR_HAVE_ADIW && size >= 2)
1682 for (int i = 0; i < size - 1; i += 2)
1683 if (regno + i >= REG_24
1684 && memo.knows (regno + i, 2))
1685 {
1686 const int16_t value16 = memo[regno + i] + 256 * memo[regno + i + 1];
1687 const int16_t lo16 = val >> (8 * i);
1688 const int16_t delta = lo16 - value16;
1689 const uint8_t lo8 = val >> (8 * i);
1690 const uint8_t hi8 = val >> (8 * i + 8);
1691 if (IN_RANGE (delta, -63, 63)
1692 && lo8 != memo[regno + i]
1693 && hi8 != memo[regno + i + 1])
1694 {
1695 ps.add (ply_t { regno + i, 2, SS_PLUS, delta, 1, 2 });
1696 }
1697 }
1698
1699 // Find 1-reg plies. In an optimal sequence, each 1-reg ply will decrease
1700 // the Hamming distance. Thus we only have to consider plies that set
1701 // one of the target bytes to the target value VAL. Start with the
1702 // high registers since that is the canonical order when two plies commute.
1703
1704 for (int i = size - 1; i >= 0; --i)
1705 {
1706 const uint8_t val8 = val >> (8 * i);
1707
1708 // Nothing to do for this byte when its value is already as desired.
1709 if (memo.have_value (regno + i, 1, val8))
1710 continue;
1711
1712 // LDI or CLR.
1713 if (regno + i >= REG_16 || val8 == 0)
1714 ps.add (ply_t { regno + i, 1, SET, val8, 1 }, prev, memo,
1715 maybe_set_some);
1716
1717 // We only may need to MOV non-zero values since there is CLR,
1718 // and only when there is no LDI.
1719 if (val8 != 0
1720 && regno + i < REG_16)
1721 {
1722 // MOV where the source register is one of the target regs.
1723 for (int j = 0; j < size; ++j)
1724 if (j != i)
1725 if (memo.have_value (regno + j, 1, val8))
1726 ps.add (ply_t { regno + i, 1, REG, regno + j, 1 });
1727
1728 // MOV where the source register is not a target reg.
1729 // FIXME: ticks.
1730 for (int j = FIRST_GPR; j < REG_32; ++j)
1731 if (! IN_RANGE (j, regno, regno + size - 1))
1732 if (memo.have_value (j, 1, val8))
1733 ps.add (ply_t { regno + i, 1, REG, j, 1 });
1734
1735 // LDI + MOV.
1736 if (regno + i < REG_16 && val8 != 0)
1737 {
1738 ply_t p { regno + i, 1, SET, val8, 2 };
1739 p.scratch = ii.m_scratch;
1740 ps.add (p, prev, memo, maybe_set_some);
1741 }
1742 }
1743 }
1744
1745 // Arithmetic like INC, DEC or ASHIFT.
1746 for (int i = size - 1; i >= 0; --i)
1747 if (bbinfo_t::use_arith_p
1748 && regno + i < REG_16
1749 && memo.knows (regno + i))
1750 {
1751 const uint8_t y = val >> (8 * i);
1752 const uint8_t x = memo[regno + i];
1753 rtx_code code;
1754
1755 if (y == 0 || y == x)
1756 continue;
1757
1758 // INC, DEC, SWAP, LSL, NEG, ...
1759 if (UNKNOWN != (code = find_arith (y, x)))
1760 {
1761 ps.add (ply_t { regno + i, 1, code, x /* dummy */, 1 });
1762 continue;
1763 }
1764
1765 // ADD, AND, ...
1766 for (int r = FIRST_GPR; r < REG_32; ++r)
1767 if (r != regno + i
1768 && memo.knows (r)
1769 && memo[r] != 0
1770 && UNKNOWN != (code = find_arith2 (y, x, memo[r])))
1771 {
1772 ps.add (ply_t { regno + i, 1, code, r, 1 });
1773 }
1774
1775 if (size < 2 || size > 4)
1776 continue;
1777
1778 // SET + BLD
1779 if ((x & y) == x && popcount_hwi (x ^ y) == 1)
1780 ps.add (ply_t { regno + i, 1, MOD, x ^ y, 1 },
1781 prev, memo, maybe_set_some);
1782
1783 // CLT + BLD
1784 if ((x & y) == y && popcount_hwi (x ^ y) == 1)
1785 ps.add (ply_t { regno + i, 1, MOD, x ^ y ^ 0xff, 1 },
1786 prev, memo, maybe_set_some);
1787 }
1788
1789 if (bbinfo_t::use_arith_p
1790 // For 8-byte values, don't use ply_t's with only a partial reduction
1791 // of the hamming distance.
1792 && size <= 4)
1793 {
1794 // Find MOVW with a Hamming delta of 1, then 0.
1795 ps.add_plies_movw (regno, size, val, 1, memo);
1796 ps.add_plies_movw (regno, size, val, 0, memo);
1797 }
1798
1799 plies_t::max_n_plies = std::max (plies_t::max_n_plies, ps.n_plies);
1800 }
1801
1802
1803 // Try to combine two 8-bit insns PREV and CURR that (effectively)
1804 // are REG = CONST_INT to one 16-bit such insn. Returns true on success.
1805 bool
1806 insninfo_t::combine (const insninfo_t &prev, const insninfo_t &curr)
1807 {
1808 if (prev.m_size == 1 && curr.m_size == 1
1809 && prev.m_regno == (1 ^ curr.m_regno)
1810 && curr.m_code == CONST_INT
1811 && prev.m_code == CONST_INT)
1812 {
1813 m_regno = curr.m_regno & ~1;
1814 m_code = CONST_INT;
1815 m_size = 2;
1816 m_scratch = std::max (curr.m_scratch, prev.m_scratch);
1817 m_isrc = m_regno == prev.m_regno
1818 ? (uint8_t) prev.m_isrc + 256 * (uint8_t) curr.m_isrc
1819 : (uint8_t) curr.m_isrc + 256 * (uint8_t) prev.m_isrc;
1820
1821 return true;
1822 }
1823
1824 return false;
1825 }
1826
1827
1828 // Return the cost (in terms of words) of the respective mov<mode> insn.
1829 // This can be used as an upper bound for the ply_t's cost.
1830 int
1831 insninfo_t::cost () const
1832 {
1833 if (m_code != CONST_INT)
1834 return m_size;
1835
1836 if (m_regno >= REG_16 || m_isrc == 0)
1837 return m_size
1838 // MOVW can save one instruction.
1839 - (AVR_HAVE_MOVW && m_size == 4 && (uint32_t) m_isrc % 0x10001 == 0);
1840
1841 // LDI + MOV to a lower reg.
1842 if (m_scratch && m_size == 1)
1843 return 2;
1844
1845 if (m_size == 8)
1846 {
1847 int len = m_size;
1848 for (int i = 0; i < m_size; ++i)
1849 len += m_regno + i < REG_16 && (0xff & (m_isrc >> (8 * i))) != 0;
1850 return len;
1851 }
1852
1853 // All other cases are complicated. Ask the output oracle.
1854 const machine_mode mode = size_to_mode (m_size);
1855 rtx xscratch = m_scratch ? all_regs_rtx[m_scratch] : NULL_RTX;
1856 rtx xop[] = { gen_rtx_REG (mode, m_regno), gen_int_mode (m_isrc, mode) };
1857 int len;
1858 if (m_size == 4)
1859 output_reload_insisf (xop, xscratch, &len);
1860 else
1861 output_reload_in_const (xop, xscratch, &len, false);
1862
1863 return len;
1864 }
1865
1866 // Emit the according REG = REG-or-CONST_INT insn. Returns 1 or aborts
1867 // when the insn is not of that form.
1868 int
1869 insninfo_t::emit_insn () const
1870 {
1871 int n_insns = 0;
1872
1873 machine_mode mode = size_to_mode (m_size);
1874 rtx xsrc = NULL_RTX;
1875 rtx xscratch = NULL_RTX;
1876
1877 gcc_assert (m_size > 0);
1878
1879 switch (m_code)
1880 {
1881 default:
1882 gcc_unreachable ();
1883
1884 case CONST_INT:
1885 xsrc = gen_int_mode (m_isrc, mode);
1886 if (m_scratch && m_regno < REG_16)
1887 xscratch = gen_rtx_REG (QImode, m_scratch);
1888 break;
1889
1890 case REG:
1891 gcc_assert (gpr_regno_p (m_rsrc, m_size));
1892 if (m_regno != m_rsrc)
1893 xsrc = gen_rtx_REG (mode, m_rsrc);
1894 break;
1895 }
1896
1897 if (xsrc)
1898 {
1899 rtx dest = gen_rtx_REG (mode, m_regno);
1900 emit_valid_move_clobbercc (dest, xsrc, xscratch);
1901 n_insns += 1;
1902 }
1903
1904 return n_insns;
1905 }
1906
1907
1908 // Entering a basic block means combining known register values from
1909 // all incoming BBs.
1910 void
1911 bbinfo_t::enter ()
1912 {
1913 avr_dump ("\n;; Entering [bb %d]\n", bb->index);
1914
1915 gcc_assert (! done);
1916
1917 edge e;
1918 edge_iterator ei;
1919 gprmask_t pred_known_mask = ~0u;
1920 bbinfo_t *bbi = nullptr;
1921
1922 // A quick iteration over all predecessors / incoming edges to reveal
1923 // whether this BB is worth a closer look.
1924 FOR_EACH_EDGE (e, ei, bb->preds)
1925 {
1926 basic_block pred = e->src;
1927 bbi = & bb_info[pred->index];
1928
1929 pred_known_mask &= bbi->regs.known;
1930
1931 if (dump_file)
1932 {
1933 avr_dump (";; [bb %d] <- [bb %d] ", e->dest->index, e->src->index);
1934 if (bbi->done)
1935 bbi->regs.dump ();
1936 else
1937 avr_dump (" (unknown)\n");
1938 }
1939 }
1940
1941 // Only if all predecessors have already been handled, we can
1942 // have known values as we are entering the current BB.
1943 if (pred_known_mask != 0
1944 && bbi != nullptr)
1945 {
1946 // Initialize current BB info from BI, an arbitrary predecessor.
1947
1948 regs = bbi->regs;
1949
1950 // Coalesce the output values from all predecessing BBs. At the
1951 // start of the current BB, a value is only known if it is known
1952 // in *all* predecessors and *all* these values are the same.
1953 FOR_EACH_EDGE (e, ei, bb->preds)
1954 {
1955 regs.coalesce (bb_info[e->src->index].regs);
1956 }
1957 }
1958
1959 if (dump_file)
1960 {
1961 avr_dump (";; [bb %d] known at start: ", bb->index);
1962 if (regs.known)
1963 regs.dump ();
1964 else
1965 avr_dump (" (none)\n");
1966 avr_dump ("\n");
1967 }
1968 }
1969
1970
1971 void
1972 bbinfo_t::leave ()
1973 {
1974 done = true;
1975
1976 if (dump_file)
1977 fprintf (dump_file, ";; Leaving [bb %d]\n\n", bb->index);
1978 }
1979
1980
1981 /* Initialize according to INSN which is a 1-byte single_set that's
1982 (effectively) a reg = reg or reg = const move. INSN may be the result
1983 of the current pass's optimization, e.g. something like INC R2 where R2
1984 has a known content. MEMO is the state prior to INSN. Only CONST
1985 cases are recorded; plus cases that are non-trivial for example when
1986 an XOR decays to a move. */
1987
1988 bool
1989 insninfo_t::init1 (insn_optimize_data_t &iod, int max_size,
1990 const char *purpose = "")
1991 {
1992 m_size = 0;
1993 m_insn = iod.insn;
1994 m_old_code = UNKNOWN;
1995 iod.unused = false;
1996
1997 if (! iod.insn
1998 || ! (m_set = single_set_with_scratch (iod.insn, m_scratch)))
1999 return false;
2000
2001 rtx dest = SET_DEST (m_set);
2002 machine_mode mode = GET_MODE (dest);
2003 const int n_bytes = GET_MODE_SIZE (mode);
2004 max_size = std::min (max_size, FUSE_MOVE_MAX_MODESIZE);
2005
2006 if (! REG_P (dest)
2007 || END_REGNO (dest) > REG_32
2008 || n_bytes > max_size)
2009 return false;
2010
2011 // Omit insns that (explicitly) touch fixed GPRs in any way.
2012 using elt0_getter_HRS = elt0_getter<HARD_REG_SET, HARD_REG_ELT_TYPE>;
2013 HARD_REG_SET hregs;
2014 CLEAR_HARD_REG_SET (hregs);
2015 find_all_hard_regs (PATTERN (iod.insn), & hregs);
2016 if (memento_t::fixed_regs_mask & (gprmask_t) elt0_getter_HRS::get (hregs))
2017 {
2018 avr_dump (";; %sinit1 has fixed GPRs\n", purpose);
2019 return false;
2020 }
2021
2022 if ((iod.unused = find_reg_note (iod.insn, REG_UNUSED, dest)))
2023 return false;
2024
2025 m_src = SET_SRC (m_set);
2026 m_regno = REGNO (dest);
2027 const rtx_code src_code = GET_CODE (m_src);
2028
2029 m_ai = absint_t::explore (m_src, iod.regs, mode);
2030
2031 if (m_ai.popcount ())
2032 {
2033 if (m_ai.end_knows (CONST_INT) >= n_bytes)
2034 {
2035 m_code = CONST_INT;
2036 m_old_code = CONSTANT_P (m_src) ? UNKNOWN : src_code;
2037 m_isrc = m_ai.get_value (n_bytes);
2038 m_size = n_bytes;
2039 }
2040 else if (! REG_P (m_src)
2041 && n_bytes == 1
2042 && m_ai.end_knows (REG) >= n_bytes)
2043 {
2044 m_code = REG;
2045 m_old_code = src_code;
2046 m_rsrc = m_ai[0].regno ();
2047 m_size = n_bytes;
2048 }
2049 else if (n_bytes == 1)
2050 {
2051 absint_byte_t &aib = m_new_src;
2052 aib = m_ai[0].find_alternative_binary (iod.regs);
2053
2054 if (aib.arity () == 2
2055 && aib.arg (0).regno == m_regno)
2056 {
2057 m_old_code = src_code;
2058 m_code = aib.get_code ();
2059 m_size = n_bytes;
2060 }
2061 }
2062 else if (n_bytes >= 2
2063 && m_ai.end_knows (VALUE) >= n_bytes)
2064 {
2065 m_code = src_code;
2066 m_size = n_bytes;
2067 }
2068
2069 if (dump_file && m_size != 0)
2070 {
2071 avr_dump (";; %sinit1 (%C", purpose,
2072 m_old_code ? m_old_code : m_code);
2073 if (m_old_code)
2074 avr_dump ("-> %C", m_code);
2075 avr_dump (") insn %d to R%d[%d] := %C:%m = ", INSN_UID (iod.insn),
2076 m_regno, n_bytes, src_code, mode);
2077
2078 m_ai.dump (dest);
2079
2080 if (dump_flags & TDF_FOLDING)
2081 avr_dump ("\n");
2082 }
2083 }
2084
2085 return m_size != 0;
2086 }
2087
2088
2089 // The private worker for .apply_insn().
2090 void
2091 memento_t::apply_insn1 (rtx_insn *insn, bool unused)
2092 {
2093 gcc_assert (NONDEBUG_INSN_P (insn));
2094
2095 if (INSN_CODE (insn) == CODE_FOR_set_some)
2096 {
2097 // This insn only sets some selected bytes of register $3 of
2098 // modesize $4. If non-0, then $1 is a QImode scratch d-reg with
2099 // a known value of $2.
2100
2101 const auto &xop = recog_data.operand;
2102 extract_insn (insn);
2103 gcc_assert (recog_data.n_operands == 7);
2104 gcc_assert (set_some_operation (xop[0], VOIDmode));
2105
2106 const rtx &xscratch = xop[1];
2107 const rtx &xscratch_value = xop[2];
2108 const int sets_start = 5;
2109
2110 for (int i = sets_start; i < XVECLEN (xop[0], 0); ++i)
2111 {
2112 rtx xset = XVECEXP (xop[0], 0, i);
2113 avr_dump (";; set_some %r = %r\n", XEXP (xset, 0), XEXP (xset, 1));
2114 set_values (XEXP (xset, 0), XEXP (xset, 1));
2115 }
2116
2117 if (REG_P (xscratch))
2118 {
2119 avr_dump (";; set_some %r = %r restore\n", xscratch, xscratch_value);
2120 set_values (xscratch, xscratch_value);
2121 }
2122
2123 return;
2124 } // CODE_FOR_set_some
2125
2126 memento_t mold = *this;
2127
2128 // When insn changes a register in whatever way, set it to "unknown".
2129
2130 HARD_REG_SET rset;
2131 find_all_hard_reg_sets (insn, &rset, true /* implicit */);
2132 (*this) &= ~rset;
2133
2134 rtx set = single_set (insn);
2135 rtx dest;
2136
2137 if (! set
2138 || ! REG_P (dest = SET_DEST (set))
2139 || END_REGNO (dest) > REG_32
2140 || (regmask (dest) & memento_t::fixed_regs_mask))
2141 return;
2142
2143 rtx src = SET_SRC (set);
2144 const rtx_code src_code = GET_CODE (src);
2145 const machine_mode mode = GET_MODE (dest);
2146 const int n_bytes = GET_MODE_SIZE (mode);
2147
2148 // Insns that are too complicated or have a poor yield.
2149 // Just record which regs are clobberd / changed.
2150 if (n_bytes > FUSE_MOVE_MAX_MODESIZE
2151 || MEM_P (src)
2152 || (REG_P (src) && END_REGNO (src) > REG_32))
2153 {
2154 // Comparisons may clobber the compared reg when it is unused after.
2155 if (src_code == COMPARE
2156 && REG_P (XEXP (src, 0))
2157 && CONSTANT_P (XEXP (src, 1)))
2158 {
2159 rtx reg = XEXP (src, 0);
2160 for (unsigned r = REGNO (reg); r < END_REGNO (reg); ++r)
2161 set_unknown (r);
2162 }
2163 return;
2164 }
2165
2166 if (unused)
2167 return;
2168
2169 // Simulate the effect of some selected insns that are likely to produce
2170 // or propagate known values.
2171
2172 // Get an abstract representation of src. Bytes may be unknown,
2173 // known to equal some 8-bit compile-time constant (CTC) value,
2174 // or are known to equal some 8-bit register.
2175 // TODO: Currently, only the ai[].val8 knowledge ist used.
2176 // What's the best way to make use of ai[].regno ?
2177
2178 absint_t ai = absint_t::explore (src, mold, mode);
2179
2180 if (ai.popcount ())
2181 {
2182 avr_dump (";; apply_insn %d R%d[%d] := %C:%m = ", INSN_UID (insn),
2183 REGNO (dest), n_bytes, src_code, mode);
2184 ai.dump ();
2185
2186 for (int i = 0; i < n_bytes; ++i)
2187 if (ai[i].can (CONST_INT))
2188 set_value (i + REGNO (dest), ai[i].val8 ());
2189 }
2190 }
2191
2192
2193 void
2194 memento_t::apply (const ply_t &p)
2195 {
2196 if (p.is_movw ())
2197 {
2198 copy_value (p.regno, p.arg);
2199 copy_value (p.regno + 1, p.arg + 1);
2200 }
2201 else if (p.is_adiw ())
2202 {
2203 int val = p.arg + values[p.regno] + 256 * values[1 + p.regno];
2204 set_value (p.regno, val);
2205 set_value (p.regno + 1, val >> 8);
2206 }
2207 else if (p.size == 1)
2208 {
2209 switch (p.code)
2210 {
2211 default:
2212 gcc_unreachable ();
2213 break;
2214
2215 case REG:
2216 copy_value (p.regno, p.arg);
2217 break;
2218
2219 case SET:
2220 set_value (p.regno, p.arg);
2221 if (p.scratch >= REG_16)
2222 set_unknown (p.scratch);
2223 break;
2224
2225 case MOD: // BLD
2226 gcc_assert (knows (p.regno));
2227 if (popcount_hwi (p.arg) == 1)
2228 values[p.regno] |= p.arg;
2229 else if (popcount_hwi (p.arg) == 7)
2230 values[p.regno] &= p.arg;
2231 else
2232 gcc_unreachable ();
2233 break;
2234
2235 #define DO_ARITH1(code, expr) \
2236 case code: \
2237 gcc_assert (knows (p.regno)); \
2238 { \
2239 const int x = values[p.regno]; \
2240 set_value (p.regno, expr); \
2241 } \
2242 break
2243
2244 #define DO_ARITH2(code, expr) \
2245 case code: \
2246 gcc_assert (knows (p.regno)); \
2247 gcc_assert (knows (p.arg)); \
2248 { \
2249 const int x = values[p.regno]; \
2250 const int y = values[p.arg]; \
2251 set_value (p.regno, expr); \
2252 } \
2253 break
2254
2255 DO_ARITH1 (NEG, -x);
2256 DO_ARITH1 (NOT, ~x);
2257 DO_ARITH1 (PRE_INC, x + 1);
2258 DO_ARITH1 (PRE_DEC, x - 1);
2259 DO_ARITH1 (ROTATE, (x << 4) | (x >> 4));
2260 DO_ARITH1 (ASHIFT, x << 1);
2261 DO_ARITH1 (LSHIFTRT, x >> 1);
2262 DO_ARITH1 (ASHIFTRT, (x >> 1) | (x & 0x80));
2263
2264 DO_ARITH2 (AND, x & y);
2265 DO_ARITH2 (IOR, x | y);
2266 DO_ARITH2 (XOR, x ^ y);
2267 DO_ARITH2 (PLUS, x + y);
2268 DO_ARITH2 (MINUS, x - y);
2269 #undef DO_ARITH1
2270 #undef DO_ARITH2
2271 }
2272 } // size == 1
2273 else
2274 gcc_unreachable ();
2275 }
2276
2277
2278 // Try to find a sequence of ply_t's that represent a II.m_regno = II.m_isrc
2279 // insn that sets a reg to a compile-time constant, and that is more
2280 // efficient than just a move insn. (When try_split_any_p is on, then
2281 // solutions that perform equal to a move insn are also allowed).
2282 // MEMO0 is the GPR state before II runs. A solution has been found
2283 // when .fpd->solution has at least one entry. LEN specifies the
2284 // depth of recursion, which works on the LEN-th ply_t.
2285 void
2286 bbinfo_t::find_plies (int len, const insninfo_t &ii, const memento_t &memo0)
2287 {
2288 if (len > fpd->n_best_plys)
2289 return;
2290
2291 memento_t memo = memo0;
2292 bool ply_applied_p = false;
2293
2294 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2295 const bool extra = dump_file && (dump_flags & TDF_FOLDING);
2296
2297 if (extra)
2298 {
2299 fprintf (dump_file, ";; #%d (HAM=%d): get_plies R%d[%d] = ", len,
2300 ii.hamming (fpd->regs0), ii.m_regno, ii.m_size);
2301 fprintf (dump_file, "0x%0*" PRIx64 "\n",
2302 2 * ii.m_size, ii.m_isrc & size_to_mask (ii.m_size));
2303 }
2304
2305 plies_t &ps = fpd->plies[len - 1];
2306
2307 const ply_t *const prev = len >= 2 ? fpd->ply_stack[len - 2] : nullptr;
2308 const ply_t *const prev2 = len >= 3 ? fpd->ply_stack[len - 3] : nullptr;
2309
2310 bbinfo_t::get_plies (ps, ii, memo0, prev);
2311
2312 #define NEXT(reason) \
2313 do { \
2314 if (extra) \
2315 fprintf (dump_file, ";; cont=%s\n", reason); \
2316 goto next; \
2317 } while (0)
2318
2319 for (int ip = 0; ip < ps.n_plies; ++ip)
2320 {
2321 const ply_t &p = ps.plies[ip];
2322
2323 fpd->ply_stack[len - 1] = &p;
2324
2325 if (0)
2326 next: continue;
2327
2328 if (extra)
2329 ply_t::dump_plys (dump_file, len, 1, fpd->ply_stack + len - 1, memo0);
2330
2331 // A MOVW with a Hamming distance of < 2 requires more plys.
2332 if (p.is_movw () && len + (2 - p.dhamming) > fpd->n_best_plys)
2333 NEXT ("movw.plys");
2334
2335 if (len >= 2)
2336 {
2337 // Destroying (parts of) the results of the previous ply
2338 // won't yield an optimal sequence.
2339 if (p.overrides (prev))
2340 NEXT ("overrides");
2341
2342 // When two plys are independent of each other, then only
2343 // investigate sequences that operate on the higher reg first.
2344 // This canonicalization reduces the number of candidates,
2345 if (p.commutes_with (prev, ii.m_scratch)
2346 && p.regno > prev->regno)
2347 NEXT ("noncanonic");
2348
2349 // Two subsequent BLDs touching the same register.
2350 if (p.is_bld ()
2351 && prev->is_bld ()
2352 && p.changes_result_of (prev))
2353 NEXT ("2bld");
2354
2355 // When there is a BLD, then at least 2 of the same kind
2356 // shall occur in a row.
2357 if (prev->is_bld ()
2358 && ! p.is_bld ()
2359 && (len == 2
2360 || (prev->is_setbld () && ! prev2->is_setbld ())
2361 || (prev->is_cltbld () && ! prev2->is_cltbld ())))
2362 NEXT ("1bld");
2363 }
2364
2365 // The hamming delta of a MOVW may be less than 2, namely 0 or 1.
2366 // When the latter is the case, then a reasonable sequence must
2367 // modify the result of the MOVW.
2368 if (len >= 2
2369 && prev->is_movw ()
2370 && prev->dhamming == 1
2371 && ! p.changes_result_of (prev))
2372 NEXT ("movw.dh=1");
2373
2374 if (len >= 3
2375 && prev2->is_movw ()
2376 && prev2->dhamming == 0
2377 && ! p.changes_result_of (prev2))
2378 NEXT ("movw.dh=0");
2379
2380 // When setting an n-byte destination, then at most n/2 MOVWs
2381 // will occur in an optimal sequence.
2382 int n_movw = 0;
2383 for (int i = 0; i < len; ++i)
2384 n_movw += fpd->ply_stack[i]->is_movw ();
2385 if (n_movw > ii.m_size / 2)
2386 NEXT ("movws");
2387
2388 if (ply_applied_p)
2389 memo = memo0;
2390
2391 memo.apply (p);
2392
2393 ply_applied_p = true;
2394
2395 // Calculate the cost of the sequence we have so far. Scale by some
2396 // factor so that we can express that ADIW is more expensive than MOVW
2397 // because it is slower, but without defeating MOVW.
2398 const int SCALE = 4;
2399
2400 int penal = 0;
2401 int cost = SCALE * 0;
2402
2403 bool movw_p = 0;
2404 for (int i = 0; i < len; ++i)
2405 {
2406 bool adiw_p = fpd->ply_stack[i]->is_adiw ();
2407 cost += SCALE * fpd->ply_stack[i]->cost + adiw_p;
2408 penal += adiw_p;
2409 movw_p |= fpd->ply_stack[i]->is_movw ();
2410 }
2411 penal += movw_p;
2412
2413 const int hamm = ii.hamming (memo);
2414
2415 // The current Hamming distance yields a lower bound of how many
2416 // plys are still required. Consider that future cost already now.
2417 int future_cost = AVR_HAVE_MOVW || (AVR_HAVE_ADIW && ii.m_regno >= REG_22)
2418 ? (1 + hamm) / 2
2419 : hamm;
2420
2421 // Similarly, when MOVW doesn't decrease the Hamming distance by 2,
2422 // then we know that at least 2 - dhamming plys must follow in the
2423 // future. (MOVW + ADIW will not occur.)
2424 if (p.is_movw ())
2425 future_cost = std::max (future_cost, 2 - p.dhamming);
2426
2427 if (extra && future_cost)
2428 avr_dump (";; future cost = %d, dh=%d\n", future_cost, hamm);
2429
2430 cost += SCALE * future_cost;
2431
2432 bool profitable = (cost < SCALE * fpd->max_ply_cost
2433 || (bbinfo_t::try_split_any_p
2434 && fpd->solution.n_plies == 0
2435 && cost / SCALE <= fpd->max_ply_cost
2436 && cost / SCALE == fpd->movmode_cost));
2437 if (! profitable)
2438 {
2439 if (extra)
2440 avr_dump (";; cont=cost %d+%d/%d\n", cost / SCALE, penal, SCALE);
2441 continue;
2442 }
2443
2444 if (hamm)
2445 {
2446 // Go down that rabbit hole.
2447 gcc_assert (ply_applied_p);
2448 bbinfo_t::find_plies (1 + len, ii, memo);
2449 continue;
2450 }
2451
2452 // Found a solution that's better than everything so far.
2453
2454 // Reduce the upper cost bound according to the found solution.
2455 // No future solution will be more expensive.
2456 fpd->max_ply_cost = cost / SCALE;
2457
2458 fpd->solution = plies_t (len, fpd->ply_stack);
2459
2460 if (dump_file)
2461 {
2462 avr_dump (";; #%d FOUND COST = %d%s\n", len, cost / SCALE,
2463 penal ? " with penalty" : "");
2464 ply_t::dump_plys (dump_file, 0, len, fpd->ply_stack, fpd->regs0);
2465 if (extra)
2466 avr_dump (";; END\n");
2467 }
2468 } // for ply_t's
2469
2470 #undef NEXT
2471 }
2472
2473
2474 // Run .find_plies() and return true when .fpd->solution is a sequence of ply_t's
2475 // that represents II, a REG = CONST insn. MEMO is the GPR state prior to II.
2476 bool
2477 bbinfo_t::run_find_plies (const insninfo_t &ii, const memento_t &memo) const
2478 {
2479 fpd->solution.reset ();
2480 fpd->regs0 = memo;
2481 fpd->n_get_plies = 0;
2482
2483 const int hamm = ii.hamming (memo);
2484
2485 if (hamm == 0)
2486 {
2487 avr_dump (";; Found redundant insn %d\n",
2488 ii.m_insn ? INSN_UID (ii.m_insn) : 0);
2489 return true;
2490 }
2491
2492 // Upper bound (in words) for any solution that's better than mov<mode>.
2493 // Will be decreased by find plies as it finds better solutions.
2494 fpd->movmode_cost = ii.cost ();
2495 fpd->max_ply_cost = fpd->movmode_cost;
2496
2497 // With a non-zero Hamming distance, this insn will require at least one
2498 // instruction. When the upper bound for required instructions is that
2499 // small, then the current insn is good enough.
2500 if (fpd->max_ply_cost <= 1)
2501 return false;
2502
2503 fpd->n_best_plys = ii.n_best_plys (hamm);
2504 gcc_assert (fpd->n_best_plys <= N_BEST_PLYS);
2505
2506 if (dump_file)
2507 {
2508 const uint64_t mask = size_to_mask (ii.m_size);
2509 fprintf (dump_file, ";; find_plies R%d[%d] = 0x%0*" PRIx64,
2510 ii.m_regno, ii.m_size, 2 * ii.m_size, ii.m_isrc & mask);
2511 if (ii.m_scratch)
2512 fprintf (dump_file, ", scratch=r%d", ii.m_scratch);
2513 memo.dump ("\n;; regs%s\n");
2514 }
2515
2516 avr_dump (";; mov<mode> cost = %d\n", fpd->max_ply_cost);
2517 avr_dump (";; max plys = %d\n", fpd->n_best_plys);
2518 ply_t::n_ply_ts = 0;
2519
2520 find_plies (1, ii, memo);
2521
2522 avr_dump (";; get_plies called %d times\n", fpd->n_get_plies);
2523 avr_dump (";; n_ply_ts = %d\n", ply_t::n_ply_ts);
2524 ply_t::max_n_ply_ts = std::max (ply_t::max_n_ply_ts, ply_t::n_ply_ts);
2525
2526 return fpd->solution.n_plies != 0;
2527 }
2528
2529
2530 // Try to propagate __zero_reg__ to a mem = reg insn's source.
2531 // Returns true on success and sets .n_new_insns.
2532 bool
2533 optimize_data_t::try_mem0 (bbinfo_t *)
2534 {
2535 rtx_insn *insn = curr.ii.m_insn;
2536 rtx set, mem, reg;
2537 machine_mode mode;
2538
2539 if (insn
2540 && (set = single_set (insn))
2541 && MEM_P (mem = SET_DEST (set))
2542 && REG_P (reg = SET_SRC (set))
2543 && GET_MODE_SIZE (mode = GET_MODE (mem)) <= 4
2544 && END_REGNO (reg) <= REG_32
2545 && ! (regmask (reg) & memento_t::fixed_regs_mask)
2546 && curr.regs.have_value (REGNO (reg), GET_MODE_SIZE (mode), 0x0))
2547 {
2548 avr_dump (";; Found insn %d: mem:%m = 0 = r%d\n", INSN_UID (insn),
2549 mode, REGNO (reg));
2550
2551 // Some insns like PUSHes don't clobber REG_CC.
2552 bool clobbers_cc = GET_CODE (PATTERN (insn)) == PARALLEL;
2553
2554 if (clobbers_cc)
2555 emit_valid_move_clobbercc (mem, CONST0_RTX (mode));
2556 else
2557 emit_valid_insn (gen_rtx_SET (mem, CONST0_RTX (mode)));
2558
2559 n_new_insns = 1;
2560
2561 return true;
2562 }
2563
2564 return false;
2565 }
2566
2567
2568 // Try to fuse two 1-byte insns .prev and .curr to one 2-byte insn (MOVW).
2569 // Returns true on success, and sets .n_new_insns, .ignore_mask etc.
2570 bool
2571 optimize_data_t::try_fuse (bbinfo_t *bbi)
2572 {
2573 insninfo_t comb;
2574
2575 if (! prev.ii.m_size
2576 || ! curr.ii.m_size
2577 || ! comb.combine (prev.ii, curr.ii))
2578 return false;
2579
2580 avr_dump (";; Working on fuse of insn %d + insn %d = 0x%04x\n",
2581 INSN_UID (prev.insn), INSN_UID (curr.insn),
2582 (unsigned) comb.m_isrc);
2583
2584 bool found = bbi->run_find_plies (comb, prev.regs);
2585 if (found)
2586 {
2587 avr_dump (";; Found fuse of insns %d and %d\n",
2588 INSN_UID (prev.insn), INSN_UID (curr.insn));
2589
2590 n_new_insns = bbinfo_t::fpd->solution.emit_insns (comb, prev.regs);
2591 delete_prev_p = true;
2592
2593 if (prev.ii.m_scratch)
2594 ignore_mask |= regmask (prev.ii.m_scratch, 1);
2595 if (curr.ii.m_scratch)
2596 ignore_mask |= regmask (curr.ii.m_scratch, 1);
2597 ignore_mask &= ~regmask (comb.m_regno, comb.m_size);
2598 }
2599
2600 return found;
2601 }
2602
2603
2604 // Try to replace an arithmetic 1-byte insn by a reg-reg move.
2605 // Returns true on success, and sets .n_new_insns etc.
2606 bool
2607 optimize_data_t::try_simplify (bbinfo_t *)
2608 {
2609 if (curr.ii.m_size == 1
2610 && curr.ii.m_old_code != REG
2611 && curr.ii.m_code == REG)
2612 {
2613 avr_dump (";; Found simplify of insn %d\n", INSN_UID (curr.insn));
2614
2615 n_new_insns = curr.ii.emit_insn ();
2616
2617 return true;
2618 }
2619
2620 return false;
2621 }
2622
2623
2624 // Try to replace XEXP (*, 1) of a binary operation by a cheaper expression.
2625 // Returns true on success; sets .n_new_insns, .ignore_mask, .delete_prev_p.
2626 bool
2627 optimize_data_t::try_bin_arg1 (bbinfo_t *)
2628 {
2629 if (curr.ii.m_size != 1
2630 || curr.ii.m_new_src.arity () != 2
2631 || curr.unused)
2632 return false;
2633
2634 avr_dump (";; Working on bin_arg1 insn %d\n", INSN_UID (curr.insn));
2635
2636 gcc_assert (curr.ii.m_src && BINARY_P (curr.ii.m_src));
2637 rtx xarg1_old = XEXP (curr.ii.m_src, 1);
2638
2639 const absint_byte_t &aib = curr.ii.m_new_src;
2640 const absint_val_t &arg0 = aib.arg (0);
2641 const absint_val_t &arg1 = aib.arg (1);
2642 const absint_val_t &arg1_old = curr.ii.m_ai[0].arg (1);
2643
2644 rtx src = NULL_RTX;
2645
2646 if (CONSTANT_P (xarg1_old))
2647 {
2648 // Sometimes, we allow expensive constants as 2nd operand like
2649 // in R2 += 2 which produces two INCs. When we have the
2650 // constant handy in a reg, then use that instead of the constant.
2651 const rtx_code code = aib.get_code ();
2652 gcc_assert (arg1.val8 == (INTVAL (xarg1_old) & 0xff));
2653
2654 if (AVRasm::constant_cost (code, arg0.regno, arg1.val8) > 1)
2655 src = aib.to_rtx ();
2656 }
2657 else if (REG_P (xarg1_old)
2658 && dead_or_set_p (curr.insn, xarg1_old))
2659 {
2660 src = aib.to_rtx ();
2661
2662 // The 2nd operand is a reg with a known content that dies
2663 // at the current insn. Chances are high that the register
2664 // holds a reload value only used by the current insn.
2665 if (prev.ii.m_size == 1
2666 && rtx_equal_p (xarg1_old, SET_DEST (prev.ii.m_set))
2667 && CONSTANT_P (prev.ii.m_src))
2668 {
2669 avr_dump (";; Found dying reload insn %d\n", INSN_UID (prev.insn));
2670
2671 delete_prev_p = true;
2672 ignore_mask = regmask (arg1_old.regno, 1);
2673 }
2674 }
2675
2676 if (src)
2677 {
2678 rtx dest = SET_DEST (curr.ii.m_set);
2679
2680 avr_dump (";; Found bin_arg1 for insn %d: ", INSN_UID (curr.insn));
2681 avr_dump ("%C:%m %r", curr.ii.m_code, GET_MODE (dest), xarg1_old);
2682 aib.dump (" = %s\n");
2683
2684 emit_valid_move_clobbercc (dest, src);
2685 n_new_insns = 1;
2686 }
2687
2688 return src != NULL_RTX;
2689 }
2690
2691
2692 // Try to replace a REG = CONST insn by a cheaper sequence.
2693 // Returns true on success, and sets .n_new_insns, .ignore_mask etc.
2694 bool
2695 optimize_data_t::try_split_ldi (bbinfo_t *bbi)
2696 {
2697 if (! curr.ii.m_size
2698 || curr.unused
2699 || curr.ii.m_code != CONST_INT
2700 || (! bbinfo_t::try_split_any_p
2701 // Finding plys will only ever succeed when there are
2702 // regs with a known value.
2703 && ! (curr.regs.known
2704 || (AVR_HAVE_MOVW
2705 && curr.ii.m_regno < REG_16 && curr.ii.m_size == 4))))
2706 return false;
2707
2708 avr_dump (";; Working on split_ldi insn %d\n", INSN_UID (curr.insn));
2709
2710 bool found = bbi->run_find_plies (curr.ii, curr.regs);
2711 if (found)
2712 {
2713 avr_dump (";; Found split for ldi insn %d\n", INSN_UID (curr.insn));
2714
2715 n_new_insns = bbinfo_t::fpd->solution.emit_insns (curr.ii, curr.regs);
2716
2717 if (curr.ii.m_scratch)
2718 ignore_mask = regmask (curr.ii.m_scratch, 1);
2719 }
2720
2721 return found;
2722 }
2723
2724
2725 // Helper for try_split_any().
2726 bool
2727 optimize_data_t::fail (const char *reason)
2728 {
2729 n_new_insns = -1;
2730
2731 if (dump_file)
2732 fprintf (dump_file, ";; Giving up split_any: %s\n", reason);
2733
2734 return false;
2735 }
2736
2737
2738 // Helper for try_split_any().
2739 rtx_insn *
2740 optimize_data_t::emit_and_apply_move (memento_t &memo, rtx dest, rtx src)
2741 {
2742 rtx_insn *insn = emit_valid_move_clobbercc (dest, src);
2743 n_new_insns += 1;
2744 memo.apply_insn (insn, false);
2745
2746 return insn;
2747 }
2748
2749
2750 // Set X0 and X1 so that they are operands valid for a andqi3, iorqi3, xorqi3
2751 // or addqi3 insn with destination R_DEST. The method loads X1 to
2752 // a scratch reg as needed and records the GPR effect in IOD.regs.
2753 // EXTRA_COST are extra costs in units of words of insns that cost more
2754 // than one instruction. This is a helper for try_split_any().
2755 bool
2756 optimize_data_t
2757 ::get_2ary_operands (rtx_code &code, const absint_byte_t &aib,
2758 insn_optimize_data_t &iod, int r_dest,
2759 absint_val_t &x0, absint_val_t &x1, int &extra_cost)
2760 {
2761 if (code != IOR && code != AND && code != XOR && code != PLUS)
2762 return fail ("2ary: unknown code");
2763
2764 x0 = aib.arg (0);
2765 x1 = aib.arg (1);
2766
2767 if (! x0.knows_regno ()
2768 || x1.clueless ())
2769 return fail ("2ary: clueless");
2770
2771 int val8 = x1.val8;
2772 int val8_cost = val8 < 0 ? 100 : AVRasm::constant_cost (code, r_dest, val8);
2773
2774 if (x0.regno == r_dest
2775 && (x1.knows_regno ()
2776 || val8_cost <= 1))
2777 {
2778 if (code == XOR
2779 && val8 == 0x80
2780 && x0.regno >= REG_16)
2781 {
2782 // xorxi3 can only "r,0,r".
2783 // x0 ^ 0x80 <=> x0 - 0x80.
2784 x1.regno = 0;
2785 code = MINUS;
2786 }
2787 return true;
2788 }
2789
2790 const bool and_1_bit = code == AND && popcount_hwi (val8) == 1;
2791 // andqi3 has a "r,r,Cb1" alternative where Cb1 has exactly 1 bit set.
2792 // This can accommodate bytes of higher AND Cb<N> alternatives.
2793 if (x0.regno != r_dest)
2794 {
2795 if (and_1_bit)
2796 {
2797 extra_cost += 1 + (r_dest < REG_16);
2798 return true;
2799 }
2800 else if (x1.regno == r_dest)
2801 {
2802 std::swap (x0, x1);
2803 return true;
2804 }
2805 return fail ("2ary is a 3-operand insn");
2806 }
2807
2808 // Now we have:
2809 // 1) r_dest = x0.regno, and
2810 // 2) x1 is val8, and
2811 // 3) x1 costs 2.
2812
2813 const bool needs_scratch_p = select<bool>()
2814 : code == XOR ? true
2815 : code == AND ? popcount_hwi (val8) != 7
2816 : code == IOR ? popcount_hwi (val8) != 1
2817 : code == PLUS ? IN_RANGE (val8, 3, 0xff - 3)
2818 : bad_case<bool> ();
2819
2820 const int r_val8 = iod.regs.regno_with_value (val8, 0 /* excludes: none */);
2821 if (r_val8)
2822 {
2823 // Found a reg that already holds the constant.
2824 x1.val8 = -1;
2825 x1.regno = r_val8;
2826 return true;
2827 }
2828 else if (iod.ii.m_scratch)
2829 {
2830 // Using the insn's scratch reg.
2831 rtx xdst = gen_rtx_REG (QImode, iod.ii.m_scratch);
2832 rtx xsrc = gen_int_mode (x1.val8, QImode);
2833 emit_and_apply_move (iod.regs, xdst, xsrc);
2834
2835 x1.regno = iod.ii.m_scratch;
2836 x1.val8 = -1;
2837
2838 return true;
2839 }
2840 else if (! needs_scratch_p)
2841 {
2842 // Some constants (1 and -1) can be loaded without a scratch.
2843 extra_cost += 1;
2844 return true;
2845 }
2846 else if (and_1_bit)
2847 {
2848 // This can always fall back to BST + CLR + BLD, but may be cheaper.
2849 extra_cost += 1 + (r_dest < REG_16);
2850 return true;
2851 }
2852
2853 return fail ("2ary: expensive constant");
2854 }
2855
2856
2857 static inline bool
2858 any_shift_p (rtx_code code)
2859 {
2860 return code == LSHIFTRT || code == ASHIFTRT || code == ASHIFT;
2861 }
2862
2863 // Try to split .curr into a sequence of 1-byte insns.
2864 // Returns true on success. Sets .n_new_insns and .ignore_mask.
2865 bool
2866 optimize_data_t::try_split_any (bbinfo_t *)
2867 {
2868 if (curr.ii.m_size < 2
2869 // Constants are split by split_ldi.
2870 || CONSTANT_P (curr.ii.m_src)
2871 // Splitting requires knowledge about what to do with each byte.
2872 || curr.ii.m_ai.end_knows (VALUE) < curr.ii.m_size)
2873 return false;
2874
2875 avr_dump (";; Working on split_any %C:%m insn %d\n", curr.ii.m_code,
2876 GET_MODE (SET_DEST (curr.ii.m_set)), INSN_UID (curr.insn));
2877
2878 const insninfo_t &ii = curr.ii;
2879 const int n_bytes = ii.m_size;
2880 int extra_cost = 0;
2881 int binop_cost = -1;
2882
2883 // For plain AND, IOR, XOR get the current cost in units of words.
2884 if (BINARY_P (curr.ii.m_src))
2885 {
2886 const rtx_code code = curr.ii.m_code;
2887 if ((code == IOR || code == AND || code == XOR)
2888 && REG_P (XEXP (curr.ii.m_src, 0))
2889 && CONSTANT_P (XEXP (curr.ii.m_src, 1)))
2890 {
2891 binop_cost = get_attr_length (curr.insn);
2892 avr_dump (";; Competing against %C:%m cost = %d\n", code,
2893 GET_MODE (curr.ii.m_src), binop_cost);
2894 }
2895 }
2896
2897 // Step 1: Work out conflicts and which sign extends to perform.
2898
2899 const gprmask_t regs_dest = regmask (ii.m_regno, n_bytes);
2900 int r_sign = 0;
2901 gprmask_t regs_signs = 0;
2902 bool has_lsl = false;
2903 bool has_lsr = false;
2904
2905 for (int i = 0; i < n_bytes; ++i)
2906 {
2907 const absint_byte_t &aib = ii.m_ai[i];
2908 const int r_dest = ii.m_regno + i;
2909 const gprmask_t regs_src = aib.reg_mask ();
2910
2911 // When only regs to the right are used, or only regs to the left
2912 // are used, then there's no conflict like it is arising for rotates.
2913 // For now, only implement conflict-free splits.
2914 has_lsl |= has_bits_in (regs_src & regs_dest, 0, r_dest - 1);
2915 has_lsr |= has_bits_in (regs_src & regs_dest, r_dest + 1, 31);
2916 if (has_lsl && has_lsr)
2917 return fail ("has both << and >>");
2918
2919 if (aib.get_code () == SIGN_EXTEND)
2920 {
2921 const absint_val_t x0 = aib.arg (0);
2922 if (! r_sign)
2923 r_sign = x0.regno;
2924 else if (r_sign != x0.regno)
2925 return fail ("too many signs");
2926
2927 // Signs are handled below after all the other bytes.
2928 regs_signs |= regmask (r_dest, 1);
2929 }
2930 }
2931
2932 // Step 2: Work on the individual bytes and emit according insns.
2933
2934 n_new_insns = 0;
2935 memento_t memo = curr.regs;
2936
2937 const int step = has_lsl ? -1 : 1;
2938 const int istart = step == 1 ? 0 : n_bytes - 1;
2939 const int iend = step == 1 ? n_bytes : -1;
2940
2941 for (int i = istart; i != iend; i += step)
2942 {
2943 const absint_byte_t &aib = ii.m_ai[i];
2944 const int r_dest = ii.m_regno + i;
2945 rtx_code code = aib.get_code ();
2946 rtx xsrc = NULL_RTX;
2947 rtx xdest = gen_rtx_REG (QImode, r_dest);
2948
2949 if (code == SET)
2950 {
2951 const int r_src = aib.regno (false);
2952 const int val8 = aib.val8 (false);
2953 int r16;
2954
2955 // A no-op...
2956 if (r_dest == r_src)
2957 continue;
2958 // ...or an existing 16-bit constant...
2959 else if (AVR_HAVE_MOVW
2960 && i + step != iend
2961 // Next is not a no-op.
2962 && ii.m_ai[i + step].regno (false) != r_dest + step
2963 // Eligible for MOVW.
2964 && r_dest + step == (r_dest ^ 1)
2965 && r_dest % 2 == i % 2
2966 && (r16 = ii.m_ai.reg16_with_value (i, i + step, memo)))
2967 {
2968 xdest = gen_rtx_REG (HImode, r_dest & ~1);
2969 xsrc = gen_rtx_REG (HImode, r16);
2970 i += step;
2971 }
2972 // ...or a reg-reg move from a multi-byte move...
2973 else if (r_src
2974 // Prefer a reg-reg move over a (potential) load
2975 // of a constant, because the subsequent RTL
2976 // peephole pass may combine it to a MOVW again.
2977 && AVR_HAVE_MOVW
2978 && REG_P (curr.ii.m_src))
2979 xsrc = gen_rtx_REG (QImode, r_src);
2980 // ...or a cheap constant...
2981 else if (val8 >= 0
2982 && AVRasm::constant_cost (SET, r_dest, val8) <= 1)
2983 xsrc = gen_int_mode (val8, QImode);
2984 // ...or a reg-reg move...
2985 else if (r_src)
2986 xsrc = gen_rtx_REG (QImode, r_src);
2987 // ...or a costly constant that already exists in some reg...
2988 else if (memo.regno_with_value (val8, 0 /* excludes: none */))
2989 xsrc = gen_rtx_REG (QImode, memo.regno_with_value (val8, 0));
2990 // ...or a costly constant loaded into curr.insn's scratch reg...
2991 else if (ii.m_scratch)
2992 {
2993 rtx xscratch = gen_rtx_REG (QImode, ii.m_scratch);
2994 rtx xval8 = gen_int_mode (val8, QImode);
2995 emit_and_apply_move (memo, xscratch, xval8);
2996 xsrc = xscratch;
2997 }
2998 // ...or a costly constant (1 or -1) that doesn't need a scratch.
2999 else if (! AVRasm::ldi_needs_scratch (r_dest, val8))
3000 {
3001 extra_cost += 1;
3002 xsrc = gen_int_mode (val8, QImode);
3003 }
3004 else
3005 return fail ("expensive val8");
3006 } // SET
3007 else if (aib.arity () == 1)
3008 {
3009 if (aib.get_code () == SIGN_EXTEND)
3010 // Signs are handled after all the others.
3011 continue;
3012 else
3013 {
3014 const absint_val_t x0 = aib.arg (0);
3015 rtx xop0 = gen_rtx_REG (QImode, x0.regno);
3016 xsrc = gen_rtx_fmt_e (code, QImode, xop0);
3017 }
3018 } // unary
3019 else if (aib.arity () == 2)
3020 {
3021 absint_val_t x0;
3022 absint_val_t x1;
3023 insn_optimize_data_t iod (memo);
3024 iod.ii = curr.ii;
3025
3026 if (! get_2ary_operands (code, aib, iod, r_dest, x0, x1, extra_cost))
3027 return false;
3028 rtx xop0 = gen_rtx_REG (QImode, x0.regno);
3029 rtx xop1 = x1.knows_val8 ()
3030 ? gen_int_mode (x1.val8, QImode)
3031 : gen_rtx_REG (QImode, x1.regno);
3032
3033 xsrc = gen_rtx_fmt_ee (code, QImode, xop0, xop1);
3034 } // binary
3035
3036 if (! xsrc)
3037 return fail ("no source found");
3038
3039 if (r_sign
3040 && (regmask (xdest) & regmask (r_sign, 1)))
3041 return fail ("clobbered r_sign");
3042
3043 emit_and_apply_move (memo, xdest, xsrc);
3044 }
3045
3046 // Step 3: Emit insns for sign extend.
3047 // No more need to track memo beyond this point.
3048
3049 if (! emit_signs (r_sign, regs_signs))
3050 return false;
3051
3052 if (binop_cost >= 0)
3053 {
3054 avr_dump (";; Expected cost: %d + %d\n", n_new_insns, extra_cost);
3055 if (n_new_insns + extra_cost > binop_cost)
3056 return fail ("too expensive");
3057 }
3058
3059 if (ii.m_scratch)
3060 ignore_mask = regmask (ii.m_scratch, 1);
3061
3062 return true;
3063 }
3064
3065
3066 // A helper for try_split_any() above.
3067 // Emit sign extends from R_MSB.7 to all regs in REGS_SIGNS.
3068 bool
3069 optimize_data_t::emit_signs (const int r_msb, gprmask_t regs_signs)
3070 {
3071 if (! regs_signs)
3072 return true;
3073 else if (! r_msb)
3074 return fail ("fatal: no r_msb given");
3075
3076 // Pick an arbitrary reg from the sign destinations when the source
3077 // isn't one of the signs.
3078 const int r_signs = regs_signs & regmask (r_msb, 1)
3079 ? r_msb
3080 : ctz_hwi (regs_signs);
3081
3082 // Set all bits in r_signs according to the sign of r_msb using the
3083 // r,r,C07 alternative of ashrqi3.
3084 rtx xsrc = gen_rtx_fmt_ee (ASHIFTRT, QImode,
3085 gen_rtx_REG (QImode, r_msb), GEN_INT (7));
3086 emit_valid_move_clobbercc (gen_rtx_REG (QImode, r_signs), xsrc);
3087 regs_signs &= ~regmask (r_signs, 1);
3088
3089 // Set up a 16-bit sign register if possible.
3090 int r16_signs = 0;
3091 if (regs_signs & regmask (r_signs ^ 1, 1))
3092 {
3093 emit_move_mask (r_signs ^ 1, r_signs, 1, regs_signs);
3094 r16_signs = r_signs & ~1;
3095 }
3096
3097 // Handle all 16-bit signs regs provided MOVW.
3098 if (AVR_HAVE_MOVW)
3099 for (int r = FIRST_GPR; r < REG_32; r += 2)
3100 {
3101 const gprmask_t m = regmask (r, 2);
3102 if ((m & regs_signs) == m)
3103 {
3104 if (r16_signs)
3105 emit_move_mask (r, r16_signs, 2, regs_signs);
3106 else
3107 {
3108 emit_move_mask (r + 0, r_signs, 1, regs_signs);
3109 emit_move_mask (r + 1, r_signs, 1, regs_signs);
3110 r16_signs = r;
3111 }
3112 }
3113 }
3114
3115 // Handle all remaining signs.
3116 while (regs_signs)
3117 emit_move_mask (ctz_hwi (regs_signs), r_signs, 1, regs_signs);
3118
3119 return true;
3120 }
3121
3122 // Helper for the method above. Move N_BYTES registers from R_SRC to R_DST,
3123 // keeping track of which regs are still to be done in MASK.
3124 void
3125 optimize_data_t::emit_move_mask (int r_dst, int r_src, int n_bytes,
3126 gprmask_t &mask)
3127 {
3128 const gprmask_t mask_dst = regmask (r_dst, n_bytes);
3129 const gprmask_t mask_src = regmask (r_src, n_bytes);
3130 gcc_assert ((mask_dst & mask) == mask_dst);
3131 gcc_assert ((mask_src & mask) == 0);
3132 rtx xdst = gen_rtx_REG (size_to_mode (n_bytes), r_dst);
3133 rtx xsrc = gen_rtx_REG (size_to_mode (n_bytes), r_src);
3134 emit_valid_move_clobbercc (xdst, xsrc);
3135 n_new_insns += 1;
3136 mask &= ~mask_dst;
3137 }
3138
3139
3140 void
3141 bbinfo_t::optimize_one_block (bool &changed)
3142 {
3143 memento_t prev_regs;
3144
3145 rtx_insn *insn = next_nondebug_insn_bb (bb, BB_HEAD (bb));
3146
3147 for (rtx_insn *next_insn; insn; insn = next_insn)
3148 {
3149 next_insn = next_nondebug_insn_bb (bb, insn);
3150
3151 avr_dump ("\n;; Working on insn %d\n%r\n\n", INSN_UID (insn), insn);
3152
3153 optimize_data_t od (prev_regs, regs);
3154
3155 od.prev.insn = prev_nondebug_insn_bb (bb, insn);
3156 od.curr.insn = insn;
3157
3158 od.prev.ii.init1 (od.prev, 1, "IIprev ");
3159 od.curr.ii.init1 (od.curr, 8, "IIcurr ");
3160
3161 start_sequence ();
3162
3163 bool found = ((bbinfo_t::try_fuse_p && od.try_fuse (this))
3164 || (bbinfo_t::try_bin_arg1_p && od.try_bin_arg1 (this))
3165 || (bbinfo_t::try_simplify_p && od.try_simplify (this))
3166 || (bbinfo_t::try_split_ldi_p && od.try_split_ldi (this))
3167 || (bbinfo_t::try_split_any_p && od.try_split_any (this))
3168 || (bbinfo_t::try_mem0_p && od.try_mem0 (this)));
3169
3170 rtx_insn *new_insns = get_insns ();
3171 end_sequence ();
3172
3173 gcc_assert (found == (od.n_new_insns >= 0));
3174
3175 ++tick;
3176
3177 // This insn will become the previous one in the next loop iteration.
3178 // Just used in dumps.
3179 rtx_insn *new_curr_insn;
3180
3181 if (! found)
3182 {
3183 // Nothing changed.
3184 avr_dump (";; Keeping old route.\n");
3185 gcc_assert (! od.delete_prev_p);
3186
3187 prev_regs = regs;
3188 regs.apply_insn (insn, false);
3189
3190 new_curr_insn = insn;
3191 }
3192 else
3193 {
3194 // We have new_insns.
3195 changed = true;
3196
3197 if (dump_file)
3198 {
3199 avr_dump ("\n;; EMIT %d new insn%s replacing ",
3200 od.n_new_insns, "s" + (od.n_new_insns == 1));
3201 if (od.delete_prev_p)
3202 avr_dump ("insn %d and ", INSN_UID (od.prev.insn));
3203 avr_dump ("insn %d, delete_prev=%d:\n%L\n", INSN_UID (insn),
3204 od.delete_prev_p, new_insns);
3205 }
3206
3207 new_curr_insn = od.emit_sequence (bb, new_insns);
3208 } // found
3209
3210 if (dump_file && new_curr_insn)
3211 {
3212 avr_dump ("\n");
3213
3214 const int d = regs.distance_to (prev_regs);
3215 if (d || new_curr_insn != insn)
3216 avr_dump (";; %d regs changed state:\n", d);
3217
3218 if (new_curr_insn != insn)
3219 {
3220 avr_dump (";; Befor insn %d", INSN_UID (new_curr_insn));
3221 prev_regs.dump ();
3222 }
3223
3224 avr_dump (";; After insn %d", INSN_UID (new_curr_insn));
3225 regs.dump ();
3226 }
3227 } // for BB insns
3228 }
3229
3230
3231 void
3232 bbinfo_t::optimize_one_function (function *func)
3233 {
3234 bbinfo_t::fpd = XNEW (bbinfo_t::find_plies_data_t);
3235 bbinfo_t::bb_info = XCNEWVEC (bbinfo_t, last_basic_block_for_fn (func));
3236 int *post_order = XNEWVEC (int, n_basic_blocks_for_fn (func));
3237
3238 plies_t::max_n_plies = 0;
3239
3240 using elt0_getter_HRS = elt0_getter<HARD_REG_SET, HARD_REG_ELT_TYPE>;
3241 memento_t::fixed_regs_mask = (gprmask_t) elt0_getter_HRS::get (fixed_reg_set);
3242
3243 // Option -mfuse-move=<0,23> provides a 3:2:2:2 mixed radix value:
3244 // -mfuse-move= 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 20 1 2 3 Digit
3245 // fuse 1 1 1 1 1 1 1 1 1 1 1 1 0
3246 // bin_arg1 1 1 1 1 1 1 1 1 1 1 1 1 1
3247 // split_any 1 1 1 1 1 1 1 1 1 1 1 1 2
3248 // split_ldi 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3
3249 // use arith 1 1 1 1 1 1 1 1 3
3250
3251 // Which optimization(s) to perform.
3252 bbinfo_t::try_fuse_p = avropt_fuse_move & 0x1; // Digit 0 in [0, 1].
3253 bbinfo_t::try_mem0_p = avropt_fuse_move & 0x1; // Digit 0 in [0, 1].
3254 bbinfo_t::try_bin_arg1_p = avropt_fuse_move & 0x2; // Digit 1 in [0, 1].
3255 bbinfo_t::try_split_any_p = avropt_fuse_move & 0x4; // Digit 2 in [0, 1].
3256 bbinfo_t::try_split_ldi_p = avropt_fuse_move >> 3; // Digit 3 in [0, 2].
3257 bbinfo_t::use_arith_p = (avropt_fuse_move >> 3) >= 2; // Digit 3 in [0, 2].
3258 bbinfo_t::use_set_some_p = bbinfo_t::try_split_ldi_p; // Digit 3 in [0, 2].
3259 bbinfo_t::try_simplify_p = avropt_fuse_move != 0;
3260
3261 // Topologically sort BBs from last to first.
3262
3263 const int n_post_order = post_order_compute (post_order, false, false);
3264 bool changed = false;
3265
3266 // Traverse the BBs from first to last in order to increase the chance
3267 // that register values from all incoming edges are known.
3268
3269 for (int n = n_post_order - 1; n >= 0; --n)
3270 {
3271 basic_block bb = BASIC_BLOCK_FOR_FN (func, post_order[n]);
3272
3273 bbinfo_t::bb_info[bb->index].bb = bb;
3274 bbinfo_t::bb_info[bb->index].enter ();
3275 bbinfo_t::bb_info[bb->index].optimize_one_block (changed);
3276 bbinfo_t::bb_info[bb->index].leave ();
3277 }
3278
3279 if (plies_t::max_n_plies)
3280 avr_dump (";; max_n_plies=%d\n", (int) plies_t::max_n_plies);
3281
3282 if (changed)
3283 {
3284 df_note_add_problem ();
3285 df_analyze ();
3286 }
3287
3288 XDELETEVEC (post_order);
3289 XDELETEVEC (bbinfo_t::bb_info);
3290 XDELETE (bbinfo_t::fpd);
3291 }
3292
3293 } // anonymous namespace
3294
3295
3296 namespace
3297 {
3298
3299
3300 //////////////////////////////////////////////////////////////////////////////
3301 // Try to replace 2 cbranch insns with 1 comparison and 2 branches.
3302
3303 static const pass_data avr_pass_data_ifelse =
3304 {
3305 RTL_PASS, // type
3306 "", // name (will be patched)
3307 OPTGROUP_NONE, // optinfo_flags
3308 TV_DF_SCAN, // tv_id
3309 0, // properties_required
3310 0, // properties_provided
3311 0, // properties_destroyed
3312 0, // todo_flags_start
3313 TODO_df_finish | TODO_df_verify // todo_flags_finish
3314 };
3315
3316 class avr_pass_ifelse : public rtl_opt_pass
3317 {
3318 public:
3319 avr_pass_ifelse (gcc::context *ctxt, const char *name)
3320 : rtl_opt_pass (avr_pass_data_ifelse, ctxt)
3321 {
3322 this->name = name;
3323 }
3324
3325 bool gate (function *) final override
3326 {
3327 return optimize > 0;
3328 }
3329
3330 unsigned int execute (function *func) final override;
3331 }; // avr_pass_ifelse
3332
3333
3334 /* Return TRUE iff comparison code CODE is explicitly signed. */
3335
3336 static bool
3337 avr_strict_signed_p (rtx_code code)
3338 {
3339 return code == GT || code == GE || code == LT || code == LE;
3340 }
3341
3342
3343 /* Return TRUE iff comparison code CODE is explicitly unsigned. */
3344
3345 static bool
3346 avr_strict_unsigned_p (rtx_code code)
3347 {
3348 return code == GTU || code == GEU || code == LTU || code == LEU;
3349 }
3350
3351 #include "config/avr/ranges.h"
3352
3353 /* Suppose the inputs represent a code like
3354
3355 if (x <CMP1> XVAL1) goto way1;
3356 if (x <CMP2> XVAL2) goto way2;
3357 way3:;
3358
3359 with two integer mode comparisons where XVAL1 and XVAL2 are CONST_INT.
3360 When this can be rewritten in the form
3361
3362 if (x <cond1> xval) goto way1;
3363 if (x <cond2> xval) goto way2;
3364 way3:;
3365
3366 then set CMP1 = cond1, CMP2 = cond2, and return xval. Else return NULL_RTX.
3367 When SWAPT is returned true, then way1 and way2 must be swapped.
3368 When the incomping SWAPT is false, the outgoing one will be false, too. */
3369
3370 static rtx
3371 avr_2comparisons_rhs (rtx_code &cmp1, rtx xval1,
3372 rtx_code &cmp2, rtx xval2,
3373 machine_mode mode, bool &swapt)
3374 {
3375 const bool may_swapt = swapt;
3376 swapt = false;
3377
3378 //////////////////////////////////////////////////////////////////
3379 // Step 0: Decide about signedness, map xval1/2 to the range
3380 // of [un]signed machine mode.
3381
3382 const bool signed1_p = avr_strict_signed_p (cmp1);
3383 const bool signed2_p = avr_strict_signed_p (cmp2);
3384 const bool unsigned1_p = avr_strict_unsigned_p (cmp1);
3385 const bool unsigned2_p = avr_strict_unsigned_p (cmp2);
3386 const bool signed_p = signed1_p || signed2_p;
3387 bool unsigned_p = unsigned1_p || unsigned2_p;
3388
3389 using T = Ranges::scalar_type;
3390 T val1 = INTVAL (xval1);
3391 T val2 = INTVAL (xval2);
3392
3393 if (signed_p + unsigned_p > 1)
3394 {
3395 // Don't go down that rabbit hole. When the RHSs are the
3396 // same, we can still save one comparison.
3397 return val1 == val2 ? xval1 : NULL_RTX;
3398 }
3399
3400 // Decide about signedness. When no explicit signedness is present,
3401 // then cases that are close to the unsigned boundary like EQ 0, EQ 1
3402 // can also be optimized.
3403 if (unsigned_p
3404 || (! signed_p && IN_RANGE (val1, -2, 2)))
3405 {
3406 unsigned_p = true;
3407 val1 = UINTVAL (xval1) & GET_MODE_MASK (mode);
3408 val2 = UINTVAL (xval2) & GET_MODE_MASK (mode);
3409 }
3410
3411 // No way we can decompose the domain in a usable manner when the
3412 // RHSes are too far apart.
3413 if (! IN_RANGE (val1 - val2, -2, 2))
3414 return NULL_RTX;
3415
3416 //////////////////////////////////////////////////////////////////
3417 // Step 1: Represent the input conditions as truth Ranges. This
3418 // establishes a decomposition / coloring of the domain.
3419
3420 Ranges dom = Ranges::NBitsRanges (GET_MODE_BITSIZE (mode), unsigned_p,
3421 Ranges::ALL);
3422 Ranges r[4] = { dom, dom.truth (cmp1, val1), dom.truth (cmp2, val2), dom };
3423
3424 // r[1] shadows r[2] shadows r[3]. r[0] is just for nice indices.
3425 r[3].minus (r[2]);
3426 r[3].minus (r[1]);
3427 r[2].minus (r[1]);
3428
3429 //////////////////////////////////////////////////////////////////
3430 // Step 2: Filter for cases where the domain decomposes into three
3431 // intervals: One to the left, one to the right, and one
3432 // in the middle where the latter holds exactly one value.
3433
3434 for (int i = 1; i <= 3; ++i)
3435 {
3436 // Keep track of which Ranges is which.
3437 r[i].tag = i;
3438
3439 gcc_assert (r[i].check ());
3440
3441 // Filter for proper intervals. Also return for the empty set,
3442 // since cases where [m_min, m_max] decomposes into two intervals
3443 // or less have been sorted out by the generic optimizers already,
3444 // and hence should not be seen here. And more than two intervals
3445 // at a time cannot be optimized of course.
3446 if (r[i].size () != 1)
3447 return NULL_RTX;
3448 }
3449
3450 // Bubble-sort the three intervals such that:
3451 // [1] is the left interval, i.e. the one taken by LT[U].
3452 // [2] is the middle interval, i.e. the one taken by EQ.
3453 // [3] is the right interval, i.e. the one taken by GT[U].
3454 Ranges::sort2 (r[1], r[3]);
3455 Ranges::sort2 (r[2], r[3]);
3456 Ranges::sort2 (r[1], r[2]);
3457
3458 if (dump_file)
3459 fprintf (dump_file,
3460 ";; Decomposed: .%d=[%ld, %ld] .%d=[%ld, %ld] .%d=[%ld, %ld]\n",
3461 r[1].tag, (long) r[1].ranges[0].lo, (long) r[1].ranges[0].hi,
3462 r[2].tag, (long) r[2].ranges[0].lo, (long) r[2].ranges[0].hi,
3463 r[3].tag, (long) r[3].ranges[0].lo, (long) r[3].ranges[0].hi);
3464
3465 // EQ / NE can handle only one value.
3466 if (r[2].cardinality (0) != 1)
3467 return NULL_RTX;
3468
3469 // Success! This is the sought for xval.
3470 const T val = r[2].ranges[0].lo;
3471
3472 //////////////////////////////////////////////////////////////////
3473 // Step 3: Work out which label gets which condition, trying to
3474 // avoid the expensive codes GT[U] and LE[U] if possible.
3475 // Avoiding expensive codes is always possible when labels
3476 // way1 and way2 may be swapped.
3477
3478 // The xx1 ways have an expensive GT for cmp1 which can be avoided
3479 // by swapping way1 with way2.
3480 swapt = may_swapt && r[3].tag == 1;
3481 if (swapt)
3482 std::swap (r[3], r[2].tag == 2 ? r[2] : r[1]);
3483
3484 // 6 = 3! ways to assign LT, EQ, GT to the three labels.
3485 const int way = 100 * r[1].tag + 10 * r[2].tag + r[3].tag;
3486
3487 if (dump_file)
3488 fprintf (dump_file, ";; Success: unsigned=%d, swapt=%d, way=%d, rhs=%ld\n",
3489 unsigned_p, swapt, way, (long) val);
3490
3491 #define WAY(w, c1, c2) \
3492 case w: \
3493 cmp1 = unsigned_p ? unsigned_condition (c1) : c1; \
3494 cmp2 = unsigned_p ? unsigned_condition (c2) : c2; \
3495 break;
3496
3497 switch (way)
3498 {
3499 default:
3500 gcc_unreachable ();
3501
3502 // cmp1 gets the LT, avoid difficult branches for cmp2.
3503 WAY (123, LT, EQ);
3504 WAY (132, LT, NE);
3505
3506 // cmp1 gets the EQ, avoid difficult branches for cmp2.
3507 WAY (213, EQ, LT);
3508 WAY (312, EQ, GE);
3509
3510 // cmp1 gets the difficult GT, unavoidable as we may not swap way1/2.
3511 WAY (231, GT, NE);
3512 WAY (321, GT, EQ);
3513 }
3514
3515 #undef WAY
3516
3517 return gen_int_mode (val, mode);
3518 }
3519
3520
3521 /* A helper for the next method. Suppose we have two conditional branches
3522 with REG and CONST_INT operands
3523
3524 if (reg <cond1> xval1) goto label1;
3525 if (reg <cond2> xval2) goto label2;
3526
3527 If the second comparison is redundant and there are codes <cmp1>
3528 and <cmp2> such that the sequence can be performed as
3529
3530 REG_CC = compare (reg, xval);
3531 if (REG_CC <cmp1> 0) goto label1;
3532 if (REG_CC <cmp2> 0) goto label2;
3533
3534 then set COND1 to cmp1, COND2 to cmp2, SWAPT to true when the branch
3535 targets have to be swapped, and return XVAL. Otherwise, return NULL_RTX.
3536 This function may clobber COND1 and COND2 even when it returns NULL_RTX.
3537
3538 REVERSE_COND1 can be set to reverse condition COND1. This is useful
3539 when the second comparison does not follow the first one, but is
3540 located after label1 like in:
3541
3542 if (reg <cond1> xval1) goto label1;
3543 ...
3544 label1:
3545 if (reg <cond2> xval2) goto label2;
3546
3547 In such a case we cannot swap the labels, and we may end up with a
3548 difficult branch -- though one comparison can still be optimized out.
3549 Getting rid of such difficult branches would require to reorder blocks. */
3550
3551 static rtx
3552 avr_redundant_compare (rtx xreg1, rtx_code &cond1, rtx xval1,
3553 rtx xreg2, rtx_code &cond2, rtx xval2,
3554 bool reverse_cond1, bool &swapt)
3555 {
3556 // Make sure we have two REG <cond> CONST_INT comparisons with the same reg.
3557 if (! rtx_equal_p (xreg1, xreg2)
3558 || ! CONST_INT_P (xval1)
3559 || ! CONST_INT_P (xval2))
3560 return NULL_RTX;
3561
3562 if (reverse_cond1)
3563 cond1 = reverse_condition (cond1);
3564
3565 // Allow swapping label1 <-> label2 only when ! reverse_cond1.
3566 swapt = ! reverse_cond1;
3567 rtx_code c1 = cond1;
3568 rtx_code c2 = cond2;
3569 rtx xval = avr_2comparisons_rhs (c1, xval1,
3570 c2, xval2, GET_MODE (xreg1), swapt);
3571 if (! xval)
3572 return NULL_RTX;
3573
3574 if (dump_file)
3575 {
3576 rtx_code a1 = reverse_cond1 ? reverse_condition (cond1) : cond1;
3577 rtx_code b1 = reverse_cond1 ? reverse_condition (c1) : c1;
3578 const char *s_rev1 = reverse_cond1 ? " reverse_cond1" : "";
3579 avr_dump (";; cond1: %C %r%s\n", a1, xval1, s_rev1);
3580 avr_dump (";; cond2: %C %r\n", cond2, xval2);
3581 avr_dump (";; => %C %d\n", b1, (int) INTVAL (xval));
3582 avr_dump (";; => %C %d\n", c2, (int) INTVAL (xval));
3583 }
3584
3585 cond1 = c1;
3586 cond2 = c2;
3587
3588 return xval;
3589 }
3590
3591
3592 /* Similar to the function above, but assume that
3593
3594 if (xreg1 <cond1> xval1) goto label1;
3595 if (xreg2 <cond2> xval2) goto label2;
3596
3597 are two subsequent REG-REG comparisons. When this can be represented as
3598
3599 REG_CC = compare (reg, xval);
3600 if (REG_CC <cmp1> 0) goto label1;
3601 if (REG_CC <cmp2> 0) goto label2;
3602
3603 then set XREG1 to reg, COND1 and COND2 accordingly, and return xval.
3604 Otherwise, return NULL_RTX. This optmization can be performed
3605 when { xreg1, xval1 } and { xreg2, xval2 } are equal as sets.
3606 It can be done in such a way that no difficult branches occur. */
3607
3608 static rtx
3609 avr_redundant_compare_regs (rtx &xreg1, rtx_code &cond1, rtx &xval1,
3610 rtx &xreg2, rtx_code &cond2, rtx &xval2,
3611 bool reverse_cond1)
3612 {
3613 bool swapped;
3614
3615 if (! REG_P (xval1))
3616 return NULL_RTX;
3617 else if (rtx_equal_p (xreg1, xreg2)
3618 && rtx_equal_p (xval1, xval2))
3619 swapped = false;
3620 else if (rtx_equal_p (xreg1, xval2)
3621 && rtx_equal_p (xreg2, xval1))
3622 swapped = true;
3623 else
3624 return NULL_RTX;
3625
3626 // Found a redundant REG-REG comparison. Assume that the incoming
3627 // representation has been canonicalized by CANONICALIZE_COMPARISON.
3628 // We can always represent this using only one comparison and in such
3629 // a way that no difficult branches are required.
3630
3631 if (dump_file)
3632 {
3633 const char *s_rev1 = reverse_cond1 ? " reverse_cond1" : "";
3634 avr_dump (";; %r %C %r%s\n", xreg1, cond1, xval1, s_rev1);
3635 avr_dump (";; %r %C %r\n", xreg2, cond2, xval2);
3636 }
3637
3638 if (reverse_cond1)
3639 cond1 = reverse_condition (cond1);
3640
3641 if (swapped)
3642 {
3643 if (cond1 == EQ || cond1 == NE)
3644 {
3645 avr_dump (";; case #21\n");
3646 std::swap (xreg1, xval1);
3647 }
3648 else
3649 {
3650 std::swap (xreg2, xval2);
3651 cond2 = swap_condition (cond2);
3652
3653 // The swap may have introduced a difficult comparison.
3654 // In order to get of it, only a few cases need extra care.
3655 if ((cond1 == LT && cond2 == GT)
3656 || (cond1 == LTU && cond2 == GTU))
3657 {
3658 avr_dump (";; case #22\n");
3659 cond2 = NE;
3660 }
3661 else
3662 avr_dump (";; case #23\n");
3663 }
3664 }
3665 else
3666 avr_dump (";; case #20\n");
3667
3668 return xval1;
3669 }
3670
3671
3672 /* INSN1 and INSN2 are two cbranch insns for the same integer mode.
3673 When FOLLOW_LABEL1 is false, then INSN2 is located in the fallthrough
3674 path of INSN1. When FOLLOW_LABEL1 is true, then INSN2 is located at
3675 the true edge of INSN1, INSN2 is preceded by a barrier, and no other
3676 edge leads to the basic block of INSN2.
3677
3678 Try to replace INSN1 and INSN2 by a compare insn and two branch insns.
3679 When such a replacement has been performed, then return the insn where the
3680 caller should continue scanning the insn stream. Else, return nullptr. */
3681
3682 static rtx_insn *
3683 avr_optimize_2ifelse (rtx_jump_insn *insn1,
3684 rtx_jump_insn *insn2, bool follow_label1)
3685 {
3686 avr_dump (";; Investigating jump_insn %d and jump_insn %d.\n",
3687 INSN_UID (insn1), INSN_UID (insn2));
3688
3689 // Extract the operands of the insns:
3690 // $0 = comparison operator ($1, $2)
3691 // $1 = reg
3692 // $2 = reg or const_int
3693 // $3 = code_label
3694 // $4 = optional SCRATCH for HI, PSI, SI cases.
3695
3696 const auto &op = recog_data.operand;
3697
3698 extract_insn (insn1);
3699 rtx xop1[5] = { op[0], op[1], op[2], op[3], op[4] };
3700 int n_operands = recog_data.n_operands;
3701
3702 extract_insn (insn2);
3703 rtx xop2[5] = { op[0], op[1], op[2], op[3], op[4] };
3704
3705 rtx_code code1 = GET_CODE (xop1[0]);
3706 rtx_code code2 = GET_CODE (xop2[0]);
3707 bool swap_targets = false;
3708
3709 // Search redundant REG-REG comparison.
3710 rtx xval = avr_redundant_compare_regs (xop1[1], code1, xop1[2],
3711 xop2[1], code2, xop2[2],
3712 follow_label1);
3713
3714 // Search redundant REG-CONST_INT comparison.
3715 if (! xval)
3716 xval = avr_redundant_compare (xop1[1], code1, xop1[2],
3717 xop2[1], code2, xop2[2],
3718 follow_label1, swap_targets);
3719 if (! xval)
3720 {
3721 avr_dump (";; Nothing found for jump_insn %d and jump_insn %d.\n",
3722 INSN_UID (insn1), INSN_UID (insn2));
3723 return nullptr;
3724 }
3725
3726 if (follow_label1)
3727 code1 = reverse_condition (code1);
3728
3729 //////////////////////////////////////////////////////
3730 // Found a replacement.
3731
3732 if (dump_file)
3733 {
3734 avr_dump (";; => %C %r\n", code1, xval);
3735 avr_dump (";; => %C %r\n", code2, xval);
3736
3737 fprintf (dump_file, "\n;; Found chain of jump_insn %d and"
3738 " jump_insn %d, follow_label1=%d:\n",
3739 INSN_UID (insn1), INSN_UID (insn2), follow_label1);
3740 print_rtl_single (dump_file, PATTERN (insn1));
3741 print_rtl_single (dump_file, PATTERN (insn2));
3742 }
3743
3744 rtx_insn *next_insn
3745 = next_nonnote_nondebug_insn (follow_label1 ? insn1 : insn2);
3746
3747 // Pop the new branch conditions and the new comparison.
3748 // Prematurely split into compare + branch so that we can drop
3749 // the 2nd comparison. The following pass, split2, splits all
3750 // insns for REG_CC, and it should still work as usual even when
3751 // there are already some REG_CC insns around.
3752
3753 rtx xcond1 = gen_rtx_fmt_ee (code1, VOIDmode, cc_reg_rtx, const0_rtx);
3754 rtx xcond2 = gen_rtx_fmt_ee (code2, VOIDmode, cc_reg_rtx, const0_rtx);
3755 rtx xpat1 = gen_branch (xop1[3], xcond1);
3756 rtx xpat2 = gen_branch (xop2[3], xcond2);
3757 rtx xcompare = NULL_RTX;
3758 machine_mode mode = GET_MODE (xop1[1]);
3759
3760 if (mode == QImode)
3761 {
3762 gcc_assert (n_operands == 4);
3763 xcompare = gen_cmpqi3 (xop1[1], xval);
3764 }
3765 else
3766 {
3767 gcc_assert (n_operands == 5);
3768 rtx scratch = GET_CODE (xop1[4]) == SCRATCH ? xop2[4] : xop1[4];
3769 rtx (*gen_cmp)(rtx,rtx,rtx)
3770 = mode == HImode ? gen_gen_comparehi
3771 : mode == PSImode ? gen_gen_comparepsi
3772 : gen_gen_comparesi; // SImode
3773 xcompare = gen_cmp (xop1[1], xval, scratch);
3774 }
3775
3776 // Emit that stuff.
3777
3778 rtx_insn *cmp = emit_insn_before (xcompare, insn1);
3779 rtx_jump_insn *branch1 = emit_jump_insn_after (xpat1, insn1);
3780 rtx_jump_insn *branch2 = emit_jump_insn_after (xpat2, insn2);
3781
3782 JUMP_LABEL (branch1) = xop1[3];
3783 JUMP_LABEL (branch2) = xop2[3];
3784 // delete_insn() decrements LABEL_NUSES when deleting a JUMP_INSN,
3785 // but when we pop a new JUMP_INSN, do it by hand.
3786 ++LABEL_NUSES (xop1[3]);
3787 ++LABEL_NUSES (xop2[3]);
3788
3789 delete_insn (insn1);
3790 delete_insn (insn2);
3791
3792 if (swap_targets)
3793 {
3794 gcc_assert (! follow_label1);
3795
3796 basic_block to1 = BLOCK_FOR_INSN (xop1[3]);
3797 basic_block to2 = BLOCK_FOR_INSN (xop2[3]);
3798 edge e1 = find_edge (BLOCK_FOR_INSN (branch1), to1);
3799 edge e2 = find_edge (BLOCK_FOR_INSN (branch2), to2);
3800 gcc_assert (e1);
3801 gcc_assert (e2);
3802 redirect_edge_and_branch (e1, to2);
3803 redirect_edge_and_branch (e2, to1);
3804 }
3805
3806 // As a side effect, also recog the new insns.
3807 gcc_assert (valid_insn_p (cmp));
3808 gcc_assert (valid_insn_p (branch1));
3809 gcc_assert (valid_insn_p (branch2));
3810
3811 return next_insn;
3812 }
3813
3814
3815 /* Sequences like
3816
3817 SREG = compare (reg, 1 + val);
3818 if (SREG >= 0) goto label1;
3819 SREG = compare (reg, val);
3820 if (SREG == 0) goto label2;
3821
3822 can be optimized to
3823
3824 SREG = compare (reg, val);
3825 if (SREG == 0) goto label2;
3826 if (SREG >= 0) goto label1;
3827
3828 Almost all cases where one of the comparisons is redundant can
3829 be transformed in such a way that only one comparison is required
3830 and no difficult branches are needed. */
3831
3832 unsigned int
3833 avr_pass_ifelse::execute (function *)
3834 {
3835 rtx_insn *next_insn;
3836
3837 for (rtx_insn *insn = get_insns (); insn; insn = next_insn)
3838 {
3839 next_insn = next_nonnote_nondebug_insn (insn);
3840
3841 if (! next_insn)
3842 break;
3843
3844 // Search for two cbranch insns. The first one is a cbranch.
3845 // Filter for "cbranch<mode>4_insn" with mode in QI, HI, PSI, SI.
3846
3847 if (! JUMP_P (insn))
3848 continue;
3849
3850 int icode1 = recog_memoized (insn);
3851
3852 if (icode1 != CODE_FOR_cbranchqi4_insn
3853 && icode1 != CODE_FOR_cbranchhi4_insn
3854 && icode1 != CODE_FOR_cbranchpsi4_insn
3855 && icode1 != CODE_FOR_cbranchsi4_insn)
3856 continue;
3857
3858 rtx_jump_insn *insn1 = as_a<rtx_jump_insn *> (insn);
3859
3860 // jmp[0]: We can optimize cbranches that follow cbranch insn1.
3861 rtx_insn *jmp[2] = { next_insn, nullptr };
3862
3863 // jmp[1]: A cbranch following the label of cbranch insn1.
3864 if (LABEL_NUSES (JUMP_LABEL (insn1)) == 1)
3865 {
3866 rtx_insn *code_label1 = JUMP_LABEL_AS_INSN (insn1);
3867 rtx_insn *barrier = prev_nonnote_nondebug_insn (code_label1);
3868
3869 // When the target label of insn1 is used exactly once and is
3870 // not a fallthrough, i.e. is preceded by a barrier, then
3871 // consider the insn following that label.
3872 if (barrier && BARRIER_P (barrier))
3873 jmp[1] = next_nonnote_nondebug_insn (code_label1);
3874 }
3875
3876 // With almost certainty, only one of the two possible jumps can
3877 // be optimized with insn1, but it's hard to tell which one a priori.
3878 // Just try both. In the unlikely case where both could be optimized,
3879 // prefer jmp[0] because eliminating difficult branches is impeded
3880 // by following label1.
3881
3882 for (int j = 0; j < 2; ++j)
3883 if (jmp[j] && JUMP_P (jmp[j])
3884 && recog_memoized (jmp[j]) == icode1)
3885 {
3886 rtx_insn *next
3887 = avr_optimize_2ifelse (insn1, as_a<rtx_jump_insn *> (jmp[j]),
3888 j == 1 /* follow_label1 */);
3889 if (next)
3890 {
3891 next_insn = next;
3892 break;
3893 }
3894 }
3895
3896 } // loop insns
3897
3898 return 0;
3899 }
3900
3901
3902
3903 //////////////////////////////////////////////////////////////////////////////
3904 // Optimize results of the casesi expander for modes < SImode.
3905
3906 static const pass_data avr_pass_data_casesi =
3907 {
3908 RTL_PASS, // type
3909 "", // name (will be patched)
3910 OPTGROUP_NONE, // optinfo_flags
3911 TV_DF_SCAN, // tv_id
3912 0, // properties_required
3913 0, // properties_provided
3914 0, // properties_destroyed
3915 0, // todo_flags_start
3916 0 // todo_flags_finish
3917 };
3918
3919 class avr_pass_casesi : public rtl_opt_pass
3920 {
3921 public:
3922 avr_pass_casesi (gcc::context *ctxt, const char *name)
3923 : rtl_opt_pass (avr_pass_data_casesi, ctxt)
3924 {
3925 this->name = name;
3926 }
3927
3928 bool gate (function *) final override
3929 {
3930 return optimize > 0;
3931 }
3932
3933 unsigned int execute (function *) final override;
3934 }; // avr_pass_casesi
3935
3936
3937 /* Make one parallel insn with all the patterns from insns i[0]..i[5]. */
3938
3939 static rtx_insn *
3940 avr_parallel_insn_from_insns (rtx_insn *i[5])
3941 {
3942 rtvec vec = gen_rtvec (5, PATTERN (i[0]), PATTERN (i[1]), PATTERN (i[2]),
3943 PATTERN (i[3]), PATTERN (i[4]));
3944 start_sequence ();
3945 emit (gen_rtx_PARALLEL (VOIDmode, vec));
3946 rtx_insn *insn = get_insns ();
3947 end_sequence ();
3948
3949 return insn;
3950 }
3951
3952
3953 /* Return true if we see an insn stream generated by casesi expander together
3954 with an extension to SImode of the switch value.
3955
3956 If this is the case, fill in the insns from casesi to INSNS[1..5] and
3957 the SImode extension to INSNS[0]. Moreover, extract the operands of
3958 pattern casesi_<mode>_sequence forged from the sequence to recog_data. */
3959
3960 static bool
3961 avr_is_casesi_sequence (basic_block bb, rtx_insn *insn, rtx_insn *insns[5])
3962 {
3963 rtx set_4, set_0;
3964
3965 /* A first and quick test for a casesi sequences. As a side effect of
3966 the test, harvest respective insns to INSNS[0..4]. */
3967
3968 if (!(JUMP_P (insns[4] = insn)
3969 // casesi is the only insn that comes up with UNSPEC_INDEX_JMP,
3970 // hence the following test ensures that we are actually dealing
3971 // with code from casesi.
3972 && (set_4 = single_set (insns[4]))
3973 && UNSPEC == GET_CODE (SET_SRC (set_4))
3974 && UNSPEC_INDEX_JMP == XINT (SET_SRC (set_4), 1)
3975
3976 && (insns[3] = prev_real_insn (insns[4]))
3977 && (insns[2] = prev_real_insn (insns[3]))
3978 && (insns[1] = prev_real_insn (insns[2]))
3979
3980 // Insn prior to casesi.
3981 && (insns[0] = prev_real_insn (insns[1]))
3982 && (set_0 = single_set (insns[0]))
3983 && extend_operator (SET_SRC (set_0), SImode)))
3984 {
3985 return false;
3986 }
3987
3988 if (dump_file)
3989 {
3990 fprintf (dump_file, ";; Sequence from casesi in "
3991 "[bb %d]:\n\n", bb->index);
3992 for (int i = 0; i < 5; i++)
3993 print_rtl_single (dump_file, insns[i]);
3994 }
3995
3996 /* We have to deal with quite some operands. Extracting them by hand
3997 would be tedious, therefore wrap the insn patterns into a parallel,
3998 run recog against it and then use insn extract to get the operands. */
3999
4000 rtx_insn *xinsn = avr_parallel_insn_from_insns (insns);
4001
4002 INSN_CODE (xinsn) = recog (PATTERN (xinsn), xinsn, NULL /* num_clobbers */);
4003
4004 /* Failing to recognize means that someone changed the casesi expander or
4005 that some passes prior to this one performed some unexpected changes.
4006 Gracefully drop such situations instead of aborting. */
4007
4008 if (INSN_CODE (xinsn) < 0)
4009 {
4010 if (dump_file)
4011 fprintf (dump_file, ";; Sequence not recognized, giving up.\n\n");
4012
4013 return false;
4014 }
4015
4016 gcc_assert (CODE_FOR_casesi_qi_sequence == INSN_CODE (xinsn)
4017 || CODE_FOR_casesi_hi_sequence == INSN_CODE (xinsn));
4018
4019 extract_insn (xinsn);
4020
4021 // Assert on the anatomy of xinsn's operands we are going to work with.
4022
4023 gcc_assert (recog_data.n_operands == 12);
4024 gcc_assert (recog_data.n_dups == 3);
4025
4026 if (dump_file)
4027 {
4028 fprintf (dump_file, ";; Operands extracted:\n");
4029 for (int i = 0; i < recog_data.n_operands; i++)
4030 avr_fdump (dump_file, ";; $%d = %r\n", i, recog_data.operand[i]);
4031 fprintf (dump_file, "\n");
4032 }
4033
4034 return true;
4035 }
4036
4037
4038 /* INSNS[1..4] is a sequence as generated by casesi and INSNS[0] is an
4039 extension of an 8-bit or 16-bit integer to SImode. XOP contains the
4040 operands of INSNS as extracted by insn_extract from pattern
4041 casesi_<mode>_sequence:
4042
4043 $0: SImode reg switch value as result of $10.
4044 $1: Negative of smallest index in switch.
4045 $2: Number of entries in switch.
4046 $3: Label to table.
4047 $4: Label if out-of-bounds.
4048 $5: $0 + $1.
4049 $6: 3-byte PC: subreg:HI ($5) + label_ref ($3)
4050 2-byte PC: subreg:HI ($5)
4051 $7: HI reg index into table (Z or pseudo)
4052 $8: Z or scratch:HI (to be clobbered)
4053 $9: R24 or const0_rtx (to be clobbered)
4054 $10: Extension to SImode of an 8-bit or 16-bit integer register $11.
4055 $11: QImode or HImode register input of $10.
4056
4057 Try to optimize this sequence, i.e. use the original HImode / QImode
4058 switch value instead of SImode. */
4059
4060 static void
4061 avr_optimize_casesi (rtx_insn *insns[5], rtx *xop)
4062 {
4063 // Original mode of the switch value; this is QImode or HImode.
4064 machine_mode mode = GET_MODE (xop[11]);
4065
4066 // How the original switch value was extended to SImode; this is
4067 // SIGN_EXTEND or ZERO_EXTEND.
4068 rtx_code code = GET_CODE (xop[10]);
4069
4070 // Lower index, upper index (plus one) and range of case calues.
4071 HOST_WIDE_INT low_idx = -INTVAL (xop[1]);
4072 HOST_WIDE_INT num_idx = INTVAL (xop[2]);
4073 HOST_WIDE_INT hig_idx = low_idx + num_idx;
4074
4075 // Maximum ranges of (un)signed QImode resp. HImode.
4076 unsigned umax = QImode == mode ? 0xff : 0xffff;
4077 int imax = QImode == mode ? 0x7f : 0x7fff;
4078 int imin = -imax - 1;
4079
4080 // Testing the case range and whether it fits into the range of the
4081 // (un)signed mode. This test should actually always pass because it
4082 // makes no sense to have case values outside the mode range. Notice
4083 // that case labels which are unreachable because they are outside the
4084 // mode of the switch value (e.g. "case -1" for uint8_t) have already
4085 // been thrown away by the middle-end.
4086
4087 if (SIGN_EXTEND == code
4088 && low_idx >= imin
4089 && hig_idx <= imax)
4090 {
4091 // ok
4092 }
4093 else if (ZERO_EXTEND == code
4094 && low_idx >= 0
4095 && (unsigned) hig_idx <= umax)
4096 {
4097 // ok
4098 }
4099 else
4100 {
4101 if (dump_file)
4102 fprintf (dump_file, ";; Case ranges too big, giving up.\n\n");
4103 return;
4104 }
4105
4106 // Do normalization of switch value $10 and out-of-bound check in its
4107 // original mode instead of in SImode. Use a newly created pseudo.
4108 // This will replace insns[1..2].
4109
4110 start_sequence ();
4111
4112 rtx reg = copy_to_mode_reg (mode, xop[11]);
4113
4114 rtx (*gen_add)(rtx,rtx,rtx) = QImode == mode ? gen_addqi3 : gen_addhi3;
4115 rtx (*gen_cbranch)(rtx,rtx,rtx,rtx)
4116 = QImode == mode ? gen_cbranchqi4 : gen_cbranchhi4;
4117
4118 emit_insn (gen_add (reg, reg, gen_int_mode (-low_idx, mode)));
4119 rtx op0 = reg; rtx op1 = gen_int_mode (num_idx, mode);
4120 rtx labelref = copy_rtx (xop[4]);
4121 rtx xbranch = gen_cbranch (gen_rtx_fmt_ee (GTU, VOIDmode, op0, op1),
4122 op0, op1, labelref);
4123 rtx_insn *cbranch = emit_jump_insn (xbranch);
4124 JUMP_LABEL (cbranch) = xop[4];
4125 ++LABEL_NUSES (xop[4]);
4126
4127 rtx_insn *seq1 = get_insns ();
4128 rtx_insn *last1 = get_last_insn ();
4129 end_sequence ();
4130
4131 emit_insn_after (seq1, insns[2]);
4132
4133 // After the out-of-bounds test and corresponding branch, use a
4134 // 16-bit index. If QImode is used, extend it to HImode first.
4135 // This will replace insns[4].
4136
4137 start_sequence ();
4138
4139 if (QImode == mode)
4140 reg = force_reg (HImode, gen_rtx_fmt_e (code, HImode, reg));
4141
4142 rtx pat_4 = AVR_3_BYTE_PC
4143 ? gen_movhi (xop[7], reg)
4144 : gen_addhi3 (xop[7], reg, gen_rtx_LABEL_REF (VOIDmode, xop[3]));
4145
4146 emit_insn (pat_4);
4147
4148 rtx_insn *seq2 = get_insns ();
4149 rtx_insn *last2 = get_last_insn ();
4150 end_sequence ();
4151
4152 emit_insn_after (seq2, insns[3]);
4153
4154 if (dump_file)
4155 {
4156 fprintf (dump_file, ";; New insns: ");
4157
4158 for (rtx_insn *insn = seq1; ; insn = NEXT_INSN (insn))
4159 {
4160 fprintf (dump_file, "%d, ", INSN_UID (insn));
4161 if (insn == last1)
4162 break;
4163 }
4164 for (rtx_insn *insn = seq2; ; insn = NEXT_INSN (insn))
4165 {
4166 fprintf (dump_file, "%d%s", INSN_UID (insn),
4167 insn == last2 ? ".\n\n" : ", ");
4168 if (insn == last2)
4169 break;
4170 }
4171
4172 fprintf (dump_file, ";; Deleting insns: %d, %d, %d.\n\n",
4173 INSN_UID (insns[1]), INSN_UID (insns[2]), INSN_UID (insns[3]));
4174 }
4175
4176 // Pseudodelete the SImode and subreg of SImode insns. We don't care
4177 // about the extension insns[0]: Its result is now unused and other
4178 // passes will clean it up.
4179
4180 SET_INSN_DELETED (insns[1]);
4181 SET_INSN_DELETED (insns[2]);
4182 SET_INSN_DELETED (insns[3]);
4183 }
4184
4185
4186 unsigned int
4187 avr_pass_casesi::execute (function *func)
4188 {
4189 basic_block bb;
4190
4191 FOR_EACH_BB_FN (bb, func)
4192 {
4193 rtx_insn *insn, *insns[5];
4194
4195 FOR_BB_INSNS (bb, insn)
4196 {
4197 if (avr_is_casesi_sequence (bb, insn, insns))
4198 {
4199 avr_optimize_casesi (insns, recog_data.operand);
4200 }
4201 }
4202 }
4203
4204 return 0;
4205 }
4206
4207 } // anonymous namespace
4208
4209 /* Perform some extra checks on operands of casesi_<mode>_sequence.
4210 Not all operand dependencies can be described by means of predicates.
4211 This function performs left over checks and should always return true.
4212 Returning false means that someone changed the casesi expander but did
4213 not adjust casesi_<mode>_sequence. */
4214
4215 bool
4216 avr_casei_sequence_check_operands (rtx *xop)
4217 {
4218 rtx sub_5 = NULL_RTX;
4219
4220 if (AVR_HAVE_EIJMP_EICALL
4221 // The last clobber op of the tablejump.
4222 && xop[9] == all_regs_rtx[REG_24])
4223 {
4224 // $6 is: (subreg:SI ($5) 0)
4225 sub_5 = xop[6];
4226 }
4227
4228 if (!AVR_HAVE_EIJMP_EICALL
4229 // $6 is: (plus:HI (subreg:SI ($5) 0)
4230 // (label_ref ($3)))
4231 && PLUS == GET_CODE (xop[6])
4232 && LABEL_REF == GET_CODE (XEXP (xop[6], 1))
4233 && rtx_equal_p (xop[3], XEXP (XEXP (xop[6], 1), 0))
4234 // The last clobber op of the tablejump.
4235 && xop[9] == const0_rtx)
4236 {
4237 sub_5 = XEXP (xop[6], 0);
4238 }
4239
4240 if (sub_5
4241 && SUBREG_P (sub_5)
4242 && SUBREG_BYTE (sub_5) == 0
4243 && rtx_equal_p (xop[5], SUBREG_REG (sub_5)))
4244 return true;
4245
4246 if (dump_file)
4247 fprintf (dump_file, "\n;; Failed condition for casesi_<mode>_sequence\n\n");
4248
4249 return false;
4250 }
4251
4252 namespace
4253 {
4254
4255
4256 //////////////////////////////////////////////////////////////////////////////
4257 // Find more POST_INC and PRE_DEC cases.
4258
4259 static const pass_data avr_pass_data_fuse_add =
4260 {
4261 RTL_PASS, // type
4262 "", // name (will be patched)
4263 OPTGROUP_NONE, // optinfo_flags
4264 TV_MACH_DEP, // tv_id
4265 0, // properties_required
4266 0, // properties_provided
4267 0, // properties_destroyed
4268 0, // todo_flags_start
4269 TODO_df_finish // todo_flags_finish
4270 };
4271
4272 class avr_pass_fuse_add : public rtl_opt_pass
4273 {
4274 public:
4275 avr_pass_fuse_add (gcc::context *ctxt, const char *name)
4276 : rtl_opt_pass (avr_pass_data_fuse_add, ctxt)
4277 {
4278 this->name = name;
4279 }
4280
4281 // Cloning is required because we are running one instance of the pass
4282 // before peephole2. and a second one after cprop_hardreg.
4283 opt_pass * clone () final override
4284 {
4285 return make_avr_pass_fuse_add (m_ctxt);
4286 }
4287
4288 unsigned int execute (function *func) final override
4289 {
4290 func->machine->n_avr_fuse_add_executed += 1;
4291 n_avr_fuse_add_executed = func->machine->n_avr_fuse_add_executed;
4292
4293 if (optimize && avropt_fuse_add > 0)
4294 return execute1 (func);
4295 return 0;
4296 }
4297
4298 unsigned int execute1 (function *);
4299
4300 struct Some_Insn
4301 {
4302 rtx_insn *insn = nullptr;
4303 rtx dest, src;
4304 bool valid () const { return insn != nullptr; }
4305 void set_deleted ()
4306 {
4307 gcc_assert (insn);
4308 SET_INSN_DELETED (insn);
4309 insn = nullptr;
4310 }
4311 };
4312
4313 // If .insn is not NULL, then this is a reg:HI += const_int
4314 // of an address register.
4315 struct Add_Insn : Some_Insn
4316 {
4317 rtx addend;
4318 int regno;
4319 Add_Insn () {}
4320 Add_Insn (rtx_insn *insn);
4321 };
4322
4323 // If .insn is not NULL, then this sets an address register
4324 // to a constant value.
4325 struct Ldi_Insn : Some_Insn
4326 {
4327 int regno;
4328 Ldi_Insn () {}
4329 Ldi_Insn (rtx_insn *insn);
4330 };
4331
4332 // If .insn is not NULL, then this is a load or store insn where the
4333 // address is REG or POST_INC with an address register.
4334 struct Mem_Insn : Some_Insn
4335 {
4336 rtx reg_or_0, mem, addr, addr_reg;
4337 int addr_regno;
4338 rtx_code addr_code;
4339 machine_mode mode;
4340 addr_space_t addr_space;
4341 bool store_p, volatile_p;
4342 Mem_Insn () {}
4343 Mem_Insn (rtx_insn *insn);
4344 };
4345
4346 rtx_insn *fuse_ldi_add (Ldi_Insn &prev_ldi, Add_Insn &add);
4347 rtx_insn *fuse_add_add (Add_Insn &prev_add, Add_Insn &add);
4348 rtx_insn *fuse_add_mem (Add_Insn &prev_add, Mem_Insn &mem);
4349 rtx_insn *fuse_mem_add (Mem_Insn &prev_mem, Add_Insn &add);
4350 }; // avr_pass_fuse_add
4351
4352
4353 /* Describe properties of AVR's indirect load and store instructions
4354 LD, LDD, ST, STD, LPM, ELPM depending on register number, volatility etc.
4355 Rules for "volatile" accesses are:
4356
4357 | Xmega | non-Xmega
4358 ------+-----------------+----------------
4359 load | read LSB first | read LSB first
4360 store | write LSB first | write MSB first
4361 */
4362
4363 struct AVR_LdSt_Props
4364 {
4365 bool has_postinc, has_predec, has_ldd;
4366 // The insn printers will use POST_INC or PRE_DEC addressing, no matter
4367 // what adressing modes we are feeding into them.
4368 bool want_postinc, want_predec;
4369
4370 AVR_LdSt_Props (int regno, bool store_p, bool volatile_p, addr_space_t as)
4371 {
4372 bool generic_p = ADDR_SPACE_GENERIC_P (as);
4373 bool flashx_p = (! generic_p
4374 && as != ADDR_SPACE_MEMX && as != ADDR_SPACE_FLASHX);
4375 has_postinc = generic_p || (flashx_p && regno == REG_Z);
4376 has_predec = generic_p;
4377 has_ldd = ! AVR_TINY && generic_p && (regno == REG_Y || regno == REG_Z);
4378 want_predec = volatile_p && generic_p && ! AVR_XMEGA && store_p;
4379 want_postinc = volatile_p && generic_p && (AVR_XMEGA || ! store_p);
4380 want_postinc |= flashx_p && regno == REG_Z;
4381 }
4382
4383 AVR_LdSt_Props (const avr_pass_fuse_add::Mem_Insn &m)
4384 : AVR_LdSt_Props (m.addr_regno, m.store_p, m.volatile_p, m.addr_space)
4385 {
4386 gcc_assert (m.valid ());
4387 }
4388 };
4389
4390
4391 /* Emit a single_set that clobbers REG_CC. */
4392
4393 static rtx_insn *
4394 emit_move_ccc (rtx dest, rtx src)
4395 {
4396 return emit_insn (gen_gen_move_clobbercc (dest, src));
4397 }
4398
4399
4400 /* Emit a single_set that clobbers REG_CC after insn AFTER. */
4401
4402 static rtx_insn *
4403 emit_move_ccc_after (rtx dest, rtx src, rtx_insn *after)
4404 {
4405 return emit_insn_after (gen_gen_move_clobbercc (dest, src), after);
4406 }
4407
4408 static bool
4409 reg_seen_between_p (const_rtx reg, const rtx_insn *from, const rtx_insn *to)
4410 {
4411 return (reg_used_between_p (reg, from, to)
4412 || reg_set_between_p (reg, from, to));
4413 }
4414
4415
4416 static void
4417 avr_maybe_adjust_cfa (rtx_insn *insn, rtx reg, int addend)
4418 {
4419 if (addend
4420 && frame_pointer_needed
4421 && REGNO (reg) == FRAME_POINTER_REGNUM
4422 && avropt_fuse_add == 3)
4423 {
4424 rtx plus = plus_constant (Pmode, reg, addend);
4425 RTX_FRAME_RELATED_P (insn) = 1;
4426 add_reg_note (insn, REG_CFA_ADJUST_CFA, gen_rtx_SET (reg, plus));
4427 }
4428 }
4429
4430
4431 // If successful, this represents a SET of a pointer register to a constant.
4432 avr_pass_fuse_add::Ldi_Insn::Ldi_Insn (rtx_insn *insn)
4433 {
4434 rtx set = single_set (insn);
4435 if (!set)
4436 return;
4437
4438 src = SET_SRC (set);
4439 dest = SET_DEST (set);
4440
4441 if (REG_P (dest)
4442 && GET_MODE (dest) == Pmode
4443 && IN_RANGE (regno = REGNO (dest), REG_X, REG_Z)
4444 && CONSTANT_P (src))
4445 {
4446 this->insn = insn;
4447 }
4448 }
4449
4450 // If successful, this represents a PLUS with CONST_INT of a pointer
4451 // register X, Y or Z. Otherwise, the object is not valid().
4452 avr_pass_fuse_add::Add_Insn::Add_Insn (rtx_insn *insn)
4453 {
4454 rtx set = single_set (insn);
4455 if (!set)
4456 return;
4457
4458 src = SET_SRC (set);
4459 dest = SET_DEST (set);
4460 if (REG_P (dest)
4461 // We are only interested in PLUSes that change address regs.
4462 && GET_MODE (dest) == Pmode
4463 && IN_RANGE (regno = REGNO (dest), REG_X, REG_Z)
4464 && PLUS == GET_CODE (src)
4465 && rtx_equal_p (XEXP (src, 0), dest)
4466 && CONST_INT_P (XEXP (src, 1)))
4467 {
4468 // This is reg:HI += const_int.
4469 addend = XEXP (src, 1);
4470 this->insn = insn;
4471 }
4472 }
4473
4474 // If successful, this represents a load or store insn where the addressing
4475 // mode uses pointer register X, Y or Z. Otherwise, the object is not valid().
4476 avr_pass_fuse_add::avr_pass_fuse_add::Mem_Insn::Mem_Insn (rtx_insn *insn)
4477 {
4478 rtx set = single_set (insn);
4479 if (!set)
4480 return;
4481
4482 src = SET_SRC (set);
4483 dest = SET_DEST (set);
4484 mode = GET_MODE (dest);
4485
4486 if (MEM_P (dest)
4487 && (REG_P (src) || src == CONST0_RTX (mode)))
4488 {
4489 reg_or_0 = src;
4490 mem = dest;
4491 }
4492 else if (REG_P (dest) && MEM_P (src))
4493 {
4494 reg_or_0 = dest;
4495 mem = src;
4496 }
4497 else
4498 return;
4499
4500 if (avr_mem_memx_p (mem)
4501 || avr_load_libgcc_p (mem))
4502 return;
4503
4504 addr = XEXP (mem, 0);
4505 addr_code = GET_CODE (addr);
4506
4507 if (addr_code == REG)
4508 addr_reg = addr;
4509 else if (addr_code == POST_INC || addr_code == PRE_DEC)
4510 addr_reg = XEXP (addr, 0);
4511 else
4512 return;
4513
4514 addr_regno = REGNO (addr_reg);
4515
4516 if (avropt_fuse_add == 2
4517 && frame_pointer_needed
4518 && addr_regno == FRAME_POINTER_REGNUM)
4519 MEM_VOLATILE_P (mem) = 0;
4520
4521 if (reg_overlap_mentioned_p (reg_or_0, addr) // Can handle CONSTANT_P.
4522 || addr_regno > REG_Z
4523 || avr_mem_memx_p (mem)
4524 // The following optimizations only handle REG and POST_INC,
4525 // so that's all what we allow here.
4526 || (addr_code != REG && addr_code != POST_INC))
4527 return;
4528
4529 addr_space = MEM_ADDR_SPACE (mem);
4530 volatile_p = MEM_VOLATILE_P (mem);
4531 store_p = MEM_P (dest);
4532
4533 // Turn this "valid".
4534 this->insn = insn;
4535 }
4536
4537 /* Try to combine a Ldi insn with a PLUS CONST_INT addend to one Ldi insn.
4538 If LDI is valid, then it precedes ADD in the same block.
4539 When a replacement is found, a new insn is emitted and the old insns
4540 are pseudo-deleted. The returned insn is the point where the calling
4541 scanner should continue. When no replacement is found, nullptr is
4542 returned and nothing changed. */
4543
4544 rtx_insn *
4545 avr_pass_fuse_add::fuse_ldi_add (Ldi_Insn &ldi, Add_Insn &add)
4546 {
4547 if (! ldi.valid ()
4548 || reg_seen_between_p (ldi.dest, ldi.insn, add.insn))
4549 {
4550 // If something is between the Ldi and the current insn, we can
4551 // set the Ldi invalid to speed future scans.
4552 return ldi.insn = nullptr;
4553 }
4554
4555 // Found a Ldi with const and a PLUS insns in the same BB,
4556 // and with no interfering insns between them.
4557
4558 // Emit new Ldi with the sum of the original offsets after the old Ldi.
4559 rtx xval = plus_constant (Pmode, ldi.src, INTVAL (add.addend));
4560
4561 rtx_insn *insn = emit_move_ccc_after (ldi.dest, xval, ldi.insn);
4562 avr_dump (";; new Ldi[%d] insn %d after %d: R%d = %r\n\n", ldi.regno,
4563 INSN_UID (insn), INSN_UID (ldi.insn), ldi.regno, xval);
4564
4565 rtx_insn *next = NEXT_INSN (add.insn);
4566 ldi.set_deleted ();
4567 add.set_deleted ();
4568
4569 return next;
4570 }
4571
4572 /* Try to combine two PLUS insns with CONST_INT addend to one such insn.
4573 If PREV_ADD is valid, then it precedes ADD in the same basic block.
4574 When a replacement is found, a new insn is emitted and the old insns
4575 are pseudo-deleted. The returned insn is the point where the calling
4576 scanner should continue. When no replacement is found, nullptr is
4577 returned and nothing changed. */
4578
4579 rtx_insn *
4580 avr_pass_fuse_add::fuse_add_add (Add_Insn &prev_add, Add_Insn &add)
4581 {
4582 if (! prev_add.valid ()
4583 || reg_seen_between_p (add.dest, prev_add.insn, add.insn))
4584 {
4585 // If something is between the previous Add and the current insn,
4586 // we can set the previous Add invalid to speed future scans.
4587 return prev_add.insn = nullptr;
4588 }
4589
4590 // Found two PLUS insns in the same BB, and with no interfering
4591 // insns between them.
4592 rtx plus = plus_constant (Pmode, add.src, INTVAL (prev_add.addend));
4593
4594 rtx_insn *next;
4595 if (REG_P (plus))
4596 {
4597 avr_dump (";; Add[%d] from %d annihilates %d\n\n", add.regno,
4598 INSN_UID (prev_add.insn), INSN_UID (add.insn));
4599 next = NEXT_INSN (add.insn);
4600 }
4601 else
4602 {
4603 // Emit after the current insn, so that it will be picked
4604 // up as next valid Add insn.
4605 next = emit_move_ccc_after (add.dest, plus, add.insn);
4606 avr_dump (";; #1 new Add[%d] insn %d after %d: R%d += %d\n\n",
4607 add.regno, INSN_UID (next), INSN_UID (add.insn),
4608 add.regno, (int) INTVAL (XEXP (plus, 1)));
4609 gcc_assert (GET_CODE (plus) == PLUS);
4610 }
4611
4612 add.set_deleted ();
4613 prev_add.set_deleted ();
4614
4615 return next;
4616 }
4617
4618 /* Try to combine a PLUS of the address register with a load or store insn.
4619 If ADD is valid, then it precedes MEM in the same basic block.
4620 When a replacement is found, a new insn is emitted and the old insns
4621 are pseudo-deleted. The returned insn is the point where the calling
4622 scanner should continue. When no replacement is found, nullptr is
4623 returned and nothing changed. */
4624
4625 rtx_insn *
4626 avr_pass_fuse_add::fuse_add_mem (Add_Insn &add, Mem_Insn &mem)
4627 {
4628 if (! add.valid ()
4629 || reg_seen_between_p (add.dest, add.insn, mem.insn))
4630 {
4631 // If something is between the Add and the current insn, we can
4632 // set the Add invalid to speed future scans.
4633 return add.insn = nullptr;
4634 }
4635
4636 AVR_LdSt_Props ap { mem };
4637
4638 int msize = GET_MODE_SIZE (mem.mode);
4639
4640 // The mem insn really wants PRE_DEC.
4641 bool case1 = ((mem.addr_code == REG || mem.addr_code == POST_INC)
4642 && msize > 1 && ap.want_predec && ! ap.has_ldd);
4643
4644 // The offset can be consumed by a PRE_DEC.
4645 bool case2 = (- INTVAL (add.addend) == msize
4646 && (mem.addr_code == REG || mem.addr_code == POST_INC)
4647 && ap.has_predec && ! ap.want_postinc);
4648
4649 if (! case1 && ! case2)
4650 return nullptr;
4651
4652 // Change from REG or POST_INC to PRE_DEC.
4653 rtx xmem = change_address (mem.mem, mem.mode,
4654 gen_rtx_PRE_DEC (Pmode, mem.addr_reg));
4655 rtx dest = mem.store_p ? xmem : mem.reg_or_0;
4656 rtx src = mem.store_p ? mem.reg_or_0 : xmem;
4657
4658 rtx_insn *next = emit_move_ccc_after (dest, src, mem.insn);
4659 add_reg_note (next, REG_INC, mem.addr_reg);
4660 avr_dump (";; new Mem[%d] insn %d after %d: %r = %r\n\n", mem.addr_regno,
4661 INSN_UID (next), INSN_UID (mem.insn), dest, src);
4662
4663 // Changing REG or POST_INC -> PRE_DEC means that the addend before
4664 // the memory access must be increased by the size of the access,
4665 rtx plus = plus_constant (Pmode, add.src, msize);
4666 if (! REG_P (plus))
4667 {
4668 rtx_insn *insn = emit_move_ccc_after (add.dest, plus, add.insn);
4669 avr_dump (";; #2 new Add[%d] insn %d after %d: R%d += %d\n\n",
4670 add.regno, INSN_UID (insn), INSN_UID (add.insn),
4671 add.regno, (int) INTVAL (XEXP (plus, 1)));
4672 gcc_assert (GET_CODE (plus) == PLUS);
4673 }
4674 else
4675 avr_dump (";; Add[%d] insn %d consumed into %d\n\n",
4676 add.regno, INSN_UID (add.insn), INSN_UID (next));
4677
4678 // Changing POST_INC -> PRE_DEC means that the addend after the mem has to be
4679 // the size of the access. The hope is that this new add insn may be unused.
4680 if (mem.addr_code == POST_INC)
4681 {
4682 plus = plus_constant (Pmode, add.dest, msize);
4683 rtx_insn *next2 = emit_move_ccc_after (add.dest, plus, next);
4684 avr_dump (";; #3 new Add[%d] insn %d after %d: R%d += %d\n\n", add.regno,
4685 INSN_UID (next2), INSN_UID (next), add.regno, msize);
4686 next = next2;
4687 }
4688
4689 add.set_deleted ();
4690 mem.set_deleted ();
4691
4692 return next;
4693 }
4694
4695 /* Try to combine a load or store insn with a PLUS of the address register.
4696 If MEM is valid, then it precedes ADD in the same basic block.
4697 When a replacement is found, a new insn is emitted and the old insns
4698 are pseudo-deleted. The returned insn is the point where the calling
4699 scanner should continue. When no replacement is found, nullptr is
4700 returned and nothing changed. */
4701
4702 rtx_insn *
4703 avr_pass_fuse_add::fuse_mem_add (Mem_Insn &mem, Add_Insn &add)
4704 {
4705 if (! mem.valid ()
4706 || reg_seen_between_p (add.dest, mem.insn, add.insn))
4707 {
4708 // If something is between the Mem and the current insn, we can
4709 // set the Mem invalid to speed future scans.
4710 return mem.insn = nullptr;
4711 }
4712
4713 AVR_LdSt_Props ap { mem };
4714
4715 int msize = GET_MODE_SIZE (mem.mode);
4716
4717 // The add insn can be consumed by a POST_INC.
4718 bool case1 = (mem.addr_code == REG
4719 && INTVAL (add.addend) == msize
4720 && ap.has_postinc && ! ap.want_predec);
4721
4722 // There are cases where even a partial consumption of the offset is better.
4723 // This are the cases where no LD+offset addressing is available, because
4724 // the address register is obviously used after the mem insn, and a mem insn
4725 // with REG addressing mode will have to restore the address.
4726 bool case2 = (mem.addr_code == REG
4727 && msize > 1 && ap.want_postinc && ! ap.has_ldd);
4728
4729 if (! case1 && ! case2)
4730 return nullptr;
4731
4732 // Change addressing mode from REG to POST_INC.
4733 rtx xmem = change_address (mem.mem, mem.mode,
4734 gen_rtx_POST_INC (Pmode, mem.addr_reg));
4735 rtx dest = mem.store_p ? xmem : mem.reg_or_0;
4736 rtx src = mem.store_p ? mem.reg_or_0 : xmem;
4737
4738 rtx_insn *insn = emit_move_ccc_after (dest, src, mem.insn);
4739 add_reg_note (insn, REG_INC, mem.addr_reg);
4740 avr_dump (";; new Mem[%d] insn %d after %d: %r = %r\n\n", add.regno,
4741 INSN_UID (insn), INSN_UID (mem.insn), dest, src);
4742
4743 rtx_insn *next = NEXT_INSN (add.insn);
4744
4745 // Changing REG -> POST_INC means that the post addend must be
4746 // decreased by the size of the access.
4747 rtx plus = plus_constant (Pmode, add.src, -msize);
4748 if (! REG_P (plus))
4749 {
4750 next = emit_move_ccc_after (mem.addr_reg, plus, add.insn);
4751 avr_dump (";; #4 new Add[%d] insn %d after %d: R%d += %d\n\n",
4752 add.regno, INSN_UID (next), INSN_UID (add.insn),
4753 add.regno, (int) INTVAL (XEXP (plus, 1)));
4754 gcc_assert (GET_CODE (plus) == PLUS);
4755 }
4756 else
4757 avr_dump (";; Add[%d] insn %d consumed into %d\n\n",
4758 add.regno, INSN_UID (add.insn), INSN_UID (insn));
4759
4760 add.set_deleted ();
4761 mem.set_deleted ();
4762
4763 return next;
4764 }
4765
4766 /* Try to post-reload combine PLUS with CONST_INt of pointer registers with:
4767 - Sets to a constant address.
4768 - PLUS insn of that kind.
4769 - Indirect loads and stores.
4770 In almost all cases, combine opportunities arise from the preparation
4771 done by `avr_split_fake_addressing_move', but in some rare cases combinations
4772 are found for the ordinary cores, too.
4773 As we consider at most one Mem insn per try, there may still be missed
4774 optimizations like POST_INC + PLUS + POST_INC might be performed
4775 as PRE_DEC + PRE_DEC for two adjacent locations. */
4776
4777 unsigned int
4778 avr_pass_fuse_add::execute1 (function *func)
4779 {
4780 df_note_add_problem ();
4781 df_analyze ();
4782
4783 int n_add = 0, n_mem = 0, n_ldi = 0;
4784 basic_block bb;
4785
4786 FOR_EACH_BB_FN (bb, func)
4787 {
4788 Ldi_Insn prev_ldi_insns[REG_32];
4789 Add_Insn prev_add_insns[REG_32];
4790 Mem_Insn prev_mem_insns[REG_32];
4791 rtx_insn *insn, *curr;
4792
4793 avr_dump ("\n;; basic block %d\n\n", bb->index);
4794
4795 FOR_BB_INSNS_SAFE (bb, insn, curr)
4796 {
4797 rtx_insn *next = nullptr;
4798 Ldi_Insn ldi_insn { insn };
4799 Add_Insn add_insn { insn };
4800 Mem_Insn mem_insn { insn };
4801
4802 if (add_insn.valid ())
4803 {
4804 // Found reg:HI += const_int
4805 avr_dump (";; insn %d: Add[%d]: R%d += %d\n\n",
4806 INSN_UID (add_insn.insn), add_insn.regno,
4807 add_insn.regno, (int) INTVAL (add_insn.addend));
4808 Ldi_Insn &prev_ldi_insn = prev_ldi_insns[add_insn.regno];
4809 Add_Insn &prev_add_insn = prev_add_insns[add_insn.regno];
4810 Mem_Insn &prev_mem_insn = prev_mem_insns[add_insn.regno];
4811 if ((next = fuse_ldi_add (prev_ldi_insn, add_insn)))
4812 curr = next, n_ldi += 1;
4813 else if ((next = fuse_add_add (prev_add_insn, add_insn)))
4814 curr = next, n_add += 1;
4815 else if ((next = fuse_mem_add (prev_mem_insn, add_insn)))
4816 curr = next, n_mem += 1;
4817 else
4818 prev_add_insn = add_insn;
4819 }
4820 else if (mem_insn.valid ())
4821 {
4822 int addr_regno = REGNO (mem_insn.addr_reg);
4823 avr_dump (";; insn %d: Mem[%d]: %r = %r\n\n",
4824 INSN_UID (mem_insn.insn), addr_regno,
4825 mem_insn.dest, mem_insn.src);
4826 Add_Insn &prev_add_insn = prev_add_insns[addr_regno];
4827 if ((next = fuse_add_mem (prev_add_insn, mem_insn)))
4828 curr = next, n_mem += 1;
4829 else
4830 prev_mem_insns[addr_regno] = mem_insn;
4831 }
4832 else if (ldi_insn.valid ())
4833 {
4834 if (! CONST_INT_P (ldi_insn.src))
4835 avr_dump (";; insn %d: Ldi[%d]: R%d = %r\n\n",
4836 INSN_UID (ldi_insn.insn), ldi_insn.regno,
4837 ldi_insn.regno, ldi_insn.src);
4838 prev_ldi_insns[ldi_insn.regno] = ldi_insn;
4839 }
4840 } // for insns
4841 } // for BBs
4842
4843 avr_dump (";; Function %f: Found %d changes: %d ldi, %d add, %d mem.\n",
4844 n_ldi + n_add + n_mem, n_ldi, n_add, n_mem);
4845
4846 return 0;
4847 }
4848
4849
4850
4851 //////////////////////////////////////////////////////////////////////////////
4852 // Fuse 2 move insns after combine.
4853
4854 static const pass_data avr_pass_data_2moves =
4855 {
4856 RTL_PASS, // type
4857 "", // name (will be patched)
4858 OPTGROUP_NONE, // optinfo_flags
4859 TV_DF_SCAN, // tv_id
4860 0, // properties_required
4861 0, // properties_provided
4862 0, // properties_destroyed
4863 0, // todo_flags_start
4864 0 // todo_flags_finish
4865 };
4866
4867 class avr_pass_2moves : public rtl_opt_pass
4868 {
4869 public:
4870 avr_pass_2moves (gcc::context *ctxt, const char *name)
4871 : rtl_opt_pass (avr_pass_data_2moves, ctxt)
4872 {
4873 this->name = name;
4874 }
4875
4876 unsigned int execute (function *func) final override
4877 {
4878 if (optimize && avropt_fuse_move2)
4879 {
4880 bool changed = false;
4881 basic_block bb;
4882
4883 FOR_EACH_BB_FN (bb, func)
4884 {
4885 changed |= optimize_2moves_bb (bb);
4886 }
4887
4888 if (changed)
4889 {
4890 df_note_add_problem ();
4891 df_analyze ();
4892 }
4893 }
4894
4895 return 0;
4896 }
4897
4898 bool optimize_2moves (rtx_insn *, rtx_insn *);
4899 bool optimize_2moves_bb (basic_block);
4900 }; // avr_pass_2moves
4901
4902 bool
4903 avr_pass_2moves::optimize_2moves_bb (basic_block bb)
4904 {
4905 bool changed = false;
4906 rtx_insn *insn1 = nullptr;
4907 rtx_insn *insn2 = nullptr;
4908 rtx_insn *curr;
4909
4910 FOR_BB_INSNS (bb, curr)
4911 {
4912 if (insn1 && INSN_P (insn1)
4913 && insn2 && INSN_P (insn2))
4914 changed |= optimize_2moves (insn1, insn2);
4915
4916 insn1 = insn2;
4917 insn2 = curr;
4918 }
4919
4920 return changed;
4921 }
4922
4923 bool
4924 avr_pass_2moves::optimize_2moves (rtx_insn *insn1, rtx_insn *insn2)
4925 {
4926 bool good = false;
4927 bool bad = false;
4928 rtx set1, dest1, src1;
4929 rtx set2, dest2, src2;
4930
4931 if ((set1 = single_set (insn1))
4932 && (set2 = single_set (insn2))
4933 && (src1 = SET_SRC (set1))
4934 && REG_P (src2 = SET_SRC (set2))
4935 && REG_P (dest1 = SET_DEST (set1))
4936 && REG_P (dest2 = SET_DEST (set2))
4937 && rtx_equal_p (dest1, src2)
4938 // Now we have:
4939 // insn1: dest1 = src1
4940 // insn2: dest2 = dest1
4941 && REGNO (dest1) >= FIRST_PSEUDO_REGISTER
4942 // Paranoia.
4943 && GET_CODE (PATTERN (insn1)) != PARALLEL
4944 && GET_CODE (PATTERN (insn2)) != PARALLEL
4945 && (rtx_equal_p (dest2, src1)
4946 || !reg_overlap_mentioned_p (dest2, src1)))
4947 {
4948 avr_dump ("\n;; Found 2moves:\n%r\n%r\n", insn1, insn2);
4949 avr_dump (";; reg %d: insn uses uids:", REGNO (dest1));
4950
4951 // Go check that dest1 is used exactly once, namely by insn2.
4952
4953 df_ref use = DF_REG_USE_CHAIN (REGNO (dest1));
4954 for (; use; use = DF_REF_NEXT_REG (use))
4955 {
4956 rtx_insn *user = DF_REF_INSN (use);
4957 avr_dump (" %d", INSN_UID (user));
4958 good |= INSN_UID (user) == INSN_UID (insn2);
4959 bad |= INSN_UID (user) != INSN_UID (insn2);
4960 }
4961 avr_dump (".\n");
4962
4963 if (good && !bad
4964 // Propagate src1 to insn2:
4965 // insn1: # Deleted
4966 // insn2: dest2 = src1
4967 && validate_change (insn2, &SET_SRC (set2), src1, false))
4968 {
4969 SET_INSN_DELETED (insn1);
4970 return true;
4971 }
4972 }
4973
4974 if (good && !bad)
4975 avr_dump (";; Failed\n");
4976
4977 return false;
4978 }
4979
4980
4981
4982 //////////////////////////////////////////////////////////////////////////////
4983 // Split insns with nonzero_bits() after combine.
4984
4985 static const pass_data avr_pass_data_split_nzb =
4986 {
4987 RTL_PASS, // type
4988 "", // name (will be patched)
4989 OPTGROUP_NONE, // optinfo_flags
4990 TV_DF_SCAN, // tv_id
4991 0, // properties_required
4992 0, // properties_provided
4993 0, // properties_destroyed
4994 0, // todo_flags_start
4995 0 // todo_flags_finish
4996 };
4997
4998 class avr_pass_split_nzb : public rtl_opt_pass
4999 {
5000 public:
5001 avr_pass_split_nzb (gcc::context *ctxt, const char *name)
5002 : rtl_opt_pass (avr_pass_data_split_nzb, ctxt)
5003 {
5004 this->name = name;
5005 }
5006
5007 unsigned int execute (function *) final override
5008 {
5009 if (avropt_use_nonzero_bits)
5010 split_nzb_insns ();
5011 return 0;
5012 }
5013
5014 void split_nzb_insns ();
5015
5016 }; // avr_pass_split_nzb
5017
5018
5019 void
5020 avr_pass_split_nzb::split_nzb_insns ()
5021 {
5022 rtx_insn *next;
5023
5024 for (rtx_insn *insn = get_insns (); insn; insn = next)
5025 {
5026 next = NEXT_INSN (insn);
5027
5028 if (INSN_P (insn)
5029 && single_set (insn)
5030 && get_attr_nzb (insn) == NZB_YES)
5031 {
5032 rtx_insn *last = try_split (PATTERN (insn), insn, 1 /*last*/);
5033
5034 // The nonzero_bits() insns *must* split. If not: ICE.
5035 if (last == insn)
5036 {
5037 debug_rtx (insn);
5038 internal_error ("failed to split insn");
5039 }
5040 }
5041 }
5042 }
5043
5044
5045
5046 //////////////////////////////////////////////////////////////////////////////
5047 // Split shift insns after peephole2 / befor avr-fuse-move.
5048
5049 static const pass_data avr_pass_data_split_after_peephole2 =
5050 {
5051 RTL_PASS, // type
5052 "", // name (will be patched)
5053 OPTGROUP_NONE, // optinfo_flags
5054 TV_DF_SCAN, // tv_id
5055 0, // properties_required
5056 0, // properties_provided
5057 0, // properties_destroyed
5058 0, // todo_flags_start
5059 0 // todo_flags_finish
5060 };
5061
5062 class avr_pass_split_after_peephole2 : public rtl_opt_pass
5063 {
5064 public:
5065 avr_pass_split_after_peephole2 (gcc::context *ctxt, const char *name)
5066 : rtl_opt_pass (avr_pass_data_split_after_peephole2, ctxt)
5067 {
5068 this->name = name;
5069 }
5070
5071 unsigned int execute (function *) final override
5072 {
5073 if (avr_shift_is_3op ())
5074 split_all_insns ();
5075 return 0;
5076 }
5077
5078 }; // avr_pass_split_after_peephole2
5079
5080 } // anonymous namespace
5081
5082
5083 /* Whether some shift insn alternatives are a `3op' 3-operand insn.
5084 This 3op alternatives allow the source and the destination register
5085 of the shift to be different right from the start, because the splitter
5086 will split the 3op shift into a 3-operand byte shift and a 2-operand
5087 residual bit shift. (When the residual shift has an offset of one
5088 less than the bitsize, then the residual shift is also a 3op insn.) */
5089
5090 bool
5091 avr_shift_is_3op ()
5092 {
5093 // Don't split for OPTIMIZE_SIZE_MAX (-Oz).
5094 // For OPTIMIZE_SIZE_BALANCED (-Os), we still split because
5095 // the size overhead (if at all) is marginal.
5096
5097 return (avropt_split_bit_shift
5098 && optimize > 0
5099 && avr_optimize_size_level () < OPTIMIZE_SIZE_MAX);
5100 }
5101
5102
5103 /* Implement constraints `C2a', `C2l', `C2r' ... `C4a', `C4l', `C4r'.
5104 Whether we split an N_BYTES shift of code CODE in { ASHIFTRT,
5105 LSHIFTRT, ASHIFT } into a byte shift and a residual bit shift. */
5106
5107 bool
5108 avr_split_shift_p (int n_bytes, int offset, rtx_code code)
5109 {
5110 gcc_assert (n_bytes == 4 || n_bytes == 3 || n_bytes == 2);
5111
5112 if (! avr_shift_is_3op ()
5113 || offset % 8 == 0)
5114 return false;
5115
5116 if (n_bytes == 4)
5117 return select<bool>()
5118 : code == ASHIFT ? IN_RANGE (offset, 9, 31) && offset != 15
5119 : code == ASHIFTRT ? IN_RANGE (offset, 9, 29) && offset != 15
5120 : code == LSHIFTRT ? IN_RANGE (offset, 9, 31) && offset != 15
5121 : bad_case<bool> ();
5122
5123 if (n_bytes == 3)
5124 return select<bool>()
5125 : code == ASHIFT ? IN_RANGE (offset, 9, 23) && offset != 15
5126 : code == ASHIFTRT ? IN_RANGE (offset, 9, 21) && offset != 15
5127 : code == LSHIFTRT ? IN_RANGE (offset, 9, 23) && offset != 15
5128 : bad_case<bool> ();
5129
5130 if (n_bytes == 2)
5131 return select<bool>()
5132 : code == ASHIFT ? IN_RANGE (offset, 9, 15)
5133 : code == ASHIFTRT ? IN_RANGE (offset, 9, 13)
5134 : code == LSHIFTRT ? IN_RANGE (offset, 9, 15)
5135 : bad_case<bool> ();
5136
5137 return false;
5138 }
5139
5140
5141 /* Emit a DEST = SRC <code> OFF shift of QImode, HImode or PSImode.
5142 SCRATCH is a QImode d-register, scratch:QI, or NULL_RTX.
5143 This function is used to emit shifts that have been split into
5144 a byte shift and a residual bit shift that operates on a mode
5145 strictly smaller than the original shift. */
5146
5147 static void
5148 avr_emit_shift (rtx_code code, rtx dest, rtx src, int off, rtx scratch)
5149 {
5150 const machine_mode mode = GET_MODE (dest);
5151 const int n_bits = GET_MODE_BITSIZE (mode);
5152 rtx xoff = GEN_INT (off);
5153
5154 // Work out which alternatives can handle 3 operands independent
5155 // of options.
5156
5157 const bool b8_is_3op = off == 6;
5158
5159 const bool b16_is_3op = select<bool>()
5160 : code == ASHIFT ? (satisfies_constraint_C7c (xoff) // 7...12
5161 // The "C05 C06" alternative of *ashlhi3_const.
5162 || (AVR_HAVE_MUL && scratch && (off == 5 || off == 6)))
5163 : code == LSHIFTRT ? satisfies_constraint_C7c (xoff)
5164 : code == ASHIFTRT ? off == 7
5165 : bad_case<bool> ();
5166
5167 const bool b24_is_3op = select<bool>()
5168 : code == ASHIFT ? off == 15
5169 : code == LSHIFTRT ? off == 15
5170 : code == ASHIFTRT ? false
5171 : bad_case<bool> ();
5172
5173 const bool is_3op = (off % 8 == 0
5174 || off == n_bits - 1
5175 || (code == ASHIFTRT && off == n_bits - 2)
5176 || (n_bits == 8 && b8_is_3op)
5177 || (n_bits == 16 && b16_is_3op)
5178 || (n_bits == 24 && b24_is_3op));
5179 rtx shift;
5180
5181 if (is_3op)
5182 {
5183 shift = gen_rtx_fmt_ee (code, mode, src, xoff);
5184 }
5185 else
5186 {
5187 if (REGNO (dest) != REGNO (src))
5188 emit_valid_move_clobbercc (dest, src);
5189 shift = gen_rtx_fmt_ee (code, mode, dest, xoff);
5190 }
5191
5192 if (n_bits == 8)
5193 // 8-bit shifts don't have a scratch operand.
5194 scratch = NULL_RTX;
5195 else if (! scratch && n_bits == 24)
5196 // 24-bit shifts always have a scratch operand.
5197 scratch = gen_rtx_SCRATCH (QImode);
5198
5199 emit_valid_move_clobbercc (dest, shift, scratch);
5200 }
5201
5202
5203 /* Handle the 4-byte case of avr_split_shift below:
5204 Split 4-byte shift DEST = SRC <code> IOFF into a 3-operand
5205 byte shift and a residual shift in a smaller mode if possible.
5206 SCRATCH is a QImode upper scratch register or NULL_RTX. */
5207
5208 static bool
5209 avr_split_shift4 (rtx dest, rtx src, int ioff, rtx scratch, rtx_code code)
5210 {
5211 gcc_assert (GET_MODE_SIZE (GET_MODE (dest)) == 4);
5212
5213 if (code == ASHIFT)
5214 {
5215 if (IN_RANGE (ioff, 25, 31))
5216 {
5217 rtx dst8 = avr_byte (dest, 3);
5218 rtx src8 = avr_byte (src, 0);
5219 avr_emit_shift (code, dst8, src8, ioff - 24, NULL_RTX);
5220 emit_valid_move_clobbercc (avr_byte (dest, 2), const0_rtx);
5221 emit_valid_move_clobbercc (avr_word (dest, 0), const0_rtx);
5222 return true;
5223 }
5224 else if (IN_RANGE (ioff, 17, 23))
5225 {
5226 rtx dst16 = avr_word (dest, 2);
5227 rtx src16 = avr_word (src, 0);
5228 avr_emit_shift (code, dst16, src16, ioff - 16, scratch);
5229 emit_valid_move_clobbercc (avr_word (dest, 0), const0_rtx);
5230 return true;
5231 }
5232 // ...the 9...14 cases are only handled by define_split because
5233 // for now, we don't exploit that the low byte is zero.
5234 }
5235 else if (code == ASHIFTRT
5236 || code == LSHIFTRT)
5237 {
5238 if (IN_RANGE (ioff, 25, 30 + (code == LSHIFTRT)))
5239 {
5240 rtx dst8 = avr_byte (dest, 0);
5241 rtx src8 = avr_byte (src, 3);
5242 avr_emit_shift (code, dst8, src8, ioff - 24, NULL_RTX);
5243 if (code == ASHIFTRT)
5244 {
5245 rtx signs = avr_byte (dest, 1);
5246 avr_emit_shift (code, signs, src8, 7, NULL_RTX);
5247 emit_valid_move_clobbercc (avr_byte (dest, 2), signs);
5248 emit_valid_move_clobbercc (avr_byte (dest, 3), signs);
5249 }
5250 else
5251 {
5252 emit_valid_move_clobbercc (avr_byte (dest, 1), const0_rtx);
5253 emit_valid_move_clobbercc (avr_word (dest, 2), const0_rtx);
5254 }
5255 return true;
5256 }
5257 else if (IN_RANGE (ioff, 17, 23))
5258 {
5259 rtx dst16 = avr_word (dest, 0);
5260 rtx src16 = avr_word (src, 2);
5261 avr_emit_shift (code, dst16, src16, ioff - 16, scratch);
5262 if (code == ASHIFTRT)
5263 {
5264 rtx msb = avr_byte (src, 3);
5265 rtx signs = avr_byte (dest, 2);
5266 avr_emit_shift (code, signs, msb, 7, NULL_RTX);
5267 emit_valid_move_clobbercc (avr_byte (dest, 3), signs);
5268 }
5269 else
5270 emit_valid_move_clobbercc (avr_word (dest, 2), const0_rtx);
5271
5272 return true;
5273 }
5274 else if (IN_RANGE (ioff, 9, 15))
5275 {
5276 avr_emit_shift (code, dest, src, 8, scratch);
5277 rtx dst24 = avr_chunk (PSImode, dest, 0);
5278 rtx src24 = avr_chunk (PSImode, dest, 0);
5279 avr_emit_shift (code, dst24, src24, ioff - 8, scratch);
5280 return true;
5281 }
5282 }
5283 else
5284 gcc_unreachable ();
5285
5286 return false;
5287 }
5288
5289
5290 /* Handle the 3-byte case of avr_split_shift below:
5291 Split 3-byte shift DEST = SRC <code> IOFF into a 3-operand
5292 byte shift and a residual shift in a smaller mode if possible.
5293 SCRATCH is a QImode upper scratch register or NULL_RTX. */
5294
5295 static bool
5296 avr_split_shift3 (rtx dest, rtx src, int ioff, rtx scratch, rtx_code code)
5297 {
5298 gcc_assert (GET_MODE_SIZE (GET_MODE (dest)) == 3);
5299
5300 if (code == ASHIFT)
5301 {
5302 if (IN_RANGE (ioff, 17, 23))
5303 {
5304 rtx dst8 = avr_byte (dest, 2);
5305 rtx src8 = avr_byte (src, 0);
5306 avr_emit_shift (code, dst8, src8, ioff - 16, NULL_RTX);
5307 emit_valid_move_clobbercc (avr_word (dest, 0), const0_rtx);
5308 return true;
5309 }
5310 // ...the 9...14 cases are only handled by define_split because
5311 // for now, we don't exploit that the low byte is zero.
5312 }
5313 else if (code == ASHIFTRT
5314 || code == LSHIFTRT)
5315 {
5316 if (IN_RANGE (ioff, 17, 22 + (code == LSHIFTRT)))
5317 {
5318 rtx dst8 = avr_byte (dest, 0);
5319 rtx src8 = avr_byte (src, 2);
5320 avr_emit_shift (code, dst8, src8, ioff - 16, NULL_RTX);
5321 if (code == ASHIFTRT)
5322 {
5323 rtx signs = avr_byte (dest, 1);
5324 avr_emit_shift (code, signs, src8, 7, NULL_RTX);
5325 emit_valid_move_clobbercc (avr_byte (dest, 2), signs);
5326 }
5327 else
5328 {
5329 emit_valid_move_clobbercc (avr_byte (dest, 1), const0_rtx);
5330 emit_valid_move_clobbercc (avr_byte (dest, 2), const0_rtx);
5331 }
5332 return true;
5333 }
5334 else if (IN_RANGE (ioff, 9, 15))
5335 {
5336 avr_emit_shift (code, dest, src, 8, scratch);
5337 rtx dst16 = avr_chunk (HImode, dest, 0);
5338 rtx src16 = avr_chunk (HImode, dest, 0);
5339 avr_emit_shift (code, dst16, src16, ioff - 8, scratch);
5340 return true;
5341 }
5342 }
5343 else
5344 gcc_unreachable ();
5345
5346 return false;
5347 }
5348
5349
5350 /* Handle the 2-byte case of avr_split_shift below:
5351 Split 2-byte shift DEST = SRC <code> IOFF into a 3-operand
5352 byte shift and a residual shift in a smaller mode if possible.
5353 SCRATCH is a QImode upper scratch register or NULL_RTX. */
5354
5355 static bool
5356 avr_split_shift2 (rtx dest, rtx src, int ioff, rtx /*scratch*/, rtx_code code)
5357 {
5358 gcc_assert (GET_MODE_SIZE (GET_MODE (dest)) == 2);
5359
5360 if (code == ASHIFT)
5361 {
5362 if (IN_RANGE (ioff, 9, 15))
5363 {
5364 rtx dst8 = avr_byte (dest, 1);
5365 rtx src8 = avr_byte (src, 0);
5366 avr_emit_shift (code, dst8, src8, ioff - 8, NULL_RTX);
5367 emit_valid_move_clobbercc (avr_byte (dest, 0), const0_rtx);
5368 return true;
5369 }
5370 }
5371 else if (code == ASHIFTRT
5372 || code == LSHIFTRT)
5373 {
5374 if (IN_RANGE (ioff, 9, 14 + (code == LSHIFTRT)))
5375 {
5376 rtx dst8 = avr_byte (dest, 0);
5377 rtx src8 = avr_byte (src, 1);
5378 rtx signs = const0_rtx;
5379 avr_emit_shift (code, dst8, src8, ioff - 8, NULL_RTX);
5380 if (code == ASHIFTRT)
5381 {
5382 signs = avr_byte (dest, 1);
5383 avr_emit_shift (code, signs, src8, 7, NULL_RTX);
5384 }
5385 emit_valid_move_clobbercc (avr_byte (dest, 1), signs);
5386 return true;
5387 }
5388 }
5389 else
5390 gcc_unreachable ();
5391
5392 return false;
5393 }
5394
5395
5396 /* Worker for a define_split that runs when -msplit-bit-shift is on.
5397 Split a shift of code CODE into a 3op byte shift and a residual bit shift.
5398 Return 'true' when a split has been performed and insns have been emitted.
5399 Otherwise, return 'false'. */
5400
5401 bool
5402 avr_split_shift (rtx xop[], rtx scratch, rtx_code code)
5403 {
5404 scratch = scratch && REG_P (scratch) ? scratch : NULL_RTX;
5405 rtx dest = xop[0];
5406 rtx src = xop[1];
5407 int ioff = INTVAL (xop[2]);
5408 int n_bytes = GET_MODE_SIZE (GET_MODE (dest));
5409
5410 return select<bool>()
5411 : n_bytes == 2 ? avr_split_shift2 (dest, src, ioff, scratch, code)
5412 : n_bytes == 3 ? avr_split_shift3 (dest, src, ioff, scratch, code)
5413 : n_bytes == 4 ? avr_split_shift4 (dest, src, ioff, scratch, code)
5414 : bad_case<bool> ();
5415 }
5416
5417
5418 namespace
5419 {
5420
5421
5422 //////////////////////////////////////////////////////////////////////////////
5423 // Determine whether an ISR may use the __gcc_isr pseudo-instruction.
5424
5425 static const pass_data avr_pass_data_pre_proep =
5426 {
5427 RTL_PASS, // type
5428 "", // name (will be patched)
5429 OPTGROUP_NONE, // optinfo_flags
5430 TV_DF_SCAN, // tv_id
5431 0, // properties_required
5432 0, // properties_provided
5433 0, // properties_destroyed
5434 0, // todo_flags_start
5435 0 // todo_flags_finish
5436 };
5437
5438 class avr_pass_pre_proep : public rtl_opt_pass
5439 {
5440 public:
5441 avr_pass_pre_proep (gcc::context *ctxt, const char *name)
5442 : rtl_opt_pass (avr_pass_data_pre_proep, ctxt)
5443 {
5444 this->name = name;
5445 }
5446
5447 void compute_maybe_gasisr (function *);
5448
5449 unsigned int execute (function *fun) final override
5450 {
5451 if (avropt_gasisr_prologues
5452 // Whether this function is an ISR worth scanning at all.
5453 && !fun->machine->is_no_gccisr
5454 && (fun->machine->is_interrupt
5455 || fun->machine->is_signal)
5456 && !cfun->machine->is_naked
5457 // Paranoia: Non-local gotos and labels that might escape.
5458 && !cfun->calls_setjmp
5459 && !cfun->has_nonlocal_label
5460 && !cfun->has_forced_label_in_static)
5461 {
5462 compute_maybe_gasisr (fun);
5463 }
5464
5465 return 0;
5466 }
5467
5468 }; // avr_pass_pre_proep
5469
5470
5471 /* Set fun->machine->gasisr.maybe provided we don't find anything that
5472 prohibits GAS generating parts of ISR prologues / epilogues for us. */
5473
5474 void
5475 avr_pass_pre_proep::compute_maybe_gasisr (function *fun)
5476 {
5477 // Don't use BB iterators so that we see JUMP_TABLE_DATA.
5478
5479 for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
5480 {
5481 // Transparent calls always use [R]CALL and are filtered out by GAS.
5482 // ISRs don't use -mcall-prologues, hence what remains to be filtered
5483 // out are open coded (tail) calls.
5484
5485 if (CALL_P (insn))
5486 return;
5487
5488 // __tablejump2__ clobbers something and is targeted by JMP so
5489 // that GAS won't see its usage.
5490
5491 if (AVR_HAVE_JMP_CALL
5492 && JUMP_TABLE_DATA_P (insn))
5493 return;
5494
5495 // Non-local gotos not seen in *FUN.
5496
5497 if (JUMP_P (insn)
5498 && find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
5499 return;
5500 }
5501
5502 fun->machine->gasisr.maybe = 1;
5503 }
5504
5505
5506
5507 //////////////////////////////////////////////////////////////////////////////
5508 // Late recomputation of notes so we can use `reg_unused_after()' and friends.
5509
5510 static const pass_data avr_pass_data_recompute_notes =
5511 {
5512 RTL_PASS, // type
5513 "", // name (will be patched)
5514 OPTGROUP_NONE, // optinfo_flags
5515 TV_DF_SCAN, // tv_id
5516 0, // properties_required
5517 0, // properties_provided
5518 0, // properties_destroyed
5519 0, // todo_flags_start
5520 TODO_df_finish | TODO_df_verify // todo_flags_finish
5521 };
5522
5523 class avr_pass_recompute_notes : public rtl_opt_pass
5524 {
5525 public:
5526 avr_pass_recompute_notes (gcc::context *ctxt, const char *name)
5527 : rtl_opt_pass (avr_pass_data_recompute_notes, ctxt)
5528 {
5529 this->name = name;
5530 }
5531
5532 unsigned int execute (function *) final override
5533 {
5534 df_note_add_problem ();
5535 df_analyze ();
5536
5537 return 0;
5538 }
5539 }; // avr_pass_recompute_notes
5540
5541 } // anonymous namespace
5542
5543
5544
5545 //////////////////////////////////////////////////////////////////////////////
5546 // Function visible and used outside this module.
5547
5548 /* During reload, we allow much more addresses than Reduced Tiny actually
5549 supports. Split them after reload in order to get closer to the
5550 core's capabilities. This sets the stage for pass .avr-fuse-add. */
5551
5552 bool
5553 avr_split_fake_addressing_move (rtx_insn * /*insn*/, rtx *xop)
5554 {
5555 bool store_p = false;
5556 rtx mem, reg_or_0;
5557
5558 if (REG_P (xop[0]) && MEM_P (xop[1]))
5559 {
5560 reg_or_0 = xop[0];
5561 mem = xop[1];
5562 }
5563 else if (MEM_P (xop[0])
5564 && (REG_P (xop[1])
5565 || xop[1] == CONST0_RTX (GET_MODE (xop[0]))))
5566 {
5567 mem = xop[0];
5568 reg_or_0 = xop[1];
5569 store_p = true;
5570 }
5571 else
5572 return false;
5573
5574 machine_mode mode = GET_MODE (mem);
5575 rtx base, addr = XEXP (mem, 0);
5576 rtx_code addr_code = GET_CODE (addr);
5577
5578 if (REG_P (reg_or_0)
5579 && reg_overlap_mentioned_p (reg_or_0, addr))
5580 return false;
5581 else if (addr_code == PLUS || addr_code == PRE_DEC || addr_code == POST_INC)
5582 base = XEXP (addr, 0);
5583 else if (addr_code == REG)
5584 base = addr;
5585 else
5586 return false;
5587
5588 if (REGNO (base) > REG_Z)
5589 return false;
5590
5591 if (! AVR_TINY
5592 // Only keep base registers that can't do PLUS addressing.
5593 && ((REGNO (base) != REG_X
5594 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem)))
5595 || avr_load_libgcc_p (mem)
5596 || avr_mem_memx_p (mem)))
5597 return false;
5598
5599 bool volatile_p = MEM_VOLATILE_P (mem);
5600 bool mem_volatile_p = false;
5601 if (frame_pointer_needed
5602 && REGNO (base) == FRAME_POINTER_REGNUM)
5603 {
5604 if (avropt_fuse_add < 2
5605 // Be a projection (we always split PLUS).
5606 || (avropt_fuse_add == 2 && volatile_p && addr_code != PLUS))
5607 return false;
5608
5609 // Changing the frame pointer locally may confuse later passes
5610 // like .dse2 which don't track changes of FP, not even when
5611 // respective CFA notes are present. An example is pr22141-1.c.
5612 if (avropt_fuse_add == 2)
5613 mem_volatile_p = true;
5614 }
5615
5616 rtx_code new_code = UNKNOWN;
5617 HOST_WIDE_INT add = 0, sub = 0;
5618 int msize = GET_MODE_SIZE (mode);
5619
5620 AVR_LdSt_Props ap { (int) REGNO (base), store_p, volatile_p,
5621 ADDR_SPACE_GENERIC };
5622
5623 switch (addr_code)
5624 {
5625 default:
5626 return false;
5627
5628 case PLUS:
5629 add = INTVAL (XEXP (addr, 1));
5630 if (msize == 1)
5631 {
5632 new_code = REG;
5633 sub = -add;
5634 }
5635 else if (ap.want_predec)
5636 {
5637 // volatile stores prefer PRE_DEC (MSB first)
5638 sub = -add;
5639 add += msize;
5640 new_code = PRE_DEC;
5641 }
5642 else
5643 {
5644 new_code = POST_INC;
5645 sub = -add - msize;
5646 }
5647 break;
5648
5649 case POST_INC:
5650 // volatile stores prefer PRE_DEC (MSB first)
5651 if (msize > 1 && ap.want_predec)
5652 {
5653 add = msize;
5654 new_code = PRE_DEC;
5655 sub = msize;
5656 break;
5657 }
5658 return false;
5659
5660 case PRE_DEC:
5661 // volatile loads prefer POST_INC (LSB first)
5662 if (msize > 1 && ap.want_postinc)
5663 {
5664 add = -msize;
5665 new_code = POST_INC;
5666 sub = -msize;
5667 break;
5668 }
5669 return false;
5670
5671 case REG:
5672 if (msize == 1)
5673 return false;
5674
5675 if (ap.want_predec)
5676 {
5677 add = msize;
5678 new_code = PRE_DEC;
5679 sub = 0;
5680 }
5681 else
5682 {
5683 add = 0;
5684 new_code = POST_INC;
5685 sub = -msize;
5686 }
5687 break;
5688 } // switch addr_code
5689
5690 rtx_insn *insn;
5691
5692 if (add)
5693 {
5694 insn = emit_move_ccc (base, plus_constant (Pmode, base, add));
5695 avr_maybe_adjust_cfa (insn, base, add);
5696 }
5697
5698 rtx new_addr = new_code == REG
5699 ? base
5700 : gen_rtx_fmt_e (new_code, Pmode, base);
5701
5702 rtx new_mem = change_address (mem, mode, new_addr);
5703 if (mem_volatile_p)
5704 MEM_VOLATILE_P (new_mem) = 1;
5705
5706 insn = emit_move_ccc (store_p ? new_mem : reg_or_0,
5707 store_p ? reg_or_0 : new_mem);
5708 if (auto_inc_p (new_addr))
5709 {
5710 add_reg_note (insn, REG_INC, base);
5711 int off = new_code == POST_INC ? msize : -msize;
5712 avr_maybe_adjust_cfa (insn, base, off);
5713 }
5714
5715 if (sub)
5716 {
5717 insn = emit_move_ccc (base, plus_constant (Pmode, base, sub));
5718 avr_maybe_adjust_cfa (insn, base, sub);
5719 }
5720
5721 return true;
5722 }
5723
5724
5725 /* Given memory reference mem(ADDR), return true when it can be split into
5726 single-byte moves, and all resulting addresses are natively supported.
5727 ADDR is in addr-space generic. */
5728
5729 static bool
5730 splittable_address_p (rtx addr, int n_bytes)
5731 {
5732 if (CONSTANT_ADDRESS_P (addr)
5733 || GET_CODE (addr) == PRE_DEC
5734 || GET_CODE (addr) == POST_INC)
5735 return true;
5736
5737 if (! AVR_TINY)
5738 {
5739 rtx base = select<rtx>()
5740 : REG_P (addr) ? addr
5741 : GET_CODE (addr) == PLUS ? XEXP (addr, 0)
5742 : NULL_RTX;
5743
5744 int off = select<int>()
5745 : REG_P (addr) ? 0
5746 : GET_CODE (addr) == PLUS ? (int) INTVAL (XEXP (addr, 1))
5747 : -1;
5748
5749 return (base && REG_P (base)
5750 && (REGNO (base) == REG_Y || REGNO (base) == REG_Z)
5751 && IN_RANGE (off, 0, 64 - n_bytes));
5752 }
5753
5754 return false;
5755 }
5756
5757
5758 /* Like avr_byte(), but also knows how to split POST_INC and PRE_DEC
5759 memory references. */
5760
5761 static rtx
5762 avr_byte_maybe_mem (rtx x, int n)
5763 {
5764 rtx addr, b;
5765 if (MEM_P (x)
5766 && (GET_CODE (addr = XEXP (x, 0)) == POST_INC
5767 || GET_CODE (addr) == PRE_DEC))
5768 b = gen_rtx_MEM (QImode, copy_rtx (addr));
5769 else
5770 b = avr_byte (x, n);
5771
5772 if (MEM_P (x))
5773 gcc_assert (MEM_P (b));
5774
5775 return b;
5776 }
5777
5778
5779 /* Split multi-byte load / stores into 1-byte such insns
5780 provided non-volatile, addr-space = generic, no reg-overlap
5781 and the resulting addressings are all natively supported.
5782 Returns true when the XOP[0] = XOP[1] insn has been split and
5783 false, otherwise. */
5784
5785 bool
5786 avr_split_ldst (rtx *xop)
5787 {
5788 rtx dest = xop[0];
5789 rtx src = xop[1];
5790 machine_mode mode = GET_MODE (dest);
5791 int n_bytes = GET_MODE_SIZE (mode);
5792 rtx mem, reg_or_0;
5793
5794 if (MEM_P (dest) && reg_or_0_operand (src, mode))
5795 {
5796 mem = dest;
5797 reg_or_0 = src;
5798 }
5799 else if (register_operand (dest, mode) && MEM_P (src))
5800 {
5801 reg_or_0 = dest;
5802 mem = src;
5803 }
5804 else
5805 return false;
5806
5807 rtx addr = XEXP (mem, 0);
5808
5809 if (MEM_VOLATILE_P (mem)
5810 || ! ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem))
5811 || ! IN_RANGE (n_bytes, 2, 4)
5812 || ! splittable_address_p (addr, n_bytes)
5813 || reg_overlap_mentioned_p (reg_or_0, addr))
5814 return false;
5815
5816 const int step = GET_CODE (addr) == PRE_DEC ? -1 : 1;
5817 const int istart = step > 0 ? 0 : n_bytes - 1;
5818 const int iend = istart + step * n_bytes;
5819
5820 for (int i = istart; i != iend; i += step)
5821 {
5822 rtx di = avr_byte_maybe_mem (dest, i);
5823 rtx si = avr_byte_maybe_mem (src, i);
5824 emit_move_ccc (di, si);
5825 }
5826
5827 return true;
5828 }
5829
5830
5831
5832 // Functions make_<pass-name> (gcc::context*) where <pass-name> is
5833 // according to the pass declaration in avr-passes.def. GCC's pass
5834 // manager uses these function to create the respective pass object.
5835
5836 // Optimize results of the casesi expander for modes < SImode.
5837
5838 rtl_opt_pass *
5839 make_avr_pass_casesi (gcc::context *ctxt)
5840 {
5841 return new avr_pass_casesi (ctxt, "avr-casesi");
5842 }
5843
5844 // Optimize 2 consecutive moves after combine.
5845
5846 rtl_opt_pass *
5847 make_avr_pass_2moves (gcc::context *ctxt)
5848 {
5849 return new avr_pass_2moves (ctxt, "avr-2moves");
5850 }
5851
5852 rtl_opt_pass *
5853 make_avr_pass_split_nzb (gcc::context *ctxt)
5854 {
5855 return new avr_pass_split_nzb (ctxt, "avr-split-nzb");
5856 }
5857
5858 // Try to replace 2 cbranch insns with 1 comparison and 2 branches.
5859
5860 rtl_opt_pass *
5861 make_avr_pass_ifelse (gcc::context *ctxt)
5862 {
5863 return new avr_pass_ifelse (ctxt, "avr-ifelse");
5864 }
5865
5866 // Determine whether an ISR may use the __gcc_isr pseudo-instruction.
5867
5868 rtl_opt_pass *
5869 make_avr_pass_pre_proep (gcc::context *ctxt)
5870 {
5871 return new avr_pass_pre_proep (ctxt, "avr-pre-proep");
5872 }
5873
5874 // Find more POST_INC and PRE_DEC cases.
5875
5876 rtl_opt_pass *
5877 make_avr_pass_fuse_add (gcc::context *ctxt)
5878 {
5879 return new avr_pass_fuse_add (ctxt, "avr-fuse-add");
5880 }
5881
5882 // Late recomputation of notes so we can use `reg_unused_after()' and friends.
5883
5884 rtl_opt_pass *
5885 make_avr_pass_recompute_notes (gcc::context *ctxt)
5886 {
5887 return new avr_pass_recompute_notes (ctxt, "avr-notes-free-cfg");
5888 }
5889
5890 // Optimize moves after reload.
5891
5892 rtl_opt_pass *
5893 make_avr_pass_fuse_move (gcc::context *ctxt)
5894 {
5895 return new avr_pass_fuse_move (ctxt, "avr-fuse-move");
5896 }
5897
5898 // Split insns after peephole2 / befor avr-fuse-move.
5899
5900 rtl_opt_pass *
5901 make_avr_pass_split_after_peephole2 (gcc::context *ctxt)
5902 {
5903 return new avr_pass_split_after_peephole2 (ctxt, "avr-split-after-peephole2");
5904 }