]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/combine.c
Warning fixes:
[thirdparty/gcc.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987, 88, 92-97, 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC 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 2, or (at your option)
9 any later version.
10
11 GNU CC 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 GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23 Portable Optimizer, but redone to work on our list-structured
24 representation for RTL instead of their string representation.
25
26 The LOG_LINKS of each insn identify the most recent assignment
27 to each REG used in the insn. It is a list of previous insns,
28 each of which contains a SET for a REG that is used in this insn
29 and not used or set in between. LOG_LINKs never cross basic blocks.
30 They were set up by the preceding pass (lifetime analysis).
31
32 We try to combine each pair of insns joined by a logical link.
33 We also try to combine triples of insns A, B and C when
34 C has a link back to B and B has a link back to A.
35
36 LOG_LINKS does not have links for use of the CC0. They don't
37 need to, because the insn that sets the CC0 is always immediately
38 before the insn that tests it. So we always regard a branch
39 insn as having a logical link to the preceding insn. The same is true
40 for an insn explicitly using CC0.
41
42 We check (with use_crosses_set_p) to avoid combining in such a way
43 as to move a computation to a place where its value would be different.
44
45 Combination is done by mathematically substituting the previous
46 insn(s) values for the regs they set into the expressions in
47 the later insns that refer to these regs. If the result is a valid insn
48 for our target machine, according to the machine description,
49 we install it, delete the earlier insns, and update the data flow
50 information (LOG_LINKS and REG_NOTES) for what we did.
51
52 There are a few exceptions where the dataflow information created by
53 flow.c aren't completely updated:
54
55 - reg_live_length is not updated
56 - reg_n_refs is not adjusted in the rare case when a register is
57 no longer required in a computation
58 - there are extremely rare cases (see distribute_regnotes) when a
59 REG_DEAD note is lost
60 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
61 removed because there is no way to know which register it was
62 linking
63
64 To simplify substitution, we combine only when the earlier insn(s)
65 consist of only a single assignment. To simplify updating afterward,
66 we never combine when a subroutine call appears in the middle.
67
68 Since we do not represent assignments to CC0 explicitly except when that
69 is all an insn does, there is no LOG_LINKS entry in an insn that uses
70 the condition code for the insn that set the condition code.
71 Fortunately, these two insns must be consecutive.
72 Therefore, every JUMP_INSN is taken to have an implicit logical link
73 to the preceding insn. This is not quite right, since non-jumps can
74 also use the condition code; but in practice such insns would not
75 combine anyway. */
76
77 #include "config.h"
78 #ifdef __STDC__
79 #include <stdarg.h>
80 #else
81 #include <varargs.h>
82 #endif
83
84 /* stdio.h must precede rtl.h for FFS. */
85 #include "system.h"
86
87 #include "rtl.h"
88 #include "flags.h"
89 #include "regs.h"
90 #include "hard-reg-set.h"
91 #include "basic-block.h"
92 #include "insn-config.h"
93 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
94 #include "expr.h"
95 #include "insn-flags.h"
96 #include "insn-codes.h"
97 #include "insn-attr.h"
98 #include "recog.h"
99 #include "real.h"
100
101 /* It is not safe to use ordinary gen_lowpart in combine.
102 Use gen_lowpart_for_combine instead. See comments there. */
103 #define gen_lowpart dont_use_gen_lowpart_you_dummy
104
105 /* Number of attempts to combine instructions in this function. */
106
107 static int combine_attempts;
108
109 /* Number of attempts that got as far as substitution in this function. */
110
111 static int combine_merges;
112
113 /* Number of instructions combined with added SETs in this function. */
114
115 static int combine_extras;
116
117 /* Number of instructions combined in this function. */
118
119 static int combine_successes;
120
121 /* Totals over entire compilation. */
122
123 static int total_attempts, total_merges, total_extras, total_successes;
124
125 /* Define a default value for REVERSIBLE_CC_MODE.
126 We can never assume that a condition code mode is safe to reverse unless
127 the md tells us so. */
128 #ifndef REVERSIBLE_CC_MODE
129 #define REVERSIBLE_CC_MODE(MODE) 0
130 #endif
131 \f
132 /* Vector mapping INSN_UIDs to cuids.
133 The cuids are like uids but increase monotonically always.
134 Combine always uses cuids so that it can compare them.
135 But actually renumbering the uids, which we used to do,
136 proves to be a bad idea because it makes it hard to compare
137 the dumps produced by earlier passes with those from later passes. */
138
139 static int *uid_cuid;
140 static int max_uid_cuid;
141
142 /* Get the cuid of an insn. */
143
144 #define INSN_CUID(INSN) \
145 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
146
147 /* Maximum register number, which is the size of the tables below. */
148
149 static int combine_max_regno;
150
151 /* Record last point of death of (hard or pseudo) register n. */
152
153 static rtx *reg_last_death;
154
155 /* Record last point of modification of (hard or pseudo) register n. */
156
157 static rtx *reg_last_set;
158
159 /* Record the cuid of the last insn that invalidated memory
160 (anything that writes memory, and subroutine calls, but not pushes). */
161
162 static int mem_last_set;
163
164 /* Record the cuid of the last CALL_INSN
165 so we can tell whether a potential combination crosses any calls. */
166
167 static int last_call_cuid;
168
169 /* When `subst' is called, this is the insn that is being modified
170 (by combining in a previous insn). The PATTERN of this insn
171 is still the old pattern partially modified and it should not be
172 looked at, but this may be used to examine the successors of the insn
173 to judge whether a simplification is valid. */
174
175 static rtx subst_insn;
176
177 /* This is an insn that belongs before subst_insn, but is not currently
178 on the insn chain. */
179
180 static rtx subst_prev_insn;
181
182 /* This is the lowest CUID that `subst' is currently dealing with.
183 get_last_value will not return a value if the register was set at or
184 after this CUID. If not for this mechanism, we could get confused if
185 I2 or I1 in try_combine were an insn that used the old value of a register
186 to obtain a new value. In that case, we might erroneously get the
187 new value of the register when we wanted the old one. */
188
189 static int subst_low_cuid;
190
191 /* This contains any hard registers that are used in newpat; reg_dead_at_p
192 must consider all these registers to be always live. */
193
194 static HARD_REG_SET newpat_used_regs;
195
196 /* This is an insn to which a LOG_LINKS entry has been added. If this
197 insn is the earlier than I2 or I3, combine should rescan starting at
198 that location. */
199
200 static rtx added_links_insn;
201
202 /* Basic block number of the block in which we are performing combines. */
203 static int this_basic_block;
204 \f
205 /* The next group of arrays allows the recording of the last value assigned
206 to (hard or pseudo) register n. We use this information to see if a
207 operation being processed is redundant given a prior operation performed
208 on the register. For example, an `and' with a constant is redundant if
209 all the zero bits are already known to be turned off.
210
211 We use an approach similar to that used by cse, but change it in the
212 following ways:
213
214 (1) We do not want to reinitialize at each label.
215 (2) It is useful, but not critical, to know the actual value assigned
216 to a register. Often just its form is helpful.
217
218 Therefore, we maintain the following arrays:
219
220 reg_last_set_value the last value assigned
221 reg_last_set_label records the value of label_tick when the
222 register was assigned
223 reg_last_set_table_tick records the value of label_tick when a
224 value using the register is assigned
225 reg_last_set_invalid set to non-zero when it is not valid
226 to use the value of this register in some
227 register's value
228
229 To understand the usage of these tables, it is important to understand
230 the distinction between the value in reg_last_set_value being valid
231 and the register being validly contained in some other expression in the
232 table.
233
234 Entry I in reg_last_set_value is valid if it is non-zero, and either
235 reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
236
237 Register I may validly appear in any expression returned for the value
238 of another register if reg_n_sets[i] is 1. It may also appear in the
239 value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
240 reg_last_set_invalid[j] is zero.
241
242 If an expression is found in the table containing a register which may
243 not validly appear in an expression, the register is replaced by
244 something that won't match, (clobber (const_int 0)).
245
246 reg_last_set_invalid[i] is set non-zero when register I is being assigned
247 to and reg_last_set_table_tick[i] == label_tick. */
248
249 /* Record last value assigned to (hard or pseudo) register n. */
250
251 static rtx *reg_last_set_value;
252
253 /* Record the value of label_tick when the value for register n is placed in
254 reg_last_set_value[n]. */
255
256 static int *reg_last_set_label;
257
258 /* Record the value of label_tick when an expression involving register n
259 is placed in reg_last_set_value. */
260
261 static int *reg_last_set_table_tick;
262
263 /* Set non-zero if references to register n in expressions should not be
264 used. */
265
266 static char *reg_last_set_invalid;
267
268 /* Incremented for each label. */
269
270 static int label_tick;
271
272 /* Some registers that are set more than once and used in more than one
273 basic block are nevertheless always set in similar ways. For example,
274 a QImode register may be loaded from memory in two places on a machine
275 where byte loads zero extend.
276
277 We record in the following array what we know about the nonzero
278 bits of a register, specifically which bits are known to be zero.
279
280 If an entry is zero, it means that we don't know anything special. */
281
282 static unsigned HOST_WIDE_INT *reg_nonzero_bits;
283
284 /* Mode used to compute significance in reg_nonzero_bits. It is the largest
285 integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
286
287 static enum machine_mode nonzero_bits_mode;
288
289 /* Nonzero if we know that a register has some leading bits that are always
290 equal to the sign bit. */
291
292 static char *reg_sign_bit_copies;
293
294 /* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
295 It is zero while computing them and after combine has completed. This
296 former test prevents propagating values based on previously set values,
297 which can be incorrect if a variable is modified in a loop. */
298
299 static int nonzero_sign_valid;
300
301 /* These arrays are maintained in parallel with reg_last_set_value
302 and are used to store the mode in which the register was last set,
303 the bits that were known to be zero when it was last set, and the
304 number of sign bits copies it was known to have when it was last set. */
305
306 static enum machine_mode *reg_last_set_mode;
307 static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
308 static char *reg_last_set_sign_bit_copies;
309 \f
310 /* Record one modification to rtl structure
311 to be undone by storing old_contents into *where.
312 is_int is 1 if the contents are an int. */
313
314 struct undo
315 {
316 struct undo *next;
317 int is_int;
318 union {rtx r; int i;} old_contents;
319 union {rtx *r; int *i;} where;
320 };
321
322 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
323 num_undo says how many are currently recorded.
324
325 storage is nonzero if we must undo the allocation of new storage.
326 The value of storage is what to pass to obfree.
327
328 other_insn is nonzero if we have modified some other insn in the process
329 of working on subst_insn. It must be verified too.
330
331 previous_undos is the value of undobuf.undos when we started processing
332 this substitution. This will prevent gen_rtx_combine from re-used a piece
333 from the previous expression. Doing so can produce circular rtl
334 structures. */
335
336 struct undobuf
337 {
338 char *storage;
339 struct undo *undos;
340 struct undo *frees;
341 struct undo *previous_undos;
342 rtx other_insn;
343 };
344
345 static struct undobuf undobuf;
346
347 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
348 insn. The substitution can be undone by undo_all. If INTO is already
349 set to NEWVAL, do not record this change. Because computing NEWVAL might
350 also call SUBST, we have to compute it before we put anything into
351 the undo table. */
352
353 #define SUBST(INTO, NEWVAL) \
354 do { rtx _new = (NEWVAL); \
355 struct undo *_buf; \
356 \
357 if (undobuf.frees) \
358 _buf = undobuf.frees, undobuf.frees = _buf->next; \
359 else \
360 _buf = (struct undo *) xmalloc (sizeof (struct undo)); \
361 \
362 _buf->is_int = 0; \
363 _buf->where.r = &INTO; \
364 _buf->old_contents.r = INTO; \
365 INTO = _new; \
366 if (_buf->old_contents.r == INTO) \
367 _buf->next = undobuf.frees, undobuf.frees = _buf; \
368 else \
369 _buf->next = undobuf.undos, undobuf.undos = _buf; \
370 } while (0)
371
372 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
373 for the value of a HOST_WIDE_INT value (including CONST_INT) is
374 not safe. */
375
376 #define SUBST_INT(INTO, NEWVAL) \
377 do { struct undo *_buf; \
378 \
379 if (undobuf.frees) \
380 _buf = undobuf.frees, undobuf.frees = _buf->next; \
381 else \
382 _buf = (struct undo *) xmalloc (sizeof (struct undo)); \
383 \
384 _buf->is_int = 1; \
385 _buf->where.i = (int *) &INTO; \
386 _buf->old_contents.i = INTO; \
387 INTO = NEWVAL; \
388 if (_buf->old_contents.i == INTO) \
389 _buf->next = undobuf.frees, undobuf.frees = _buf; \
390 else \
391 _buf->next = undobuf.undos, undobuf.undos = _buf; \
392 } while (0)
393
394 /* Number of times the pseudo being substituted for
395 was found and replaced. */
396
397 static int n_occurrences;
398
399 static void init_reg_last_arrays PROTO((void));
400 static void setup_incoming_promotions PROTO((void));
401 static void set_nonzero_bits_and_sign_copies PROTO((rtx, rtx));
402 static int can_combine_p PROTO((rtx, rtx, rtx, rtx, rtx *, rtx *));
403 static int sets_function_arg_p PROTO((rtx));
404 static int combinable_i3pat PROTO((rtx, rtx *, rtx, rtx, int, rtx *));
405 static rtx try_combine PROTO((rtx, rtx, rtx));
406 static void undo_all PROTO((void));
407 static rtx *find_split_point PROTO((rtx *, rtx));
408 static rtx subst PROTO((rtx, rtx, rtx, int, int));
409 static rtx simplify_rtx PROTO((rtx, enum machine_mode, int, int));
410 static rtx simplify_if_then_else PROTO((rtx));
411 static rtx simplify_set PROTO((rtx));
412 static rtx simplify_logical PROTO((rtx, int));
413 static rtx expand_compound_operation PROTO((rtx));
414 static rtx expand_field_assignment PROTO((rtx));
415 static rtx make_extraction PROTO((enum machine_mode, rtx, int, rtx, int,
416 int, int, int));
417 static rtx extract_left_shift PROTO((rtx, int));
418 static rtx make_compound_operation PROTO((rtx, enum rtx_code));
419 static int get_pos_from_mask PROTO((unsigned HOST_WIDE_INT, int *));
420 static rtx force_to_mode PROTO((rtx, enum machine_mode,
421 unsigned HOST_WIDE_INT, rtx, int));
422 static rtx if_then_else_cond PROTO((rtx, rtx *, rtx *));
423 static rtx known_cond PROTO((rtx, enum rtx_code, rtx, rtx));
424 static int rtx_equal_for_field_assignment_p PROTO((rtx, rtx));
425 static rtx make_field_assignment PROTO((rtx));
426 static rtx apply_distributive_law PROTO((rtx));
427 static rtx simplify_and_const_int PROTO((rtx, enum machine_mode, rtx,
428 unsigned HOST_WIDE_INT));
429 static unsigned HOST_WIDE_INT nonzero_bits PROTO((rtx, enum machine_mode));
430 static int num_sign_bit_copies PROTO((rtx, enum machine_mode));
431 static int merge_outer_ops PROTO((enum rtx_code *, HOST_WIDE_INT *,
432 enum rtx_code, HOST_WIDE_INT,
433 enum machine_mode, int *));
434 static rtx simplify_shift_const PROTO((rtx, enum rtx_code, enum machine_mode,
435 rtx, int));
436 static int recog_for_combine PROTO((rtx *, rtx, rtx *, int *));
437 static rtx gen_lowpart_for_combine PROTO((enum machine_mode, rtx));
438 static rtx gen_rtx_combine PVPROTO((enum rtx_code code, enum machine_mode mode,
439 ...));
440 static rtx gen_binary PROTO((enum rtx_code, enum machine_mode,
441 rtx, rtx));
442 static rtx gen_unary PROTO((enum rtx_code, enum machine_mode,
443 enum machine_mode, rtx));
444 static enum rtx_code simplify_comparison PROTO((enum rtx_code, rtx *, rtx *));
445 static int reversible_comparison_p PROTO((rtx));
446 static void update_table_tick PROTO((rtx));
447 static void record_value_for_reg PROTO((rtx, rtx, rtx));
448 static void record_dead_and_set_regs_1 PROTO((rtx, rtx));
449 static void record_dead_and_set_regs PROTO((rtx));
450 static int get_last_value_validate PROTO((rtx *, rtx, int, int));
451 static rtx get_last_value PROTO((rtx));
452 static int use_crosses_set_p PROTO((rtx, int));
453 static void reg_dead_at_p_1 PROTO((rtx, rtx));
454 static int reg_dead_at_p PROTO((rtx, rtx));
455 static void move_deaths PROTO((rtx, rtx, int, rtx, rtx *));
456 static int reg_bitfield_target_p PROTO((rtx, rtx));
457 static void distribute_notes PROTO((rtx, rtx, rtx, rtx, rtx, rtx));
458 static void distribute_links PROTO((rtx));
459 static void mark_used_regs_combine PROTO((rtx));
460 static int insn_cuid PROTO((rtx));
461 \f
462 /* Main entry point for combiner. F is the first insn of the function.
463 NREGS is the first unused pseudo-reg number. */
464
465 void
466 combine_instructions (f, nregs)
467 rtx f;
468 int nregs;
469 {
470 register rtx insn, next;
471 #ifdef HAVE_cc0
472 register rtx prev;
473 #endif
474 register int i;
475 register rtx links, nextlinks;
476
477 combine_attempts = 0;
478 combine_merges = 0;
479 combine_extras = 0;
480 combine_successes = 0;
481 undobuf.undos = undobuf.previous_undos = 0;
482
483 combine_max_regno = nregs;
484
485 reg_nonzero_bits
486 = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
487 reg_sign_bit_copies = (char *) alloca (nregs * sizeof (char));
488
489 bzero ((char *) reg_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
490 bzero (reg_sign_bit_copies, nregs * sizeof (char));
491
492 reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
493 reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
494 reg_last_set_value = (rtx *) alloca (nregs * sizeof (rtx));
495 reg_last_set_table_tick = (int *) alloca (nregs * sizeof (int));
496 reg_last_set_label = (int *) alloca (nregs * sizeof (int));
497 reg_last_set_invalid = (char *) alloca (nregs * sizeof (char));
498 reg_last_set_mode
499 = (enum machine_mode *) alloca (nregs * sizeof (enum machine_mode));
500 reg_last_set_nonzero_bits
501 = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
502 reg_last_set_sign_bit_copies
503 = (char *) alloca (nregs * sizeof (char));
504
505 init_reg_last_arrays ();
506
507 init_recog_no_volatile ();
508
509 /* Compute maximum uid value so uid_cuid can be allocated. */
510
511 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
512 if (INSN_UID (insn) > i)
513 i = INSN_UID (insn);
514
515 uid_cuid = (int *) alloca ((i + 1) * sizeof (int));
516 max_uid_cuid = i;
517
518 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
519
520 /* Don't use reg_nonzero_bits when computing it. This can cause problems
521 when, for example, we have j <<= 1 in a loop. */
522
523 nonzero_sign_valid = 0;
524
525 /* Compute the mapping from uids to cuids.
526 Cuids are numbers assigned to insns, like uids,
527 except that cuids increase monotonically through the code.
528
529 Scan all SETs and see if we can deduce anything about what
530 bits are known to be zero for some registers and how many copies
531 of the sign bit are known to exist for those registers.
532
533 Also set any known values so that we can use it while searching
534 for what bits are known to be set. */
535
536 label_tick = 1;
537
538 /* We need to initialize it here, because record_dead_and_set_regs may call
539 get_last_value. */
540 subst_prev_insn = NULL_RTX;
541
542 setup_incoming_promotions ();
543
544 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
545 {
546 uid_cuid[INSN_UID (insn)] = ++i;
547 subst_low_cuid = i;
548 subst_insn = insn;
549
550 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
551 {
552 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies);
553 record_dead_and_set_regs (insn);
554
555 #ifdef AUTO_INC_DEC
556 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
557 if (REG_NOTE_KIND (links) == REG_INC)
558 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX);
559 #endif
560 }
561
562 if (GET_CODE (insn) == CODE_LABEL)
563 label_tick++;
564 }
565
566 nonzero_sign_valid = 1;
567
568 /* Now scan all the insns in forward order. */
569
570 this_basic_block = -1;
571 label_tick = 1;
572 last_call_cuid = 0;
573 mem_last_set = 0;
574 init_reg_last_arrays ();
575 setup_incoming_promotions ();
576
577 for (insn = f; insn; insn = next ? next : NEXT_INSN (insn))
578 {
579 next = 0;
580
581 /* If INSN starts a new basic block, update our basic block number. */
582 if (this_basic_block + 1 < n_basic_blocks
583 && basic_block_head[this_basic_block + 1] == insn)
584 this_basic_block++;
585
586 if (GET_CODE (insn) == CODE_LABEL)
587 label_tick++;
588
589 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
590 {
591 /* Try this insn with each insn it links back to. */
592
593 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
594 if ((next = try_combine (insn, XEXP (links, 0), NULL_RTX)) != 0)
595 goto retry;
596
597 /* Try each sequence of three linked insns ending with this one. */
598
599 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
600 for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
601 nextlinks = XEXP (nextlinks, 1))
602 if ((next = try_combine (insn, XEXP (links, 0),
603 XEXP (nextlinks, 0))) != 0)
604 goto retry;
605
606 #ifdef HAVE_cc0
607 /* Try to combine a jump insn that uses CC0
608 with a preceding insn that sets CC0, and maybe with its
609 logical predecessor as well.
610 This is how we make decrement-and-branch insns.
611 We need this special code because data flow connections
612 via CC0 do not get entered in LOG_LINKS. */
613
614 if (GET_CODE (insn) == JUMP_INSN
615 && (prev = prev_nonnote_insn (insn)) != 0
616 && GET_CODE (prev) == INSN
617 && sets_cc0_p (PATTERN (prev)))
618 {
619 if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
620 goto retry;
621
622 for (nextlinks = LOG_LINKS (prev); nextlinks;
623 nextlinks = XEXP (nextlinks, 1))
624 if ((next = try_combine (insn, prev,
625 XEXP (nextlinks, 0))) != 0)
626 goto retry;
627 }
628
629 /* Do the same for an insn that explicitly references CC0. */
630 if (GET_CODE (insn) == INSN
631 && (prev = prev_nonnote_insn (insn)) != 0
632 && GET_CODE (prev) == INSN
633 && sets_cc0_p (PATTERN (prev))
634 && GET_CODE (PATTERN (insn)) == SET
635 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
636 {
637 if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
638 goto retry;
639
640 for (nextlinks = LOG_LINKS (prev); nextlinks;
641 nextlinks = XEXP (nextlinks, 1))
642 if ((next = try_combine (insn, prev,
643 XEXP (nextlinks, 0))) != 0)
644 goto retry;
645 }
646
647 /* Finally, see if any of the insns that this insn links to
648 explicitly references CC0. If so, try this insn, that insn,
649 and its predecessor if it sets CC0. */
650 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
651 if (GET_CODE (XEXP (links, 0)) == INSN
652 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
653 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
654 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
655 && GET_CODE (prev) == INSN
656 && sets_cc0_p (PATTERN (prev))
657 && (next = try_combine (insn, XEXP (links, 0), prev)) != 0)
658 goto retry;
659 #endif
660
661 /* Try combining an insn with two different insns whose results it
662 uses. */
663 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
664 for (nextlinks = XEXP (links, 1); nextlinks;
665 nextlinks = XEXP (nextlinks, 1))
666 if ((next = try_combine (insn, XEXP (links, 0),
667 XEXP (nextlinks, 0))) != 0)
668 goto retry;
669
670 if (GET_CODE (insn) != NOTE)
671 record_dead_and_set_regs (insn);
672
673 retry:
674 ;
675 }
676 }
677
678 total_attempts += combine_attempts;
679 total_merges += combine_merges;
680 total_extras += combine_extras;
681 total_successes += combine_successes;
682
683 nonzero_sign_valid = 0;
684 }
685
686 /* Wipe the reg_last_xxx arrays in preparation for another pass. */
687
688 static void
689 init_reg_last_arrays ()
690 {
691 int nregs = combine_max_regno;
692
693 bzero ((char *) reg_last_death, nregs * sizeof (rtx));
694 bzero ((char *) reg_last_set, nregs * sizeof (rtx));
695 bzero ((char *) reg_last_set_value, nregs * sizeof (rtx));
696 bzero ((char *) reg_last_set_table_tick, nregs * sizeof (int));
697 bzero ((char *) reg_last_set_label, nregs * sizeof (int));
698 bzero (reg_last_set_invalid, nregs * sizeof (char));
699 bzero ((char *) reg_last_set_mode, nregs * sizeof (enum machine_mode));
700 bzero ((char *) reg_last_set_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
701 bzero (reg_last_set_sign_bit_copies, nregs * sizeof (char));
702 }
703 \f
704 /* Set up any promoted values for incoming argument registers. */
705
706 static void
707 setup_incoming_promotions ()
708 {
709 #ifdef PROMOTE_FUNCTION_ARGS
710 int regno;
711 rtx reg;
712 enum machine_mode mode;
713 int unsignedp;
714 rtx first = get_insns ();
715
716 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
717 if (FUNCTION_ARG_REGNO_P (regno)
718 && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
719 {
720 record_value_for_reg
721 (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
722 : SIGN_EXTEND),
723 GET_MODE (reg),
724 gen_rtx_CLOBBER (mode, const0_rtx)));
725 }
726 #endif
727 }
728 \f
729 /* Called via note_stores. If X is a pseudo that is narrower than
730 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
731
732 If we are setting only a portion of X and we can't figure out what
733 portion, assume all bits will be used since we don't know what will
734 be happening.
735
736 Similarly, set how many bits of X are known to be copies of the sign bit
737 at all locations in the function. This is the smallest number implied
738 by any set of X. */
739
740 static void
741 set_nonzero_bits_and_sign_copies (x, set)
742 rtx x;
743 rtx set;
744 {
745 int num;
746
747 if (GET_CODE (x) == REG
748 && REGNO (x) >= FIRST_PSEUDO_REGISTER
749 /* If this register is undefined at the start of the file, we can't
750 say what its contents were. */
751 && ! REGNO_REG_SET_P (basic_block_live_at_start[0], REGNO (x))
752 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
753 {
754 if (set == 0 || GET_CODE (set) == CLOBBER)
755 {
756 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
757 reg_sign_bit_copies[REGNO (x)] = 1;
758 return;
759 }
760
761 /* If this is a complex assignment, see if we can convert it into a
762 simple assignment. */
763 set = expand_field_assignment (set);
764
765 /* If this is a simple assignment, or we have a paradoxical SUBREG,
766 set what we know about X. */
767
768 if (SET_DEST (set) == x
769 || (GET_CODE (SET_DEST (set)) == SUBREG
770 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
771 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
772 && SUBREG_REG (SET_DEST (set)) == x))
773 {
774 rtx src = SET_SRC (set);
775
776 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
777 /* If X is narrower than a word and SRC is a non-negative
778 constant that would appear negative in the mode of X,
779 sign-extend it for use in reg_nonzero_bits because some
780 machines (maybe most) will actually do the sign-extension
781 and this is the conservative approach.
782
783 ??? For 2.5, try to tighten up the MD files in this regard
784 instead of this kludge. */
785
786 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
787 && GET_CODE (src) == CONST_INT
788 && INTVAL (src) > 0
789 && 0 != (INTVAL (src)
790 & ((HOST_WIDE_INT) 1
791 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
792 src = GEN_INT (INTVAL (src)
793 | ((HOST_WIDE_INT) (-1)
794 << GET_MODE_BITSIZE (GET_MODE (x))));
795 #endif
796
797 reg_nonzero_bits[REGNO (x)]
798 |= nonzero_bits (src, nonzero_bits_mode);
799 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
800 if (reg_sign_bit_copies[REGNO (x)] == 0
801 || reg_sign_bit_copies[REGNO (x)] > num)
802 reg_sign_bit_copies[REGNO (x)] = num;
803 }
804 else
805 {
806 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
807 reg_sign_bit_copies[REGNO (x)] = 1;
808 }
809 }
810 }
811 \f
812 /* See if INSN can be combined into I3. PRED and SUCC are optionally
813 insns that were previously combined into I3 or that will be combined
814 into the merger of INSN and I3.
815
816 Return 0 if the combination is not allowed for any reason.
817
818 If the combination is allowed, *PDEST will be set to the single
819 destination of INSN and *PSRC to the single source, and this function
820 will return 1. */
821
822 static int
823 can_combine_p (insn, i3, pred, succ, pdest, psrc)
824 rtx insn;
825 rtx i3;
826 rtx pred, succ;
827 rtx *pdest, *psrc;
828 {
829 int i;
830 rtx set = 0, src, dest;
831 rtx p;
832 #ifdef AUTO_INC_DEC
833 rtx link;
834 #endif
835 int all_adjacent = (succ ? (next_active_insn (insn) == succ
836 && next_active_insn (succ) == i3)
837 : next_active_insn (insn) == i3);
838
839 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
840 or a PARALLEL consisting of such a SET and CLOBBERs.
841
842 If INSN has CLOBBER parallel parts, ignore them for our processing.
843 By definition, these happen during the execution of the insn. When it
844 is merged with another insn, all bets are off. If they are, in fact,
845 needed and aren't also supplied in I3, they may be added by
846 recog_for_combine. Otherwise, it won't match.
847
848 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
849 note.
850
851 Get the source and destination of INSN. If more than one, can't
852 combine. */
853
854 if (GET_CODE (PATTERN (insn)) == SET)
855 set = PATTERN (insn);
856 else if (GET_CODE (PATTERN (insn)) == PARALLEL
857 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
858 {
859 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
860 {
861 rtx elt = XVECEXP (PATTERN (insn), 0, i);
862
863 switch (GET_CODE (elt))
864 {
865 /* This is important to combine floating point insns
866 for the SH4 port. */
867 case USE:
868 /* Combining an isolated USE doesn't make sense.
869 We depend here on combinable_i3_pat to reject them. */
870 /* The code below this loop only verifies that the inputs of
871 the SET in INSN do not change. We call reg_set_between_p
872 to verify that the REG in the USE does not change betweeen
873 I3 and INSN.
874 If the USE in INSN was for a pseudo register, the matching
875 insn pattern will likely match any register; combining this
876 with any other USE would only be safe if we knew that the
877 used registers have identical values, or if there was
878 something to tell them apart, e.g. different modes. For
879 now, we forgo such compilcated tests and simply disallow
880 combining of USES of pseudo registers with any other USE. */
881 if (GET_CODE (XEXP (elt, 0)) == REG
882 && GET_CODE (PATTERN (i3)) == PARALLEL)
883 {
884 rtx i3pat = PATTERN (i3);
885 int i = XVECLEN (i3pat, 0) - 1;
886 int regno = REGNO (XEXP (elt, 0));
887 do
888 {
889 rtx i3elt = XVECEXP (i3pat, 0, i);
890 if (GET_CODE (i3elt) == USE
891 && GET_CODE (XEXP (i3elt, 0)) == REG
892 && (REGNO (XEXP (i3elt, 0)) == regno
893 ? reg_set_between_p (XEXP (elt, 0),
894 PREV_INSN (insn), i3)
895 : regno >= FIRST_PSEUDO_REGISTER))
896 return 0;
897 }
898 while (--i >= 0);
899 }
900 break;
901
902 /* We can ignore CLOBBERs. */
903 case CLOBBER:
904 break;
905
906 case SET:
907 /* Ignore SETs whose result isn't used but not those that
908 have side-effects. */
909 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
910 && ! side_effects_p (elt))
911 break;
912
913 /* If we have already found a SET, this is a second one and
914 so we cannot combine with this insn. */
915 if (set)
916 return 0;
917
918 set = elt;
919 break;
920
921 default:
922 /* Anything else means we can't combine. */
923 return 0;
924 }
925 }
926
927 if (set == 0
928 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
929 so don't do anything with it. */
930 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
931 return 0;
932 }
933 else
934 return 0;
935
936 if (set == 0)
937 return 0;
938
939 set = expand_field_assignment (set);
940 src = SET_SRC (set), dest = SET_DEST (set);
941
942 /* Don't eliminate a store in the stack pointer. */
943 if (dest == stack_pointer_rtx
944 /* If we couldn't eliminate a field assignment, we can't combine. */
945 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == STRICT_LOW_PART
946 /* Don't combine with an insn that sets a register to itself if it has
947 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
948 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
949 /* Can't merge a function call. */
950 || GET_CODE (src) == CALL
951 /* Don't eliminate a function call argument. */
952 || (GET_CODE (i3) == CALL_INSN
953 && (find_reg_fusage (i3, USE, dest)
954 || (GET_CODE (dest) == REG
955 && REGNO (dest) < FIRST_PSEUDO_REGISTER
956 && global_regs[REGNO (dest)])))
957 /* Don't substitute into an incremented register. */
958 || FIND_REG_INC_NOTE (i3, dest)
959 || (succ && FIND_REG_INC_NOTE (succ, dest))
960 /* Don't combine the end of a libcall into anything. */
961 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
962 /* Make sure that DEST is not used after SUCC but before I3. */
963 || (succ && ! all_adjacent
964 && reg_used_between_p (dest, succ, i3))
965 /* Make sure that the value that is to be substituted for the register
966 does not use any registers whose values alter in between. However,
967 If the insns are adjacent, a use can't cross a set even though we
968 think it might (this can happen for a sequence of insns each setting
969 the same destination; reg_last_set of that register might point to
970 a NOTE). If INSN has a REG_EQUIV note, the register is always
971 equivalent to the memory so the substitution is valid even if there
972 are intervening stores. Also, don't move a volatile asm or
973 UNSPEC_VOLATILE across any other insns. */
974 || (! all_adjacent
975 && (((GET_CODE (src) != MEM
976 || ! find_reg_note (insn, REG_EQUIV, src))
977 && use_crosses_set_p (src, INSN_CUID (insn)))
978 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
979 || GET_CODE (src) == UNSPEC_VOLATILE))
980 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
981 better register allocation by not doing the combine. */
982 || find_reg_note (i3, REG_NO_CONFLICT, dest)
983 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
984 /* Don't combine across a CALL_INSN, because that would possibly
985 change whether the life span of some REGs crosses calls or not,
986 and it is a pain to update that information.
987 Exception: if source is a constant, moving it later can't hurt.
988 Accept that special case, because it helps -fforce-addr a lot. */
989 || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
990 return 0;
991
992 /* DEST must either be a REG or CC0. */
993 if (GET_CODE (dest) == REG)
994 {
995 /* If register alignment is being enforced for multi-word items in all
996 cases except for parameters, it is possible to have a register copy
997 insn referencing a hard register that is not allowed to contain the
998 mode being copied and which would not be valid as an operand of most
999 insns. Eliminate this problem by not combining with such an insn.
1000
1001 Also, on some machines we don't want to extend the life of a hard
1002 register.
1003
1004 This is the same test done in can_combine except that we don't test
1005 if SRC is a CALL operation to permit a hard register with
1006 SMALL_REGISTER_CLASSES, and that we have to take all_adjacent
1007 into account. */
1008
1009 if (GET_CODE (src) == REG
1010 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1011 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1012 /* Don't extend the life of a hard register unless it is
1013 user variable (if we have few registers) or it can't
1014 fit into the desired register (meaning something special
1015 is going on).
1016 Also avoid substituting a return register into I3, because
1017 reload can't handle a conflict with constraints of other
1018 inputs. */
1019 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1020 && (! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src))
1021 || (SMALL_REGISTER_CLASSES
1022 && ((! all_adjacent && ! REG_USERVAR_P (src))
1023 || (FUNCTION_VALUE_REGNO_P (REGNO (src))
1024 && ! REG_USERVAR_P (src))))))))
1025 return 0;
1026 }
1027 else if (GET_CODE (dest) != CC0)
1028 return 0;
1029
1030 /* Don't substitute for a register intended as a clobberable operand.
1031 Similarly, don't substitute an expression containing a register that
1032 will be clobbered in I3. */
1033 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1034 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1035 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
1036 && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
1037 src)
1038 || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
1039 return 0;
1040
1041 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1042 or not), reject, unless nothing volatile comes between it and I3 */
1043
1044 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1045 {
1046 /* Make sure succ doesn't contain a volatile reference. */
1047 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1048 return 0;
1049
1050 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1051 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1052 && p != succ && volatile_refs_p (PATTERN (p)))
1053 return 0;
1054 }
1055
1056 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1057 to be an explicit register variable, and was chosen for a reason. */
1058
1059 if (GET_CODE (src) == ASM_OPERANDS
1060 && GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1061 return 0;
1062
1063 /* If there are any volatile insns between INSN and I3, reject, because
1064 they might affect machine state. */
1065
1066 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1067 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1068 && p != succ && volatile_insn_p (PATTERN (p)))
1069 return 0;
1070
1071 /* If INSN or I2 contains an autoincrement or autodecrement,
1072 make sure that register is not used between there and I3,
1073 and not already used in I3 either.
1074 Also insist that I3 not be a jump; if it were one
1075 and the incremented register were spilled, we would lose. */
1076
1077 #ifdef AUTO_INC_DEC
1078 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1079 if (REG_NOTE_KIND (link) == REG_INC
1080 && (GET_CODE (i3) == JUMP_INSN
1081 || reg_used_between_p (XEXP (link, 0), insn, i3)
1082 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1083 return 0;
1084 #endif
1085
1086 #ifdef HAVE_cc0
1087 /* Don't combine an insn that follows a CC0-setting insn.
1088 An insn that uses CC0 must not be separated from the one that sets it.
1089 We do, however, allow I2 to follow a CC0-setting insn if that insn
1090 is passed as I1; in that case it will be deleted also.
1091 We also allow combining in this case if all the insns are adjacent
1092 because that would leave the two CC0 insns adjacent as well.
1093 It would be more logical to test whether CC0 occurs inside I1 or I2,
1094 but that would be much slower, and this ought to be equivalent. */
1095
1096 p = prev_nonnote_insn (insn);
1097 if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1098 && ! all_adjacent)
1099 return 0;
1100 #endif
1101
1102 /* If we get here, we have passed all the tests and the combination is
1103 to be allowed. */
1104
1105 *pdest = dest;
1106 *psrc = src;
1107
1108 return 1;
1109 }
1110 \f
1111 /* Check if PAT is an insn - or a part of it - used to set up an
1112 argument for a function in a hard register. */
1113
1114 static int
1115 sets_function_arg_p (pat)
1116 rtx pat;
1117 {
1118 int i;
1119 rtx inner_dest;
1120
1121 switch (GET_CODE (pat))
1122 {
1123 case INSN:
1124 return sets_function_arg_p (PATTERN (pat));
1125
1126 case PARALLEL:
1127 for (i = XVECLEN (pat, 0); --i >= 0;)
1128 if (sets_function_arg_p (XVECEXP (pat, 0, i)))
1129 return 1;
1130
1131 break;
1132
1133 case SET:
1134 inner_dest = SET_DEST (pat);
1135 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1136 || GET_CODE (inner_dest) == SUBREG
1137 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1138 inner_dest = XEXP (inner_dest, 0);
1139
1140 return (GET_CODE (inner_dest) == REG
1141 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1142 && FUNCTION_ARG_REGNO_P (REGNO (inner_dest)));
1143
1144 default:
1145 break;
1146 }
1147
1148 return 0;
1149 }
1150
1151 /* LOC is the location within I3 that contains its pattern or the component
1152 of a PARALLEL of the pattern. We validate that it is valid for combining.
1153
1154 One problem is if I3 modifies its output, as opposed to replacing it
1155 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1156 so would produce an insn that is not equivalent to the original insns.
1157
1158 Consider:
1159
1160 (set (reg:DI 101) (reg:DI 100))
1161 (set (subreg:SI (reg:DI 101) 0) <foo>)
1162
1163 This is NOT equivalent to:
1164
1165 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1166 (set (reg:DI 101) (reg:DI 100))])
1167
1168 Not only does this modify 100 (in which case it might still be valid
1169 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1170
1171 We can also run into a problem if I2 sets a register that I1
1172 uses and I1 gets directly substituted into I3 (not via I2). In that
1173 case, we would be getting the wrong value of I2DEST into I3, so we
1174 must reject the combination. This case occurs when I2 and I1 both
1175 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1176 If I1_NOT_IN_SRC is non-zero, it means that finding I1 in the source
1177 of a SET must prevent combination from occurring.
1178
1179 On machines where SMALL_REGISTER_CLASSES is non-zero, we don't combine
1180 if the destination of a SET is a hard register that isn't a user
1181 variable.
1182
1183 Before doing the above check, we first try to expand a field assignment
1184 into a set of logical operations.
1185
1186 If PI3_DEST_KILLED is non-zero, it is a pointer to a location in which
1187 we place a register that is both set and used within I3. If more than one
1188 such register is detected, we fail.
1189
1190 Return 1 if the combination is valid, zero otherwise. */
1191
1192 static int
1193 combinable_i3pat (i3, loc, i2dest, i1dest, i1_not_in_src, pi3dest_killed)
1194 rtx i3;
1195 rtx *loc;
1196 rtx i2dest;
1197 rtx i1dest;
1198 int i1_not_in_src;
1199 rtx *pi3dest_killed;
1200 {
1201 rtx x = *loc;
1202
1203 if (GET_CODE (x) == SET)
1204 {
1205 rtx set = expand_field_assignment (x);
1206 rtx dest = SET_DEST (set);
1207 rtx src = SET_SRC (set);
1208 rtx inner_dest = dest;
1209
1210 #if 0
1211 rtx inner_src = src;
1212 #endif
1213
1214 SUBST (*loc, set);
1215
1216 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1217 || GET_CODE (inner_dest) == SUBREG
1218 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1219 inner_dest = XEXP (inner_dest, 0);
1220
1221 /* We probably don't need this any more now that LIMIT_RELOAD_CLASS
1222 was added. */
1223 #if 0
1224 while (GET_CODE (inner_src) == STRICT_LOW_PART
1225 || GET_CODE (inner_src) == SUBREG
1226 || GET_CODE (inner_src) == ZERO_EXTRACT)
1227 inner_src = XEXP (inner_src, 0);
1228
1229 /* If it is better that two different modes keep two different pseudos,
1230 avoid combining them. This avoids producing the following pattern
1231 on a 386:
1232 (set (subreg:SI (reg/v:QI 21) 0)
1233 (lshiftrt:SI (reg/v:SI 20)
1234 (const_int 24)))
1235 If that were made, reload could not handle the pair of
1236 reg 20/21, since it would try to get any GENERAL_REGS
1237 but some of them don't handle QImode. */
1238
1239 if (rtx_equal_p (inner_src, i2dest)
1240 && GET_CODE (inner_dest) == REG
1241 && ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (inner_dest)))
1242 return 0;
1243 #endif
1244
1245 /* Check for the case where I3 modifies its output, as
1246 discussed above. */
1247 if ((inner_dest != dest
1248 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1249 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1250
1251 /* This is the same test done in can_combine_p except that we
1252 allow a hard register with SMALL_REGISTER_CLASSES if SRC is a
1253 CALL operation. Moreover, we can't test all_adjacent; we don't
1254 have to, since this instruction will stay in place, thus we are
1255 not considering increasing the lifetime of INNER_DEST.
1256
1257 Also, if this insn sets a function argument, combining it with
1258 something that might need a spill could clobber a previous
1259 function argument; the all_adjacent test in can_combine_p also
1260 checks this; here, we do a more specific test for this case. */
1261
1262 || (GET_CODE (inner_dest) == REG
1263 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1264 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1265 GET_MODE (inner_dest))
1266 || (SMALL_REGISTER_CLASSES && GET_CODE (src) != CALL
1267 && ! REG_USERVAR_P (inner_dest)
1268 && (FUNCTION_VALUE_REGNO_P (REGNO (inner_dest))
1269 || (FUNCTION_ARG_REGNO_P (REGNO (inner_dest))
1270 && i3 != 0
1271 && sets_function_arg_p (prev_nonnote_insn (i3)))))))
1272 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1273 return 0;
1274
1275 /* If DEST is used in I3, it is being killed in this insn,
1276 so record that for later.
1277 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1278 STACK_POINTER_REGNUM, since these are always considered to be
1279 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1280 if (pi3dest_killed && GET_CODE (dest) == REG
1281 && reg_referenced_p (dest, PATTERN (i3))
1282 && REGNO (dest) != FRAME_POINTER_REGNUM
1283 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1284 && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1285 #endif
1286 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1287 && (REGNO (dest) != ARG_POINTER_REGNUM
1288 || ! fixed_regs [REGNO (dest)])
1289 #endif
1290 && REGNO (dest) != STACK_POINTER_REGNUM)
1291 {
1292 if (*pi3dest_killed)
1293 return 0;
1294
1295 *pi3dest_killed = dest;
1296 }
1297 }
1298
1299 else if (GET_CODE (x) == PARALLEL)
1300 {
1301 int i;
1302
1303 for (i = 0; i < XVECLEN (x, 0); i++)
1304 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1305 i1_not_in_src, pi3dest_killed))
1306 return 0;
1307 }
1308
1309 return 1;
1310 }
1311 \f
1312 /* Try to combine the insns I1 and I2 into I3.
1313 Here I1 and I2 appear earlier than I3.
1314 I1 can be zero; then we combine just I2 into I3.
1315
1316 It we are combining three insns and the resulting insn is not recognized,
1317 try splitting it into two insns. If that happens, I2 and I3 are retained
1318 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
1319 are pseudo-deleted.
1320
1321 Return 0 if the combination does not work. Then nothing is changed.
1322 If we did the combination, return the insn at which combine should
1323 resume scanning. */
1324
1325 static rtx
1326 try_combine (i3, i2, i1)
1327 register rtx i3, i2, i1;
1328 {
1329 /* New patterns for I3 and I3, respectively. */
1330 rtx newpat, newi2pat = 0;
1331 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
1332 int added_sets_1, added_sets_2;
1333 /* Total number of SETs to put into I3. */
1334 int total_sets;
1335 /* Nonzero is I2's body now appears in I3. */
1336 int i2_is_used;
1337 /* INSN_CODEs for new I3, new I2, and user of condition code. */
1338 int insn_code_number, i2_code_number, other_code_number;
1339 /* Contains I3 if the destination of I3 is used in its source, which means
1340 that the old life of I3 is being killed. If that usage is placed into
1341 I2 and not in I3, a REG_DEAD note must be made. */
1342 rtx i3dest_killed = 0;
1343 /* SET_DEST and SET_SRC of I2 and I1. */
1344 rtx i2dest, i2src, i1dest = 0, i1src = 0;
1345 /* PATTERN (I2), or a copy of it in certain cases. */
1346 rtx i2pat;
1347 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
1348 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1349 int i1_feeds_i3 = 0;
1350 /* Notes that must be added to REG_NOTES in I3 and I2. */
1351 rtx new_i3_notes, new_i2_notes;
1352 /* Notes that we substituted I3 into I2 instead of the normal case. */
1353 int i3_subst_into_i2 = 0;
1354 /* Notes that I1, I2 or I3 is a MULT operation. */
1355 int have_mult = 0;
1356 /* Number of clobbers of SCRATCH we had to add. */
1357 int i3_scratches = 0, i2_scratches = 0, other_scratches = 0;
1358
1359 int maxreg;
1360 rtx temp;
1361 register rtx link;
1362 int i;
1363
1364 /* If any of I1, I2, and I3 isn't really an insn, we can't do anything.
1365 This can occur when flow deletes an insn that it has merged into an
1366 auto-increment address. We also can't do anything if I3 has a
1367 REG_LIBCALL note since we don't want to disrupt the contiguity of a
1368 libcall. */
1369
1370 if (GET_RTX_CLASS (GET_CODE (i3)) != 'i'
1371 || GET_RTX_CLASS (GET_CODE (i2)) != 'i'
1372 || (i1 && GET_RTX_CLASS (GET_CODE (i1)) != 'i')
1373 || find_reg_note (i3, REG_LIBCALL, NULL_RTX))
1374 return 0;
1375
1376 combine_attempts++;
1377
1378 undobuf.undos = undobuf.previous_undos = 0;
1379 undobuf.other_insn = 0;
1380
1381 /* Save the current high-water-mark so we can free storage if we didn't
1382 accept this combination. */
1383 undobuf.storage = (char *) oballoc (0);
1384
1385 /* Reset the hard register usage information. */
1386 CLEAR_HARD_REG_SET (newpat_used_regs);
1387
1388 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
1389 code below, set I1 to be the earlier of the two insns. */
1390 if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1391 temp = i1, i1 = i2, i2 = temp;
1392
1393 added_links_insn = 0;
1394
1395 /* First check for one important special-case that the code below will
1396 not handle. Namely, the case where I1 is zero, I2 has multiple sets,
1397 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
1398 we may be able to replace that destination with the destination of I3.
1399 This occurs in the common code where we compute both a quotient and
1400 remainder into a structure, in which case we want to do the computation
1401 directly into the structure to avoid register-register copies.
1402
1403 We make very conservative checks below and only try to handle the
1404 most common cases of this. For example, we only handle the case
1405 where I2 and I3 are adjacent to avoid making difficult register
1406 usage tests. */
1407
1408 if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1409 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1410 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1411 && (! SMALL_REGISTER_CLASSES
1412 || (GET_CODE (SET_DEST (PATTERN (i3))) != REG
1413 || REGNO (SET_DEST (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1414 || REG_USERVAR_P (SET_DEST (PATTERN (i3)))))
1415 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1416 && GET_CODE (PATTERN (i2)) == PARALLEL
1417 && ! side_effects_p (SET_DEST (PATTERN (i3)))
1418 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1419 below would need to check what is inside (and reg_overlap_mentioned_p
1420 doesn't support those codes anyway). Don't allow those destinations;
1421 the resulting insn isn't likely to be recognized anyway. */
1422 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1423 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1424 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1425 SET_DEST (PATTERN (i3)))
1426 && next_real_insn (i2) == i3)
1427 {
1428 rtx p2 = PATTERN (i2);
1429
1430 /* Make sure that the destination of I3,
1431 which we are going to substitute into one output of I2,
1432 is not used within another output of I2. We must avoid making this:
1433 (parallel [(set (mem (reg 69)) ...)
1434 (set (reg 69) ...)])
1435 which is not well-defined as to order of actions.
1436 (Besides, reload can't handle output reloads for this.)
1437
1438 The problem can also happen if the dest of I3 is a memory ref,
1439 if another dest in I2 is an indirect memory ref. */
1440 for (i = 0; i < XVECLEN (p2, 0); i++)
1441 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1442 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1443 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1444 SET_DEST (XVECEXP (p2, 0, i))))
1445 break;
1446
1447 if (i == XVECLEN (p2, 0))
1448 for (i = 0; i < XVECLEN (p2, 0); i++)
1449 if (SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1450 {
1451 combine_merges++;
1452
1453 subst_insn = i3;
1454 subst_low_cuid = INSN_CUID (i2);
1455
1456 added_sets_2 = added_sets_1 = 0;
1457 i2dest = SET_SRC (PATTERN (i3));
1458
1459 /* Replace the dest in I2 with our dest and make the resulting
1460 insn the new pattern for I3. Then skip to where we
1461 validate the pattern. Everything was set up above. */
1462 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1463 SET_DEST (PATTERN (i3)));
1464
1465 newpat = p2;
1466 i3_subst_into_i2 = 1;
1467 goto validate_replacement;
1468 }
1469 }
1470
1471 #ifndef HAVE_cc0
1472 /* If we have no I1 and I2 looks like:
1473 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1474 (set Y OP)])
1475 make up a dummy I1 that is
1476 (set Y OP)
1477 and change I2 to be
1478 (set (reg:CC X) (compare:CC Y (const_int 0)))
1479
1480 (We can ignore any trailing CLOBBERs.)
1481
1482 This undoes a previous combination and allows us to match a branch-and-
1483 decrement insn. */
1484
1485 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1486 && XVECLEN (PATTERN (i2), 0) >= 2
1487 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1488 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1489 == MODE_CC)
1490 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1491 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1492 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1493 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1494 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1495 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1496 {
1497 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1498 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1499 break;
1500
1501 if (i == 1)
1502 {
1503 /* We make I1 with the same INSN_UID as I2. This gives it
1504 the same INSN_CUID for value tracking. Our fake I1 will
1505 never appear in the insn stream so giving it the same INSN_UID
1506 as I2 will not cause a problem. */
1507
1508 subst_prev_insn = i1
1509 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1510 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1511 NULL_RTX);
1512
1513 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1514 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1515 SET_DEST (PATTERN (i1)));
1516 }
1517 }
1518 #endif
1519
1520 /* Verify that I2 and I1 are valid for combining. */
1521 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1522 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1523 {
1524 undo_all ();
1525 return 0;
1526 }
1527
1528 /* Record whether I2DEST is used in I2SRC and similarly for the other
1529 cases. Knowing this will help in register status updating below. */
1530 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1531 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1532 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1533
1534 /* See if I1 directly feeds into I3. It does if I1DEST is not used
1535 in I2SRC. */
1536 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1537
1538 /* Ensure that I3's pattern can be the destination of combines. */
1539 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1540 i1 && i2dest_in_i1src && i1_feeds_i3,
1541 &i3dest_killed))
1542 {
1543 undo_all ();
1544 return 0;
1545 }
1546
1547 /* See if any of the insns is a MULT operation. Unless one is, we will
1548 reject a combination that is, since it must be slower. Be conservative
1549 here. */
1550 if (GET_CODE (i2src) == MULT
1551 || (i1 != 0 && GET_CODE (i1src) == MULT)
1552 || (GET_CODE (PATTERN (i3)) == SET
1553 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1554 have_mult = 1;
1555
1556 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1557 We used to do this EXCEPT in one case: I3 has a post-inc in an
1558 output operand. However, that exception can give rise to insns like
1559 mov r3,(r3)+
1560 which is a famous insn on the PDP-11 where the value of r3 used as the
1561 source was model-dependent. Avoid this sort of thing. */
1562
1563 #if 0
1564 if (!(GET_CODE (PATTERN (i3)) == SET
1565 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1566 && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1567 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1568 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1569 /* It's not the exception. */
1570 #endif
1571 #ifdef AUTO_INC_DEC
1572 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1573 if (REG_NOTE_KIND (link) == REG_INC
1574 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1575 || (i1 != 0
1576 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1577 {
1578 undo_all ();
1579 return 0;
1580 }
1581 #endif
1582
1583 /* See if the SETs in I1 or I2 need to be kept around in the merged
1584 instruction: whenever the value set there is still needed past I3.
1585 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1586
1587 For the SET in I1, we have two cases: If I1 and I2 independently
1588 feed into I3, the set in I1 needs to be kept around if I1DEST dies
1589 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
1590 in I1 needs to be kept around unless I1DEST dies or is set in either
1591 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
1592 I1DEST. If so, we know I1 feeds into I2. */
1593
1594 added_sets_2 = ! dead_or_set_p (i3, i2dest);
1595
1596 added_sets_1
1597 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1598 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1599
1600 /* If the set in I2 needs to be kept around, we must make a copy of
1601 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1602 PATTERN (I2), we are only substituting for the original I1DEST, not into
1603 an already-substituted copy. This also prevents making self-referential
1604 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1605 I2DEST. */
1606
1607 i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1608 ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1609 : PATTERN (i2));
1610
1611 if (added_sets_2)
1612 i2pat = copy_rtx (i2pat);
1613
1614 combine_merges++;
1615
1616 /* Substitute in the latest insn for the regs set by the earlier ones. */
1617
1618 maxreg = max_reg_num ();
1619
1620 subst_insn = i3;
1621
1622 /* It is possible that the source of I2 or I1 may be performing an
1623 unneeded operation, such as a ZERO_EXTEND of something that is known
1624 to have the high part zero. Handle that case by letting subst look at
1625 the innermost one of them.
1626
1627 Another way to do this would be to have a function that tries to
1628 simplify a single insn instead of merging two or more insns. We don't
1629 do this because of the potential of infinite loops and because
1630 of the potential extra memory required. However, doing it the way
1631 we are is a bit of a kludge and doesn't catch all cases.
1632
1633 But only do this if -fexpensive-optimizations since it slows things down
1634 and doesn't usually win. */
1635
1636 if (flag_expensive_optimizations)
1637 {
1638 /* Pass pc_rtx so no substitutions are done, just simplifications.
1639 The cases that we are interested in here do not involve the few
1640 cases were is_replaced is checked. */
1641 if (i1)
1642 {
1643 subst_low_cuid = INSN_CUID (i1);
1644 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1645 }
1646 else
1647 {
1648 subst_low_cuid = INSN_CUID (i2);
1649 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1650 }
1651
1652 undobuf.previous_undos = undobuf.undos;
1653 }
1654
1655 #ifndef HAVE_cc0
1656 /* Many machines that don't use CC0 have insns that can both perform an
1657 arithmetic operation and set the condition code. These operations will
1658 be represented as a PARALLEL with the first element of the vector
1659 being a COMPARE of an arithmetic operation with the constant zero.
1660 The second element of the vector will set some pseudo to the result
1661 of the same arithmetic operation. If we simplify the COMPARE, we won't
1662 match such a pattern and so will generate an extra insn. Here we test
1663 for this case, where both the comparison and the operation result are
1664 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1665 I2SRC. Later we will make the PARALLEL that contains I2. */
1666
1667 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1668 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1669 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1670 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1671 {
1672 #ifdef EXTRA_CC_MODES
1673 rtx *cc_use;
1674 enum machine_mode compare_mode;
1675 #endif
1676
1677 newpat = PATTERN (i3);
1678 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1679
1680 i2_is_used = 1;
1681
1682 #ifdef EXTRA_CC_MODES
1683 /* See if a COMPARE with the operand we substituted in should be done
1684 with the mode that is currently being used. If not, do the same
1685 processing we do in `subst' for a SET; namely, if the destination
1686 is used only once, try to replace it with a register of the proper
1687 mode and also replace the COMPARE. */
1688 if (undobuf.other_insn == 0
1689 && (cc_use = find_single_use (SET_DEST (newpat), i3,
1690 &undobuf.other_insn))
1691 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1692 i2src, const0_rtx))
1693 != GET_MODE (SET_DEST (newpat))))
1694 {
1695 int regno = REGNO (SET_DEST (newpat));
1696 rtx new_dest = gen_rtx_REG (compare_mode, regno);
1697
1698 if (regno < FIRST_PSEUDO_REGISTER
1699 || (REG_N_SETS (regno) == 1 && ! added_sets_2
1700 && ! REG_USERVAR_P (SET_DEST (newpat))))
1701 {
1702 if (regno >= FIRST_PSEUDO_REGISTER)
1703 SUBST (regno_reg_rtx[regno], new_dest);
1704
1705 SUBST (SET_DEST (newpat), new_dest);
1706 SUBST (XEXP (*cc_use, 0), new_dest);
1707 SUBST (SET_SRC (newpat),
1708 gen_rtx_combine (COMPARE, compare_mode,
1709 i2src, const0_rtx));
1710 }
1711 else
1712 undobuf.other_insn = 0;
1713 }
1714 #endif
1715 }
1716 else
1717 #endif
1718 {
1719 n_occurrences = 0; /* `subst' counts here */
1720
1721 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1722 need to make a unique copy of I2SRC each time we substitute it
1723 to avoid self-referential rtl. */
1724
1725 subst_low_cuid = INSN_CUID (i2);
1726 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1727 ! i1_feeds_i3 && i1dest_in_i1src);
1728 undobuf.previous_undos = undobuf.undos;
1729
1730 /* Record whether i2's body now appears within i3's body. */
1731 i2_is_used = n_occurrences;
1732 }
1733
1734 /* If we already got a failure, don't try to do more. Otherwise,
1735 try to substitute in I1 if we have it. */
1736
1737 if (i1 && GET_CODE (newpat) != CLOBBER)
1738 {
1739 /* Before we can do this substitution, we must redo the test done
1740 above (see detailed comments there) that ensures that I1DEST
1741 isn't mentioned in any SETs in NEWPAT that are field assignments. */
1742
1743 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1744 0, NULL_PTR))
1745 {
1746 undo_all ();
1747 return 0;
1748 }
1749
1750 n_occurrences = 0;
1751 subst_low_cuid = INSN_CUID (i1);
1752 newpat = subst (newpat, i1dest, i1src, 0, 0);
1753 undobuf.previous_undos = undobuf.undos;
1754 }
1755
1756 /* Fail if an autoincrement side-effect has been duplicated. Be careful
1757 to count all the ways that I2SRC and I1SRC can be used. */
1758 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1759 && i2_is_used + added_sets_2 > 1)
1760 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1761 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1762 > 1))
1763 /* Fail if we tried to make a new register (we used to abort, but there's
1764 really no reason to). */
1765 || max_reg_num () != maxreg
1766 /* Fail if we couldn't do something and have a CLOBBER. */
1767 || GET_CODE (newpat) == CLOBBER
1768 /* Fail if this new pattern is a MULT and we didn't have one before
1769 at the outer level. */
1770 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1771 && ! have_mult))
1772 {
1773 undo_all ();
1774 return 0;
1775 }
1776
1777 /* If the actions of the earlier insns must be kept
1778 in addition to substituting them into the latest one,
1779 we must make a new PARALLEL for the latest insn
1780 to hold additional the SETs. */
1781
1782 if (added_sets_1 || added_sets_2)
1783 {
1784 combine_extras++;
1785
1786 if (GET_CODE (newpat) == PARALLEL)
1787 {
1788 rtvec old = XVEC (newpat, 0);
1789 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
1790 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1791 bcopy ((char *) &old->elem[0], (char *) XVEC (newpat, 0)->elem,
1792 sizeof (old->elem[0]) * old->num_elem);
1793 }
1794 else
1795 {
1796 rtx old = newpat;
1797 total_sets = 1 + added_sets_1 + added_sets_2;
1798 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1799 XVECEXP (newpat, 0, 0) = old;
1800 }
1801
1802 if (added_sets_1)
1803 XVECEXP (newpat, 0, --total_sets)
1804 = (GET_CODE (PATTERN (i1)) == PARALLEL
1805 ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
1806
1807 if (added_sets_2)
1808 {
1809 /* If there is no I1, use I2's body as is. We used to also not do
1810 the subst call below if I2 was substituted into I3,
1811 but that could lose a simplification. */
1812 if (i1 == 0)
1813 XVECEXP (newpat, 0, --total_sets) = i2pat;
1814 else
1815 /* See comment where i2pat is assigned. */
1816 XVECEXP (newpat, 0, --total_sets)
1817 = subst (i2pat, i1dest, i1src, 0, 0);
1818 }
1819 }
1820
1821 /* We come here when we are replacing a destination in I2 with the
1822 destination of I3. */
1823 validate_replacement:
1824
1825 /* Note which hard regs this insn has as inputs. */
1826 mark_used_regs_combine (newpat);
1827
1828 /* Is the result of combination a valid instruction? */
1829 insn_code_number
1830 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1831
1832 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
1833 the second SET's destination is a register that is unused. In that case,
1834 we just need the first SET. This can occur when simplifying a divmod
1835 insn. We *must* test for this case here because the code below that
1836 splits two independent SETs doesn't handle this case correctly when it
1837 updates the register status. Also check the case where the first
1838 SET's destination is unused. That would not cause incorrect code, but
1839 does cause an unneeded insn to remain. */
1840
1841 if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1842 && XVECLEN (newpat, 0) == 2
1843 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1844 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1845 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
1846 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
1847 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
1848 && asm_noperands (newpat) < 0)
1849 {
1850 newpat = XVECEXP (newpat, 0, 0);
1851 insn_code_number
1852 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1853 }
1854
1855 else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1856 && XVECLEN (newpat, 0) == 2
1857 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1858 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1859 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
1860 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
1861 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
1862 && asm_noperands (newpat) < 0)
1863 {
1864 newpat = XVECEXP (newpat, 0, 1);
1865 insn_code_number
1866 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1867 }
1868
1869 /* If we were combining three insns and the result is a simple SET
1870 with no ASM_OPERANDS that wasn't recognized, try to split it into two
1871 insns. There are two ways to do this. It can be split using a
1872 machine-specific method (like when you have an addition of a large
1873 constant) or by combine in the function find_split_point. */
1874
1875 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
1876 && asm_noperands (newpat) < 0)
1877 {
1878 rtx m_split, *split;
1879 rtx ni2dest = i2dest;
1880
1881 /* See if the MD file can split NEWPAT. If it can't, see if letting it
1882 use I2DEST as a scratch register will help. In the latter case,
1883 convert I2DEST to the mode of the source of NEWPAT if we can. */
1884
1885 m_split = split_insns (newpat, i3);
1886
1887 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
1888 inputs of NEWPAT. */
1889
1890 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
1891 possible to try that as a scratch reg. This would require adding
1892 more code to make it work though. */
1893
1894 if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
1895 {
1896 /* If I2DEST is a hard register or the only use of a pseudo,
1897 we can change its mode. */
1898 if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
1899 && GET_MODE (SET_DEST (newpat)) != VOIDmode
1900 && GET_CODE (i2dest) == REG
1901 && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
1902 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
1903 && ! REG_USERVAR_P (i2dest))))
1904 ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
1905 REGNO (i2dest));
1906
1907 m_split = split_insns
1908 (gen_rtx_PARALLEL (VOIDmode,
1909 gen_rtvec (2, newpat,
1910 gen_rtx_CLOBBER (VOIDmode,
1911 ni2dest))),
1912 i3);
1913 }
1914
1915 if (m_split && GET_CODE (m_split) == SEQUENCE
1916 && XVECLEN (m_split, 0) == 2
1917 && (next_real_insn (i2) == i3
1918 || ! use_crosses_set_p (PATTERN (XVECEXP (m_split, 0, 0)),
1919 INSN_CUID (i2))))
1920 {
1921 rtx i2set, i3set;
1922 rtx newi3pat = PATTERN (XVECEXP (m_split, 0, 1));
1923 newi2pat = PATTERN (XVECEXP (m_split, 0, 0));
1924
1925 i3set = single_set (XVECEXP (m_split, 0, 1));
1926 i2set = single_set (XVECEXP (m_split, 0, 0));
1927
1928 /* In case we changed the mode of I2DEST, replace it in the
1929 pseudo-register table here. We can't do it above in case this
1930 code doesn't get executed and we do a split the other way. */
1931
1932 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1933 SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
1934
1935 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes,
1936 &i2_scratches);
1937
1938 /* If I2 or I3 has multiple SETs, we won't know how to track
1939 register status, so don't use these insns. If I2's destination
1940 is used between I2 and I3, we also can't use these insns. */
1941
1942 if (i2_code_number >= 0 && i2set && i3set
1943 && (next_real_insn (i2) == i3
1944 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
1945 insn_code_number = recog_for_combine (&newi3pat, i3, &new_i3_notes,
1946 &i3_scratches);
1947 if (insn_code_number >= 0)
1948 newpat = newi3pat;
1949
1950 /* It is possible that both insns now set the destination of I3.
1951 If so, we must show an extra use of it. */
1952
1953 if (insn_code_number >= 0)
1954 {
1955 rtx new_i3_dest = SET_DEST (i3set);
1956 rtx new_i2_dest = SET_DEST (i2set);
1957
1958 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
1959 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
1960 || GET_CODE (new_i3_dest) == SUBREG)
1961 new_i3_dest = XEXP (new_i3_dest, 0);
1962
1963 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
1964 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
1965 || GET_CODE (new_i2_dest) == SUBREG)
1966 new_i2_dest = XEXP (new_i2_dest, 0);
1967
1968 if (GET_CODE (new_i3_dest) == REG
1969 && GET_CODE (new_i2_dest) == REG
1970 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
1971 REG_N_SETS (REGNO (new_i2_dest))++;
1972 }
1973 }
1974
1975 /* If we can split it and use I2DEST, go ahead and see if that
1976 helps things be recognized. Verify that none of the registers
1977 are set between I2 and I3. */
1978 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
1979 #ifdef HAVE_cc0
1980 && GET_CODE (i2dest) == REG
1981 #endif
1982 /* We need I2DEST in the proper mode. If it is a hard register
1983 or the only use of a pseudo, we can change its mode. */
1984 && (GET_MODE (*split) == GET_MODE (i2dest)
1985 || GET_MODE (*split) == VOIDmode
1986 || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
1987 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
1988 && ! REG_USERVAR_P (i2dest)))
1989 && (next_real_insn (i2) == i3
1990 || ! use_crosses_set_p (*split, INSN_CUID (i2)))
1991 /* We can't overwrite I2DEST if its value is still used by
1992 NEWPAT. */
1993 && ! reg_referenced_p (i2dest, newpat))
1994 {
1995 rtx newdest = i2dest;
1996 enum rtx_code split_code = GET_CODE (*split);
1997 enum machine_mode split_mode = GET_MODE (*split);
1998
1999 /* Get NEWDEST as a register in the proper mode. We have already
2000 validated that we can do this. */
2001 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2002 {
2003 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2004
2005 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2006 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2007 }
2008
2009 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2010 an ASHIFT. This can occur if it was inside a PLUS and hence
2011 appeared to be a memory address. This is a kludge. */
2012 if (split_code == MULT
2013 && GET_CODE (XEXP (*split, 1)) == CONST_INT
2014 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2015 {
2016 SUBST (*split, gen_rtx_combine (ASHIFT, split_mode,
2017 XEXP (*split, 0), GEN_INT (i)));
2018 /* Update split_code because we may not have a multiply
2019 anymore. */
2020 split_code = GET_CODE (*split);
2021 }
2022
2023 #ifdef INSN_SCHEDULING
2024 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2025 be written as a ZERO_EXTEND. */
2026 if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
2027 SUBST (*split, gen_rtx_combine (ZERO_EXTEND, split_mode,
2028 XEXP (*split, 0)));
2029 #endif
2030
2031 newi2pat = gen_rtx_combine (SET, VOIDmode, newdest, *split);
2032 SUBST (*split, newdest);
2033 i2_code_number
2034 = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
2035
2036 /* If the split point was a MULT and we didn't have one before,
2037 don't use one now. */
2038 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2039 insn_code_number
2040 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
2041 }
2042 }
2043
2044 /* Check for a case where we loaded from memory in a narrow mode and
2045 then sign extended it, but we need both registers. In that case,
2046 we have a PARALLEL with both loads from the same memory location.
2047 We can split this into a load from memory followed by a register-register
2048 copy. This saves at least one insn, more if register allocation can
2049 eliminate the copy.
2050
2051 We cannot do this if the destination of the second assignment is
2052 a register that we have already assumed is zero-extended. Similarly
2053 for a SUBREG of such a register. */
2054
2055 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2056 && GET_CODE (newpat) == PARALLEL
2057 && XVECLEN (newpat, 0) == 2
2058 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2059 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2060 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2061 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2062 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2063 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2064 INSN_CUID (i2))
2065 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2066 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2067 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2068 (GET_CODE (temp) == REG
2069 && reg_nonzero_bits[REGNO (temp)] != 0
2070 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2071 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2072 && (reg_nonzero_bits[REGNO (temp)]
2073 != GET_MODE_MASK (word_mode))))
2074 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2075 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2076 (GET_CODE (temp) == REG
2077 && reg_nonzero_bits[REGNO (temp)] != 0
2078 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2079 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2080 && (reg_nonzero_bits[REGNO (temp)]
2081 != GET_MODE_MASK (word_mode)))))
2082 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2083 SET_SRC (XVECEXP (newpat, 0, 1)))
2084 && ! find_reg_note (i3, REG_UNUSED,
2085 SET_DEST (XVECEXP (newpat, 0, 0))))
2086 {
2087 rtx ni2dest;
2088
2089 newi2pat = XVECEXP (newpat, 0, 0);
2090 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2091 newpat = XVECEXP (newpat, 0, 1);
2092 SUBST (SET_SRC (newpat),
2093 gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
2094 i2_code_number
2095 = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
2096
2097 if (i2_code_number >= 0)
2098 insn_code_number
2099 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
2100
2101 if (insn_code_number >= 0)
2102 {
2103 rtx insn;
2104 rtx link;
2105
2106 /* If we will be able to accept this, we have made a change to the
2107 destination of I3. This can invalidate a LOG_LINKS pointing
2108 to I3. No other part of combine.c makes such a transformation.
2109
2110 The new I3 will have a destination that was previously the
2111 destination of I1 or I2 and which was used in i2 or I3. Call
2112 distribute_links to make a LOG_LINK from the next use of
2113 that destination. */
2114
2115 PATTERN (i3) = newpat;
2116 distribute_links (gen_rtx_INSN_LIST (VOIDmode, i3, NULL_RTX));
2117
2118 /* I3 now uses what used to be its destination and which is
2119 now I2's destination. That means we need a LOG_LINK from
2120 I3 to I2. But we used to have one, so we still will.
2121
2122 However, some later insn might be using I2's dest and have
2123 a LOG_LINK pointing at I3. We must remove this link.
2124 The simplest way to remove the link is to point it at I1,
2125 which we know will be a NOTE. */
2126
2127 for (insn = NEXT_INSN (i3);
2128 insn && (this_basic_block == n_basic_blocks - 1
2129 || insn != basic_block_head[this_basic_block + 1]);
2130 insn = NEXT_INSN (insn))
2131 {
2132 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
2133 && reg_referenced_p (ni2dest, PATTERN (insn)))
2134 {
2135 for (link = LOG_LINKS (insn); link;
2136 link = XEXP (link, 1))
2137 if (XEXP (link, 0) == i3)
2138 XEXP (link, 0) = i1;
2139
2140 break;
2141 }
2142 }
2143 }
2144 }
2145
2146 /* Similarly, check for a case where we have a PARALLEL of two independent
2147 SETs but we started with three insns. In this case, we can do the sets
2148 as two separate insns. This case occurs when some SET allows two
2149 other insns to combine, but the destination of that SET is still live. */
2150
2151 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2152 && GET_CODE (newpat) == PARALLEL
2153 && XVECLEN (newpat, 0) == 2
2154 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2155 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2156 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2157 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2158 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2159 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2160 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2161 INSN_CUID (i2))
2162 /* Don't pass sets with (USE (MEM ...)) dests to the following. */
2163 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2164 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2165 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2166 XVECEXP (newpat, 0, 0))
2167 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2168 XVECEXP (newpat, 0, 1)))
2169 {
2170 /* Normally, it doesn't matter which of the two is done first,
2171 but it does if one references cc0. In that case, it has to
2172 be first. */
2173 #ifdef HAVE_cc0
2174 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2175 {
2176 newi2pat = XVECEXP (newpat, 0, 0);
2177 newpat = XVECEXP (newpat, 0, 1);
2178 }
2179 else
2180 #endif
2181 {
2182 newi2pat = XVECEXP (newpat, 0, 1);
2183 newpat = XVECEXP (newpat, 0, 0);
2184 }
2185
2186 i2_code_number
2187 = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
2188
2189 if (i2_code_number >= 0)
2190 insn_code_number
2191 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
2192 }
2193
2194 /* If it still isn't recognized, fail and change things back the way they
2195 were. */
2196 if ((insn_code_number < 0
2197 /* Is the result a reasonable ASM_OPERANDS? */
2198 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2199 {
2200 undo_all ();
2201 return 0;
2202 }
2203
2204 /* If we had to change another insn, make sure it is valid also. */
2205 if (undobuf.other_insn)
2206 {
2207 rtx other_pat = PATTERN (undobuf.other_insn);
2208 rtx new_other_notes;
2209 rtx note, next;
2210
2211 CLEAR_HARD_REG_SET (newpat_used_regs);
2212
2213 other_code_number
2214 = recog_for_combine (&other_pat, undobuf.other_insn,
2215 &new_other_notes, &other_scratches);
2216
2217 if (other_code_number < 0 && ! check_asm_operands (other_pat))
2218 {
2219 undo_all ();
2220 return 0;
2221 }
2222
2223 PATTERN (undobuf.other_insn) = other_pat;
2224
2225 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2226 are still valid. Then add any non-duplicate notes added by
2227 recog_for_combine. */
2228 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2229 {
2230 next = XEXP (note, 1);
2231
2232 if (REG_NOTE_KIND (note) == REG_UNUSED
2233 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2234 {
2235 if (GET_CODE (XEXP (note, 0)) == REG)
2236 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2237
2238 remove_note (undobuf.other_insn, note);
2239 }
2240 }
2241
2242 for (note = new_other_notes; note; note = XEXP (note, 1))
2243 if (GET_CODE (XEXP (note, 0)) == REG)
2244 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2245
2246 distribute_notes (new_other_notes, undobuf.other_insn,
2247 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
2248 }
2249
2250 /* We now know that we can do this combination. Merge the insns and
2251 update the status of registers and LOG_LINKS. */
2252
2253 {
2254 rtx i3notes, i2notes, i1notes = 0;
2255 rtx i3links, i2links, i1links = 0;
2256 rtx midnotes = 0;
2257 register int regno;
2258 /* Compute which registers we expect to eliminate. newi2pat may be setting
2259 either i3dest or i2dest, so we must check it. Also, i1dest may be the
2260 same as i3dest, in which case newi2pat may be setting i1dest. */
2261 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
2262 || i2dest_in_i2src || i2dest_in_i1src
2263 ? 0 : i2dest);
2264 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
2265 || (newi2pat && reg_set_p (i1dest, newi2pat))
2266 ? 0 : i1dest);
2267
2268 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2269 clear them. */
2270 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2271 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2272 if (i1)
2273 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2274
2275 /* Ensure that we do not have something that should not be shared but
2276 occurs multiple times in the new insns. Check this by first
2277 resetting all the `used' flags and then copying anything is shared. */
2278
2279 reset_used_flags (i3notes);
2280 reset_used_flags (i2notes);
2281 reset_used_flags (i1notes);
2282 reset_used_flags (newpat);
2283 reset_used_flags (newi2pat);
2284 if (undobuf.other_insn)
2285 reset_used_flags (PATTERN (undobuf.other_insn));
2286
2287 i3notes = copy_rtx_if_shared (i3notes);
2288 i2notes = copy_rtx_if_shared (i2notes);
2289 i1notes = copy_rtx_if_shared (i1notes);
2290 newpat = copy_rtx_if_shared (newpat);
2291 newi2pat = copy_rtx_if_shared (newi2pat);
2292 if (undobuf.other_insn)
2293 reset_used_flags (PATTERN (undobuf.other_insn));
2294
2295 INSN_CODE (i3) = insn_code_number;
2296 PATTERN (i3) = newpat;
2297 if (undobuf.other_insn)
2298 INSN_CODE (undobuf.other_insn) = other_code_number;
2299
2300 /* We had one special case above where I2 had more than one set and
2301 we replaced a destination of one of those sets with the destination
2302 of I3. In that case, we have to update LOG_LINKS of insns later
2303 in this basic block. Note that this (expensive) case is rare.
2304
2305 Also, in this case, we must pretend that all REG_NOTEs for I2
2306 actually came from I3, so that REG_UNUSED notes from I2 will be
2307 properly handled. */
2308
2309 if (i3_subst_into_i2)
2310 {
2311 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2312 if (GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2313 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2314 && ! find_reg_note (i2, REG_UNUSED,
2315 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2316 for (temp = NEXT_INSN (i2);
2317 temp && (this_basic_block == n_basic_blocks - 1
2318 || basic_block_head[this_basic_block] != temp);
2319 temp = NEXT_INSN (temp))
2320 if (temp != i3 && GET_RTX_CLASS (GET_CODE (temp)) == 'i')
2321 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2322 if (XEXP (link, 0) == i2)
2323 XEXP (link, 0) = i3;
2324
2325 if (i3notes)
2326 {
2327 rtx link = i3notes;
2328 while (XEXP (link, 1))
2329 link = XEXP (link, 1);
2330 XEXP (link, 1) = i2notes;
2331 }
2332 else
2333 i3notes = i2notes;
2334 i2notes = 0;
2335 }
2336
2337 LOG_LINKS (i3) = 0;
2338 REG_NOTES (i3) = 0;
2339 LOG_LINKS (i2) = 0;
2340 REG_NOTES (i2) = 0;
2341
2342 if (newi2pat)
2343 {
2344 INSN_CODE (i2) = i2_code_number;
2345 PATTERN (i2) = newi2pat;
2346 }
2347 else
2348 {
2349 PUT_CODE (i2, NOTE);
2350 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2351 NOTE_SOURCE_FILE (i2) = 0;
2352 }
2353
2354 if (i1)
2355 {
2356 LOG_LINKS (i1) = 0;
2357 REG_NOTES (i1) = 0;
2358 PUT_CODE (i1, NOTE);
2359 NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2360 NOTE_SOURCE_FILE (i1) = 0;
2361 }
2362
2363 /* Get death notes for everything that is now used in either I3 or
2364 I2 and used to die in a previous insn. If we built two new
2365 patterns, move from I1 to I2 then I2 to I3 so that we get the
2366 proper movement on registers that I2 modifies. */
2367
2368 if (newi2pat)
2369 {
2370 move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2371 move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2372 }
2373 else
2374 move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2375 i3, &midnotes);
2376
2377 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
2378 if (i3notes)
2379 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
2380 elim_i2, elim_i1);
2381 if (i2notes)
2382 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
2383 elim_i2, elim_i1);
2384 if (i1notes)
2385 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
2386 elim_i2, elim_i1);
2387 if (midnotes)
2388 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2389 elim_i2, elim_i1);
2390
2391 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
2392 know these are REG_UNUSED and want them to go to the desired insn,
2393 so we always pass it as i3. We have not counted the notes in
2394 reg_n_deaths yet, so we need to do so now. */
2395
2396 if (newi2pat && new_i2_notes)
2397 {
2398 for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2399 if (GET_CODE (XEXP (temp, 0)) == REG)
2400 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2401
2402 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2403 }
2404
2405 if (new_i3_notes)
2406 {
2407 for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2408 if (GET_CODE (XEXP (temp, 0)) == REG)
2409 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2410
2411 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
2412 }
2413
2414 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
2415 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
2416 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
2417 in that case, it might delete I2. Similarly for I2 and I1.
2418 Show an additional death due to the REG_DEAD note we make here. If
2419 we discard it in distribute_notes, we will decrement it again. */
2420
2421 if (i3dest_killed)
2422 {
2423 if (GET_CODE (i3dest_killed) == REG)
2424 REG_N_DEATHS (REGNO (i3dest_killed))++;
2425
2426 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2427 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2428 NULL_RTX),
2429 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
2430 else
2431 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2432 NULL_RTX),
2433 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2434 elim_i2, elim_i1);
2435 }
2436
2437 if (i2dest_in_i2src)
2438 {
2439 if (GET_CODE (i2dest) == REG)
2440 REG_N_DEATHS (REGNO (i2dest))++;
2441
2442 if (newi2pat && reg_set_p (i2dest, newi2pat))
2443 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2444 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2445 else
2446 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2447 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2448 NULL_RTX, NULL_RTX);
2449 }
2450
2451 if (i1dest_in_i1src)
2452 {
2453 if (GET_CODE (i1dest) == REG)
2454 REG_N_DEATHS (REGNO (i1dest))++;
2455
2456 if (newi2pat && reg_set_p (i1dest, newi2pat))
2457 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2458 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2459 else
2460 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2461 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2462 NULL_RTX, NULL_RTX);
2463 }
2464
2465 distribute_links (i3links);
2466 distribute_links (i2links);
2467 distribute_links (i1links);
2468
2469 if (GET_CODE (i2dest) == REG)
2470 {
2471 rtx link;
2472 rtx i2_insn = 0, i2_val = 0, set;
2473
2474 /* The insn that used to set this register doesn't exist, and
2475 this life of the register may not exist either. See if one of
2476 I3's links points to an insn that sets I2DEST. If it does,
2477 that is now the last known value for I2DEST. If we don't update
2478 this and I2 set the register to a value that depended on its old
2479 contents, we will get confused. If this insn is used, thing
2480 will be set correctly in combine_instructions. */
2481
2482 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2483 if ((set = single_set (XEXP (link, 0))) != 0
2484 && rtx_equal_p (i2dest, SET_DEST (set)))
2485 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2486
2487 record_value_for_reg (i2dest, i2_insn, i2_val);
2488
2489 /* If the reg formerly set in I2 died only once and that was in I3,
2490 zero its use count so it won't make `reload' do any work. */
2491 if (! added_sets_2
2492 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2493 && ! i2dest_in_i2src)
2494 {
2495 regno = REGNO (i2dest);
2496 REG_N_SETS (regno)--;
2497 if (REG_N_SETS (regno) == 0
2498 && ! REGNO_REG_SET_P (basic_block_live_at_start[0], regno))
2499 REG_N_REFS (regno) = 0;
2500 }
2501 }
2502
2503 if (i1 && GET_CODE (i1dest) == REG)
2504 {
2505 rtx link;
2506 rtx i1_insn = 0, i1_val = 0, set;
2507
2508 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2509 if ((set = single_set (XEXP (link, 0))) != 0
2510 && rtx_equal_p (i1dest, SET_DEST (set)))
2511 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2512
2513 record_value_for_reg (i1dest, i1_insn, i1_val);
2514
2515 regno = REGNO (i1dest);
2516 if (! added_sets_1 && ! i1dest_in_i1src)
2517 {
2518 REG_N_SETS (regno)--;
2519 if (REG_N_SETS (regno) == 0
2520 && ! REGNO_REG_SET_P (basic_block_live_at_start[0], regno))
2521 REG_N_REFS (regno) = 0;
2522 }
2523 }
2524
2525 /* Update reg_nonzero_bits et al for any changes that may have been made
2526 to this insn. */
2527
2528 note_stores (newpat, set_nonzero_bits_and_sign_copies);
2529 if (newi2pat)
2530 note_stores (newi2pat, set_nonzero_bits_and_sign_copies);
2531
2532 /* If we added any (clobber (scratch)), add them to the max for a
2533 block. This is a very pessimistic calculation, since we might
2534 have had them already and this might not be the worst block, but
2535 it's not worth doing any better. */
2536 max_scratch += i3_scratches + i2_scratches + other_scratches;
2537
2538 /* If I3 is now an unconditional jump, ensure that it has a
2539 BARRIER following it since it may have initially been a
2540 conditional jump. It may also be the last nonnote insn. */
2541
2542 if ((GET_CODE (newpat) == RETURN || simplejump_p (i3))
2543 && ((temp = next_nonnote_insn (i3)) == NULL_RTX
2544 || GET_CODE (temp) != BARRIER))
2545 emit_barrier_after (i3);
2546 }
2547
2548 combine_successes++;
2549
2550 /* Clear this here, so that subsequent get_last_value calls are not
2551 affected. */
2552 subst_prev_insn = NULL_RTX;
2553
2554 if (added_links_insn
2555 && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2556 && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2557 return added_links_insn;
2558 else
2559 return newi2pat ? i2 : i3;
2560 }
2561 \f
2562 /* Undo all the modifications recorded in undobuf. */
2563
2564 static void
2565 undo_all ()
2566 {
2567 struct undo *undo, *next;
2568
2569 for (undo = undobuf.undos; undo; undo = next)
2570 {
2571 next = undo->next;
2572 if (undo->is_int)
2573 *undo->where.i = undo->old_contents.i;
2574 else
2575 *undo->where.r = undo->old_contents.r;
2576
2577 undo->next = undobuf.frees;
2578 undobuf.frees = undo;
2579 }
2580
2581 obfree (undobuf.storage);
2582 undobuf.undos = undobuf.previous_undos = 0;
2583
2584 /* Clear this here, so that subsequent get_last_value calls are not
2585 affected. */
2586 subst_prev_insn = NULL_RTX;
2587 }
2588 \f
2589 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2590 where we have an arithmetic expression and return that point. LOC will
2591 be inside INSN.
2592
2593 try_combine will call this function to see if an insn can be split into
2594 two insns. */
2595
2596 static rtx *
2597 find_split_point (loc, insn)
2598 rtx *loc;
2599 rtx insn;
2600 {
2601 rtx x = *loc;
2602 enum rtx_code code = GET_CODE (x);
2603 rtx *split;
2604 int len = 0, pos, unsignedp;
2605 rtx inner;
2606
2607 /* First special-case some codes. */
2608 switch (code)
2609 {
2610 case SUBREG:
2611 #ifdef INSN_SCHEDULING
2612 /* If we are making a paradoxical SUBREG invalid, it becomes a split
2613 point. */
2614 if (GET_CODE (SUBREG_REG (x)) == MEM)
2615 return loc;
2616 #endif
2617 return find_split_point (&SUBREG_REG (x), insn);
2618
2619 case MEM:
2620 #ifdef HAVE_lo_sum
2621 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2622 using LO_SUM and HIGH. */
2623 if (GET_CODE (XEXP (x, 0)) == CONST
2624 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2625 {
2626 SUBST (XEXP (x, 0),
2627 gen_rtx_combine (LO_SUM, Pmode,
2628 gen_rtx_combine (HIGH, Pmode, XEXP (x, 0)),
2629 XEXP (x, 0)));
2630 return &XEXP (XEXP (x, 0), 0);
2631 }
2632 #endif
2633
2634 /* If we have a PLUS whose second operand is a constant and the
2635 address is not valid, perhaps will can split it up using
2636 the machine-specific way to split large constants. We use
2637 the first pseudo-reg (one of the virtual regs) as a placeholder;
2638 it will not remain in the result. */
2639 if (GET_CODE (XEXP (x, 0)) == PLUS
2640 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2641 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2642 {
2643 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2644 rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
2645 subst_insn);
2646
2647 /* This should have produced two insns, each of which sets our
2648 placeholder. If the source of the second is a valid address,
2649 we can make put both sources together and make a split point
2650 in the middle. */
2651
2652 if (seq && XVECLEN (seq, 0) == 2
2653 && GET_CODE (XVECEXP (seq, 0, 0)) == INSN
2654 && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) == SET
2655 && SET_DEST (PATTERN (XVECEXP (seq, 0, 0))) == reg
2656 && ! reg_mentioned_p (reg,
2657 SET_SRC (PATTERN (XVECEXP (seq, 0, 0))))
2658 && GET_CODE (XVECEXP (seq, 0, 1)) == INSN
2659 && GET_CODE (PATTERN (XVECEXP (seq, 0, 1))) == SET
2660 && SET_DEST (PATTERN (XVECEXP (seq, 0, 1))) == reg
2661 && memory_address_p (GET_MODE (x),
2662 SET_SRC (PATTERN (XVECEXP (seq, 0, 1)))))
2663 {
2664 rtx src1 = SET_SRC (PATTERN (XVECEXP (seq, 0, 0)));
2665 rtx src2 = SET_SRC (PATTERN (XVECEXP (seq, 0, 1)));
2666
2667 /* Replace the placeholder in SRC2 with SRC1. If we can
2668 find where in SRC2 it was placed, that can become our
2669 split point and we can replace this address with SRC2.
2670 Just try two obvious places. */
2671
2672 src2 = replace_rtx (src2, reg, src1);
2673 split = 0;
2674 if (XEXP (src2, 0) == src1)
2675 split = &XEXP (src2, 0);
2676 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2677 && XEXP (XEXP (src2, 0), 0) == src1)
2678 split = &XEXP (XEXP (src2, 0), 0);
2679
2680 if (split)
2681 {
2682 SUBST (XEXP (x, 0), src2);
2683 return split;
2684 }
2685 }
2686
2687 /* If that didn't work, perhaps the first operand is complex and
2688 needs to be computed separately, so make a split point there.
2689 This will occur on machines that just support REG + CONST
2690 and have a constant moved through some previous computation. */
2691
2692 else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2693 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2694 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2695 == 'o')))
2696 return &XEXP (XEXP (x, 0), 0);
2697 }
2698 break;
2699
2700 case SET:
2701 #ifdef HAVE_cc0
2702 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2703 ZERO_EXTRACT, the most likely reason why this doesn't match is that
2704 we need to put the operand into a register. So split at that
2705 point. */
2706
2707 if (SET_DEST (x) == cc0_rtx
2708 && GET_CODE (SET_SRC (x)) != COMPARE
2709 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2710 && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2711 && ! (GET_CODE (SET_SRC (x)) == SUBREG
2712 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2713 return &SET_SRC (x);
2714 #endif
2715
2716 /* See if we can split SET_SRC as it stands. */
2717 split = find_split_point (&SET_SRC (x), insn);
2718 if (split && split != &SET_SRC (x))
2719 return split;
2720
2721 /* See if we can split SET_DEST as it stands. */
2722 split = find_split_point (&SET_DEST (x), insn);
2723 if (split && split != &SET_DEST (x))
2724 return split;
2725
2726 /* See if this is a bitfield assignment with everything constant. If
2727 so, this is an IOR of an AND, so split it into that. */
2728 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2729 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2730 <= HOST_BITS_PER_WIDE_INT)
2731 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
2732 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
2733 && GET_CODE (SET_SRC (x)) == CONST_INT
2734 && ((INTVAL (XEXP (SET_DEST (x), 1))
2735 + INTVAL (XEXP (SET_DEST (x), 2)))
2736 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
2737 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
2738 {
2739 int pos = INTVAL (XEXP (SET_DEST (x), 2));
2740 int len = INTVAL (XEXP (SET_DEST (x), 1));
2741 int src = INTVAL (SET_SRC (x));
2742 rtx dest = XEXP (SET_DEST (x), 0);
2743 enum machine_mode mode = GET_MODE (dest);
2744 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
2745
2746 if (BITS_BIG_ENDIAN)
2747 pos = GET_MODE_BITSIZE (mode) - len - pos;
2748
2749 if (src == mask)
2750 SUBST (SET_SRC (x),
2751 gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
2752 else
2753 SUBST (SET_SRC (x),
2754 gen_binary (IOR, mode,
2755 gen_binary (AND, mode, dest,
2756 GEN_INT (~ (mask << pos)
2757 & GET_MODE_MASK (mode))),
2758 GEN_INT (src << pos)));
2759
2760 SUBST (SET_DEST (x), dest);
2761
2762 split = find_split_point (&SET_SRC (x), insn);
2763 if (split && split != &SET_SRC (x))
2764 return split;
2765 }
2766
2767 /* Otherwise, see if this is an operation that we can split into two.
2768 If so, try to split that. */
2769 code = GET_CODE (SET_SRC (x));
2770
2771 switch (code)
2772 {
2773 case AND:
2774 /* If we are AND'ing with a large constant that is only a single
2775 bit and the result is only being used in a context where we
2776 need to know if it is zero or non-zero, replace it with a bit
2777 extraction. This will avoid the large constant, which might
2778 have taken more than one insn to make. If the constant were
2779 not a valid argument to the AND but took only one insn to make,
2780 this is no worse, but if it took more than one insn, it will
2781 be better. */
2782
2783 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2784 && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
2785 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
2786 && GET_CODE (SET_DEST (x)) == REG
2787 && (split = find_single_use (SET_DEST (x), insn, NULL_PTR)) != 0
2788 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
2789 && XEXP (*split, 0) == SET_DEST (x)
2790 && XEXP (*split, 1) == const0_rtx)
2791 {
2792 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
2793 XEXP (SET_SRC (x), 0),
2794 pos, NULL_RTX, 1, 1, 0, 0);
2795 if (extraction != 0)
2796 {
2797 SUBST (SET_SRC (x), extraction);
2798 return find_split_point (loc, insn);
2799 }
2800 }
2801 break;
2802
2803 case NE:
2804 /* if STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
2805 is known to be on, this can be converted into a NEG of a shift. */
2806 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
2807 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
2808 && 1 <= (pos = exact_log2
2809 (nonzero_bits (XEXP (SET_SRC (x), 0),
2810 GET_MODE (XEXP (SET_SRC (x), 0))))))
2811 {
2812 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
2813
2814 SUBST (SET_SRC (x),
2815 gen_rtx_combine (NEG, mode,
2816 gen_rtx_combine (LSHIFTRT, mode,
2817 XEXP (SET_SRC (x), 0),
2818 GEN_INT (pos))));
2819
2820 split = find_split_point (&SET_SRC (x), insn);
2821 if (split && split != &SET_SRC (x))
2822 return split;
2823 }
2824 break;
2825
2826 case SIGN_EXTEND:
2827 inner = XEXP (SET_SRC (x), 0);
2828
2829 /* We can't optimize if either mode is a partial integer
2830 mode as we don't know how many bits are significant
2831 in those modes. */
2832 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
2833 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
2834 break;
2835
2836 pos = 0;
2837 len = GET_MODE_BITSIZE (GET_MODE (inner));
2838 unsignedp = 0;
2839 break;
2840
2841 case SIGN_EXTRACT:
2842 case ZERO_EXTRACT:
2843 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2844 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
2845 {
2846 inner = XEXP (SET_SRC (x), 0);
2847 len = INTVAL (XEXP (SET_SRC (x), 1));
2848 pos = INTVAL (XEXP (SET_SRC (x), 2));
2849
2850 if (BITS_BIG_ENDIAN)
2851 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
2852 unsignedp = (code == ZERO_EXTRACT);
2853 }
2854 break;
2855
2856 default:
2857 break;
2858 }
2859
2860 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
2861 {
2862 enum machine_mode mode = GET_MODE (SET_SRC (x));
2863
2864 /* For unsigned, we have a choice of a shift followed by an
2865 AND or two shifts. Use two shifts for field sizes where the
2866 constant might be too large. We assume here that we can
2867 always at least get 8-bit constants in an AND insn, which is
2868 true for every current RISC. */
2869
2870 if (unsignedp && len <= 8)
2871 {
2872 SUBST (SET_SRC (x),
2873 gen_rtx_combine
2874 (AND, mode,
2875 gen_rtx_combine (LSHIFTRT, mode,
2876 gen_lowpart_for_combine (mode, inner),
2877 GEN_INT (pos)),
2878 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
2879
2880 split = find_split_point (&SET_SRC (x), insn);
2881 if (split && split != &SET_SRC (x))
2882 return split;
2883 }
2884 else
2885 {
2886 SUBST (SET_SRC (x),
2887 gen_rtx_combine
2888 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
2889 gen_rtx_combine (ASHIFT, mode,
2890 gen_lowpart_for_combine (mode, inner),
2891 GEN_INT (GET_MODE_BITSIZE (mode)
2892 - len - pos)),
2893 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
2894
2895 split = find_split_point (&SET_SRC (x), insn);
2896 if (split && split != &SET_SRC (x))
2897 return split;
2898 }
2899 }
2900
2901 /* See if this is a simple operation with a constant as the second
2902 operand. It might be that this constant is out of range and hence
2903 could be used as a split point. */
2904 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2905 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2906 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
2907 && CONSTANT_P (XEXP (SET_SRC (x), 1))
2908 && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
2909 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
2910 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
2911 == 'o'))))
2912 return &XEXP (SET_SRC (x), 1);
2913
2914 /* Finally, see if this is a simple operation with its first operand
2915 not in a register. The operation might require this operand in a
2916 register, so return it as a split point. We can always do this
2917 because if the first operand were another operation, we would have
2918 already found it as a split point. */
2919 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2920 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2921 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
2922 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
2923 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
2924 return &XEXP (SET_SRC (x), 0);
2925
2926 return 0;
2927
2928 case AND:
2929 case IOR:
2930 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
2931 it is better to write this as (not (ior A B)) so we can split it.
2932 Similarly for IOR. */
2933 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
2934 {
2935 SUBST (*loc,
2936 gen_rtx_combine (NOT, GET_MODE (x),
2937 gen_rtx_combine (code == IOR ? AND : IOR,
2938 GET_MODE (x),
2939 XEXP (XEXP (x, 0), 0),
2940 XEXP (XEXP (x, 1), 0))));
2941 return find_split_point (loc, insn);
2942 }
2943
2944 /* Many RISC machines have a large set of logical insns. If the
2945 second operand is a NOT, put it first so we will try to split the
2946 other operand first. */
2947 if (GET_CODE (XEXP (x, 1)) == NOT)
2948 {
2949 rtx tem = XEXP (x, 0);
2950 SUBST (XEXP (x, 0), XEXP (x, 1));
2951 SUBST (XEXP (x, 1), tem);
2952 }
2953 break;
2954
2955 default:
2956 break;
2957 }
2958
2959 /* Otherwise, select our actions depending on our rtx class. */
2960 switch (GET_RTX_CLASS (code))
2961 {
2962 case 'b': /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
2963 case '3':
2964 split = find_split_point (&XEXP (x, 2), insn);
2965 if (split)
2966 return split;
2967 /* ... fall through ... */
2968 case '2':
2969 case 'c':
2970 case '<':
2971 split = find_split_point (&XEXP (x, 1), insn);
2972 if (split)
2973 return split;
2974 /* ... fall through ... */
2975 case '1':
2976 /* Some machines have (and (shift ...) ...) insns. If X is not
2977 an AND, but XEXP (X, 0) is, use it as our split point. */
2978 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
2979 return &XEXP (x, 0);
2980
2981 split = find_split_point (&XEXP (x, 0), insn);
2982 if (split)
2983 return split;
2984 return loc;
2985 }
2986
2987 /* Otherwise, we don't have a split point. */
2988 return 0;
2989 }
2990 \f
2991 /* Throughout X, replace FROM with TO, and return the result.
2992 The result is TO if X is FROM;
2993 otherwise the result is X, but its contents may have been modified.
2994 If they were modified, a record was made in undobuf so that
2995 undo_all will (among other things) return X to its original state.
2996
2997 If the number of changes necessary is too much to record to undo,
2998 the excess changes are not made, so the result is invalid.
2999 The changes already made can still be undone.
3000 undobuf.num_undo is incremented for such changes, so by testing that
3001 the caller can tell whether the result is valid.
3002
3003 `n_occurrences' is incremented each time FROM is replaced.
3004
3005 IN_DEST is non-zero if we are processing the SET_DEST of a SET.
3006
3007 UNIQUE_COPY is non-zero if each substitution must be unique. We do this
3008 by copying if `n_occurrences' is non-zero. */
3009
3010 static rtx
3011 subst (x, from, to, in_dest, unique_copy)
3012 register rtx x, from, to;
3013 int in_dest;
3014 int unique_copy;
3015 {
3016 register enum rtx_code code = GET_CODE (x);
3017 enum machine_mode op0_mode = VOIDmode;
3018 register char *fmt;
3019 register int len, i;
3020 rtx new;
3021
3022 /* Two expressions are equal if they are identical copies of a shared
3023 RTX or if they are both registers with the same register number
3024 and mode. */
3025
3026 #define COMBINE_RTX_EQUAL_P(X,Y) \
3027 ((X) == (Y) \
3028 || (GET_CODE (X) == REG && GET_CODE (Y) == REG \
3029 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3030
3031 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3032 {
3033 n_occurrences++;
3034 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3035 }
3036
3037 /* If X and FROM are the same register but different modes, they will
3038 not have been seen as equal above. However, flow.c will make a
3039 LOG_LINKS entry for that case. If we do nothing, we will try to
3040 rerecognize our original insn and, when it succeeds, we will
3041 delete the feeding insn, which is incorrect.
3042
3043 So force this insn not to match in this (rare) case. */
3044 if (! in_dest && code == REG && GET_CODE (from) == REG
3045 && REGNO (x) == REGNO (from))
3046 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3047
3048 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3049 of which may contain things that can be combined. */
3050 if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
3051 return x;
3052
3053 /* It is possible to have a subexpression appear twice in the insn.
3054 Suppose that FROM is a register that appears within TO.
3055 Then, after that subexpression has been scanned once by `subst',
3056 the second time it is scanned, TO may be found. If we were
3057 to scan TO here, we would find FROM within it and create a
3058 self-referent rtl structure which is completely wrong. */
3059 if (COMBINE_RTX_EQUAL_P (x, to))
3060 return to;
3061
3062 len = GET_RTX_LENGTH (code);
3063 fmt = GET_RTX_FORMAT (code);
3064
3065 /* We don't need to process a SET_DEST that is a register, CC0, or PC, so
3066 set up to skip this common case. All other cases where we want to
3067 suppress replacing something inside a SET_SRC are handled via the
3068 IN_DEST operand. */
3069 if (code == SET
3070 && (GET_CODE (SET_DEST (x)) == REG
3071 || GET_CODE (SET_DEST (x)) == CC0
3072 || GET_CODE (SET_DEST (x)) == PC))
3073 fmt = "ie";
3074
3075 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3076 constant. */
3077 if (fmt[0] == 'e')
3078 op0_mode = GET_MODE (XEXP (x, 0));
3079
3080 for (i = 0; i < len; i++)
3081 {
3082 if (fmt[i] == 'E')
3083 {
3084 register int j;
3085 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3086 {
3087 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3088 {
3089 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3090 n_occurrences++;
3091 }
3092 else
3093 {
3094 new = subst (XVECEXP (x, i, j), from, to, 0, unique_copy);
3095
3096 /* If this substitution failed, this whole thing fails. */
3097 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3098 return new;
3099 }
3100
3101 SUBST (XVECEXP (x, i, j), new);
3102 }
3103 }
3104 else if (fmt[i] == 'e')
3105 {
3106 if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3107 {
3108 /* In general, don't install a subreg involving two modes not
3109 tieable. It can worsen register allocation, and can even
3110 make invalid reload insns, since the reg inside may need to
3111 be copied from in the outside mode, and that may be invalid
3112 if it is an fp reg copied in integer mode.
3113
3114 We allow two exceptions to this: It is valid if it is inside
3115 another SUBREG and the mode of that SUBREG and the mode of
3116 the inside of TO is tieable and it is valid if X is a SET
3117 that copies FROM to CC0. */
3118 if (GET_CODE (to) == SUBREG
3119 && ! MODES_TIEABLE_P (GET_MODE (to),
3120 GET_MODE (SUBREG_REG (to)))
3121 && ! (code == SUBREG
3122 && MODES_TIEABLE_P (GET_MODE (x),
3123 GET_MODE (SUBREG_REG (to))))
3124 #ifdef HAVE_cc0
3125 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3126 #endif
3127 )
3128 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3129
3130 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3131 n_occurrences++;
3132 }
3133 else
3134 /* If we are in a SET_DEST, suppress most cases unless we
3135 have gone inside a MEM, in which case we want to
3136 simplify the address. We assume here that things that
3137 are actually part of the destination have their inner
3138 parts in the first expression. This is true for SUBREG,
3139 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3140 things aside from REG and MEM that should appear in a
3141 SET_DEST. */
3142 new = subst (XEXP (x, i), from, to,
3143 (((in_dest
3144 && (code == SUBREG || code == STRICT_LOW_PART
3145 || code == ZERO_EXTRACT))
3146 || code == SET)
3147 && i == 0), unique_copy);
3148
3149 /* If we found that we will have to reject this combination,
3150 indicate that by returning the CLOBBER ourselves, rather than
3151 an expression containing it. This will speed things up as
3152 well as prevent accidents where two CLOBBERs are considered
3153 to be equal, thus producing an incorrect simplification. */
3154
3155 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3156 return new;
3157
3158 SUBST (XEXP (x, i), new);
3159 }
3160 }
3161
3162 /* Try to simplify X. If the simplification changed the code, it is likely
3163 that further simplification will help, so loop, but limit the number
3164 of repetitions that will be performed. */
3165
3166 for (i = 0; i < 4; i++)
3167 {
3168 /* If X is sufficiently simple, don't bother trying to do anything
3169 with it. */
3170 if (code != CONST_INT && code != REG && code != CLOBBER)
3171 x = simplify_rtx (x, op0_mode, i == 3, in_dest);
3172
3173 if (GET_CODE (x) == code)
3174 break;
3175
3176 code = GET_CODE (x);
3177
3178 /* We no longer know the original mode of operand 0 since we
3179 have changed the form of X) */
3180 op0_mode = VOIDmode;
3181 }
3182
3183 return x;
3184 }
3185 \f
3186 /* Simplify X, a piece of RTL. We just operate on the expression at the
3187 outer level; call `subst' to simplify recursively. Return the new
3188 expression.
3189
3190 OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
3191 will be the iteration even if an expression with a code different from
3192 X is returned; IN_DEST is nonzero if we are inside a SET_DEST. */
3193
3194 static rtx
3195 simplify_rtx (x, op0_mode, last, in_dest)
3196 rtx x;
3197 enum machine_mode op0_mode;
3198 int last;
3199 int in_dest;
3200 {
3201 enum rtx_code code = GET_CODE (x);
3202 enum machine_mode mode = GET_MODE (x);
3203 rtx temp;
3204 int i;
3205
3206 /* If this is a commutative operation, put a constant last and a complex
3207 expression first. We don't need to do this for comparisons here. */
3208 if (GET_RTX_CLASS (code) == 'c'
3209 && ((CONSTANT_P (XEXP (x, 0)) && GET_CODE (XEXP (x, 1)) != CONST_INT)
3210 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == 'o'
3211 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')
3212 || (GET_CODE (XEXP (x, 0)) == SUBREG
3213 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == 'o'
3214 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')))
3215 {
3216 temp = XEXP (x, 0);
3217 SUBST (XEXP (x, 0), XEXP (x, 1));
3218 SUBST (XEXP (x, 1), temp);
3219 }
3220
3221 /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3222 sign extension of a PLUS with a constant, reverse the order of the sign
3223 extension and the addition. Note that this not the same as the original
3224 code, but overflow is undefined for signed values. Also note that the
3225 PLUS will have been partially moved "inside" the sign-extension, so that
3226 the first operand of X will really look like:
3227 (ashiftrt (plus (ashift A C4) C5) C4).
3228 We convert this to
3229 (plus (ashiftrt (ashift A C4) C2) C4)
3230 and replace the first operand of X with that expression. Later parts
3231 of this function may simplify the expression further.
3232
3233 For example, if we start with (mult (sign_extend (plus A C1)) C2),
3234 we swap the SIGN_EXTEND and PLUS. Later code will apply the
3235 distributive law to produce (plus (mult (sign_extend X) C1) C3).
3236
3237 We do this to simplify address expressions. */
3238
3239 if ((code == PLUS || code == MINUS || code == MULT)
3240 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3241 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3242 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3243 && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3244 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3245 && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3246 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3247 && (temp = simplify_binary_operation (ASHIFTRT, mode,
3248 XEXP (XEXP (XEXP (x, 0), 0), 1),
3249 XEXP (XEXP (x, 0), 1))) != 0)
3250 {
3251 rtx new
3252 = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3253 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3254 INTVAL (XEXP (XEXP (x, 0), 1)));
3255
3256 new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3257 INTVAL (XEXP (XEXP (x, 0), 1)));
3258
3259 SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3260 }
3261
3262 /* If this is a simple operation applied to an IF_THEN_ELSE, try
3263 applying it to the arms of the IF_THEN_ELSE. This often simplifies
3264 things. Check for cases where both arms are testing the same
3265 condition.
3266
3267 Don't do anything if all operands are very simple. */
3268
3269 if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3270 || GET_RTX_CLASS (code) == '<')
3271 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3272 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3273 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3274 == 'o')))
3275 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3276 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3277 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3278 == 'o')))))
3279 || (GET_RTX_CLASS (code) == '1'
3280 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3281 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3282 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3283 == 'o'))))))
3284 {
3285 rtx cond, true, false;
3286
3287 cond = if_then_else_cond (x, &true, &false);
3288 if (cond != 0
3289 /* If everything is a comparison, what we have is highly unlikely
3290 to be simpler, so don't use it. */
3291 && ! (GET_RTX_CLASS (code) == '<'
3292 && (GET_RTX_CLASS (GET_CODE (true)) == '<'
3293 || GET_RTX_CLASS (GET_CODE (false)) == '<')))
3294 {
3295 rtx cop1 = const0_rtx;
3296 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3297
3298 if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3299 return x;
3300
3301 /* Simplify the alternative arms; this may collapse the true and
3302 false arms to store-flag values. */
3303 true = subst (true, pc_rtx, pc_rtx, 0, 0);
3304 false = subst (false, pc_rtx, pc_rtx, 0, 0);
3305
3306 /* Restarting if we generate a store-flag expression will cause
3307 us to loop. Just drop through in this case. */
3308
3309 /* If the result values are STORE_FLAG_VALUE and zero, we can
3310 just make the comparison operation. */
3311 if (true == const_true_rtx && false == const0_rtx)
3312 x = gen_binary (cond_code, mode, cond, cop1);
3313 else if (true == const0_rtx && false == const_true_rtx)
3314 x = gen_binary (reverse_condition (cond_code), mode, cond, cop1);
3315
3316 /* Likewise, we can make the negate of a comparison operation
3317 if the result values are - STORE_FLAG_VALUE and zero. */
3318 else if (GET_CODE (true) == CONST_INT
3319 && INTVAL (true) == - STORE_FLAG_VALUE
3320 && false == const0_rtx)
3321 x = gen_unary (NEG, mode, mode,
3322 gen_binary (cond_code, mode, cond, cop1));
3323 else if (GET_CODE (false) == CONST_INT
3324 && INTVAL (false) == - STORE_FLAG_VALUE
3325 && true == const0_rtx)
3326 x = gen_unary (NEG, mode, mode,
3327 gen_binary (reverse_condition (cond_code),
3328 mode, cond, cop1));
3329 else
3330 return gen_rtx_IF_THEN_ELSE (mode,
3331 gen_binary (cond_code, VOIDmode,
3332 cond, cop1),
3333 true, false);
3334
3335 code = GET_CODE (x);
3336 op0_mode = VOIDmode;
3337 }
3338 }
3339
3340 /* Try to fold this expression in case we have constants that weren't
3341 present before. */
3342 temp = 0;
3343 switch (GET_RTX_CLASS (code))
3344 {
3345 case '1':
3346 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3347 break;
3348 case '<':
3349 temp = simplify_relational_operation (code, op0_mode,
3350 XEXP (x, 0), XEXP (x, 1));
3351 #ifdef FLOAT_STORE_FLAG_VALUE
3352 if (temp != 0 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3353 temp = ((temp == const0_rtx) ? CONST0_RTX (GET_MODE (x))
3354 : immed_real_const_1 (FLOAT_STORE_FLAG_VALUE, GET_MODE (x)));
3355 #endif
3356 break;
3357 case 'c':
3358 case '2':
3359 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3360 break;
3361 case 'b':
3362 case '3':
3363 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3364 XEXP (x, 1), XEXP (x, 2));
3365 break;
3366 }
3367
3368 if (temp)
3369 x = temp, code = GET_CODE (temp);
3370
3371 /* First see if we can apply the inverse distributive law. */
3372 if (code == PLUS || code == MINUS
3373 || code == AND || code == IOR || code == XOR)
3374 {
3375 x = apply_distributive_law (x);
3376 code = GET_CODE (x);
3377 }
3378
3379 /* If CODE is an associative operation not otherwise handled, see if we
3380 can associate some operands. This can win if they are constants or
3381 if they are logically related (i.e. (a & b) & a. */
3382 if ((code == PLUS || code == MINUS
3383 || code == MULT || code == AND || code == IOR || code == XOR
3384 || code == DIV || code == UDIV
3385 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3386 && INTEGRAL_MODE_P (mode))
3387 {
3388 if (GET_CODE (XEXP (x, 0)) == code)
3389 {
3390 rtx other = XEXP (XEXP (x, 0), 0);
3391 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3392 rtx inner_op1 = XEXP (x, 1);
3393 rtx inner;
3394
3395 /* Make sure we pass the constant operand if any as the second
3396 one if this is a commutative operation. */
3397 if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3398 {
3399 rtx tem = inner_op0;
3400 inner_op0 = inner_op1;
3401 inner_op1 = tem;
3402 }
3403 inner = simplify_binary_operation (code == MINUS ? PLUS
3404 : code == DIV ? MULT
3405 : code == UDIV ? MULT
3406 : code,
3407 mode, inner_op0, inner_op1);
3408
3409 /* For commutative operations, try the other pair if that one
3410 didn't simplify. */
3411 if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3412 {
3413 other = XEXP (XEXP (x, 0), 1);
3414 inner = simplify_binary_operation (code, mode,
3415 XEXP (XEXP (x, 0), 0),
3416 XEXP (x, 1));
3417 }
3418
3419 if (inner)
3420 return gen_binary (code, mode, other, inner);
3421 }
3422 }
3423
3424 /* A little bit of algebraic simplification here. */
3425 switch (code)
3426 {
3427 case MEM:
3428 /* Ensure that our address has any ASHIFTs converted to MULT in case
3429 address-recognizing predicates are called later. */
3430 temp = make_compound_operation (XEXP (x, 0), MEM);
3431 SUBST (XEXP (x, 0), temp);
3432 break;
3433
3434 case SUBREG:
3435 /* (subreg:A (mem:B X) N) becomes a modified MEM unless the SUBREG
3436 is paradoxical. If we can't do that safely, then it becomes
3437 something nonsensical so that this combination won't take place. */
3438
3439 if (GET_CODE (SUBREG_REG (x)) == MEM
3440 && (GET_MODE_SIZE (mode)
3441 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3442 {
3443 rtx inner = SUBREG_REG (x);
3444 int endian_offset = 0;
3445 /* Don't change the mode of the MEM
3446 if that would change the meaning of the address. */
3447 if (MEM_VOLATILE_P (SUBREG_REG (x))
3448 || mode_dependent_address_p (XEXP (inner, 0)))
3449 return gen_rtx_CLOBBER (mode, const0_rtx);
3450
3451 if (BYTES_BIG_ENDIAN)
3452 {
3453 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3454 endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (mode);
3455 if (GET_MODE_SIZE (GET_MODE (inner)) < UNITS_PER_WORD)
3456 endian_offset -= (UNITS_PER_WORD
3457 - GET_MODE_SIZE (GET_MODE (inner)));
3458 }
3459 /* Note if the plus_constant doesn't make a valid address
3460 then this combination won't be accepted. */
3461 x = gen_rtx_MEM (mode,
3462 plus_constant (XEXP (inner, 0),
3463 (SUBREG_WORD (x) * UNITS_PER_WORD
3464 + endian_offset)));
3465 MEM_VOLATILE_P (x) = MEM_VOLATILE_P (inner);
3466 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (inner);
3467 MEM_IN_STRUCT_P (x) = MEM_IN_STRUCT_P (inner);
3468 return x;
3469 }
3470
3471 /* If we are in a SET_DEST, these other cases can't apply. */
3472 if (in_dest)
3473 return x;
3474
3475 /* Changing mode twice with SUBREG => just change it once,
3476 or not at all if changing back to starting mode. */
3477 if (GET_CODE (SUBREG_REG (x)) == SUBREG)
3478 {
3479 if (mode == GET_MODE (SUBREG_REG (SUBREG_REG (x)))
3480 && SUBREG_WORD (x) == 0 && SUBREG_WORD (SUBREG_REG (x)) == 0)
3481 return SUBREG_REG (SUBREG_REG (x));
3482
3483 SUBST_INT (SUBREG_WORD (x),
3484 SUBREG_WORD (x) + SUBREG_WORD (SUBREG_REG (x)));
3485 SUBST (SUBREG_REG (x), SUBREG_REG (SUBREG_REG (x)));
3486 }
3487
3488 /* SUBREG of a hard register => just change the register number
3489 and/or mode. If the hard register is not valid in that mode,
3490 suppress this combination. If the hard register is the stack,
3491 frame, or argument pointer, leave this as a SUBREG. */
3492
3493 if (GET_CODE (SUBREG_REG (x)) == REG
3494 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
3495 && REGNO (SUBREG_REG (x)) != FRAME_POINTER_REGNUM
3496 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3497 && REGNO (SUBREG_REG (x)) != HARD_FRAME_POINTER_REGNUM
3498 #endif
3499 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3500 && REGNO (SUBREG_REG (x)) != ARG_POINTER_REGNUM
3501 #endif
3502 && REGNO (SUBREG_REG (x)) != STACK_POINTER_REGNUM)
3503 {
3504 if (HARD_REGNO_MODE_OK (REGNO (SUBREG_REG (x)) + SUBREG_WORD (x),
3505 mode))
3506 return gen_rtx_REG (mode,
3507 REGNO (SUBREG_REG (x)) + SUBREG_WORD (x));
3508 else
3509 return gen_rtx_CLOBBER (mode, const0_rtx);
3510 }
3511
3512 /* For a constant, try to pick up the part we want. Handle a full
3513 word and low-order part. Only do this if we are narrowing
3514 the constant; if it is being widened, we have no idea what
3515 the extra bits will have been set to. */
3516
3517 if (CONSTANT_P (SUBREG_REG (x)) && op0_mode != VOIDmode
3518 && GET_MODE_SIZE (mode) == UNITS_PER_WORD
3519 && GET_MODE_SIZE (op0_mode) > UNITS_PER_WORD
3520 && GET_MODE_CLASS (mode) == MODE_INT)
3521 {
3522 temp = operand_subword (SUBREG_REG (x), SUBREG_WORD (x),
3523 0, op0_mode);
3524 if (temp)
3525 return temp;
3526 }
3527
3528 /* If we want a subreg of a constant, at offset 0,
3529 take the low bits. On a little-endian machine, that's
3530 always valid. On a big-endian machine, it's valid
3531 only if the constant's mode fits in one word. Note that we
3532 cannot use subreg_lowpart_p since SUBREG_REG may be VOIDmode. */
3533 if (CONSTANT_P (SUBREG_REG (x))
3534 && ((GET_MODE_SIZE (op0_mode) <= UNITS_PER_WORD
3535 || ! WORDS_BIG_ENDIAN)
3536 ? SUBREG_WORD (x) == 0
3537 : (SUBREG_WORD (x)
3538 == ((GET_MODE_SIZE (op0_mode)
3539 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
3540 / UNITS_PER_WORD)))
3541 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (op0_mode)
3542 && (! WORDS_BIG_ENDIAN
3543 || GET_MODE_BITSIZE (op0_mode) <= BITS_PER_WORD))
3544 return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3545
3546 /* A paradoxical SUBREG of a VOIDmode constant is the same constant,
3547 since we are saying that the high bits don't matter. */
3548 if (CONSTANT_P (SUBREG_REG (x)) && GET_MODE (SUBREG_REG (x)) == VOIDmode
3549 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (op0_mode))
3550 return SUBREG_REG (x);
3551
3552 /* Note that we cannot do any narrowing for non-constants since
3553 we might have been counting on using the fact that some bits were
3554 zero. We now do this in the SET. */
3555
3556 break;
3557
3558 case NOT:
3559 /* (not (plus X -1)) can become (neg X). */
3560 if (GET_CODE (XEXP (x, 0)) == PLUS
3561 && XEXP (XEXP (x, 0), 1) == constm1_rtx)
3562 return gen_rtx_combine (NEG, mode, XEXP (XEXP (x, 0), 0));
3563
3564 /* Similarly, (not (neg X)) is (plus X -1). */
3565 if (GET_CODE (XEXP (x, 0)) == NEG)
3566 return gen_rtx_combine (PLUS, mode, XEXP (XEXP (x, 0), 0),
3567 constm1_rtx);
3568
3569 /* (not (xor X C)) for C constant is (xor X D) with D = ~ C. */
3570 if (GET_CODE (XEXP (x, 0)) == XOR
3571 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3572 && (temp = simplify_unary_operation (NOT, mode,
3573 XEXP (XEXP (x, 0), 1),
3574 mode)) != 0)
3575 return gen_binary (XOR, mode, XEXP (XEXP (x, 0), 0), temp);
3576
3577 /* (not (ashift 1 X)) is (rotate ~1 X). We used to do this for operands
3578 other than 1, but that is not valid. We could do a similar
3579 simplification for (not (lshiftrt C X)) where C is just the sign bit,
3580 but this doesn't seem common enough to bother with. */
3581 if (GET_CODE (XEXP (x, 0)) == ASHIFT
3582 && XEXP (XEXP (x, 0), 0) == const1_rtx)
3583 return gen_rtx_ROTATE (mode, gen_unary (NOT, mode, mode, const1_rtx),
3584 XEXP (XEXP (x, 0), 1));
3585
3586 if (GET_CODE (XEXP (x, 0)) == SUBREG
3587 && subreg_lowpart_p (XEXP (x, 0))
3588 && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3589 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3590 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3591 && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3592 {
3593 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3594
3595 x = gen_rtx_ROTATE (inner_mode,
3596 gen_unary (NOT, inner_mode, inner_mode,
3597 const1_rtx),
3598 XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3599 return gen_lowpart_for_combine (mode, x);
3600 }
3601
3602 /* If STORE_FLAG_VALUE is -1, (not (comparison foo bar)) can be done by
3603 reversing the comparison code if valid. */
3604 if (STORE_FLAG_VALUE == -1
3605 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3606 && reversible_comparison_p (XEXP (x, 0)))
3607 return gen_rtx_combine (reverse_condition (GET_CODE (XEXP (x, 0))),
3608 mode, XEXP (XEXP (x, 0), 0),
3609 XEXP (XEXP (x, 0), 1));
3610
3611 /* (ashiftrt foo C) where C is the number of bits in FOO minus 1
3612 is (lt foo (const_int 0)) if STORE_FLAG_VALUE is -1, so we can
3613 perform the above simplification. */
3614
3615 if (STORE_FLAG_VALUE == -1
3616 && XEXP (x, 1) == const1_rtx
3617 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3618 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3619 && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
3620 return gen_rtx_combine (GE, mode, XEXP (XEXP (x, 0), 0), const0_rtx);
3621
3622 /* Apply De Morgan's laws to reduce number of patterns for machines
3623 with negating logical insns (and-not, nand, etc.). If result has
3624 only one NOT, put it first, since that is how the patterns are
3625 coded. */
3626
3627 if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3628 {
3629 rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3630
3631 if (GET_CODE (in1) == NOT)
3632 in1 = XEXP (in1, 0);
3633 else
3634 in1 = gen_rtx_combine (NOT, GET_MODE (in1), in1);
3635
3636 if (GET_CODE (in2) == NOT)
3637 in2 = XEXP (in2, 0);
3638 else if (GET_CODE (in2) == CONST_INT
3639 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3640 in2 = GEN_INT (GET_MODE_MASK (mode) & ~ INTVAL (in2));
3641 else
3642 in2 = gen_rtx_combine (NOT, GET_MODE (in2), in2);
3643
3644 if (GET_CODE (in2) == NOT)
3645 {
3646 rtx tem = in2;
3647 in2 = in1; in1 = tem;
3648 }
3649
3650 return gen_rtx_combine (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3651 mode, in1, in2);
3652 }
3653 break;
3654
3655 case NEG:
3656 /* (neg (plus X 1)) can become (not X). */
3657 if (GET_CODE (XEXP (x, 0)) == PLUS
3658 && XEXP (XEXP (x, 0), 1) == const1_rtx)
3659 return gen_rtx_combine (NOT, mode, XEXP (XEXP (x, 0), 0));
3660
3661 /* Similarly, (neg (not X)) is (plus X 1). */
3662 if (GET_CODE (XEXP (x, 0)) == NOT)
3663 return plus_constant (XEXP (XEXP (x, 0), 0), 1);
3664
3665 /* (neg (minus X Y)) can become (minus Y X). */
3666 if (GET_CODE (XEXP (x, 0)) == MINUS
3667 && (! FLOAT_MODE_P (mode)
3668 /* x-y != -(y-x) with IEEE floating point. */
3669 || TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3670 || flag_fast_math))
3671 return gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
3672 XEXP (XEXP (x, 0), 0));
3673
3674 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
3675 if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
3676 && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
3677 return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3678
3679 /* NEG commutes with ASHIFT since it is multiplication. Only do this
3680 if we can then eliminate the NEG (e.g.,
3681 if the operand is a constant). */
3682
3683 if (GET_CODE (XEXP (x, 0)) == ASHIFT)
3684 {
3685 temp = simplify_unary_operation (NEG, mode,
3686 XEXP (XEXP (x, 0), 0), mode);
3687 if (temp)
3688 {
3689 SUBST (XEXP (XEXP (x, 0), 0), temp);
3690 return XEXP (x, 0);
3691 }
3692 }
3693
3694 temp = expand_compound_operation (XEXP (x, 0));
3695
3696 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3697 replaced by (lshiftrt X C). This will convert
3698 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
3699
3700 if (GET_CODE (temp) == ASHIFTRT
3701 && GET_CODE (XEXP (temp, 1)) == CONST_INT
3702 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
3703 return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
3704 INTVAL (XEXP (temp, 1)));
3705
3706 /* If X has only a single bit that might be nonzero, say, bit I, convert
3707 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
3708 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
3709 (sign_extract X 1 Y). But only do this if TEMP isn't a register
3710 or a SUBREG of one since we'd be making the expression more
3711 complex if it was just a register. */
3712
3713 if (GET_CODE (temp) != REG
3714 && ! (GET_CODE (temp) == SUBREG
3715 && GET_CODE (SUBREG_REG (temp)) == REG)
3716 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
3717 {
3718 rtx temp1 = simplify_shift_const
3719 (NULL_RTX, ASHIFTRT, mode,
3720 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
3721 GET_MODE_BITSIZE (mode) - 1 - i),
3722 GET_MODE_BITSIZE (mode) - 1 - i);
3723
3724 /* If all we did was surround TEMP with the two shifts, we
3725 haven't improved anything, so don't use it. Otherwise,
3726 we are better off with TEMP1. */
3727 if (GET_CODE (temp1) != ASHIFTRT
3728 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
3729 || XEXP (XEXP (temp1, 0), 0) != temp)
3730 return temp1;
3731 }
3732 break;
3733
3734 case TRUNCATE:
3735 /* We can't handle truncation to a partial integer mode here
3736 because we don't know the real bitsize of the partial
3737 integer mode. */
3738 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
3739 break;
3740
3741 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3742 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3743 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
3744 SUBST (XEXP (x, 0),
3745 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
3746 GET_MODE_MASK (mode), NULL_RTX, 0));
3747
3748 /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI. */
3749 if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
3750 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
3751 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3752 return XEXP (XEXP (x, 0), 0);
3753
3754 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
3755 (OP:SI foo:SI) if OP is NEG or ABS. */
3756 if ((GET_CODE (XEXP (x, 0)) == ABS
3757 || GET_CODE (XEXP (x, 0)) == NEG)
3758 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
3759 || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
3760 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3761 return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3762 XEXP (XEXP (XEXP (x, 0), 0), 0));
3763
3764 /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
3765 (truncate:SI x). */
3766 if (GET_CODE (XEXP (x, 0)) == SUBREG
3767 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
3768 && subreg_lowpart_p (XEXP (x, 0)))
3769 return SUBREG_REG (XEXP (x, 0));
3770
3771 /* If we know that the value is already truncated, we can
3772 replace the TRUNCATE with a SUBREG. */
3773 if (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) <= HOST_BITS_PER_WIDE_INT
3774 && (nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
3775 &~ GET_MODE_MASK (mode)) == 0)
3776 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3777
3778 /* A truncate of a comparison can be replaced with a subreg if
3779 STORE_FLAG_VALUE permits. This is like the previous test,
3780 but it works even if the comparison is done in a mode larger
3781 than HOST_BITS_PER_WIDE_INT. */
3782 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3783 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3784 && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0)
3785 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3786
3787 /* Similarly, a truncate of a register whose value is a
3788 comparison can be replaced with a subreg if STORE_FLAG_VALUE
3789 permits. */
3790 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3791 && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0
3792 && (temp = get_last_value (XEXP (x, 0)))
3793 && GET_RTX_CLASS (GET_CODE (temp)) == '<')
3794 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3795
3796 break;
3797
3798 case FLOAT_TRUNCATE:
3799 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
3800 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
3801 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3802 return XEXP (XEXP (x, 0), 0);
3803
3804 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
3805 (OP:SF foo:SF) if OP is NEG or ABS. */
3806 if ((GET_CODE (XEXP (x, 0)) == ABS
3807 || GET_CODE (XEXP (x, 0)) == NEG)
3808 && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
3809 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3810 return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3811 XEXP (XEXP (XEXP (x, 0), 0), 0));
3812
3813 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
3814 is (float_truncate:SF x). */
3815 if (GET_CODE (XEXP (x, 0)) == SUBREG
3816 && subreg_lowpart_p (XEXP (x, 0))
3817 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
3818 return SUBREG_REG (XEXP (x, 0));
3819 break;
3820
3821 #ifdef HAVE_cc0
3822 case COMPARE:
3823 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
3824 using cc0, in which case we want to leave it as a COMPARE
3825 so we can distinguish it from a register-register-copy. */
3826 if (XEXP (x, 1) == const0_rtx)
3827 return XEXP (x, 0);
3828
3829 /* In IEEE floating point, x-0 is not the same as x. */
3830 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3831 || ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0)))
3832 || flag_fast_math)
3833 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
3834 return XEXP (x, 0);
3835 break;
3836 #endif
3837
3838 case CONST:
3839 /* (const (const X)) can become (const X). Do it this way rather than
3840 returning the inner CONST since CONST can be shared with a
3841 REG_EQUAL note. */
3842 if (GET_CODE (XEXP (x, 0)) == CONST)
3843 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
3844 break;
3845
3846 #ifdef HAVE_lo_sum
3847 case LO_SUM:
3848 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
3849 can add in an offset. find_split_point will split this address up
3850 again if it doesn't match. */
3851 if (GET_CODE (XEXP (x, 0)) == HIGH
3852 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
3853 return XEXP (x, 1);
3854 break;
3855 #endif
3856
3857 case PLUS:
3858 /* If we have (plus (plus (A const) B)), associate it so that CONST is
3859 outermost. That's because that's the way indexed addresses are
3860 supposed to appear. This code used to check many more cases, but
3861 they are now checked elsewhere. */
3862 if (GET_CODE (XEXP (x, 0)) == PLUS
3863 && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
3864 return gen_binary (PLUS, mode,
3865 gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
3866 XEXP (x, 1)),
3867 XEXP (XEXP (x, 0), 1));
3868
3869 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
3870 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
3871 bit-field and can be replaced by either a sign_extend or a
3872 sign_extract. The `and' may be a zero_extend. */
3873 if (GET_CODE (XEXP (x, 0)) == XOR
3874 && GET_CODE (XEXP (x, 1)) == CONST_INT
3875 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3876 && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (XEXP (x, 0), 1))
3877 && (i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
3878 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3879 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
3880 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3881 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
3882 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
3883 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
3884 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
3885 == i + 1))))
3886 return simplify_shift_const
3887 (NULL_RTX, ASHIFTRT, mode,
3888 simplify_shift_const (NULL_RTX, ASHIFT, mode,
3889 XEXP (XEXP (XEXP (x, 0), 0), 0),
3890 GET_MODE_BITSIZE (mode) - (i + 1)),
3891 GET_MODE_BITSIZE (mode) - (i + 1));
3892
3893 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
3894 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
3895 is 1. This produces better code than the alternative immediately
3896 below. */
3897 if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3898 && reversible_comparison_p (XEXP (x, 0))
3899 && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
3900 || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx)))
3901 return
3902 gen_unary (NEG, mode, mode,
3903 gen_binary (reverse_condition (GET_CODE (XEXP (x, 0))),
3904 mode, XEXP (XEXP (x, 0), 0),
3905 XEXP (XEXP (x, 0), 1)));
3906
3907 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
3908 can become (ashiftrt (ashift (xor x 1) C) C) where C is
3909 the bitsize of the mode - 1. This allows simplification of
3910 "a = (b & 8) == 0;" */
3911 if (XEXP (x, 1) == constm1_rtx
3912 && GET_CODE (XEXP (x, 0)) != REG
3913 && ! (GET_CODE (XEXP (x,0)) == SUBREG
3914 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
3915 && nonzero_bits (XEXP (x, 0), mode) == 1)
3916 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
3917 simplify_shift_const (NULL_RTX, ASHIFT, mode,
3918 gen_rtx_combine (XOR, mode,
3919 XEXP (x, 0), const1_rtx),
3920 GET_MODE_BITSIZE (mode) - 1),
3921 GET_MODE_BITSIZE (mode) - 1);
3922
3923 /* If we are adding two things that have no bits in common, convert
3924 the addition into an IOR. This will often be further simplified,
3925 for example in cases like ((a & 1) + (a & 2)), which can
3926 become a & 3. */
3927
3928 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3929 && (nonzero_bits (XEXP (x, 0), mode)
3930 & nonzero_bits (XEXP (x, 1), mode)) == 0)
3931 return gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
3932 break;
3933
3934 case MINUS:
3935 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
3936 by reversing the comparison code if valid. */
3937 if (STORE_FLAG_VALUE == 1
3938 && XEXP (x, 0) == const1_rtx
3939 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
3940 && reversible_comparison_p (XEXP (x, 1)))
3941 return gen_binary (reverse_condition (GET_CODE (XEXP (x, 1))),
3942 mode, XEXP (XEXP (x, 1), 0),
3943 XEXP (XEXP (x, 1), 1));
3944
3945 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
3946 (and <foo> (const_int pow2-1)) */
3947 if (GET_CODE (XEXP (x, 1)) == AND
3948 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3949 && exact_log2 (- INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
3950 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3951 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
3952 - INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
3953
3954 /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
3955 integers. */
3956 if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
3957 return gen_binary (MINUS, mode,
3958 gen_binary (MINUS, mode, XEXP (x, 0),
3959 XEXP (XEXP (x, 1), 0)),
3960 XEXP (XEXP (x, 1), 1));
3961 break;
3962
3963 case MULT:
3964 /* If we have (mult (plus A B) C), apply the distributive law and then
3965 the inverse distributive law to see if things simplify. This
3966 occurs mostly in addresses, often when unrolling loops. */
3967
3968 if (GET_CODE (XEXP (x, 0)) == PLUS)
3969 {
3970 x = apply_distributive_law
3971 (gen_binary (PLUS, mode,
3972 gen_binary (MULT, mode,
3973 XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
3974 gen_binary (MULT, mode,
3975 XEXP (XEXP (x, 0), 1), XEXP (x, 1))));
3976
3977 if (GET_CODE (x) != MULT)
3978 return x;
3979 }
3980 break;
3981
3982 case UDIV:
3983 /* If this is a divide by a power of two, treat it as a shift if
3984 its first operand is a shift. */
3985 if (GET_CODE (XEXP (x, 1)) == CONST_INT
3986 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
3987 && (GET_CODE (XEXP (x, 0)) == ASHIFT
3988 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
3989 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
3990 || GET_CODE (XEXP (x, 0)) == ROTATE
3991 || GET_CODE (XEXP (x, 0)) == ROTATERT))
3992 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
3993 break;
3994
3995 case EQ: case NE:
3996 case GT: case GTU: case GE: case GEU:
3997 case LT: case LTU: case LE: case LEU:
3998 /* If the first operand is a condition code, we can't do anything
3999 with it. */
4000 if (GET_CODE (XEXP (x, 0)) == COMPARE
4001 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4002 #ifdef HAVE_cc0
4003 && XEXP (x, 0) != cc0_rtx
4004 #endif
4005 ))
4006 {
4007 rtx op0 = XEXP (x, 0);
4008 rtx op1 = XEXP (x, 1);
4009 enum rtx_code new_code;
4010
4011 if (GET_CODE (op0) == COMPARE)
4012 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4013
4014 /* Simplify our comparison, if possible. */
4015 new_code = simplify_comparison (code, &op0, &op1);
4016
4017 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4018 if only the low-order bit is possibly nonzero in X (such as when
4019 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
4020 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
4021 known to be either 0 or -1, NE becomes a NEG and EQ becomes
4022 (plus X 1).
4023
4024 Remove any ZERO_EXTRACT we made when thinking this was a
4025 comparison. It may now be simpler to use, e.g., an AND. If a
4026 ZERO_EXTRACT is indeed appropriate, it will be placed back by
4027 the call to make_compound_operation in the SET case. */
4028
4029 if (STORE_FLAG_VALUE == 1
4030 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4031 && op1 == const0_rtx && nonzero_bits (op0, mode) == 1)
4032 return gen_lowpart_for_combine (mode,
4033 expand_compound_operation (op0));
4034
4035 else if (STORE_FLAG_VALUE == 1
4036 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4037 && op1 == const0_rtx
4038 && (num_sign_bit_copies (op0, mode)
4039 == GET_MODE_BITSIZE (mode)))
4040 {
4041 op0 = expand_compound_operation (op0);
4042 return gen_unary (NEG, mode, mode,
4043 gen_lowpart_for_combine (mode, op0));
4044 }
4045
4046 else if (STORE_FLAG_VALUE == 1
4047 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4048 && op1 == const0_rtx
4049 && nonzero_bits (op0, mode) == 1)
4050 {
4051 op0 = expand_compound_operation (op0);
4052 return gen_binary (XOR, mode,
4053 gen_lowpart_for_combine (mode, op0),
4054 const1_rtx);
4055 }
4056
4057 else if (STORE_FLAG_VALUE == 1
4058 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4059 && op1 == const0_rtx
4060 && (num_sign_bit_copies (op0, mode)
4061 == GET_MODE_BITSIZE (mode)))
4062 {
4063 op0 = expand_compound_operation (op0);
4064 return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
4065 }
4066
4067 /* If STORE_FLAG_VALUE is -1, we have cases similar to
4068 those above. */
4069 if (STORE_FLAG_VALUE == -1
4070 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4071 && op1 == const0_rtx
4072 && (num_sign_bit_copies (op0, mode)
4073 == GET_MODE_BITSIZE (mode)))
4074 return gen_lowpart_for_combine (mode,
4075 expand_compound_operation (op0));
4076
4077 else if (STORE_FLAG_VALUE == -1
4078 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4079 && op1 == const0_rtx
4080 && nonzero_bits (op0, mode) == 1)
4081 {
4082 op0 = expand_compound_operation (op0);
4083 return gen_unary (NEG, mode, mode,
4084 gen_lowpart_for_combine (mode, op0));
4085 }
4086
4087 else if (STORE_FLAG_VALUE == -1
4088 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4089 && op1 == const0_rtx
4090 && (num_sign_bit_copies (op0, mode)
4091 == GET_MODE_BITSIZE (mode)))
4092 {
4093 op0 = expand_compound_operation (op0);
4094 return gen_unary (NOT, mode, mode,
4095 gen_lowpart_for_combine (mode, op0));
4096 }
4097
4098 /* If X is 0/1, (eq X 0) is X-1. */
4099 else if (STORE_FLAG_VALUE == -1
4100 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4101 && op1 == const0_rtx
4102 && nonzero_bits (op0, mode) == 1)
4103 {
4104 op0 = expand_compound_operation (op0);
4105 return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
4106 }
4107
4108 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4109 one bit that might be nonzero, we can convert (ne x 0) to
4110 (ashift x c) where C puts the bit in the sign bit. Remove any
4111 AND with STORE_FLAG_VALUE when we are done, since we are only
4112 going to test the sign bit. */
4113 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4114 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4115 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4116 == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4117 && op1 == const0_rtx
4118 && mode == GET_MODE (op0)
4119 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4120 {
4121 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4122 expand_compound_operation (op0),
4123 GET_MODE_BITSIZE (mode) - 1 - i);
4124 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4125 return XEXP (x, 0);
4126 else
4127 return x;
4128 }
4129
4130 /* If the code changed, return a whole new comparison. */
4131 if (new_code != code)
4132 return gen_rtx_combine (new_code, mode, op0, op1);
4133
4134 /* Otherwise, keep this operation, but maybe change its operands.
4135 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
4136 SUBST (XEXP (x, 0), op0);
4137 SUBST (XEXP (x, 1), op1);
4138 }
4139 break;
4140
4141 case IF_THEN_ELSE:
4142 return simplify_if_then_else (x);
4143
4144 case ZERO_EXTRACT:
4145 case SIGN_EXTRACT:
4146 case ZERO_EXTEND:
4147 case SIGN_EXTEND:
4148 /* If we are processing SET_DEST, we are done. */
4149 if (in_dest)
4150 return x;
4151
4152 return expand_compound_operation (x);
4153
4154 case SET:
4155 return simplify_set (x);
4156
4157 case AND:
4158 case IOR:
4159 case XOR:
4160 return simplify_logical (x, last);
4161
4162 case ABS:
4163 /* (abs (neg <foo>)) -> (abs <foo>) */
4164 if (GET_CODE (XEXP (x, 0)) == NEG)
4165 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4166
4167 /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4168 do nothing. */
4169 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4170 break;
4171
4172 /* If operand is something known to be positive, ignore the ABS. */
4173 if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4174 || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4175 <= HOST_BITS_PER_WIDE_INT)
4176 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4177 & ((HOST_WIDE_INT) 1
4178 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4179 == 0)))
4180 return XEXP (x, 0);
4181
4182
4183 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
4184 if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4185 return gen_rtx_combine (NEG, mode, XEXP (x, 0));
4186
4187 break;
4188
4189 case FFS:
4190 /* (ffs (*_extend <X>)) = (ffs <X>) */
4191 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4192 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4193 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4194 break;
4195
4196 case FLOAT:
4197 /* (float (sign_extend <X>)) = (float <X>). */
4198 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4199 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4200 break;
4201
4202 case ASHIFT:
4203 case LSHIFTRT:
4204 case ASHIFTRT:
4205 case ROTATE:
4206 case ROTATERT:
4207 /* If this is a shift by a constant amount, simplify it. */
4208 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4209 return simplify_shift_const (x, code, mode, XEXP (x, 0),
4210 INTVAL (XEXP (x, 1)));
4211
4212 #ifdef SHIFT_COUNT_TRUNCATED
4213 else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4214 SUBST (XEXP (x, 1),
4215 force_to_mode (XEXP (x, 1), GET_MODE (x),
4216 ((HOST_WIDE_INT) 1
4217 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4218 - 1,
4219 NULL_RTX, 0));
4220 #endif
4221
4222 break;
4223
4224 default:
4225 break;
4226 }
4227
4228 return x;
4229 }
4230 \f
4231 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
4232
4233 static rtx
4234 simplify_if_then_else (x)
4235 rtx x;
4236 {
4237 enum machine_mode mode = GET_MODE (x);
4238 rtx cond = XEXP (x, 0);
4239 rtx true = XEXP (x, 1);
4240 rtx false = XEXP (x, 2);
4241 enum rtx_code true_code = GET_CODE (cond);
4242 int comparison_p = GET_RTX_CLASS (true_code) == '<';
4243 rtx temp;
4244 int i;
4245
4246 /* Simplify storing of the truth value. */
4247 if (comparison_p && true == const_true_rtx && false == const0_rtx)
4248 return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4249
4250 /* Also when the truth value has to be reversed. */
4251 if (comparison_p && reversible_comparison_p (cond)
4252 && true == const0_rtx && false == const_true_rtx)
4253 return gen_binary (reverse_condition (true_code),
4254 mode, XEXP (cond, 0), XEXP (cond, 1));
4255
4256 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4257 in it is being compared against certain values. Get the true and false
4258 comparisons and see if that says anything about the value of each arm. */
4259
4260 if (comparison_p && reversible_comparison_p (cond)
4261 && GET_CODE (XEXP (cond, 0)) == REG)
4262 {
4263 HOST_WIDE_INT nzb;
4264 rtx from = XEXP (cond, 0);
4265 enum rtx_code false_code = reverse_condition (true_code);
4266 rtx true_val = XEXP (cond, 1);
4267 rtx false_val = true_val;
4268 int swapped = 0;
4269
4270 /* If FALSE_CODE is EQ, swap the codes and arms. */
4271
4272 if (false_code == EQ)
4273 {
4274 swapped = 1, true_code = EQ, false_code = NE;
4275 temp = true, true = false, false = temp;
4276 }
4277
4278 /* If we are comparing against zero and the expression being tested has
4279 only a single bit that might be nonzero, that is its value when it is
4280 not equal to zero. Similarly if it is known to be -1 or 0. */
4281
4282 if (true_code == EQ && true_val == const0_rtx
4283 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4284 false_code = EQ, false_val = GEN_INT (nzb);
4285 else if (true_code == EQ && true_val == const0_rtx
4286 && (num_sign_bit_copies (from, GET_MODE (from))
4287 == GET_MODE_BITSIZE (GET_MODE (from))))
4288 false_code = EQ, false_val = constm1_rtx;
4289
4290 /* Now simplify an arm if we know the value of the register in the
4291 branch and it is used in the arm. Be careful due to the potential
4292 of locally-shared RTL. */
4293
4294 if (reg_mentioned_p (from, true))
4295 true = subst (known_cond (copy_rtx (true), true_code, from, true_val),
4296 pc_rtx, pc_rtx, 0, 0);
4297 if (reg_mentioned_p (from, false))
4298 false = subst (known_cond (copy_rtx (false), false_code,
4299 from, false_val),
4300 pc_rtx, pc_rtx, 0, 0);
4301
4302 SUBST (XEXP (x, 1), swapped ? false : true);
4303 SUBST (XEXP (x, 2), swapped ? true : false);
4304
4305 true = XEXP (x, 1), false = XEXP (x, 2), true_code = GET_CODE (cond);
4306 }
4307
4308 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4309 reversed, do so to avoid needing two sets of patterns for
4310 subtract-and-branch insns. Similarly if we have a constant in the true
4311 arm, the false arm is the same as the first operand of the comparison, or
4312 the false arm is more complicated than the true arm. */
4313
4314 if (comparison_p && reversible_comparison_p (cond)
4315 && (true == pc_rtx
4316 || (CONSTANT_P (true)
4317 && GET_CODE (false) != CONST_INT && false != pc_rtx)
4318 || true == const0_rtx
4319 || (GET_RTX_CLASS (GET_CODE (true)) == 'o'
4320 && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4321 || (GET_CODE (true) == SUBREG
4322 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true))) == 'o'
4323 && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4324 || reg_mentioned_p (true, false)
4325 || rtx_equal_p (false, XEXP (cond, 0))))
4326 {
4327 true_code = reverse_condition (true_code);
4328 SUBST (XEXP (x, 0),
4329 gen_binary (true_code, GET_MODE (cond), XEXP (cond, 0),
4330 XEXP (cond, 1)));
4331
4332 SUBST (XEXP (x, 1), false);
4333 SUBST (XEXP (x, 2), true);
4334
4335 temp = true, true = false, false = temp, cond = XEXP (x, 0);
4336
4337 /* It is possible that the conditional has been simplified out. */
4338 true_code = GET_CODE (cond);
4339 comparison_p = GET_RTX_CLASS (true_code) == '<';
4340 }
4341
4342 /* If the two arms are identical, we don't need the comparison. */
4343
4344 if (rtx_equal_p (true, false) && ! side_effects_p (cond))
4345 return true;
4346
4347 /* Convert a == b ? b : a to "a". */
4348 if (true_code == EQ && ! side_effects_p (cond)
4349 && rtx_equal_p (XEXP (cond, 0), false)
4350 && rtx_equal_p (XEXP (cond, 1), true))
4351 return false;
4352 else if (true_code == NE && ! side_effects_p (cond)
4353 && rtx_equal_p (XEXP (cond, 0), true)
4354 && rtx_equal_p (XEXP (cond, 1), false))
4355 return true;
4356
4357 /* Look for cases where we have (abs x) or (neg (abs X)). */
4358
4359 if (GET_MODE_CLASS (mode) == MODE_INT
4360 && GET_CODE (false) == NEG
4361 && rtx_equal_p (true, XEXP (false, 0))
4362 && comparison_p
4363 && rtx_equal_p (true, XEXP (cond, 0))
4364 && ! side_effects_p (true))
4365 switch (true_code)
4366 {
4367 case GT:
4368 case GE:
4369 return gen_unary (ABS, mode, mode, true);
4370 case LT:
4371 case LE:
4372 return gen_unary (NEG, mode, mode, gen_unary (ABS, mode, mode, true));
4373 default:
4374 break;
4375 }
4376
4377 /* Look for MIN or MAX. */
4378
4379 if ((! FLOAT_MODE_P (mode) || flag_fast_math)
4380 && comparison_p
4381 && rtx_equal_p (XEXP (cond, 0), true)
4382 && rtx_equal_p (XEXP (cond, 1), false)
4383 && ! side_effects_p (cond))
4384 switch (true_code)
4385 {
4386 case GE:
4387 case GT:
4388 return gen_binary (SMAX, mode, true, false);
4389 case LE:
4390 case LT:
4391 return gen_binary (SMIN, mode, true, false);
4392 case GEU:
4393 case GTU:
4394 return gen_binary (UMAX, mode, true, false);
4395 case LEU:
4396 case LTU:
4397 return gen_binary (UMIN, mode, true, false);
4398 default:
4399 break;
4400 }
4401
4402 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4403 second operand is zero, this can be done as (OP Z (mult COND C2)) where
4404 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4405 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4406 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4407 neither 1 or -1, but it isn't worth checking for. */
4408
4409 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4410 && comparison_p && mode != VOIDmode && ! side_effects_p (x))
4411 {
4412 rtx t = make_compound_operation (true, SET);
4413 rtx f = make_compound_operation (false, SET);
4414 rtx cond_op0 = XEXP (cond, 0);
4415 rtx cond_op1 = XEXP (cond, 1);
4416 enum rtx_code op, extend_op = NIL;
4417 enum machine_mode m = mode;
4418 rtx z = 0, c1;
4419
4420 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4421 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4422 || GET_CODE (t) == ASHIFT
4423 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4424 && rtx_equal_p (XEXP (t, 0), f))
4425 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4426
4427 /* If an identity-zero op is commutative, check whether there
4428 would be a match if we swapped the operands. */
4429 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4430 || GET_CODE (t) == XOR)
4431 && rtx_equal_p (XEXP (t, 1), f))
4432 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4433 else if (GET_CODE (t) == SIGN_EXTEND
4434 && (GET_CODE (XEXP (t, 0)) == PLUS
4435 || GET_CODE (XEXP (t, 0)) == MINUS
4436 || GET_CODE (XEXP (t, 0)) == IOR
4437 || GET_CODE (XEXP (t, 0)) == XOR
4438 || GET_CODE (XEXP (t, 0)) == ASHIFT
4439 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4440 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4441 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4442 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4443 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4444 && (num_sign_bit_copies (f, GET_MODE (f))
4445 > (GET_MODE_BITSIZE (mode)
4446 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4447 {
4448 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4449 extend_op = SIGN_EXTEND;
4450 m = GET_MODE (XEXP (t, 0));
4451 }
4452 else if (GET_CODE (t) == SIGN_EXTEND
4453 && (GET_CODE (XEXP (t, 0)) == PLUS
4454 || GET_CODE (XEXP (t, 0)) == IOR
4455 || GET_CODE (XEXP (t, 0)) == XOR)
4456 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4457 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4458 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4459 && (num_sign_bit_copies (f, GET_MODE (f))
4460 > (GET_MODE_BITSIZE (mode)
4461 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4462 {
4463 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4464 extend_op = SIGN_EXTEND;
4465 m = GET_MODE (XEXP (t, 0));
4466 }
4467 else if (GET_CODE (t) == ZERO_EXTEND
4468 && (GET_CODE (XEXP (t, 0)) == PLUS
4469 || GET_CODE (XEXP (t, 0)) == MINUS
4470 || GET_CODE (XEXP (t, 0)) == IOR
4471 || GET_CODE (XEXP (t, 0)) == XOR
4472 || GET_CODE (XEXP (t, 0)) == ASHIFT
4473 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4474 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4475 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4476 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4477 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4478 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4479 && ((nonzero_bits (f, GET_MODE (f))
4480 & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4481 == 0))
4482 {
4483 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4484 extend_op = ZERO_EXTEND;
4485 m = GET_MODE (XEXP (t, 0));
4486 }
4487 else if (GET_CODE (t) == ZERO_EXTEND
4488 && (GET_CODE (XEXP (t, 0)) == PLUS
4489 || GET_CODE (XEXP (t, 0)) == IOR
4490 || GET_CODE (XEXP (t, 0)) == XOR)
4491 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4492 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4493 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4494 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4495 && ((nonzero_bits (f, GET_MODE (f))
4496 & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4497 == 0))
4498 {
4499 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4500 extend_op = ZERO_EXTEND;
4501 m = GET_MODE (XEXP (t, 0));
4502 }
4503
4504 if (z)
4505 {
4506 temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
4507 pc_rtx, pc_rtx, 0, 0);
4508 temp = gen_binary (MULT, m, temp,
4509 gen_binary (MULT, m, c1, const_true_rtx));
4510 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4511 temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
4512
4513 if (extend_op != NIL)
4514 temp = gen_unary (extend_op, mode, m, temp);
4515
4516 return temp;
4517 }
4518 }
4519
4520 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4521 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4522 negation of a single bit, we can convert this operation to a shift. We
4523 can actually do this more generally, but it doesn't seem worth it. */
4524
4525 if (true_code == NE && XEXP (cond, 1) == const0_rtx
4526 && false == const0_rtx && GET_CODE (true) == CONST_INT
4527 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4528 && (i = exact_log2 (INTVAL (true))) >= 0)
4529 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4530 == GET_MODE_BITSIZE (mode))
4531 && (i = exact_log2 (- INTVAL (true))) >= 0)))
4532 return
4533 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4534 gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
4535
4536 return x;
4537 }
4538 \f
4539 /* Simplify X, a SET expression. Return the new expression. */
4540
4541 static rtx
4542 simplify_set (x)
4543 rtx x;
4544 {
4545 rtx src = SET_SRC (x);
4546 rtx dest = SET_DEST (x);
4547 enum machine_mode mode
4548 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4549 rtx other_insn;
4550 rtx *cc_use;
4551
4552 /* (set (pc) (return)) gets written as (return). */
4553 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4554 return src;
4555
4556 /* Now that we know for sure which bits of SRC we are using, see if we can
4557 simplify the expression for the object knowing that we only need the
4558 low-order bits. */
4559
4560 if (GET_MODE_CLASS (mode) == MODE_INT)
4561 src = force_to_mode (src, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
4562
4563 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
4564 the comparison result and try to simplify it unless we already have used
4565 undobuf.other_insn. */
4566 if ((GET_CODE (src) == COMPARE
4567 #ifdef HAVE_cc0
4568 || dest == cc0_rtx
4569 #endif
4570 )
4571 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
4572 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
4573 && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
4574 && rtx_equal_p (XEXP (*cc_use, 0), dest))
4575 {
4576 enum rtx_code old_code = GET_CODE (*cc_use);
4577 enum rtx_code new_code;
4578 rtx op0, op1;
4579 int other_changed = 0;
4580 enum machine_mode compare_mode = GET_MODE (dest);
4581
4582 if (GET_CODE (src) == COMPARE)
4583 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
4584 else
4585 op0 = src, op1 = const0_rtx;
4586
4587 /* Simplify our comparison, if possible. */
4588 new_code = simplify_comparison (old_code, &op0, &op1);
4589
4590 #ifdef EXTRA_CC_MODES
4591 /* If this machine has CC modes other than CCmode, check to see if we
4592 need to use a different CC mode here. */
4593 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
4594 #endif /* EXTRA_CC_MODES */
4595
4596 #if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
4597 /* If the mode changed, we have to change SET_DEST, the mode in the
4598 compare, and the mode in the place SET_DEST is used. If SET_DEST is
4599 a hard register, just build new versions with the proper mode. If it
4600 is a pseudo, we lose unless it is only time we set the pseudo, in
4601 which case we can safely change its mode. */
4602 if (compare_mode != GET_MODE (dest))
4603 {
4604 int regno = REGNO (dest);
4605 rtx new_dest = gen_rtx_REG (compare_mode, regno);
4606
4607 if (regno < FIRST_PSEUDO_REGISTER
4608 || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
4609 {
4610 if (regno >= FIRST_PSEUDO_REGISTER)
4611 SUBST (regno_reg_rtx[regno], new_dest);
4612
4613 SUBST (SET_DEST (x), new_dest);
4614 SUBST (XEXP (*cc_use, 0), new_dest);
4615 other_changed = 1;
4616
4617 dest = new_dest;
4618 }
4619 }
4620 #endif
4621
4622 /* If the code changed, we have to build a new comparison in
4623 undobuf.other_insn. */
4624 if (new_code != old_code)
4625 {
4626 unsigned HOST_WIDE_INT mask;
4627
4628 SUBST (*cc_use, gen_rtx_combine (new_code, GET_MODE (*cc_use),
4629 dest, const0_rtx));
4630
4631 /* If the only change we made was to change an EQ into an NE or
4632 vice versa, OP0 has only one bit that might be nonzero, and OP1
4633 is zero, check if changing the user of the condition code will
4634 produce a valid insn. If it won't, we can keep the original code
4635 in that insn by surrounding our operation with an XOR. */
4636
4637 if (((old_code == NE && new_code == EQ)
4638 || (old_code == EQ && new_code == NE))
4639 && ! other_changed && op1 == const0_rtx
4640 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
4641 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
4642 {
4643 rtx pat = PATTERN (other_insn), note = 0;
4644 int scratches;
4645
4646 if ((recog_for_combine (&pat, other_insn, &note, &scratches) < 0
4647 && ! check_asm_operands (pat)))
4648 {
4649 PUT_CODE (*cc_use, old_code);
4650 other_insn = 0;
4651
4652 op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
4653 }
4654 }
4655
4656 other_changed = 1;
4657 }
4658
4659 if (other_changed)
4660 undobuf.other_insn = other_insn;
4661
4662 #ifdef HAVE_cc0
4663 /* If we are now comparing against zero, change our source if
4664 needed. If we do not use cc0, we always have a COMPARE. */
4665 if (op1 == const0_rtx && dest == cc0_rtx)
4666 {
4667 SUBST (SET_SRC (x), op0);
4668 src = op0;
4669 }
4670 else
4671 #endif
4672
4673 /* Otherwise, if we didn't previously have a COMPARE in the
4674 correct mode, we need one. */
4675 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
4676 {
4677 SUBST (SET_SRC (x),
4678 gen_rtx_combine (COMPARE, compare_mode, op0, op1));
4679 src = SET_SRC (x);
4680 }
4681 else
4682 {
4683 /* Otherwise, update the COMPARE if needed. */
4684 SUBST (XEXP (src, 0), op0);
4685 SUBST (XEXP (src, 1), op1);
4686 }
4687 }
4688 else
4689 {
4690 /* Get SET_SRC in a form where we have placed back any
4691 compound expressions. Then do the checks below. */
4692 src = make_compound_operation (src, SET);
4693 SUBST (SET_SRC (x), src);
4694 }
4695
4696 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
4697 and X being a REG or (subreg (reg)), we may be able to convert this to
4698 (set (subreg:m2 x) (op)).
4699
4700 We can always do this if M1 is narrower than M2 because that means that
4701 we only care about the low bits of the result.
4702
4703 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
4704 perform a narrower operation that requested since the high-order bits will
4705 be undefined. On machine where it is defined, this transformation is safe
4706 as long as M1 and M2 have the same number of words. */
4707
4708 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4709 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
4710 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
4711 / UNITS_PER_WORD)
4712 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
4713 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
4714 #ifndef WORD_REGISTER_OPERATIONS
4715 && (GET_MODE_SIZE (GET_MODE (src))
4716 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4717 #endif
4718 #ifdef CLASS_CANNOT_CHANGE_SIZE
4719 && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
4720 && (TEST_HARD_REG_BIT
4721 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
4722 REGNO (dest)))
4723 && (GET_MODE_SIZE (GET_MODE (src))
4724 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
4725 #endif
4726 && (GET_CODE (dest) == REG
4727 || (GET_CODE (dest) == SUBREG
4728 && GET_CODE (SUBREG_REG (dest)) == REG)))
4729 {
4730 SUBST (SET_DEST (x),
4731 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
4732 dest));
4733 SUBST (SET_SRC (x), SUBREG_REG (src));
4734
4735 src = SET_SRC (x), dest = SET_DEST (x);
4736 }
4737
4738 #ifdef LOAD_EXTEND_OP
4739 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
4740 would require a paradoxical subreg. Replace the subreg with a
4741 zero_extend to avoid the reload that would otherwise be required. */
4742
4743 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4744 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
4745 && SUBREG_WORD (src) == 0
4746 && (GET_MODE_SIZE (GET_MODE (src))
4747 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4748 && GET_CODE (SUBREG_REG (src)) == MEM)
4749 {
4750 SUBST (SET_SRC (x),
4751 gen_rtx_combine (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
4752 GET_MODE (src), XEXP (src, 0)));
4753
4754 src = SET_SRC (x);
4755 }
4756 #endif
4757
4758 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
4759 are comparing an item known to be 0 or -1 against 0, use a logical
4760 operation instead. Check for one of the arms being an IOR of the other
4761 arm with some value. We compute three terms to be IOR'ed together. In
4762 practice, at most two will be nonzero. Then we do the IOR's. */
4763
4764 if (GET_CODE (dest) != PC
4765 && GET_CODE (src) == IF_THEN_ELSE
4766 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
4767 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
4768 && XEXP (XEXP (src, 0), 1) == const0_rtx
4769 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
4770 #ifdef HAVE_conditional_move
4771 && ! can_conditionally_move_p (GET_MODE (src))
4772 #endif
4773 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
4774 GET_MODE (XEXP (XEXP (src, 0), 0)))
4775 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
4776 && ! side_effects_p (src))
4777 {
4778 rtx true = (GET_CODE (XEXP (src, 0)) == NE
4779 ? XEXP (src, 1) : XEXP (src, 2));
4780 rtx false = (GET_CODE (XEXP (src, 0)) == NE
4781 ? XEXP (src, 2) : XEXP (src, 1));
4782 rtx term1 = const0_rtx, term2, term3;
4783
4784 if (GET_CODE (true) == IOR && rtx_equal_p (XEXP (true, 0), false))
4785 term1 = false, true = XEXP (true, 1), false = const0_rtx;
4786 else if (GET_CODE (true) == IOR
4787 && rtx_equal_p (XEXP (true, 1), false))
4788 term1 = false, true = XEXP (true, 0), false = const0_rtx;
4789 else if (GET_CODE (false) == IOR
4790 && rtx_equal_p (XEXP (false, 0), true))
4791 term1 = true, false = XEXP (false, 1), true = const0_rtx;
4792 else if (GET_CODE (false) == IOR
4793 && rtx_equal_p (XEXP (false, 1), true))
4794 term1 = true, false = XEXP (false, 0), true = const0_rtx;
4795
4796 term2 = gen_binary (AND, GET_MODE (src), XEXP (XEXP (src, 0), 0), true);
4797 term3 = gen_binary (AND, GET_MODE (src),
4798 gen_unary (NOT, GET_MODE (src), GET_MODE (src),
4799 XEXP (XEXP (src, 0), 0)),
4800 false);
4801
4802 SUBST (SET_SRC (x),
4803 gen_binary (IOR, GET_MODE (src),
4804 gen_binary (IOR, GET_MODE (src), term1, term2),
4805 term3));
4806
4807 src = SET_SRC (x);
4808 }
4809
4810 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
4811 whole thing fail. */
4812 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
4813 return src;
4814 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
4815 return dest;
4816 else
4817 /* Convert this into a field assignment operation, if possible. */
4818 return make_field_assignment (x);
4819 }
4820 \f
4821 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
4822 result. LAST is nonzero if this is the last retry. */
4823
4824 static rtx
4825 simplify_logical (x, last)
4826 rtx x;
4827 int last;
4828 {
4829 enum machine_mode mode = GET_MODE (x);
4830 rtx op0 = XEXP (x, 0);
4831 rtx op1 = XEXP (x, 1);
4832
4833 switch (GET_CODE (x))
4834 {
4835 case AND:
4836 /* Convert (A ^ B) & A to A & (~ B) since the latter is often a single
4837 insn (and may simplify more). */
4838 if (GET_CODE (op0) == XOR
4839 && rtx_equal_p (XEXP (op0, 0), op1)
4840 && ! side_effects_p (op1))
4841 x = gen_binary (AND, mode,
4842 gen_unary (NOT, mode, mode, XEXP (op0, 1)), op1);
4843
4844 if (GET_CODE (op0) == XOR
4845 && rtx_equal_p (XEXP (op0, 1), op1)
4846 && ! side_effects_p (op1))
4847 x = gen_binary (AND, mode,
4848 gen_unary (NOT, mode, mode, XEXP (op0, 0)), op1);
4849
4850 /* Similarly for (~ (A ^ B)) & A. */
4851 if (GET_CODE (op0) == NOT
4852 && GET_CODE (XEXP (op0, 0)) == XOR
4853 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
4854 && ! side_effects_p (op1))
4855 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
4856
4857 if (GET_CODE (op0) == NOT
4858 && GET_CODE (XEXP (op0, 0)) == XOR
4859 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
4860 && ! side_effects_p (op1))
4861 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
4862
4863 if (GET_CODE (op1) == CONST_INT)
4864 {
4865 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
4866
4867 /* If we have (ior (and (X C1) C2)) and the next restart would be
4868 the last, simplify this by making C1 as small as possible
4869 and then exit. */
4870 if (last
4871 && GET_CODE (x) == IOR && GET_CODE (op0) == AND
4872 && GET_CODE (XEXP (op0, 1)) == CONST_INT
4873 && GET_CODE (op1) == CONST_INT)
4874 return gen_binary (IOR, mode,
4875 gen_binary (AND, mode, XEXP (op0, 0),
4876 GEN_INT (INTVAL (XEXP (op0, 1))
4877 & ~ INTVAL (op1))), op1);
4878
4879 if (GET_CODE (x) != AND)
4880 return x;
4881
4882 if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
4883 || GET_RTX_CLASS (GET_CODE (x)) == '2')
4884 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
4885 }
4886
4887 /* Convert (A | B) & A to A. */
4888 if (GET_CODE (op0) == IOR
4889 && (rtx_equal_p (XEXP (op0, 0), op1)
4890 || rtx_equal_p (XEXP (op0, 1), op1))
4891 && ! side_effects_p (XEXP (op0, 0))
4892 && ! side_effects_p (XEXP (op0, 1)))
4893 return op1;
4894
4895 /* In the following group of tests (and those in case IOR below),
4896 we start with some combination of logical operations and apply
4897 the distributive law followed by the inverse distributive law.
4898 Most of the time, this results in no change. However, if some of
4899 the operands are the same or inverses of each other, simplifications
4900 will result.
4901
4902 For example, (and (ior A B) (not B)) can occur as the result of
4903 expanding a bit field assignment. When we apply the distributive
4904 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
4905 which then simplifies to (and (A (not B))).
4906
4907 If we have (and (ior A B) C), apply the distributive law and then
4908 the inverse distributive law to see if things simplify. */
4909
4910 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
4911 {
4912 x = apply_distributive_law
4913 (gen_binary (GET_CODE (op0), mode,
4914 gen_binary (AND, mode, XEXP (op0, 0), op1),
4915 gen_binary (AND, mode, XEXP (op0, 1), op1)));
4916 if (GET_CODE (x) != AND)
4917 return x;
4918 }
4919
4920 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
4921 return apply_distributive_law
4922 (gen_binary (GET_CODE (op1), mode,
4923 gen_binary (AND, mode, XEXP (op1, 0), op0),
4924 gen_binary (AND, mode, XEXP (op1, 1), op0)));
4925
4926 /* Similarly, taking advantage of the fact that
4927 (and (not A) (xor B C)) == (xor (ior A B) (ior A C)) */
4928
4929 if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
4930 return apply_distributive_law
4931 (gen_binary (XOR, mode,
4932 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
4933 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 1))));
4934
4935 else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
4936 return apply_distributive_law
4937 (gen_binary (XOR, mode,
4938 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
4939 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 1))));
4940 break;
4941
4942 case IOR:
4943 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
4944 if (GET_CODE (op1) == CONST_INT
4945 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4946 && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
4947 return op1;
4948
4949 /* Convert (A & B) | A to A. */
4950 if (GET_CODE (op0) == AND
4951 && (rtx_equal_p (XEXP (op0, 0), op1)
4952 || rtx_equal_p (XEXP (op0, 1), op1))
4953 && ! side_effects_p (XEXP (op0, 0))
4954 && ! side_effects_p (XEXP (op0, 1)))
4955 return op1;
4956
4957 /* If we have (ior (and A B) C), apply the distributive law and then
4958 the inverse distributive law to see if things simplify. */
4959
4960 if (GET_CODE (op0) == AND)
4961 {
4962 x = apply_distributive_law
4963 (gen_binary (AND, mode,
4964 gen_binary (IOR, mode, XEXP (op0, 0), op1),
4965 gen_binary (IOR, mode, XEXP (op0, 1), op1)));
4966
4967 if (GET_CODE (x) != IOR)
4968 return x;
4969 }
4970
4971 if (GET_CODE (op1) == AND)
4972 {
4973 x = apply_distributive_law
4974 (gen_binary (AND, mode,
4975 gen_binary (IOR, mode, XEXP (op1, 0), op0),
4976 gen_binary (IOR, mode, XEXP (op1, 1), op0)));
4977
4978 if (GET_CODE (x) != IOR)
4979 return x;
4980 }
4981
4982 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
4983 mode size to (rotate A CX). */
4984
4985 if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
4986 || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
4987 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
4988 && GET_CODE (XEXP (op0, 1)) == CONST_INT
4989 && GET_CODE (XEXP (op1, 1)) == CONST_INT
4990 && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
4991 == GET_MODE_BITSIZE (mode)))
4992 return gen_rtx_ROTATE (mode, XEXP (op0, 0),
4993 (GET_CODE (op0) == ASHIFT
4994 ? XEXP (op0, 1) : XEXP (op1, 1)));
4995
4996 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
4997 a (sign_extend (plus ...)). If so, OP1 is a CONST_INT, and the PLUS
4998 does not affect any of the bits in OP1, it can really be done
4999 as a PLUS and we can associate. We do this by seeing if OP1
5000 can be safely shifted left C bits. */
5001 if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5002 && GET_CODE (XEXP (op0, 0)) == PLUS
5003 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5004 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5005 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5006 {
5007 int count = INTVAL (XEXP (op0, 1));
5008 HOST_WIDE_INT mask = INTVAL (op1) << count;
5009
5010 if (mask >> count == INTVAL (op1)
5011 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5012 {
5013 SUBST (XEXP (XEXP (op0, 0), 1),
5014 GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5015 return op0;
5016 }
5017 }
5018 break;
5019
5020 case XOR:
5021 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5022 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5023 (NOT y). */
5024 {
5025 int num_negated = 0;
5026
5027 if (GET_CODE (op0) == NOT)
5028 num_negated++, op0 = XEXP (op0, 0);
5029 if (GET_CODE (op1) == NOT)
5030 num_negated++, op1 = XEXP (op1, 0);
5031
5032 if (num_negated == 2)
5033 {
5034 SUBST (XEXP (x, 0), op0);
5035 SUBST (XEXP (x, 1), op1);
5036 }
5037 else if (num_negated == 1)
5038 return gen_unary (NOT, mode, mode, gen_binary (XOR, mode, op0, op1));
5039 }
5040
5041 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
5042 correspond to a machine insn or result in further simplifications
5043 if B is a constant. */
5044
5045 if (GET_CODE (op0) == AND
5046 && rtx_equal_p (XEXP (op0, 1), op1)
5047 && ! side_effects_p (op1))
5048 return gen_binary (AND, mode,
5049 gen_unary (NOT, mode, mode, XEXP (op0, 0)),
5050 op1);
5051
5052 else if (GET_CODE (op0) == AND
5053 && rtx_equal_p (XEXP (op0, 0), op1)
5054 && ! side_effects_p (op1))
5055 return gen_binary (AND, mode,
5056 gen_unary (NOT, mode, mode, XEXP (op0, 1)),
5057 op1);
5058
5059 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5060 comparison if STORE_FLAG_VALUE is 1. */
5061 if (STORE_FLAG_VALUE == 1
5062 && op1 == const1_rtx
5063 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5064 && reversible_comparison_p (op0))
5065 return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
5066 mode, XEXP (op0, 0), XEXP (op0, 1));
5067
5068 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5069 is (lt foo (const_int 0)), so we can perform the above
5070 simplification if STORE_FLAG_VALUE is 1. */
5071
5072 if (STORE_FLAG_VALUE == 1
5073 && op1 == const1_rtx
5074 && GET_CODE (op0) == LSHIFTRT
5075 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5076 && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5077 return gen_rtx_combine (GE, mode, XEXP (op0, 0), const0_rtx);
5078
5079 /* (xor (comparison foo bar) (const_int sign-bit))
5080 when STORE_FLAG_VALUE is the sign bit. */
5081 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5082 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5083 == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5084 && op1 == const_true_rtx
5085 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5086 && reversible_comparison_p (op0))
5087 return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
5088 mode, XEXP (op0, 0), XEXP (op0, 1));
5089 break;
5090
5091 default:
5092 abort ();
5093 }
5094
5095 return x;
5096 }
5097 \f
5098 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5099 operations" because they can be replaced with two more basic operations.
5100 ZERO_EXTEND is also considered "compound" because it can be replaced with
5101 an AND operation, which is simpler, though only one operation.
5102
5103 The function expand_compound_operation is called with an rtx expression
5104 and will convert it to the appropriate shifts and AND operations,
5105 simplifying at each stage.
5106
5107 The function make_compound_operation is called to convert an expression
5108 consisting of shifts and ANDs into the equivalent compound expression.
5109 It is the inverse of this function, loosely speaking. */
5110
5111 static rtx
5112 expand_compound_operation (x)
5113 rtx x;
5114 {
5115 int pos = 0, len;
5116 int unsignedp = 0;
5117 int modewidth;
5118 rtx tem;
5119
5120 switch (GET_CODE (x))
5121 {
5122 case ZERO_EXTEND:
5123 unsignedp = 1;
5124 case SIGN_EXTEND:
5125 /* We can't necessarily use a const_int for a multiword mode;
5126 it depends on implicitly extending the value.
5127 Since we don't know the right way to extend it,
5128 we can't tell whether the implicit way is right.
5129
5130 Even for a mode that is no wider than a const_int,
5131 we can't win, because we need to sign extend one of its bits through
5132 the rest of it, and we don't know which bit. */
5133 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5134 return x;
5135
5136 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5137 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5138 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5139 reloaded. If not for that, MEM's would very rarely be safe.
5140
5141 Reject MODEs bigger than a word, because we might not be able
5142 to reference a two-register group starting with an arbitrary register
5143 (and currently gen_lowpart might crash for a SUBREG). */
5144
5145 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5146 return x;
5147
5148 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5149 /* If the inner object has VOIDmode (the only way this can happen
5150 is if it is a ASM_OPERANDS), we can't do anything since we don't
5151 know how much masking to do. */
5152 if (len == 0)
5153 return x;
5154
5155 break;
5156
5157 case ZERO_EXTRACT:
5158 unsignedp = 1;
5159 case SIGN_EXTRACT:
5160 /* If the operand is a CLOBBER, just return it. */
5161 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5162 return XEXP (x, 0);
5163
5164 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5165 || GET_CODE (XEXP (x, 2)) != CONST_INT
5166 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5167 return x;
5168
5169 len = INTVAL (XEXP (x, 1));
5170 pos = INTVAL (XEXP (x, 2));
5171
5172 /* If this goes outside the object being extracted, replace the object
5173 with a (use (mem ...)) construct that only combine understands
5174 and is used only for this purpose. */
5175 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5176 SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5177
5178 if (BITS_BIG_ENDIAN)
5179 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5180
5181 break;
5182
5183 default:
5184 return x;
5185 }
5186
5187 /* We can optimize some special cases of ZERO_EXTEND. */
5188 if (GET_CODE (x) == ZERO_EXTEND)
5189 {
5190 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5191 know that the last value didn't have any inappropriate bits
5192 set. */
5193 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5194 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5195 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5196 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5197 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5198 return XEXP (XEXP (x, 0), 0);
5199
5200 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5201 if (GET_CODE (XEXP (x, 0)) == SUBREG
5202 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5203 && subreg_lowpart_p (XEXP (x, 0))
5204 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5205 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5206 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))) == 0)
5207 return SUBREG_REG (XEXP (x, 0));
5208
5209 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5210 is a comparison and STORE_FLAG_VALUE permits. This is like
5211 the first case, but it works even when GET_MODE (x) is larger
5212 than HOST_WIDE_INT. */
5213 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5214 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5215 && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) == '<'
5216 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5217 <= HOST_BITS_PER_WIDE_INT)
5218 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5219 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5220 return XEXP (XEXP (x, 0), 0);
5221
5222 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5223 if (GET_CODE (XEXP (x, 0)) == SUBREG
5224 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5225 && subreg_lowpart_p (XEXP (x, 0))
5226 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == '<'
5227 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5228 <= HOST_BITS_PER_WIDE_INT)
5229 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5230 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5231 return SUBREG_REG (XEXP (x, 0));
5232
5233 /* If sign extension is cheaper than zero extension, then use it
5234 if we know that no extraneous bits are set, and that the high
5235 bit is not set. */
5236 if (flag_expensive_optimizations
5237 && ((GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5238 && ((nonzero_bits (XEXP (x, 0), GET_MODE (x))
5239 & ~ (((unsigned HOST_WIDE_INT)
5240 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5241 >> 1))
5242 == 0))
5243 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
5244 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5245 <= HOST_BITS_PER_WIDE_INT)
5246 && (((HOST_WIDE_INT) STORE_FLAG_VALUE
5247 & ~ (((unsigned HOST_WIDE_INT)
5248 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5249 >> 1))
5250 == 0))))
5251 {
5252 rtx temp = gen_rtx_SIGN_EXTEND (GET_MODE (x), XEXP (x, 0));
5253
5254 if (rtx_cost (temp, SET) < rtx_cost (x, SET))
5255 return expand_compound_operation (temp);
5256 }
5257 }
5258
5259 /* If we reach here, we want to return a pair of shifts. The inner
5260 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5261 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5262 logical depending on the value of UNSIGNEDP.
5263
5264 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5265 converted into an AND of a shift.
5266
5267 We must check for the case where the left shift would have a negative
5268 count. This can happen in a case like (x >> 31) & 255 on machines
5269 that can't shift by a constant. On those machines, we would first
5270 combine the shift with the AND to produce a variable-position
5271 extraction. Then the constant of 31 would be substituted in to produce
5272 a such a position. */
5273
5274 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5275 if (modewidth >= pos - len)
5276 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5277 GET_MODE (x),
5278 simplify_shift_const (NULL_RTX, ASHIFT,
5279 GET_MODE (x),
5280 XEXP (x, 0),
5281 modewidth - pos - len),
5282 modewidth - len);
5283
5284 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5285 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5286 simplify_shift_const (NULL_RTX, LSHIFTRT,
5287 GET_MODE (x),
5288 XEXP (x, 0), pos),
5289 ((HOST_WIDE_INT) 1 << len) - 1);
5290 else
5291 /* Any other cases we can't handle. */
5292 return x;
5293
5294
5295 /* If we couldn't do this for some reason, return the original
5296 expression. */
5297 if (GET_CODE (tem) == CLOBBER)
5298 return x;
5299
5300 return tem;
5301 }
5302 \f
5303 /* X is a SET which contains an assignment of one object into
5304 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5305 or certain SUBREGS). If possible, convert it into a series of
5306 logical operations.
5307
5308 We half-heartedly support variable positions, but do not at all
5309 support variable lengths. */
5310
5311 static rtx
5312 expand_field_assignment (x)
5313 rtx x;
5314 {
5315 rtx inner;
5316 rtx pos; /* Always counts from low bit. */
5317 int len;
5318 rtx mask;
5319 enum machine_mode compute_mode;
5320
5321 /* Loop until we find something we can't simplify. */
5322 while (1)
5323 {
5324 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5325 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5326 {
5327 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5328 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5329 pos = GEN_INT (BITS_PER_WORD * SUBREG_WORD (XEXP (SET_DEST (x), 0)));
5330 }
5331 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5332 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5333 {
5334 inner = XEXP (SET_DEST (x), 0);
5335 len = INTVAL (XEXP (SET_DEST (x), 1));
5336 pos = XEXP (SET_DEST (x), 2);
5337
5338 /* If the position is constant and spans the width of INNER,
5339 surround INNER with a USE to indicate this. */
5340 if (GET_CODE (pos) == CONST_INT
5341 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5342 inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5343
5344 if (BITS_BIG_ENDIAN)
5345 {
5346 if (GET_CODE (pos) == CONST_INT)
5347 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5348 - INTVAL (pos));
5349 else if (GET_CODE (pos) == MINUS
5350 && GET_CODE (XEXP (pos, 1)) == CONST_INT
5351 && (INTVAL (XEXP (pos, 1))
5352 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5353 /* If position is ADJUST - X, new position is X. */
5354 pos = XEXP (pos, 0);
5355 else
5356 pos = gen_binary (MINUS, GET_MODE (pos),
5357 GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5358 - len),
5359 pos);
5360 }
5361 }
5362
5363 /* A SUBREG between two modes that occupy the same numbers of words
5364 can be done by moving the SUBREG to the source. */
5365 else if (GET_CODE (SET_DEST (x)) == SUBREG
5366 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5367 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5368 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5369 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5370 {
5371 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5372 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (SET_DEST (x))),
5373 SET_SRC (x)));
5374 continue;
5375 }
5376 else
5377 break;
5378
5379 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5380 inner = SUBREG_REG (inner);
5381
5382 compute_mode = GET_MODE (inner);
5383
5384 /* Compute a mask of LEN bits, if we can do this on the host machine. */
5385 if (len < HOST_BITS_PER_WIDE_INT)
5386 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5387 else
5388 break;
5389
5390 /* Now compute the equivalent expression. Make a copy of INNER
5391 for the SET_DEST in case it is a MEM into which we will substitute;
5392 we don't want shared RTL in that case. */
5393 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
5394 gen_binary (IOR, compute_mode,
5395 gen_binary (AND, compute_mode,
5396 gen_unary (NOT, compute_mode,
5397 compute_mode,
5398 gen_binary (ASHIFT,
5399 compute_mode,
5400 mask, pos)),
5401 inner),
5402 gen_binary (ASHIFT, compute_mode,
5403 gen_binary (AND, compute_mode,
5404 gen_lowpart_for_combine
5405 (compute_mode,
5406 SET_SRC (x)),
5407 mask),
5408 pos)));
5409 }
5410
5411 return x;
5412 }
5413 \f
5414 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
5415 it is an RTX that represents a variable starting position; otherwise,
5416 POS is the (constant) starting bit position (counted from the LSB).
5417
5418 INNER may be a USE. This will occur when we started with a bitfield
5419 that went outside the boundary of the object in memory, which is
5420 allowed on most machines. To isolate this case, we produce a USE
5421 whose mode is wide enough and surround the MEM with it. The only
5422 code that understands the USE is this routine. If it is not removed,
5423 it will cause the resulting insn not to match.
5424
5425 UNSIGNEDP is non-zero for an unsigned reference and zero for a
5426 signed reference.
5427
5428 IN_DEST is non-zero if this is a reference in the destination of a
5429 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If non-zero,
5430 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5431 be used.
5432
5433 IN_COMPARE is non-zero if we are in a COMPARE. This means that a
5434 ZERO_EXTRACT should be built even for bits starting at bit 0.
5435
5436 MODE is the desired mode of the result (if IN_DEST == 0).
5437
5438 The result is an RTX for the extraction or NULL_RTX if the target
5439 can't handle it. */
5440
5441 static rtx
5442 make_extraction (mode, inner, pos, pos_rtx, len,
5443 unsignedp, in_dest, in_compare)
5444 enum machine_mode mode;
5445 rtx inner;
5446 int pos;
5447 rtx pos_rtx;
5448 int len;
5449 int unsignedp;
5450 int in_dest, in_compare;
5451 {
5452 /* This mode describes the size of the storage area
5453 to fetch the overall value from. Within that, we
5454 ignore the POS lowest bits, etc. */
5455 enum machine_mode is_mode = GET_MODE (inner);
5456 enum machine_mode inner_mode;
5457 enum machine_mode wanted_inner_mode = byte_mode;
5458 enum machine_mode wanted_inner_reg_mode = word_mode;
5459 enum machine_mode pos_mode = word_mode;
5460 enum machine_mode extraction_mode = word_mode;
5461 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5462 int spans_byte = 0;
5463 rtx new = 0;
5464 rtx orig_pos_rtx = pos_rtx;
5465 int orig_pos;
5466
5467 /* Get some information about INNER and get the innermost object. */
5468 if (GET_CODE (inner) == USE)
5469 /* (use:SI (mem:QI foo)) stands for (mem:SI foo). */
5470 /* We don't need to adjust the position because we set up the USE
5471 to pretend that it was a full-word object. */
5472 spans_byte = 1, inner = XEXP (inner, 0);
5473 else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5474 {
5475 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5476 consider just the QI as the memory to extract from.
5477 The subreg adds or removes high bits; its mode is
5478 irrelevant to the meaning of this extraction,
5479 since POS and LEN count from the lsb. */
5480 if (GET_CODE (SUBREG_REG (inner)) == MEM)
5481 is_mode = GET_MODE (SUBREG_REG (inner));
5482 inner = SUBREG_REG (inner);
5483 }
5484
5485 inner_mode = GET_MODE (inner);
5486
5487 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
5488 pos = INTVAL (pos_rtx), pos_rtx = 0;
5489
5490 /* See if this can be done without an extraction. We never can if the
5491 width of the field is not the same as that of some integer mode. For
5492 registers, we can only avoid the extraction if the position is at the
5493 low-order bit and this is either not in the destination or we have the
5494 appropriate STRICT_LOW_PART operation available.
5495
5496 For MEM, we can avoid an extract if the field starts on an appropriate
5497 boundary and we can change the mode of the memory reference. However,
5498 we cannot directly access the MEM if we have a USE and the underlying
5499 MEM is not TMODE. This combination means that MEM was being used in a
5500 context where bits outside its mode were being referenced; that is only
5501 valid in bit-field insns. */
5502
5503 if (tmode != BLKmode
5504 && ! (spans_byte && inner_mode != tmode)
5505 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
5506 && GET_CODE (inner) != MEM
5507 && (! in_dest
5508 || (GET_CODE (inner) == REG
5509 && (movstrict_optab->handlers[(int) tmode].insn_code
5510 != CODE_FOR_nothing))))
5511 || (GET_CODE (inner) == MEM && pos_rtx == 0
5512 && (pos
5513 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
5514 : BITS_PER_UNIT)) == 0
5515 /* We can't do this if we are widening INNER_MODE (it
5516 may not be aligned, for one thing). */
5517 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
5518 && (inner_mode == tmode
5519 || (! mode_dependent_address_p (XEXP (inner, 0))
5520 && ! MEM_VOLATILE_P (inner))))))
5521 {
5522 /* If INNER is a MEM, make a new MEM that encompasses just the desired
5523 field. If the original and current mode are the same, we need not
5524 adjust the offset. Otherwise, we do if bytes big endian.
5525
5526 If INNER is not a MEM, get a piece consisting of just the field
5527 of interest (in this case POS % BITS_PER_WORD must be 0). */
5528
5529 if (GET_CODE (inner) == MEM)
5530 {
5531 int offset;
5532 /* POS counts from lsb, but make OFFSET count in memory order. */
5533 if (BYTES_BIG_ENDIAN)
5534 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
5535 else
5536 offset = pos / BITS_PER_UNIT;
5537
5538 new = gen_rtx_MEM (tmode, plus_constant (XEXP (inner, 0), offset));
5539 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (inner);
5540 MEM_VOLATILE_P (new) = MEM_VOLATILE_P (inner);
5541 MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (inner);
5542 }
5543 else if (GET_CODE (inner) == REG)
5544 {
5545 /* We can't call gen_lowpart_for_combine here since we always want
5546 a SUBREG and it would sometimes return a new hard register. */
5547 if (tmode != inner_mode)
5548 new = gen_rtx_SUBREG (tmode, inner,
5549 (WORDS_BIG_ENDIAN
5550 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD
5551 ? (((GET_MODE_SIZE (inner_mode)
5552 - GET_MODE_SIZE (tmode))
5553 / UNITS_PER_WORD)
5554 - pos / BITS_PER_WORD)
5555 : pos / BITS_PER_WORD));
5556 else
5557 new = inner;
5558 }
5559 else
5560 new = force_to_mode (inner, tmode,
5561 len >= HOST_BITS_PER_WIDE_INT
5562 ? GET_MODE_MASK (tmode)
5563 : ((HOST_WIDE_INT) 1 << len) - 1,
5564 NULL_RTX, 0);
5565
5566 /* If this extraction is going into the destination of a SET,
5567 make a STRICT_LOW_PART unless we made a MEM. */
5568
5569 if (in_dest)
5570 return (GET_CODE (new) == MEM ? new
5571 : (GET_CODE (new) != SUBREG
5572 ? gen_rtx_CLOBBER (tmode, const0_rtx)
5573 : gen_rtx_combine (STRICT_LOW_PART, VOIDmode, new)));
5574
5575 /* Otherwise, sign- or zero-extend unless we already are in the
5576 proper mode. */
5577
5578 return (mode == tmode ? new
5579 : gen_rtx_combine (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
5580 mode, new));
5581 }
5582
5583 /* Unless this is a COMPARE or we have a funny memory reference,
5584 don't do anything with zero-extending field extracts starting at
5585 the low-order bit since they are simple AND operations. */
5586 if (pos_rtx == 0 && pos == 0 && ! in_dest
5587 && ! in_compare && ! spans_byte && unsignedp)
5588 return 0;
5589
5590 /* Unless we are allowed to span bytes, reject this if we would be
5591 spanning bytes or if the position is not a constant and the length
5592 is not 1. In all other cases, we would only be going outside
5593 out object in cases when an original shift would have been
5594 undefined. */
5595 if (! spans_byte
5596 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
5597 || (pos_rtx != 0 && len != 1)))
5598 return 0;
5599
5600 /* Get the mode to use should INNER not be a MEM, the mode for the position,
5601 and the mode for the result. */
5602 #ifdef HAVE_insv
5603 if (in_dest)
5604 {
5605 wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_insv][0];
5606 pos_mode = insn_operand_mode[(int) CODE_FOR_insv][2];
5607 extraction_mode = insn_operand_mode[(int) CODE_FOR_insv][3];
5608 }
5609 #endif
5610
5611 #ifdef HAVE_extzv
5612 if (! in_dest && unsignedp)
5613 {
5614 wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_extzv][1];
5615 pos_mode = insn_operand_mode[(int) CODE_FOR_extzv][3];
5616 extraction_mode = insn_operand_mode[(int) CODE_FOR_extzv][0];
5617 }
5618 #endif
5619
5620 #ifdef HAVE_extv
5621 if (! in_dest && ! unsignedp)
5622 {
5623 wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_extv][1];
5624 pos_mode = insn_operand_mode[(int) CODE_FOR_extv][3];
5625 extraction_mode = insn_operand_mode[(int) CODE_FOR_extv][0];
5626 }
5627 #endif
5628
5629 /* Never narrow an object, since that might not be safe. */
5630
5631 if (mode != VOIDmode
5632 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
5633 extraction_mode = mode;
5634
5635 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
5636 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5637 pos_mode = GET_MODE (pos_rtx);
5638
5639 /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
5640 if we have to change the mode of memory and cannot, the desired mode is
5641 EXTRACTION_MODE. */
5642 if (GET_CODE (inner) != MEM)
5643 wanted_inner_mode = wanted_inner_reg_mode;
5644 else if (inner_mode != wanted_inner_mode
5645 && (mode_dependent_address_p (XEXP (inner, 0))
5646 || MEM_VOLATILE_P (inner)))
5647 wanted_inner_mode = extraction_mode;
5648
5649 orig_pos = pos;
5650
5651 if (BITS_BIG_ENDIAN)
5652 {
5653 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
5654 BITS_BIG_ENDIAN style. If position is constant, compute new
5655 position. Otherwise, build subtraction.
5656 Note that POS is relative to the mode of the original argument.
5657 If it's a MEM we need to recompute POS relative to that.
5658 However, if we're extracting from (or inserting into) a register,
5659 we want to recompute POS relative to wanted_inner_mode. */
5660 int width = (GET_CODE (inner) == MEM
5661 ? GET_MODE_BITSIZE (is_mode)
5662 : GET_MODE_BITSIZE (wanted_inner_mode));
5663
5664 if (pos_rtx == 0)
5665 pos = width - len - pos;
5666 else
5667 pos_rtx
5668 = gen_rtx_combine (MINUS, GET_MODE (pos_rtx),
5669 GEN_INT (width - len), pos_rtx);
5670 /* POS may be less than 0 now, but we check for that below.
5671 Note that it can only be less than 0 if GET_CODE (inner) != MEM. */
5672 }
5673
5674 /* If INNER has a wider mode, make it smaller. If this is a constant
5675 extract, try to adjust the byte to point to the byte containing
5676 the value. */
5677 if (wanted_inner_mode != VOIDmode
5678 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
5679 && ((GET_CODE (inner) == MEM
5680 && (inner_mode == wanted_inner_mode
5681 || (! mode_dependent_address_p (XEXP (inner, 0))
5682 && ! MEM_VOLATILE_P (inner))))))
5683 {
5684 int offset = 0;
5685
5686 /* The computations below will be correct if the machine is big
5687 endian in both bits and bytes or little endian in bits and bytes.
5688 If it is mixed, we must adjust. */
5689
5690 /* If bytes are big endian and we had a paradoxical SUBREG, we must
5691 adjust OFFSET to compensate. */
5692 if (BYTES_BIG_ENDIAN
5693 && ! spans_byte
5694 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
5695 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
5696
5697 /* If this is a constant position, we can move to the desired byte. */
5698 if (pos_rtx == 0)
5699 {
5700 offset += pos / BITS_PER_UNIT;
5701 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
5702 }
5703
5704 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
5705 && ! spans_byte
5706 && is_mode != wanted_inner_mode)
5707 offset = (GET_MODE_SIZE (is_mode)
5708 - GET_MODE_SIZE (wanted_inner_mode) - offset);
5709
5710 if (offset != 0 || inner_mode != wanted_inner_mode)
5711 {
5712 rtx newmem = gen_rtx_MEM (wanted_inner_mode,
5713 plus_constant (XEXP (inner, 0), offset));
5714 RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (inner);
5715 MEM_VOLATILE_P (newmem) = MEM_VOLATILE_P (inner);
5716 MEM_IN_STRUCT_P (newmem) = MEM_IN_STRUCT_P (inner);
5717 inner = newmem;
5718 }
5719 }
5720
5721 /* If INNER is not memory, we can always get it into the proper mode. If we
5722 are changing its mode, POS must be a constant and smaller than the size
5723 of the new mode. */
5724 else if (GET_CODE (inner) != MEM)
5725 {
5726 if (GET_MODE (inner) != wanted_inner_mode
5727 && (pos_rtx != 0
5728 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
5729 return 0;
5730
5731 inner = force_to_mode (inner, wanted_inner_mode,
5732 pos_rtx
5733 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
5734 ? GET_MODE_MASK (wanted_inner_mode)
5735 : (((HOST_WIDE_INT) 1 << len) - 1) << orig_pos,
5736 NULL_RTX, 0);
5737 }
5738
5739 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
5740 have to zero extend. Otherwise, we can just use a SUBREG. */
5741 if (pos_rtx != 0
5742 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
5743 pos_rtx = gen_rtx_combine (ZERO_EXTEND, pos_mode, pos_rtx);
5744 else if (pos_rtx != 0
5745 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5746 pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
5747
5748 /* Make POS_RTX unless we already have it and it is correct. If we don't
5749 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
5750 be a CONST_INT. */
5751 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
5752 pos_rtx = orig_pos_rtx;
5753
5754 else if (pos_rtx == 0)
5755 pos_rtx = GEN_INT (pos);
5756
5757 /* Make the required operation. See if we can use existing rtx. */
5758 new = gen_rtx_combine (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
5759 extraction_mode, inner, GEN_INT (len), pos_rtx);
5760 if (! in_dest)
5761 new = gen_lowpart_for_combine (mode, new);
5762
5763 return new;
5764 }
5765 \f
5766 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
5767 with any other operations in X. Return X without that shift if so. */
5768
5769 static rtx
5770 extract_left_shift (x, count)
5771 rtx x;
5772 int count;
5773 {
5774 enum rtx_code code = GET_CODE (x);
5775 enum machine_mode mode = GET_MODE (x);
5776 rtx tem;
5777
5778 switch (code)
5779 {
5780 case ASHIFT:
5781 /* This is the shift itself. If it is wide enough, we will return
5782 either the value being shifted if the shift count is equal to
5783 COUNT or a shift for the difference. */
5784 if (GET_CODE (XEXP (x, 1)) == CONST_INT
5785 && INTVAL (XEXP (x, 1)) >= count)
5786 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
5787 INTVAL (XEXP (x, 1)) - count);
5788 break;
5789
5790 case NEG: case NOT:
5791 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5792 return gen_unary (code, mode, mode, tem);
5793
5794 break;
5795
5796 case PLUS: case IOR: case XOR: case AND:
5797 /* If we can safely shift this constant and we find the inner shift,
5798 make a new operation. */
5799 if (GET_CODE (XEXP (x,1)) == CONST_INT
5800 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
5801 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5802 return gen_binary (code, mode, tem,
5803 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
5804
5805 break;
5806
5807 default:
5808 break;
5809 }
5810
5811 return 0;
5812 }
5813 \f
5814 /* Look at the expression rooted at X. Look for expressions
5815 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
5816 Form these expressions.
5817
5818 Return the new rtx, usually just X.
5819
5820 Also, for machines like the Vax that don't have logical shift insns,
5821 try to convert logical to arithmetic shift operations in cases where
5822 they are equivalent. This undoes the canonicalizations to logical
5823 shifts done elsewhere.
5824
5825 We try, as much as possible, to re-use rtl expressions to save memory.
5826
5827 IN_CODE says what kind of expression we are processing. Normally, it is
5828 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
5829 being kludges), it is MEM. When processing the arguments of a comparison
5830 or a COMPARE against zero, it is COMPARE. */
5831
5832 static rtx
5833 make_compound_operation (x, in_code)
5834 rtx x;
5835 enum rtx_code in_code;
5836 {
5837 enum rtx_code code = GET_CODE (x);
5838 enum machine_mode mode = GET_MODE (x);
5839 int mode_width = GET_MODE_BITSIZE (mode);
5840 rtx rhs, lhs;
5841 enum rtx_code next_code;
5842 int i;
5843 rtx new = 0;
5844 rtx tem;
5845 char *fmt;
5846
5847 /* Select the code to be used in recursive calls. Once we are inside an
5848 address, we stay there. If we have a comparison, set to COMPARE,
5849 but once inside, go back to our default of SET. */
5850
5851 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
5852 : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
5853 && XEXP (x, 1) == const0_rtx) ? COMPARE
5854 : in_code == COMPARE ? SET : in_code);
5855
5856 /* Process depending on the code of this operation. If NEW is set
5857 non-zero, it will be returned. */
5858
5859 switch (code)
5860 {
5861 case ASHIFT:
5862 /* Convert shifts by constants into multiplications if inside
5863 an address. */
5864 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
5865 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
5866 && INTVAL (XEXP (x, 1)) >= 0)
5867 {
5868 new = make_compound_operation (XEXP (x, 0), next_code);
5869 new = gen_rtx_combine (MULT, mode, new,
5870 GEN_INT ((HOST_WIDE_INT) 1
5871 << INTVAL (XEXP (x, 1))));
5872 }
5873 break;
5874
5875 case AND:
5876 /* If the second operand is not a constant, we can't do anything
5877 with it. */
5878 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5879 break;
5880
5881 /* If the constant is a power of two minus one and the first operand
5882 is a logical right shift, make an extraction. */
5883 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
5884 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5885 {
5886 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
5887 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
5888 0, in_code == COMPARE);
5889 }
5890
5891 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
5892 else if (GET_CODE (XEXP (x, 0)) == SUBREG
5893 && subreg_lowpart_p (XEXP (x, 0))
5894 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
5895 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5896 {
5897 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
5898 next_code);
5899 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
5900 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
5901 0, in_code == COMPARE);
5902 }
5903 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
5904 else if ((GET_CODE (XEXP (x, 0)) == XOR
5905 || GET_CODE (XEXP (x, 0)) == IOR)
5906 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
5907 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
5908 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5909 {
5910 /* Apply the distributive law, and then try to make extractions. */
5911 new = gen_rtx_combine (GET_CODE (XEXP (x, 0)), mode,
5912 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
5913 XEXP (x, 1)),
5914 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
5915 XEXP (x, 1)));
5916 new = make_compound_operation (new, in_code);
5917 }
5918
5919 /* If we are have (and (rotate X C) M) and C is larger than the number
5920 of bits in M, this is an extraction. */
5921
5922 else if (GET_CODE (XEXP (x, 0)) == ROTATE
5923 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5924 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
5925 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
5926 {
5927 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
5928 new = make_extraction (mode, new,
5929 (GET_MODE_BITSIZE (mode)
5930 - INTVAL (XEXP (XEXP (x, 0), 1))),
5931 NULL_RTX, i, 1, 0, in_code == COMPARE);
5932 }
5933
5934 /* On machines without logical shifts, if the operand of the AND is
5935 a logical shift and our mask turns off all the propagated sign
5936 bits, we can replace the logical shift with an arithmetic shift. */
5937 else if (ashr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
5938 && (lshr_optab->handlers[(int) mode].insn_code
5939 == CODE_FOR_nothing)
5940 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
5941 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5942 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
5943 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
5944 && mode_width <= HOST_BITS_PER_WIDE_INT)
5945 {
5946 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
5947
5948 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
5949 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
5950 SUBST (XEXP (x, 0),
5951 gen_rtx_combine (ASHIFTRT, mode,
5952 make_compound_operation (XEXP (XEXP (x, 0), 0),
5953 next_code),
5954 XEXP (XEXP (x, 0), 1)));
5955 }
5956
5957 /* If the constant is one less than a power of two, this might be
5958 representable by an extraction even if no shift is present.
5959 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
5960 we are in a COMPARE. */
5961 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5962 new = make_extraction (mode,
5963 make_compound_operation (XEXP (x, 0),
5964 next_code),
5965 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
5966
5967 /* If we are in a comparison and this is an AND with a power of two,
5968 convert this into the appropriate bit extract. */
5969 else if (in_code == COMPARE
5970 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
5971 new = make_extraction (mode,
5972 make_compound_operation (XEXP (x, 0),
5973 next_code),
5974 i, NULL_RTX, 1, 1, 0, 1);
5975
5976 break;
5977
5978 case LSHIFTRT:
5979 /* If the sign bit is known to be zero, replace this with an
5980 arithmetic shift. */
5981 if (ashr_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing
5982 && lshr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
5983 && mode_width <= HOST_BITS_PER_WIDE_INT
5984 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
5985 {
5986 new = gen_rtx_combine (ASHIFTRT, mode,
5987 make_compound_operation (XEXP (x, 0),
5988 next_code),
5989 XEXP (x, 1));
5990 break;
5991 }
5992
5993 /* ... fall through ... */
5994
5995 case ASHIFTRT:
5996 lhs = XEXP (x, 0);
5997 rhs = XEXP (x, 1);
5998
5999 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6000 this is a SIGN_EXTRACT. */
6001 if (GET_CODE (rhs) == CONST_INT
6002 && GET_CODE (lhs) == ASHIFT
6003 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6004 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6005 {
6006 new = make_compound_operation (XEXP (lhs, 0), next_code);
6007 new = make_extraction (mode, new,
6008 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6009 NULL_RTX, mode_width - INTVAL (rhs),
6010 code == LSHIFTRT, 0, in_code == COMPARE);
6011 }
6012
6013 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6014 If so, try to merge the shifts into a SIGN_EXTEND. We could
6015 also do this for some cases of SIGN_EXTRACT, but it doesn't
6016 seem worth the effort; the case checked for occurs on Alpha. */
6017
6018 if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
6019 && ! (GET_CODE (lhs) == SUBREG
6020 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
6021 && GET_CODE (rhs) == CONST_INT
6022 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6023 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6024 new = make_extraction (mode, make_compound_operation (new, next_code),
6025 0, NULL_RTX, mode_width - INTVAL (rhs),
6026 code == LSHIFTRT, 0, in_code == COMPARE);
6027
6028 break;
6029
6030 case SUBREG:
6031 /* Call ourselves recursively on the inner expression. If we are
6032 narrowing the object and it has a different RTL code from
6033 what it originally did, do this SUBREG as a force_to_mode. */
6034
6035 tem = make_compound_operation (SUBREG_REG (x), in_code);
6036 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6037 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6038 && subreg_lowpart_p (x))
6039 {
6040 rtx newer = force_to_mode (tem, mode,
6041 GET_MODE_MASK (mode), NULL_RTX, 0);
6042
6043 /* If we have something other than a SUBREG, we might have
6044 done an expansion, so rerun outselves. */
6045 if (GET_CODE (newer) != SUBREG)
6046 newer = make_compound_operation (newer, in_code);
6047
6048 return newer;
6049 }
6050
6051 /* If this is a paradoxical subreg, and the new code is a sign or
6052 zero extension, omit the subreg and widen the extension. If it
6053 is a regular subreg, we can still get rid of the subreg by not
6054 widening so much, or in fact removing the extension entirely. */
6055 if ((GET_CODE (tem) == SIGN_EXTEND
6056 || GET_CODE (tem) == ZERO_EXTEND)
6057 && subreg_lowpart_p (x))
6058 {
6059 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6060 || (GET_MODE_SIZE (mode) >
6061 GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6062 tem = gen_rtx_combine (GET_CODE (tem), mode, XEXP (tem, 0));
6063 else
6064 tem = gen_lowpart_for_combine (mode, XEXP (tem, 0));
6065 return tem;
6066 }
6067 break;
6068
6069 default:
6070 break;
6071 }
6072
6073 if (new)
6074 {
6075 x = gen_lowpart_for_combine (mode, new);
6076 code = GET_CODE (x);
6077 }
6078
6079 /* Now recursively process each operand of this operation. */
6080 fmt = GET_RTX_FORMAT (code);
6081 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6082 if (fmt[i] == 'e')
6083 {
6084 new = make_compound_operation (XEXP (x, i), next_code);
6085 SUBST (XEXP (x, i), new);
6086 }
6087
6088 return x;
6089 }
6090 \f
6091 /* Given M see if it is a value that would select a field of bits
6092 within an item, but not the entire word. Return -1 if not.
6093 Otherwise, return the starting position of the field, where 0 is the
6094 low-order bit.
6095
6096 *PLEN is set to the length of the field. */
6097
6098 static int
6099 get_pos_from_mask (m, plen)
6100 unsigned HOST_WIDE_INT m;
6101 int *plen;
6102 {
6103 /* Get the bit number of the first 1 bit from the right, -1 if none. */
6104 int pos = exact_log2 (m & - m);
6105
6106 if (pos < 0)
6107 return -1;
6108
6109 /* Now shift off the low-order zero bits and see if we have a power of
6110 two minus 1. */
6111 *plen = exact_log2 ((m >> pos) + 1);
6112
6113 if (*plen <= 0)
6114 return -1;
6115
6116 return pos;
6117 }
6118 \f
6119 /* See if X can be simplified knowing that we will only refer to it in
6120 MODE and will only refer to those bits that are nonzero in MASK.
6121 If other bits are being computed or if masking operations are done
6122 that select a superset of the bits in MASK, they can sometimes be
6123 ignored.
6124
6125 Return a possibly simplified expression, but always convert X to
6126 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
6127
6128 Also, if REG is non-zero and X is a register equal in value to REG,
6129 replace X with REG.
6130
6131 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6132 are all off in X. This is used when X will be complemented, by either
6133 NOT, NEG, or XOR. */
6134
6135 static rtx
6136 force_to_mode (x, mode, mask, reg, just_select)
6137 rtx x;
6138 enum machine_mode mode;
6139 unsigned HOST_WIDE_INT mask;
6140 rtx reg;
6141 int just_select;
6142 {
6143 enum rtx_code code = GET_CODE (x);
6144 int next_select = just_select || code == XOR || code == NOT || code == NEG;
6145 enum machine_mode op_mode;
6146 unsigned HOST_WIDE_INT fuller_mask, nonzero;
6147 rtx op0, op1, temp;
6148
6149 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
6150 code below will do the wrong thing since the mode of such an
6151 expression is VOIDmode.
6152
6153 Also do nothing if X is a CLOBBER; this can happen if X was
6154 the return value from a call to gen_lowpart_for_combine. */
6155 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6156 return x;
6157
6158 /* We want to perform the operation is its present mode unless we know
6159 that the operation is valid in MODE, in which case we do the operation
6160 in MODE. */
6161 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6162 && code_to_optab[(int) code] != 0
6163 && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
6164 != CODE_FOR_nothing))
6165 ? mode : GET_MODE (x));
6166
6167 /* It is not valid to do a right-shift in a narrower mode
6168 than the one it came in with. */
6169 if ((code == LSHIFTRT || code == ASHIFTRT)
6170 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6171 op_mode = GET_MODE (x);
6172
6173 /* Truncate MASK to fit OP_MODE. */
6174 if (op_mode)
6175 mask &= GET_MODE_MASK (op_mode);
6176
6177 /* When we have an arithmetic operation, or a shift whose count we
6178 do not know, we need to assume that all bit the up to the highest-order
6179 bit in MASK will be needed. This is how we form such a mask. */
6180 if (op_mode)
6181 fuller_mask = (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT
6182 ? GET_MODE_MASK (op_mode)
6183 : ((HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1)) - 1);
6184 else
6185 fuller_mask = ~ (HOST_WIDE_INT) 0;
6186
6187 /* Determine what bits of X are guaranteed to be (non)zero. */
6188 nonzero = nonzero_bits (x, mode);
6189
6190 /* If none of the bits in X are needed, return a zero. */
6191 if (! just_select && (nonzero & mask) == 0)
6192 return const0_rtx;
6193
6194 /* If X is a CONST_INT, return a new one. Do this here since the
6195 test below will fail. */
6196 if (GET_CODE (x) == CONST_INT)
6197 {
6198 HOST_WIDE_INT cval = INTVAL (x) & mask;
6199 int width = GET_MODE_BITSIZE (mode);
6200
6201 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6202 number, sign extend it. */
6203 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6204 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6205 cval |= (HOST_WIDE_INT) -1 << width;
6206
6207 return GEN_INT (cval);
6208 }
6209
6210 /* If X is narrower than MODE and we want all the bits in X's mode, just
6211 get X in the proper mode. */
6212 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6213 && (GET_MODE_MASK (GET_MODE (x)) & ~ mask) == 0)
6214 return gen_lowpart_for_combine (mode, x);
6215
6216 /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6217 MASK are already known to be zero in X, we need not do anything. */
6218 if (GET_MODE (x) == mode && code != SUBREG && (~ mask & nonzero) == 0)
6219 return x;
6220
6221 switch (code)
6222 {
6223 case CLOBBER:
6224 /* If X is a (clobber (const_int)), return it since we know we are
6225 generating something that won't match. */
6226 return x;
6227
6228 case USE:
6229 /* X is a (use (mem ..)) that was made from a bit-field extraction that
6230 spanned the boundary of the MEM. If we are now masking so it is
6231 within that boundary, we don't need the USE any more. */
6232 if (! BITS_BIG_ENDIAN
6233 && (mask & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6234 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6235 break;
6236
6237 case SIGN_EXTEND:
6238 case ZERO_EXTEND:
6239 case ZERO_EXTRACT:
6240 case SIGN_EXTRACT:
6241 x = expand_compound_operation (x);
6242 if (GET_CODE (x) != code)
6243 return force_to_mode (x, mode, mask, reg, next_select);
6244 break;
6245
6246 case REG:
6247 if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6248 || rtx_equal_p (reg, get_last_value (x))))
6249 x = reg;
6250 break;
6251
6252 case SUBREG:
6253 if (subreg_lowpart_p (x)
6254 /* We can ignore the effect of this SUBREG if it narrows the mode or
6255 if the constant masks to zero all the bits the mode doesn't
6256 have. */
6257 && ((GET_MODE_SIZE (GET_MODE (x))
6258 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6259 || (0 == (mask
6260 & GET_MODE_MASK (GET_MODE (x))
6261 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6262 return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6263 break;
6264
6265 case AND:
6266 /* If this is an AND with a constant, convert it into an AND
6267 whose constant is the AND of that constant with MASK. If it
6268 remains an AND of MASK, delete it since it is redundant. */
6269
6270 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6271 {
6272 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6273 mask & INTVAL (XEXP (x, 1)));
6274
6275 /* If X is still an AND, see if it is an AND with a mask that
6276 is just some low-order bits. If so, and it is MASK, we don't
6277 need it. */
6278
6279 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6280 && INTVAL (XEXP (x, 1)) == mask)
6281 x = XEXP (x, 0);
6282
6283 /* If it remains an AND, try making another AND with the bits
6284 in the mode mask that aren't in MASK turned on. If the
6285 constant in the AND is wide enough, this might make a
6286 cheaper constant. */
6287
6288 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6289 && GET_MODE_MASK (GET_MODE (x)) != mask
6290 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6291 {
6292 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6293 | (GET_MODE_MASK (GET_MODE (x)) & ~ mask));
6294 int width = GET_MODE_BITSIZE (GET_MODE (x));
6295 rtx y;
6296
6297 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6298 number, sign extend it. */
6299 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6300 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6301 cval |= (HOST_WIDE_INT) -1 << width;
6302
6303 y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6304 if (rtx_cost (y, SET) < rtx_cost (x, SET))
6305 x = y;
6306 }
6307
6308 break;
6309 }
6310
6311 goto binop;
6312
6313 case PLUS:
6314 /* In (and (plus FOO C1) M), if M is a mask that just turns off
6315 low-order bits (as in an alignment operation) and FOO is already
6316 aligned to that boundary, mask C1 to that boundary as well.
6317 This may eliminate that PLUS and, later, the AND. */
6318
6319 {
6320 int width = GET_MODE_BITSIZE (mode);
6321 unsigned HOST_WIDE_INT smask = mask;
6322
6323 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6324 number, sign extend it. */
6325
6326 if (width < HOST_BITS_PER_WIDE_INT
6327 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6328 smask |= (HOST_WIDE_INT) -1 << width;
6329
6330 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6331 && exact_log2 (- smask) >= 0)
6332 {
6333 #ifdef STACK_BIAS
6334 if (STACK_BIAS
6335 && (XEXP (x, 0) == stack_pointer_rtx
6336 || XEXP (x, 0) == frame_pointer_rtx))
6337 {
6338 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
6339 unsigned HOST_WIDE_INT sp_mask = GET_MODE_MASK (mode);
6340
6341 sp_mask &= ~ (sp_alignment - 1);
6342 if ((sp_mask & ~ mask) == 0
6343 && ((INTVAL (XEXP (x, 1)) - STACK_BIAS) & ~ mask) != 0)
6344 return force_to_mode (plus_constant (XEXP (x, 0),
6345 ((INTVAL (XEXP (x, 1)) -
6346 STACK_BIAS) & mask)
6347 + STACK_BIAS),
6348 mode, mask, reg, next_select);
6349 }
6350 #endif
6351 if ((nonzero_bits (XEXP (x, 0), mode) & ~ mask) == 0
6352 && (INTVAL (XEXP (x, 1)) & ~ mask) != 0)
6353 return force_to_mode (plus_constant (XEXP (x, 0),
6354 INTVAL (XEXP (x, 1)) & mask),
6355 mode, mask, reg, next_select);
6356 }
6357 }
6358
6359 /* ... fall through ... */
6360
6361 case MINUS:
6362 case MULT:
6363 /* For PLUS, MINUS and MULT, we need any bits less significant than the
6364 most significant bit in MASK since carries from those bits will
6365 affect the bits we are interested in. */
6366 mask = fuller_mask;
6367 goto binop;
6368
6369 case IOR:
6370 case XOR:
6371 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6372 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6373 operation which may be a bitfield extraction. Ensure that the
6374 constant we form is not wider than the mode of X. */
6375
6376 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6377 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6378 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6379 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6380 && GET_CODE (XEXP (x, 1)) == CONST_INT
6381 && ((INTVAL (XEXP (XEXP (x, 0), 1))
6382 + floor_log2 (INTVAL (XEXP (x, 1))))
6383 < GET_MODE_BITSIZE (GET_MODE (x)))
6384 && (INTVAL (XEXP (x, 1))
6385 & ~ nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6386 {
6387 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6388 << INTVAL (XEXP (XEXP (x, 0), 1)));
6389 temp = gen_binary (GET_CODE (x), GET_MODE (x),
6390 XEXP (XEXP (x, 0), 0), temp);
6391 x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
6392 XEXP (XEXP (x, 0), 1));
6393 return force_to_mode (x, mode, mask, reg, next_select);
6394 }
6395
6396 binop:
6397 /* For most binary operations, just propagate into the operation and
6398 change the mode if we have an operation of that mode. */
6399
6400 op0 = gen_lowpart_for_combine (op_mode,
6401 force_to_mode (XEXP (x, 0), mode, mask,
6402 reg, next_select));
6403 op1 = gen_lowpart_for_combine (op_mode,
6404 force_to_mode (XEXP (x, 1), mode, mask,
6405 reg, next_select));
6406
6407 /* If OP1 is a CONST_INT and X is an IOR or XOR, clear bits outside
6408 MASK since OP1 might have been sign-extended but we never want
6409 to turn on extra bits, since combine might have previously relied
6410 on them being off. */
6411 if (GET_CODE (op1) == CONST_INT && (code == IOR || code == XOR)
6412 && (INTVAL (op1) & mask) != 0)
6413 op1 = GEN_INT (INTVAL (op1) & mask);
6414
6415 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6416 x = gen_binary (code, op_mode, op0, op1);
6417 break;
6418
6419 case ASHIFT:
6420 /* For left shifts, do the same, but just for the first operand.
6421 However, we cannot do anything with shifts where we cannot
6422 guarantee that the counts are smaller than the size of the mode
6423 because such a count will have a different meaning in a
6424 wider mode. */
6425
6426 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
6427 && INTVAL (XEXP (x, 1)) >= 0
6428 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
6429 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
6430 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
6431 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
6432 break;
6433
6434 /* If the shift count is a constant and we can do arithmetic in
6435 the mode of the shift, refine which bits we need. Otherwise, use the
6436 conservative form of the mask. */
6437 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6438 && INTVAL (XEXP (x, 1)) >= 0
6439 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
6440 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6441 mask >>= INTVAL (XEXP (x, 1));
6442 else
6443 mask = fuller_mask;
6444
6445 op0 = gen_lowpart_for_combine (op_mode,
6446 force_to_mode (XEXP (x, 0), op_mode,
6447 mask, reg, next_select));
6448
6449 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6450 x = gen_binary (code, op_mode, op0, XEXP (x, 1));
6451 break;
6452
6453 case LSHIFTRT:
6454 /* Here we can only do something if the shift count is a constant,
6455 this shift constant is valid for the host, and we can do arithmetic
6456 in OP_MODE. */
6457
6458 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6459 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6460 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6461 {
6462 rtx inner = XEXP (x, 0);
6463
6464 /* Select the mask of the bits we need for the shift operand. */
6465 mask <<= INTVAL (XEXP (x, 1));
6466
6467 /* We can only change the mode of the shift if we can do arithmetic
6468 in the mode of the shift and MASK is no wider than the width of
6469 OP_MODE. */
6470 if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
6471 || (mask & ~ GET_MODE_MASK (op_mode)) != 0)
6472 op_mode = GET_MODE (x);
6473
6474 inner = force_to_mode (inner, op_mode, mask, reg, next_select);
6475
6476 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
6477 x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
6478 }
6479
6480 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
6481 shift and AND produces only copies of the sign bit (C2 is one less
6482 than a power of two), we can do this with just a shift. */
6483
6484 if (GET_CODE (x) == LSHIFTRT
6485 && GET_CODE (XEXP (x, 1)) == CONST_INT
6486 && ((INTVAL (XEXP (x, 1))
6487 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
6488 >= GET_MODE_BITSIZE (GET_MODE (x)))
6489 && exact_log2 (mask + 1) >= 0
6490 && (num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6491 >= exact_log2 (mask + 1)))
6492 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6493 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
6494 - exact_log2 (mask + 1)));
6495 break;
6496
6497 case ASHIFTRT:
6498 /* If we are just looking for the sign bit, we don't need this shift at
6499 all, even if it has a variable count. */
6500 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6501 && (mask == ((HOST_WIDE_INT) 1
6502 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
6503 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6504
6505 /* If this is a shift by a constant, get a mask that contains those bits
6506 that are not copies of the sign bit. We then have two cases: If
6507 MASK only includes those bits, this can be a logical shift, which may
6508 allow simplifications. If MASK is a single-bit field not within
6509 those bits, we are requesting a copy of the sign bit and hence can
6510 shift the sign bit to the appropriate location. */
6511
6512 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
6513 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
6514 {
6515 int i = -1;
6516
6517 /* If the considered data is wider then HOST_WIDE_INT, we can't
6518 represent a mask for all its bits in a single scalar.
6519 But we only care about the lower bits, so calculate these. */
6520
6521 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
6522 {
6523 nonzero = ~ (HOST_WIDE_INT) 0;
6524
6525 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6526 is the number of bits a full-width mask would have set.
6527 We need only shift if these are fewer than nonzero can
6528 hold. If not, we must keep all bits set in nonzero. */
6529
6530 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6531 < HOST_BITS_PER_WIDE_INT)
6532 nonzero >>= INTVAL (XEXP (x, 1))
6533 + HOST_BITS_PER_WIDE_INT
6534 - GET_MODE_BITSIZE (GET_MODE (x)) ;
6535 }
6536 else
6537 {
6538 nonzero = GET_MODE_MASK (GET_MODE (x));
6539 nonzero >>= INTVAL (XEXP (x, 1));
6540 }
6541
6542 if ((mask & ~ nonzero) == 0
6543 || (i = exact_log2 (mask)) >= 0)
6544 {
6545 x = simplify_shift_const
6546 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6547 i < 0 ? INTVAL (XEXP (x, 1))
6548 : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
6549
6550 if (GET_CODE (x) != ASHIFTRT)
6551 return force_to_mode (x, mode, mask, reg, next_select);
6552 }
6553 }
6554
6555 /* If MASK is 1, convert this to a LSHIFTRT. This can be done
6556 even if the shift count isn't a constant. */
6557 if (mask == 1)
6558 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
6559
6560 /* If this is a sign-extension operation that just affects bits
6561 we don't care about, remove it. Be sure the call above returned
6562 something that is still a shift. */
6563
6564 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
6565 && GET_CODE (XEXP (x, 1)) == CONST_INT
6566 && INTVAL (XEXP (x, 1)) >= 0
6567 && (INTVAL (XEXP (x, 1))
6568 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
6569 && GET_CODE (XEXP (x, 0)) == ASHIFT
6570 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6571 && INTVAL (XEXP (XEXP (x, 0), 1)) == INTVAL (XEXP (x, 1)))
6572 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
6573 reg, next_select);
6574
6575 break;
6576
6577 case ROTATE:
6578 case ROTATERT:
6579 /* If the shift count is constant and we can do computations
6580 in the mode of X, compute where the bits we care about are.
6581 Otherwise, we can't do anything. Don't change the mode of
6582 the shift or propagate MODE into the shift, though. */
6583 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6584 && INTVAL (XEXP (x, 1)) >= 0)
6585 {
6586 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
6587 GET_MODE (x), GEN_INT (mask),
6588 XEXP (x, 1));
6589 if (temp && GET_CODE(temp) == CONST_INT)
6590 SUBST (XEXP (x, 0),
6591 force_to_mode (XEXP (x, 0), GET_MODE (x),
6592 INTVAL (temp), reg, next_select));
6593 }
6594 break;
6595
6596 case NEG:
6597 /* If we just want the low-order bit, the NEG isn't needed since it
6598 won't change the low-order bit. */
6599 if (mask == 1)
6600 return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
6601
6602 /* We need any bits less significant than the most significant bit in
6603 MASK since carries from those bits will affect the bits we are
6604 interested in. */
6605 mask = fuller_mask;
6606 goto unop;
6607
6608 case NOT:
6609 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
6610 same as the XOR case above. Ensure that the constant we form is not
6611 wider than the mode of X. */
6612
6613 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6614 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6615 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6616 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
6617 < GET_MODE_BITSIZE (GET_MODE (x)))
6618 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
6619 {
6620 temp = GEN_INT (mask << INTVAL (XEXP (XEXP (x, 0), 1)));
6621 temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
6622 x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
6623
6624 return force_to_mode (x, mode, mask, reg, next_select);
6625 }
6626
6627 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
6628 use the full mask inside the NOT. */
6629 mask = fuller_mask;
6630
6631 unop:
6632 op0 = gen_lowpart_for_combine (op_mode,
6633 force_to_mode (XEXP (x, 0), mode, mask,
6634 reg, next_select));
6635 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6636 x = gen_unary (code, op_mode, op_mode, op0);
6637 break;
6638
6639 case NE:
6640 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
6641 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
6642 which is equal to STORE_FLAG_VALUE. */
6643 if ((mask & ~ STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
6644 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
6645 && nonzero_bits (XEXP (x, 0), mode) == STORE_FLAG_VALUE)
6646 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6647
6648 break;
6649
6650 case IF_THEN_ELSE:
6651 /* We have no way of knowing if the IF_THEN_ELSE can itself be
6652 written in a narrower mode. We play it safe and do not do so. */
6653
6654 SUBST (XEXP (x, 1),
6655 gen_lowpart_for_combine (GET_MODE (x),
6656 force_to_mode (XEXP (x, 1), mode,
6657 mask, reg, next_select)));
6658 SUBST (XEXP (x, 2),
6659 gen_lowpart_for_combine (GET_MODE (x),
6660 force_to_mode (XEXP (x, 2), mode,
6661 mask, reg,next_select)));
6662 break;
6663
6664 default:
6665 break;
6666 }
6667
6668 /* Ensure we return a value of the proper mode. */
6669 return gen_lowpart_for_combine (mode, x);
6670 }
6671 \f
6672 /* Return nonzero if X is an expression that has one of two values depending on
6673 whether some other value is zero or nonzero. In that case, we return the
6674 value that is being tested, *PTRUE is set to the value if the rtx being
6675 returned has a nonzero value, and *PFALSE is set to the other alternative.
6676
6677 If we return zero, we set *PTRUE and *PFALSE to X. */
6678
6679 static rtx
6680 if_then_else_cond (x, ptrue, pfalse)
6681 rtx x;
6682 rtx *ptrue, *pfalse;
6683 {
6684 enum machine_mode mode = GET_MODE (x);
6685 enum rtx_code code = GET_CODE (x);
6686 int size = GET_MODE_BITSIZE (mode);
6687 rtx cond0, cond1, true0, true1, false0, false1;
6688 unsigned HOST_WIDE_INT nz;
6689
6690 /* If this is a unary operation whose operand has one of two values, apply
6691 our opcode to compute those values. */
6692 if (GET_RTX_CLASS (code) == '1'
6693 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
6694 {
6695 *ptrue = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), true0);
6696 *pfalse = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), false0);
6697 return cond0;
6698 }
6699
6700 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
6701 make can't possibly match and would suppress other optimizations. */
6702 else if (code == COMPARE)
6703 ;
6704
6705 /* If this is a binary operation, see if either side has only one of two
6706 values. If either one does or if both do and they are conditional on
6707 the same value, compute the new true and false values. */
6708 else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
6709 || GET_RTX_CLASS (code) == '<')
6710 {
6711 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
6712 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
6713
6714 if ((cond0 != 0 || cond1 != 0)
6715 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
6716 {
6717 /* If if_then_else_cond returned zero, then true/false are the
6718 same rtl. We must copy one of them to prevent invalid rtl
6719 sharing. */
6720 if (cond0 == 0)
6721 true0 = copy_rtx (true0);
6722 else if (cond1 == 0)
6723 true1 = copy_rtx (true1);
6724
6725 *ptrue = gen_binary (code, mode, true0, true1);
6726 *pfalse = gen_binary (code, mode, false0, false1);
6727 return cond0 ? cond0 : cond1;
6728 }
6729
6730 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
6731 operands is zero when the other is non-zero, and vice-versa,
6732 and STORE_FLAG_VALUE is 1 or -1. */
6733
6734 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6735 && (code == PLUS || code == IOR || code == XOR || code == MINUS
6736 || code == UMAX)
6737 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6738 {
6739 rtx op0 = XEXP (XEXP (x, 0), 1);
6740 rtx op1 = XEXP (XEXP (x, 1), 1);
6741
6742 cond0 = XEXP (XEXP (x, 0), 0);
6743 cond1 = XEXP (XEXP (x, 1), 0);
6744
6745 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6746 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6747 && reversible_comparison_p (cond1)
6748 && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6749 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6750 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6751 || ((swap_condition (GET_CODE (cond0))
6752 == reverse_condition (GET_CODE (cond1)))
6753 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6754 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6755 && ! side_effects_p (x))
6756 {
6757 *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
6758 *pfalse = gen_binary (MULT, mode,
6759 (code == MINUS
6760 ? gen_unary (NEG, mode, mode, op1) : op1),
6761 const_true_rtx);
6762 return cond0;
6763 }
6764 }
6765
6766 /* Similarly for MULT, AND and UMIN, execpt that for these the result
6767 is always zero. */
6768 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6769 && (code == MULT || code == AND || code == UMIN)
6770 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6771 {
6772 cond0 = XEXP (XEXP (x, 0), 0);
6773 cond1 = XEXP (XEXP (x, 1), 0);
6774
6775 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6776 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6777 && reversible_comparison_p (cond1)
6778 && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6779 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6780 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6781 || ((swap_condition (GET_CODE (cond0))
6782 == reverse_condition (GET_CODE (cond1)))
6783 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6784 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6785 && ! side_effects_p (x))
6786 {
6787 *ptrue = *pfalse = const0_rtx;
6788 return cond0;
6789 }
6790 }
6791 }
6792
6793 else if (code == IF_THEN_ELSE)
6794 {
6795 /* If we have IF_THEN_ELSE already, extract the condition and
6796 canonicalize it if it is NE or EQ. */
6797 cond0 = XEXP (x, 0);
6798 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
6799 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
6800 return XEXP (cond0, 0);
6801 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
6802 {
6803 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
6804 return XEXP (cond0, 0);
6805 }
6806 else
6807 return cond0;
6808 }
6809
6810 /* If X is a normal SUBREG with both inner and outer modes integral,
6811 we can narrow both the true and false values of the inner expression,
6812 if there is a condition. */
6813 else if (code == SUBREG && GET_MODE_CLASS (mode) == MODE_INT
6814 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT
6815 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
6816 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
6817 &true0, &false0)))
6818 {
6819 *ptrue = force_to_mode (true0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
6820 *pfalse
6821 = force_to_mode (false0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
6822
6823 return cond0;
6824 }
6825
6826 /* If X is a constant, this isn't special and will cause confusions
6827 if we treat it as such. Likewise if it is equivalent to a constant. */
6828 else if (CONSTANT_P (x)
6829 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
6830 ;
6831
6832 /* If X is known to be either 0 or -1, those are the true and
6833 false values when testing X. */
6834 else if (num_sign_bit_copies (x, mode) == size)
6835 {
6836 *ptrue = constm1_rtx, *pfalse = const0_rtx;
6837 return x;
6838 }
6839
6840 /* Likewise for 0 or a single bit. */
6841 else if (exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
6842 {
6843 *ptrue = GEN_INT (nz), *pfalse = const0_rtx;
6844 return x;
6845 }
6846
6847 /* Otherwise fail; show no condition with true and false values the same. */
6848 *ptrue = *pfalse = x;
6849 return 0;
6850 }
6851 \f
6852 /* Return the value of expression X given the fact that condition COND
6853 is known to be true when applied to REG as its first operand and VAL
6854 as its second. X is known to not be shared and so can be modified in
6855 place.
6856
6857 We only handle the simplest cases, and specifically those cases that
6858 arise with IF_THEN_ELSE expressions. */
6859
6860 static rtx
6861 known_cond (x, cond, reg, val)
6862 rtx x;
6863 enum rtx_code cond;
6864 rtx reg, val;
6865 {
6866 enum rtx_code code = GET_CODE (x);
6867 rtx temp;
6868 char *fmt;
6869 int i, j;
6870
6871 if (side_effects_p (x))
6872 return x;
6873
6874 if (cond == EQ && rtx_equal_p (x, reg))
6875 return val;
6876
6877 /* If X is (abs REG) and we know something about REG's relationship
6878 with zero, we may be able to simplify this. */
6879
6880 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
6881 switch (cond)
6882 {
6883 case GE: case GT: case EQ:
6884 return XEXP (x, 0);
6885 case LT: case LE:
6886 return gen_unary (NEG, GET_MODE (XEXP (x, 0)), GET_MODE (XEXP (x, 0)),
6887 XEXP (x, 0));
6888 default:
6889 break;
6890 }
6891
6892 /* The only other cases we handle are MIN, MAX, and comparisons if the
6893 operands are the same as REG and VAL. */
6894
6895 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
6896 {
6897 if (rtx_equal_p (XEXP (x, 0), val))
6898 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
6899
6900 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
6901 {
6902 if (GET_RTX_CLASS (code) == '<')
6903 return (comparison_dominates_p (cond, code) ? const_true_rtx
6904 : (comparison_dominates_p (cond,
6905 reverse_condition (code))
6906 ? const0_rtx : x));
6907
6908 else if (code == SMAX || code == SMIN
6909 || code == UMIN || code == UMAX)
6910 {
6911 int unsignedp = (code == UMIN || code == UMAX);
6912
6913 if (code == SMAX || code == UMAX)
6914 cond = reverse_condition (cond);
6915
6916 switch (cond)
6917 {
6918 case GE: case GT:
6919 return unsignedp ? x : XEXP (x, 1);
6920 case LE: case LT:
6921 return unsignedp ? x : XEXP (x, 0);
6922 case GEU: case GTU:
6923 return unsignedp ? XEXP (x, 1) : x;
6924 case LEU: case LTU:
6925 return unsignedp ? XEXP (x, 0) : x;
6926 default:
6927 break;
6928 }
6929 }
6930 }
6931 }
6932
6933 fmt = GET_RTX_FORMAT (code);
6934 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6935 {
6936 if (fmt[i] == 'e')
6937 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
6938 else if (fmt[i] == 'E')
6939 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6940 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
6941 cond, reg, val));
6942 }
6943
6944 return x;
6945 }
6946 \f
6947 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
6948 assignment as a field assignment. */
6949
6950 static int
6951 rtx_equal_for_field_assignment_p (x, y)
6952 rtx x;
6953 rtx y;
6954 {
6955 rtx last_x, last_y;
6956
6957 if (x == y || rtx_equal_p (x, y))
6958 return 1;
6959
6960 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
6961 return 0;
6962
6963 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
6964 Note that all SUBREGs of MEM are paradoxical; otherwise they
6965 would have been rewritten. */
6966 if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
6967 && GET_CODE (SUBREG_REG (y)) == MEM
6968 && rtx_equal_p (SUBREG_REG (y),
6969 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
6970 return 1;
6971
6972 if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
6973 && GET_CODE (SUBREG_REG (x)) == MEM
6974 && rtx_equal_p (SUBREG_REG (x),
6975 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
6976 return 1;
6977
6978 last_x = get_last_value (x);
6979 last_y = get_last_value (y);
6980
6981 return ((last_x != 0
6982 && GET_CODE (last_x) != CLOBBER
6983 && rtx_equal_for_field_assignment_p (last_x, y))
6984 || (last_y != 0
6985 && GET_CODE (last_y) != CLOBBER
6986 && rtx_equal_for_field_assignment_p (x, last_y))
6987 || (last_x != 0 && last_y != 0
6988 && GET_CODE (last_x) != CLOBBER
6989 && GET_CODE (last_y) != CLOBBER
6990 && rtx_equal_for_field_assignment_p (last_x, last_y)));
6991 }
6992 \f
6993 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
6994 Return that assignment if so.
6995
6996 We only handle the most common cases. */
6997
6998 static rtx
6999 make_field_assignment (x)
7000 rtx x;
7001 {
7002 rtx dest = SET_DEST (x);
7003 rtx src = SET_SRC (x);
7004 rtx assign;
7005 rtx rhs, lhs;
7006 HOST_WIDE_INT c1;
7007 int pos, len;
7008 rtx other;
7009 enum machine_mode mode;
7010
7011 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7012 a clear of a one-bit field. We will have changed it to
7013 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
7014 for a SUBREG. */
7015
7016 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7017 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7018 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7019 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7020 {
7021 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7022 1, 1, 1, 0);
7023 if (assign != 0)
7024 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7025 return x;
7026 }
7027
7028 else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7029 && subreg_lowpart_p (XEXP (src, 0))
7030 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7031 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7032 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7033 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7034 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7035 {
7036 assign = make_extraction (VOIDmode, dest, 0,
7037 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7038 1, 1, 1, 0);
7039 if (assign != 0)
7040 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7041 return x;
7042 }
7043
7044 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7045 one-bit field. */
7046 else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7047 && XEXP (XEXP (src, 0), 0) == const1_rtx
7048 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7049 {
7050 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7051 1, 1, 1, 0);
7052 if (assign != 0)
7053 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7054 return x;
7055 }
7056
7057 /* The other case we handle is assignments into a constant-position
7058 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
7059 a mask that has all one bits except for a group of zero bits and
7060 OTHER is known to have zeros where C1 has ones, this is such an
7061 assignment. Compute the position and length from C1. Shift OTHER
7062 to the appropriate position, force it to the required mode, and
7063 make the extraction. Check for the AND in both operands. */
7064
7065 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7066 return x;
7067
7068 rhs = expand_compound_operation (XEXP (src, 0));
7069 lhs = expand_compound_operation (XEXP (src, 1));
7070
7071 if (GET_CODE (rhs) == AND
7072 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7073 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7074 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7075 else if (GET_CODE (lhs) == AND
7076 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7077 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7078 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7079 else
7080 return x;
7081
7082 pos = get_pos_from_mask ((~ c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7083 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7084 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7085 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7086 return x;
7087
7088 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7089 if (assign == 0)
7090 return x;
7091
7092 /* The mode to use for the source is the mode of the assignment, or of
7093 what is inside a possible STRICT_LOW_PART. */
7094 mode = (GET_CODE (assign) == STRICT_LOW_PART
7095 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7096
7097 /* Shift OTHER right POS places and make it the source, restricting it
7098 to the proper length and mode. */
7099
7100 src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7101 GET_MODE (src), other, pos),
7102 mode,
7103 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7104 ? GET_MODE_MASK (mode)
7105 : ((HOST_WIDE_INT) 1 << len) - 1,
7106 dest, 0);
7107
7108 return gen_rtx_combine (SET, VOIDmode, assign, src);
7109 }
7110 \f
7111 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7112 if so. */
7113
7114 static rtx
7115 apply_distributive_law (x)
7116 rtx x;
7117 {
7118 enum rtx_code code = GET_CODE (x);
7119 rtx lhs, rhs, other;
7120 rtx tem;
7121 enum rtx_code inner_code;
7122
7123 /* Distributivity is not true for floating point.
7124 It can change the value. So don't do it.
7125 -- rms and moshier@world.std.com. */
7126 if (FLOAT_MODE_P (GET_MODE (x)))
7127 return x;
7128
7129 /* The outer operation can only be one of the following: */
7130 if (code != IOR && code != AND && code != XOR
7131 && code != PLUS && code != MINUS)
7132 return x;
7133
7134 lhs = XEXP (x, 0), rhs = XEXP (x, 1);
7135
7136 /* If either operand is a primitive we can't do anything, so get out
7137 fast. */
7138 if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
7139 || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
7140 return x;
7141
7142 lhs = expand_compound_operation (lhs);
7143 rhs = expand_compound_operation (rhs);
7144 inner_code = GET_CODE (lhs);
7145 if (inner_code != GET_CODE (rhs))
7146 return x;
7147
7148 /* See if the inner and outer operations distribute. */
7149 switch (inner_code)
7150 {
7151 case LSHIFTRT:
7152 case ASHIFTRT:
7153 case AND:
7154 case IOR:
7155 /* These all distribute except over PLUS. */
7156 if (code == PLUS || code == MINUS)
7157 return x;
7158 break;
7159
7160 case MULT:
7161 if (code != PLUS && code != MINUS)
7162 return x;
7163 break;
7164
7165 case ASHIFT:
7166 /* This is also a multiply, so it distributes over everything. */
7167 break;
7168
7169 case SUBREG:
7170 /* Non-paradoxical SUBREGs distributes over all operations, provided
7171 the inner modes and word numbers are the same, this is an extraction
7172 of a low-order part, we don't convert an fp operation to int or
7173 vice versa, and we would not be converting a single-word
7174 operation into a multi-word operation. The latter test is not
7175 required, but it prevents generating unneeded multi-word operations.
7176 Some of the previous tests are redundant given the latter test, but
7177 are retained because they are required for correctness.
7178
7179 We produce the result slightly differently in this case. */
7180
7181 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7182 || SUBREG_WORD (lhs) != SUBREG_WORD (rhs)
7183 || ! subreg_lowpart_p (lhs)
7184 || (GET_MODE_CLASS (GET_MODE (lhs))
7185 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7186 || (GET_MODE_SIZE (GET_MODE (lhs))
7187 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7188 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
7189 return x;
7190
7191 tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7192 SUBREG_REG (lhs), SUBREG_REG (rhs));
7193 return gen_lowpart_for_combine (GET_MODE (x), tem);
7194
7195 default:
7196 return x;
7197 }
7198
7199 /* Set LHS and RHS to the inner operands (A and B in the example
7200 above) and set OTHER to the common operand (C in the example).
7201 These is only one way to do this unless the inner operation is
7202 commutative. */
7203 if (GET_RTX_CLASS (inner_code) == 'c'
7204 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7205 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7206 else if (GET_RTX_CLASS (inner_code) == 'c'
7207 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7208 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7209 else if (GET_RTX_CLASS (inner_code) == 'c'
7210 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7211 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7212 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7213 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7214 else
7215 return x;
7216
7217 /* Form the new inner operation, seeing if it simplifies first. */
7218 tem = gen_binary (code, GET_MODE (x), lhs, rhs);
7219
7220 /* There is one exception to the general way of distributing:
7221 (a ^ b) | (a ^ c) -> (~a) & (b ^ c) */
7222 if (code == XOR && inner_code == IOR)
7223 {
7224 inner_code = AND;
7225 other = gen_unary (NOT, GET_MODE (x), GET_MODE (x), other);
7226 }
7227
7228 /* We may be able to continuing distributing the result, so call
7229 ourselves recursively on the inner operation before forming the
7230 outer operation, which we return. */
7231 return gen_binary (inner_code, GET_MODE (x),
7232 apply_distributive_law (tem), other);
7233 }
7234 \f
7235 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7236 in MODE.
7237
7238 Return an equivalent form, if different from X. Otherwise, return X. If
7239 X is zero, we are to always construct the equivalent form. */
7240
7241 static rtx
7242 simplify_and_const_int (x, mode, varop, constop)
7243 rtx x;
7244 enum machine_mode mode;
7245 rtx varop;
7246 unsigned HOST_WIDE_INT constop;
7247 {
7248 unsigned HOST_WIDE_INT nonzero;
7249 int width = GET_MODE_BITSIZE (mode);
7250 int i;
7251
7252 /* Simplify VAROP knowing that we will be only looking at some of the
7253 bits in it. */
7254 varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
7255
7256 /* If VAROP is a CLOBBER, we will fail so return it; if it is a
7257 CONST_INT, we are done. */
7258 if (GET_CODE (varop) == CLOBBER || GET_CODE (varop) == CONST_INT)
7259 return varop;
7260
7261 /* See what bits may be nonzero in VAROP. Unlike the general case of
7262 a call to nonzero_bits, here we don't care about bits outside
7263 MODE. */
7264
7265 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
7266
7267 /* If this would be an entire word for the target, but is not for
7268 the host, then sign-extend on the host so that the number will look
7269 the same way on the host that it would on the target.
7270
7271 For example, when building a 64 bit alpha hosted 32 bit sparc
7272 targeted compiler, then we want the 32 bit unsigned value -1 to be
7273 represented as a 64 bit value -1, and not as 0x00000000ffffffff.
7274 The later confuses the sparc backend. */
7275
7276 if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
7277 && (nonzero & ((HOST_WIDE_INT) 1 << (width - 1))))
7278 nonzero |= ((HOST_WIDE_INT) (-1) << width);
7279
7280 /* Turn off all bits in the constant that are known to already be zero.
7281 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
7282 which is tested below. */
7283
7284 constop &= nonzero;
7285
7286 /* If we don't have any bits left, return zero. */
7287 if (constop == 0)
7288 return const0_rtx;
7289
7290 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7291 a power of two, we can replace this with a ASHIFT. */
7292 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
7293 && (i = exact_log2 (constop)) >= 0)
7294 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
7295
7296 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
7297 or XOR, then try to apply the distributive law. This may eliminate
7298 operations if either branch can be simplified because of the AND.
7299 It may also make some cases more complex, but those cases probably
7300 won't match a pattern either with or without this. */
7301
7302 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
7303 return
7304 gen_lowpart_for_combine
7305 (mode,
7306 apply_distributive_law
7307 (gen_binary (GET_CODE (varop), GET_MODE (varop),
7308 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7309 XEXP (varop, 0), constop),
7310 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7311 XEXP (varop, 1), constop))));
7312
7313 /* Get VAROP in MODE. Try to get a SUBREG if not. Don't make a new SUBREG
7314 if we already had one (just check for the simplest cases). */
7315 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
7316 && GET_MODE (XEXP (x, 0)) == mode
7317 && SUBREG_REG (XEXP (x, 0)) == varop)
7318 varop = XEXP (x, 0);
7319 else
7320 varop = gen_lowpart_for_combine (mode, varop);
7321
7322 /* If we can't make the SUBREG, try to return what we were given. */
7323 if (GET_CODE (varop) == CLOBBER)
7324 return x ? x : varop;
7325
7326 /* If we are only masking insignificant bits, return VAROP. */
7327 if (constop == nonzero)
7328 x = varop;
7329
7330 /* Otherwise, return an AND. See how much, if any, of X we can use. */
7331 else if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
7332 x = gen_binary (AND, mode, varop, GEN_INT (constop));
7333
7334 else
7335 {
7336 if (GET_CODE (XEXP (x, 1)) != CONST_INT
7337 || INTVAL (XEXP (x, 1)) != constop)
7338 SUBST (XEXP (x, 1), GEN_INT (constop));
7339
7340 SUBST (XEXP (x, 0), varop);
7341 }
7342
7343 return x;
7344 }
7345 \f
7346 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
7347 We don't let nonzero_bits recur into num_sign_bit_copies, because that
7348 is less useful. We can't allow both, because that results in exponential
7349 run time recursion. There is a nullstone testcase that triggered
7350 this. This macro avoids accidental uses of num_sign_bit_copies. */
7351 #define num_sign_bit_copies()
7352
7353 /* Given an expression, X, compute which bits in X can be non-zero.
7354 We don't care about bits outside of those defined in MODE.
7355
7356 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
7357 a shift, AND, or zero_extract, we can do better. */
7358
7359 static unsigned HOST_WIDE_INT
7360 nonzero_bits (x, mode)
7361 rtx x;
7362 enum machine_mode mode;
7363 {
7364 unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
7365 unsigned HOST_WIDE_INT inner_nz;
7366 enum rtx_code code;
7367 int mode_width = GET_MODE_BITSIZE (mode);
7368 rtx tem;
7369
7370 /* For floating-point values, assume all bits are needed. */
7371 if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
7372 return nonzero;
7373
7374 /* If X is wider than MODE, use its mode instead. */
7375 if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
7376 {
7377 mode = GET_MODE (x);
7378 nonzero = GET_MODE_MASK (mode);
7379 mode_width = GET_MODE_BITSIZE (mode);
7380 }
7381
7382 if (mode_width > HOST_BITS_PER_WIDE_INT)
7383 /* Our only callers in this case look for single bit values. So
7384 just return the mode mask. Those tests will then be false. */
7385 return nonzero;
7386
7387 #ifndef WORD_REGISTER_OPERATIONS
7388 /* If MODE is wider than X, but both are a single word for both the host
7389 and target machines, we can compute this from which bits of the
7390 object might be nonzero in its own mode, taking into account the fact
7391 that on many CISC machines, accessing an object in a wider mode
7392 causes the high-order bits to become undefined. So they are
7393 not known to be zero. */
7394
7395 if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
7396 && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
7397 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7398 && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
7399 {
7400 nonzero &= nonzero_bits (x, GET_MODE (x));
7401 nonzero |= GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x));
7402 return nonzero;
7403 }
7404 #endif
7405
7406 code = GET_CODE (x);
7407 switch (code)
7408 {
7409 case REG:
7410 #ifdef POINTERS_EXTEND_UNSIGNED
7411 /* If pointers extend unsigned and this is a pointer in Pmode, say that
7412 all the bits above ptr_mode are known to be zero. */
7413 if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
7414 && REGNO_POINTER_FLAG (REGNO (x)))
7415 nonzero &= GET_MODE_MASK (ptr_mode);
7416 #endif
7417
7418 #ifdef STACK_BOUNDARY
7419 /* If this is the stack pointer, we may know something about its
7420 alignment. If PUSH_ROUNDING is defined, it is possible for the
7421 stack to be momentarily aligned only to that amount, so we pick
7422 the least alignment. */
7423
7424 /* We can't check for arg_pointer_rtx here, because it is not
7425 guaranteed to have as much alignment as the stack pointer.
7426 In particular, in the Irix6 n64 ABI, the stack has 128 bit
7427 alignment but the argument pointer has only 64 bit alignment. */
7428
7429 if ((x == frame_pointer_rtx
7430 || x == stack_pointer_rtx
7431 || x == hard_frame_pointer_rtx
7432 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
7433 && REGNO (x) <= LAST_VIRTUAL_REGISTER))
7434 #ifdef STACK_BIAS
7435 && !STACK_BIAS
7436 #endif
7437 )
7438 {
7439 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
7440
7441 #ifdef PUSH_ROUNDING
7442 if (REGNO (x) == STACK_POINTER_REGNUM)
7443 sp_alignment = MIN (PUSH_ROUNDING (1), sp_alignment);
7444 #endif
7445
7446 /* We must return here, otherwise we may get a worse result from
7447 one of the choices below. There is nothing useful below as
7448 far as the stack pointer is concerned. */
7449 return nonzero &= ~ (sp_alignment - 1);
7450 }
7451 #endif
7452
7453 /* If X is a register whose nonzero bits value is current, use it.
7454 Otherwise, if X is a register whose value we can find, use that
7455 value. Otherwise, use the previously-computed global nonzero bits
7456 for this register. */
7457
7458 if (reg_last_set_value[REGNO (x)] != 0
7459 && reg_last_set_mode[REGNO (x)] == mode
7460 && (REG_N_SETS (REGNO (x)) == 1
7461 || reg_last_set_label[REGNO (x)] == label_tick)
7462 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7463 return reg_last_set_nonzero_bits[REGNO (x)];
7464
7465 tem = get_last_value (x);
7466
7467 if (tem)
7468 {
7469 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7470 /* If X is narrower than MODE and TEM is a non-negative
7471 constant that would appear negative in the mode of X,
7472 sign-extend it for use in reg_nonzero_bits because some
7473 machines (maybe most) will actually do the sign-extension
7474 and this is the conservative approach.
7475
7476 ??? For 2.5, try to tighten up the MD files in this regard
7477 instead of this kludge. */
7478
7479 if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
7480 && GET_CODE (tem) == CONST_INT
7481 && INTVAL (tem) > 0
7482 && 0 != (INTVAL (tem)
7483 & ((HOST_WIDE_INT) 1
7484 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7485 tem = GEN_INT (INTVAL (tem)
7486 | ((HOST_WIDE_INT) (-1)
7487 << GET_MODE_BITSIZE (GET_MODE (x))));
7488 #endif
7489 return nonzero_bits (tem, mode);
7490 }
7491 else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
7492 return reg_nonzero_bits[REGNO (x)] & nonzero;
7493 else
7494 return nonzero;
7495
7496 case CONST_INT:
7497 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7498 /* If X is negative in MODE, sign-extend the value. */
7499 if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
7500 && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
7501 return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
7502 #endif
7503
7504 return INTVAL (x);
7505
7506 case MEM:
7507 #ifdef LOAD_EXTEND_OP
7508 /* In many, if not most, RISC machines, reading a byte from memory
7509 zeros the rest of the register. Noticing that fact saves a lot
7510 of extra zero-extends. */
7511 if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
7512 nonzero &= GET_MODE_MASK (GET_MODE (x));
7513 #endif
7514 break;
7515
7516 case EQ: case NE:
7517 case GT: case GTU:
7518 case LT: case LTU:
7519 case GE: case GEU:
7520 case LE: case LEU:
7521
7522 /* If this produces an integer result, we know which bits are set.
7523 Code here used to clear bits outside the mode of X, but that is
7524 now done above. */
7525
7526 if (GET_MODE_CLASS (mode) == MODE_INT
7527 && mode_width <= HOST_BITS_PER_WIDE_INT)
7528 nonzero = STORE_FLAG_VALUE;
7529 break;
7530
7531 case NEG:
7532 #if 0
7533 /* Disabled to avoid exponential mutual recursion between nonzero_bits
7534 and num_sign_bit_copies. */
7535 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7536 == GET_MODE_BITSIZE (GET_MODE (x)))
7537 nonzero = 1;
7538 #endif
7539
7540 if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
7541 nonzero |= (GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x)));
7542 break;
7543
7544 case ABS:
7545 #if 0
7546 /* Disabled to avoid exponential mutual recursion between nonzero_bits
7547 and num_sign_bit_copies. */
7548 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7549 == GET_MODE_BITSIZE (GET_MODE (x)))
7550 nonzero = 1;
7551 #endif
7552 break;
7553
7554 case TRUNCATE:
7555 nonzero &= (nonzero_bits (XEXP (x, 0), mode) & GET_MODE_MASK (mode));
7556 break;
7557
7558 case ZERO_EXTEND:
7559 nonzero &= nonzero_bits (XEXP (x, 0), mode);
7560 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7561 nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7562 break;
7563
7564 case SIGN_EXTEND:
7565 /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
7566 Otherwise, show all the bits in the outer mode but not the inner
7567 may be non-zero. */
7568 inner_nz = nonzero_bits (XEXP (x, 0), mode);
7569 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7570 {
7571 inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7572 if (inner_nz
7573 & (((HOST_WIDE_INT) 1
7574 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
7575 inner_nz |= (GET_MODE_MASK (mode)
7576 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
7577 }
7578
7579 nonzero &= inner_nz;
7580 break;
7581
7582 case AND:
7583 nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7584 & nonzero_bits (XEXP (x, 1), mode));
7585 break;
7586
7587 case XOR: case IOR:
7588 case UMIN: case UMAX: case SMIN: case SMAX:
7589 nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7590 | nonzero_bits (XEXP (x, 1), mode));
7591 break;
7592
7593 case PLUS: case MINUS:
7594 case MULT:
7595 case DIV: case UDIV:
7596 case MOD: case UMOD:
7597 /* We can apply the rules of arithmetic to compute the number of
7598 high- and low-order zero bits of these operations. We start by
7599 computing the width (position of the highest-order non-zero bit)
7600 and the number of low-order zero bits for each value. */
7601 {
7602 unsigned HOST_WIDE_INT nz0 = nonzero_bits (XEXP (x, 0), mode);
7603 unsigned HOST_WIDE_INT nz1 = nonzero_bits (XEXP (x, 1), mode);
7604 int width0 = floor_log2 (nz0) + 1;
7605 int width1 = floor_log2 (nz1) + 1;
7606 int low0 = floor_log2 (nz0 & -nz0);
7607 int low1 = floor_log2 (nz1 & -nz1);
7608 HOST_WIDE_INT op0_maybe_minusp
7609 = (nz0 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7610 HOST_WIDE_INT op1_maybe_minusp
7611 = (nz1 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7612 int result_width = mode_width;
7613 int result_low = 0;
7614
7615 switch (code)
7616 {
7617 case PLUS:
7618 #ifdef STACK_BIAS
7619 if (STACK_BIAS
7620 && (XEXP (x, 0) == stack_pointer_rtx
7621 || XEXP (x, 0) == frame_pointer_rtx)
7622 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7623 {
7624 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
7625
7626 nz0 = (GET_MODE_MASK (mode) & ~ (sp_alignment - 1));
7627 nz1 = INTVAL (XEXP (x, 1)) - STACK_BIAS;
7628 width0 = floor_log2 (nz0) + 1;
7629 width1 = floor_log2 (nz1) + 1;
7630 low0 = floor_log2 (nz0 & -nz0);
7631 low1 = floor_log2 (nz1 & -nz1);
7632 }
7633 #endif
7634 result_width = MAX (width0, width1) + 1;
7635 result_low = MIN (low0, low1);
7636 break;
7637 case MINUS:
7638 result_low = MIN (low0, low1);
7639 break;
7640 case MULT:
7641 result_width = width0 + width1;
7642 result_low = low0 + low1;
7643 break;
7644 case DIV:
7645 if (! op0_maybe_minusp && ! op1_maybe_minusp)
7646 result_width = width0;
7647 break;
7648 case UDIV:
7649 result_width = width0;
7650 break;
7651 case MOD:
7652 if (! op0_maybe_minusp && ! op1_maybe_minusp)
7653 result_width = MIN (width0, width1);
7654 result_low = MIN (low0, low1);
7655 break;
7656 case UMOD:
7657 result_width = MIN (width0, width1);
7658 result_low = MIN (low0, low1);
7659 break;
7660 default:
7661 abort ();
7662 }
7663
7664 if (result_width < mode_width)
7665 nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
7666
7667 if (result_low > 0)
7668 nonzero &= ~ (((HOST_WIDE_INT) 1 << result_low) - 1);
7669 }
7670 break;
7671
7672 case ZERO_EXTRACT:
7673 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7674 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7675 nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
7676 break;
7677
7678 case SUBREG:
7679 /* If this is a SUBREG formed for a promoted variable that has
7680 been zero-extended, we know that at least the high-order bits
7681 are zero, though others might be too. */
7682
7683 if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x))
7684 nonzero = (GET_MODE_MASK (GET_MODE (x))
7685 & nonzero_bits (SUBREG_REG (x), GET_MODE (x)));
7686
7687 /* If the inner mode is a single word for both the host and target
7688 machines, we can compute this from which bits of the inner
7689 object might be nonzero. */
7690 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
7691 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7692 <= HOST_BITS_PER_WIDE_INT))
7693 {
7694 nonzero &= nonzero_bits (SUBREG_REG (x), mode);
7695
7696 #ifndef WORD_REGISTER_OPERATIONS
7697 /* On many CISC machines, accessing an object in a wider mode
7698 causes the high-order bits to become undefined. So they are
7699 not known to be zero. */
7700 if (GET_MODE_SIZE (GET_MODE (x))
7701 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7702 nonzero |= (GET_MODE_MASK (GET_MODE (x))
7703 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
7704 #endif
7705 }
7706 break;
7707
7708 case ASHIFTRT:
7709 case LSHIFTRT:
7710 case ASHIFT:
7711 case ROTATE:
7712 /* The nonzero bits are in two classes: any bits within MODE
7713 that aren't in GET_MODE (x) are always significant. The rest of the
7714 nonzero bits are those that are significant in the operand of
7715 the shift when shifted the appropriate number of bits. This
7716 shows that high-order bits are cleared by the right shift and
7717 low-order bits by left shifts. */
7718 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7719 && INTVAL (XEXP (x, 1)) >= 0
7720 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7721 {
7722 enum machine_mode inner_mode = GET_MODE (x);
7723 int width = GET_MODE_BITSIZE (inner_mode);
7724 int count = INTVAL (XEXP (x, 1));
7725 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
7726 unsigned HOST_WIDE_INT op_nonzero = nonzero_bits (XEXP (x, 0), mode);
7727 unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
7728 unsigned HOST_WIDE_INT outer = 0;
7729
7730 if (mode_width > width)
7731 outer = (op_nonzero & nonzero & ~ mode_mask);
7732
7733 if (code == LSHIFTRT)
7734 inner >>= count;
7735 else if (code == ASHIFTRT)
7736 {
7737 inner >>= count;
7738
7739 /* If the sign bit may have been nonzero before the shift, we
7740 need to mark all the places it could have been copied to
7741 by the shift as possibly nonzero. */
7742 if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
7743 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
7744 }
7745 else if (code == ASHIFT)
7746 inner <<= count;
7747 else
7748 inner = ((inner << (count % width)
7749 | (inner >> (width - (count % width)))) & mode_mask);
7750
7751 nonzero &= (outer | inner);
7752 }
7753 break;
7754
7755 case FFS:
7756 /* This is at most the number of bits in the mode. */
7757 nonzero = ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width) + 1)) - 1;
7758 break;
7759
7760 case IF_THEN_ELSE:
7761 nonzero &= (nonzero_bits (XEXP (x, 1), mode)
7762 | nonzero_bits (XEXP (x, 2), mode));
7763 break;
7764
7765 default:
7766 break;
7767 }
7768
7769 return nonzero;
7770 }
7771
7772 /* See the macro definition above. */
7773 #undef num_sign_bit_copies
7774 \f
7775 /* Return the number of bits at the high-order end of X that are known to
7776 be equal to the sign bit. X will be used in mode MODE; if MODE is
7777 VOIDmode, X will be used in its own mode. The returned value will always
7778 be between 1 and the number of bits in MODE. */
7779
7780 static int
7781 num_sign_bit_copies (x, mode)
7782 rtx x;
7783 enum machine_mode mode;
7784 {
7785 enum rtx_code code = GET_CODE (x);
7786 int bitwidth;
7787 int num0, num1, result;
7788 unsigned HOST_WIDE_INT nonzero;
7789 rtx tem;
7790
7791 /* If we weren't given a mode, use the mode of X. If the mode is still
7792 VOIDmode, we don't know anything. Likewise if one of the modes is
7793 floating-point. */
7794
7795 if (mode == VOIDmode)
7796 mode = GET_MODE (x);
7797
7798 if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
7799 return 1;
7800
7801 bitwidth = GET_MODE_BITSIZE (mode);
7802
7803 /* For a smaller object, just ignore the high bits. */
7804 if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
7805 return MAX (1, (num_sign_bit_copies (x, GET_MODE (x))
7806 - (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth)));
7807
7808 if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
7809 {
7810 #ifndef WORD_REGISTER_OPERATIONS
7811 /* If this machine does not do all register operations on the entire
7812 register and MODE is wider than the mode of X, we can say nothing
7813 at all about the high-order bits. */
7814 return 1;
7815 #else
7816 /* Likewise on machines that do, if the mode of the object is smaller
7817 than a word and loads of that size don't sign extend, we can say
7818 nothing about the high order bits. */
7819 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
7820 #ifdef LOAD_EXTEND_OP
7821 && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
7822 #endif
7823 )
7824 return 1;
7825 #endif
7826 }
7827
7828 switch (code)
7829 {
7830 case REG:
7831
7832 #ifdef POINTERS_EXTEND_UNSIGNED
7833 /* If pointers extend signed and this is a pointer in Pmode, say that
7834 all the bits above ptr_mode are known to be sign bit copies. */
7835 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
7836 && REGNO_POINTER_FLAG (REGNO (x)))
7837 return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
7838 #endif
7839
7840 if (reg_last_set_value[REGNO (x)] != 0
7841 && reg_last_set_mode[REGNO (x)] == mode
7842 && (REG_N_SETS (REGNO (x)) == 1
7843 || reg_last_set_label[REGNO (x)] == label_tick)
7844 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7845 return reg_last_set_sign_bit_copies[REGNO (x)];
7846
7847 tem = get_last_value (x);
7848 if (tem != 0)
7849 return num_sign_bit_copies (tem, mode);
7850
7851 if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0)
7852 return reg_sign_bit_copies[REGNO (x)];
7853 break;
7854
7855 case MEM:
7856 #ifdef LOAD_EXTEND_OP
7857 /* Some RISC machines sign-extend all loads of smaller than a word. */
7858 if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
7859 return MAX (1, bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1);
7860 #endif
7861 break;
7862
7863 case CONST_INT:
7864 /* If the constant is negative, take its 1's complement and remask.
7865 Then see how many zero bits we have. */
7866 nonzero = INTVAL (x) & GET_MODE_MASK (mode);
7867 if (bitwidth <= HOST_BITS_PER_WIDE_INT
7868 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7869 nonzero = (~ nonzero) & GET_MODE_MASK (mode);
7870
7871 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
7872
7873 case SUBREG:
7874 /* If this is a SUBREG for a promoted object that is sign-extended
7875 and we are looking at it in a wider mode, we know that at least the
7876 high-order bits are known to be sign bit copies. */
7877
7878 if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
7879 return MAX (bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1,
7880 num_sign_bit_copies (SUBREG_REG (x), mode));
7881
7882 /* For a smaller object, just ignore the high bits. */
7883 if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
7884 {
7885 num0 = num_sign_bit_copies (SUBREG_REG (x), VOIDmode);
7886 return MAX (1, (num0
7887 - (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7888 - bitwidth)));
7889 }
7890
7891 #ifdef WORD_REGISTER_OPERATIONS
7892 #ifdef LOAD_EXTEND_OP
7893 /* For paradoxical SUBREGs on machines where all register operations
7894 affect the entire register, just look inside. Note that we are
7895 passing MODE to the recursive call, so the number of sign bit copies
7896 will remain relative to that mode, not the inner mode. */
7897
7898 /* This works only if loads sign extend. Otherwise, if we get a
7899 reload for the inner part, it may be loaded from the stack, and
7900 then we lose all sign bit copies that existed before the store
7901 to the stack. */
7902
7903 if ((GET_MODE_SIZE (GET_MODE (x))
7904 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7905 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND)
7906 return num_sign_bit_copies (SUBREG_REG (x), mode);
7907 #endif
7908 #endif
7909 break;
7910
7911 case SIGN_EXTRACT:
7912 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7913 return MAX (1, bitwidth - INTVAL (XEXP (x, 1)));
7914 break;
7915
7916 case SIGN_EXTEND:
7917 return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
7918 + num_sign_bit_copies (XEXP (x, 0), VOIDmode));
7919
7920 case TRUNCATE:
7921 /* For a smaller object, just ignore the high bits. */
7922 num0 = num_sign_bit_copies (XEXP (x, 0), VOIDmode);
7923 return MAX (1, (num0 - (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
7924 - bitwidth)));
7925
7926 case NOT:
7927 return num_sign_bit_copies (XEXP (x, 0), mode);
7928
7929 case ROTATE: case ROTATERT:
7930 /* If we are rotating left by a number of bits less than the number
7931 of sign bit copies, we can just subtract that amount from the
7932 number. */
7933 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7934 && INTVAL (XEXP (x, 1)) >= 0 && INTVAL (XEXP (x, 1)) < bitwidth)
7935 {
7936 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7937 return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
7938 : bitwidth - INTVAL (XEXP (x, 1))));
7939 }
7940 break;
7941
7942 case NEG:
7943 /* In general, this subtracts one sign bit copy. But if the value
7944 is known to be positive, the number of sign bit copies is the
7945 same as that of the input. Finally, if the input has just one bit
7946 that might be nonzero, all the bits are copies of the sign bit. */
7947 nonzero = nonzero_bits (XEXP (x, 0), mode);
7948 if (nonzero == 1)
7949 return bitwidth;
7950
7951 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7952 if (num0 > 1
7953 && bitwidth <= HOST_BITS_PER_WIDE_INT
7954 && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
7955 num0--;
7956
7957 return num0;
7958
7959 case IOR: case AND: case XOR:
7960 case SMIN: case SMAX: case UMIN: case UMAX:
7961 /* Logical operations will preserve the number of sign-bit copies.
7962 MIN and MAX operations always return one of the operands. */
7963 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7964 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7965 return MIN (num0, num1);
7966
7967 case PLUS: case MINUS:
7968 /* For addition and subtraction, we can have a 1-bit carry. However,
7969 if we are subtracting 1 from a positive number, there will not
7970 be such a carry. Furthermore, if the positive number is known to
7971 be 0 or 1, we know the result is either -1 or 0. */
7972
7973 if (code == PLUS && XEXP (x, 1) == constm1_rtx
7974 && bitwidth <= HOST_BITS_PER_WIDE_INT)
7975 {
7976 nonzero = nonzero_bits (XEXP (x, 0), mode);
7977 if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
7978 return (nonzero == 1 || nonzero == 0 ? bitwidth
7979 : bitwidth - floor_log2 (nonzero) - 1);
7980 }
7981
7982 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7983 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7984 return MAX (1, MIN (num0, num1) - 1);
7985
7986 case MULT:
7987 /* The number of bits of the product is the sum of the number of
7988 bits of both terms. However, unless one of the terms if known
7989 to be positive, we must allow for an additional bit since negating
7990 a negative number can remove one sign bit copy. */
7991
7992 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7993 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7994
7995 result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
7996 if (result > 0
7997 && bitwidth <= HOST_BITS_PER_WIDE_INT
7998 && ((nonzero_bits (XEXP (x, 0), mode)
7999 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8000 && ((nonzero_bits (XEXP (x, 1), mode)
8001 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8002 result--;
8003
8004 return MAX (1, result);
8005
8006 case UDIV:
8007 /* The result must be <= the first operand. */
8008 return num_sign_bit_copies (XEXP (x, 0), mode);
8009
8010 case UMOD:
8011 /* The result must be <= the scond operand. */
8012 return num_sign_bit_copies (XEXP (x, 1), mode);
8013
8014 case DIV:
8015 /* Similar to unsigned division, except that we have to worry about
8016 the case where the divisor is negative, in which case we have
8017 to add 1. */
8018 result = num_sign_bit_copies (XEXP (x, 0), mode);
8019 if (result > 1
8020 && bitwidth <= HOST_BITS_PER_WIDE_INT
8021 && (nonzero_bits (XEXP (x, 1), mode)
8022 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8023 result --;
8024
8025 return result;
8026
8027 case MOD:
8028 result = num_sign_bit_copies (XEXP (x, 1), mode);
8029 if (result > 1
8030 && bitwidth <= HOST_BITS_PER_WIDE_INT
8031 && (nonzero_bits (XEXP (x, 1), mode)
8032 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8033 result --;
8034
8035 return result;
8036
8037 case ASHIFTRT:
8038 /* Shifts by a constant add to the number of bits equal to the
8039 sign bit. */
8040 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8041 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8042 && INTVAL (XEXP (x, 1)) > 0)
8043 num0 = MIN (bitwidth, num0 + INTVAL (XEXP (x, 1)));
8044
8045 return num0;
8046
8047 case ASHIFT:
8048 /* Left shifts destroy copies. */
8049 if (GET_CODE (XEXP (x, 1)) != CONST_INT
8050 || INTVAL (XEXP (x, 1)) < 0
8051 || INTVAL (XEXP (x, 1)) >= bitwidth)
8052 return 1;
8053
8054 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8055 return MAX (1, num0 - INTVAL (XEXP (x, 1)));
8056
8057 case IF_THEN_ELSE:
8058 num0 = num_sign_bit_copies (XEXP (x, 1), mode);
8059 num1 = num_sign_bit_copies (XEXP (x, 2), mode);
8060 return MIN (num0, num1);
8061
8062 case EQ: case NE: case GE: case GT: case LE: case LT:
8063 case GEU: case GTU: case LEU: case LTU:
8064 if (STORE_FLAG_VALUE == -1)
8065 return bitwidth;
8066 break;
8067
8068 default:
8069 break;
8070 }
8071
8072 /* If we haven't been able to figure it out by one of the above rules,
8073 see if some of the high-order bits are known to be zero. If so,
8074 count those bits and return one less than that amount. If we can't
8075 safely compute the mask for this mode, always return BITWIDTH. */
8076
8077 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8078 return 1;
8079
8080 nonzero = nonzero_bits (x, mode);
8081 return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
8082 ? 1 : bitwidth - floor_log2 (nonzero) - 1);
8083 }
8084 \f
8085 /* Return the number of "extended" bits there are in X, when interpreted
8086 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8087 unsigned quantities, this is the number of high-order zero bits.
8088 For signed quantities, this is the number of copies of the sign bit
8089 minus 1. In both case, this function returns the number of "spare"
8090 bits. For example, if two quantities for which this function returns
8091 at least 1 are added, the addition is known not to overflow.
8092
8093 This function will always return 0 unless called during combine, which
8094 implies that it must be called from a define_split. */
8095
8096 int
8097 extended_count (x, mode, unsignedp)
8098 rtx x;
8099 enum machine_mode mode;
8100 int unsignedp;
8101 {
8102 if (nonzero_sign_valid == 0)
8103 return 0;
8104
8105 return (unsignedp
8106 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8107 && (GET_MODE_BITSIZE (mode) - 1
8108 - floor_log2 (nonzero_bits (x, mode))))
8109 : num_sign_bit_copies (x, mode) - 1);
8110 }
8111 \f
8112 /* This function is called from `simplify_shift_const' to merge two
8113 outer operations. Specifically, we have already found that we need
8114 to perform operation *POP0 with constant *PCONST0 at the outermost
8115 position. We would now like to also perform OP1 with constant CONST1
8116 (with *POP0 being done last).
8117
8118 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8119 the resulting operation. *PCOMP_P is set to 1 if we would need to
8120 complement the innermost operand, otherwise it is unchanged.
8121
8122 MODE is the mode in which the operation will be done. No bits outside
8123 the width of this mode matter. It is assumed that the width of this mode
8124 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8125
8126 If *POP0 or OP1 are NIL, it means no operation is required. Only NEG, PLUS,
8127 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8128 result is simply *PCONST0.
8129
8130 If the resulting operation cannot be expressed as one operation, we
8131 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
8132
8133 static int
8134 merge_outer_ops (pop0, pconst0, op1, const1, mode, pcomp_p)
8135 enum rtx_code *pop0;
8136 HOST_WIDE_INT *pconst0;
8137 enum rtx_code op1;
8138 HOST_WIDE_INT const1;
8139 enum machine_mode mode;
8140 int *pcomp_p;
8141 {
8142 enum rtx_code op0 = *pop0;
8143 HOST_WIDE_INT const0 = *pconst0;
8144 int width = GET_MODE_BITSIZE (mode);
8145
8146 const0 &= GET_MODE_MASK (mode);
8147 const1 &= GET_MODE_MASK (mode);
8148
8149 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8150 if (op0 == AND)
8151 const1 &= const0;
8152
8153 /* If OP0 or OP1 is NIL, this is easy. Similarly if they are the same or
8154 if OP0 is SET. */
8155
8156 if (op1 == NIL || op0 == SET)
8157 return 1;
8158
8159 else if (op0 == NIL)
8160 op0 = op1, const0 = const1;
8161
8162 else if (op0 == op1)
8163 {
8164 switch (op0)
8165 {
8166 case AND:
8167 const0 &= const1;
8168 break;
8169 case IOR:
8170 const0 |= const1;
8171 break;
8172 case XOR:
8173 const0 ^= const1;
8174 break;
8175 case PLUS:
8176 const0 += const1;
8177 break;
8178 case NEG:
8179 op0 = NIL;
8180 break;
8181 default:
8182 break;
8183 }
8184 }
8185
8186 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8187 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8188 return 0;
8189
8190 /* If the two constants aren't the same, we can't do anything. The
8191 remaining six cases can all be done. */
8192 else if (const0 != const1)
8193 return 0;
8194
8195 else
8196 switch (op0)
8197 {
8198 case IOR:
8199 if (op1 == AND)
8200 /* (a & b) | b == b */
8201 op0 = SET;
8202 else /* op1 == XOR */
8203 /* (a ^ b) | b == a | b */
8204 {;}
8205 break;
8206
8207 case XOR:
8208 if (op1 == AND)
8209 /* (a & b) ^ b == (~a) & b */
8210 op0 = AND, *pcomp_p = 1;
8211 else /* op1 == IOR */
8212 /* (a | b) ^ b == a & ~b */
8213 op0 = AND, *pconst0 = ~ const0;
8214 break;
8215
8216 case AND:
8217 if (op1 == IOR)
8218 /* (a | b) & b == b */
8219 op0 = SET;
8220 else /* op1 == XOR */
8221 /* (a ^ b) & b) == (~a) & b */
8222 *pcomp_p = 1;
8223 break;
8224 default:
8225 break;
8226 }
8227
8228 /* Check for NO-OP cases. */
8229 const0 &= GET_MODE_MASK (mode);
8230 if (const0 == 0
8231 && (op0 == IOR || op0 == XOR || op0 == PLUS))
8232 op0 = NIL;
8233 else if (const0 == 0 && op0 == AND)
8234 op0 = SET;
8235 else if (const0 == GET_MODE_MASK (mode) && op0 == AND)
8236 op0 = NIL;
8237
8238 /* If this would be an entire word for the target, but is not for
8239 the host, then sign-extend on the host so that the number will look
8240 the same way on the host that it would on the target.
8241
8242 For example, when building a 64 bit alpha hosted 32 bit sparc
8243 targeted compiler, then we want the 32 bit unsigned value -1 to be
8244 represented as a 64 bit value -1, and not as 0x00000000ffffffff.
8245 The later confuses the sparc backend. */
8246
8247 if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
8248 && (const0 & ((HOST_WIDE_INT) 1 << (width - 1))))
8249 const0 |= ((HOST_WIDE_INT) (-1) << width);
8250
8251 *pop0 = op0;
8252 *pconst0 = const0;
8253
8254 return 1;
8255 }
8256 \f
8257 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8258 The result of the shift is RESULT_MODE. X, if non-zero, is an expression
8259 that we started with.
8260
8261 The shift is normally computed in the widest mode we find in VAROP, as
8262 long as it isn't a different number of words than RESULT_MODE. Exceptions
8263 are ASHIFTRT and ROTATE, which are always done in their original mode, */
8264
8265 static rtx
8266 simplify_shift_const (x, code, result_mode, varop, count)
8267 rtx x;
8268 enum rtx_code code;
8269 enum machine_mode result_mode;
8270 rtx varop;
8271 int count;
8272 {
8273 enum rtx_code orig_code = code;
8274 int orig_count = count;
8275 enum machine_mode mode = result_mode;
8276 enum machine_mode shift_mode, tmode;
8277 int mode_words
8278 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8279 /* We form (outer_op (code varop count) (outer_const)). */
8280 enum rtx_code outer_op = NIL;
8281 HOST_WIDE_INT outer_const = 0;
8282 rtx const_rtx;
8283 int complement_p = 0;
8284 rtx new;
8285
8286 /* If we were given an invalid count, don't do anything except exactly
8287 what was requested. */
8288
8289 if (count < 0 || count > GET_MODE_BITSIZE (mode))
8290 {
8291 if (x)
8292 return x;
8293
8294 return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (count));
8295 }
8296
8297 /* Unless one of the branches of the `if' in this loop does a `continue',
8298 we will `break' the loop after the `if'. */
8299
8300 while (count != 0)
8301 {
8302 /* If we have an operand of (clobber (const_int 0)), just return that
8303 value. */
8304 if (GET_CODE (varop) == CLOBBER)
8305 return varop;
8306
8307 /* If we discovered we had to complement VAROP, leave. Making a NOT
8308 here would cause an infinite loop. */
8309 if (complement_p)
8310 break;
8311
8312 /* Convert ROTATERT to ROTATE. */
8313 if (code == ROTATERT)
8314 code = ROTATE, count = GET_MODE_BITSIZE (result_mode) - count;
8315
8316 /* We need to determine what mode we will do the shift in. If the
8317 shift is a right shift or a ROTATE, we must always do it in the mode
8318 it was originally done in. Otherwise, we can do it in MODE, the
8319 widest mode encountered. */
8320 shift_mode
8321 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8322 ? result_mode : mode);
8323
8324 /* Handle cases where the count is greater than the size of the mode
8325 minus 1. For ASHIFT, use the size minus one as the count (this can
8326 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8327 take the count modulo the size. For other shifts, the result is
8328 zero.
8329
8330 Since these shifts are being produced by the compiler by combining
8331 multiple operations, each of which are defined, we know what the
8332 result is supposed to be. */
8333
8334 if (count > GET_MODE_BITSIZE (shift_mode) - 1)
8335 {
8336 if (code == ASHIFTRT)
8337 count = GET_MODE_BITSIZE (shift_mode) - 1;
8338 else if (code == ROTATE || code == ROTATERT)
8339 count %= GET_MODE_BITSIZE (shift_mode);
8340 else
8341 {
8342 /* We can't simply return zero because there may be an
8343 outer op. */
8344 varop = const0_rtx;
8345 count = 0;
8346 break;
8347 }
8348 }
8349
8350 /* Negative counts are invalid and should not have been made (a
8351 programmer-specified negative count should have been handled
8352 above). */
8353 else if (count < 0)
8354 abort ();
8355
8356 /* An arithmetic right shift of a quantity known to be -1 or 0
8357 is a no-op. */
8358 if (code == ASHIFTRT
8359 && (num_sign_bit_copies (varop, shift_mode)
8360 == GET_MODE_BITSIZE (shift_mode)))
8361 {
8362 count = 0;
8363 break;
8364 }
8365
8366 /* If we are doing an arithmetic right shift and discarding all but
8367 the sign bit copies, this is equivalent to doing a shift by the
8368 bitsize minus one. Convert it into that shift because it will often
8369 allow other simplifications. */
8370
8371 if (code == ASHIFTRT
8372 && (count + num_sign_bit_copies (varop, shift_mode)
8373 >= GET_MODE_BITSIZE (shift_mode)))
8374 count = GET_MODE_BITSIZE (shift_mode) - 1;
8375
8376 /* We simplify the tests below and elsewhere by converting
8377 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8378 `make_compound_operation' will convert it to a ASHIFTRT for
8379 those machines (such as Vax) that don't have a LSHIFTRT. */
8380 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8381 && code == ASHIFTRT
8382 && ((nonzero_bits (varop, shift_mode)
8383 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8384 == 0))
8385 code = LSHIFTRT;
8386
8387 switch (GET_CODE (varop))
8388 {
8389 case SIGN_EXTEND:
8390 case ZERO_EXTEND:
8391 case SIGN_EXTRACT:
8392 case ZERO_EXTRACT:
8393 new = expand_compound_operation (varop);
8394 if (new != varop)
8395 {
8396 varop = new;
8397 continue;
8398 }
8399 break;
8400
8401 case MEM:
8402 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8403 minus the width of a smaller mode, we can do this with a
8404 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
8405 if ((code == ASHIFTRT || code == LSHIFTRT)
8406 && ! mode_dependent_address_p (XEXP (varop, 0))
8407 && ! MEM_VOLATILE_P (varop)
8408 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8409 MODE_INT, 1)) != BLKmode)
8410 {
8411 if (BYTES_BIG_ENDIAN)
8412 new = gen_rtx_MEM (tmode, XEXP (varop, 0));
8413 else
8414 new = gen_rtx_MEM (tmode,
8415 plus_constant (XEXP (varop, 0),
8416 count / BITS_PER_UNIT));
8417 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (varop);
8418 MEM_VOLATILE_P (new) = MEM_VOLATILE_P (varop);
8419 MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (varop);
8420 varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8421 : ZERO_EXTEND, mode, new);
8422 count = 0;
8423 continue;
8424 }
8425 break;
8426
8427 case USE:
8428 /* Similar to the case above, except that we can only do this if
8429 the resulting mode is the same as that of the underlying
8430 MEM and adjust the address depending on the *bits* endianness
8431 because of the way that bit-field extract insns are defined. */
8432 if ((code == ASHIFTRT || code == LSHIFTRT)
8433 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8434 MODE_INT, 1)) != BLKmode
8435 && tmode == GET_MODE (XEXP (varop, 0)))
8436 {
8437 if (BITS_BIG_ENDIAN)
8438 new = XEXP (varop, 0);
8439 else
8440 {
8441 new = copy_rtx (XEXP (varop, 0));
8442 SUBST (XEXP (new, 0),
8443 plus_constant (XEXP (new, 0),
8444 count / BITS_PER_UNIT));
8445 }
8446
8447 varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8448 : ZERO_EXTEND, mode, new);
8449 count = 0;
8450 continue;
8451 }
8452 break;
8453
8454 case SUBREG:
8455 /* If VAROP is a SUBREG, strip it as long as the inner operand has
8456 the same number of words as what we've seen so far. Then store
8457 the widest mode in MODE. */
8458 if (subreg_lowpart_p (varop)
8459 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8460 > GET_MODE_SIZE (GET_MODE (varop)))
8461 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8462 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8463 == mode_words))
8464 {
8465 varop = SUBREG_REG (varop);
8466 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8467 mode = GET_MODE (varop);
8468 continue;
8469 }
8470 break;
8471
8472 case MULT:
8473 /* Some machines use MULT instead of ASHIFT because MULT
8474 is cheaper. But it is still better on those machines to
8475 merge two shifts into one. */
8476 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8477 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8478 {
8479 varop = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
8480 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));;
8481 continue;
8482 }
8483 break;
8484
8485 case UDIV:
8486 /* Similar, for when divides are cheaper. */
8487 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8488 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8489 {
8490 varop = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
8491 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
8492 continue;
8493 }
8494 break;
8495
8496 case ASHIFTRT:
8497 /* If we are extracting just the sign bit of an arithmetic right
8498 shift, that shift is not needed. */
8499 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1)
8500 {
8501 varop = XEXP (varop, 0);
8502 continue;
8503 }
8504
8505 /* ... fall through ... */
8506
8507 case LSHIFTRT:
8508 case ASHIFT:
8509 case ROTATE:
8510 /* Here we have two nested shifts. The result is usually the
8511 AND of a new shift with a mask. We compute the result below. */
8512 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8513 && INTVAL (XEXP (varop, 1)) >= 0
8514 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8515 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8516 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8517 {
8518 enum rtx_code first_code = GET_CODE (varop);
8519 int first_count = INTVAL (XEXP (varop, 1));
8520 unsigned HOST_WIDE_INT mask;
8521 rtx mask_rtx;
8522
8523 /* We have one common special case. We can't do any merging if
8524 the inner code is an ASHIFTRT of a smaller mode. However, if
8525 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8526 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8527 we can convert it to
8528 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8529 This simplifies certain SIGN_EXTEND operations. */
8530 if (code == ASHIFT && first_code == ASHIFTRT
8531 && (GET_MODE_BITSIZE (result_mode)
8532 - GET_MODE_BITSIZE (GET_MODE (varop))) == count)
8533 {
8534 /* C3 has the low-order C1 bits zero. */
8535
8536 mask = (GET_MODE_MASK (mode)
8537 & ~ (((HOST_WIDE_INT) 1 << first_count) - 1));
8538
8539 varop = simplify_and_const_int (NULL_RTX, result_mode,
8540 XEXP (varop, 0), mask);
8541 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8542 varop, count);
8543 count = first_count;
8544 code = ASHIFTRT;
8545 continue;
8546 }
8547
8548 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8549 than C1 high-order bits equal to the sign bit, we can convert
8550 this to either an ASHIFT or a ASHIFTRT depending on the
8551 two counts.
8552
8553 We cannot do this if VAROP's mode is not SHIFT_MODE. */
8554
8555 if (code == ASHIFTRT && first_code == ASHIFT
8556 && GET_MODE (varop) == shift_mode
8557 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8558 > first_count))
8559 {
8560 count -= first_count;
8561 if (count < 0)
8562 count = - count, code = ASHIFT;
8563 varop = XEXP (varop, 0);
8564 continue;
8565 }
8566
8567 /* There are some cases we can't do. If CODE is ASHIFTRT,
8568 we can only do this if FIRST_CODE is also ASHIFTRT.
8569
8570 We can't do the case when CODE is ROTATE and FIRST_CODE is
8571 ASHIFTRT.
8572
8573 If the mode of this shift is not the mode of the outer shift,
8574 we can't do this if either shift is a right shift or ROTATE.
8575
8576 Finally, we can't do any of these if the mode is too wide
8577 unless the codes are the same.
8578
8579 Handle the case where the shift codes are the same
8580 first. */
8581
8582 if (code == first_code)
8583 {
8584 if (GET_MODE (varop) != result_mode
8585 && (code == ASHIFTRT || code == LSHIFTRT
8586 || code == ROTATE))
8587 break;
8588
8589 count += first_count;
8590 varop = XEXP (varop, 0);
8591 continue;
8592 }
8593
8594 if (code == ASHIFTRT
8595 || (code == ROTATE && first_code == ASHIFTRT)
8596 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8597 || (GET_MODE (varop) != result_mode
8598 && (first_code == ASHIFTRT || first_code == LSHIFTRT
8599 || first_code == ROTATE
8600 || code == ROTATE)))
8601 break;
8602
8603 /* To compute the mask to apply after the shift, shift the
8604 nonzero bits of the inner shift the same way the
8605 outer shift will. */
8606
8607 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
8608
8609 mask_rtx
8610 = simplify_binary_operation (code, result_mode, mask_rtx,
8611 GEN_INT (count));
8612
8613 /* Give up if we can't compute an outer operation to use. */
8614 if (mask_rtx == 0
8615 || GET_CODE (mask_rtx) != CONST_INT
8616 || ! merge_outer_ops (&outer_op, &outer_const, AND,
8617 INTVAL (mask_rtx),
8618 result_mode, &complement_p))
8619 break;
8620
8621 /* If the shifts are in the same direction, we add the
8622 counts. Otherwise, we subtract them. */
8623 if ((code == ASHIFTRT || code == LSHIFTRT)
8624 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8625 count += first_count;
8626 else
8627 count -= first_count;
8628
8629 /* If COUNT is positive, the new shift is usually CODE,
8630 except for the two exceptions below, in which case it is
8631 FIRST_CODE. If the count is negative, FIRST_CODE should
8632 always be used */
8633 if (count > 0
8634 && ((first_code == ROTATE && code == ASHIFT)
8635 || (first_code == ASHIFTRT && code == LSHIFTRT)))
8636 code = first_code;
8637 else if (count < 0)
8638 code = first_code, count = - count;
8639
8640 varop = XEXP (varop, 0);
8641 continue;
8642 }
8643
8644 /* If we have (A << B << C) for any shift, we can convert this to
8645 (A << C << B). This wins if A is a constant. Only try this if
8646 B is not a constant. */
8647
8648 else if (GET_CODE (varop) == code
8649 && GET_CODE (XEXP (varop, 1)) != CONST_INT
8650 && 0 != (new
8651 = simplify_binary_operation (code, mode,
8652 XEXP (varop, 0),
8653 GEN_INT (count))))
8654 {
8655 varop = gen_rtx_combine (code, mode, new, XEXP (varop, 1));
8656 count = 0;
8657 continue;
8658 }
8659 break;
8660
8661 case NOT:
8662 /* Make this fit the case below. */
8663 varop = gen_rtx_combine (XOR, mode, XEXP (varop, 0),
8664 GEN_INT (GET_MODE_MASK (mode)));
8665 continue;
8666
8667 case IOR:
8668 case AND:
8669 case XOR:
8670 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8671 with C the size of VAROP - 1 and the shift is logical if
8672 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8673 we have an (le X 0) operation. If we have an arithmetic shift
8674 and STORE_FLAG_VALUE is 1 or we have a logical shift with
8675 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
8676
8677 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8678 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8679 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8680 && (code == LSHIFTRT || code == ASHIFTRT)
8681 && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
8682 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8683 {
8684 count = 0;
8685 varop = gen_rtx_combine (LE, GET_MODE (varop), XEXP (varop, 1),
8686 const0_rtx);
8687
8688 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8689 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8690
8691 continue;
8692 }
8693
8694 /* If we have (shift (logical)), move the logical to the outside
8695 to allow it to possibly combine with another logical and the
8696 shift to combine with another shift. This also canonicalizes to
8697 what a ZERO_EXTRACT looks like. Also, some machines have
8698 (and (shift)) insns. */
8699
8700 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8701 && (new = simplify_binary_operation (code, result_mode,
8702 XEXP (varop, 1),
8703 GEN_INT (count))) != 0
8704 && GET_CODE(new) == CONST_INT
8705 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
8706 INTVAL (new), result_mode, &complement_p))
8707 {
8708 varop = XEXP (varop, 0);
8709 continue;
8710 }
8711
8712 /* If we can't do that, try to simplify the shift in each arm of the
8713 logical expression, make a new logical expression, and apply
8714 the inverse distributive law. */
8715 {
8716 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8717 XEXP (varop, 0), count);
8718 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8719 XEXP (varop, 1), count);
8720
8721 varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
8722 varop = apply_distributive_law (varop);
8723
8724 count = 0;
8725 }
8726 break;
8727
8728 case EQ:
8729 /* convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
8730 says that the sign bit can be tested, FOO has mode MODE, C is
8731 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
8732 that may be nonzero. */
8733 if (code == LSHIFTRT
8734 && XEXP (varop, 1) == const0_rtx
8735 && GET_MODE (XEXP (varop, 0)) == result_mode
8736 && count == GET_MODE_BITSIZE (result_mode) - 1
8737 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8738 && ((STORE_FLAG_VALUE
8739 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (result_mode) - 1))))
8740 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8741 && merge_outer_ops (&outer_op, &outer_const, XOR,
8742 (HOST_WIDE_INT) 1, result_mode,
8743 &complement_p))
8744 {
8745 varop = XEXP (varop, 0);
8746 count = 0;
8747 continue;
8748 }
8749 break;
8750
8751 case NEG:
8752 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
8753 than the number of bits in the mode is equivalent to A. */
8754 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8755 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
8756 {
8757 varop = XEXP (varop, 0);
8758 count = 0;
8759 continue;
8760 }
8761
8762 /* NEG commutes with ASHIFT since it is multiplication. Move the
8763 NEG outside to allow shifts to combine. */
8764 if (code == ASHIFT
8765 && merge_outer_ops (&outer_op, &outer_const, NEG,
8766 (HOST_WIDE_INT) 0, result_mode,
8767 &complement_p))
8768 {
8769 varop = XEXP (varop, 0);
8770 continue;
8771 }
8772 break;
8773
8774 case PLUS:
8775 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
8776 is one less than the number of bits in the mode is
8777 equivalent to (xor A 1). */
8778 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8779 && XEXP (varop, 1) == constm1_rtx
8780 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8781 && merge_outer_ops (&outer_op, &outer_const, XOR,
8782 (HOST_WIDE_INT) 1, result_mode,
8783 &complement_p))
8784 {
8785 count = 0;
8786 varop = XEXP (varop, 0);
8787 continue;
8788 }
8789
8790 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
8791 that might be nonzero in BAR are those being shifted out and those
8792 bits are known zero in FOO, we can replace the PLUS with FOO.
8793 Similarly in the other operand order. This code occurs when
8794 we are computing the size of a variable-size array. */
8795
8796 if ((code == ASHIFTRT || code == LSHIFTRT)
8797 && count < HOST_BITS_PER_WIDE_INT
8798 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
8799 && (nonzero_bits (XEXP (varop, 1), result_mode)
8800 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
8801 {
8802 varop = XEXP (varop, 0);
8803 continue;
8804 }
8805 else if ((code == ASHIFTRT || code == LSHIFTRT)
8806 && count < HOST_BITS_PER_WIDE_INT
8807 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8808 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8809 >> count)
8810 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8811 & nonzero_bits (XEXP (varop, 1),
8812 result_mode)))
8813 {
8814 varop = XEXP (varop, 1);
8815 continue;
8816 }
8817
8818 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
8819 if (code == ASHIFT
8820 && GET_CODE (XEXP (varop, 1)) == CONST_INT
8821 && (new = simplify_binary_operation (ASHIFT, result_mode,
8822 XEXP (varop, 1),
8823 GEN_INT (count))) != 0
8824 && GET_CODE(new) == CONST_INT
8825 && merge_outer_ops (&outer_op, &outer_const, PLUS,
8826 INTVAL (new), result_mode, &complement_p))
8827 {
8828 varop = XEXP (varop, 0);
8829 continue;
8830 }
8831 break;
8832
8833 case MINUS:
8834 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
8835 with C the size of VAROP - 1 and the shift is logical if
8836 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8837 we have a (gt X 0) operation. If the shift is arithmetic with
8838 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
8839 we have a (neg (gt X 0)) operation. */
8840
8841 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8842 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
8843 && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
8844 && (code == LSHIFTRT || code == ASHIFTRT)
8845 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
8846 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
8847 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8848 {
8849 count = 0;
8850 varop = gen_rtx_combine (GT, GET_MODE (varop), XEXP (varop, 1),
8851 const0_rtx);
8852
8853 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8854 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8855
8856 continue;
8857 }
8858 break;
8859
8860 case TRUNCATE:
8861 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
8862 if the truncate does not affect the value. */
8863 if (code == LSHIFTRT
8864 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
8865 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
8866 && (INTVAL (XEXP (XEXP (varop, 0), 1))
8867 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
8868 - GET_MODE_BITSIZE (GET_MODE (varop)))))
8869 {
8870 rtx varop_inner = XEXP (varop, 0);
8871
8872 varop_inner = gen_rtx_combine (LSHIFTRT,
8873 GET_MODE (varop_inner),
8874 XEXP (varop_inner, 0),
8875 GEN_INT (count + INTVAL (XEXP (varop_inner, 1))));
8876 varop = gen_rtx_combine (TRUNCATE, GET_MODE (varop),
8877 varop_inner);
8878 count = 0;
8879 continue;
8880 }
8881 break;
8882
8883 default:
8884 break;
8885 }
8886
8887 break;
8888 }
8889
8890 /* We need to determine what mode to do the shift in. If the shift is
8891 a right shift or ROTATE, we must always do it in the mode it was
8892 originally done in. Otherwise, we can do it in MODE, the widest mode
8893 encountered. The code we care about is that of the shift that will
8894 actually be done, not the shift that was originally requested. */
8895 shift_mode
8896 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8897 ? result_mode : mode);
8898
8899 /* We have now finished analyzing the shift. The result should be
8900 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
8901 OUTER_OP is non-NIL, it is an operation that needs to be applied
8902 to the result of the shift. OUTER_CONST is the relevant constant,
8903 but we must turn off all bits turned off in the shift.
8904
8905 If we were passed a value for X, see if we can use any pieces of
8906 it. If not, make new rtx. */
8907
8908 if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
8909 && GET_CODE (XEXP (x, 1)) == CONST_INT
8910 && INTVAL (XEXP (x, 1)) == count)
8911 const_rtx = XEXP (x, 1);
8912 else
8913 const_rtx = GEN_INT (count);
8914
8915 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8916 && GET_MODE (XEXP (x, 0)) == shift_mode
8917 && SUBREG_REG (XEXP (x, 0)) == varop)
8918 varop = XEXP (x, 0);
8919 else if (GET_MODE (varop) != shift_mode)
8920 varop = gen_lowpart_for_combine (shift_mode, varop);
8921
8922 /* If we can't make the SUBREG, try to return what we were given. */
8923 if (GET_CODE (varop) == CLOBBER)
8924 return x ? x : varop;
8925
8926 new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
8927 if (new != 0)
8928 x = new;
8929 else
8930 {
8931 if (x == 0 || GET_CODE (x) != code || GET_MODE (x) != shift_mode)
8932 x = gen_rtx_combine (code, shift_mode, varop, const_rtx);
8933
8934 SUBST (XEXP (x, 0), varop);
8935 SUBST (XEXP (x, 1), const_rtx);
8936 }
8937
8938 /* If we have an outer operation and we just made a shift, it is
8939 possible that we could have simplified the shift were it not
8940 for the outer operation. So try to do the simplification
8941 recursively. */
8942
8943 if (outer_op != NIL && GET_CODE (x) == code
8944 && GET_CODE (XEXP (x, 1)) == CONST_INT)
8945 x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
8946 INTVAL (XEXP (x, 1)));
8947
8948 /* If we were doing a LSHIFTRT in a wider mode than it was originally,
8949 turn off all the bits that the shift would have turned off. */
8950 if (orig_code == LSHIFTRT && result_mode != shift_mode)
8951 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
8952 GET_MODE_MASK (result_mode) >> orig_count);
8953
8954 /* Do the remainder of the processing in RESULT_MODE. */
8955 x = gen_lowpart_for_combine (result_mode, x);
8956
8957 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
8958 operation. */
8959 if (complement_p)
8960 x = gen_unary (NOT, result_mode, result_mode, x);
8961
8962 if (outer_op != NIL)
8963 {
8964 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
8965 {
8966 int width = GET_MODE_BITSIZE (result_mode);
8967
8968 outer_const &= GET_MODE_MASK (result_mode);
8969
8970 /* If this would be an entire word for the target, but is not for
8971 the host, then sign-extend on the host so that the number will
8972 look the same way on the host that it would on the target.
8973
8974 For example, when building a 64 bit alpha hosted 32 bit sparc
8975 targeted compiler, then we want the 32 bit unsigned value -1 to be
8976 represented as a 64 bit value -1, and not as 0x00000000ffffffff.
8977 The later confuses the sparc backend. */
8978
8979 if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
8980 && (outer_const & ((HOST_WIDE_INT) 1 << (width - 1))))
8981 outer_const |= ((HOST_WIDE_INT) (-1) << width);
8982 }
8983
8984 if (outer_op == AND)
8985 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
8986 else if (outer_op == SET)
8987 /* This means that we have determined that the result is
8988 equivalent to a constant. This should be rare. */
8989 x = GEN_INT (outer_const);
8990 else if (GET_RTX_CLASS (outer_op) == '1')
8991 x = gen_unary (outer_op, result_mode, result_mode, x);
8992 else
8993 x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
8994 }
8995
8996 return x;
8997 }
8998 \f
8999 /* Like recog, but we receive the address of a pointer to a new pattern.
9000 We try to match the rtx that the pointer points to.
9001 If that fails, we may try to modify or replace the pattern,
9002 storing the replacement into the same pointer object.
9003
9004 Modifications include deletion or addition of CLOBBERs.
9005
9006 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9007 the CLOBBERs are placed.
9008
9009 PADDED_SCRATCHES is set to the number of (clobber (scratch)) patterns
9010 we had to add.
9011
9012 The value is the final insn code from the pattern ultimately matched,
9013 or -1. */
9014
9015 static int
9016 recog_for_combine (pnewpat, insn, pnotes, padded_scratches)
9017 rtx *pnewpat;
9018 rtx insn;
9019 rtx *pnotes;
9020 int *padded_scratches;
9021 {
9022 register rtx pat = *pnewpat;
9023 int insn_code_number;
9024 int num_clobbers_to_add = 0;
9025 int i;
9026 rtx notes = 0;
9027
9028 *padded_scratches = 0;
9029
9030 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9031 we use to indicate that something didn't match. If we find such a
9032 thing, force rejection. */
9033 if (GET_CODE (pat) == PARALLEL)
9034 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9035 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9036 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9037 return -1;
9038
9039 /* Is the result of combination a valid instruction? */
9040 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9041
9042 /* If it isn't, there is the possibility that we previously had an insn
9043 that clobbered some register as a side effect, but the combined
9044 insn doesn't need to do that. So try once more without the clobbers
9045 unless this represents an ASM insn. */
9046
9047 if (insn_code_number < 0 && ! check_asm_operands (pat)
9048 && GET_CODE (pat) == PARALLEL)
9049 {
9050 int pos;
9051
9052 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9053 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9054 {
9055 if (i != pos)
9056 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9057 pos++;
9058 }
9059
9060 SUBST_INT (XVECLEN (pat, 0), pos);
9061
9062 if (pos == 1)
9063 pat = XVECEXP (pat, 0, 0);
9064
9065 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9066 }
9067
9068 /* If we had any clobbers to add, make a new pattern than contains
9069 them. Then check to make sure that all of them are dead. */
9070 if (num_clobbers_to_add)
9071 {
9072 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9073 gen_rtvec (GET_CODE (pat) == PARALLEL
9074 ? XVECLEN (pat, 0) + num_clobbers_to_add
9075 : num_clobbers_to_add + 1));
9076
9077 if (GET_CODE (pat) == PARALLEL)
9078 for (i = 0; i < XVECLEN (pat, 0); i++)
9079 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9080 else
9081 XVECEXP (newpat, 0, 0) = pat;
9082
9083 add_clobbers (newpat, insn_code_number);
9084
9085 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9086 i < XVECLEN (newpat, 0); i++)
9087 {
9088 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
9089 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9090 return -1;
9091 else if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == SCRATCH)
9092 (*padded_scratches)++;
9093 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9094 XEXP (XVECEXP (newpat, 0, i), 0), notes);
9095 }
9096 pat = newpat;
9097 }
9098
9099 *pnewpat = pat;
9100 *pnotes = notes;
9101
9102 return insn_code_number;
9103 }
9104 \f
9105 /* Like gen_lowpart but for use by combine. In combine it is not possible
9106 to create any new pseudoregs. However, it is safe to create
9107 invalid memory addresses, because combine will try to recognize
9108 them and all they will do is make the combine attempt fail.
9109
9110 If for some reason this cannot do its job, an rtx
9111 (clobber (const_int 0)) is returned.
9112 An insn containing that will not be recognized. */
9113
9114 #undef gen_lowpart
9115
9116 static rtx
9117 gen_lowpart_for_combine (mode, x)
9118 enum machine_mode mode;
9119 register rtx x;
9120 {
9121 rtx result;
9122
9123 if (GET_MODE (x) == mode)
9124 return x;
9125
9126 /* We can only support MODE being wider than a word if X is a
9127 constant integer or has a mode the same size. */
9128
9129 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
9130 && ! ((GET_MODE (x) == VOIDmode
9131 && (GET_CODE (x) == CONST_INT
9132 || GET_CODE (x) == CONST_DOUBLE))
9133 || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
9134 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9135
9136 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
9137 won't know what to do. So we will strip off the SUBREG here and
9138 process normally. */
9139 if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
9140 {
9141 x = SUBREG_REG (x);
9142 if (GET_MODE (x) == mode)
9143 return x;
9144 }
9145
9146 result = gen_lowpart_common (mode, x);
9147 if (result != 0
9148 && GET_CODE (result) == SUBREG
9149 && GET_CODE (SUBREG_REG (result)) == REG
9150 && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER
9151 && (GET_MODE_SIZE (GET_MODE (result))
9152 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (result)))))
9153 REG_CHANGES_SIZE (REGNO (SUBREG_REG (result))) = 1;
9154
9155 if (result)
9156 return result;
9157
9158 if (GET_CODE (x) == MEM)
9159 {
9160 register int offset = 0;
9161 rtx new;
9162
9163 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9164 address. */
9165 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9166 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9167
9168 /* If we want to refer to something bigger than the original memref,
9169 generate a perverse subreg instead. That will force a reload
9170 of the original memref X. */
9171 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
9172 return gen_rtx_SUBREG (mode, x, 0);
9173
9174 if (WORDS_BIG_ENDIAN)
9175 offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
9176 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
9177 if (BYTES_BIG_ENDIAN)
9178 {
9179 /* Adjust the address so that the address-after-the-data is
9180 unchanged. */
9181 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
9182 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
9183 }
9184 new = gen_rtx_MEM (mode, plus_constant (XEXP (x, 0), offset));
9185 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
9186 MEM_VOLATILE_P (new) = MEM_VOLATILE_P (x);
9187 MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (x);
9188 return new;
9189 }
9190
9191 /* If X is a comparison operator, rewrite it in a new mode. This
9192 probably won't match, but may allow further simplifications. */
9193 else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
9194 return gen_rtx_combine (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
9195
9196 /* If we couldn't simplify X any other way, just enclose it in a
9197 SUBREG. Normally, this SUBREG won't match, but some patterns may
9198 include an explicit SUBREG or we may simplify it further in combine. */
9199 else
9200 {
9201 int word = 0;
9202
9203 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
9204 word = ((GET_MODE_SIZE (GET_MODE (x))
9205 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
9206 / UNITS_PER_WORD);
9207 return gen_rtx_SUBREG (mode, x, word);
9208 }
9209 }
9210 \f
9211 /* Make an rtx expression. This is a subset of gen_rtx and only supports
9212 expressions of 1, 2, or 3 operands, each of which are rtx expressions.
9213
9214 If the identical expression was previously in the insn (in the undobuf),
9215 it will be returned. Only if it is not found will a new expression
9216 be made. */
9217
9218 /*VARARGS2*/
9219 static rtx
9220 gen_rtx_combine VPROTO((enum rtx_code code, enum machine_mode mode, ...))
9221 {
9222 #ifndef __STDC__
9223 enum rtx_code code;
9224 enum machine_mode mode;
9225 #endif
9226 va_list p;
9227 int n_args;
9228 rtx args[3];
9229 int j;
9230 char *fmt;
9231 rtx rt;
9232 struct undo *undo;
9233
9234 VA_START (p, mode);
9235
9236 #ifndef __STDC__
9237 code = va_arg (p, enum rtx_code);
9238 mode = va_arg (p, enum machine_mode);
9239 #endif
9240
9241 n_args = GET_RTX_LENGTH (code);
9242 fmt = GET_RTX_FORMAT (code);
9243
9244 if (n_args == 0 || n_args > 3)
9245 abort ();
9246
9247 /* Get each arg and verify that it is supposed to be an expression. */
9248 for (j = 0; j < n_args; j++)
9249 {
9250 if (*fmt++ != 'e')
9251 abort ();
9252
9253 args[j] = va_arg (p, rtx);
9254 }
9255
9256 /* See if this is in undobuf. Be sure we don't use objects that came
9257 from another insn; this could produce circular rtl structures. */
9258
9259 for (undo = undobuf.undos; undo != undobuf.previous_undos; undo = undo->next)
9260 if (!undo->is_int
9261 && GET_CODE (undo->old_contents.r) == code
9262 && GET_MODE (undo->old_contents.r) == mode)
9263 {
9264 for (j = 0; j < n_args; j++)
9265 if (XEXP (undo->old_contents.r, j) != args[j])
9266 break;
9267
9268 if (j == n_args)
9269 return undo->old_contents.r;
9270 }
9271
9272 /* Otherwise make a new rtx. We know we have 1, 2, or 3 args.
9273 Use rtx_alloc instead of gen_rtx because it's faster on RISC. */
9274 rt = rtx_alloc (code);
9275 PUT_MODE (rt, mode);
9276 XEXP (rt, 0) = args[0];
9277 if (n_args > 1)
9278 {
9279 XEXP (rt, 1) = args[1];
9280 if (n_args > 2)
9281 XEXP (rt, 2) = args[2];
9282 }
9283 return rt;
9284 }
9285
9286 /* These routines make binary and unary operations by first seeing if they
9287 fold; if not, a new expression is allocated. */
9288
9289 static rtx
9290 gen_binary (code, mode, op0, op1)
9291 enum rtx_code code;
9292 enum machine_mode mode;
9293 rtx op0, op1;
9294 {
9295 rtx result;
9296 rtx tem;
9297
9298 if (GET_RTX_CLASS (code) == 'c'
9299 && (GET_CODE (op0) == CONST_INT
9300 || (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)))
9301 tem = op0, op0 = op1, op1 = tem;
9302
9303 if (GET_RTX_CLASS (code) == '<')
9304 {
9305 enum machine_mode op_mode = GET_MODE (op0);
9306
9307 /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get
9308 just (REL_OP X Y). */
9309 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
9310 {
9311 op1 = XEXP (op0, 1);
9312 op0 = XEXP (op0, 0);
9313 op_mode = GET_MODE (op0);
9314 }
9315
9316 if (op_mode == VOIDmode)
9317 op_mode = GET_MODE (op1);
9318 result = simplify_relational_operation (code, op_mode, op0, op1);
9319 }
9320 else
9321 result = simplify_binary_operation (code, mode, op0, op1);
9322
9323 if (result)
9324 return result;
9325
9326 /* Put complex operands first and constants second. */
9327 if (GET_RTX_CLASS (code) == 'c'
9328 && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9329 || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
9330 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
9331 || (GET_CODE (op0) == SUBREG
9332 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
9333 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
9334 return gen_rtx_combine (code, mode, op1, op0);
9335
9336 /* If we are turning off bits already known off in OP0, we need not do
9337 an AND. */
9338 else if (code == AND && GET_CODE (op1) == CONST_INT
9339 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9340 && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
9341 return op0;
9342
9343 return gen_rtx_combine (code, mode, op0, op1);
9344 }
9345
9346 static rtx
9347 gen_unary (code, mode, op0_mode, op0)
9348 enum rtx_code code;
9349 enum machine_mode mode, op0_mode;
9350 rtx op0;
9351 {
9352 rtx result = simplify_unary_operation (code, mode, op0, op0_mode);
9353
9354 if (result)
9355 return result;
9356
9357 return gen_rtx_combine (code, mode, op0);
9358 }
9359 \f
9360 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9361 comparison code that will be tested.
9362
9363 The result is a possibly different comparison code to use. *POP0 and
9364 *POP1 may be updated.
9365
9366 It is possible that we might detect that a comparison is either always
9367 true or always false. However, we do not perform general constant
9368 folding in combine, so this knowledge isn't useful. Such tautologies
9369 should have been detected earlier. Hence we ignore all such cases. */
9370
9371 static enum rtx_code
9372 simplify_comparison (code, pop0, pop1)
9373 enum rtx_code code;
9374 rtx *pop0;
9375 rtx *pop1;
9376 {
9377 rtx op0 = *pop0;
9378 rtx op1 = *pop1;
9379 rtx tem, tem1;
9380 int i;
9381 enum machine_mode mode, tmode;
9382
9383 /* Try a few ways of applying the same transformation to both operands. */
9384 while (1)
9385 {
9386 #ifndef WORD_REGISTER_OPERATIONS
9387 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9388 so check specially. */
9389 if (code != GTU && code != GEU && code != LTU && code != LEU
9390 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9391 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9392 && GET_CODE (XEXP (op1, 0)) == ASHIFT
9393 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9394 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9395 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9396 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9397 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9398 && GET_CODE (XEXP (op1, 1)) == CONST_INT
9399 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9400 && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT
9401 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (op1, 1))
9402 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op0, 0), 1))
9403 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
9404 && (INTVAL (XEXP (op0, 1))
9405 == (GET_MODE_BITSIZE (GET_MODE (op0))
9406 - (GET_MODE_BITSIZE
9407 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9408 {
9409 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9410 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9411 }
9412 #endif
9413
9414 /* If both operands are the same constant shift, see if we can ignore the
9415 shift. We can if the shift is a rotate or if the bits shifted out of
9416 this shift are known to be zero for both inputs and if the type of
9417 comparison is compatible with the shift. */
9418 if (GET_CODE (op0) == GET_CODE (op1)
9419 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9420 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9421 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9422 && (code != GT && code != LT && code != GE && code != LE))
9423 || (GET_CODE (op0) == ASHIFTRT
9424 && (code != GTU && code != LTU
9425 && code != GEU && code != GEU)))
9426 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9427 && INTVAL (XEXP (op0, 1)) >= 0
9428 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9429 && XEXP (op0, 1) == XEXP (op1, 1))
9430 {
9431 enum machine_mode mode = GET_MODE (op0);
9432 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9433 int shift_count = INTVAL (XEXP (op0, 1));
9434
9435 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9436 mask &= (mask >> shift_count) << shift_count;
9437 else if (GET_CODE (op0) == ASHIFT)
9438 mask = (mask & (mask << shift_count)) >> shift_count;
9439
9440 if ((nonzero_bits (XEXP (op0, 0), mode) & ~ mask) == 0
9441 && (nonzero_bits (XEXP (op1, 0), mode) & ~ mask) == 0)
9442 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9443 else
9444 break;
9445 }
9446
9447 /* If both operands are AND's of a paradoxical SUBREG by constant, the
9448 SUBREGs are of the same mode, and, in both cases, the AND would
9449 be redundant if the comparison was done in the narrower mode,
9450 do the comparison in the narrower mode (e.g., we are AND'ing with 1
9451 and the operand's possibly nonzero bits are 0xffffff01; in that case
9452 if we only care about QImode, we don't need the AND). This case
9453 occurs if the output mode of an scc insn is not SImode and
9454 STORE_FLAG_VALUE == 1 (e.g., the 386).
9455
9456 Similarly, check for a case where the AND's are ZERO_EXTEND
9457 operations from some narrower mode even though a SUBREG is not
9458 present. */
9459
9460 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9461 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9462 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9463 {
9464 rtx inner_op0 = XEXP (op0, 0);
9465 rtx inner_op1 = XEXP (op1, 0);
9466 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9467 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9468 int changed = 0;
9469
9470 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9471 && (GET_MODE_SIZE (GET_MODE (inner_op0))
9472 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9473 && (GET_MODE (SUBREG_REG (inner_op0))
9474 == GET_MODE (SUBREG_REG (inner_op1)))
9475 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
9476 <= HOST_BITS_PER_WIDE_INT)
9477 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9478 GET_MODE (SUBREG_REG (op0)))))
9479 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9480 GET_MODE (SUBREG_REG (inner_op1))))))
9481 {
9482 op0 = SUBREG_REG (inner_op0);
9483 op1 = SUBREG_REG (inner_op1);
9484
9485 /* The resulting comparison is always unsigned since we masked
9486 off the original sign bit. */
9487 code = unsigned_condition (code);
9488
9489 changed = 1;
9490 }
9491
9492 else if (c0 == c1)
9493 for (tmode = GET_CLASS_NARROWEST_MODE
9494 (GET_MODE_CLASS (GET_MODE (op0)));
9495 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9496 if (c0 == GET_MODE_MASK (tmode))
9497 {
9498 op0 = gen_lowpart_for_combine (tmode, inner_op0);
9499 op1 = gen_lowpart_for_combine (tmode, inner_op1);
9500 code = unsigned_condition (code);
9501 changed = 1;
9502 break;
9503 }
9504
9505 if (! changed)
9506 break;
9507 }
9508
9509 /* If both operands are NOT, we can strip off the outer operation
9510 and adjust the comparison code for swapped operands; similarly for
9511 NEG, except that this must be an equality comparison. */
9512 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9513 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9514 && (code == EQ || code == NE)))
9515 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
9516
9517 else
9518 break;
9519 }
9520
9521 /* If the first operand is a constant, swap the operands and adjust the
9522 comparison code appropriately, but don't do this if the second operand
9523 is already a constant integer. */
9524 if (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9525 {
9526 tem = op0, op0 = op1, op1 = tem;
9527 code = swap_condition (code);
9528 }
9529
9530 /* We now enter a loop during which we will try to simplify the comparison.
9531 For the most part, we only are concerned with comparisons with zero,
9532 but some things may really be comparisons with zero but not start
9533 out looking that way. */
9534
9535 while (GET_CODE (op1) == CONST_INT)
9536 {
9537 enum machine_mode mode = GET_MODE (op0);
9538 int mode_width = GET_MODE_BITSIZE (mode);
9539 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9540 int equality_comparison_p;
9541 int sign_bit_comparison_p;
9542 int unsigned_comparison_p;
9543 HOST_WIDE_INT const_op;
9544
9545 /* We only want to handle integral modes. This catches VOIDmode,
9546 CCmode, and the floating-point modes. An exception is that we
9547 can handle VOIDmode if OP0 is a COMPARE or a comparison
9548 operation. */
9549
9550 if (GET_MODE_CLASS (mode) != MODE_INT
9551 && ! (mode == VOIDmode
9552 && (GET_CODE (op0) == COMPARE
9553 || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
9554 break;
9555
9556 /* Get the constant we are comparing against and turn off all bits
9557 not on in our mode. */
9558 const_op = INTVAL (op1);
9559 if (mode_width <= HOST_BITS_PER_WIDE_INT)
9560 const_op &= mask;
9561
9562 /* If we are comparing against a constant power of two and the value
9563 being compared can only have that single bit nonzero (e.g., it was
9564 `and'ed with that bit), we can replace this with a comparison
9565 with zero. */
9566 if (const_op
9567 && (code == EQ || code == NE || code == GE || code == GEU
9568 || code == LT || code == LTU)
9569 && mode_width <= HOST_BITS_PER_WIDE_INT
9570 && exact_log2 (const_op) >= 0
9571 && nonzero_bits (op0, mode) == const_op)
9572 {
9573 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9574 op1 = const0_rtx, const_op = 0;
9575 }
9576
9577 /* Similarly, if we are comparing a value known to be either -1 or
9578 0 with -1, change it to the opposite comparison against zero. */
9579
9580 if (const_op == -1
9581 && (code == EQ || code == NE || code == GT || code == LE
9582 || code == GEU || code == LTU)
9583 && num_sign_bit_copies (op0, mode) == mode_width)
9584 {
9585 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9586 op1 = const0_rtx, const_op = 0;
9587 }
9588
9589 /* Do some canonicalizations based on the comparison code. We prefer
9590 comparisons against zero and then prefer equality comparisons.
9591 If we can reduce the size of a constant, we will do that too. */
9592
9593 switch (code)
9594 {
9595 case LT:
9596 /* < C is equivalent to <= (C - 1) */
9597 if (const_op > 0)
9598 {
9599 const_op -= 1;
9600 op1 = GEN_INT (const_op);
9601 code = LE;
9602 /* ... fall through to LE case below. */
9603 }
9604 else
9605 break;
9606
9607 case LE:
9608 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
9609 if (const_op < 0)
9610 {
9611 const_op += 1;
9612 op1 = GEN_INT (const_op);
9613 code = LT;
9614 }
9615
9616 /* If we are doing a <= 0 comparison on a value known to have
9617 a zero sign bit, we can replace this with == 0. */
9618 else if (const_op == 0
9619 && mode_width <= HOST_BITS_PER_WIDE_INT
9620 && (nonzero_bits (op0, mode)
9621 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9622 code = EQ;
9623 break;
9624
9625 case GE:
9626 /* >= C is equivalent to > (C - 1). */
9627 if (const_op > 0)
9628 {
9629 const_op -= 1;
9630 op1 = GEN_INT (const_op);
9631 code = GT;
9632 /* ... fall through to GT below. */
9633 }
9634 else
9635 break;
9636
9637 case GT:
9638 /* > C is equivalent to >= (C + 1); we do this for C < 0*/
9639 if (const_op < 0)
9640 {
9641 const_op += 1;
9642 op1 = GEN_INT (const_op);
9643 code = GE;
9644 }
9645
9646 /* If we are doing a > 0 comparison on a value known to have
9647 a zero sign bit, we can replace this with != 0. */
9648 else if (const_op == 0
9649 && mode_width <= HOST_BITS_PER_WIDE_INT
9650 && (nonzero_bits (op0, mode)
9651 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9652 code = NE;
9653 break;
9654
9655 case LTU:
9656 /* < C is equivalent to <= (C - 1). */
9657 if (const_op > 0)
9658 {
9659 const_op -= 1;
9660 op1 = GEN_INT (const_op);
9661 code = LEU;
9662 /* ... fall through ... */
9663 }
9664
9665 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
9666 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9667 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9668 {
9669 const_op = 0, op1 = const0_rtx;
9670 code = GE;
9671 break;
9672 }
9673 else
9674 break;
9675
9676 case LEU:
9677 /* unsigned <= 0 is equivalent to == 0 */
9678 if (const_op == 0)
9679 code = EQ;
9680
9681 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
9682 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9683 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9684 {
9685 const_op = 0, op1 = const0_rtx;
9686 code = GE;
9687 }
9688 break;
9689
9690 case GEU:
9691 /* >= C is equivalent to < (C - 1). */
9692 if (const_op > 1)
9693 {
9694 const_op -= 1;
9695 op1 = GEN_INT (const_op);
9696 code = GTU;
9697 /* ... fall through ... */
9698 }
9699
9700 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
9701 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9702 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9703 {
9704 const_op = 0, op1 = const0_rtx;
9705 code = LT;
9706 break;
9707 }
9708 else
9709 break;
9710
9711 case GTU:
9712 /* unsigned > 0 is equivalent to != 0 */
9713 if (const_op == 0)
9714 code = NE;
9715
9716 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
9717 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9718 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9719 {
9720 const_op = 0, op1 = const0_rtx;
9721 code = LT;
9722 }
9723 break;
9724
9725 default:
9726 break;
9727 }
9728
9729 /* Compute some predicates to simplify code below. */
9730
9731 equality_comparison_p = (code == EQ || code == NE);
9732 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9733 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9734 || code == LEU);
9735
9736 /* If this is a sign bit comparison and we can do arithmetic in
9737 MODE, say that we will only be needing the sign bit of OP0. */
9738 if (sign_bit_comparison_p
9739 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9740 op0 = force_to_mode (op0, mode,
9741 ((HOST_WIDE_INT) 1
9742 << (GET_MODE_BITSIZE (mode) - 1)),
9743 NULL_RTX, 0);
9744
9745 /* Now try cases based on the opcode of OP0. If none of the cases
9746 does a "continue", we exit this loop immediately after the
9747 switch. */
9748
9749 switch (GET_CODE (op0))
9750 {
9751 case ZERO_EXTRACT:
9752 /* If we are extracting a single bit from a variable position in
9753 a constant that has only a single bit set and are comparing it
9754 with zero, we can convert this into an equality comparison
9755 between the position and the location of the single bit. */
9756
9757 if (GET_CODE (XEXP (op0, 0)) == CONST_INT
9758 && XEXP (op0, 1) == const1_rtx
9759 && equality_comparison_p && const_op == 0
9760 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
9761 {
9762 if (BITS_BIG_ENDIAN)
9763 #ifdef HAVE_extzv
9764 i = (GET_MODE_BITSIZE
9765 (insn_operand_mode[(int) CODE_FOR_extzv][1]) - 1 - i);
9766 #else
9767 i = BITS_PER_WORD - 1 - i;
9768 #endif
9769
9770 op0 = XEXP (op0, 2);
9771 op1 = GEN_INT (i);
9772 const_op = i;
9773
9774 /* Result is nonzero iff shift count is equal to I. */
9775 code = reverse_condition (code);
9776 continue;
9777 }
9778
9779 /* ... fall through ... */
9780
9781 case SIGN_EXTRACT:
9782 tem = expand_compound_operation (op0);
9783 if (tem != op0)
9784 {
9785 op0 = tem;
9786 continue;
9787 }
9788 break;
9789
9790 case NOT:
9791 /* If testing for equality, we can take the NOT of the constant. */
9792 if (equality_comparison_p
9793 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
9794 {
9795 op0 = XEXP (op0, 0);
9796 op1 = tem;
9797 continue;
9798 }
9799
9800 /* If just looking at the sign bit, reverse the sense of the
9801 comparison. */
9802 if (sign_bit_comparison_p)
9803 {
9804 op0 = XEXP (op0, 0);
9805 code = (code == GE ? LT : GE);
9806 continue;
9807 }
9808 break;
9809
9810 case NEG:
9811 /* If testing for equality, we can take the NEG of the constant. */
9812 if (equality_comparison_p
9813 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
9814 {
9815 op0 = XEXP (op0, 0);
9816 op1 = tem;
9817 continue;
9818 }
9819
9820 /* The remaining cases only apply to comparisons with zero. */
9821 if (const_op != 0)
9822 break;
9823
9824 /* When X is ABS or is known positive,
9825 (neg X) is < 0 if and only if X != 0. */
9826
9827 if (sign_bit_comparison_p
9828 && (GET_CODE (XEXP (op0, 0)) == ABS
9829 || (mode_width <= HOST_BITS_PER_WIDE_INT
9830 && (nonzero_bits (XEXP (op0, 0), mode)
9831 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
9832 {
9833 op0 = XEXP (op0, 0);
9834 code = (code == LT ? NE : EQ);
9835 continue;
9836 }
9837
9838 /* If we have NEG of something whose two high-order bits are the
9839 same, we know that "(-a) < 0" is equivalent to "a > 0". */
9840 if (num_sign_bit_copies (op0, mode) >= 2)
9841 {
9842 op0 = XEXP (op0, 0);
9843 code = swap_condition (code);
9844 continue;
9845 }
9846 break;
9847
9848 case ROTATE:
9849 /* If we are testing equality and our count is a constant, we
9850 can perform the inverse operation on our RHS. */
9851 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
9852 && (tem = simplify_binary_operation (ROTATERT, mode,
9853 op1, XEXP (op0, 1))) != 0)
9854 {
9855 op0 = XEXP (op0, 0);
9856 op1 = tem;
9857 continue;
9858 }
9859
9860 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
9861 a particular bit. Convert it to an AND of a constant of that
9862 bit. This will be converted into a ZERO_EXTRACT. */
9863 if (const_op == 0 && sign_bit_comparison_p
9864 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9865 && mode_width <= HOST_BITS_PER_WIDE_INT)
9866 {
9867 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
9868 ((HOST_WIDE_INT) 1
9869 << (mode_width - 1
9870 - INTVAL (XEXP (op0, 1)))));
9871 code = (code == LT ? NE : EQ);
9872 continue;
9873 }
9874
9875 /* ... fall through ... */
9876
9877 case ABS:
9878 /* ABS is ignorable inside an equality comparison with zero. */
9879 if (const_op == 0 && equality_comparison_p)
9880 {
9881 op0 = XEXP (op0, 0);
9882 continue;
9883 }
9884 break;
9885
9886
9887 case SIGN_EXTEND:
9888 /* Can simplify (compare (zero/sign_extend FOO) CONST)
9889 to (compare FOO CONST) if CONST fits in FOO's mode and we
9890 are either testing inequality or have an unsigned comparison
9891 with ZERO_EXTEND or a signed comparison with SIGN_EXTEND. */
9892 if (! unsigned_comparison_p
9893 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9894 <= HOST_BITS_PER_WIDE_INT)
9895 && ((unsigned HOST_WIDE_INT) const_op
9896 < (((HOST_WIDE_INT) 1
9897 << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
9898 {
9899 op0 = XEXP (op0, 0);
9900 continue;
9901 }
9902 break;
9903
9904 case SUBREG:
9905 /* Check for the case where we are comparing A - C1 with C2,
9906 both constants are smaller than 1/2 the maximum positive
9907 value in MODE, and the comparison is equality or unsigned.
9908 In that case, if A is either zero-extended to MODE or has
9909 sufficient sign bits so that the high-order bit in MODE
9910 is a copy of the sign in the inner mode, we can prove that it is
9911 safe to do the operation in the wider mode. This simplifies
9912 many range checks. */
9913
9914 if (mode_width <= HOST_BITS_PER_WIDE_INT
9915 && subreg_lowpart_p (op0)
9916 && GET_CODE (SUBREG_REG (op0)) == PLUS
9917 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
9918 && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
9919 && (- INTVAL (XEXP (SUBREG_REG (op0), 1))
9920 < GET_MODE_MASK (mode) / 2)
9921 && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
9922 && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
9923 GET_MODE (SUBREG_REG (op0)))
9924 & ~ GET_MODE_MASK (mode))
9925 || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
9926 GET_MODE (SUBREG_REG (op0)))
9927 > (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
9928 - GET_MODE_BITSIZE (mode)))))
9929 {
9930 op0 = SUBREG_REG (op0);
9931 continue;
9932 }
9933
9934 /* If the inner mode is narrower and we are extracting the low part,
9935 we can treat the SUBREG as if it were a ZERO_EXTEND. */
9936 if (subreg_lowpart_p (op0)
9937 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
9938 /* Fall through */ ;
9939 else
9940 break;
9941
9942 /* ... fall through ... */
9943
9944 case ZERO_EXTEND:
9945 if ((unsigned_comparison_p || equality_comparison_p)
9946 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9947 <= HOST_BITS_PER_WIDE_INT)
9948 && ((unsigned HOST_WIDE_INT) const_op
9949 < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
9950 {
9951 op0 = XEXP (op0, 0);
9952 continue;
9953 }
9954 break;
9955
9956 case PLUS:
9957 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
9958 this for equality comparisons due to pathological cases involving
9959 overflows. */
9960 if (equality_comparison_p
9961 && 0 != (tem = simplify_binary_operation (MINUS, mode,
9962 op1, XEXP (op0, 1))))
9963 {
9964 op0 = XEXP (op0, 0);
9965 op1 = tem;
9966 continue;
9967 }
9968
9969 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
9970 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
9971 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
9972 {
9973 op0 = XEXP (XEXP (op0, 0), 0);
9974 code = (code == LT ? EQ : NE);
9975 continue;
9976 }
9977 break;
9978
9979 case MINUS:
9980 /* (eq (minus A B) C) -> (eq A (plus B C)) or
9981 (eq B (minus A C)), whichever simplifies. We can only do
9982 this for equality comparisons due to pathological cases involving
9983 overflows. */
9984 if (equality_comparison_p
9985 && 0 != (tem = simplify_binary_operation (PLUS, mode,
9986 XEXP (op0, 1), op1)))
9987 {
9988 op0 = XEXP (op0, 0);
9989 op1 = tem;
9990 continue;
9991 }
9992
9993 if (equality_comparison_p
9994 && 0 != (tem = simplify_binary_operation (MINUS, mode,
9995 XEXP (op0, 0), op1)))
9996 {
9997 op0 = XEXP (op0, 1);
9998 op1 = tem;
9999 continue;
10000 }
10001
10002 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10003 of bits in X minus 1, is one iff X > 0. */
10004 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10005 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10006 && INTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
10007 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10008 {
10009 op0 = XEXP (op0, 1);
10010 code = (code == GE ? LE : GT);
10011 continue;
10012 }
10013 break;
10014
10015 case XOR:
10016 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10017 if C is zero or B is a constant. */
10018 if (equality_comparison_p
10019 && 0 != (tem = simplify_binary_operation (XOR, mode,
10020 XEXP (op0, 1), op1)))
10021 {
10022 op0 = XEXP (op0, 0);
10023 op1 = tem;
10024 continue;
10025 }
10026 break;
10027
10028 case EQ: case NE:
10029 case LT: case LTU: case LE: case LEU:
10030 case GT: case GTU: case GE: case GEU:
10031 /* We can't do anything if OP0 is a condition code value, rather
10032 than an actual data value. */
10033 if (const_op != 0
10034 #ifdef HAVE_cc0
10035 || XEXP (op0, 0) == cc0_rtx
10036 #endif
10037 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10038 break;
10039
10040 /* Get the two operands being compared. */
10041 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10042 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10043 else
10044 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10045
10046 /* Check for the cases where we simply want the result of the
10047 earlier test or the opposite of that result. */
10048 if (code == NE
10049 || (code == EQ && reversible_comparison_p (op0))
10050 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10051 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10052 && (STORE_FLAG_VALUE
10053 & (((HOST_WIDE_INT) 1
10054 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10055 && (code == LT
10056 || (code == GE && reversible_comparison_p (op0)))))
10057 {
10058 code = (code == LT || code == NE
10059 ? GET_CODE (op0) : reverse_condition (GET_CODE (op0)));
10060 op0 = tem, op1 = tem1;
10061 continue;
10062 }
10063 break;
10064
10065 case IOR:
10066 /* The sign bit of (ior (plus X (const_int -1)) X) is non-zero
10067 iff X <= 0. */
10068 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10069 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10070 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10071 {
10072 op0 = XEXP (op0, 1);
10073 code = (code == GE ? GT : LE);
10074 continue;
10075 }
10076 break;
10077
10078 case AND:
10079 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10080 will be converted to a ZERO_EXTRACT later. */
10081 if (const_op == 0 && equality_comparison_p
10082 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10083 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10084 {
10085 op0 = simplify_and_const_int
10086 (op0, mode, gen_rtx_combine (LSHIFTRT, mode,
10087 XEXP (op0, 1),
10088 XEXP (XEXP (op0, 0), 1)),
10089 (HOST_WIDE_INT) 1);
10090 continue;
10091 }
10092
10093 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10094 zero and X is a comparison and C1 and C2 describe only bits set
10095 in STORE_FLAG_VALUE, we can compare with X. */
10096 if (const_op == 0 && equality_comparison_p
10097 && mode_width <= HOST_BITS_PER_WIDE_INT
10098 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10099 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10100 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10101 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10102 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10103 {
10104 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10105 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10106 if ((~ STORE_FLAG_VALUE & mask) == 0
10107 && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
10108 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10109 && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
10110 {
10111 op0 = XEXP (XEXP (op0, 0), 0);
10112 continue;
10113 }
10114 }
10115
10116 /* If we are doing an equality comparison of an AND of a bit equal
10117 to the sign bit, replace this with a LT or GE comparison of
10118 the underlying value. */
10119 if (equality_comparison_p
10120 && const_op == 0
10121 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10122 && mode_width <= HOST_BITS_PER_WIDE_INT
10123 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10124 == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10125 {
10126 op0 = XEXP (op0, 0);
10127 code = (code == EQ ? GE : LT);
10128 continue;
10129 }
10130
10131 /* If this AND operation is really a ZERO_EXTEND from a narrower
10132 mode, the constant fits within that mode, and this is either an
10133 equality or unsigned comparison, try to do this comparison in
10134 the narrower mode. */
10135 if ((equality_comparison_p || unsigned_comparison_p)
10136 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10137 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10138 & GET_MODE_MASK (mode))
10139 + 1)) >= 0
10140 && const_op >> i == 0
10141 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10142 {
10143 op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
10144 continue;
10145 }
10146
10147 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1 fits
10148 in both M1 and M2 and the SUBREG is either paradoxical or
10149 represents the low part, permute the SUBREG and the AND and
10150 try again. */
10151 if (GET_CODE (XEXP (op0, 0)) == SUBREG
10152 && ((mode_width
10153 >= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0)))))
10154 || subreg_lowpart_p (XEXP (op0, 0)))
10155 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10156 && mode_width <= HOST_BITS_PER_WIDE_INT
10157 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
10158 <= HOST_BITS_PER_WIDE_INT)
10159 && (INTVAL (XEXP (op0, 1)) & ~ mask) == 0
10160 && 0 == (~ GET_MODE_MASK (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
10161 & INTVAL (XEXP (op0, 1))))
10162
10163 {
10164 op0
10165 = gen_lowpart_for_combine
10166 (mode,
10167 gen_binary (AND, GET_MODE (SUBREG_REG (XEXP (op0, 0))),
10168 SUBREG_REG (XEXP (op0, 0)), XEXP (op0, 1)));
10169 continue;
10170 }
10171
10172 break;
10173
10174 case ASHIFT:
10175 /* If we have (compare (ashift FOO N) (const_int C)) and
10176 the high order N bits of FOO (N+1 if an inequality comparison)
10177 are known to be zero, we can do this by comparing FOO with C
10178 shifted right N bits so long as the low-order N bits of C are
10179 zero. */
10180 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10181 && INTVAL (XEXP (op0, 1)) >= 0
10182 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10183 < HOST_BITS_PER_WIDE_INT)
10184 && ((const_op
10185 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10186 && mode_width <= HOST_BITS_PER_WIDE_INT
10187 && (nonzero_bits (XEXP (op0, 0), mode)
10188 & ~ (mask >> (INTVAL (XEXP (op0, 1))
10189 + ! equality_comparison_p))) == 0)
10190 {
10191 const_op >>= INTVAL (XEXP (op0, 1));
10192 op1 = GEN_INT (const_op);
10193 op0 = XEXP (op0, 0);
10194 continue;
10195 }
10196
10197 /* If we are doing a sign bit comparison, it means we are testing
10198 a particular bit. Convert it to the appropriate AND. */
10199 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10200 && mode_width <= HOST_BITS_PER_WIDE_INT)
10201 {
10202 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10203 ((HOST_WIDE_INT) 1
10204 << (mode_width - 1
10205 - INTVAL (XEXP (op0, 1)))));
10206 code = (code == LT ? NE : EQ);
10207 continue;
10208 }
10209
10210 /* If this an equality comparison with zero and we are shifting
10211 the low bit to the sign bit, we can convert this to an AND of the
10212 low-order bit. */
10213 if (const_op == 0 && equality_comparison_p
10214 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10215 && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10216 {
10217 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10218 (HOST_WIDE_INT) 1);
10219 continue;
10220 }
10221 break;
10222
10223 case ASHIFTRT:
10224 /* If this is an equality comparison with zero, we can do this
10225 as a logical shift, which might be much simpler. */
10226 if (equality_comparison_p && const_op == 0
10227 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10228 {
10229 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10230 XEXP (op0, 0),
10231 INTVAL (XEXP (op0, 1)));
10232 continue;
10233 }
10234
10235 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10236 do the comparison in a narrower mode. */
10237 if (! unsigned_comparison_p
10238 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10239 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10240 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10241 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10242 MODE_INT, 1)) != BLKmode
10243 && ((unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (tmode)
10244 || ((unsigned HOST_WIDE_INT) - const_op
10245 <= GET_MODE_MASK (tmode))))
10246 {
10247 op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
10248 continue;
10249 }
10250
10251 /* ... fall through ... */
10252 case LSHIFTRT:
10253 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10254 the low order N bits of FOO are known to be zero, we can do this
10255 by comparing FOO with C shifted left N bits so long as no
10256 overflow occurs. */
10257 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10258 && INTVAL (XEXP (op0, 1)) >= 0
10259 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10260 && mode_width <= HOST_BITS_PER_WIDE_INT
10261 && (nonzero_bits (XEXP (op0, 0), mode)
10262 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10263 && (const_op == 0
10264 || (floor_log2 (const_op) + INTVAL (XEXP (op0, 1))
10265 < mode_width)))
10266 {
10267 const_op <<= INTVAL (XEXP (op0, 1));
10268 op1 = GEN_INT (const_op);
10269 op0 = XEXP (op0, 0);
10270 continue;
10271 }
10272
10273 /* If we are using this shift to extract just the sign bit, we
10274 can replace this with an LT or GE comparison. */
10275 if (const_op == 0
10276 && (equality_comparison_p || sign_bit_comparison_p)
10277 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10278 && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10279 {
10280 op0 = XEXP (op0, 0);
10281 code = (code == NE || code == GT ? LT : GE);
10282 continue;
10283 }
10284 break;
10285
10286 default:
10287 break;
10288 }
10289
10290 break;
10291 }
10292
10293 /* Now make any compound operations involved in this comparison. Then,
10294 check for an outmost SUBREG on OP0 that is not doing anything or is
10295 paradoxical. The latter case can only occur when it is known that the
10296 "extra" bits will be zero. Therefore, it is safe to remove the SUBREG.
10297 We can never remove a SUBREG for a non-equality comparison because the
10298 sign bit is in a different place in the underlying object. */
10299
10300 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10301 op1 = make_compound_operation (op1, SET);
10302
10303 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10304 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10305 && (code == NE || code == EQ)
10306 && ((GET_MODE_SIZE (GET_MODE (op0))
10307 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))))
10308 {
10309 op0 = SUBREG_REG (op0);
10310 op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
10311 }
10312
10313 else if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10314 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10315 && (code == NE || code == EQ)
10316 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10317 <= HOST_BITS_PER_WIDE_INT)
10318 && (nonzero_bits (SUBREG_REG (op0), GET_MODE (SUBREG_REG (op0)))
10319 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0
10320 && (tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)),
10321 op1),
10322 (nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10323 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0))
10324 op0 = SUBREG_REG (op0), op1 = tem;
10325
10326 /* We now do the opposite procedure: Some machines don't have compare
10327 insns in all modes. If OP0's mode is an integer mode smaller than a
10328 word and we can't do a compare in that mode, see if there is a larger
10329 mode for which we can do the compare. There are a number of cases in
10330 which we can use the wider mode. */
10331
10332 mode = GET_MODE (op0);
10333 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10334 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10335 && cmp_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
10336 for (tmode = GET_MODE_WIDER_MODE (mode);
10337 (tmode != VOIDmode
10338 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
10339 tmode = GET_MODE_WIDER_MODE (tmode))
10340 if (cmp_optab->handlers[(int) tmode].insn_code != CODE_FOR_nothing)
10341 {
10342 /* If the only nonzero bits in OP0 and OP1 are those in the
10343 narrower mode and this is an equality or unsigned comparison,
10344 we can use the wider mode. Similarly for sign-extended
10345 values, in which case it is true for all comparisons. */
10346 if (((code == EQ || code == NE
10347 || code == GEU || code == GTU || code == LEU || code == LTU)
10348 && (nonzero_bits (op0, tmode) & ~ GET_MODE_MASK (mode)) == 0
10349 && (nonzero_bits (op1, tmode) & ~ GET_MODE_MASK (mode)) == 0)
10350 || ((num_sign_bit_copies (op0, tmode)
10351 > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))
10352 && (num_sign_bit_copies (op1, tmode)
10353 > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))))
10354 {
10355 op0 = gen_lowpart_for_combine (tmode, op0);
10356 op1 = gen_lowpart_for_combine (tmode, op1);
10357 break;
10358 }
10359
10360 /* If this is a test for negative, we can make an explicit
10361 test of the sign bit. */
10362
10363 if (op1 == const0_rtx && (code == LT || code == GE)
10364 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10365 {
10366 op0 = gen_binary (AND, tmode,
10367 gen_lowpart_for_combine (tmode, op0),
10368 GEN_INT ((HOST_WIDE_INT) 1
10369 << (GET_MODE_BITSIZE (mode) - 1)));
10370 code = (code == LT) ? NE : EQ;
10371 break;
10372 }
10373 }
10374
10375 #ifdef CANONICALIZE_COMPARISON
10376 /* If this machine only supports a subset of valid comparisons, see if we
10377 can convert an unsupported one into a supported one. */
10378 CANONICALIZE_COMPARISON (code, op0, op1);
10379 #endif
10380
10381 *pop0 = op0;
10382 *pop1 = op1;
10383
10384 return code;
10385 }
10386 \f
10387 /* Return 1 if we know that X, a comparison operation, is not operating
10388 on a floating-point value or is EQ or NE, meaning that we can safely
10389 reverse it. */
10390
10391 static int
10392 reversible_comparison_p (x)
10393 rtx x;
10394 {
10395 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
10396 || flag_fast_math
10397 || GET_CODE (x) == NE || GET_CODE (x) == EQ)
10398 return 1;
10399
10400 switch (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))))
10401 {
10402 case MODE_INT:
10403 case MODE_PARTIAL_INT:
10404 case MODE_COMPLEX_INT:
10405 return 1;
10406
10407 case MODE_CC:
10408 /* If the mode of the condition codes tells us that this is safe,
10409 we need look no further. */
10410 if (REVERSIBLE_CC_MODE (GET_MODE (XEXP (x, 0))))
10411 return 1;
10412
10413 /* Otherwise try and find where the condition codes were last set and
10414 use that. */
10415 x = get_last_value (XEXP (x, 0));
10416 return (x && GET_CODE (x) == COMPARE
10417 && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0))));
10418
10419 default:
10420 return 0;
10421 }
10422 }
10423 \f
10424 /* Utility function for following routine. Called when X is part of a value
10425 being stored into reg_last_set_value. Sets reg_last_set_table_tick
10426 for each register mentioned. Similar to mention_regs in cse.c */
10427
10428 static void
10429 update_table_tick (x)
10430 rtx x;
10431 {
10432 register enum rtx_code code = GET_CODE (x);
10433 register char *fmt = GET_RTX_FORMAT (code);
10434 register int i;
10435
10436 if (code == REG)
10437 {
10438 int regno = REGNO (x);
10439 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10440 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10441
10442 for (i = regno; i < endregno; i++)
10443 reg_last_set_table_tick[i] = label_tick;
10444
10445 return;
10446 }
10447
10448 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10449 /* Note that we can't have an "E" in values stored; see
10450 get_last_value_validate. */
10451 if (fmt[i] == 'e')
10452 update_table_tick (XEXP (x, i));
10453 }
10454
10455 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
10456 are saying that the register is clobbered and we no longer know its
10457 value. If INSN is zero, don't update reg_last_set; this is only permitted
10458 with VALUE also zero and is used to invalidate the register. */
10459
10460 static void
10461 record_value_for_reg (reg, insn, value)
10462 rtx reg;
10463 rtx insn;
10464 rtx value;
10465 {
10466 int regno = REGNO (reg);
10467 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10468 ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
10469 int i;
10470
10471 /* If VALUE contains REG and we have a previous value for REG, substitute
10472 the previous value. */
10473 if (value && insn && reg_overlap_mentioned_p (reg, value))
10474 {
10475 rtx tem;
10476
10477 /* Set things up so get_last_value is allowed to see anything set up to
10478 our insn. */
10479 subst_low_cuid = INSN_CUID (insn);
10480 tem = get_last_value (reg);
10481
10482 if (tem)
10483 value = replace_rtx (copy_rtx (value), reg, tem);
10484 }
10485
10486 /* For each register modified, show we don't know its value, that
10487 we don't know about its bitwise content, that its value has been
10488 updated, and that we don't know the location of the death of the
10489 register. */
10490 for (i = regno; i < endregno; i ++)
10491 {
10492 if (insn)
10493 reg_last_set[i] = insn;
10494 reg_last_set_value[i] = 0;
10495 reg_last_set_mode[i] = 0;
10496 reg_last_set_nonzero_bits[i] = 0;
10497 reg_last_set_sign_bit_copies[i] = 0;
10498 reg_last_death[i] = 0;
10499 }
10500
10501 /* Mark registers that are being referenced in this value. */
10502 if (value)
10503 update_table_tick (value);
10504
10505 /* Now update the status of each register being set.
10506 If someone is using this register in this block, set this register
10507 to invalid since we will get confused between the two lives in this
10508 basic block. This makes using this register always invalid. In cse, we
10509 scan the table to invalidate all entries using this register, but this
10510 is too much work for us. */
10511
10512 for (i = regno; i < endregno; i++)
10513 {
10514 reg_last_set_label[i] = label_tick;
10515 if (value && reg_last_set_table_tick[i] == label_tick)
10516 reg_last_set_invalid[i] = 1;
10517 else
10518 reg_last_set_invalid[i] = 0;
10519 }
10520
10521 /* The value being assigned might refer to X (like in "x++;"). In that
10522 case, we must replace it with (clobber (const_int 0)) to prevent
10523 infinite loops. */
10524 if (value && ! get_last_value_validate (&value, insn,
10525 reg_last_set_label[regno], 0))
10526 {
10527 value = copy_rtx (value);
10528 if (! get_last_value_validate (&value, insn,
10529 reg_last_set_label[regno], 1))
10530 value = 0;
10531 }
10532
10533 /* For the main register being modified, update the value, the mode, the
10534 nonzero bits, and the number of sign bit copies. */
10535
10536 reg_last_set_value[regno] = value;
10537
10538 if (value)
10539 {
10540 subst_low_cuid = INSN_CUID (insn);
10541 reg_last_set_mode[regno] = GET_MODE (reg);
10542 reg_last_set_nonzero_bits[regno] = nonzero_bits (value, GET_MODE (reg));
10543 reg_last_set_sign_bit_copies[regno]
10544 = num_sign_bit_copies (value, GET_MODE (reg));
10545 }
10546 }
10547
10548 /* Used for communication between the following two routines. */
10549 static rtx record_dead_insn;
10550
10551 /* Called via note_stores from record_dead_and_set_regs to handle one
10552 SET or CLOBBER in an insn. */
10553
10554 static void
10555 record_dead_and_set_regs_1 (dest, setter)
10556 rtx dest, setter;
10557 {
10558 if (GET_CODE (dest) == SUBREG)
10559 dest = SUBREG_REG (dest);
10560
10561 if (GET_CODE (dest) == REG)
10562 {
10563 /* If we are setting the whole register, we know its value. Otherwise
10564 show that we don't know the value. We can handle SUBREG in
10565 some cases. */
10566 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
10567 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
10568 else if (GET_CODE (setter) == SET
10569 && GET_CODE (SET_DEST (setter)) == SUBREG
10570 && SUBREG_REG (SET_DEST (setter)) == dest
10571 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
10572 && subreg_lowpart_p (SET_DEST (setter)))
10573 record_value_for_reg (dest, record_dead_insn,
10574 gen_lowpart_for_combine (GET_MODE (dest),
10575 SET_SRC (setter)));
10576 else
10577 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
10578 }
10579 else if (GET_CODE (dest) == MEM
10580 /* Ignore pushes, they clobber nothing. */
10581 && ! push_operand (dest, GET_MODE (dest)))
10582 mem_last_set = INSN_CUID (record_dead_insn);
10583 }
10584
10585 /* Update the records of when each REG was most recently set or killed
10586 for the things done by INSN. This is the last thing done in processing
10587 INSN in the combiner loop.
10588
10589 We update reg_last_set, reg_last_set_value, reg_last_set_mode,
10590 reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
10591 and also the similar information mem_last_set (which insn most recently
10592 modified memory) and last_call_cuid (which insn was the most recent
10593 subroutine call). */
10594
10595 static void
10596 record_dead_and_set_regs (insn)
10597 rtx insn;
10598 {
10599 register rtx link;
10600 int i;
10601
10602 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
10603 {
10604 if (REG_NOTE_KIND (link) == REG_DEAD
10605 && GET_CODE (XEXP (link, 0)) == REG)
10606 {
10607 int regno = REGNO (XEXP (link, 0));
10608 int endregno
10609 = regno + (regno < FIRST_PSEUDO_REGISTER
10610 ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
10611 : 1);
10612
10613 for (i = regno; i < endregno; i++)
10614 reg_last_death[i] = insn;
10615 }
10616 else if (REG_NOTE_KIND (link) == REG_INC)
10617 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
10618 }
10619
10620 if (GET_CODE (insn) == CALL_INSN)
10621 {
10622 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
10623 if (call_used_regs[i])
10624 {
10625 reg_last_set_value[i] = 0;
10626 reg_last_set_mode[i] = 0;
10627 reg_last_set_nonzero_bits[i] = 0;
10628 reg_last_set_sign_bit_copies[i] = 0;
10629 reg_last_death[i] = 0;
10630 }
10631
10632 last_call_cuid = mem_last_set = INSN_CUID (insn);
10633 }
10634
10635 record_dead_insn = insn;
10636 note_stores (PATTERN (insn), record_dead_and_set_regs_1);
10637 }
10638 \f
10639 /* Utility routine for the following function. Verify that all the registers
10640 mentioned in *LOC are valid when *LOC was part of a value set when
10641 label_tick == TICK. Return 0 if some are not.
10642
10643 If REPLACE is non-zero, replace the invalid reference with
10644 (clobber (const_int 0)) and return 1. This replacement is useful because
10645 we often can get useful information about the form of a value (e.g., if
10646 it was produced by a shift that always produces -1 or 0) even though
10647 we don't know exactly what registers it was produced from. */
10648
10649 static int
10650 get_last_value_validate (loc, insn, tick, replace)
10651 rtx *loc;
10652 rtx insn;
10653 int tick;
10654 int replace;
10655 {
10656 rtx x = *loc;
10657 char *fmt = GET_RTX_FORMAT (GET_CODE (x));
10658 int len = GET_RTX_LENGTH (GET_CODE (x));
10659 int i;
10660
10661 if (GET_CODE (x) == REG)
10662 {
10663 int regno = REGNO (x);
10664 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10665 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10666 int j;
10667
10668 for (j = regno; j < endregno; j++)
10669 if (reg_last_set_invalid[j]
10670 /* If this is a pseudo-register that was only set once, it is
10671 always valid. */
10672 || (! (regno >= FIRST_PSEUDO_REGISTER && REG_N_SETS (regno) == 1)
10673 && reg_last_set_label[j] > tick))
10674 {
10675 if (replace)
10676 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10677 return replace;
10678 }
10679
10680 return 1;
10681 }
10682 /* If this is a memory reference, make sure that there were
10683 no stores after it that might have clobbered the value. We don't
10684 have alias info, so we assume any store invalidates it. */
10685 else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
10686 && INSN_CUID (insn) <= mem_last_set)
10687 {
10688 if (replace)
10689 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10690 return replace;
10691 }
10692
10693 for (i = 0; i < len; i++)
10694 if ((fmt[i] == 'e'
10695 && get_last_value_validate (&XEXP (x, i), insn, tick, replace) == 0)
10696 /* Don't bother with these. They shouldn't occur anyway. */
10697 || fmt[i] == 'E')
10698 return 0;
10699
10700 /* If we haven't found a reason for it to be invalid, it is valid. */
10701 return 1;
10702 }
10703
10704 /* Get the last value assigned to X, if known. Some registers
10705 in the value may be replaced with (clobber (const_int 0)) if their value
10706 is known longer known reliably. */
10707
10708 static rtx
10709 get_last_value (x)
10710 rtx x;
10711 {
10712 int regno;
10713 rtx value;
10714
10715 /* If this is a non-paradoxical SUBREG, get the value of its operand and
10716 then convert it to the desired mode. If this is a paradoxical SUBREG,
10717 we cannot predict what values the "extra" bits might have. */
10718 if (GET_CODE (x) == SUBREG
10719 && subreg_lowpart_p (x)
10720 && (GET_MODE_SIZE (GET_MODE (x))
10721 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
10722 && (value = get_last_value (SUBREG_REG (x))) != 0)
10723 return gen_lowpart_for_combine (GET_MODE (x), value);
10724
10725 if (GET_CODE (x) != REG)
10726 return 0;
10727
10728 regno = REGNO (x);
10729 value = reg_last_set_value[regno];
10730
10731 /* If we don't have a value or if it isn't for this basic block,
10732 return 0. */
10733
10734 if (value == 0
10735 || (REG_N_SETS (regno) != 1
10736 && reg_last_set_label[regno] != label_tick))
10737 return 0;
10738
10739 /* If the value was set in a later insn than the ones we are processing,
10740 we can't use it even if the register was only set once, but make a quick
10741 check to see if the previous insn set it to something. This is commonly
10742 the case when the same pseudo is used by repeated insns.
10743
10744 This does not work if there exists an instruction which is temporarily
10745 not on the insn chain. */
10746
10747 if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
10748 {
10749 rtx insn, set;
10750
10751 /* We can not do anything useful in this case, because there is
10752 an instruction which is not on the insn chain. */
10753 if (subst_prev_insn)
10754 return 0;
10755
10756 /* Skip over USE insns. They are not useful here, and they may have
10757 been made by combine, in which case they do not have a INSN_CUID
10758 value. We can't use prev_real_insn, because that would incorrectly
10759 take us backwards across labels. Skip over BARRIERs also, since
10760 they could have been made by combine. If we see one, we must be
10761 optimizing dead code, so it doesn't matter what we do. */
10762 for (insn = prev_nonnote_insn (subst_insn);
10763 insn && ((GET_CODE (insn) == INSN
10764 && GET_CODE (PATTERN (insn)) == USE)
10765 || GET_CODE (insn) == BARRIER
10766 || INSN_CUID (insn) >= subst_low_cuid);
10767 insn = prev_nonnote_insn (insn))
10768 ;
10769
10770 if (insn
10771 && (set = single_set (insn)) != 0
10772 && rtx_equal_p (SET_DEST (set), x))
10773 {
10774 value = SET_SRC (set);
10775
10776 /* Make sure that VALUE doesn't reference X. Replace any
10777 explicit references with a CLOBBER. If there are any remaining
10778 references (rare), don't use the value. */
10779
10780 if (reg_mentioned_p (x, value))
10781 value = replace_rtx (copy_rtx (value), x,
10782 gen_rtx_CLOBBER (GET_MODE (x), const0_rtx));
10783
10784 if (reg_overlap_mentioned_p (x, value))
10785 return 0;
10786 }
10787 else
10788 return 0;
10789 }
10790
10791 /* If the value has all its registers valid, return it. */
10792 if (get_last_value_validate (&value, reg_last_set[regno],
10793 reg_last_set_label[regno], 0))
10794 return value;
10795
10796 /* Otherwise, make a copy and replace any invalid register with
10797 (clobber (const_int 0)). If that fails for some reason, return 0. */
10798
10799 value = copy_rtx (value);
10800 if (get_last_value_validate (&value, reg_last_set[regno],
10801 reg_last_set_label[regno], 1))
10802 return value;
10803
10804 return 0;
10805 }
10806 \f
10807 /* Return nonzero if expression X refers to a REG or to memory
10808 that is set in an instruction more recent than FROM_CUID. */
10809
10810 static int
10811 use_crosses_set_p (x, from_cuid)
10812 register rtx x;
10813 int from_cuid;
10814 {
10815 register char *fmt;
10816 register int i;
10817 register enum rtx_code code = GET_CODE (x);
10818
10819 if (code == REG)
10820 {
10821 register int regno = REGNO (x);
10822 int endreg = regno + (regno < FIRST_PSEUDO_REGISTER
10823 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10824
10825 #ifdef PUSH_ROUNDING
10826 /* Don't allow uses of the stack pointer to be moved,
10827 because we don't know whether the move crosses a push insn. */
10828 if (regno == STACK_POINTER_REGNUM)
10829 return 1;
10830 #endif
10831 for (;regno < endreg; regno++)
10832 if (reg_last_set[regno]
10833 && INSN_CUID (reg_last_set[regno]) > from_cuid)
10834 return 1;
10835 return 0;
10836 }
10837
10838 if (code == MEM && mem_last_set > from_cuid)
10839 return 1;
10840
10841 fmt = GET_RTX_FORMAT (code);
10842
10843 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10844 {
10845 if (fmt[i] == 'E')
10846 {
10847 register int j;
10848 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10849 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
10850 return 1;
10851 }
10852 else if (fmt[i] == 'e'
10853 && use_crosses_set_p (XEXP (x, i), from_cuid))
10854 return 1;
10855 }
10856 return 0;
10857 }
10858 \f
10859 /* Define three variables used for communication between the following
10860 routines. */
10861
10862 static int reg_dead_regno, reg_dead_endregno;
10863 static int reg_dead_flag;
10864
10865 /* Function called via note_stores from reg_dead_at_p.
10866
10867 If DEST is within [reg_dead_regno, reg_dead_endregno), set
10868 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
10869
10870 static void
10871 reg_dead_at_p_1 (dest, x)
10872 rtx dest;
10873 rtx x;
10874 {
10875 int regno, endregno;
10876
10877 if (GET_CODE (dest) != REG)
10878 return;
10879
10880 regno = REGNO (dest);
10881 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10882 ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
10883
10884 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
10885 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
10886 }
10887
10888 /* Return non-zero if REG is known to be dead at INSN.
10889
10890 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
10891 referencing REG, it is dead. If we hit a SET referencing REG, it is
10892 live. Otherwise, see if it is live or dead at the start of the basic
10893 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
10894 must be assumed to be always live. */
10895
10896 static int
10897 reg_dead_at_p (reg, insn)
10898 rtx reg;
10899 rtx insn;
10900 {
10901 int block, i;
10902
10903 /* Set variables for reg_dead_at_p_1. */
10904 reg_dead_regno = REGNO (reg);
10905 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
10906 ? HARD_REGNO_NREGS (reg_dead_regno,
10907 GET_MODE (reg))
10908 : 1);
10909
10910 reg_dead_flag = 0;
10911
10912 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. */
10913 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
10914 {
10915 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
10916 if (TEST_HARD_REG_BIT (newpat_used_regs, i))
10917 return 0;
10918 }
10919
10920 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
10921 beginning of function. */
10922 for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
10923 insn = prev_nonnote_insn (insn))
10924 {
10925 note_stores (PATTERN (insn), reg_dead_at_p_1);
10926 if (reg_dead_flag)
10927 return reg_dead_flag == 1 ? 1 : 0;
10928
10929 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
10930 return 1;
10931 }
10932
10933 /* Get the basic block number that we were in. */
10934 if (insn == 0)
10935 block = 0;
10936 else
10937 {
10938 for (block = 0; block < n_basic_blocks; block++)
10939 if (insn == basic_block_head[block])
10940 break;
10941
10942 if (block == n_basic_blocks)
10943 return 0;
10944 }
10945
10946 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
10947 if (REGNO_REG_SET_P (basic_block_live_at_start[block], i))
10948 return 0;
10949
10950 return 1;
10951 }
10952 \f
10953 /* Note hard registers in X that are used. This code is similar to
10954 that in flow.c, but much simpler since we don't care about pseudos. */
10955
10956 static void
10957 mark_used_regs_combine (x)
10958 rtx x;
10959 {
10960 register RTX_CODE code = GET_CODE (x);
10961 register int regno;
10962 int i;
10963
10964 switch (code)
10965 {
10966 case LABEL_REF:
10967 case SYMBOL_REF:
10968 case CONST_INT:
10969 case CONST:
10970 case CONST_DOUBLE:
10971 case PC:
10972 case ADDR_VEC:
10973 case ADDR_DIFF_VEC:
10974 case ASM_INPUT:
10975 #ifdef HAVE_cc0
10976 /* CC0 must die in the insn after it is set, so we don't need to take
10977 special note of it here. */
10978 case CC0:
10979 #endif
10980 return;
10981
10982 case CLOBBER:
10983 /* If we are clobbering a MEM, mark any hard registers inside the
10984 address as used. */
10985 if (GET_CODE (XEXP (x, 0)) == MEM)
10986 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
10987 return;
10988
10989 case REG:
10990 regno = REGNO (x);
10991 /* A hard reg in a wide mode may really be multiple registers.
10992 If so, mark all of them just like the first. */
10993 if (regno < FIRST_PSEUDO_REGISTER)
10994 {
10995 /* None of this applies to the stack, frame or arg pointers */
10996 if (regno == STACK_POINTER_REGNUM
10997 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10998 || regno == HARD_FRAME_POINTER_REGNUM
10999 #endif
11000 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11001 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11002 #endif
11003 || regno == FRAME_POINTER_REGNUM)
11004 return;
11005
11006 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
11007 while (i-- > 0)
11008 SET_HARD_REG_BIT (newpat_used_regs, regno + i);
11009 }
11010 return;
11011
11012 case SET:
11013 {
11014 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11015 the address. */
11016 register rtx testreg = SET_DEST (x);
11017
11018 while (GET_CODE (testreg) == SUBREG
11019 || GET_CODE (testreg) == ZERO_EXTRACT
11020 || GET_CODE (testreg) == SIGN_EXTRACT
11021 || GET_CODE (testreg) == STRICT_LOW_PART)
11022 testreg = XEXP (testreg, 0);
11023
11024 if (GET_CODE (testreg) == MEM)
11025 mark_used_regs_combine (XEXP (testreg, 0));
11026
11027 mark_used_regs_combine (SET_SRC (x));
11028 }
11029 return;
11030
11031 default:
11032 break;
11033 }
11034
11035 /* Recursively scan the operands of this expression. */
11036
11037 {
11038 register char *fmt = GET_RTX_FORMAT (code);
11039
11040 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11041 {
11042 if (fmt[i] == 'e')
11043 mark_used_regs_combine (XEXP (x, i));
11044 else if (fmt[i] == 'E')
11045 {
11046 register int j;
11047
11048 for (j = 0; j < XVECLEN (x, i); j++)
11049 mark_used_regs_combine (XVECEXP (x, i, j));
11050 }
11051 }
11052 }
11053 }
11054
11055 \f
11056 /* Remove register number REGNO from the dead registers list of INSN.
11057
11058 Return the note used to record the death, if there was one. */
11059
11060 rtx
11061 remove_death (regno, insn)
11062 int regno;
11063 rtx insn;
11064 {
11065 register rtx note = find_regno_note (insn, REG_DEAD, regno);
11066
11067 if (note)
11068 {
11069 REG_N_DEATHS (regno)--;
11070 remove_note (insn, note);
11071 }
11072
11073 return note;
11074 }
11075
11076 /* For each register (hardware or pseudo) used within expression X, if its
11077 death is in an instruction with cuid between FROM_CUID (inclusive) and
11078 TO_INSN (exclusive), put a REG_DEAD note for that register in the
11079 list headed by PNOTES.
11080
11081 That said, don't move registers killed by maybe_kill_insn.
11082
11083 This is done when X is being merged by combination into TO_INSN. These
11084 notes will then be distributed as needed. */
11085
11086 static void
11087 move_deaths (x, maybe_kill_insn, from_cuid, to_insn, pnotes)
11088 rtx x;
11089 rtx maybe_kill_insn;
11090 int from_cuid;
11091 rtx to_insn;
11092 rtx *pnotes;
11093 {
11094 register char *fmt;
11095 register int len, i;
11096 register enum rtx_code code = GET_CODE (x);
11097
11098 if (code == REG)
11099 {
11100 register int regno = REGNO (x);
11101 register rtx where_dead = reg_last_death[regno];
11102 register rtx before_dead, after_dead;
11103
11104 /* Don't move the register if it gets killed in between from and to */
11105 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11106 && !reg_referenced_p (x, maybe_kill_insn))
11107 return;
11108
11109 /* WHERE_DEAD could be a USE insn made by combine, so first we
11110 make sure that we have insns with valid INSN_CUID values. */
11111 before_dead = where_dead;
11112 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
11113 before_dead = PREV_INSN (before_dead);
11114 after_dead = where_dead;
11115 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
11116 after_dead = NEXT_INSN (after_dead);
11117
11118 if (before_dead && after_dead
11119 && INSN_CUID (before_dead) >= from_cuid
11120 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
11121 || (where_dead != after_dead
11122 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
11123 {
11124 rtx note = remove_death (regno, where_dead);
11125
11126 /* It is possible for the call above to return 0. This can occur
11127 when reg_last_death points to I2 or I1 that we combined with.
11128 In that case make a new note.
11129
11130 We must also check for the case where X is a hard register
11131 and NOTE is a death note for a range of hard registers
11132 including X. In that case, we must put REG_DEAD notes for
11133 the remaining registers in place of NOTE. */
11134
11135 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
11136 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11137 > GET_MODE_SIZE (GET_MODE (x))))
11138 {
11139 int deadregno = REGNO (XEXP (note, 0));
11140 int deadend
11141 = (deadregno + HARD_REGNO_NREGS (deadregno,
11142 GET_MODE (XEXP (note, 0))));
11143 int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11144 int i;
11145
11146 for (i = deadregno; i < deadend; i++)
11147 if (i < regno || i >= ourend)
11148 REG_NOTES (where_dead)
11149 = gen_rtx_EXPR_LIST (REG_DEAD,
11150 gen_rtx_REG (reg_raw_mode[i], i),
11151 REG_NOTES (where_dead));
11152 }
11153 /* If we didn't find any note, or if we found a REG_DEAD note that
11154 covers only part of the given reg, and we have a multi-reg hard
11155 register, then to be safe we must check for REG_DEAD notes
11156 for each register other than the first. They could have
11157 their own REG_DEAD notes lying around. */
11158 else if ((note == 0
11159 || (note != 0
11160 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11161 < GET_MODE_SIZE (GET_MODE (x)))))
11162 && regno < FIRST_PSEUDO_REGISTER
11163 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
11164 {
11165 int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11166 int i, offset;
11167 rtx oldnotes = 0;
11168
11169 if (note)
11170 offset = HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0)));
11171 else
11172 offset = 1;
11173
11174 for (i = regno + offset; i < ourend; i++)
11175 move_deaths (gen_rtx_REG (reg_raw_mode[i], i),
11176 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
11177 }
11178
11179 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
11180 {
11181 XEXP (note, 1) = *pnotes;
11182 *pnotes = note;
11183 }
11184 else
11185 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
11186
11187 REG_N_DEATHS (regno)++;
11188 }
11189
11190 return;
11191 }
11192
11193 else if (GET_CODE (x) == SET)
11194 {
11195 rtx dest = SET_DEST (x);
11196
11197 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
11198
11199 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
11200 that accesses one word of a multi-word item, some
11201 piece of everything register in the expression is used by
11202 this insn, so remove any old death. */
11203
11204 if (GET_CODE (dest) == ZERO_EXTRACT
11205 || GET_CODE (dest) == STRICT_LOW_PART
11206 || (GET_CODE (dest) == SUBREG
11207 && (((GET_MODE_SIZE (GET_MODE (dest))
11208 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
11209 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
11210 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
11211 {
11212 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
11213 return;
11214 }
11215
11216 /* If this is some other SUBREG, we know it replaces the entire
11217 value, so use that as the destination. */
11218 if (GET_CODE (dest) == SUBREG)
11219 dest = SUBREG_REG (dest);
11220
11221 /* If this is a MEM, adjust deaths of anything used in the address.
11222 For a REG (the only other possibility), the entire value is
11223 being replaced so the old value is not used in this insn. */
11224
11225 if (GET_CODE (dest) == MEM)
11226 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
11227 to_insn, pnotes);
11228 return;
11229 }
11230
11231 else if (GET_CODE (x) == CLOBBER)
11232 return;
11233
11234 len = GET_RTX_LENGTH (code);
11235 fmt = GET_RTX_FORMAT (code);
11236
11237 for (i = 0; i < len; i++)
11238 {
11239 if (fmt[i] == 'E')
11240 {
11241 register int j;
11242 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11243 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
11244 to_insn, pnotes);
11245 }
11246 else if (fmt[i] == 'e')
11247 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
11248 }
11249 }
11250 \f
11251 /* Return 1 if X is the target of a bit-field assignment in BODY, the
11252 pattern of an insn. X must be a REG. */
11253
11254 static int
11255 reg_bitfield_target_p (x, body)
11256 rtx x;
11257 rtx body;
11258 {
11259 int i;
11260
11261 if (GET_CODE (body) == SET)
11262 {
11263 rtx dest = SET_DEST (body);
11264 rtx target;
11265 int regno, tregno, endregno, endtregno;
11266
11267 if (GET_CODE (dest) == ZERO_EXTRACT)
11268 target = XEXP (dest, 0);
11269 else if (GET_CODE (dest) == STRICT_LOW_PART)
11270 target = SUBREG_REG (XEXP (dest, 0));
11271 else
11272 return 0;
11273
11274 if (GET_CODE (target) == SUBREG)
11275 target = SUBREG_REG (target);
11276
11277 if (GET_CODE (target) != REG)
11278 return 0;
11279
11280 tregno = REGNO (target), regno = REGNO (x);
11281 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
11282 return target == x;
11283
11284 endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
11285 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11286
11287 return endregno > tregno && regno < endtregno;
11288 }
11289
11290 else if (GET_CODE (body) == PARALLEL)
11291 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
11292 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
11293 return 1;
11294
11295 return 0;
11296 }
11297 \f
11298 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
11299 as appropriate. I3 and I2 are the insns resulting from the combination
11300 insns including FROM (I2 may be zero).
11301
11302 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
11303 not need REG_DEAD notes because they are being substituted for. This
11304 saves searching in the most common cases.
11305
11306 Each note in the list is either ignored or placed on some insns, depending
11307 on the type of note. */
11308
11309 static void
11310 distribute_notes (notes, from_insn, i3, i2, elim_i2, elim_i1)
11311 rtx notes;
11312 rtx from_insn;
11313 rtx i3, i2;
11314 rtx elim_i2, elim_i1;
11315 {
11316 rtx note, next_note;
11317 rtx tem;
11318
11319 for (note = notes; note; note = next_note)
11320 {
11321 rtx place = 0, place2 = 0;
11322
11323 /* If this NOTE references a pseudo register, ensure it references
11324 the latest copy of that register. */
11325 if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
11326 && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11327 XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11328
11329 next_note = XEXP (note, 1);
11330 switch (REG_NOTE_KIND (note))
11331 {
11332 case REG_BR_PROB:
11333 case REG_EXEC_COUNT:
11334 /* Doesn't matter much where we put this, as long as it's somewhere.
11335 It is preferable to keep these notes on branches, which is most
11336 likely to be i3. */
11337 place = i3;
11338 break;
11339
11340 case REG_UNUSED:
11341 /* Any clobbers for i3 may still exist, and so we must process
11342 REG_UNUSED notes from that insn.
11343
11344 Any clobbers from i2 or i1 can only exist if they were added by
11345 recog_for_combine. In that case, recog_for_combine created the
11346 necessary REG_UNUSED notes. Trying to keep any original
11347 REG_UNUSED notes from these insns can cause incorrect output
11348 if it is for the same register as the original i3 dest.
11349 In that case, we will notice that the register is set in i3,
11350 and then add a REG_UNUSED note for the destination of i3, which
11351 is wrong. However, it is possible to have REG_UNUSED notes from
11352 i2 or i1 for register which were both used and clobbered, so
11353 we keep notes from i2 or i1 if they will turn into REG_DEAD
11354 notes. */
11355
11356 /* If this register is set or clobbered in I3, put the note there
11357 unless there is one already. */
11358 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
11359 {
11360 if (from_insn != i3)
11361 break;
11362
11363 if (! (GET_CODE (XEXP (note, 0)) == REG
11364 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11365 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11366 place = i3;
11367 }
11368 /* Otherwise, if this register is used by I3, then this register
11369 now dies here, so we must put a REG_DEAD note here unless there
11370 is one already. */
11371 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11372 && ! (GET_CODE (XEXP (note, 0)) == REG
11373 ? find_regno_note (i3, REG_DEAD, REGNO (XEXP (note, 0)))
11374 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11375 {
11376 PUT_REG_NOTE_KIND (note, REG_DEAD);
11377 place = i3;
11378 }
11379 break;
11380
11381 case REG_EQUAL:
11382 case REG_EQUIV:
11383 case REG_NONNEG:
11384 case REG_NOALIAS:
11385 /* These notes say something about results of an insn. We can
11386 only support them if they used to be on I3 in which case they
11387 remain on I3. Otherwise they are ignored.
11388
11389 If the note refers to an expression that is not a constant, we
11390 must also ignore the note since we cannot tell whether the
11391 equivalence is still true. It might be possible to do
11392 slightly better than this (we only have a problem if I2DEST
11393 or I1DEST is present in the expression), but it doesn't
11394 seem worth the trouble. */
11395
11396 if (from_insn == i3
11397 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
11398 place = i3;
11399 break;
11400
11401 case REG_INC:
11402 case REG_NO_CONFLICT:
11403 case REG_LABEL:
11404 /* These notes say something about how a register is used. They must
11405 be present on any use of the register in I2 or I3. */
11406 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
11407 place = i3;
11408
11409 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
11410 {
11411 if (place)
11412 place2 = i2;
11413 else
11414 place = i2;
11415 }
11416 break;
11417
11418 case REG_WAS_0:
11419 /* It is too much trouble to try to see if this note is still
11420 correct in all situations. It is better to simply delete it. */
11421 break;
11422
11423 case REG_RETVAL:
11424 /* If the insn previously containing this note still exists,
11425 put it back where it was. Otherwise move it to the previous
11426 insn. Adjust the corresponding REG_LIBCALL note. */
11427 if (GET_CODE (from_insn) != NOTE)
11428 place = from_insn;
11429 else
11430 {
11431 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
11432 place = prev_real_insn (from_insn);
11433 if (tem && place)
11434 XEXP (tem, 0) = place;
11435 }
11436 break;
11437
11438 case REG_LIBCALL:
11439 /* This is handled similarly to REG_RETVAL. */
11440 if (GET_CODE (from_insn) != NOTE)
11441 place = from_insn;
11442 else
11443 {
11444 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
11445 place = next_real_insn (from_insn);
11446 if (tem && place)
11447 XEXP (tem, 0) = place;
11448 }
11449 break;
11450
11451 case REG_DEAD:
11452 /* If the register is used as an input in I3, it dies there.
11453 Similarly for I2, if it is non-zero and adjacent to I3.
11454
11455 If the register is not used as an input in either I3 or I2
11456 and it is not one of the registers we were supposed to eliminate,
11457 there are two possibilities. We might have a non-adjacent I2
11458 or we might have somehow eliminated an additional register
11459 from a computation. For example, we might have had A & B where
11460 we discover that B will always be zero. In this case we will
11461 eliminate the reference to A.
11462
11463 In both cases, we must search to see if we can find a previous
11464 use of A and put the death note there. */
11465
11466 if (from_insn
11467 && GET_CODE (from_insn) == CALL_INSN
11468 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
11469 place = from_insn;
11470 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
11471 place = i3;
11472 else if (i2 != 0 && next_nonnote_insn (i2) == i3
11473 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11474 place = i2;
11475
11476 if (XEXP (note, 0) == elim_i2 || XEXP (note, 0) == elim_i1)
11477 break;
11478
11479 /* If the register is used in both I2 and I3 and it dies in I3,
11480 we might have added another reference to it. If reg_n_refs
11481 was 2, bump it to 3. This has to be correct since the
11482 register must have been set somewhere. The reason this is
11483 done is because local-alloc.c treats 2 references as a
11484 special case. */
11485
11486 if (place == i3 && i2 != 0 && GET_CODE (XEXP (note, 0)) == REG
11487 && REG_N_REFS (REGNO (XEXP (note, 0)))== 2
11488 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11489 REG_N_REFS (REGNO (XEXP (note, 0))) = 3;
11490
11491 if (place == 0)
11492 {
11493 for (tem = prev_nonnote_insn (i3);
11494 place == 0 && tem
11495 && (GET_CODE (tem) == INSN || GET_CODE (tem) == CALL_INSN);
11496 tem = prev_nonnote_insn (tem))
11497 {
11498 /* If the register is being set at TEM, see if that is all
11499 TEM is doing. If so, delete TEM. Otherwise, make this
11500 into a REG_UNUSED note instead. */
11501 if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
11502 {
11503 rtx set = single_set (tem);
11504 rtx inner_dest = 0;
11505
11506 if (set != 0)
11507 for (inner_dest = SET_DEST (set);
11508 GET_CODE (inner_dest) == STRICT_LOW_PART
11509 || GET_CODE (inner_dest) == SUBREG
11510 || GET_CODE (inner_dest) == ZERO_EXTRACT;
11511 inner_dest = XEXP (inner_dest, 0))
11512 ;
11513
11514 /* Verify that it was the set, and not a clobber that
11515 modified the register. */
11516
11517 if (set != 0 && ! side_effects_p (SET_SRC (set))
11518 && rtx_equal_p (XEXP (note, 0), inner_dest))
11519 {
11520 /* Move the notes and links of TEM elsewhere.
11521 This might delete other dead insns recursively.
11522 First set the pattern to something that won't use
11523 any register. */
11524
11525 PATTERN (tem) = pc_rtx;
11526
11527 distribute_notes (REG_NOTES (tem), tem, tem,
11528 NULL_RTX, NULL_RTX, NULL_RTX);
11529 distribute_links (LOG_LINKS (tem));
11530
11531 PUT_CODE (tem, NOTE);
11532 NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
11533 NOTE_SOURCE_FILE (tem) = 0;
11534 }
11535 /* If the register is both set and used here, put the
11536 REG_DEAD note here, but place a REG_UNUSED note
11537 here too unless there already is one. */
11538 else if (reg_referenced_p (XEXP (note, 0),
11539 PATTERN (tem)))
11540 {
11541 place = tem;
11542
11543 if (! find_regno_note (tem, REG_UNUSED,
11544 REGNO (XEXP (note, 0))))
11545 REG_NOTES (tem)
11546 = gen_rtx (EXPR_LIST, REG_UNUSED, XEXP (note, 0),
11547 REG_NOTES (tem));
11548 }
11549 else
11550 {
11551 PUT_REG_NOTE_KIND (note, REG_UNUSED);
11552
11553 /* If there isn't already a REG_UNUSED note, put one
11554 here. */
11555 if (! find_regno_note (tem, REG_UNUSED,
11556 REGNO (XEXP (note, 0))))
11557 place = tem;
11558 break;
11559 }
11560 }
11561 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
11562 || (GET_CODE (tem) == CALL_INSN
11563 && find_reg_fusage (tem, USE, XEXP (note, 0))))
11564 {
11565 place = tem;
11566
11567 /* If we are doing a 3->2 combination, and we have a
11568 register which formerly died in i3 and was not used
11569 by i2, which now no longer dies in i3 and is used in
11570 i2 but does not die in i2, and place is between i2
11571 and i3, then we may need to move a link from place to
11572 i2. */
11573 if (i2 && INSN_UID (place) <= max_uid_cuid
11574 && INSN_CUID (place) > INSN_CUID (i2)
11575 && from_insn && INSN_CUID (from_insn) > INSN_CUID (i2)
11576 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11577 {
11578 rtx links = LOG_LINKS (place);
11579 LOG_LINKS (place) = 0;
11580 distribute_links (links);
11581 }
11582 break;
11583 }
11584 }
11585
11586 /* If we haven't found an insn for the death note and it
11587 is still a REG_DEAD note, but we have hit a CODE_LABEL,
11588 insert a USE insn for the register at that label and
11589 put the death node there. This prevents problems with
11590 call-state tracking in caller-save.c. */
11591 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0 && tem != 0)
11592 {
11593 place
11594 = emit_insn_after (gen_rtx_USE (VOIDmode, XEXP (note, 0)),
11595 tem);
11596
11597 /* If this insn was emitted between blocks, then update
11598 basic_block_head of the current block to include it. */
11599 if (basic_block_end[this_basic_block - 1] == tem)
11600 basic_block_head[this_basic_block] = place;
11601 }
11602 }
11603
11604 /* If the register is set or already dead at PLACE, we needn't do
11605 anything with this note if it is still a REG_DEAD note.
11606 We can here if it is set at all, not if is it totally replace,
11607 which is what `dead_or_set_p' checks, so also check for it being
11608 set partially. */
11609
11610
11611 if (place && REG_NOTE_KIND (note) == REG_DEAD)
11612 {
11613 int regno = REGNO (XEXP (note, 0));
11614
11615 if (dead_or_set_p (place, XEXP (note, 0))
11616 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
11617 {
11618 /* Unless the register previously died in PLACE, clear
11619 reg_last_death. [I no longer understand why this is
11620 being done.] */
11621 if (reg_last_death[regno] != place)
11622 reg_last_death[regno] = 0;
11623 place = 0;
11624 }
11625 else
11626 reg_last_death[regno] = place;
11627
11628 /* If this is a death note for a hard reg that is occupying
11629 multiple registers, ensure that we are still using all
11630 parts of the object. If we find a piece of the object
11631 that is unused, we must add a USE for that piece before
11632 PLACE and put the appropriate REG_DEAD note on it.
11633
11634 An alternative would be to put a REG_UNUSED for the pieces
11635 on the insn that set the register, but that can't be done if
11636 it is not in the same block. It is simpler, though less
11637 efficient, to add the USE insns. */
11638
11639 if (place && regno < FIRST_PSEUDO_REGISTER
11640 && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
11641 {
11642 int endregno
11643 = regno + HARD_REGNO_NREGS (regno,
11644 GET_MODE (XEXP (note, 0)));
11645 int all_used = 1;
11646 int i;
11647
11648 for (i = regno; i < endregno; i++)
11649 if (! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
11650 && ! find_regno_fusage (place, USE, i))
11651 {
11652 rtx piece = gen_rtx_REG (reg_raw_mode[i], i);
11653 rtx p;
11654
11655 /* See if we already placed a USE note for this
11656 register in front of PLACE. */
11657 for (p = place;
11658 GET_CODE (PREV_INSN (p)) == INSN
11659 && GET_CODE (PATTERN (PREV_INSN (p))) == USE;
11660 p = PREV_INSN (p))
11661 if (rtx_equal_p (piece,
11662 XEXP (PATTERN (PREV_INSN (p)), 0)))
11663 {
11664 p = 0;
11665 break;
11666 }
11667
11668 if (p)
11669 {
11670 rtx use_insn
11671 = emit_insn_before (gen_rtx_USE (VOIDmode,
11672 piece),
11673 p);
11674 REG_NOTES (use_insn)
11675 = gen_rtx_EXPR_LIST (REG_DEAD, piece,
11676 REG_NOTES (use_insn));
11677 }
11678
11679 all_used = 0;
11680 }
11681
11682 /* Check for the case where the register dying partially
11683 overlaps the register set by this insn. */
11684 if (all_used)
11685 for (i = regno; i < endregno; i++)
11686 if (dead_or_set_regno_p (place, i))
11687 {
11688 all_used = 0;
11689 break;
11690 }
11691
11692 if (! all_used)
11693 {
11694 /* Put only REG_DEAD notes for pieces that are
11695 still used and that are not already dead or set. */
11696
11697 for (i = regno; i < endregno; i++)
11698 {
11699 rtx piece = gen_rtx_REG (reg_raw_mode[i], i);
11700
11701 if ((reg_referenced_p (piece, PATTERN (place))
11702 || (GET_CODE (place) == CALL_INSN
11703 && find_reg_fusage (place, USE, piece)))
11704 && ! dead_or_set_p (place, piece)
11705 && ! reg_bitfield_target_p (piece,
11706 PATTERN (place)))
11707 REG_NOTES (place)
11708 = gen_rtx_EXPR_LIST (REG_DEAD,
11709 piece, REG_NOTES (place));
11710 }
11711
11712 place = 0;
11713 }
11714 }
11715 }
11716 break;
11717
11718 default:
11719 /* Any other notes should not be present at this point in the
11720 compilation. */
11721 abort ();
11722 }
11723
11724 if (place)
11725 {
11726 XEXP (note, 1) = REG_NOTES (place);
11727 REG_NOTES (place) = note;
11728 }
11729 else if ((REG_NOTE_KIND (note) == REG_DEAD
11730 || REG_NOTE_KIND (note) == REG_UNUSED)
11731 && GET_CODE (XEXP (note, 0)) == REG)
11732 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
11733
11734 if (place2)
11735 {
11736 if ((REG_NOTE_KIND (note) == REG_DEAD
11737 || REG_NOTE_KIND (note) == REG_UNUSED)
11738 && GET_CODE (XEXP (note, 0)) == REG)
11739 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
11740
11741 REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
11742 REG_NOTE_KIND (note),
11743 XEXP (note, 0),
11744 REG_NOTES (place2));
11745 }
11746 }
11747 }
11748 \f
11749 /* Similarly to above, distribute the LOG_LINKS that used to be present on
11750 I3, I2, and I1 to new locations. This is also called in one case to
11751 add a link pointing at I3 when I3's destination is changed. */
11752
11753 static void
11754 distribute_links (links)
11755 rtx links;
11756 {
11757 rtx link, next_link;
11758
11759 for (link = links; link; link = next_link)
11760 {
11761 rtx place = 0;
11762 rtx insn;
11763 rtx set, reg;
11764
11765 next_link = XEXP (link, 1);
11766
11767 /* If the insn that this link points to is a NOTE or isn't a single
11768 set, ignore it. In the latter case, it isn't clear what we
11769 can do other than ignore the link, since we can't tell which
11770 register it was for. Such links wouldn't be used by combine
11771 anyway.
11772
11773 It is not possible for the destination of the target of the link to
11774 have been changed by combine. The only potential of this is if we
11775 replace I3, I2, and I1 by I3 and I2. But in that case the
11776 destination of I2 also remains unchanged. */
11777
11778 if (GET_CODE (XEXP (link, 0)) == NOTE
11779 || (set = single_set (XEXP (link, 0))) == 0)
11780 continue;
11781
11782 reg = SET_DEST (set);
11783 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
11784 || GET_CODE (reg) == SIGN_EXTRACT
11785 || GET_CODE (reg) == STRICT_LOW_PART)
11786 reg = XEXP (reg, 0);
11787
11788 /* A LOG_LINK is defined as being placed on the first insn that uses
11789 a register and points to the insn that sets the register. Start
11790 searching at the next insn after the target of the link and stop
11791 when we reach a set of the register or the end of the basic block.
11792
11793 Note that this correctly handles the link that used to point from
11794 I3 to I2. Also note that not much searching is typically done here
11795 since most links don't point very far away. */
11796
11797 for (insn = NEXT_INSN (XEXP (link, 0));
11798 (insn && (this_basic_block == n_basic_blocks - 1
11799 || basic_block_head[this_basic_block + 1] != insn));
11800 insn = NEXT_INSN (insn))
11801 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
11802 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
11803 {
11804 if (reg_referenced_p (reg, PATTERN (insn)))
11805 place = insn;
11806 break;
11807 }
11808 else if (GET_CODE (insn) == CALL_INSN
11809 && find_reg_fusage (insn, USE, reg))
11810 {
11811 place = insn;
11812 break;
11813 }
11814
11815 /* If we found a place to put the link, place it there unless there
11816 is already a link to the same insn as LINK at that point. */
11817
11818 if (place)
11819 {
11820 rtx link2;
11821
11822 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
11823 if (XEXP (link2, 0) == XEXP (link, 0))
11824 break;
11825
11826 if (link2 == 0)
11827 {
11828 XEXP (link, 1) = LOG_LINKS (place);
11829 LOG_LINKS (place) = link;
11830
11831 /* Set added_links_insn to the earliest insn we added a
11832 link to. */
11833 if (added_links_insn == 0
11834 || INSN_CUID (added_links_insn) > INSN_CUID (place))
11835 added_links_insn = place;
11836 }
11837 }
11838 }
11839 }
11840 \f
11841 /* Compute INSN_CUID for INSN, which is an insn made by combine. */
11842
11843 static int
11844 insn_cuid (insn)
11845 rtx insn;
11846 {
11847 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
11848 && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
11849 insn = NEXT_INSN (insn);
11850
11851 if (INSN_UID (insn) > max_uid_cuid)
11852 abort ();
11853
11854 return INSN_CUID (insn);
11855 }
11856 \f
11857 void
11858 dump_combine_stats (file)
11859 FILE *file;
11860 {
11861 fprintf
11862 (file,
11863 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
11864 combine_attempts, combine_merges, combine_extras, combine_successes);
11865 }
11866
11867 void
11868 dump_combine_total_stats (file)
11869 FILE *file;
11870 {
11871 fprintf
11872 (file,
11873 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
11874 total_attempts, total_merges, total_extras, total_successes);
11875 }