Debian Codegraph @

gcc/config/avr/avr-passes-fuse-move.h

raw
1 /* Support for avr-passes.cc 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
21 /* FIXME: The documentation in hard-reg-set.h is wrong in that it states
22 that HARD_REG_SET is a scalar iff HARD_REG_SET is a macro.
23 This is not the case: HARD_REG_SET is a typedef no matter what.
24 So in order to get the lower 32 bits (and maybe more) as a scalar
25 we have to invoke type traits as we can't #ifdef HARD_REG_SET */
26 template<typename T, typename ELT, bool = std::is_same<T, ELT>::value>
27 struct elt0_getter;
28
29 // All hard regs fit in one HARD_REG_ELT_TYPE: T === ELT.
30 template<typename T, typename ELT>
31 struct elt0_getter<T, ELT, true>
32 {
33 static inline const ELT &get (const T &t)
34 {
35 return t;
36 }
37 };
38
39 // HARD_REG_SET is not a scalar but a composite with HARD_REG_ELT_TYPE elts[].
40 template<typename T, typename ELT>
41 struct elt0_getter<T, ELT, false>
42 {
43 static inline const ELT &get (const T &t)
44 {
45 return t.elts[0];
46 }
47 };
48
49
50 // To track known values held in General Purpose Registers R2 ... R31.
51
52 struct memento_t
53 {
54 // One bit for each GPR.
55 gprmask_t known = 0;
56
57 std::array<uint8_t, REG_32> values;
58
59 static gprmask_t fixed_regs_mask;
60
61 void apply (const ply_t &);
62
63 void apply_insn (rtx_insn *insn, bool unused)
64 {
65 apply_insn1 (insn, unused);
66 known &= ~memento_t::fixed_regs_mask;
67 }
68
69 private:
70 void apply_insn1 (rtx_insn *, bool);
71
72 public:
73 bool knows (int rno, int n = 1) const
74 {
75 gcc_checking_assert (gpr_regno_p (rno, n));
76 const gprmask_t mask = regmask (rno, n);
77 return (known & mask) == mask;
78 }
79
80 uint8_t operator[] (int rno) const
81 {
82 gcc_checking_assert (gpr_regno_p (rno));
83 return values[rno];
84 }
85
86 // Set the 8-bit register number DEST as known to hold value VAL.
87 void set_value (int dest, int val)
88 {
89 gcc_checking_assert (gpr_regno_p (dest, 1));
90 values[dest] = (uint8_t) val;
91 set_known (dest);
92 }
93
94 void copy_value (int dest, int src)
95 {
96 gcc_checking_assert (gpr_regno_p (dest, 1));
97 gcc_checking_assert (gpr_regno_p (src, 1));
98 values[dest] = values[src];
99 set_known (dest, knows (src));
100 }
101
102 void copy_values (int dest, int src, int n_bytes)
103 {
104 gcc_checking_assert (gpr_regno_p (dest, n_bytes));
105 gcc_checking_assert (gpr_regno_p (src, n_bytes));
106 if (dest < src)
107 for (int n = 0; n < n_bytes; ++n)
108 copy_value (n + dest, n + src);
109 else if (dest > src)
110 for (int n = n_bytes - 1; n >= 0; --n)
111 copy_value (n + dest, n + src);
112 }
113
114 // Get the value as a CONST_INT or NULL_RTX when any byte is unknown.
115 rtx get_value_as_const_int (int regno, int n_bytes) const
116 {
117 gcc_checking_assert (gpr_regno_p (regno, n_bytes));
118
119 if (! knows (regno, n_bytes))
120 return NULL_RTX;
121
122 const machine_mode mode = size_to_mode (n_bytes);
123 uint64_t val = 0;
124
125 for (int i = n_bytes - 1; i >= 0; --i)
126 val = 256 * val + values[regno + i];
127
128 return gen_int_mode (val, mode);
129 }
130
131 // Copy the known state and the value (provided it is known) from
132 // register SRC to register DEST.
133 void copy_values (rtx dest, rtx src)
134 {
135 if (REG_P (dest) && REG_P (src)
136 && GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (GET_MODE (dest)))
137 {
138 int n_bytes = std::min (GET_MODE_SIZE (GET_MODE (src)),
139 GET_MODE_SIZE (GET_MODE (dest)));
140 copy_values (REGNO (dest), REGNO (src), n_bytes);
141 }
142 }
143
144 void set_values (rtx dest, rtx src)
145 {
146 gcc_assert (REG_P (dest) && CONST_INT_P (src));
147 int regno = REGNO (dest);
148 for (int i = 0; i < GET_MODE_SIZE (GET_MODE (dest)); ++i)
149 set_value (regno + i, avr_uint8 (src, i));
150 }
151
152 // Value >= 0 of the i-th reg or -1 if unknown.
153 int value (int i) const
154 {
155 gcc_checking_assert (gpr_regno_p (i));
156 return knows (i) ? (int) values[i] : -1;
157 }
158
159 // Value >= 0 of the rno-th reg[n] or -1 if unknown.
160 int64_t value (int rno, int n, bool strict = true) const
161 {
162 gcc_assert (n <= 4);
163 gcc_checking_assert (gpr_regno_p (rno, n));
164 if (! knows (rno, n))
165 {
166 if (! strict)
167 return -1;
168 gcc_unreachable ();
169 }
170
171 uint64_t val = 0;
172 for (int r = rno + n - 1; r >= rno; --r)
173 val = 256 * val + values[r];
174
175 return val;
176 }
177
178 void set_known (int r, bool kno = true)
179 {
180 gcc_checking_assert (gpr_regno_p (r));
181 known = kno
182 ? known | (1u << r)
183 : known & ~(1u << r);
184 }
185
186 void set_unknown (int r)
187 {
188 gcc_checking_assert (gpr_regno_p (r));
189 set_known (r, false);
190 }
191
192 int n_known () const
193 {
194 return popcount_hwi (known);
195 }
196
197 // Hamming byte distance of R[n] to VAL.
198 int hamming (int r, int n, uint64_t val) const
199 {
200 gcc_assert (n <= 8);
201 gcc_checking_assert (gpr_regno_p (r, n));
202
203 int ham = 0;
204 for (int i = 0; i < n; ++i)
205 ham += value (r + i) != (uint8_t) (val >> (8 * i));
206
207 return ham;
208 }
209
210 // Calculate the Hamming byte distance, ignoring regs in IGNORES.
211 int distance_to (const memento_t &that, gprmask_t ignores = 0) const
212 {
213 int d = 0;
214 for (int r = FIRST_GPR; r < REG_32; ++r)
215 if (! (ignores & (1u << r)))
216 d += value (r) != that.value (r);
217 return d;
218 }
219
220 // Return true when *this and THAT are the same, with the only allowed
221 // exceptions as of mask IGNORES.
222 bool equals (const memento_t &that, gprmask_t ignores) const
223 {
224 if ((known & ~ignores) != (that.known & ~ignores))
225 return false;
226
227 for (int r = FIRST_GPR; r < REG_32; ++r)
228 if (! (ignores & (1u << r)))
229 if (value (r) != that.value (r))
230 return false;
231
232 return true;
233 }
234
235 // Return TRUE iff the N_BYTES registers starting at REGNO are known
236 // to contain VAL.
237 bool have_value (int rno, int n_bytes, int val) const
238 {
239 gcc_assert (n_bytes <= 4);
240 for (int i = rno; i < rno + n_bytes; ++i)
241 if (value (i) != (uint8_t) val)
242 return false;
243 else
244 val >>= 8;
245
246 return true;
247 }
248
249 // The regno of a d-reg that has a known value, or 0 if none found.
250 int known_dregno (void) const
251 {
252 const gprmask_t dregs = known & 0xffff0000 & ~memento_t::fixed_regs_mask;
253 return dregs ? clz_hwi (1) - clz_hwi (dregs) : 0;
254 }
255
256 // Return a regno for a register that contains value VAL8 and that does
257 // not overlap with the registers mentioned in EXCLUDES. Else return 0.
258 int regno_with_value (uint8_t val8, gprmask_t excludes) const
259 {
260 for (int r = REG_31; r >= FIRST_GPR; --r)
261 if (value (r) == val8
262 && ! (regmask (r, 1) & excludes))
263 return r;
264 return 0;
265 }
266
267 // Return a regno for a 16-bit reg that contains value HI8:LO8 and that does
268 // not overlap with the registers mentioned in EXCLUDES. Else return 0.
269 int reg16_with_value (uint8_t lo8, uint8_t hi8, gprmask_t excludes) const
270 {
271 for (int r = REG_30; r >= FIRST_GPR; r -= 2)
272 if (! (regmask (r, 2) & excludes)
273 && value (r) == lo8
274 && value (r + 1) == hi8)
275 return r;
276 return 0;
277 }
278
279 void operator&= (const HARD_REG_SET &hrs)
280 {
281 known &= elt0_getter<HARD_REG_SET, HARD_REG_ELT_TYPE>::get (hrs);
282 }
283
284 // Coalesce register knowledge about *this and THAT.
285 void coalesce (const memento_t &that)
286 {
287 known &= that.known;
288
289 for (int i = FIRST_GPR; i < REG_32; ++i)
290 if (values[i] != that.values[i])
291 set_unknown (i);
292 }
293
294 void dump (const char *msg = nullptr, FILE *f = dump_file) const
295 {
296 if (f)
297 {
298 msg = msg && msg[0] ? msg : "%s\n";
299 const char *const xs = strstr (msg, "%s");
300 gcc_assert (xs);
301
302 fprintf (f, "%.*s", (int) (xs - msg), msg);
303 fprintf (f, " (%d known): ", n_known ());
304 for (int i = FIRST_GPR; i < REG_32; ++i)
305 if (knows (i))
306 fprintf (f, " r%d=%02x", i, values[i]);
307
308 fprintf (f, "%s", xs + strlen ("%s"));
309 }
310 }
311 }; // memento_t
312
313
314 // In avr-fuse-move, a possible step towards an optimal code sequence
315 // to load a compile-time constant. A ply_t represents one or two
316 // instructions. There are cases where there is no 1-to-1 correspondence
317 // between a ply_t and an insn; but a sequence of ply_ts can be mapped to
318 // a sequence of insns; though there are cases where 2 or more ply_ts map
319 // to a single insn and vice versa.
320
321 struct ply_t
322 {
323 // The destination register with .size in { 1, 2 }.
324 int regno;
325 int size;
326
327 // The performed operation where .arg represents an optional source operand.
328 // .code may be one of: SET (ldi, clr, ldi+mov), REG (mov, movw), NEG (neg),
329 // NOT (com), PRE_INC (inc), PRE_DEC (dec), ROTATE (swap), ASHIFT (lsl),
330 // LSHIFTRT (lsr), ASHIFTRT (asr), PLUS (add), MINUS (sub), AND (and),
331 // IOR (or), XOR (eor), SS_PLUS (adiw, sbiw), MOD (set+bld, clt+bld, bld).
332 rtx_code code;
333 int arg;
334
335 // Code size in terms of words / instructions. Extra costs for, say
336 // a CLT prior to a sequence of BLDs, are added to the 1st element.
337 int cost;
338
339 // We only consider ply_ts that reduce the Hamming distance by 0, 1 or 2.
340 // There are exotic cases where the Hamming distance temporarily increases,
341 // but we don't consider them. (They may fall out of the algorithm anyways,
342 // for example when a "set_some" insn is used that restores its scratch.
343 int dhamming = 1;
344
345 // Whether this is a SET that's intended for insn "set_some"'s payload.
346 bool in_set_some = false;
347
348 // 0 or an upper scratch register. One needed for SETs of a lower reg.
349 // SETs in a set_some don't need a scratch.
350 int scratch = 0;
351
352 // Statistics.
353 static int n_ply_ts;
354 static int max_n_ply_ts;
355
356 gprmask_t mask_dest () const
357 {
358 return regmask (regno, size);
359 }
360
361 gprmask_t mask_src () const
362 {
363 if (code == SET)
364 return 0;
365 else if (code == REG)
366 return regmask (arg, size);
367 else if (code == PLUS || code == MINUS || code == AND
368 || code == IOR || code == XOR)
369 return regmask (arg, size) | mask_dest ();
370 else
371 return mask_dest ();
372 }
373
374 bool is_movw () const
375 {
376 return size == 2 && code == REG;
377 }
378
379 bool is_adiw () const
380 {
381 return size == 2 && code == SS_PLUS;
382 }
383
384 bool is_bld () const
385 {
386 return code == MOD;
387 }
388
389 // A BLD setting one bit.
390 bool is_setbld () const
391 {
392 return is_bld () && popcount_hwi (arg) == 1;
393 }
394
395 // A BLD clearing one bit.
396 bool is_cltbld () const
397 {
398 return is_bld () && popcount_hwi (arg) == 7;
399 }
400
401 rtx_code bld_rtx_code () const
402 {
403 return select<rtx_code>()
404 : is_setbld () ? IOR
405 : is_cltbld () ? AND
406 : UNKNOWN;
407 }
408
409 // Is *P a BLD of the same kind?
410 bool is_same_bld (const ply_t *p) const
411 {
412 gcc_assert (is_bld ());
413 return p && bld_rtx_code () == p->bld_rtx_code ();
414 }
415
416 int bld_bitno () const
417 {
418 gcc_assert (is_bld ());
419 int bit = exact_log2 (popcount_hwi (arg) == 1 ? arg : 0xff ^ arg);
420 gcc_assert (IN_RANGE (bit, 0, 7));
421
422 return bit;
423 }
424
425 bool needs_scratch () const
426 {
427 return code == SET && AVRasm::ldi_needs_scratch (regno, arg);
428 }
429
430 // Return true when *this modifies (changes *AND* uses) the result
431 // generated by *P.
432 bool changes_result_of (const ply_t *p) const
433 {
434 return code != REG && code != SET && (mask_dest() & p->mask_dest());
435 }
436
437 bool overrides (const ply_t *p) const
438 {
439 return code == REG || code == SET
440 ? mask_dest () & p->mask_dest ()
441 : false;
442 }
443
444 bool commutes_with (const ply_t *p, int scratch = 0) const
445 {
446 if (code == SET || p->code == SET)
447 {
448 // SETs will be emit as a group where they commute.
449 if (code == SET && p->code == SET)
450 return true;
451
452 // Grant more flexibility to move around expensive SETs.
453 if (! scratch
454 && (needs_scratch () || p->needs_scratch ()))
455 return false;
456 }
457
458 if (is_bld () || p->is_bld ())
459 {
460 // BLD requires a previous SET or CLT which means that like
461 // BLDs should occur as a contiguous sequence. This limits
462 // re-ordering for the purpose of canonicalization of instruction
463 // ordering.
464 return ((is_cltbld () && p->is_cltbld ())
465 || (is_setbld () && p->is_setbld ()));
466 }
467
468 gprmask_t msrc = 1u << scratch;
469 gprmask_t m1 = mask_dest() | mask_src();
470 gprmask_t m2 = p->mask_dest() | p->mask_src();
471 return (m1 & m2) == 0 && ((m1 | m2) & msrc) == 0;
472 }
473
474 // Expected insn name; used in dumps.
475 const char *insn_name () const
476 {
477 if (code == SET)
478 return select<const char *>()
479 : in_set_some ? "set_some"
480 : scratch && needs_scratch () ? "*reload_inqi"
481 : "movqi_insn";
482
483 return "???";
484 }
485
486 void dump (int level = 0, FILE *f = dump_file) const
487 {
488 if (f)
489 {
490 if (level)
491 avr_fdump (f, ";; .%d ply_t R%d[%d] = %C", level, regno, size, code);
492 else
493 avr_fdump (f, ";; ply_t R%d[%d] = %C", regno, size, code);
494 if (code == REG || is_adiw ())
495 fprintf (f, " %d", arg);
496 else if (code == PLUS || code == MINUS || code == AND
497 || code == IOR || code == XOR)
498 fprintf (f, " R%d", arg);
499 else if (is_setbld ())
500 fprintf (f, " BLD |= 0x%02x", arg);
501 else if (is_cltbld ())
502 fprintf (f, " BLD &= 0x%02x", arg);
503 else
504 fprintf (f, " 0x%x = %d", arg, arg);
505
506 const char *const name = insn_name ();
507 fprintf (f, ", cost=%d, dhamm=%d", cost, dhamming);
508 if (name && name[0] != '?')
509 fprintf (f, ", \"%s\"", name);
510 fprintf (f, "\n");
511 }
512 }
513
514 // Helper for dump_plys: Value of the destination.
515 int dest_value (const memento_t &memo) const
516 {
517 return memo.value (regno, size);
518 }
519
520 // Helper for dump_plys: Value of 1st source arg provided it is a register.
521 int src1_value (const memento_t &memo) const
522 {
523 int rsrc = regno;
524
525 switch (code)
526 {
527 default:
528 return -1;
529
530 case REG:
531 gcc_assert (size == 1 || size == 2);
532 rsrc = arg;
533 break;
534
535 case SS_PLUS:
536 gcc_assert (size == 2);
537 break;
538
539 case NEG: case NOT: case PRE_DEC: case PRE_INC:
540 case ASHIFT: case LSHIFTRT: case ASHIFTRT: case ROTATE:
541 case AND: case IOR: case XOR: case MOD:
542 case PLUS: case MINUS:
543 gcc_assert (size == 1);
544 break;
545 }
546
547 return memo.value (rsrc, size);
548 }
549
550 // Helper for dump_plys: Value of 2nd source argument.
551 int src2_value (const memento_t &memo) const
552 {
553 switch (code)
554 {
555 default:
556 break;
557
558 case AND: case IOR: case XOR:
559 case PLUS: case MINUS:
560 gcc_assert (size == 1);
561
562 return memo.value (arg, 1);
563 }
564
565 return -1;
566 }
567
568 // Dumping a solution (or parts of it) is tedious because when
569 // their specific action should be displayed.
570 static void dump_plys (FILE *f, int level, int len,
571 const ply_t *const ps[], const memento_t &m0)
572 {
573 if (f)
574 {
575 memento_t memo = m0;
576
577 for (int i = 0; i < len; ++i)
578 ps[i]->dump (level, memo, f);
579 }
580 }
581
582 void dump (int level, memento_t &memo, FILE *f = dump_file) const
583 {
584 if (! f)
585 return;
586
587 const ply_t &p = *this;
588
589 // Keep track of chars in the current line for neat alignment.
590 int cs = level > 0
591 ? fprintf (f, ";; .%d ", level)
592 : fprintf (f, ";; ");
593 cs += fprintf (f, "ply_t %-4s R%d[%d] = ", p.mnemonic (), p.regno, p.size);
594
595 const int x = p.src1_value (memo);
596 const int y = p.src2_value (memo);
597
598 memo.apply (p);
599
600 const int z = p.dest_value (memo);
601
602 switch (p.code)
603 {
604 default:
605 fprintf (f, "%s ???", rtx_name[p.code]);
606 gcc_unreachable ();
607 break;
608
609 case REG:
610 cs += fprintf (f, "R%d = 0x%0*x", p.arg, 2 * p.size, x);
611 break;
612
613 case SET:
614 cs += fprintf (f, "0x%02x = %d, \"%s\"", p.arg, p.arg, insn_name ());
615 break;
616
617 case PRE_DEC: case PRE_INC:
618 case ASHIFT: case LSHIFTRT: case ASHIFTRT: case ROTATE:
619 cs += fprintf (f, "R%d %s = 0x%02x = 0x%02x %s",
620 p.regno, p.op_str (), z, x, p.op_str ());
621 break;
622
623 case NEG: case NOT:
624 cs += fprintf (f, "%sR%d = 0x%02x = %s0x%02x",
625 p.op_str (), p.regno, z, p.op_str (), x);
626 break;
627
628 case PLUS: case MINUS:
629 case AND: case IOR: case XOR:
630 cs += fprintf (f, "R%d %s R%d = 0x%02x = 0x%02x %s 0x%02x",
631 p.regno, p.op_str (), p.arg, z, x, p.op_str (), y);
632 break;
633
634 case SS_PLUS: // ADIW / SBIW
635 {
636 int arg = (int16_t) p.arg;
637 char op = arg < 0 ? '-' : '+';
638 cs += fprintf (f, "R%d %c %d = 0x%04x = 0x%04x %c %d", p.regno,
639 op, std::abs (arg), z, x, op, std::abs (arg));
640 }
641 break;
642
643 case MOD: // BLD
644 {
645 const char opc = "&|" [p.is_setbld ()];
646 cs += fprintf (f, "R%d %c 0x%02x = 0x%02x = 0x%02x %c bit%d",
647 p.regno, opc, p.arg, z, x, opc, p.bld_bitno ());
648 }
649 break;
650 }
651
652 cs += fprintf (f, ", ");
653
654 while (cs++ < 56)
655 fputc (' ', f);
656
657 fprintf (f, "cost=%d, dhamm=%d\n", p.cost, p.dhamming);
658 }
659
660 // AVR mnemnic; used in dumps.
661 const char *mnemonic () const
662 {
663 if (is_bld ())
664 {
665 static char s_bld[] = "BLD*";
666 s_bld[3] = '0' + bld_bitno ();
667 return s_bld;
668 }
669
670 return select<const char *>()
671 : code == LSHIFTRT ? "LSR"
672 : code == ASHIFTRT ? "ASR"
673 : code == ASHIFT ? "LSL"
674 : code == ROTATE ? "SWAP"
675 : code == PRE_DEC ? "DEC"
676 : code == PRE_INC ? "INC"
677 : code == MINUS ? "SUB"
678 : code == PLUS ? "ADD"
679 : code == NEG ? "NEG"
680 : code == NOT ? "COM"
681 : code == AND ? "AND"
682 : code == IOR ? "OR"
683 : code == XOR ? "EOR"
684 : code == REG ? size == 1 ? "MOV" : "MOVW"
685 : code == SET ? arg == 0 ? "CLR" : "LDI"
686 : code == SS_PLUS ? arg < 0 ? "SBIW" : "ADIW"
687 : rtx_name[code];
688 }
689
690 // Return a string of length 1 for CODE, or "?".
691 static const char *code_name_str1 (rtx_code code)
692 {
693 return select<const char *>()
694 : code == NEG ? "-"
695 : code == NOT ? "~"
696 : code == AND ? "&"
697 : code == IOR ? "|"
698 : code == XOR ? "^"
699 : code == PLUS ? "+"
700 : code == MINUS ? "-"
701 : "?";
702 }
703
704 // Short semantics representation used in dumps.
705 const char *op_str () const
706 {
707 return select<const char *>()
708 : code == LSHIFTRT ? ">> 1"
709 : code == ASHIFTRT ? ">> 1"
710 : code == ASHIFT ? "<< 1"
711 : code == ROTATE ? ">>> 4"
712 : code == PRE_DEC ? "- 1"
713 : code == PRE_INC ? "+ 1"
714 : code == SS_PLUS ? "+"
715 : *(ply_t::code_name_str1 (code)) != '?' ? ply_t::code_name_str1 (code)
716 : rtx_name[code];
717 }
718 }; // ply_t
719
720
721 // A set of ply_t's. We prefer std:array (with some expected upper
722 // bound for the number of ply_t's as generated by bbinfo_t::get_plies())
723 // over std::vector. That way, all plies_t are only allocated once as
724 // elements of avr_pass_fuse_move::BInfo.
725
726 struct plies_t
727 {
728 int n_plies;
729 std::array<ply_t, 50> plies;
730
731 int emit_insns (const insninfo_t &, const memento_t &) const;
732 int emit_sets (const insninfo_t&, int &n_insns, const memento_t&, int) const;
733 int emit_blds (const insninfo_t &, int &n_insns, int i0) const;
734 void add_plies_movw (int regno, int size, uint64_t, int, const memento_t &);
735
736 void reset ()
737 {
738 n_plies = 0;
739 }
740
741 void add (const ply_t &ply)
742 {
743 if (n_plies < (int) plies.size ())
744 {
745 plies[n_plies++] = ply;
746 ply_t::n_ply_ts += 1;
747 }
748 else
749 avr_dump (";; WARNING: plies_t is full\n");
750 }
751
752 void add (ply_t, const ply_t *prev, const memento_t &, bool maybe_set_some);
753
754 plies_t () {}
755
756 plies_t (int n, const ply_t *const ps[])
757 {
758 gcc_assert (n <= (int) plies.size ());
759 for (int i = 0; i < n; ++i)
760 plies[i] = *ps[i];
761 n_plies = n;
762 }
763
764 static int max_n_plies;
765 }; // plies_t
766
767
768 // An 8-bit value leaf of absint_byte_t.
769 // May be known to equal an 8-bit value.
770 // May be known to equal the content of an 8-bit GPR.
771 struct absint_val_t
772 {
773 int16_t val8 = -1;
774 int8_t regno = 0;
775
776 absint_val_t () {}
777
778 bool knows_val8 () const
779 {
780 gcc_assert (IN_RANGE (val8, -1, 0xff));
781 return val8 >= 0;
782 }
783
784 bool knows_regno () const
785 {
786 gcc_assert (IN_RANGE (regno, 0, REG_31));
787 return regno;
788 }
789
790 bool clueless () const
791 {
792 return ! knows_val8 () && ! knows_regno ();
793 }
794
795 gprmask_t reg_mask () const
796 {
797 return regno ? regmask (regno, 1) : 0;
798 }
799
800 void dump (FILE *f = dump_file) const
801 {
802 if (f)
803 {
804 if (knows_regno ())
805 fprintf (f, "r%d%s", regno, knows_val8 () ? "=" : "");
806 if (knows_val8 ())
807 fprintf (f, "%02x", val8);
808 else if (! knows_regno ())
809 fprintf (f, "--");
810 }
811 }
812 }; // absint_val_t
813
814
815 // One byte in AbsInt.
816 class absint_byte_t
817 {
818 // "SET": the value is .x0.
819 rtx_code code = UNKNOWN;
820 absint_val_t x0;
821 absint_val_t x1;
822
823 public:
824
825 const absint_val_t &arg (int i) const
826 {
827 gcc_assert (IN_RANGE (i, 0, arity () - 1));
828 return i == 1 ? x1 : x0;
829 }
830
831 rtx_code get_code () const
832 {
833 return code;
834 }
835
836 absint_byte_t () {}
837
838 absint_byte_t (absint_val_t x)
839 : code(x.clueless () ? UNKNOWN : SET), x0(x)
840 {}
841
842 // new = <code> A0 where CODE is a unary operation.
843 absint_byte_t (rtx_code c, const absint_byte_t &a0)
844 : code(c)
845 {
846 switch (code)
847 {
848 default:
849 gcc_unreachable ();
850
851 case NOT:
852 if (a0.can (CONST_INT))
853 init_val8 (absint_byte_t::eval (code, a0.val8 ()));
854 else if (a0.can (REG))
855 x0 = a0.x0;
856 else if (a0.can (NOT))
857 init_regno (a0.regno ());
858 else
859 code = UNKNOWN;
860 break;
861
862 case SIGN_EXTEND:
863 if (a0.can (CONST_INT))
864 init_val8 (absint_byte_t::eval (code, a0.val8 ()));
865 else if (a0.can (REG))
866 x0 = a0.x0;
867 else
868 code = UNKNOWN;
869 break;
870 }
871 }
872
873 // new = A0 <code> A1 where CODE is a binary operation.
874 absint_byte_t (rtx_code c, const absint_byte_t &a0, const absint_byte_t &a1)
875 : code(c)
876 {
877 gcc_assert (c == AND || c == IOR || c == XOR || code == PLUS);
878
879 if (a1.is_image1 (c))
880 *this = a1;
881 else if (a0.is_image1 (c))
882 *this = a0;
883 else if (a1.is_neutral (c))
884 *this = a0;
885 else if (a0.is_neutral (c))
886 *this = a1;
887 else if (a0.can (CONST_INT) && a1.can (CONST_INT))
888 init_val8 (absint_byte_t::eval (code, a0.val8 (), a1.val8 ()));
889 else if (a0.can (REG) && a1.can (CONST_INT))
890 {
891 x0 = a0.x0;
892 x1 = a1.x0;
893 if (code == XOR && a1.val8 () == 0xff)
894 code = NOT;
895 }
896 else if (a0.can (CONST_INT) && a1.can (REG))
897 {
898 x0 = a1.x0;
899 x1 = a0.x0;
900 if (code == XOR && a0.val8 () == 0xff)
901 code = NOT;
902 }
903 else if (a0.can (REG) && a1.can (REG))
904 {
905 x0.regno = std::min (a0.regno (), a1.regno ());
906 x1.regno = std::max (a0.regno (), a1.regno ());
907 }
908 else
909 code = UNKNOWN;
910 }
911
912 int arity () const
913 {
914 return select<int>()
915 : code == UNKNOWN ? 0
916 : code == SET || code == NOT || code == SIGN_EXTEND ? 1
917 : code == AND || code == IOR || code == XOR || code == PLUS ? 2
918 : bad_case<int> ();
919 }
920
921 // Return a byte with 8 signs according to code CODE.
922 absint_byte_t get_signs (rtx_code ext) const
923 {
924 return select<absint_byte_t>()
925 : ext == ZERO_EXTEND ? absint_byte_t::from_val8 (0)
926 : ext == SIGN_EXTEND ? absint_byte_t (SIGN_EXTEND, *this)
927 : ext == LSHIFTRT ? absint_byte_t::from_val8 (0)
928 : ext == ASHIFTRT ? absint_byte_t (SIGN_EXTEND, *this)
929 : bad_case<absint_byte_t> ();
930 }
931
932 gprmask_t reg_mask () const
933 {
934 return select<gprmask_t>()
935 : code == SET ? x0.reg_mask ()
936 : arity () == 1 ? x0.reg_mask ()
937 : arity () == 2 ? x0.reg_mask () | x1.reg_mask ()
938 : bad_case<gprmask_t> ();
939 }
940
941 bool check () const
942 {
943 return select<bool>()
944 : arity () >= 1 && x0.clueless () ? false
945 : arity () == 2 && x1.clueless () ? false
946 : true;
947 }
948
949 static inline uint8_t eval (rtx_code code, uint8_t x)
950 {
951 return select<int>()
952 : code == NOT ? ~x
953 : code == SIGN_EXTEND ? (x >= 0x80 ? 0xff : 0x00)
954 : bad_case<int> ();
955 }
956
957 static inline uint8_t eval (rtx_code code, uint8_t x, uint8_t y)
958 {
959 return select<int>()
960 : code == AND ? x & y
961 : code == IOR ? x | y
962 : code == XOR ? x ^ y
963 : code == PLUS ? x + y
964 : bad_case<int> ();
965 }
966
967 bool is_neutral (rtx_code c) const
968 {
969 return can (CONST_INT) && val8 () == AVRasm::neutral_val (c);
970 }
971
972 bool is_image1 (rtx_code c) const
973 {
974 return can (CONST_INT) && val8 () == AVRasm::image1_val (c);
975 }
976
977 bool can (rtx_code c) const
978 {
979 if (code == SET)
980 gcc_assert (IN_RANGE (x0.val8, 0, 0xff) || gpr_regno_p (x0.regno));
981
982 if (c == CONST_INT)
983 return code == SET && x0.knows_val8 ();
984 else if (c == REG)
985 return code == SET && x0.knows_regno ();
986 else if (c == VALUE)
987 return code != UNKNOWN;
988 else if (c == UNKNOWN
989 || c == SET || c == NOT || c == SIGN_EXTEND
990 || c == AND || c == IOR || c == XOR || c == PLUS)
991 return code == c;
992
993 gcc_unreachable ();
994 }
995
996 // Return the known byte value in 0...0xff, or -1 if unknown and ! STRICT.
997 int val8 (bool strict = true) const
998 {
999 gcc_assert (! strict || code == SET);
1000 gcc_assert (! strict || can (CONST_INT));
1001 return can (CONST_INT) ? x0.val8 : -1;
1002 }
1003
1004 int regno (bool strict = true) const
1005 {
1006 gcc_assert (! strict || code == SET);
1007 gcc_assert (! strict || can (REG));
1008 return can (REG) ? x0.regno : 0;
1009 }
1010
1011 void init_val8 (int v)
1012 {
1013 gcc_assert (IN_RANGE (v, 0, 0xff));
1014 x0.val8 = v;
1015 x0.regno = 0;
1016 code = SET;
1017 }
1018
1019 void init_regno (int r)
1020 {
1021 gcc_assert (gpr_regno_p (r));
1022 x0.val8 = -1;
1023 x0.regno = r;
1024 code = SET;
1025 }
1026
1027 void learn_val8 (int v)
1028 {
1029 gcc_assert (IN_RANGE (v, 0, 0xff));
1030 gcc_assert (code == SET || code == UNKNOWN);
1031 x0.val8 = v;
1032 code = SET;
1033 }
1034
1035 void learn_regno (int r)
1036 {
1037 gcc_assert (gpr_regno_p (r));
1038 gcc_assert (code == SET || code == UNKNOWN);
1039 x0.regno = r;
1040 code = SET;
1041 }
1042
1043 static inline absint_byte_t from_val8 (int val, bool strict = true)
1044 {
1045 gcc_assert (IN_RANGE (val, -1, 0xff));
1046 gcc_assert (! strict || val >= 0);
1047 absint_byte_t b;
1048 if (val >= 0)
1049 b.init_val8 (val);
1050
1051 return b;
1052 }
1053
1054 // Return a SET rtx that can replace the set_src of INSN.
1055 // Returns BINARY_P or NULL_RTX.
1056 absint_byte_t find_alternative_binary (const memento_t &memo) const
1057 {
1058 gprmask_t excludes = x1.knows_regno () ? regmask (x1.regno, 1) : 0;
1059 absint_byte_t alt = *this;
1060
1061 if (arity () == 2
1062 && x0.knows_regno ()
1063 && x1.knows_val8 ()
1064 && (! x1.knows_regno () || x0.regno != x1.regno)
1065 && (alt.x1.regno = memo.regno_with_value (x1.val8, excludes)))
1066 {
1067 if (dump_flags & TDF_FOLDING)
1068 {
1069 alt.dump (";; AI.alternative AI=[%s]");
1070 dump (" can replace AI=[%s]\n");
1071 }
1072
1073 return alt;
1074 }
1075
1076 return absint_byte_t {};
1077 }
1078
1079 rtx to_rtx () const
1080 {
1081 if (arity () == 2)
1082 {
1083 gcc_assert (x0.knows_regno ());
1084 gcc_assert (x1.knows_regno ());
1085 rtx op0 = gen_rtx_REG (QImode, x0.regno);
1086 rtx op1 = gen_rtx_REG (QImode, x1.regno);
1087 return gen_rtx_fmt_ee (code, QImode, op0, op1);
1088 }
1089
1090 gcc_unreachable ();
1091 }
1092
1093 void dump (const char *msg = nullptr, FILE *f = dump_file) const
1094 {
1095 if (f)
1096 {
1097 msg = msg && msg[0] ? msg : "%s";
1098 const char *const xs = strstr (msg, "%s");
1099 gcc_assert (xs);
1100
1101 fprintf (f, "%.*s", (int) (xs - msg), msg);
1102 if (code == UNKNOWN)
1103 fprintf (f, "--");
1104 else if (code == SET)
1105 x0.dump (f);
1106 else if (code == NOT)
1107 {
1108 fprintf (f, "~");
1109 x0.dump (f);
1110 }
1111 else if (code == SIGN_EXTEND)
1112 {
1113 fprintf (f, "signs(");
1114 x0.dump (f);
1115 fprintf (f, ")");
1116 }
1117 else if (arity () == 2)
1118 {
1119 x0.dump (f);
1120 fprintf (f, "%s", ply_t::code_name_str1 (code));
1121 x1.dump (f);
1122 }
1123 else
1124 gcc_unreachable ();
1125
1126 fprintf (f, "%s", xs + strlen ("%s"));
1127 }
1128 }
1129 }; // absint_byte_t
1130
1131
1132 struct bbinfo_t
1133 {
1134 // All BBs of the current function.
1135 static bbinfo_t *bb_info;
1136
1137 // bbinfo_t holds additional information for this basic block.
1138 basic_block bb;
1139
1140 // Known values held in GPRs.
1141 memento_t regs;
1142
1143 // Represents the "time" when the value was set. When we have the choice
1144 // between several registers to copy from, we use the first (oldest) set.
1145 // This can avoid copy-chains.
1146 std::array<int, REG_32> ticks;
1147 static int tick;
1148
1149 // Whether according BB is done and optimized.
1150 bool done;
1151
1152 static void optimize_one_function (function *func);
1153 void optimize_one_block (bool &changed);
1154 void enter ();
1155 void leave ();
1156
1157 // Used when finding a best plies_t. This object is only needed
1158 // once and can be shared between all basic blocks.
1159 struct find_plies_data_t
1160 {
1161 // These are used by [run_]find_plies()
1162 const ply_t *ply_stack[N_BEST_PLYS];
1163 plies_t plies[N_BEST_PLYS];
1164 plies_t solution;
1165 // Register knowledge at start of recursive algo.
1166 memento_t regs0;
1167 int max_ply_cost;
1168 int movmode_cost;
1169 int n_best_plys;
1170 int n_get_plies; // Only for bookkeeping / statistics.
1171 }; // find_plies_data_t
1172
1173 static find_plies_data_t *fpd;
1174 static bool try_fuse_p;
1175 static bool try_mem0_p;
1176 static bool try_bin_arg1_p;
1177 static bool try_simplify_p;
1178 static bool try_split_ldi_p;
1179 static bool try_split_any_p;
1180 static bool use_arith_p;
1181 static bool use_set_some_p;
1182
1183 static void get_plies (plies_t &, const insninfo_t &, const memento_t &,
1184 const ply_t *);
1185 static void find_plies (int depth, const insninfo_t &, const memento_t &);
1186 bool run_find_plies (const insninfo_t &, const memento_t &) const;
1187 }; // bbinfo_t