fix compilation on AUTO_INC_DEC targets
[gcc.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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 isn't
53 completely updated (however this is only a local issue since it is
54 regenerated before the next pass that uses it):
55
56 - reg_live_length is not updated
57 - reg_n_refs is not adjusted in the rare case when a register is
58 no longer required in a computation
59 - there are extremely rare cases (see distribute_notes) when a
60 REG_DEAD note is lost
61 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
62 removed because there is no way to know which register it was
63 linking
64
65 To simplify substitution, we combine only when the earlier insn(s)
66 consist of only a single assignment. To simplify updating afterward,
67 we never combine when a subroutine call appears in the middle.
68
69 Since we do not represent assignments to CC0 explicitly except when that
70 is all an insn does, there is no LOG_LINKS entry in an insn that uses
71 the condition code for the insn that set the condition code.
72 Fortunately, these two insns must be consecutive.
73 Therefore, every JUMP_INSN is taken to have an implicit logical link
74 to the preceding insn. This is not quite right, since non-jumps can
75 also use the condition code; but in practice such insns would not
76 combine anyway. */
77
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "tm.h"
82 #include "rtl.h"
83 #include "tree.h"
84 #include "tm_p.h"
85 #include "flags.h"
86 #include "regs.h"
87 #include "hard-reg-set.h"
88 #include "basic-block.h"
89 #include "insn-config.h"
90 #include "function.h"
91 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
92 #include "expr.h"
93 #include "insn-attr.h"
94 #include "recog.h"
95 #include "diagnostic-core.h"
96 #include "target.h"
97 #include "optabs.h"
98 #include "insn-codes.h"
99 #include "rtlhooks-def.h"
100 /* Include output.h for dump_file. */
101 #include "output.h"
102 #include "params.h"
103 #include "timevar.h"
104 #include "tree-pass.h"
105 #include "df.h"
106 #include "cgraph.h"
107 #include "obstack.h"
108
109 /* Number of attempts to combine instructions in this function. */
110
111 static int combine_attempts;
112
113 /* Number of attempts that got as far as substitution in this function. */
114
115 static int combine_merges;
116
117 /* Number of instructions combined with added SETs in this function. */
118
119 static int combine_extras;
120
121 /* Number of instructions combined in this function. */
122
123 static int combine_successes;
124
125 /* Totals over entire compilation. */
126
127 static int total_attempts, total_merges, total_extras, total_successes;
128
129 /* combine_instructions may try to replace the right hand side of the
130 second instruction with the value of an associated REG_EQUAL note
131 before throwing it at try_combine. That is problematic when there
132 is a REG_DEAD note for a register used in the old right hand side
133 and can cause distribute_notes to do wrong things. This is the
134 second instruction if it has been so modified, null otherwise. */
135
136 static rtx i2mod;
137
138 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
139
140 static rtx i2mod_old_rhs;
141
142 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
143
144 static rtx i2mod_new_rhs;
145 \f
146 typedef struct reg_stat_struct {
147 /* Record last point of death of (hard or pseudo) register n. */
148 rtx last_death;
149
150 /* Record last point of modification of (hard or pseudo) register n. */
151 rtx last_set;
152
153 /* The next group of fields allows the recording of the last value assigned
154 to (hard or pseudo) register n. We use this information to see if an
155 operation being processed is redundant given a prior operation performed
156 on the register. For example, an `and' with a constant is redundant if
157 all the zero bits are already known to be turned off.
158
159 We use an approach similar to that used by cse, but change it in the
160 following ways:
161
162 (1) We do not want to reinitialize at each label.
163 (2) It is useful, but not critical, to know the actual value assigned
164 to a register. Often just its form is helpful.
165
166 Therefore, we maintain the following fields:
167
168 last_set_value the last value assigned
169 last_set_label records the value of label_tick when the
170 register was assigned
171 last_set_table_tick records the value of label_tick when a
172 value using the register is assigned
173 last_set_invalid set to nonzero when it is not valid
174 to use the value of this register in some
175 register's value
176
177 To understand the usage of these tables, it is important to understand
178 the distinction between the value in last_set_value being valid and
179 the register being validly contained in some other expression in the
180 table.
181
182 (The next two parameters are out of date).
183
184 reg_stat[i].last_set_value is valid if it is nonzero, and either
185 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
186
187 Register I may validly appear in any expression returned for the value
188 of another register if reg_n_sets[i] is 1. It may also appear in the
189 value for register J if reg_stat[j].last_set_invalid is zero, or
190 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
191
192 If an expression is found in the table containing a register which may
193 not validly appear in an expression, the register is replaced by
194 something that won't match, (clobber (const_int 0)). */
195
196 /* Record last value assigned to (hard or pseudo) register n. */
197
198 rtx last_set_value;
199
200 /* Record the value of label_tick when an expression involving register n
201 is placed in last_set_value. */
202
203 int last_set_table_tick;
204
205 /* Record the value of label_tick when the value for register n is placed in
206 last_set_value. */
207
208 int last_set_label;
209
210 /* These fields are maintained in parallel with last_set_value and are
211 used to store the mode in which the register was last set, the bits
212 that were known to be zero when it was last set, and the number of
213 sign bits copies it was known to have when it was last set. */
214
215 unsigned HOST_WIDE_INT last_set_nonzero_bits;
216 char last_set_sign_bit_copies;
217 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
218
219 /* Set nonzero if references to register n in expressions should not be
220 used. last_set_invalid is set nonzero when this register is being
221 assigned to and last_set_table_tick == label_tick. */
222
223 char last_set_invalid;
224
225 /* Some registers that are set more than once and used in more than one
226 basic block are nevertheless always set in similar ways. For example,
227 a QImode register may be loaded from memory in two places on a machine
228 where byte loads zero extend.
229
230 We record in the following fields if a register has some leading bits
231 that are always equal to the sign bit, and what we know about the
232 nonzero bits of a register, specifically which bits are known to be
233 zero.
234
235 If an entry is zero, it means that we don't know anything special. */
236
237 unsigned char sign_bit_copies;
238
239 unsigned HOST_WIDE_INT nonzero_bits;
240
241 /* Record the value of the label_tick when the last truncation
242 happened. The field truncated_to_mode is only valid if
243 truncation_label == label_tick. */
244
245 int truncation_label;
246
247 /* Record the last truncation seen for this register. If truncation
248 is not a nop to this mode we might be able to save an explicit
249 truncation if we know that value already contains a truncated
250 value. */
251
252 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
253 } reg_stat_type;
254
255 DEF_VEC_O(reg_stat_type);
256 DEF_VEC_ALLOC_O(reg_stat_type,heap);
257
258 static VEC(reg_stat_type,heap) *reg_stat;
259
260 /* Record the luid of the last insn that invalidated memory
261 (anything that writes memory, and subroutine calls, but not pushes). */
262
263 static int mem_last_set;
264
265 /* Record the luid of the last CALL_INSN
266 so we can tell whether a potential combination crosses any calls. */
267
268 static int last_call_luid;
269
270 /* When `subst' is called, this is the insn that is being modified
271 (by combining in a previous insn). The PATTERN of this insn
272 is still the old pattern partially modified and it should not be
273 looked at, but this may be used to examine the successors of the insn
274 to judge whether a simplification is valid. */
275
276 static rtx subst_insn;
277
278 /* This is the lowest LUID that `subst' is currently dealing with.
279 get_last_value will not return a value if the register was set at or
280 after this LUID. If not for this mechanism, we could get confused if
281 I2 or I1 in try_combine were an insn that used the old value of a register
282 to obtain a new value. In that case, we might erroneously get the
283 new value of the register when we wanted the old one. */
284
285 static int subst_low_luid;
286
287 /* This contains any hard registers that are used in newpat; reg_dead_at_p
288 must consider all these registers to be always live. */
289
290 static HARD_REG_SET newpat_used_regs;
291
292 /* This is an insn to which a LOG_LINKS entry has been added. If this
293 insn is the earlier than I2 or I3, combine should rescan starting at
294 that location. */
295
296 static rtx added_links_insn;
297
298 /* Basic block in which we are performing combines. */
299 static basic_block this_basic_block;
300 static bool optimize_this_for_speed_p;
301
302 \f
303 /* Length of the currently allocated uid_insn_cost array. */
304
305 static int max_uid_known;
306
307 /* The following array records the insn_rtx_cost for every insn
308 in the instruction stream. */
309
310 static int *uid_insn_cost;
311
312 /* The following array records the LOG_LINKS for every insn in the
313 instruction stream as struct insn_link pointers. */
314
315 struct insn_link {
316 rtx insn;
317 struct insn_link *next;
318 };
319
320 static struct insn_link **uid_log_links;
321
322 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
323 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
324
325 #define FOR_EACH_LOG_LINK(L, INSN) \
326 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
327
328 /* Links for LOG_LINKS are allocated from this obstack. */
329
330 static struct obstack insn_link_obstack;
331
332 /* Allocate a link. */
333
334 static inline struct insn_link *
335 alloc_insn_link (rtx insn, struct insn_link *next)
336 {
337 struct insn_link *l
338 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
339 sizeof (struct insn_link));
340 l->insn = insn;
341 l->next = next;
342 return l;
343 }
344
345 /* Incremented for each basic block. */
346
347 static int label_tick;
348
349 /* Reset to label_tick for each extended basic block in scanning order. */
350
351 static int label_tick_ebb_start;
352
353 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
354 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
355
356 static enum machine_mode nonzero_bits_mode;
357
358 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
359 be safely used. It is zero while computing them and after combine has
360 completed. This former test prevents propagating values based on
361 previously set values, which can be incorrect if a variable is modified
362 in a loop. */
363
364 static int nonzero_sign_valid;
365
366 \f
367 /* Record one modification to rtl structure
368 to be undone by storing old_contents into *where. */
369
370 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE };
371
372 struct undo
373 {
374 struct undo *next;
375 enum undo_kind kind;
376 union { rtx r; int i; enum machine_mode m; } old_contents;
377 union { rtx *r; int *i; } where;
378 };
379
380 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
381 num_undo says how many are currently recorded.
382
383 other_insn is nonzero if we have modified some other insn in the process
384 of working on subst_insn. It must be verified too. */
385
386 struct undobuf
387 {
388 struct undo *undos;
389 struct undo *frees;
390 rtx other_insn;
391 };
392
393 static struct undobuf undobuf;
394
395 /* Number of times the pseudo being substituted for
396 was found and replaced. */
397
398 static int n_occurrences;
399
400 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
401 enum machine_mode,
402 unsigned HOST_WIDE_INT,
403 unsigned HOST_WIDE_INT *);
404 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
405 enum machine_mode,
406 unsigned int, unsigned int *);
407 static void do_SUBST (rtx *, rtx);
408 static void do_SUBST_INT (int *, int);
409 static void init_reg_last (void);
410 static void setup_incoming_promotions (rtx);
411 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
412 static int cant_combine_insn_p (rtx);
413 static int can_combine_p (rtx, rtx, rtx, rtx, rtx, rtx, rtx *, rtx *);
414 static int combinable_i3pat (rtx, rtx *, rtx, rtx, rtx, int, int, rtx *);
415 static int contains_muldiv (rtx);
416 static rtx try_combine (rtx, rtx, rtx, rtx, int *);
417 static void undo_all (void);
418 static void undo_commit (void);
419 static rtx *find_split_point (rtx *, rtx, bool);
420 static rtx subst (rtx, rtx, rtx, int, int);
421 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
422 static rtx simplify_if_then_else (rtx);
423 static rtx simplify_set (rtx);
424 static rtx simplify_logical (rtx);
425 static rtx expand_compound_operation (rtx);
426 static const_rtx expand_field_assignment (const_rtx);
427 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
428 rtx, unsigned HOST_WIDE_INT, int, int, int);
429 static rtx extract_left_shift (rtx, int);
430 static rtx make_compound_operation (rtx, enum rtx_code);
431 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
432 unsigned HOST_WIDE_INT *);
433 static rtx canon_reg_for_combine (rtx, rtx);
434 static rtx force_to_mode (rtx, enum machine_mode,
435 unsigned HOST_WIDE_INT, int);
436 static rtx if_then_else_cond (rtx, rtx *, rtx *);
437 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
438 static int rtx_equal_for_field_assignment_p (rtx, rtx);
439 static rtx make_field_assignment (rtx);
440 static rtx apply_distributive_law (rtx);
441 static rtx distribute_and_simplify_rtx (rtx, int);
442 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
443 unsigned HOST_WIDE_INT);
444 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
445 unsigned HOST_WIDE_INT);
446 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
447 HOST_WIDE_INT, enum machine_mode, int *);
448 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
449 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
450 int);
451 static int recog_for_combine (rtx *, rtx, rtx *);
452 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
453 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
454 static void update_table_tick (rtx);
455 static void record_value_for_reg (rtx, rtx, rtx);
456 static void check_promoted_subreg (rtx, rtx);
457 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
458 static void record_dead_and_set_regs (rtx);
459 static int get_last_value_validate (rtx *, rtx, int, int);
460 static rtx get_last_value (const_rtx);
461 static int use_crosses_set_p (const_rtx, int);
462 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
463 static int reg_dead_at_p (rtx, rtx);
464 static void move_deaths (rtx, rtx, int, rtx, rtx *);
465 static int reg_bitfield_target_p (rtx, rtx);
466 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx, rtx);
467 static void distribute_links (struct insn_link *);
468 static void mark_used_regs_combine (rtx);
469 static void record_promoted_value (rtx, rtx);
470 static int unmentioned_reg_p_1 (rtx *, void *);
471 static bool unmentioned_reg_p (rtx, rtx);
472 static int record_truncated_value (rtx *, void *);
473 static void record_truncated_values (rtx *, void *);
474 static bool reg_truncated_to_mode (enum machine_mode, const_rtx);
475 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
476 \f
477
478 /* It is not safe to use ordinary gen_lowpart in combine.
479 See comments in gen_lowpart_for_combine. */
480 #undef RTL_HOOKS_GEN_LOWPART
481 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
482
483 /* Our implementation of gen_lowpart never emits a new pseudo. */
484 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
485 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
486
487 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
488 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
489
490 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
491 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
492
493 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
494 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
495
496 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
497
498 \f
499 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
500 PATTERN can not be split. Otherwise, it returns an insn sequence.
501 This is a wrapper around split_insns which ensures that the
502 reg_stat vector is made larger if the splitter creates a new
503 register. */
504
505 static rtx
506 combine_split_insns (rtx pattern, rtx insn)
507 {
508 rtx ret;
509 unsigned int nregs;
510
511 ret = split_insns (pattern, insn);
512 nregs = max_reg_num ();
513 if (nregs > VEC_length (reg_stat_type, reg_stat))
514 VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
515 return ret;
516 }
517
518 /* This is used by find_single_use to locate an rtx in LOC that
519 contains exactly one use of DEST, which is typically either a REG
520 or CC0. It returns a pointer to the innermost rtx expression
521 containing DEST. Appearances of DEST that are being used to
522 totally replace it are not counted. */
523
524 static rtx *
525 find_single_use_1 (rtx dest, rtx *loc)
526 {
527 rtx x = *loc;
528 enum rtx_code code = GET_CODE (x);
529 rtx *result = NULL;
530 rtx *this_result;
531 int i;
532 const char *fmt;
533
534 switch (code)
535 {
536 case CONST_INT:
537 case CONST:
538 case LABEL_REF:
539 case SYMBOL_REF:
540 case CONST_DOUBLE:
541 case CONST_VECTOR:
542 case CLOBBER:
543 return 0;
544
545 case SET:
546 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
547 of a REG that occupies all of the REG, the insn uses DEST if
548 it is mentioned in the destination or the source. Otherwise, we
549 need just check the source. */
550 if (GET_CODE (SET_DEST (x)) != CC0
551 && GET_CODE (SET_DEST (x)) != PC
552 && !REG_P (SET_DEST (x))
553 && ! (GET_CODE (SET_DEST (x)) == SUBREG
554 && REG_P (SUBREG_REG (SET_DEST (x)))
555 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
556 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
557 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
558 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
559 break;
560
561 return find_single_use_1 (dest, &SET_SRC (x));
562
563 case MEM:
564 case SUBREG:
565 return find_single_use_1 (dest, &XEXP (x, 0));
566
567 default:
568 break;
569 }
570
571 /* If it wasn't one of the common cases above, check each expression and
572 vector of this code. Look for a unique usage of DEST. */
573
574 fmt = GET_RTX_FORMAT (code);
575 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
576 {
577 if (fmt[i] == 'e')
578 {
579 if (dest == XEXP (x, i)
580 || (REG_P (dest) && REG_P (XEXP (x, i))
581 && REGNO (dest) == REGNO (XEXP (x, i))))
582 this_result = loc;
583 else
584 this_result = find_single_use_1 (dest, &XEXP (x, i));
585
586 if (result == NULL)
587 result = this_result;
588 else if (this_result)
589 /* Duplicate usage. */
590 return NULL;
591 }
592 else if (fmt[i] == 'E')
593 {
594 int j;
595
596 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
597 {
598 if (XVECEXP (x, i, j) == dest
599 || (REG_P (dest)
600 && REG_P (XVECEXP (x, i, j))
601 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
602 this_result = loc;
603 else
604 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
605
606 if (result == NULL)
607 result = this_result;
608 else if (this_result)
609 return NULL;
610 }
611 }
612 }
613
614 return result;
615 }
616
617
618 /* See if DEST, produced in INSN, is used only a single time in the
619 sequel. If so, return a pointer to the innermost rtx expression in which
620 it is used.
621
622 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
623
624 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
625 care about REG_DEAD notes or LOG_LINKS.
626
627 Otherwise, we find the single use by finding an insn that has a
628 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
629 only referenced once in that insn, we know that it must be the first
630 and last insn referencing DEST. */
631
632 static rtx *
633 find_single_use (rtx dest, rtx insn, rtx *ploc)
634 {
635 basic_block bb;
636 rtx next;
637 rtx *result;
638 struct insn_link *link;
639
640 #ifdef HAVE_cc0
641 if (dest == cc0_rtx)
642 {
643 next = NEXT_INSN (insn);
644 if (next == 0
645 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
646 return 0;
647
648 result = find_single_use_1 (dest, &PATTERN (next));
649 if (result && ploc)
650 *ploc = next;
651 return result;
652 }
653 #endif
654
655 if (!REG_P (dest))
656 return 0;
657
658 bb = BLOCK_FOR_INSN (insn);
659 for (next = NEXT_INSN (insn);
660 next && BLOCK_FOR_INSN (next) == bb;
661 next = NEXT_INSN (next))
662 if (INSN_P (next) && dead_or_set_p (next, dest))
663 {
664 FOR_EACH_LOG_LINK (link, next)
665 if (link->insn == insn)
666 break;
667
668 if (link)
669 {
670 result = find_single_use_1 (dest, &PATTERN (next));
671 if (ploc)
672 *ploc = next;
673 return result;
674 }
675 }
676
677 return 0;
678 }
679 \f
680 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
681 insn. The substitution can be undone by undo_all. If INTO is already
682 set to NEWVAL, do not record this change. Because computing NEWVAL might
683 also call SUBST, we have to compute it before we put anything into
684 the undo table. */
685
686 static void
687 do_SUBST (rtx *into, rtx newval)
688 {
689 struct undo *buf;
690 rtx oldval = *into;
691
692 if (oldval == newval)
693 return;
694
695 /* We'd like to catch as many invalid transformations here as
696 possible. Unfortunately, there are way too many mode changes
697 that are perfectly valid, so we'd waste too much effort for
698 little gain doing the checks here. Focus on catching invalid
699 transformations involving integer constants. */
700 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
701 && CONST_INT_P (newval))
702 {
703 /* Sanity check that we're replacing oldval with a CONST_INT
704 that is a valid sign-extension for the original mode. */
705 gcc_assert (INTVAL (newval)
706 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
707
708 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
709 CONST_INT is not valid, because after the replacement, the
710 original mode would be gone. Unfortunately, we can't tell
711 when do_SUBST is called to replace the operand thereof, so we
712 perform this test on oldval instead, checking whether an
713 invalid replacement took place before we got here. */
714 gcc_assert (!(GET_CODE (oldval) == SUBREG
715 && CONST_INT_P (SUBREG_REG (oldval))));
716 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
717 && CONST_INT_P (XEXP (oldval, 0))));
718 }
719
720 if (undobuf.frees)
721 buf = undobuf.frees, undobuf.frees = buf->next;
722 else
723 buf = XNEW (struct undo);
724
725 buf->kind = UNDO_RTX;
726 buf->where.r = into;
727 buf->old_contents.r = oldval;
728 *into = newval;
729
730 buf->next = undobuf.undos, undobuf.undos = buf;
731 }
732
733 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
734
735 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
736 for the value of a HOST_WIDE_INT value (including CONST_INT) is
737 not safe. */
738
739 static void
740 do_SUBST_INT (int *into, int newval)
741 {
742 struct undo *buf;
743 int oldval = *into;
744
745 if (oldval == newval)
746 return;
747
748 if (undobuf.frees)
749 buf = undobuf.frees, undobuf.frees = buf->next;
750 else
751 buf = XNEW (struct undo);
752
753 buf->kind = UNDO_INT;
754 buf->where.i = into;
755 buf->old_contents.i = oldval;
756 *into = newval;
757
758 buf->next = undobuf.undos, undobuf.undos = buf;
759 }
760
761 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
762
763 /* Similar to SUBST, but just substitute the mode. This is used when
764 changing the mode of a pseudo-register, so that any other
765 references to the entry in the regno_reg_rtx array will change as
766 well. */
767
768 static void
769 do_SUBST_MODE (rtx *into, enum machine_mode newval)
770 {
771 struct undo *buf;
772 enum machine_mode oldval = GET_MODE (*into);
773
774 if (oldval == newval)
775 return;
776
777 if (undobuf.frees)
778 buf = undobuf.frees, undobuf.frees = buf->next;
779 else
780 buf = XNEW (struct undo);
781
782 buf->kind = UNDO_MODE;
783 buf->where.r = into;
784 buf->old_contents.m = oldval;
785 adjust_reg_mode (*into, newval);
786
787 buf->next = undobuf.undos, undobuf.undos = buf;
788 }
789
790 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE(&(INTO), (NEWVAL))
791 \f
792 /* Subroutine of try_combine. Determine whether the combine replacement
793 patterns NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to
794 insn_rtx_cost that the original instruction sequence I0, I1, I2, I3 and
795 undobuf.other_insn. Note that I1 and/or NEWI2PAT may be NULL_RTX.
796 NEWOTHERPAT and undobuf.other_insn may also both be NULL_RTX. This
797 function returns false, if the costs of all instructions can be
798 estimated, and the replacements are more expensive than the original
799 sequence. */
800
801 static bool
802 combine_validate_cost (rtx i0, rtx i1, rtx i2, rtx i3, rtx newpat,
803 rtx newi2pat, rtx newotherpat)
804 {
805 int i0_cost, i1_cost, i2_cost, i3_cost;
806 int new_i2_cost, new_i3_cost;
807 int old_cost, new_cost;
808
809 /* Lookup the original insn_rtx_costs. */
810 i2_cost = INSN_COST (i2);
811 i3_cost = INSN_COST (i3);
812
813 if (i1)
814 {
815 i1_cost = INSN_COST (i1);
816 if (i0)
817 {
818 i0_cost = INSN_COST (i0);
819 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
820 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
821 }
822 else
823 {
824 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
825 ? i1_cost + i2_cost + i3_cost : 0);
826 i0_cost = 0;
827 }
828 }
829 else
830 {
831 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
832 i1_cost = i0_cost = 0;
833 }
834
835 /* Calculate the replacement insn_rtx_costs. */
836 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
837 if (newi2pat)
838 {
839 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
840 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
841 ? new_i2_cost + new_i3_cost : 0;
842 }
843 else
844 {
845 new_cost = new_i3_cost;
846 new_i2_cost = 0;
847 }
848
849 if (undobuf.other_insn)
850 {
851 int old_other_cost, new_other_cost;
852
853 old_other_cost = INSN_COST (undobuf.other_insn);
854 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
855 if (old_other_cost > 0 && new_other_cost > 0)
856 {
857 old_cost += old_other_cost;
858 new_cost += new_other_cost;
859 }
860 else
861 old_cost = 0;
862 }
863
864 /* Disallow this recombination if both new_cost and old_cost are
865 greater than zero, and new_cost is greater than old cost. */
866 if (old_cost > 0
867 && new_cost > old_cost)
868 {
869 if (dump_file)
870 {
871 if (i0)
872 {
873 fprintf (dump_file,
874 "rejecting combination of insns %d, %d, %d and %d\n",
875 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2),
876 INSN_UID (i3));
877 fprintf (dump_file, "original costs %d + %d + %d + %d = %d\n",
878 i0_cost, i1_cost, i2_cost, i3_cost, old_cost);
879 }
880 else if (i1)
881 {
882 fprintf (dump_file,
883 "rejecting combination of insns %d, %d and %d\n",
884 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
885 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
886 i1_cost, i2_cost, i3_cost, old_cost);
887 }
888 else
889 {
890 fprintf (dump_file,
891 "rejecting combination of insns %d and %d\n",
892 INSN_UID (i2), INSN_UID (i3));
893 fprintf (dump_file, "original costs %d + %d = %d\n",
894 i2_cost, i3_cost, old_cost);
895 }
896
897 if (newi2pat)
898 {
899 fprintf (dump_file, "replacement costs %d + %d = %d\n",
900 new_i2_cost, new_i3_cost, new_cost);
901 }
902 else
903 fprintf (dump_file, "replacement cost %d\n", new_cost);
904 }
905
906 return false;
907 }
908
909 /* Update the uid_insn_cost array with the replacement costs. */
910 INSN_COST (i2) = new_i2_cost;
911 INSN_COST (i3) = new_i3_cost;
912 if (i1)
913 INSN_COST (i1) = 0;
914
915 return true;
916 }
917
918
919 /* Delete any insns that copy a register to itself. */
920
921 static void
922 delete_noop_moves (void)
923 {
924 rtx insn, next;
925 basic_block bb;
926
927 FOR_EACH_BB (bb)
928 {
929 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
930 {
931 next = NEXT_INSN (insn);
932 if (INSN_P (insn) && noop_move_p (insn))
933 {
934 if (dump_file)
935 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
936
937 delete_insn_and_edges (insn);
938 }
939 }
940 }
941 }
942
943 \f
944 /* Fill in log links field for all insns. */
945
946 static void
947 create_log_links (void)
948 {
949 basic_block bb;
950 rtx *next_use, insn;
951 df_ref *def_vec, *use_vec;
952
953 next_use = XCNEWVEC (rtx, max_reg_num ());
954
955 /* Pass through each block from the end, recording the uses of each
956 register and establishing log links when def is encountered.
957 Note that we do not clear next_use array in order to save time,
958 so we have to test whether the use is in the same basic block as def.
959
960 There are a few cases below when we do not consider the definition or
961 usage -- these are taken from original flow.c did. Don't ask me why it is
962 done this way; I don't know and if it works, I don't want to know. */
963
964 FOR_EACH_BB (bb)
965 {
966 FOR_BB_INSNS_REVERSE (bb, insn)
967 {
968 if (!NONDEBUG_INSN_P (insn))
969 continue;
970
971 /* Log links are created only once. */
972 gcc_assert (!LOG_LINKS (insn));
973
974 for (def_vec = DF_INSN_DEFS (insn); *def_vec; def_vec++)
975 {
976 df_ref def = *def_vec;
977 int regno = DF_REF_REGNO (def);
978 rtx use_insn;
979
980 if (!next_use[regno])
981 continue;
982
983 /* Do not consider if it is pre/post modification in MEM. */
984 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
985 continue;
986
987 /* Do not make the log link for frame pointer. */
988 if ((regno == FRAME_POINTER_REGNUM
989 && (! reload_completed || frame_pointer_needed))
990 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
991 || (regno == HARD_FRAME_POINTER_REGNUM
992 && (! reload_completed || frame_pointer_needed))
993 #endif
994 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
995 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
996 #endif
997 )
998 continue;
999
1000 use_insn = next_use[regno];
1001 if (BLOCK_FOR_INSN (use_insn) == bb)
1002 {
1003 /* flow.c claimed:
1004
1005 We don't build a LOG_LINK for hard registers contained
1006 in ASM_OPERANDs. If these registers get replaced,
1007 we might wind up changing the semantics of the insn,
1008 even if reload can make what appear to be valid
1009 assignments later. */
1010 if (regno >= FIRST_PSEUDO_REGISTER
1011 || asm_noperands (PATTERN (use_insn)) < 0)
1012 {
1013 /* Don't add duplicate links between instructions. */
1014 struct insn_link *links;
1015 FOR_EACH_LOG_LINK (links, use_insn)
1016 if (insn == links->insn)
1017 break;
1018
1019 if (!links)
1020 LOG_LINKS (use_insn)
1021 = alloc_insn_link (insn, LOG_LINKS (use_insn));
1022 }
1023 }
1024 next_use[regno] = NULL_RTX;
1025 }
1026
1027 for (use_vec = DF_INSN_USES (insn); *use_vec; use_vec++)
1028 {
1029 df_ref use = *use_vec;
1030 int regno = DF_REF_REGNO (use);
1031
1032 /* Do not consider the usage of the stack pointer
1033 by function call. */
1034 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1035 continue;
1036
1037 next_use[regno] = insn;
1038 }
1039 }
1040 }
1041
1042 free (next_use);
1043 }
1044
1045 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1046 true if we found a LOG_LINK that proves that A feeds B. This only works
1047 if there are no instructions between A and B which could have a link
1048 depending on A, since in that case we would not record a link for B.
1049 We also check the implicit dependency created by a cc0 setter/user
1050 pair. */
1051
1052 static bool
1053 insn_a_feeds_b (rtx a, rtx b)
1054 {
1055 struct insn_link *links;
1056 FOR_EACH_LOG_LINK (links, b)
1057 if (links->insn == a)
1058 return true;
1059 #ifdef HAVE_cc0
1060 if (sets_cc0_p (a))
1061 return true;
1062 #endif
1063 return false;
1064 }
1065 \f
1066 /* Main entry point for combiner. F is the first insn of the function.
1067 NREGS is the first unused pseudo-reg number.
1068
1069 Return nonzero if the combiner has turned an indirect jump
1070 instruction into a direct jump. */
1071 static int
1072 combine_instructions (rtx f, unsigned int nregs)
1073 {
1074 rtx insn, next;
1075 #ifdef HAVE_cc0
1076 rtx prev;
1077 #endif
1078 struct insn_link *links, *nextlinks;
1079 rtx first;
1080 basic_block last_bb;
1081
1082 int new_direct_jump_p = 0;
1083
1084 for (first = f; first && !INSN_P (first); )
1085 first = NEXT_INSN (first);
1086 if (!first)
1087 return 0;
1088
1089 combine_attempts = 0;
1090 combine_merges = 0;
1091 combine_extras = 0;
1092 combine_successes = 0;
1093
1094 rtl_hooks = combine_rtl_hooks;
1095
1096 VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
1097
1098 init_recog_no_volatile ();
1099
1100 /* Allocate array for insn info. */
1101 max_uid_known = get_max_uid ();
1102 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1103 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1104 gcc_obstack_init (&insn_link_obstack);
1105
1106 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1107
1108 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1109 problems when, for example, we have j <<= 1 in a loop. */
1110
1111 nonzero_sign_valid = 0;
1112 label_tick = label_tick_ebb_start = 1;
1113
1114 /* Scan all SETs and see if we can deduce anything about what
1115 bits are known to be zero for some registers and how many copies
1116 of the sign bit are known to exist for those registers.
1117
1118 Also set any known values so that we can use it while searching
1119 for what bits are known to be set. */
1120
1121 setup_incoming_promotions (first);
1122 /* Allow the entry block and the first block to fall into the same EBB.
1123 Conceptually the incoming promotions are assigned to the entry block. */
1124 last_bb = ENTRY_BLOCK_PTR;
1125
1126 create_log_links ();
1127 FOR_EACH_BB (this_basic_block)
1128 {
1129 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1130 last_call_luid = 0;
1131 mem_last_set = -1;
1132
1133 label_tick++;
1134 if (!single_pred_p (this_basic_block)
1135 || single_pred (this_basic_block) != last_bb)
1136 label_tick_ebb_start = label_tick;
1137 last_bb = this_basic_block;
1138
1139 FOR_BB_INSNS (this_basic_block, insn)
1140 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1141 {
1142 rtx links;
1143
1144 subst_low_luid = DF_INSN_LUID (insn);
1145 subst_insn = insn;
1146
1147 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1148 insn);
1149 record_dead_and_set_regs (insn);
1150
1151 #ifdef AUTO_INC_DEC
1152 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1153 if (REG_NOTE_KIND (links) == REG_INC)
1154 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1155 insn);
1156 #endif
1157
1158 /* Record the current insn_rtx_cost of this instruction. */
1159 if (NONJUMP_INSN_P (insn))
1160 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1161 optimize_this_for_speed_p);
1162 if (dump_file)
1163 fprintf(dump_file, "insn_cost %d: %d\n",
1164 INSN_UID (insn), INSN_COST (insn));
1165 }
1166 }
1167
1168 nonzero_sign_valid = 1;
1169
1170 /* Now scan all the insns in forward order. */
1171 label_tick = label_tick_ebb_start = 1;
1172 init_reg_last ();
1173 setup_incoming_promotions (first);
1174 last_bb = ENTRY_BLOCK_PTR;
1175
1176 FOR_EACH_BB (this_basic_block)
1177 {
1178 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1179 last_call_luid = 0;
1180 mem_last_set = -1;
1181
1182 label_tick++;
1183 if (!single_pred_p (this_basic_block)
1184 || single_pred (this_basic_block) != last_bb)
1185 label_tick_ebb_start = label_tick;
1186 last_bb = this_basic_block;
1187
1188 rtl_profile_for_bb (this_basic_block);
1189 for (insn = BB_HEAD (this_basic_block);
1190 insn != NEXT_INSN (BB_END (this_basic_block));
1191 insn = next ? next : NEXT_INSN (insn))
1192 {
1193 next = 0;
1194 if (NONDEBUG_INSN_P (insn))
1195 {
1196 /* See if we know about function return values before this
1197 insn based upon SUBREG flags. */
1198 check_promoted_subreg (insn, PATTERN (insn));
1199
1200 /* See if we can find hardregs and subreg of pseudos in
1201 narrower modes. This could help turning TRUNCATEs
1202 into SUBREGs. */
1203 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1204
1205 /* Try this insn with each insn it links back to. */
1206
1207 FOR_EACH_LOG_LINK (links, insn)
1208 if ((next = try_combine (insn, links->insn, NULL_RTX,
1209 NULL_RTX, &new_direct_jump_p)) != 0)
1210 goto retry;
1211
1212 /* Try each sequence of three linked insns ending with this one. */
1213
1214 FOR_EACH_LOG_LINK (links, insn)
1215 {
1216 rtx link = links->insn;
1217
1218 /* If the linked insn has been replaced by a note, then there
1219 is no point in pursuing this chain any further. */
1220 if (NOTE_P (link))
1221 continue;
1222
1223 FOR_EACH_LOG_LINK (nextlinks, link)
1224 if ((next = try_combine (insn, link, nextlinks->insn,
1225 NULL_RTX,
1226 &new_direct_jump_p)) != 0)
1227 goto retry;
1228 }
1229
1230 #ifdef HAVE_cc0
1231 /* Try to combine a jump insn that uses CC0
1232 with a preceding insn that sets CC0, and maybe with its
1233 logical predecessor as well.
1234 This is how we make decrement-and-branch insns.
1235 We need this special code because data flow connections
1236 via CC0 do not get entered in LOG_LINKS. */
1237
1238 if (JUMP_P (insn)
1239 && (prev = prev_nonnote_insn (insn)) != 0
1240 && NONJUMP_INSN_P (prev)
1241 && sets_cc0_p (PATTERN (prev)))
1242 {
1243 if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
1244 &new_direct_jump_p)) != 0)
1245 goto retry;
1246
1247 FOR_EACH_LOG_LINK (nextlinks, prev)
1248 if ((next = try_combine (insn, prev, nextlinks->insn,
1249 NULL_RTX,
1250 &new_direct_jump_p)) != 0)
1251 goto retry;
1252 }
1253
1254 /* Do the same for an insn that explicitly references CC0. */
1255 if (NONJUMP_INSN_P (insn)
1256 && (prev = prev_nonnote_insn (insn)) != 0
1257 && NONJUMP_INSN_P (prev)
1258 && sets_cc0_p (PATTERN (prev))
1259 && GET_CODE (PATTERN (insn)) == SET
1260 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1261 {
1262 if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
1263 &new_direct_jump_p)) != 0)
1264 goto retry;
1265
1266 FOR_EACH_LOG_LINK (nextlinks, prev)
1267 if ((next = try_combine (insn, prev, nextlinks->insn,
1268 NULL_RTX,
1269 &new_direct_jump_p)) != 0)
1270 goto retry;
1271 }
1272
1273 /* Finally, see if any of the insns that this insn links to
1274 explicitly references CC0. If so, try this insn, that insn,
1275 and its predecessor if it sets CC0. */
1276 FOR_EACH_LOG_LINK (links, insn)
1277 if (NONJUMP_INSN_P (links->insn)
1278 && GET_CODE (PATTERN (links->insn)) == SET
1279 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1280 && (prev = prev_nonnote_insn (links->insn)) != 0
1281 && NONJUMP_INSN_P (prev)
1282 && sets_cc0_p (PATTERN (prev))
1283 && (next = try_combine (insn, links->insn,
1284 prev, NULL_RTX,
1285 &new_direct_jump_p)) != 0)
1286 goto retry;
1287 #endif
1288
1289 /* Try combining an insn with two different insns whose results it
1290 uses. */
1291 FOR_EACH_LOG_LINK (links, insn)
1292 for (nextlinks = links->next; nextlinks;
1293 nextlinks = nextlinks->next)
1294 if ((next = try_combine (insn, links->insn,
1295 nextlinks->insn, NULL_RTX,
1296 &new_direct_jump_p)) != 0)
1297 goto retry;
1298
1299 /* Try four-instruction combinations. */
1300 FOR_EACH_LOG_LINK (links, insn)
1301 {
1302 struct insn_link *next1;
1303 rtx link = links->insn;
1304
1305 /* If the linked insn has been replaced by a note, then there
1306 is no point in pursuing this chain any further. */
1307 if (NOTE_P (link))
1308 continue;
1309
1310 FOR_EACH_LOG_LINK (next1, link)
1311 {
1312 rtx link1 = next1->insn;
1313 if (NOTE_P (link1))
1314 continue;
1315 /* I0 -> I1 -> I2 -> I3. */
1316 FOR_EACH_LOG_LINK (nextlinks, link1)
1317 if ((next = try_combine (insn, link, link1,
1318 nextlinks->insn,
1319 &new_direct_jump_p)) != 0)
1320 goto retry;
1321 /* I0, I1 -> I2, I2 -> I3. */
1322 for (nextlinks = next1->next; nextlinks;
1323 nextlinks = nextlinks->next)
1324 if ((next = try_combine (insn, link, link1,
1325 nextlinks->insn,
1326 &new_direct_jump_p)) != 0)
1327 goto retry;
1328 }
1329
1330 for (next1 = links->next; next1; next1 = next1->next)
1331 {
1332 rtx link1 = next1->insn;
1333 if (NOTE_P (link1))
1334 continue;
1335 /* I0 -> I2; I1, I2 -> I3. */
1336 FOR_EACH_LOG_LINK (nextlinks, link)
1337 if ((next = try_combine (insn, link, link1,
1338 nextlinks->insn,
1339 &new_direct_jump_p)) != 0)
1340 goto retry;
1341 /* I0 -> I1; I1, I2 -> I3. */
1342 FOR_EACH_LOG_LINK (nextlinks, link1)
1343 if ((next = try_combine (insn, link, link1,
1344 nextlinks->insn,
1345 &new_direct_jump_p)) != 0)
1346 goto retry;
1347 }
1348 }
1349
1350 /* Try this insn with each REG_EQUAL note it links back to. */
1351 FOR_EACH_LOG_LINK (links, insn)
1352 {
1353 rtx set, note;
1354 rtx temp = links->insn;
1355 if ((set = single_set (temp)) != 0
1356 && (note = find_reg_equal_equiv_note (temp)) != 0
1357 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1358 /* Avoid using a register that may already been marked
1359 dead by an earlier instruction. */
1360 && ! unmentioned_reg_p (note, SET_SRC (set))
1361 && (GET_MODE (note) == VOIDmode
1362 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1363 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1364 {
1365 /* Temporarily replace the set's source with the
1366 contents of the REG_EQUAL note. The insn will
1367 be deleted or recognized by try_combine. */
1368 rtx orig = SET_SRC (set);
1369 SET_SRC (set) = note;
1370 i2mod = temp;
1371 i2mod_old_rhs = copy_rtx (orig);
1372 i2mod_new_rhs = copy_rtx (note);
1373 next = try_combine (insn, i2mod, NULL_RTX, NULL_RTX,
1374 &new_direct_jump_p);
1375 i2mod = NULL_RTX;
1376 if (next)
1377 goto retry;
1378 SET_SRC (set) = orig;
1379 }
1380 }
1381
1382 if (!NOTE_P (insn))
1383 record_dead_and_set_regs (insn);
1384
1385 retry:
1386 ;
1387 }
1388 }
1389 }
1390
1391 default_rtl_profile ();
1392 clear_bb_flags ();
1393 new_direct_jump_p |= purge_all_dead_edges ();
1394 delete_noop_moves ();
1395
1396 /* Clean up. */
1397 obstack_free (&insn_link_obstack, NULL);
1398 free (uid_log_links);
1399 free (uid_insn_cost);
1400 VEC_free (reg_stat_type, heap, reg_stat);
1401
1402 {
1403 struct undo *undo, *next;
1404 for (undo = undobuf.frees; undo; undo = next)
1405 {
1406 next = undo->next;
1407 free (undo);
1408 }
1409 undobuf.frees = 0;
1410 }
1411
1412 total_attempts += combine_attempts;
1413 total_merges += combine_merges;
1414 total_extras += combine_extras;
1415 total_successes += combine_successes;
1416
1417 nonzero_sign_valid = 0;
1418 rtl_hooks = general_rtl_hooks;
1419
1420 /* Make recognizer allow volatile MEMs again. */
1421 init_recog ();
1422
1423 return new_direct_jump_p;
1424 }
1425
1426 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1427
1428 static void
1429 init_reg_last (void)
1430 {
1431 unsigned int i;
1432 reg_stat_type *p;
1433
1434 FOR_EACH_VEC_ELT (reg_stat_type, reg_stat, i, p)
1435 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1436 }
1437 \f
1438 /* Set up any promoted values for incoming argument registers. */
1439
1440 static void
1441 setup_incoming_promotions (rtx first)
1442 {
1443 tree arg;
1444 bool strictly_local = false;
1445
1446 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1447 arg = DECL_CHAIN (arg))
1448 {
1449 rtx x, reg = DECL_INCOMING_RTL (arg);
1450 int uns1, uns3;
1451 enum machine_mode mode1, mode2, mode3, mode4;
1452
1453 /* Only continue if the incoming argument is in a register. */
1454 if (!REG_P (reg))
1455 continue;
1456
1457 /* Determine, if possible, whether all call sites of the current
1458 function lie within the current compilation unit. (This does
1459 take into account the exporting of a function via taking its
1460 address, and so forth.) */
1461 strictly_local = cgraph_local_info (current_function_decl)->local;
1462
1463 /* The mode and signedness of the argument before any promotions happen
1464 (equal to the mode of the pseudo holding it at that stage). */
1465 mode1 = TYPE_MODE (TREE_TYPE (arg));
1466 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1467
1468 /* The mode and signedness of the argument after any source language and
1469 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1470 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1471 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1472
1473 /* The mode and signedness of the argument as it is actually passed,
1474 after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions. */
1475 mode3 = promote_function_mode (DECL_ARG_TYPE (arg), mode2, &uns3,
1476 TREE_TYPE (cfun->decl), 0);
1477
1478 /* The mode of the register in which the argument is being passed. */
1479 mode4 = GET_MODE (reg);
1480
1481 /* Eliminate sign extensions in the callee when:
1482 (a) A mode promotion has occurred; */
1483 if (mode1 == mode3)
1484 continue;
1485 /* (b) The mode of the register is the same as the mode of
1486 the argument as it is passed; */
1487 if (mode3 != mode4)
1488 continue;
1489 /* (c) There's no language level extension; */
1490 if (mode1 == mode2)
1491 ;
1492 /* (c.1) All callers are from the current compilation unit. If that's
1493 the case we don't have to rely on an ABI, we only have to know
1494 what we're generating right now, and we know that we will do the
1495 mode1 to mode2 promotion with the given sign. */
1496 else if (!strictly_local)
1497 continue;
1498 /* (c.2) The combination of the two promotions is useful. This is
1499 true when the signs match, or if the first promotion is unsigned.
1500 In the later case, (sign_extend (zero_extend x)) is the same as
1501 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1502 else if (uns1)
1503 uns3 = true;
1504 else if (uns3)
1505 continue;
1506
1507 /* Record that the value was promoted from mode1 to mode3,
1508 so that any sign extension at the head of the current
1509 function may be eliminated. */
1510 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1511 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1512 record_value_for_reg (reg, first, x);
1513 }
1514 }
1515
1516 /* Called via note_stores. If X is a pseudo that is narrower than
1517 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1518
1519 If we are setting only a portion of X and we can't figure out what
1520 portion, assume all bits will be used since we don't know what will
1521 be happening.
1522
1523 Similarly, set how many bits of X are known to be copies of the sign bit
1524 at all locations in the function. This is the smallest number implied
1525 by any set of X. */
1526
1527 static void
1528 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1529 {
1530 rtx insn = (rtx) data;
1531 unsigned int num;
1532
1533 if (REG_P (x)
1534 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1535 /* If this register is undefined at the start of the file, we can't
1536 say what its contents were. */
1537 && ! REGNO_REG_SET_P
1538 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x))
1539 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
1540 {
1541 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
1542
1543 if (set == 0 || GET_CODE (set) == CLOBBER)
1544 {
1545 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1546 rsp->sign_bit_copies = 1;
1547 return;
1548 }
1549
1550 /* If this register is being initialized using itself, and the
1551 register is uninitialized in this basic block, and there are
1552 no LOG_LINKS which set the register, then part of the
1553 register is uninitialized. In that case we can't assume
1554 anything about the number of nonzero bits.
1555
1556 ??? We could do better if we checked this in
1557 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1558 could avoid making assumptions about the insn which initially
1559 sets the register, while still using the information in other
1560 insns. We would have to be careful to check every insn
1561 involved in the combination. */
1562
1563 if (insn
1564 && reg_referenced_p (x, PATTERN (insn))
1565 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1566 REGNO (x)))
1567 {
1568 struct insn_link *link;
1569
1570 FOR_EACH_LOG_LINK (link, insn)
1571 if (dead_or_set_p (link->insn, x))
1572 break;
1573 if (!link)
1574 {
1575 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1576 rsp->sign_bit_copies = 1;
1577 return;
1578 }
1579 }
1580
1581 /* If this is a complex assignment, see if we can convert it into a
1582 simple assignment. */
1583 set = expand_field_assignment (set);
1584
1585 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1586 set what we know about X. */
1587
1588 if (SET_DEST (set) == x
1589 || (GET_CODE (SET_DEST (set)) == SUBREG
1590 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1591 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1592 && SUBREG_REG (SET_DEST (set)) == x))
1593 {
1594 rtx src = SET_SRC (set);
1595
1596 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1597 /* If X is narrower than a word and SRC is a non-negative
1598 constant that would appear negative in the mode of X,
1599 sign-extend it for use in reg_stat[].nonzero_bits because some
1600 machines (maybe most) will actually do the sign-extension
1601 and this is the conservative approach.
1602
1603 ??? For 2.5, try to tighten up the MD files in this regard
1604 instead of this kludge. */
1605
1606 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1607 && CONST_INT_P (src)
1608 && INTVAL (src) > 0
1609 && 0 != (UINTVAL (src)
1610 & ((unsigned HOST_WIDE_INT) 1
1611 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1612 src = GEN_INT (UINTVAL (src)
1613 | ((unsigned HOST_WIDE_INT) (-1)
1614 << GET_MODE_BITSIZE (GET_MODE (x))));
1615 #endif
1616
1617 /* Don't call nonzero_bits if it cannot change anything. */
1618 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1619 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1620 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1621 if (rsp->sign_bit_copies == 0
1622 || rsp->sign_bit_copies > num)
1623 rsp->sign_bit_copies = num;
1624 }
1625 else
1626 {
1627 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1628 rsp->sign_bit_copies = 1;
1629 }
1630 }
1631 }
1632 \f
1633 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1634 optionally insns that were previously combined into I3 or that will be
1635 combined into the merger of INSN and I3. The order is PRED, PRED2,
1636 INSN, SUCC, SUCC2, I3.
1637
1638 Return 0 if the combination is not allowed for any reason.
1639
1640 If the combination is allowed, *PDEST will be set to the single
1641 destination of INSN and *PSRC to the single source, and this function
1642 will return 1. */
1643
1644 static int
1645 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED,
1646 rtx pred2 ATTRIBUTE_UNUSED, rtx succ, rtx succ2,
1647 rtx *pdest, rtx *psrc)
1648 {
1649 int i;
1650 const_rtx set = 0;
1651 rtx src, dest;
1652 rtx p;
1653 #ifdef AUTO_INC_DEC
1654 rtx link;
1655 #endif
1656 bool all_adjacent = true;
1657
1658 if (succ)
1659 {
1660 if (succ2)
1661 {
1662 if (next_active_insn (succ2) != i3)
1663 all_adjacent = false;
1664 if (next_active_insn (succ) != succ2)
1665 all_adjacent = false;
1666 }
1667 else if (next_active_insn (succ) != i3)
1668 all_adjacent = false;
1669 if (next_active_insn (insn) != succ)
1670 all_adjacent = false;
1671 }
1672 else if (next_active_insn (insn) != i3)
1673 all_adjacent = false;
1674
1675 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1676 or a PARALLEL consisting of such a SET and CLOBBERs.
1677
1678 If INSN has CLOBBER parallel parts, ignore them for our processing.
1679 By definition, these happen during the execution of the insn. When it
1680 is merged with another insn, all bets are off. If they are, in fact,
1681 needed and aren't also supplied in I3, they may be added by
1682 recog_for_combine. Otherwise, it won't match.
1683
1684 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1685 note.
1686
1687 Get the source and destination of INSN. If more than one, can't
1688 combine. */
1689
1690 if (GET_CODE (PATTERN (insn)) == SET)
1691 set = PATTERN (insn);
1692 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1693 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1694 {
1695 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1696 {
1697 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1698
1699 switch (GET_CODE (elt))
1700 {
1701 /* This is important to combine floating point insns
1702 for the SH4 port. */
1703 case USE:
1704 /* Combining an isolated USE doesn't make sense.
1705 We depend here on combinable_i3pat to reject them. */
1706 /* The code below this loop only verifies that the inputs of
1707 the SET in INSN do not change. We call reg_set_between_p
1708 to verify that the REG in the USE does not change between
1709 I3 and INSN.
1710 If the USE in INSN was for a pseudo register, the matching
1711 insn pattern will likely match any register; combining this
1712 with any other USE would only be safe if we knew that the
1713 used registers have identical values, or if there was
1714 something to tell them apart, e.g. different modes. For
1715 now, we forgo such complicated tests and simply disallow
1716 combining of USES of pseudo registers with any other USE. */
1717 if (REG_P (XEXP (elt, 0))
1718 && GET_CODE (PATTERN (i3)) == PARALLEL)
1719 {
1720 rtx i3pat = PATTERN (i3);
1721 int i = XVECLEN (i3pat, 0) - 1;
1722 unsigned int regno = REGNO (XEXP (elt, 0));
1723
1724 do
1725 {
1726 rtx i3elt = XVECEXP (i3pat, 0, i);
1727
1728 if (GET_CODE (i3elt) == USE
1729 && REG_P (XEXP (i3elt, 0))
1730 && (REGNO (XEXP (i3elt, 0)) == regno
1731 ? reg_set_between_p (XEXP (elt, 0),
1732 PREV_INSN (insn), i3)
1733 : regno >= FIRST_PSEUDO_REGISTER))
1734 return 0;
1735 }
1736 while (--i >= 0);
1737 }
1738 break;
1739
1740 /* We can ignore CLOBBERs. */
1741 case CLOBBER:
1742 break;
1743
1744 case SET:
1745 /* Ignore SETs whose result isn't used but not those that
1746 have side-effects. */
1747 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1748 && insn_nothrow_p (insn)
1749 && !side_effects_p (elt))
1750 break;
1751
1752 /* If we have already found a SET, this is a second one and
1753 so we cannot combine with this insn. */
1754 if (set)
1755 return 0;
1756
1757 set = elt;
1758 break;
1759
1760 default:
1761 /* Anything else means we can't combine. */
1762 return 0;
1763 }
1764 }
1765
1766 if (set == 0
1767 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1768 so don't do anything with it. */
1769 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1770 return 0;
1771 }
1772 else
1773 return 0;
1774
1775 if (set == 0)
1776 return 0;
1777
1778 set = expand_field_assignment (set);
1779 src = SET_SRC (set), dest = SET_DEST (set);
1780
1781 /* Don't eliminate a store in the stack pointer. */
1782 if (dest == stack_pointer_rtx
1783 /* Don't combine with an insn that sets a register to itself if it has
1784 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1785 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1786 /* Can't merge an ASM_OPERANDS. */
1787 || GET_CODE (src) == ASM_OPERANDS
1788 /* Can't merge a function call. */
1789 || GET_CODE (src) == CALL
1790 /* Don't eliminate a function call argument. */
1791 || (CALL_P (i3)
1792 && (find_reg_fusage (i3, USE, dest)
1793 || (REG_P (dest)
1794 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1795 && global_regs[REGNO (dest)])))
1796 /* Don't substitute into an incremented register. */
1797 || FIND_REG_INC_NOTE (i3, dest)
1798 || (succ && FIND_REG_INC_NOTE (succ, dest))
1799 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1800 /* Don't substitute into a non-local goto, this confuses CFG. */
1801 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1802 /* Make sure that DEST is not used after SUCC but before I3. */
1803 || (!all_adjacent
1804 && ((succ2
1805 && (reg_used_between_p (dest, succ2, i3)
1806 || reg_used_between_p (dest, succ, succ2)))
1807 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1808 /* Make sure that the value that is to be substituted for the register
1809 does not use any registers whose values alter in between. However,
1810 If the insns are adjacent, a use can't cross a set even though we
1811 think it might (this can happen for a sequence of insns each setting
1812 the same destination; last_set of that register might point to
1813 a NOTE). If INSN has a REG_EQUIV note, the register is always
1814 equivalent to the memory so the substitution is valid even if there
1815 are intervening stores. Also, don't move a volatile asm or
1816 UNSPEC_VOLATILE across any other insns. */
1817 || (! all_adjacent
1818 && (((!MEM_P (src)
1819 || ! find_reg_note (insn, REG_EQUIV, src))
1820 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1821 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1822 || GET_CODE (src) == UNSPEC_VOLATILE))
1823 /* Don't combine across a CALL_INSN, because that would possibly
1824 change whether the life span of some REGs crosses calls or not,
1825 and it is a pain to update that information.
1826 Exception: if source is a constant, moving it later can't hurt.
1827 Accept that as a special case. */
1828 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1829 return 0;
1830
1831 /* DEST must either be a REG or CC0. */
1832 if (REG_P (dest))
1833 {
1834 /* If register alignment is being enforced for multi-word items in all
1835 cases except for parameters, it is possible to have a register copy
1836 insn referencing a hard register that is not allowed to contain the
1837 mode being copied and which would not be valid as an operand of most
1838 insns. Eliminate this problem by not combining with such an insn.
1839
1840 Also, on some machines we don't want to extend the life of a hard
1841 register. */
1842
1843 if (REG_P (src)
1844 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1845 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1846 /* Don't extend the life of a hard register unless it is
1847 user variable (if we have few registers) or it can't
1848 fit into the desired register (meaning something special
1849 is going on).
1850 Also avoid substituting a return register into I3, because
1851 reload can't handle a conflict with constraints of other
1852 inputs. */
1853 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1854 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1855 return 0;
1856 }
1857 else if (GET_CODE (dest) != CC0)
1858 return 0;
1859
1860
1861 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1862 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1863 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1864 {
1865 /* Don't substitute for a register intended as a clobberable
1866 operand. */
1867 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1868 if (rtx_equal_p (reg, dest))
1869 return 0;
1870
1871 /* If the clobber represents an earlyclobber operand, we must not
1872 substitute an expression containing the clobbered register.
1873 As we do not analyze the constraint strings here, we have to
1874 make the conservative assumption. However, if the register is
1875 a fixed hard reg, the clobber cannot represent any operand;
1876 we leave it up to the machine description to either accept or
1877 reject use-and-clobber patterns. */
1878 if (!REG_P (reg)
1879 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1880 || !fixed_regs[REGNO (reg)])
1881 if (reg_overlap_mentioned_p (reg, src))
1882 return 0;
1883 }
1884
1885 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1886 or not), reject, unless nothing volatile comes between it and I3 */
1887
1888 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1889 {
1890 /* Make sure neither succ nor succ2 contains a volatile reference. */
1891 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
1892 return 0;
1893 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1894 return 0;
1895 /* We'll check insns between INSN and I3 below. */
1896 }
1897
1898 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1899 to be an explicit register variable, and was chosen for a reason. */
1900
1901 if (GET_CODE (src) == ASM_OPERANDS
1902 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1903 return 0;
1904
1905 /* If there are any volatile insns between INSN and I3, reject, because
1906 they might affect machine state. */
1907
1908 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1909 if (INSN_P (p) && p != succ && p != succ2 && volatile_insn_p (PATTERN (p)))
1910 return 0;
1911
1912 /* If INSN contains an autoincrement or autodecrement, make sure that
1913 register is not used between there and I3, and not already used in
1914 I3 either. Neither must it be used in PRED or SUCC, if they exist.
1915 Also insist that I3 not be a jump; if it were one
1916 and the incremented register were spilled, we would lose. */
1917
1918 #ifdef AUTO_INC_DEC
1919 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1920 if (REG_NOTE_KIND (link) == REG_INC
1921 && (JUMP_P (i3)
1922 || reg_used_between_p (XEXP (link, 0), insn, i3)
1923 || (pred != NULL_RTX
1924 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1925 || (pred2 != NULL_RTX
1926 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
1927 || (succ != NULL_RTX
1928 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1929 || (succ2 != NULL_RTX
1930 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
1931 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1932 return 0;
1933 #endif
1934
1935 #ifdef HAVE_cc0
1936 /* Don't combine an insn that follows a CC0-setting insn.
1937 An insn that uses CC0 must not be separated from the one that sets it.
1938 We do, however, allow I2 to follow a CC0-setting insn if that insn
1939 is passed as I1; in that case it will be deleted also.
1940 We also allow combining in this case if all the insns are adjacent
1941 because that would leave the two CC0 insns adjacent as well.
1942 It would be more logical to test whether CC0 occurs inside I1 or I2,
1943 but that would be much slower, and this ought to be equivalent. */
1944
1945 p = prev_nonnote_insn (insn);
1946 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1947 && ! all_adjacent)
1948 return 0;
1949 #endif
1950
1951 /* If we get here, we have passed all the tests and the combination is
1952 to be allowed. */
1953
1954 *pdest = dest;
1955 *psrc = src;
1956
1957 return 1;
1958 }
1959 \f
1960 /* LOC is the location within I3 that contains its pattern or the component
1961 of a PARALLEL of the pattern. We validate that it is valid for combining.
1962
1963 One problem is if I3 modifies its output, as opposed to replacing it
1964 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
1965 doing so would produce an insn that is not equivalent to the original insns.
1966
1967 Consider:
1968
1969 (set (reg:DI 101) (reg:DI 100))
1970 (set (subreg:SI (reg:DI 101) 0) <foo>)
1971
1972 This is NOT equivalent to:
1973
1974 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1975 (set (reg:DI 101) (reg:DI 100))])
1976
1977 Not only does this modify 100 (in which case it might still be valid
1978 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1979
1980 We can also run into a problem if I2 sets a register that I1
1981 uses and I1 gets directly substituted into I3 (not via I2). In that
1982 case, we would be getting the wrong value of I2DEST into I3, so we
1983 must reject the combination. This case occurs when I2 and I1 both
1984 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1985 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1986 of a SET must prevent combination from occurring. The same situation
1987 can occur for I0, in which case I0_NOT_IN_SRC is set.
1988
1989 Before doing the above check, we first try to expand a field assignment
1990 into a set of logical operations.
1991
1992 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1993 we place a register that is both set and used within I3. If more than one
1994 such register is detected, we fail.
1995
1996 Return 1 if the combination is valid, zero otherwise. */
1997
1998 static int
1999 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2000 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2001 {
2002 rtx x = *loc;
2003
2004 if (GET_CODE (x) == SET)
2005 {
2006 rtx set = x ;
2007 rtx dest = SET_DEST (set);
2008 rtx src = SET_SRC (set);
2009 rtx inner_dest = dest;
2010 rtx subdest;
2011
2012 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2013 || GET_CODE (inner_dest) == SUBREG
2014 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2015 inner_dest = XEXP (inner_dest, 0);
2016
2017 /* Check for the case where I3 modifies its output, as discussed
2018 above. We don't want to prevent pseudos from being combined
2019 into the address of a MEM, so only prevent the combination if
2020 i1 or i2 set the same MEM. */
2021 if ((inner_dest != dest &&
2022 (!MEM_P (inner_dest)
2023 || rtx_equal_p (i2dest, inner_dest)
2024 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2025 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2026 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2027 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2028 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2029
2030 /* This is the same test done in can_combine_p except we can't test
2031 all_adjacent; we don't have to, since this instruction will stay
2032 in place, thus we are not considering increasing the lifetime of
2033 INNER_DEST.
2034
2035 Also, if this insn sets a function argument, combining it with
2036 something that might need a spill could clobber a previous
2037 function argument; the all_adjacent test in can_combine_p also
2038 checks this; here, we do a more specific test for this case. */
2039
2040 || (REG_P (inner_dest)
2041 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2042 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2043 GET_MODE (inner_dest))))
2044 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2045 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2046 return 0;
2047
2048 /* If DEST is used in I3, it is being killed in this insn, so
2049 record that for later. We have to consider paradoxical
2050 subregs here, since they kill the whole register, but we
2051 ignore partial subregs, STRICT_LOW_PART, etc.
2052 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2053 STACK_POINTER_REGNUM, since these are always considered to be
2054 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2055 subdest = dest;
2056 if (GET_CODE (subdest) == SUBREG
2057 && (GET_MODE_SIZE (GET_MODE (subdest))
2058 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2059 subdest = SUBREG_REG (subdest);
2060 if (pi3dest_killed
2061 && REG_P (subdest)
2062 && reg_referenced_p (subdest, PATTERN (i3))
2063 && REGNO (subdest) != FRAME_POINTER_REGNUM
2064 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2065 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2066 #endif
2067 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2068 && (REGNO (subdest) != ARG_POINTER_REGNUM
2069 || ! fixed_regs [REGNO (subdest)])
2070 #endif
2071 && REGNO (subdest) != STACK_POINTER_REGNUM)
2072 {
2073 if (*pi3dest_killed)
2074 return 0;
2075
2076 *pi3dest_killed = subdest;
2077 }
2078 }
2079
2080 else if (GET_CODE (x) == PARALLEL)
2081 {
2082 int i;
2083
2084 for (i = 0; i < XVECLEN (x, 0); i++)
2085 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2086 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2087 return 0;
2088 }
2089
2090 return 1;
2091 }
2092 \f
2093 /* Return 1 if X is an arithmetic expression that contains a multiplication
2094 and division. We don't count multiplications by powers of two here. */
2095
2096 static int
2097 contains_muldiv (rtx x)
2098 {
2099 switch (GET_CODE (x))
2100 {
2101 case MOD: case DIV: case UMOD: case UDIV:
2102 return 1;
2103
2104 case MULT:
2105 return ! (CONST_INT_P (XEXP (x, 1))
2106 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2107 default:
2108 if (BINARY_P (x))
2109 return contains_muldiv (XEXP (x, 0))
2110 || contains_muldiv (XEXP (x, 1));
2111
2112 if (UNARY_P (x))
2113 return contains_muldiv (XEXP (x, 0));
2114
2115 return 0;
2116 }
2117 }
2118 \f
2119 /* Determine whether INSN can be used in a combination. Return nonzero if
2120 not. This is used in try_combine to detect early some cases where we
2121 can't perform combinations. */
2122
2123 static int
2124 cant_combine_insn_p (rtx insn)
2125 {
2126 rtx set;
2127 rtx src, dest;
2128
2129 /* If this isn't really an insn, we can't do anything.
2130 This can occur when flow deletes an insn that it has merged into an
2131 auto-increment address. */
2132 if (! INSN_P (insn))
2133 return 1;
2134
2135 /* Never combine loads and stores involving hard regs that are likely
2136 to be spilled. The register allocator can usually handle such
2137 reg-reg moves by tying. If we allow the combiner to make
2138 substitutions of likely-spilled regs, reload might die.
2139 As an exception, we allow combinations involving fixed regs; these are
2140 not available to the register allocator so there's no risk involved. */
2141
2142 set = single_set (insn);
2143 if (! set)
2144 return 0;
2145 src = SET_SRC (set);
2146 dest = SET_DEST (set);
2147 if (GET_CODE (src) == SUBREG)
2148 src = SUBREG_REG (src);
2149 if (GET_CODE (dest) == SUBREG)
2150 dest = SUBREG_REG (dest);
2151 if (REG_P (src) && REG_P (dest)
2152 && ((HARD_REGISTER_P (src)
2153 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2154 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2155 || (HARD_REGISTER_P (dest)
2156 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2157 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2158 return 1;
2159
2160 return 0;
2161 }
2162
2163 struct likely_spilled_retval_info
2164 {
2165 unsigned regno, nregs;
2166 unsigned mask;
2167 };
2168
2169 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2170 hard registers that are known to be written to / clobbered in full. */
2171 static void
2172 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2173 {
2174 struct likely_spilled_retval_info *const info =
2175 (struct likely_spilled_retval_info *) data;
2176 unsigned regno, nregs;
2177 unsigned new_mask;
2178
2179 if (!REG_P (XEXP (set, 0)))
2180 return;
2181 regno = REGNO (x);
2182 if (regno >= info->regno + info->nregs)
2183 return;
2184 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2185 if (regno + nregs <= info->regno)
2186 return;
2187 new_mask = (2U << (nregs - 1)) - 1;
2188 if (regno < info->regno)
2189 new_mask >>= info->regno - regno;
2190 else
2191 new_mask <<= regno - info->regno;
2192 info->mask &= ~new_mask;
2193 }
2194
2195 /* Return nonzero iff part of the return value is live during INSN, and
2196 it is likely spilled. This can happen when more than one insn is needed
2197 to copy the return value, e.g. when we consider to combine into the
2198 second copy insn for a complex value. */
2199
2200 static int
2201 likely_spilled_retval_p (rtx insn)
2202 {
2203 rtx use = BB_END (this_basic_block);
2204 rtx reg, p;
2205 unsigned regno, nregs;
2206 /* We assume here that no machine mode needs more than
2207 32 hard registers when the value overlaps with a register
2208 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2209 unsigned mask;
2210 struct likely_spilled_retval_info info;
2211
2212 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2213 return 0;
2214 reg = XEXP (PATTERN (use), 0);
2215 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2216 return 0;
2217 regno = REGNO (reg);
2218 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2219 if (nregs == 1)
2220 return 0;
2221 mask = (2U << (nregs - 1)) - 1;
2222
2223 /* Disregard parts of the return value that are set later. */
2224 info.regno = regno;
2225 info.nregs = nregs;
2226 info.mask = mask;
2227 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2228 if (INSN_P (p))
2229 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2230 mask = info.mask;
2231
2232 /* Check if any of the (probably) live return value registers is
2233 likely spilled. */
2234 nregs --;
2235 do
2236 {
2237 if ((mask & 1 << nregs)
2238 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2239 return 1;
2240 } while (nregs--);
2241 return 0;
2242 }
2243
2244 /* Adjust INSN after we made a change to its destination.
2245
2246 Changing the destination can invalidate notes that say something about
2247 the results of the insn and a LOG_LINK pointing to the insn. */
2248
2249 static void
2250 adjust_for_new_dest (rtx insn)
2251 {
2252 /* For notes, be conservative and simply remove them. */
2253 remove_reg_equal_equiv_notes (insn);
2254
2255 /* The new insn will have a destination that was previously the destination
2256 of an insn just above it. Call distribute_links to make a LOG_LINK from
2257 the next use of that destination. */
2258 distribute_links (alloc_insn_link (insn, NULL));
2259
2260 df_insn_rescan (insn);
2261 }
2262
2263 /* Return TRUE if combine can reuse reg X in mode MODE.
2264 ADDED_SETS is nonzero if the original set is still required. */
2265 static bool
2266 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2267 {
2268 unsigned int regno;
2269
2270 if (!REG_P(x))
2271 return false;
2272
2273 regno = REGNO (x);
2274 /* Allow hard registers if the new mode is legal, and occupies no more
2275 registers than the old mode. */
2276 if (regno < FIRST_PSEUDO_REGISTER)
2277 return (HARD_REGNO_MODE_OK (regno, mode)
2278 && (hard_regno_nregs[regno][GET_MODE (x)]
2279 >= hard_regno_nregs[regno][mode]));
2280
2281 /* Or a pseudo that is only used once. */
2282 return (REG_N_SETS (regno) == 1 && !added_sets
2283 && !REG_USERVAR_P (x));
2284 }
2285
2286
2287 /* Check whether X, the destination of a set, refers to part of
2288 the register specified by REG. */
2289
2290 static bool
2291 reg_subword_p (rtx x, rtx reg)
2292 {
2293 /* Check that reg is an integer mode register. */
2294 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2295 return false;
2296
2297 if (GET_CODE (x) == STRICT_LOW_PART
2298 || GET_CODE (x) == ZERO_EXTRACT)
2299 x = XEXP (x, 0);
2300
2301 return GET_CODE (x) == SUBREG
2302 && SUBREG_REG (x) == reg
2303 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2304 }
2305
2306 #ifdef AUTO_INC_DEC
2307 /* Replace auto-increment addressing modes with explicit operations to access
2308 the same addresses without modifying the corresponding registers. */
2309
2310 static rtx
2311 cleanup_auto_inc_dec (rtx src, enum machine_mode mem_mode)
2312 {
2313 rtx x = src;
2314 const RTX_CODE code = GET_CODE (x);
2315 int i;
2316 const char *fmt;
2317
2318 switch (code)
2319 {
2320 case REG:
2321 case CONST_INT:
2322 case CONST_DOUBLE:
2323 case CONST_FIXED:
2324 case CONST_VECTOR:
2325 case SYMBOL_REF:
2326 case CODE_LABEL:
2327 case PC:
2328 case CC0:
2329 case SCRATCH:
2330 /* SCRATCH must be shared because they represent distinct values. */
2331 return x;
2332 case CLOBBER:
2333 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
2334 return x;
2335 break;
2336
2337 case CONST:
2338 if (shared_const_p (x))
2339 return x;
2340 break;
2341
2342 case MEM:
2343 mem_mode = GET_MODE (x);
2344 break;
2345
2346 case PRE_INC:
2347 case PRE_DEC:
2348 gcc_assert (mem_mode != VOIDmode && mem_mode != BLKmode);
2349 return gen_rtx_PLUS (GET_MODE (x),
2350 cleanup_auto_inc_dec (XEXP (x, 0), mem_mode),
2351 GEN_INT (code == PRE_INC
2352 ? GET_MODE_SIZE (mem_mode)
2353 : -GET_MODE_SIZE (mem_mode)));
2354
2355 case POST_INC:
2356 case POST_DEC:
2357 case PRE_MODIFY:
2358 case POST_MODIFY:
2359 return cleanup_auto_inc_dec (code == PRE_MODIFY
2360 ? XEXP (x, 1) : XEXP (x, 0),
2361 mem_mode);
2362
2363 default:
2364 break;
2365 }
2366
2367 /* Copy the various flags, fields, and other information. We assume
2368 that all fields need copying, and then clear the fields that should
2369 not be copied. That is the sensible default behavior, and forces
2370 us to explicitly document why we are *not* copying a flag. */
2371 x = shallow_copy_rtx (x);
2372
2373 /* We do not copy the USED flag, which is used as a mark bit during
2374 walks over the RTL. */
2375 RTX_FLAG (x, used) = 0;
2376
2377 /* We do not copy FRAME_RELATED for INSNs. */
2378 if (INSN_P (x))
2379 RTX_FLAG (x, frame_related) = 0;
2380
2381 fmt = GET_RTX_FORMAT (code);
2382 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2383 if (fmt[i] == 'e')
2384 XEXP (x, i) = cleanup_auto_inc_dec (XEXP (x, i), mem_mode);
2385 else if (fmt[i] == 'E' || fmt[i] == 'V')
2386 {
2387 int j;
2388 XVEC (x, i) = rtvec_alloc (XVECLEN (x, i));
2389 for (j = 0; j < XVECLEN (x, i); j++)
2390 XVECEXP (x, i, j)
2391 = cleanup_auto_inc_dec (XVECEXP (src, i, j), mem_mode);
2392 }
2393
2394 return x;
2395 }
2396 #endif
2397
2398 /* Auxiliary data structure for propagate_for_debug_stmt. */
2399
2400 struct rtx_subst_pair
2401 {
2402 rtx to;
2403 bool adjusted;
2404 };
2405
2406 /* DATA points to an rtx_subst_pair. Return the value that should be
2407 substituted. */
2408
2409 static rtx
2410 propagate_for_debug_subst (rtx from, const_rtx old_rtx, void *data)
2411 {
2412 struct rtx_subst_pair *pair = (struct rtx_subst_pair *)data;
2413
2414 if (!rtx_equal_p (from, old_rtx))
2415 return NULL_RTX;
2416 if (!pair->adjusted)
2417 {
2418 pair->adjusted = true;
2419 #ifdef AUTO_INC_DEC
2420 pair->to = cleanup_auto_inc_dec (pair->to, VOIDmode);
2421 #else
2422 pair->to = copy_rtx (pair->to);
2423 #endif
2424 pair->to = make_compound_operation (pair->to, SET);
2425 return pair->to;
2426 }
2427 return copy_rtx (pair->to);
2428 }
2429
2430 /* Replace all the occurrences of DEST with SRC in DEBUG_INSNs between INSN
2431 and LAST. */
2432
2433 static void
2434 propagate_for_debug (rtx insn, rtx last, rtx dest, rtx src)
2435 {
2436 rtx next, loc;
2437
2438 struct rtx_subst_pair p;
2439 p.to = src;
2440 p.adjusted = false;
2441
2442 next = NEXT_INSN (insn);
2443 while (next != last)
2444 {
2445 insn = next;
2446 next = NEXT_INSN (insn);
2447 if (DEBUG_INSN_P (insn))
2448 {
2449 loc = simplify_replace_fn_rtx (INSN_VAR_LOCATION_LOC (insn),
2450 dest, propagate_for_debug_subst, &p);
2451 if (loc == INSN_VAR_LOCATION_LOC (insn))
2452 continue;
2453 INSN_VAR_LOCATION_LOC (insn) = loc;
2454 df_insn_rescan (insn);
2455 }
2456 }
2457 }
2458
2459 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2460 Note that the INSN should be deleted *after* removing dead edges, so
2461 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2462 but not for a (set (pc) (label_ref FOO)). */
2463
2464 static void
2465 update_cfg_for_uncondjump (rtx insn)
2466 {
2467 basic_block bb = BLOCK_FOR_INSN (insn);
2468 bool at_end = (BB_END (bb) == insn);
2469
2470 if (at_end)
2471 purge_dead_edges (bb);
2472
2473 delete_insn (insn);
2474 if (at_end && EDGE_COUNT (bb->succs) == 1)
2475 {
2476 rtx insn;
2477
2478 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2479
2480 /* Remove barriers from the footer if there are any. */
2481 for (insn = bb->il.rtl->footer; insn; insn = NEXT_INSN (insn))
2482 if (BARRIER_P (insn))
2483 {
2484 if (PREV_INSN (insn))
2485 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2486 else
2487 bb->il.rtl->footer = NEXT_INSN (insn);
2488 if (NEXT_INSN (insn))
2489 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2490 }
2491 else if (LABEL_P (insn))
2492 break;
2493 }
2494 }
2495
2496 /* Try to combine the insns I0, I1 and I2 into I3.
2497 Here I0, I1 and I2 appear earlier than I3.
2498 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2499 I3.
2500
2501 If we are combining more than two insns and the resulting insn is not
2502 recognized, try splitting it into two insns. If that happens, I2 and I3
2503 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2504 Otherwise, I0, I1 and I2 are pseudo-deleted.
2505
2506 Return 0 if the combination does not work. Then nothing is changed.
2507 If we did the combination, return the insn at which combine should
2508 resume scanning.
2509
2510 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2511 new direct jump instruction. */
2512
2513 static rtx
2514 try_combine (rtx i3, rtx i2, rtx i1, rtx i0, int *new_direct_jump_p)
2515 {
2516 /* New patterns for I3 and I2, respectively. */
2517 rtx newpat, newi2pat = 0;
2518 rtvec newpat_vec_with_clobbers = 0;
2519 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2520 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2521 dead. */
2522 int added_sets_0, added_sets_1, added_sets_2;
2523 /* Total number of SETs to put into I3. */
2524 int total_sets;
2525 /* Nonzero if I2's or I1's body now appears in I3. */
2526 int i2_is_used = 0, i1_is_used = 0;
2527 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2528 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2529 /* Contains I3 if the destination of I3 is used in its source, which means
2530 that the old life of I3 is being killed. If that usage is placed into
2531 I2 and not in I3, a REG_DEAD note must be made. */
2532 rtx i3dest_killed = 0;
2533 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2534 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2535 /* Copy of SET_SRC of I1, if needed. */
2536 rtx i1src_copy = 0;
2537 /* Set if I2DEST was reused as a scratch register. */
2538 bool i2scratch = false;
2539 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2540 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2541 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2542 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2543 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2544 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2545 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2546 /* Notes that must be added to REG_NOTES in I3 and I2. */
2547 rtx new_i3_notes, new_i2_notes;
2548 /* Notes that we substituted I3 into I2 instead of the normal case. */
2549 int i3_subst_into_i2 = 0;
2550 /* Notes that I1, I2 or I3 is a MULT operation. */
2551 int have_mult = 0;
2552 int swap_i2i3 = 0;
2553 int changed_i3_dest = 0;
2554
2555 int maxreg;
2556 rtx temp;
2557 struct insn_link *link;
2558 rtx other_pat = 0;
2559 rtx new_other_notes;
2560 int i;
2561
2562 /* Only try four-insn combinations when there's high likelihood of
2563 success. Look for simple insns, such as loads of constants or
2564 binary operations involving a constant. */
2565 if (i0)
2566 {
2567 int i;
2568 int ngood = 0;
2569 int nshift = 0;
2570
2571 if (!flag_expensive_optimizations)
2572 return 0;
2573
2574 for (i = 0; i < 4; i++)
2575 {
2576 rtx insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2577 rtx set = single_set (insn);
2578 rtx src;
2579 if (!set)
2580 continue;
2581 src = SET_SRC (set);
2582 if (CONSTANT_P (src))
2583 {
2584 ngood += 2;
2585 break;
2586 }
2587 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2588 ngood++;
2589 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2590 || GET_CODE (src) == LSHIFTRT)
2591 nshift++;
2592 }
2593 if (ngood < 2 && nshift < 2)
2594 return 0;
2595 }
2596
2597 /* Exit early if one of the insns involved can't be used for
2598 combinations. */
2599 if (cant_combine_insn_p (i3)
2600 || cant_combine_insn_p (i2)
2601 || (i1 && cant_combine_insn_p (i1))
2602 || (i0 && cant_combine_insn_p (i0))
2603 || likely_spilled_retval_p (i3))
2604 return 0;
2605
2606 combine_attempts++;
2607 undobuf.other_insn = 0;
2608
2609 /* Reset the hard register usage information. */
2610 CLEAR_HARD_REG_SET (newpat_used_regs);
2611
2612 if (dump_file && (dump_flags & TDF_DETAILS))
2613 {
2614 if (i0)
2615 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2616 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2617 else if (i1)
2618 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2619 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2620 else
2621 fprintf (dump_file, "\nTrying %d -> %d:\n",
2622 INSN_UID (i2), INSN_UID (i3));
2623 }
2624
2625 /* If multiple insns feed into one of I2 or I3, they can be in any
2626 order. To simplify the code below, reorder them in sequence. */
2627 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2628 temp = i2, i2 = i0, i0 = temp;
2629 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2630 temp = i1, i1 = i0, i0 = temp;
2631 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2632 temp = i1, i1 = i2, i2 = temp;
2633
2634 added_links_insn = 0;
2635
2636 /* First check for one important special case that the code below will
2637 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2638 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2639 we may be able to replace that destination with the destination of I3.
2640 This occurs in the common code where we compute both a quotient and
2641 remainder into a structure, in which case we want to do the computation
2642 directly into the structure to avoid register-register copies.
2643
2644 Note that this case handles both multiple sets in I2 and also cases
2645 where I2 has a number of CLOBBERs inside the PARALLEL.
2646
2647 We make very conservative checks below and only try to handle the
2648 most common cases of this. For example, we only handle the case
2649 where I2 and I3 are adjacent to avoid making difficult register
2650 usage tests. */
2651
2652 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2653 && REG_P (SET_SRC (PATTERN (i3)))
2654 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2655 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2656 && GET_CODE (PATTERN (i2)) == PARALLEL
2657 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2658 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2659 below would need to check what is inside (and reg_overlap_mentioned_p
2660 doesn't support those codes anyway). Don't allow those destinations;
2661 the resulting insn isn't likely to be recognized anyway. */
2662 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2663 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2664 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2665 SET_DEST (PATTERN (i3)))
2666 && next_active_insn (i2) == i3)
2667 {
2668 rtx p2 = PATTERN (i2);
2669
2670 /* Make sure that the destination of I3,
2671 which we are going to substitute into one output of I2,
2672 is not used within another output of I2. We must avoid making this:
2673 (parallel [(set (mem (reg 69)) ...)
2674 (set (reg 69) ...)])
2675 which is not well-defined as to order of actions.
2676 (Besides, reload can't handle output reloads for this.)
2677
2678 The problem can also happen if the dest of I3 is a memory ref,
2679 if another dest in I2 is an indirect memory ref. */
2680 for (i = 0; i < XVECLEN (p2, 0); i++)
2681 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2682 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2683 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2684 SET_DEST (XVECEXP (p2, 0, i))))
2685 break;
2686
2687 if (i == XVECLEN (p2, 0))
2688 for (i = 0; i < XVECLEN (p2, 0); i++)
2689 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2690 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2691 {
2692 combine_merges++;
2693
2694 subst_insn = i3;
2695 subst_low_luid = DF_INSN_LUID (i2);
2696
2697 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2698 i2src = SET_SRC (XVECEXP (p2, 0, i));
2699 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2700 i2dest_killed = dead_or_set_p (i2, i2dest);
2701
2702 /* Replace the dest in I2 with our dest and make the resulting
2703 insn the new pattern for I3. Then skip to where we validate
2704 the pattern. Everything was set up above. */
2705 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2706 newpat = p2;
2707 i3_subst_into_i2 = 1;
2708 goto validate_replacement;
2709 }
2710 }
2711
2712 /* If I2 is setting a pseudo to a constant and I3 is setting some
2713 sub-part of it to another constant, merge them by making a new
2714 constant. */
2715 if (i1 == 0
2716 && (temp = single_set (i2)) != 0
2717 && (CONST_INT_P (SET_SRC (temp))
2718 || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
2719 && GET_CODE (PATTERN (i3)) == SET
2720 && (CONST_INT_P (SET_SRC (PATTERN (i3)))
2721 || GET_CODE (SET_SRC (PATTERN (i3))) == CONST_DOUBLE)
2722 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2723 {
2724 rtx dest = SET_DEST (PATTERN (i3));
2725 int offset = -1;
2726 int width = 0;
2727
2728 if (GET_CODE (dest) == ZERO_EXTRACT)
2729 {
2730 if (CONST_INT_P (XEXP (dest, 1))
2731 && CONST_INT_P (XEXP (dest, 2)))
2732 {
2733 width = INTVAL (XEXP (dest, 1));
2734 offset = INTVAL (XEXP (dest, 2));
2735 dest = XEXP (dest, 0);
2736 if (BITS_BIG_ENDIAN)
2737 offset = GET_MODE_BITSIZE (GET_MODE (dest)) - width - offset;
2738 }
2739 }
2740 else
2741 {
2742 if (GET_CODE (dest) == STRICT_LOW_PART)
2743 dest = XEXP (dest, 0);
2744 width = GET_MODE_BITSIZE (GET_MODE (dest));
2745 offset = 0;
2746 }
2747
2748 if (offset >= 0)
2749 {
2750 /* If this is the low part, we're done. */
2751 if (subreg_lowpart_p (dest))
2752 ;
2753 /* Handle the case where inner is twice the size of outer. */
2754 else if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2755 == 2 * GET_MODE_BITSIZE (GET_MODE (dest)))
2756 offset += GET_MODE_BITSIZE (GET_MODE (dest));
2757 /* Otherwise give up for now. */
2758 else
2759 offset = -1;
2760 }
2761
2762 if (offset >= 0
2763 && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2764 <= HOST_BITS_PER_DOUBLE_INT))
2765 {
2766 double_int m, o, i;
2767 rtx inner = SET_SRC (PATTERN (i3));
2768 rtx outer = SET_SRC (temp);
2769
2770 o = rtx_to_double_int (outer);
2771 i = rtx_to_double_int (inner);
2772
2773 m = double_int_mask (width);
2774 i = double_int_and (i, m);
2775 m = double_int_lshift (m, offset, HOST_BITS_PER_DOUBLE_INT, false);
2776 i = double_int_lshift (i, offset, HOST_BITS_PER_DOUBLE_INT, false);
2777 o = double_int_ior (double_int_and_not (o, m), i);
2778
2779 combine_merges++;
2780 subst_insn = i3;
2781 subst_low_luid = DF_INSN_LUID (i2);
2782 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2783 i2dest = SET_DEST (temp);
2784 i2dest_killed = dead_or_set_p (i2, i2dest);
2785
2786 /* Replace the source in I2 with the new constant and make the
2787 resulting insn the new pattern for I3. Then skip to where we
2788 validate the pattern. Everything was set up above. */
2789 SUBST (SET_SRC (temp),
2790 immed_double_int_const (o, GET_MODE (SET_DEST (temp))));
2791
2792 newpat = PATTERN (i2);
2793
2794 /* The dest of I3 has been replaced with the dest of I2. */
2795 changed_i3_dest = 1;
2796 goto validate_replacement;
2797 }
2798 }
2799
2800 #ifndef HAVE_cc0
2801 /* If we have no I1 and I2 looks like:
2802 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2803 (set Y OP)])
2804 make up a dummy I1 that is
2805 (set Y OP)
2806 and change I2 to be
2807 (set (reg:CC X) (compare:CC Y (const_int 0)))
2808
2809 (We can ignore any trailing CLOBBERs.)
2810
2811 This undoes a previous combination and allows us to match a branch-and-
2812 decrement insn. */
2813
2814 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2815 && XVECLEN (PATTERN (i2), 0) >= 2
2816 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2817 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2818 == MODE_CC)
2819 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2820 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2821 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2822 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2823 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2824 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2825 {
2826 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2827 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2828 break;
2829
2830 if (i == 1)
2831 {
2832 /* We make I1 with the same INSN_UID as I2. This gives it
2833 the same DF_INSN_LUID for value tracking. Our fake I1 will
2834 never appear in the insn stream so giving it the same INSN_UID
2835 as I2 will not cause a problem. */
2836
2837 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2838 BLOCK_FOR_INSN (i2), XVECEXP (PATTERN (i2), 0, 1),
2839 INSN_LOCATOR (i2), -1, NULL_RTX);
2840
2841 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2842 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2843 SET_DEST (PATTERN (i1)));
2844 }
2845 }
2846 #endif
2847
2848 /* Verify that I2 and I1 are valid for combining. */
2849 if (! can_combine_p (i2, i3, i0, i1, NULL_RTX, NULL_RTX, &i2dest, &i2src)
2850 || (i1 && ! can_combine_p (i1, i3, i0, NULL_RTX, i2, NULL_RTX,
2851 &i1dest, &i1src))
2852 || (i0 && ! can_combine_p (i0, i3, NULL_RTX, NULL_RTX, i1, i2,
2853 &i0dest, &i0src)))
2854 {
2855 undo_all ();
2856 return 0;
2857 }
2858
2859 /* Record whether I2DEST is used in I2SRC and similarly for the other
2860 cases. Knowing this will help in register status updating below. */
2861 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2862 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2863 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2864 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2865 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2866 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2867 i2dest_killed = dead_or_set_p (i2, i2dest);
2868 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2869 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2870
2871 /* For the earlier insns, determine which of the subsequent ones they
2872 feed. */
2873 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2874 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2875 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2876 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2877 && reg_overlap_mentioned_p (i0dest, i2src))));
2878
2879 /* Ensure that I3's pattern can be the destination of combines. */
2880 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2881 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2882 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2883 || (i1dest_in_i0src && !i0_feeds_i1_n)),
2884 &i3dest_killed))
2885 {
2886 undo_all ();
2887 return 0;
2888 }
2889
2890 /* See if any of the insns is a MULT operation. Unless one is, we will
2891 reject a combination that is, since it must be slower. Be conservative
2892 here. */
2893 if (GET_CODE (i2src) == MULT
2894 || (i1 != 0 && GET_CODE (i1src) == MULT)
2895 || (i0 != 0 && GET_CODE (i0src) == MULT)
2896 || (GET_CODE (PATTERN (i3)) == SET
2897 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2898 have_mult = 1;
2899
2900 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2901 We used to do this EXCEPT in one case: I3 has a post-inc in an
2902 output operand. However, that exception can give rise to insns like
2903 mov r3,(r3)+
2904 which is a famous insn on the PDP-11 where the value of r3 used as the
2905 source was model-dependent. Avoid this sort of thing. */
2906
2907 #if 0
2908 if (!(GET_CODE (PATTERN (i3)) == SET
2909 && REG_P (SET_SRC (PATTERN (i3)))
2910 && MEM_P (SET_DEST (PATTERN (i3)))
2911 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2912 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2913 /* It's not the exception. */
2914 #endif
2915 #ifdef AUTO_INC_DEC
2916 {
2917 rtx link;
2918 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2919 if (REG_NOTE_KIND (link) == REG_INC
2920 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2921 || (i1 != 0
2922 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2923 {
2924 undo_all ();
2925 return 0;
2926 }
2927 }
2928 #endif
2929
2930 /* See if the SETs in I1 or I2 need to be kept around in the merged
2931 instruction: whenever the value set there is still needed past I3.
2932 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2933
2934 For the SET in I1, we have two cases: If I1 and I2 independently
2935 feed into I3, the set in I1 needs to be kept around if I1DEST dies
2936 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2937 in I1 needs to be kept around unless I1DEST dies or is set in either
2938 I2 or I3. The same consideration applies to I0. */
2939
2940 added_sets_2 = !dead_or_set_p (i3, i2dest);
2941
2942 if (i1)
2943 added_sets_1 = !(dead_or_set_p (i3, i1dest)
2944 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
2945 else
2946 added_sets_1 = 0;
2947
2948 if (i0)
2949 added_sets_0 = !(dead_or_set_p (i3, i0dest)
2950 || (i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
2951 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)));
2952 else
2953 added_sets_0 = 0;
2954
2955 /* We are about to copy insns for the case where they need to be kept
2956 around. Check that they can be copied in the merged instruction. */
2957
2958 if (targetm.cannot_copy_insn_p
2959 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
2960 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
2961 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
2962 {
2963 undo_all ();
2964 return 0;
2965 }
2966
2967 /* If the set in I2 needs to be kept around, we must make a copy of
2968 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2969 PATTERN (I2), we are only substituting for the original I1DEST, not into
2970 an already-substituted copy. This also prevents making self-referential
2971 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2972 I2DEST. */
2973
2974 if (added_sets_2)
2975 {
2976 if (GET_CODE (PATTERN (i2)) == PARALLEL)
2977 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2978 else
2979 i2pat = copy_rtx (PATTERN (i2));
2980 }
2981
2982 if (added_sets_1)
2983 {
2984 if (GET_CODE (PATTERN (i1)) == PARALLEL)
2985 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2986 else
2987 i1pat = copy_rtx (PATTERN (i1));
2988 }
2989
2990 if (added_sets_0)
2991 {
2992 if (GET_CODE (PATTERN (i0)) == PARALLEL)
2993 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
2994 else
2995 i0pat = copy_rtx (PATTERN (i0));
2996 }
2997
2998 combine_merges++;
2999
3000 /* Substitute in the latest insn for the regs set by the earlier ones. */
3001
3002 maxreg = max_reg_num ();
3003
3004 subst_insn = i3;
3005
3006 #ifndef HAVE_cc0
3007 /* Many machines that don't use CC0 have insns that can both perform an
3008 arithmetic operation and set the condition code. These operations will
3009 be represented as a PARALLEL with the first element of the vector
3010 being a COMPARE of an arithmetic operation with the constant zero.
3011 The second element of the vector will set some pseudo to the result
3012 of the same arithmetic operation. If we simplify the COMPARE, we won't
3013 match such a pattern and so will generate an extra insn. Here we test
3014 for this case, where both the comparison and the operation result are
3015 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3016 I2SRC. Later we will make the PARALLEL that contains I2. */
3017
3018 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3019 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3020 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
3021 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3022 {
3023 #ifdef SELECT_CC_MODE
3024 rtx *cc_use;
3025 enum machine_mode compare_mode;
3026 #endif
3027
3028 newpat = PATTERN (i3);
3029 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
3030
3031 i2_is_used = 1;
3032
3033 #ifdef SELECT_CC_MODE
3034 /* See if a COMPARE with the operand we substituted in should be done
3035 with the mode that is currently being used. If not, do the same
3036 processing we do in `subst' for a SET; namely, if the destination
3037 is used only once, try to replace it with a register of the proper
3038 mode and also replace the COMPARE. */
3039 if (undobuf.other_insn == 0
3040 && (cc_use = find_single_use (SET_DEST (newpat), i3,
3041 &undobuf.other_insn))
3042 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
3043 i2src, const0_rtx))
3044 != GET_MODE (SET_DEST (newpat))))
3045 {
3046 if (can_change_dest_mode (SET_DEST (newpat), added_sets_2,
3047 compare_mode))
3048 {
3049 unsigned int regno = REGNO (SET_DEST (newpat));
3050 rtx new_dest;
3051
3052 if (regno < FIRST_PSEUDO_REGISTER)
3053 new_dest = gen_rtx_REG (compare_mode, regno);
3054 else
3055 {
3056 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3057 new_dest = regno_reg_rtx[regno];
3058 }
3059
3060 SUBST (SET_DEST (newpat), new_dest);
3061 SUBST (XEXP (*cc_use, 0), new_dest);
3062 SUBST (SET_SRC (newpat),
3063 gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
3064 }
3065 else
3066 undobuf.other_insn = 0;
3067 }
3068 #endif
3069 }
3070 else
3071 #endif
3072 {
3073 /* It is possible that the source of I2 or I1 may be performing
3074 an unneeded operation, such as a ZERO_EXTEND of something
3075 that is known to have the high part zero. Handle that case
3076 by letting subst look at the inner insns.
3077
3078 Another way to do this would be to have a function that tries
3079 to simplify a single insn instead of merging two or more
3080 insns. We don't do this because of the potential of infinite
3081 loops and because of the potential extra memory required.
3082 However, doing it the way we are is a bit of a kludge and
3083 doesn't catch all cases.
3084
3085 But only do this if -fexpensive-optimizations since it slows
3086 things down and doesn't usually win.
3087
3088 This is not done in the COMPARE case above because the
3089 unmodified I2PAT is used in the PARALLEL and so a pattern
3090 with a modified I2SRC would not match. */
3091
3092 if (flag_expensive_optimizations)
3093 {
3094 /* Pass pc_rtx so no substitutions are done, just
3095 simplifications. */
3096 if (i1)
3097 {
3098 subst_low_luid = DF_INSN_LUID (i1);
3099 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
3100 }
3101
3102 subst_low_luid = DF_INSN_LUID (i2);
3103 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
3104 }
3105
3106 n_occurrences = 0; /* `subst' counts here */
3107 subst_low_luid = DF_INSN_LUID (i2);
3108
3109 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3110 copy of I2SRC each time we substitute it, in order to avoid creating
3111 self-referential RTL when we will be substituting I1SRC for I1DEST
3112 later. Likewise if I0 feeds into I2, either directly or indirectly
3113 through I1, and I0DEST is in I0SRC. */
3114 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
3115 (i1_feeds_i2_n && i1dest_in_i1src)
3116 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3117 && i0dest_in_i0src));
3118 substed_i2 = 1;
3119
3120 /* Record whether I2's body now appears within I3's body. */
3121 i2_is_used = n_occurrences;
3122 }
3123
3124 /* If we already got a failure, don't try to do more. Otherwise, try to
3125 substitute I1 if we have it. */
3126
3127 if (i1 && GET_CODE (newpat) != CLOBBER)
3128 {
3129 /* Check that an autoincrement side-effect on I1 has not been lost.
3130 This happens if I1DEST is mentioned in I2 and dies there, and
3131 has disappeared from the new pattern. */
3132 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3133 && i1_feeds_i2_n
3134 && dead_or_set_p (i2, i1dest)
3135 && !reg_overlap_mentioned_p (i1dest, newpat))
3136 /* Before we can do this substitution, we must redo the test done
3137 above (see detailed comments there) that ensures I1DEST isn't
3138 mentioned in any SETs in NEWPAT that are field assignments. */
3139 || !combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX, NULL_RTX,
3140 0, 0, 0))
3141 {
3142 undo_all ();
3143 return 0;
3144 }
3145
3146 n_occurrences = 0;
3147 subst_low_luid = DF_INSN_LUID (i1);
3148
3149 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3150 copy of I1SRC each time we substitute it, in order to avoid creating
3151 self-referential RTL when we will be substituting I0SRC for I0DEST
3152 later. */
3153 newpat = subst (newpat, i1dest, i1src, 0,
3154 i0_feeds_i1_n && i0dest_in_i0src);
3155 substed_i1 = 1;
3156
3157 /* Record whether I1's body now appears within I3's body. */
3158 i1_is_used = n_occurrences;
3159 }
3160
3161 /* Likewise for I0 if we have it. */
3162
3163 if (i0 && GET_CODE (newpat) != CLOBBER)
3164 {
3165 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3166 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3167 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3168 && !reg_overlap_mentioned_p (i0dest, newpat))
3169 || !combinable_i3pat (NULL_RTX, &newpat, i0dest, NULL_RTX, NULL_RTX,
3170 0, 0, 0))
3171 {
3172 undo_all ();
3173 return 0;
3174 }
3175
3176 /* If the following substitution will modify I1SRC, make a copy of it
3177 for the case where it is substituted for I1DEST in I2PAT later. */
3178 if (i0_feeds_i1_n && added_sets_2 && i1_feeds_i2_n)
3179 i1src_copy = copy_rtx (i1src);
3180
3181 n_occurrences = 0;
3182 subst_low_luid = DF_INSN_LUID (i0);
3183 newpat = subst (newpat, i0dest, i0src, 0, 0);
3184 substed_i0 = 1;
3185 }
3186
3187 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3188 to count all the ways that I2SRC and I1SRC can be used. */
3189 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3190 && i2_is_used + added_sets_2 > 1)
3191 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3192 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3193 > 1))
3194 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3195 && (n_occurrences + added_sets_0
3196 + (added_sets_1 && i0_feeds_i1_n)
3197 + (added_sets_2 && i0_feeds_i2_n)
3198 > 1))
3199 /* Fail if we tried to make a new register. */
3200 || max_reg_num () != maxreg
3201 /* Fail if we couldn't do something and have a CLOBBER. */
3202 || GET_CODE (newpat) == CLOBBER
3203 /* Fail if this new pattern is a MULT and we didn't have one before
3204 at the outer level. */
3205 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3206 && ! have_mult))
3207 {
3208 undo_all ();
3209 return 0;
3210 }
3211
3212 /* If the actions of the earlier insns must be kept
3213 in addition to substituting them into the latest one,
3214 we must make a new PARALLEL for the latest insn
3215 to hold additional the SETs. */
3216
3217 if (added_sets_0 || added_sets_1 || added_sets_2)
3218 {
3219 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3220 combine_extras++;
3221
3222 if (GET_CODE (newpat) == PARALLEL)
3223 {
3224 rtvec old = XVEC (newpat, 0);
3225 total_sets = XVECLEN (newpat, 0) + extra_sets;
3226 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3227 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3228 sizeof (old->elem[0]) * old->num_elem);
3229 }
3230 else
3231 {
3232 rtx old = newpat;
3233 total_sets = 1 + extra_sets;
3234 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3235 XVECEXP (newpat, 0, 0) = old;
3236 }
3237
3238 if (added_sets_0)
3239 XVECEXP (newpat, 0, --total_sets) = i0pat;
3240
3241 if (added_sets_1)
3242 {
3243 rtx t = i1pat;
3244 if (i0_feeds_i1_n)
3245 t = subst (t, i0dest, i0src, 0, 0);
3246
3247 XVECEXP (newpat, 0, --total_sets) = t;
3248 }
3249 if (added_sets_2)
3250 {
3251 rtx t = i2pat;
3252 if (i1_feeds_i2_n)
3253 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0,
3254 i0_feeds_i1_n && i0dest_in_i0src);
3255 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3256 t = subst (t, i0dest, i0src, 0, 0);
3257
3258 XVECEXP (newpat, 0, --total_sets) = t;
3259 }
3260 }
3261
3262 validate_replacement:
3263
3264 /* Note which hard regs this insn has as inputs. */
3265 mark_used_regs_combine (newpat);
3266
3267 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3268 consider splitting this pattern, we might need these clobbers. */
3269 if (i1 && GET_CODE (newpat) == PARALLEL
3270 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3271 {
3272 int len = XVECLEN (newpat, 0);
3273
3274 newpat_vec_with_clobbers = rtvec_alloc (len);
3275 for (i = 0; i < len; i++)
3276 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3277 }
3278
3279 /* Is the result of combination a valid instruction? */
3280 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3281
3282 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
3283 the second SET's destination is a register that is unused and isn't
3284 marked as an instruction that might trap in an EH region. In that case,
3285 we just need the first SET. This can occur when simplifying a divmod
3286 insn. We *must* test for this case here because the code below that
3287 splits two independent SETs doesn't handle this case correctly when it
3288 updates the register status.
3289
3290 It's pointless doing this if we originally had two sets, one from
3291 i3, and one from i2. Combining then splitting the parallel results
3292 in the original i2 again plus an invalid insn (which we delete).
3293 The net effect is only to move instructions around, which makes
3294 debug info less accurate.
3295
3296 Also check the case where the first SET's destination is unused.
3297 That would not cause incorrect code, but does cause an unneeded
3298 insn to remain. */
3299
3300 if (insn_code_number < 0
3301 && !(added_sets_2 && i1 == 0)
3302 && GET_CODE (newpat) == PARALLEL
3303 && XVECLEN (newpat, 0) == 2
3304 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3305 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3306 && asm_noperands (newpat) < 0)
3307 {
3308 rtx set0 = XVECEXP (newpat, 0, 0);
3309 rtx set1 = XVECEXP (newpat, 0, 1);
3310
3311 if (((REG_P (SET_DEST (set1))
3312 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3313 || (GET_CODE (SET_DEST (set1)) == SUBREG
3314 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3315 && insn_nothrow_p (i3)
3316 && !side_effects_p (SET_SRC (set1)))
3317 {
3318 newpat = set0;
3319 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3320 }
3321
3322 else if (((REG_P (SET_DEST (set0))
3323 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3324 || (GET_CODE (SET_DEST (set0)) == SUBREG
3325 && find_reg_note (i3, REG_UNUSED,
3326 SUBREG_REG (SET_DEST (set0)))))
3327 && insn_nothrow_p (i3)
3328 && !side_effects_p (SET_SRC (set0)))
3329 {
3330 newpat = set1;
3331 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3332
3333 if (insn_code_number >= 0)
3334 changed_i3_dest = 1;
3335 }
3336 }
3337
3338 /* If we were combining three insns and the result is a simple SET
3339 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3340 insns. There are two ways to do this. It can be split using a
3341 machine-specific method (like when you have an addition of a large
3342 constant) or by combine in the function find_split_point. */
3343
3344 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3345 && asm_noperands (newpat) < 0)
3346 {
3347 rtx parallel, m_split, *split;
3348
3349 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3350 use I2DEST as a scratch register will help. In the latter case,
3351 convert I2DEST to the mode of the source of NEWPAT if we can. */
3352
3353 m_split = combine_split_insns (newpat, i3);
3354
3355 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3356 inputs of NEWPAT. */
3357
3358 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3359 possible to try that as a scratch reg. This would require adding
3360 more code to make it work though. */
3361
3362 if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3363 {
3364 enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3365
3366 /* First try to split using the original register as a
3367 scratch register. */
3368 parallel = gen_rtx_PARALLEL (VOIDmode,
3369 gen_rtvec (2, newpat,
3370 gen_rtx_CLOBBER (VOIDmode,
3371 i2dest)));
3372 m_split = combine_split_insns (parallel, i3);
3373
3374 /* If that didn't work, try changing the mode of I2DEST if
3375 we can. */
3376 if (m_split == 0
3377 && new_mode != GET_MODE (i2dest)
3378 && new_mode != VOIDmode
3379 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3380 {
3381 enum machine_mode old_mode = GET_MODE (i2dest);
3382 rtx ni2dest;
3383
3384 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3385 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3386 else
3387 {
3388 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3389 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3390 }
3391
3392 parallel = (gen_rtx_PARALLEL
3393 (VOIDmode,
3394 gen_rtvec (2, newpat,
3395 gen_rtx_CLOBBER (VOIDmode,
3396 ni2dest))));
3397 m_split = combine_split_insns (parallel, i3);
3398
3399 if (m_split == 0
3400 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3401 {
3402 struct undo *buf;
3403
3404 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3405 buf = undobuf.undos;
3406 undobuf.undos = buf->next;
3407 buf->next = undobuf.frees;
3408 undobuf.frees = buf;
3409 }
3410 }
3411
3412 i2scratch = m_split != 0;
3413 }
3414
3415 /* If recog_for_combine has discarded clobbers, try to use them
3416 again for the split. */
3417 if (m_split == 0 && newpat_vec_with_clobbers)
3418 {
3419 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3420 m_split = combine_split_insns (parallel, i3);
3421 }
3422
3423 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
3424 {
3425 m_split = PATTERN (m_split);
3426 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
3427 if (insn_code_number >= 0)
3428 newpat = m_split;
3429 }
3430 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
3431 && (next_real_insn (i2) == i3
3432 || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
3433 {
3434 rtx i2set, i3set;
3435 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
3436 newi2pat = PATTERN (m_split);
3437
3438 i3set = single_set (NEXT_INSN (m_split));
3439 i2set = single_set (m_split);
3440
3441 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3442
3443 /* If I2 or I3 has multiple SETs, we won't know how to track
3444 register status, so don't use these insns. If I2's destination
3445 is used between I2 and I3, we also can't use these insns. */
3446
3447 if (i2_code_number >= 0 && i2set && i3set
3448 && (next_real_insn (i2) == i3
3449 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3450 insn_code_number = recog_for_combine (&newi3pat, i3,
3451 &new_i3_notes);
3452 if (insn_code_number >= 0)
3453 newpat = newi3pat;
3454
3455 /* It is possible that both insns now set the destination of I3.
3456 If so, we must show an extra use of it. */
3457
3458 if (insn_code_number >= 0)
3459 {
3460 rtx new_i3_dest = SET_DEST (i3set);
3461 rtx new_i2_dest = SET_DEST (i2set);
3462
3463 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3464 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3465 || GET_CODE (new_i3_dest) == SUBREG)
3466 new_i3_dest = XEXP (new_i3_dest, 0);
3467
3468 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3469 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3470 || GET_CODE (new_i2_dest) == SUBREG)
3471 new_i2_dest = XEXP (new_i2_dest, 0);
3472
3473 if (REG_P (new_i3_dest)
3474 && REG_P (new_i2_dest)
3475 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3476 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3477 }
3478 }
3479
3480 /* If we can split it and use I2DEST, go ahead and see if that
3481 helps things be recognized. Verify that none of the registers
3482 are set between I2 and I3. */
3483 if (insn_code_number < 0
3484 && (split = find_split_point (&newpat, i3, false)) != 0
3485 #ifdef HAVE_cc0
3486 && REG_P (i2dest)
3487 #endif
3488 /* We need I2DEST in the proper mode. If it is a hard register
3489 or the only use of a pseudo, we can change its mode.
3490 Make sure we don't change a hard register to have a mode that
3491 isn't valid for it, or change the number of registers. */
3492 && (GET_MODE (*split) == GET_MODE (i2dest)
3493 || GET_MODE (*split) == VOIDmode
3494 || can_change_dest_mode (i2dest, added_sets_2,
3495 GET_MODE (*split)))
3496 && (next_real_insn (i2) == i3
3497 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3498 /* We can't overwrite I2DEST if its value is still used by
3499 NEWPAT. */
3500 && ! reg_referenced_p (i2dest, newpat))
3501 {
3502 rtx newdest = i2dest;
3503 enum rtx_code split_code = GET_CODE (*split);
3504 enum machine_mode split_mode = GET_MODE (*split);
3505 bool subst_done = false;
3506 newi2pat = NULL_RTX;
3507
3508 i2scratch = true;
3509
3510 /* *SPLIT may be part of I2SRC, so make sure we have the
3511 original expression around for later debug processing.
3512 We should not need I2SRC any more in other cases. */
3513 if (MAY_HAVE_DEBUG_INSNS)
3514 i2src = copy_rtx (i2src);
3515 else
3516 i2src = NULL;
3517
3518 /* Get NEWDEST as a register in the proper mode. We have already
3519 validated that we can do this. */
3520 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3521 {
3522 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3523 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3524 else
3525 {
3526 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3527 newdest = regno_reg_rtx[REGNO (i2dest)];
3528 }
3529 }
3530
3531 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3532 an ASHIFT. This can occur if it was inside a PLUS and hence
3533 appeared to be a memory address. This is a kludge. */
3534 if (split_code == MULT
3535 && CONST_INT_P (XEXP (*split, 1))
3536 && INTVAL (XEXP (*split, 1)) > 0
3537 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3538 {
3539 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3540 XEXP (*split, 0), GEN_INT (i)));
3541 /* Update split_code because we may not have a multiply
3542 anymore. */
3543 split_code = GET_CODE (*split);
3544 }
3545
3546 #ifdef INSN_SCHEDULING
3547 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3548 be written as a ZERO_EXTEND. */
3549 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3550 {
3551 #ifdef LOAD_EXTEND_OP
3552 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3553 what it really is. */
3554 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3555 == SIGN_EXTEND)
3556 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3557 SUBREG_REG (*split)));
3558 else
3559 #endif
3560 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3561 SUBREG_REG (*split)));
3562 }
3563 #endif
3564
3565 /* Attempt to split binary operators using arithmetic identities. */
3566 if (BINARY_P (SET_SRC (newpat))
3567 && split_mode == GET_MODE (SET_SRC (newpat))
3568 && ! side_effects_p (SET_SRC (newpat)))
3569 {
3570 rtx setsrc = SET_SRC (newpat);
3571 enum machine_mode mode = GET_MODE (setsrc);
3572 enum rtx_code code = GET_CODE (setsrc);
3573 rtx src_op0 = XEXP (setsrc, 0);
3574 rtx src_op1 = XEXP (setsrc, 1);
3575
3576 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3577 if (rtx_equal_p (src_op0, src_op1))
3578 {
3579 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3580 SUBST (XEXP (setsrc, 0), newdest);
3581 SUBST (XEXP (setsrc, 1), newdest);
3582 subst_done = true;
3583 }
3584 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3585 else if ((code == PLUS || code == MULT)
3586 && GET_CODE (src_op0) == code
3587 && GET_CODE (XEXP (src_op0, 0)) == code
3588 && (INTEGRAL_MODE_P (mode)
3589 || (FLOAT_MODE_P (mode)
3590 && flag_unsafe_math_optimizations)))
3591 {
3592 rtx p = XEXP (XEXP (src_op0, 0), 0);
3593 rtx q = XEXP (XEXP (src_op0, 0), 1);
3594 rtx r = XEXP (src_op0, 1);
3595 rtx s = src_op1;
3596
3597 /* Split both "((X op Y) op X) op Y" and
3598 "((X op Y) op Y) op X" as "T op T" where T is
3599 "X op Y". */
3600 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3601 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3602 {
3603 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3604 XEXP (src_op0, 0));
3605 SUBST (XEXP (setsrc, 0), newdest);
3606 SUBST (XEXP (setsrc, 1), newdest);
3607 subst_done = true;
3608 }
3609 /* Split "((X op X) op Y) op Y)" as "T op T" where
3610 T is "X op Y". */
3611 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3612 {
3613 rtx tmp = simplify_gen_binary (code, mode, p, r);
3614 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3615 SUBST (XEXP (setsrc, 0), newdest);
3616 SUBST (XEXP (setsrc, 1), newdest);
3617 subst_done = true;
3618 }
3619 }
3620 }
3621
3622 if (!subst_done)
3623 {
3624 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3625 SUBST (*split, newdest);
3626 }
3627
3628 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3629
3630 /* recog_for_combine might have added CLOBBERs to newi2pat.
3631 Make sure NEWPAT does not depend on the clobbered regs. */
3632 if (GET_CODE (newi2pat) == PARALLEL)
3633 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3634 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3635 {
3636 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3637 if (reg_overlap_mentioned_p (reg, newpat))
3638 {
3639 undo_all ();
3640 return 0;
3641 }
3642 }
3643
3644 /* If the split point was a MULT and we didn't have one before,
3645 don't use one now. */
3646 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3647 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3648 }
3649 }
3650
3651 /* Check for a case where we loaded from memory in a narrow mode and
3652 then sign extended it, but we need both registers. In that case,
3653 we have a PARALLEL with both loads from the same memory location.
3654 We can split this into a load from memory followed by a register-register
3655 copy. This saves at least one insn, more if register allocation can
3656 eliminate the copy.
3657
3658 We cannot do this if the destination of the first assignment is a
3659 condition code register or cc0. We eliminate this case by making sure
3660 the SET_DEST and SET_SRC have the same mode.
3661
3662 We cannot do this if the destination of the second assignment is
3663 a register that we have already assumed is zero-extended. Similarly
3664 for a SUBREG of such a register. */
3665
3666 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3667 && GET_CODE (newpat) == PARALLEL
3668 && XVECLEN (newpat, 0) == 2
3669 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3670 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3671 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3672 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3673 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3674 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3675 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3676 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3677 DF_INSN_LUID (i2))
3678 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3679 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3680 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3681 (REG_P (temp)
3682 && VEC_index (reg_stat_type, reg_stat,
3683 REGNO (temp))->nonzero_bits != 0
3684 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3685 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3686 && (VEC_index (reg_stat_type, reg_stat,
3687 REGNO (temp))->nonzero_bits
3688 != GET_MODE_MASK (word_mode))))
3689 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3690 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3691 (REG_P (temp)
3692 && VEC_index (reg_stat_type, reg_stat,
3693 REGNO (temp))->nonzero_bits != 0
3694 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3695 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3696 && (VEC_index (reg_stat_type, reg_stat,
3697 REGNO (temp))->nonzero_bits
3698 != GET_MODE_MASK (word_mode)))))
3699 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3700 SET_SRC (XVECEXP (newpat, 0, 1)))
3701 && ! find_reg_note (i3, REG_UNUSED,
3702 SET_DEST (XVECEXP (newpat, 0, 0))))
3703 {
3704 rtx ni2dest;
3705
3706 newi2pat = XVECEXP (newpat, 0, 0);
3707 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3708 newpat = XVECEXP (newpat, 0, 1);
3709 SUBST (SET_SRC (newpat),
3710 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3711 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3712
3713 if (i2_code_number >= 0)
3714 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3715
3716 if (insn_code_number >= 0)
3717 swap_i2i3 = 1;
3718 }
3719
3720 /* Similarly, check for a case where we have a PARALLEL of two independent
3721 SETs but we started with three insns. In this case, we can do the sets
3722 as two separate insns. This case occurs when some SET allows two
3723 other insns to combine, but the destination of that SET is still live. */
3724
3725 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3726 && GET_CODE (newpat) == PARALLEL
3727 && XVECLEN (newpat, 0) == 2
3728 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3729 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3730 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3731 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3732 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3733 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3734 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3735 XVECEXP (newpat, 0, 0))
3736 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3737 XVECEXP (newpat, 0, 1))
3738 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3739 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3740 {
3741 /* Normally, it doesn't matter which of the two is done first,
3742 but the one that references cc0 can't be the second, and
3743 one which uses any regs/memory set in between i2 and i3 can't
3744 be first. */
3745 if (!use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3746 DF_INSN_LUID (i2))
3747 #ifdef HAVE_cc0
3748 && !reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
3749 #endif
3750 )
3751 {
3752 newi2pat = XVECEXP (newpat, 0, 1);
3753 newpat = XVECEXP (newpat, 0, 0);
3754 }
3755 else if (!use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 0)),
3756 DF_INSN_LUID (i2))
3757 #ifdef HAVE_cc0
3758 && !reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1))
3759 #endif
3760 )
3761 {
3762 newi2pat = XVECEXP (newpat, 0, 0);
3763 newpat = XVECEXP (newpat, 0, 1);
3764 }
3765 else
3766 {
3767 undo_all ();
3768 return 0;
3769 }
3770
3771 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3772
3773 if (i2_code_number >= 0)
3774 {
3775 /* recog_for_combine might have added CLOBBERs to newi2pat.
3776 Make sure NEWPAT does not depend on the clobbered regs. */
3777 if (GET_CODE (newi2pat) == PARALLEL)
3778 {
3779 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3780 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3781 {
3782 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3783 if (reg_overlap_mentioned_p (reg, newpat))
3784 {
3785 undo_all ();
3786 return 0;
3787 }
3788 }
3789 }
3790
3791 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3792 }
3793 }
3794
3795 /* If it still isn't recognized, fail and change things back the way they
3796 were. */
3797 if ((insn_code_number < 0
3798 /* Is the result a reasonable ASM_OPERANDS? */
3799 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3800 {
3801 undo_all ();
3802 return 0;
3803 }
3804
3805 /* If we had to change another insn, make sure it is valid also. */
3806 if (undobuf.other_insn)
3807 {
3808 CLEAR_HARD_REG_SET (newpat_used_regs);
3809
3810 other_pat = PATTERN (undobuf.other_insn);
3811 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3812 &new_other_notes);
3813
3814 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3815 {
3816 undo_all ();
3817 return 0;
3818 }
3819 }
3820
3821 #ifdef HAVE_cc0
3822 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3823 they are adjacent to each other or not. */
3824 {
3825 rtx p = prev_nonnote_insn (i3);
3826 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3827 && sets_cc0_p (newi2pat))
3828 {
3829 undo_all ();
3830 return 0;
3831 }
3832 }
3833 #endif
3834
3835 /* Only allow this combination if insn_rtx_costs reports that the
3836 replacement instructions are cheaper than the originals. */
3837 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
3838 {
3839 undo_all ();
3840 return 0;
3841 }
3842
3843 if (MAY_HAVE_DEBUG_INSNS)
3844 {
3845 struct undo *undo;
3846
3847 for (undo = undobuf.undos; undo; undo = undo->next)
3848 if (undo->kind == UNDO_MODE)
3849 {
3850 rtx reg = *undo->where.r;
3851 enum machine_mode new_mode = GET_MODE (reg);
3852 enum machine_mode old_mode = undo->old_contents.m;
3853
3854 /* Temporarily revert mode back. */
3855 adjust_reg_mode (reg, old_mode);
3856
3857 if (reg == i2dest && i2scratch)
3858 {
3859 /* If we used i2dest as a scratch register with a
3860 different mode, substitute it for the original
3861 i2src while its original mode is temporarily
3862 restored, and then clear i2scratch so that we don't
3863 do it again later. */
3864 propagate_for_debug (i2, i3, reg, i2src);
3865 i2scratch = false;
3866 /* Put back the new mode. */
3867 adjust_reg_mode (reg, new_mode);
3868 }
3869 else
3870 {
3871 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3872 rtx first, last;
3873
3874 if (reg == i2dest)
3875 {
3876 first = i2;
3877 last = i3;
3878 }
3879 else
3880 {
3881 first = i3;
3882 last = undobuf.other_insn;
3883 gcc_assert (last);
3884 }
3885
3886 /* We're dealing with a reg that changed mode but not
3887 meaning, so we want to turn it into a subreg for
3888 the new mode. However, because of REG sharing and
3889 because its mode had already changed, we have to do
3890 it in two steps. First, replace any debug uses of
3891 reg, with its original mode temporarily restored,
3892 with this copy we have created; then, replace the
3893 copy with the SUBREG of the original shared reg,
3894 once again changed to the new mode. */
3895 propagate_for_debug (first, last, reg, tempreg);
3896 adjust_reg_mode (reg, new_mode);
3897 propagate_for_debug (first, last, tempreg,
3898 lowpart_subreg (old_mode, reg, new_mode));
3899 }
3900 }
3901 }
3902
3903 /* If we will be able to accept this, we have made a
3904 change to the destination of I3. This requires us to
3905 do a few adjustments. */
3906
3907 if (changed_i3_dest)
3908 {
3909 PATTERN (i3) = newpat;
3910 adjust_for_new_dest (i3);
3911 }
3912
3913 /* We now know that we can do this combination. Merge the insns and
3914 update the status of registers and LOG_LINKS. */
3915
3916 if (undobuf.other_insn)
3917 {
3918 rtx note, next;
3919
3920 PATTERN (undobuf.other_insn) = other_pat;
3921
3922 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3923 are still valid. Then add any non-duplicate notes added by
3924 recog_for_combine. */
3925 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3926 {
3927 next = XEXP (note, 1);
3928
3929 if (REG_NOTE_KIND (note) == REG_UNUSED
3930 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
3931 remove_note (undobuf.other_insn, note);
3932 }
3933
3934 distribute_notes (new_other_notes, undobuf.other_insn,
3935 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX,
3936 NULL_RTX);
3937 }
3938
3939 if (swap_i2i3)
3940 {
3941 rtx insn;
3942 struct insn_link *link;
3943 rtx ni2dest;
3944
3945 /* I3 now uses what used to be its destination and which is now
3946 I2's destination. This requires us to do a few adjustments. */
3947 PATTERN (i3) = newpat;
3948 adjust_for_new_dest (i3);
3949
3950 /* We need a LOG_LINK from I3 to I2. But we used to have one,
3951 so we still will.
3952
3953 However, some later insn might be using I2's dest and have
3954 a LOG_LINK pointing at I3. We must remove this link.
3955 The simplest way to remove the link is to point it at I1,
3956 which we know will be a NOTE. */
3957
3958 /* newi2pat is usually a SET here; however, recog_for_combine might
3959 have added some clobbers. */
3960 if (GET_CODE (newi2pat) == PARALLEL)
3961 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3962 else
3963 ni2dest = SET_DEST (newi2pat);
3964
3965 for (insn = NEXT_INSN (i3);
3966 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3967 || insn != BB_HEAD (this_basic_block->next_bb));
3968 insn = NEXT_INSN (insn))
3969 {
3970 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3971 {
3972 FOR_EACH_LOG_LINK (link, insn)
3973 if (link->insn == i3)
3974 link->insn = i1;
3975
3976 break;
3977 }
3978 }
3979 }
3980
3981 {
3982 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
3983 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
3984 rtx midnotes = 0;
3985 int from_luid;
3986 /* Compute which registers we expect to eliminate. newi2pat may be setting
3987 either i3dest or i2dest, so we must check it. Also, i1dest may be the
3988 same as i3dest, in which case newi2pat may be setting i1dest. */
3989 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
3990 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
3991 || !i2dest_killed
3992 ? 0 : i2dest);
3993 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
3994 || (newi2pat && reg_set_p (i1dest, newi2pat))
3995 || !i1dest_killed
3996 ? 0 : i1dest);
3997 rtx elim_i0 = (i0 == 0 || i0dest_in_i0src
3998 || (newi2pat && reg_set_p (i0dest, newi2pat))
3999 || !i0dest_killed
4000 ? 0 : i0dest);
4001
4002 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4003 clear them. */
4004 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4005 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4006 if (i1)
4007 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4008 if (i0)
4009 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4010
4011 /* Ensure that we do not have something that should not be shared but
4012 occurs multiple times in the new insns. Check this by first
4013 resetting all the `used' flags and then copying anything is shared. */
4014
4015 reset_used_flags (i3notes);
4016 reset_used_flags (i2notes);
4017 reset_used_flags (i1notes);
4018 reset_used_flags (i0notes);
4019 reset_used_flags (newpat);
4020 reset_used_flags (newi2pat);
4021 if (undobuf.other_insn)
4022 reset_used_flags (PATTERN (undobuf.other_insn));
4023
4024 i3notes = copy_rtx_if_shared (i3notes);
4025 i2notes = copy_rtx_if_shared (i2notes);
4026 i1notes = copy_rtx_if_shared (i1notes);
4027 i0notes = copy_rtx_if_shared (i0notes);
4028 newpat = copy_rtx_if_shared (newpat);
4029 newi2pat = copy_rtx_if_shared (newi2pat);
4030 if (undobuf.other_insn)
4031 reset_used_flags (PATTERN (undobuf.other_insn));
4032
4033 INSN_CODE (i3) = insn_code_number;
4034 PATTERN (i3) = newpat;
4035
4036 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4037 {
4038 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4039
4040 reset_used_flags (call_usage);
4041 call_usage = copy_rtx (call_usage);
4042
4043 if (substed_i2)
4044 {
4045 /* I2SRC must still be meaningful at this point. Some splitting
4046 operations can invalidate I2SRC, but those operations do not
4047 apply to calls. */
4048 gcc_assert (i2src);
4049 replace_rtx (call_usage, i2dest, i2src);
4050 }
4051
4052 if (substed_i1)
4053 replace_rtx (call_usage, i1dest, i1src);
4054 if (substed_i0)
4055 replace_rtx (call_usage, i0dest, i0src);
4056
4057 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4058 }
4059
4060 if (undobuf.other_insn)
4061 INSN_CODE (undobuf.other_insn) = other_code_number;
4062
4063 /* We had one special case above where I2 had more than one set and
4064 we replaced a destination of one of those sets with the destination
4065 of I3. In that case, we have to update LOG_LINKS of insns later
4066 in this basic block. Note that this (expensive) case is rare.
4067
4068 Also, in this case, we must pretend that all REG_NOTEs for I2
4069 actually came from I3, so that REG_UNUSED notes from I2 will be
4070 properly handled. */
4071
4072 if (i3_subst_into_i2)
4073 {
4074 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4075 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4076 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4077 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4078 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4079 && ! find_reg_note (i2, REG_UNUSED,
4080 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4081 for (temp = NEXT_INSN (i2);
4082 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
4083 || BB_HEAD (this_basic_block) != temp);
4084 temp = NEXT_INSN (temp))
4085 if (temp != i3 && INSN_P (temp))
4086 FOR_EACH_LOG_LINK (link, temp)
4087 if (link->insn == i2)
4088 link->insn = i3;
4089
4090 if (i3notes)
4091 {
4092 rtx link = i3notes;
4093 while (XEXP (link, 1))
4094 link = XEXP (link, 1);
4095 XEXP (link, 1) = i2notes;
4096 }
4097 else
4098 i3notes = i2notes;
4099 i2notes = 0;
4100 }
4101
4102 LOG_LINKS (i3) = NULL;
4103 REG_NOTES (i3) = 0;
4104 LOG_LINKS (i2) = NULL;
4105 REG_NOTES (i2) = 0;
4106
4107 if (newi2pat)
4108 {
4109 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4110 propagate_for_debug (i2, i3, i2dest, i2src);
4111 INSN_CODE (i2) = i2_code_number;
4112 PATTERN (i2) = newi2pat;
4113 }
4114 else
4115 {
4116 if (MAY_HAVE_DEBUG_INSNS && i2src)
4117 propagate_for_debug (i2, i3, i2dest, i2src);
4118 SET_INSN_DELETED (i2);
4119 }
4120
4121 if (i1)
4122 {
4123 LOG_LINKS (i1) = NULL;
4124 REG_NOTES (i1) = 0;
4125 if (MAY_HAVE_DEBUG_INSNS)
4126 propagate_for_debug (i1, i3, i1dest, i1src);
4127 SET_INSN_DELETED (i1);
4128 }
4129
4130 if (i0)
4131 {
4132 LOG_LINKS (i0) = NULL;
4133 REG_NOTES (i0) = 0;
4134 if (MAY_HAVE_DEBUG_INSNS)
4135 propagate_for_debug (i0, i3, i0dest, i0src);
4136 SET_INSN_DELETED (i0);
4137 }
4138
4139 /* Get death notes for everything that is now used in either I3 or
4140 I2 and used to die in a previous insn. If we built two new
4141 patterns, move from I1 to I2 then I2 to I3 so that we get the
4142 proper movement on registers that I2 modifies. */
4143
4144 if (i0)
4145 from_luid = DF_INSN_LUID (i0);
4146 else if (i1)
4147 from_luid = DF_INSN_LUID (i1);
4148 else
4149 from_luid = DF_INSN_LUID (i2);
4150 if (newi2pat)
4151 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4152 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4153
4154 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4155 if (i3notes)
4156 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
4157 elim_i2, elim_i1, elim_i0);
4158 if (i2notes)
4159 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
4160 elim_i2, elim_i1, elim_i0);
4161 if (i1notes)
4162 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
4163 elim_i2, elim_i1, elim_i0);
4164 if (i0notes)
4165 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL_RTX,
4166 elim_i2, elim_i1, elim_i0);
4167 if (midnotes)
4168 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4169 elim_i2, elim_i1, elim_i0);
4170
4171 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4172 know these are REG_UNUSED and want them to go to the desired insn,
4173 so we always pass it as i3. */
4174
4175 if (newi2pat && new_i2_notes)
4176 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX,
4177 NULL_RTX);
4178
4179 if (new_i3_notes)
4180 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX,
4181 NULL_RTX);
4182
4183 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4184 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4185 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4186 in that case, it might delete I2. Similarly for I2 and I1.
4187 Show an additional death due to the REG_DEAD note we make here. If
4188 we discard it in distribute_notes, we will decrement it again. */
4189
4190 if (i3dest_killed)
4191 {
4192 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4193 distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
4194 NULL_RTX),
4195 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1, elim_i0);
4196 else
4197 distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
4198 NULL_RTX),
4199 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4200 elim_i2, elim_i1, elim_i0);
4201 }
4202
4203 if (i2dest_in_i2src)
4204 {
4205 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4206 if (newi2pat && reg_set_p (i2dest, newi2pat))
4207 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4208 NULL_RTX, NULL_RTX);
4209 else
4210 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4211 NULL_RTX, NULL_RTX, NULL_RTX);
4212 }
4213
4214 if (i1dest_in_i1src)
4215 {
4216 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4217 if (newi2pat && reg_set_p (i1dest, newi2pat))
4218 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4219 NULL_RTX, NULL_RTX);
4220 else
4221 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4222 NULL_RTX, NULL_RTX, NULL_RTX);
4223 }
4224
4225 if (i0dest_in_i0src)
4226 {
4227 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4228 if (newi2pat && reg_set_p (i0dest, newi2pat))
4229 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4230 NULL_RTX, NULL_RTX);
4231 else
4232 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4233 NULL_RTX, NULL_RTX, NULL_RTX);
4234 }
4235
4236 distribute_links (i3links);
4237 distribute_links (i2links);
4238 distribute_links (i1links);
4239 distribute_links (i0links);
4240
4241 if (REG_P (i2dest))
4242 {
4243 struct insn_link *link;
4244 rtx i2_insn = 0, i2_val = 0, set;
4245
4246 /* The insn that used to set this register doesn't exist, and
4247 this life of the register may not exist either. See if one of
4248 I3's links points to an insn that sets I2DEST. If it does,
4249 that is now the last known value for I2DEST. If we don't update
4250 this and I2 set the register to a value that depended on its old
4251 contents, we will get confused. If this insn is used, thing
4252 will be set correctly in combine_instructions. */
4253 FOR_EACH_LOG_LINK (link, i3)
4254 if ((set = single_set (link->insn)) != 0
4255 && rtx_equal_p (i2dest, SET_DEST (set)))
4256 i2_insn = link->insn, i2_val = SET_SRC (set);
4257
4258 record_value_for_reg (i2dest, i2_insn, i2_val);
4259
4260 /* If the reg formerly set in I2 died only once and that was in I3,
4261 zero its use count so it won't make `reload' do any work. */
4262 if (! added_sets_2
4263 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4264 && ! i2dest_in_i2src)
4265 INC_REG_N_SETS (REGNO (i2dest), -1);
4266 }
4267
4268 if (i1 && REG_P (i1dest))
4269 {
4270 struct insn_link *link;
4271 rtx i1_insn = 0, i1_val = 0, set;
4272
4273 FOR_EACH_LOG_LINK (link, i3)
4274 if ((set = single_set (link->insn)) != 0
4275 && rtx_equal_p (i1dest, SET_DEST (set)))
4276 i1_insn = link->insn, i1_val = SET_SRC (set);
4277
4278 record_value_for_reg (i1dest, i1_insn, i1_val);
4279
4280 if (! added_sets_1 && ! i1dest_in_i1src)
4281 INC_REG_N_SETS (REGNO (i1dest), -1);
4282 }
4283
4284 if (i0 && REG_P (i0dest))
4285 {
4286 struct insn_link *link;
4287 rtx i0_insn = 0, i0_val = 0, set;
4288
4289 FOR_EACH_LOG_LINK (link, i3)
4290 if ((set = single_set (link->insn)) != 0
4291 && rtx_equal_p (i0dest, SET_DEST (set)))
4292 i0_insn = link->insn, i0_val = SET_SRC (set);
4293
4294 record_value_for_reg (i0dest, i0_insn, i0_val);
4295
4296 if (! added_sets_0 && ! i0dest_in_i0src)
4297 INC_REG_N_SETS (REGNO (i0dest), -1);
4298 }
4299
4300 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4301 been made to this insn. The order of
4302 set_nonzero_bits_and_sign_copies() is important. Because newi2pat
4303 can affect nonzero_bits of newpat */
4304 if (newi2pat)
4305 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4306 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4307 }
4308
4309 if (undobuf.other_insn != NULL_RTX)
4310 {
4311 if (dump_file)
4312 {
4313 fprintf (dump_file, "modifying other_insn ");
4314 dump_insn_slim (dump_file, undobuf.other_insn);
4315 }
4316 df_insn_rescan (undobuf.other_insn);
4317 }
4318
4319 if (i0 && !(NOTE_P(i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4320 {
4321 if (dump_file)
4322 {
4323 fprintf (dump_file, "modifying insn i1 ");
4324 dump_insn_slim (dump_file, i0);
4325 }
4326 df_insn_rescan (i0);
4327 }
4328
4329 if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4330 {
4331 if (dump_file)
4332 {
4333 fprintf (dump_file, "modifying insn i1 ");
4334 dump_insn_slim (dump_file, i1);
4335 }
4336 df_insn_rescan (i1);
4337 }
4338
4339 if (i2 && !(NOTE_P(i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4340 {
4341 if (dump_file)
4342 {
4343 fprintf (dump_file, "modifying insn i2 ");
4344 dump_insn_slim (dump_file, i2);
4345 }
4346 df_insn_rescan (i2);
4347 }
4348
4349 if (i3 && !(NOTE_P(i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4350 {
4351 if (dump_file)
4352 {
4353 fprintf (dump_file, "modifying insn i3 ");
4354 dump_insn_slim (dump_file, i3);
4355 }
4356 df_insn_rescan (i3);
4357 }
4358
4359 /* Set new_direct_jump_p if a new return or simple jump instruction
4360 has been created. Adjust the CFG accordingly. */
4361
4362 if (returnjump_p (i3) || any_uncondjump_p (i3))
4363 {
4364 *new_direct_jump_p = 1;
4365 mark_jump_label (PATTERN (i3), i3, 0);
4366 update_cfg_for_uncondjump (i3);
4367 }
4368
4369 if (undobuf.other_insn != NULL_RTX
4370 && (returnjump_p (undobuf.other_insn)
4371 || any_uncondjump_p (undobuf.other_insn)))
4372 {
4373 *new_direct_jump_p = 1;
4374 update_cfg_for_uncondjump (undobuf.other_insn);
4375 }
4376
4377 /* A noop might also need cleaning up of CFG, if it comes from the
4378 simplification of a jump. */
4379 if (GET_CODE (newpat) == SET
4380 && SET_SRC (newpat) == pc_rtx
4381 && SET_DEST (newpat) == pc_rtx)
4382 {
4383 *new_direct_jump_p = 1;
4384 update_cfg_for_uncondjump (i3);
4385 }
4386
4387 if (undobuf.other_insn != NULL_RTX
4388 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4389 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4390 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4391 {
4392 *new_direct_jump_p = 1;
4393 update_cfg_for_uncondjump (undobuf.other_insn);
4394 }
4395
4396 combine_successes++;
4397 undo_commit ();
4398
4399 if (added_links_insn
4400 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4401 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4402 return added_links_insn;
4403 else
4404 return newi2pat ? i2 : i3;
4405 }
4406 \f
4407 /* Undo all the modifications recorded in undobuf. */
4408
4409 static void
4410 undo_all (void)
4411 {
4412 struct undo *undo, *next;
4413
4414 for (undo = undobuf.undos; undo; undo = next)
4415 {
4416 next = undo->next;
4417 switch (undo->kind)
4418 {
4419 case UNDO_RTX:
4420 *undo->where.r = undo->old_contents.r;
4421 break;
4422 case UNDO_INT:
4423 *undo->where.i = undo->old_contents.i;
4424 break;
4425 case UNDO_MODE:
4426 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4427 break;
4428 default:
4429 gcc_unreachable ();
4430 }
4431
4432 undo->next = undobuf.frees;
4433 undobuf.frees = undo;
4434 }
4435
4436 undobuf.undos = 0;
4437 }
4438
4439 /* We've committed to accepting the changes we made. Move all
4440 of the undos to the free list. */
4441
4442 static void
4443 undo_commit (void)
4444 {
4445 struct undo *undo, *next;
4446
4447 for (undo = undobuf.undos; undo; undo = next)
4448 {
4449 next = undo->next;
4450 undo->next = undobuf.frees;
4451 undobuf.frees = undo;
4452 }
4453 undobuf.undos = 0;
4454 }
4455 \f
4456 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4457 where we have an arithmetic expression and return that point. LOC will
4458 be inside INSN.
4459
4460 try_combine will call this function to see if an insn can be split into
4461 two insns. */
4462
4463 static rtx *
4464 find_split_point (rtx *loc, rtx insn, bool set_src)
4465 {
4466 rtx x = *loc;
4467 enum rtx_code code = GET_CODE (x);
4468 rtx *split;
4469 unsigned HOST_WIDE_INT len = 0;
4470 HOST_WIDE_INT pos = 0;
4471 int unsignedp = 0;
4472 rtx inner = NULL_RTX;
4473
4474 /* First special-case some codes. */
4475 switch (code)
4476 {
4477 case SUBREG:
4478 #ifdef INSN_SCHEDULING
4479 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4480 point. */
4481 if (MEM_P (SUBREG_REG (x)))
4482 return loc;
4483 #endif
4484 return find_split_point (&SUBREG_REG (x), insn, false);
4485
4486 case MEM:
4487 #ifdef HAVE_lo_sum
4488 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4489 using LO_SUM and HIGH. */
4490 if (GET_CODE (XEXP (x, 0)) == CONST
4491 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4492 {
4493 enum machine_mode address_mode
4494 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (x));
4495
4496 SUBST (XEXP (x, 0),
4497 gen_rtx_LO_SUM (address_mode,
4498 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4499 XEXP (x, 0)));
4500 return &XEXP (XEXP (x, 0), 0);
4501 }
4502 #endif
4503
4504 /* If we have a PLUS whose second operand is a constant and the
4505 address is not valid, perhaps will can split it up using
4506 the machine-specific way to split large constants. We use
4507 the first pseudo-reg (one of the virtual regs) as a placeholder;
4508 it will not remain in the result. */
4509 if (GET_CODE (XEXP (x, 0)) == PLUS
4510 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4511 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4512 MEM_ADDR_SPACE (x)))
4513 {
4514 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4515 rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4516 XEXP (x, 0)),
4517 subst_insn);
4518
4519 /* This should have produced two insns, each of which sets our
4520 placeholder. If the source of the second is a valid address,
4521 we can make put both sources together and make a split point
4522 in the middle. */
4523
4524 if (seq
4525 && NEXT_INSN (seq) != NULL_RTX
4526 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4527 && NONJUMP_INSN_P (seq)
4528 && GET_CODE (PATTERN (seq)) == SET
4529 && SET_DEST (PATTERN (seq)) == reg
4530 && ! reg_mentioned_p (reg,
4531 SET_SRC (PATTERN (seq)))
4532 && NONJUMP_INSN_P (NEXT_INSN (seq))
4533 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4534 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4535 && memory_address_addr_space_p
4536 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4537 MEM_ADDR_SPACE (x)))
4538 {
4539 rtx src1 = SET_SRC (PATTERN (seq));
4540 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4541
4542 /* Replace the placeholder in SRC2 with SRC1. If we can
4543 find where in SRC2 it was placed, that can become our
4544 split point and we can replace this address with SRC2.
4545 Just try two obvious places. */
4546
4547 src2 = replace_rtx (src2, reg, src1);
4548 split = 0;
4549 if (XEXP (src2, 0) == src1)
4550 split = &XEXP (src2, 0);
4551 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4552 && XEXP (XEXP (src2, 0), 0) == src1)
4553 split = &XEXP (XEXP (src2, 0), 0);
4554
4555 if (split)
4556 {
4557 SUBST (XEXP (x, 0), src2);
4558 return split;
4559 }
4560 }
4561
4562 /* If that didn't work, perhaps the first operand is complex and
4563 needs to be computed separately, so make a split point there.
4564 This will occur on machines that just support REG + CONST
4565 and have a constant moved through some previous computation. */
4566
4567 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4568 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4569 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4570 return &XEXP (XEXP (x, 0), 0);
4571 }
4572
4573 /* If we have a PLUS whose first operand is complex, try computing it
4574 separately by making a split there. */
4575 if (GET_CODE (XEXP (x, 0)) == PLUS
4576 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4577 MEM_ADDR_SPACE (x))
4578 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4579 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4580 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4581 return &XEXP (XEXP (x, 0), 0);
4582 break;
4583
4584 case SET:
4585 #ifdef HAVE_cc0
4586 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4587 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4588 we need to put the operand into a register. So split at that
4589 point. */
4590
4591 if (SET_DEST (x) == cc0_rtx
4592 && GET_CODE (SET_SRC (x)) != COMPARE
4593 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4594 && !OBJECT_P (SET_SRC (x))
4595 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4596 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4597 return &SET_SRC (x);
4598 #endif
4599
4600 /* See if we can split SET_SRC as it stands. */
4601 split = find_split_point (&SET_SRC (x), insn, true);
4602 if (split && split != &SET_SRC (x))
4603 return split;
4604
4605 /* See if we can split SET_DEST as it stands. */
4606 split = find_split_point (&SET_DEST (x), insn, false);
4607 if (split && split != &SET_DEST (x))
4608 return split;
4609
4610 /* See if this is a bitfield assignment with everything constant. If
4611 so, this is an IOR of an AND, so split it into that. */
4612 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4613 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
4614 <= HOST_BITS_PER_WIDE_INT)
4615 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4616 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4617 && CONST_INT_P (SET_SRC (x))
4618 && ((INTVAL (XEXP (SET_DEST (x), 1))
4619 + INTVAL (XEXP (SET_DEST (x), 2)))
4620 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
4621 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4622 {
4623 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4624 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4625 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4626 rtx dest = XEXP (SET_DEST (x), 0);
4627 enum machine_mode mode = GET_MODE (dest);
4628 unsigned HOST_WIDE_INT mask
4629 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4630 rtx or_mask;
4631
4632 if (BITS_BIG_ENDIAN)
4633 pos = GET_MODE_BITSIZE (mode) - len - pos;
4634
4635 or_mask = gen_int_mode (src << pos, mode);
4636 if (src == mask)
4637 SUBST (SET_SRC (x),
4638 simplify_gen_binary (IOR, mode, dest, or_mask));
4639 else
4640 {
4641 rtx negmask = gen_int_mode (~(mask << pos), mode);
4642 SUBST (SET_SRC (x),
4643 simplify_gen_binary (IOR, mode,
4644 simplify_gen_binary (AND, mode,
4645 dest, negmask),
4646 or_mask));
4647 }
4648
4649 SUBST (SET_DEST (x), dest);
4650
4651 split = find_split_point (&SET_SRC (x), insn, true);
4652 if (split && split != &SET_SRC (x))
4653 return split;
4654 }
4655
4656 /* Otherwise, see if this is an operation that we can split into two.
4657 If so, try to split that. */
4658 code = GET_CODE (SET_SRC (x));
4659
4660 switch (code)
4661 {
4662 case AND:
4663 /* If we are AND'ing with a large constant that is only a single
4664 bit and the result is only being used in a context where we
4665 need to know if it is zero or nonzero, replace it with a bit
4666 extraction. This will avoid the large constant, which might
4667 have taken more than one insn to make. If the constant were
4668 not a valid argument to the AND but took only one insn to make,
4669 this is no worse, but if it took more than one insn, it will
4670 be better. */
4671
4672 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4673 && REG_P (XEXP (SET_SRC (x), 0))
4674 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4675 && REG_P (SET_DEST (x))
4676 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4677 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4678 && XEXP (*split, 0) == SET_DEST (x)
4679 && XEXP (*split, 1) == const0_rtx)
4680 {
4681 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4682 XEXP (SET_SRC (x), 0),
4683 pos, NULL_RTX, 1, 1, 0, 0);
4684 if (extraction != 0)
4685 {
4686 SUBST (SET_SRC (x), extraction);
4687 return find_split_point (loc, insn, false);
4688 }
4689 }
4690 break;
4691
4692 case NE:
4693 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4694 is known to be on, this can be converted into a NEG of a shift. */
4695 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4696 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4697 && 1 <= (pos = exact_log2
4698 (nonzero_bits (XEXP (SET_SRC (x), 0),
4699 GET_MODE (XEXP (SET_SRC (x), 0))))))
4700 {
4701 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4702
4703 SUBST (SET_SRC (x),
4704 gen_rtx_NEG (mode,
4705 gen_rtx_LSHIFTRT (mode,
4706 XEXP (SET_SRC (x), 0),
4707 GEN_INT (pos))));
4708
4709 split = find_split_point (&SET_SRC (x), insn, true);
4710 if (split && split != &SET_SRC (x))
4711 return split;
4712 }
4713 break;
4714
4715 case SIGN_EXTEND:
4716 inner = XEXP (SET_SRC (x), 0);
4717
4718 /* We can't optimize if either mode is a partial integer
4719 mode as we don't know how many bits are significant
4720 in those modes. */
4721 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4722 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4723 break;
4724
4725 pos = 0;
4726 len = GET_MODE_BITSIZE (GET_MODE (inner));
4727 unsignedp = 0;
4728 break;
4729
4730 case SIGN_EXTRACT:
4731 case ZERO_EXTRACT:
4732 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4733 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4734 {
4735 inner = XEXP (SET_SRC (x), 0);
4736 len = INTVAL (XEXP (SET_SRC (x), 1));
4737 pos = INTVAL (XEXP (SET_SRC (x), 2));
4738
4739 if (BITS_BIG_ENDIAN)
4740 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
4741 unsignedp = (code == ZERO_EXTRACT);
4742 }
4743 break;
4744
4745 default:
4746 break;
4747 }
4748
4749 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
4750 {
4751 enum machine_mode mode = GET_MODE (SET_SRC (x));
4752
4753 /* For unsigned, we have a choice of a shift followed by an
4754 AND or two shifts. Use two shifts for field sizes where the
4755 constant might be too large. We assume here that we can
4756 always at least get 8-bit constants in an AND insn, which is
4757 true for every current RISC. */
4758
4759 if (unsignedp && len <= 8)
4760 {
4761 SUBST (SET_SRC (x),
4762 gen_rtx_AND (mode,
4763 gen_rtx_LSHIFTRT
4764 (mode, gen_lowpart (mode, inner),
4765 GEN_INT (pos)),
4766 GEN_INT (((unsigned HOST_WIDE_INT) 1 << len)
4767 - 1)));
4768
4769 split = find_split_point (&SET_SRC (x), insn, true);
4770 if (split && split != &SET_SRC (x))
4771 return split;
4772 }
4773 else
4774 {
4775 SUBST (SET_SRC (x),
4776 gen_rtx_fmt_ee
4777 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4778 gen_rtx_ASHIFT (mode,
4779 gen_lowpart (mode, inner),
4780 GEN_INT (GET_MODE_BITSIZE (mode)
4781 - len - pos)),
4782 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
4783
4784 split = find_split_point (&SET_SRC (x), insn, true);
4785 if (split && split != &SET_SRC (x))
4786 return split;
4787 }
4788 }
4789
4790 /* See if this is a simple operation with a constant as the second
4791 operand. It might be that this constant is out of range and hence
4792 could be used as a split point. */
4793 if (BINARY_P (SET_SRC (x))
4794 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4795 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4796 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4797 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4798 return &XEXP (SET_SRC (x), 1);
4799
4800 /* Finally, see if this is a simple operation with its first operand
4801 not in a register. The operation might require this operand in a
4802 register, so return it as a split point. We can always do this
4803 because if the first operand were another operation, we would have
4804 already found it as a split point. */
4805 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4806 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4807 return &XEXP (SET_SRC (x), 0);
4808
4809 return 0;
4810
4811 case AND:
4812 case IOR:
4813 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4814 it is better to write this as (not (ior A B)) so we can split it.
4815 Similarly for IOR. */
4816 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4817 {
4818 SUBST (*loc,
4819 gen_rtx_NOT (GET_MODE (x),
4820 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4821 GET_MODE (x),
4822 XEXP (XEXP (x, 0), 0),
4823 XEXP (XEXP (x, 1), 0))));
4824 return find_split_point (loc, insn, set_src);
4825 }
4826
4827 /* Many RISC machines have a large set of logical insns. If the
4828 second operand is a NOT, put it first so we will try to split the
4829 other operand first. */
4830 if (GET_CODE (XEXP (x, 1)) == NOT)
4831 {
4832 rtx tem = XEXP (x, 0);
4833 SUBST (XEXP (x, 0), XEXP (x, 1));
4834 SUBST (XEXP (x, 1), tem);
4835 }
4836 break;
4837
4838 case PLUS:
4839 case MINUS:
4840 /* Canonicalization can produce (minus A (mult B C)), where C is a
4841 constant. It may be better to try splitting (plus (mult B -C) A)
4842 instead if this isn't a multiply by a power of two. */
4843 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
4844 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4845 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
4846 {
4847 enum machine_mode mode = GET_MODE (x);
4848 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
4849 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
4850 SUBST (*loc, gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
4851 XEXP (XEXP (x, 1), 0),
4852 GEN_INT (other_int)),
4853 XEXP (x, 0)));
4854 return find_split_point (loc, insn, set_src);
4855 }
4856
4857 /* Split at a multiply-accumulate instruction. However if this is
4858 the SET_SRC, we likely do not have such an instruction and it's
4859 worthless to try this split. */
4860 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4861 return loc;
4862
4863 default:
4864 break;
4865 }
4866
4867 /* Otherwise, select our actions depending on our rtx class. */
4868 switch (GET_RTX_CLASS (code))
4869 {
4870 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
4871 case RTX_TERNARY:
4872 split = find_split_point (&XEXP (x, 2), insn, false);
4873 if (split)
4874 return split;
4875 /* ... fall through ... */
4876 case RTX_BIN_ARITH:
4877 case RTX_COMM_ARITH:
4878 case RTX_COMPARE:
4879 case RTX_COMM_COMPARE:
4880 split = find_split_point (&XEXP (x, 1), insn, false);
4881 if (split)
4882 return split;
4883 /* ... fall through ... */
4884 case RTX_UNARY:
4885 /* Some machines have (and (shift ...) ...) insns. If X is not
4886 an AND, but XEXP (X, 0) is, use it as our split point. */
4887 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4888 return &XEXP (x, 0);
4889
4890 split = find_split_point (&XEXP (x, 0), insn, false);
4891 if (split)
4892 return split;
4893 return loc;
4894
4895 default:
4896 /* Otherwise, we don't have a split point. */
4897 return 0;
4898 }
4899 }
4900 \f
4901 /* Throughout X, replace FROM with TO, and return the result.
4902 The result is TO if X is FROM;
4903 otherwise the result is X, but its contents may have been modified.
4904 If they were modified, a record was made in undobuf so that
4905 undo_all will (among other things) return X to its original state.
4906
4907 If the number of changes necessary is too much to record to undo,
4908 the excess changes are not made, so the result is invalid.
4909 The changes already made can still be undone.
4910 undobuf.num_undo is incremented for such changes, so by testing that
4911 the caller can tell whether the result is valid.
4912
4913 `n_occurrences' is incremented each time FROM is replaced.
4914
4915 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4916
4917 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
4918 by copying if `n_occurrences' is nonzero. */
4919
4920 static rtx
4921 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
4922 {
4923 enum rtx_code code = GET_CODE (x);
4924 enum machine_mode op0_mode = VOIDmode;
4925 const char *fmt;
4926 int len, i;
4927 rtx new_rtx;
4928
4929 /* Two expressions are equal if they are identical copies of a shared
4930 RTX or if they are both registers with the same register number
4931 and mode. */
4932
4933 #define COMBINE_RTX_EQUAL_P(X,Y) \
4934 ((X) == (Y) \
4935 || (REG_P (X) && REG_P (Y) \
4936 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4937
4938 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4939 {
4940 n_occurrences++;
4941 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4942 }
4943
4944 /* If X and FROM are the same register but different modes, they
4945 will not have been seen as equal above. However, the log links code
4946 will make a LOG_LINKS entry for that case. If we do nothing, we
4947 will try to rerecognize our original insn and, when it succeeds,
4948 we will delete the feeding insn, which is incorrect.
4949
4950 So force this insn not to match in this (rare) case. */
4951 if (! in_dest && code == REG && REG_P (from)
4952 && reg_overlap_mentioned_p (x, from))
4953 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4954
4955 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4956 of which may contain things that can be combined. */
4957 if (code != MEM && code != LO_SUM && OBJECT_P (x))
4958 return x;
4959
4960 /* It is possible to have a subexpression appear twice in the insn.
4961 Suppose that FROM is a register that appears within TO.
4962 Then, after that subexpression has been scanned once by `subst',
4963 the second time it is scanned, TO may be found. If we were
4964 to scan TO here, we would find FROM within it and create a
4965 self-referent rtl structure which is completely wrong. */
4966 if (COMBINE_RTX_EQUAL_P (x, to))
4967 return to;
4968
4969 /* Parallel asm_operands need special attention because all of the
4970 inputs are shared across the arms. Furthermore, unsharing the
4971 rtl results in recognition failures. Failure to handle this case
4972 specially can result in circular rtl.
4973
4974 Solve this by doing a normal pass across the first entry of the
4975 parallel, and only processing the SET_DESTs of the subsequent
4976 entries. Ug. */
4977
4978 if (code == PARALLEL
4979 && GET_CODE (XVECEXP (x, 0, 0)) == SET
4980 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
4981 {
4982 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
4983
4984 /* If this substitution failed, this whole thing fails. */
4985 if (GET_CODE (new_rtx) == CLOBBER
4986 && XEXP (new_rtx, 0) == const0_rtx)
4987 return new_rtx;
4988
4989 SUBST (XVECEXP (x, 0, 0), new_rtx);
4990
4991 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
4992 {
4993 rtx dest = SET_DEST (XVECEXP (x, 0, i));
4994
4995 if (!REG_P (dest)
4996 && GET_CODE (dest) != CC0
4997 && GET_CODE (dest) != PC)
4998 {
4999 new_rtx = subst (dest, from, to, 0, unique_copy);
5000
5001 /* If this substitution failed, this whole thing fails. */
5002 if (GET_CODE (new_rtx) == CLOBBER
5003 && XEXP (new_rtx, 0) == const0_rtx)
5004 return new_rtx;
5005
5006 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5007 }
5008 }
5009 }
5010 else
5011 {
5012 len = GET_RTX_LENGTH (code);
5013 fmt = GET_RTX_FORMAT (code);
5014
5015 /* We don't need to process a SET_DEST that is a register, CC0,
5016 or PC, so set up to skip this common case. All other cases
5017 where we want to suppress replacing something inside a
5018 SET_SRC are handled via the IN_DEST operand. */
5019 if (code == SET
5020 && (REG_P (SET_DEST (x))
5021 || GET_CODE (SET_DEST (x)) == CC0
5022 || GET_CODE (SET_DEST (x)) == PC))
5023 fmt = "ie";
5024
5025 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5026 constant. */
5027 if (fmt[0] == 'e')
5028 op0_mode = GET_MODE (XEXP (x, 0));
5029
5030 for (i = 0; i < len; i++)
5031 {
5032 if (fmt[i] == 'E')
5033 {
5034 int j;
5035 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5036 {
5037 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5038 {
5039 new_rtx = (unique_copy && n_occurrences
5040 ? copy_rtx (to) : to);
5041 n_occurrences++;
5042 }
5043 else
5044 {
5045 new_rtx = subst (XVECEXP (x, i, j), from, to, 0,
5046 unique_copy);
5047
5048 /* If this substitution failed, this whole thing
5049 fails. */
5050 if (GET_CODE (new_rtx) == CLOBBER
5051 && XEXP (new_rtx, 0) == const0_rtx)
5052 return new_rtx;
5053 }
5054
5055 SUBST (XVECEXP (x, i, j), new_rtx);
5056 }
5057 }
5058 else if (fmt[i] == 'e')
5059 {
5060 /* If this is a register being set, ignore it. */
5061 new_rtx = XEXP (x, i);
5062 if (in_dest
5063 && i == 0
5064 && (((code == SUBREG || code == ZERO_EXTRACT)
5065 && REG_P (new_rtx))
5066 || code == STRICT_LOW_PART))
5067 ;
5068
5069 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5070 {
5071 /* In general, don't install a subreg involving two
5072 modes not tieable. It can worsen register
5073 allocation, and can even make invalid reload
5074 insns, since the reg inside may need to be copied
5075 from in the outside mode, and that may be invalid
5076 if it is an fp reg copied in integer mode.
5077
5078 We allow two exceptions to this: It is valid if
5079 it is inside another SUBREG and the mode of that
5080 SUBREG and the mode of the inside of TO is
5081 tieable and it is valid if X is a SET that copies
5082 FROM to CC0. */
5083
5084 if (GET_CODE (to) == SUBREG
5085 && ! MODES_TIEABLE_P (GET_MODE (to),
5086 GET_MODE (SUBREG_REG (to)))
5087 && ! (code == SUBREG
5088 && MODES_TIEABLE_P (GET_MODE (x),
5089 GET_MODE (SUBREG_REG (to))))
5090 #ifdef HAVE_cc0
5091 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5092 #endif
5093 )
5094 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5095
5096 #ifdef CANNOT_CHANGE_MODE_CLASS
5097 if (code == SUBREG
5098 && REG_P (to)
5099 && REGNO (to) < FIRST_PSEUDO_REGISTER
5100 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
5101 GET_MODE (to),
5102 GET_MODE (x)))
5103 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5104 #endif
5105
5106 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5107 n_occurrences++;
5108 }
5109 else
5110 /* If we are in a SET_DEST, suppress most cases unless we
5111 have gone inside a MEM, in which case we want to
5112 simplify the address. We assume here that things that
5113 are actually part of the destination have their inner
5114 parts in the first expression. This is true for SUBREG,
5115 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5116 things aside from REG and MEM that should appear in a
5117 SET_DEST. */
5118 new_rtx = subst (XEXP (x, i), from, to,
5119 (((in_dest
5120 && (code == SUBREG || code == STRICT_LOW_PART
5121 || code == ZERO_EXTRACT))
5122 || code == SET)
5123 && i == 0), unique_copy);
5124
5125 /* If we found that we will have to reject this combination,
5126 indicate that by returning the CLOBBER ourselves, rather than
5127 an expression containing it. This will speed things up as
5128 well as prevent accidents where two CLOBBERs are considered
5129 to be equal, thus producing an incorrect simplification. */
5130
5131 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5132 return new_rtx;
5133
5134 if (GET_CODE (x) == SUBREG
5135 && (CONST_INT_P (new_rtx)
5136 || GET_CODE (new_rtx) == CONST_DOUBLE))
5137 {
5138 enum machine_mode mode = GET_MODE (x);
5139
5140 x = simplify_subreg (GET_MODE (x), new_rtx,
5141 GET_MODE (SUBREG_REG (x)),
5142 SUBREG_BYTE (x));
5143 if (! x)
5144 x = gen_rtx_CLOBBER (mode, const0_rtx);
5145 }
5146 else if (CONST_INT_P (new_rtx)
5147 && GET_CODE (x) == ZERO_EXTEND)
5148 {
5149 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5150 new_rtx, GET_MODE (XEXP (x, 0)));
5151 gcc_assert (x);
5152 }
5153 else
5154 SUBST (XEXP (x, i), new_rtx);
5155 }
5156 }
5157 }
5158
5159 /* Check if we are loading something from the constant pool via float
5160 extension; in this case we would undo compress_float_constant
5161 optimization and degenerate constant load to an immediate value. */
5162 if (GET_CODE (x) == FLOAT_EXTEND
5163 && MEM_P (XEXP (x, 0))
5164 && MEM_READONLY_P (XEXP (x, 0)))
5165 {
5166 rtx tmp = avoid_constant_pool_reference (x);
5167 if (x != tmp)
5168 return x;
5169 }
5170
5171 /* Try to simplify X. If the simplification changed the code, it is likely
5172 that further simplification will help, so loop, but limit the number
5173 of repetitions that will be performed. */
5174
5175 for (i = 0; i < 4; i++)
5176 {
5177 /* If X is sufficiently simple, don't bother trying to do anything
5178 with it. */
5179 if (code != CONST_INT && code != REG && code != CLOBBER)
5180 x = combine_simplify_rtx (x, op0_mode, in_dest);
5181
5182 if (GET_CODE (x) == code)
5183 break;
5184
5185 code = GET_CODE (x);
5186
5187 /* We no longer know the original mode of operand 0 since we
5188 have changed the form of X) */
5189 op0_mode = VOIDmode;
5190 }
5191
5192 return x;
5193 }
5194 \f
5195 /* Simplify X, a piece of RTL. We just operate on the expression at the
5196 outer level; call `subst' to simplify recursively. Return the new
5197 expression.
5198
5199 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5200 if we are inside a SET_DEST. */
5201
5202 static rtx
5203 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
5204 {
5205 enum rtx_code code = GET_CODE (x);
5206 enum machine_mode mode = GET_MODE (x);
5207 rtx temp;
5208 int i;
5209
5210 /* If this is a commutative operation, put a constant last and a complex
5211 expression first. We don't need to do this for comparisons here. */
5212 if (COMMUTATIVE_ARITH_P (x)
5213 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5214 {
5215 temp = XEXP (x, 0);
5216 SUBST (XEXP (x, 0), XEXP (x, 1));
5217 SUBST (XEXP (x, 1), temp);
5218 }
5219
5220 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5221 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5222 things. Check for cases where both arms are testing the same
5223 condition.
5224
5225 Don't do anything if all operands are very simple. */
5226
5227 if ((BINARY_P (x)
5228 && ((!OBJECT_P (XEXP (x, 0))
5229 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5230 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5231 || (!OBJECT_P (XEXP (x, 1))
5232 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5233 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5234 || (UNARY_P (x)
5235 && (!OBJECT_P (XEXP (x, 0))
5236 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5237 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5238 {
5239 rtx cond, true_rtx, false_rtx;
5240
5241 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5242 if (cond != 0
5243 /* If everything is a comparison, what we have is highly unlikely
5244 to be simpler, so don't use it. */
5245 && ! (COMPARISON_P (x)
5246 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5247 {
5248 rtx cop1 = const0_rtx;
5249 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5250
5251 if (cond_code == NE && COMPARISON_P (cond))
5252 return x;
5253
5254 /* Simplify the alternative arms; this may collapse the true and
5255 false arms to store-flag values. Be careful to use copy_rtx
5256 here since true_rtx or false_rtx might share RTL with x as a
5257 result of the if_then_else_cond call above. */
5258 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
5259 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
5260
5261 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5262 is unlikely to be simpler. */
5263 if (general_operand (true_rtx, VOIDmode)
5264 && general_operand (false_rtx, VOIDmode))
5265 {
5266 enum rtx_code reversed;
5267
5268 /* Restarting if we generate a store-flag expression will cause
5269 us to loop. Just drop through in this case. */
5270
5271 /* If the result values are STORE_FLAG_VALUE and zero, we can
5272 just make the comparison operation. */
5273 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5274 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5275 cond, cop1);
5276 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5277 && ((reversed = reversed_comparison_code_parts
5278 (cond_code, cond, cop1, NULL))
5279 != UNKNOWN))
5280 x = simplify_gen_relational (reversed, mode, VOIDmode,
5281 cond, cop1);
5282
5283 /* Likewise, we can make the negate of a comparison operation
5284 if the result values are - STORE_FLAG_VALUE and zero. */
5285 else if (CONST_INT_P (true_rtx)
5286 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5287 && false_rtx == const0_rtx)
5288 x = simplify_gen_unary (NEG, mode,
5289 simplify_gen_relational (cond_code,
5290 mode, VOIDmode,
5291 cond, cop1),
5292 mode);
5293 else if (CONST_INT_P (false_rtx)
5294 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5295 && true_rtx == const0_rtx
5296 && ((reversed = reversed_comparison_code_parts
5297 (cond_code, cond, cop1, NULL))
5298 != UNKNOWN))
5299 x = simplify_gen_unary (NEG, mode,
5300 simplify_gen_relational (reversed,
5301 mode, VOIDmode,
5302 cond, cop1),
5303 mode);
5304 else
5305 return gen_rtx_IF_THEN_ELSE (mode,
5306 simplify_gen_relational (cond_code,
5307 mode,
5308 VOIDmode,
5309 cond,
5310 cop1),
5311 true_rtx, false_rtx);
5312
5313 code = GET_CODE (x);
5314 op0_mode = VOIDmode;
5315 }
5316 }
5317 }
5318
5319 /* Try to fold this expression in case we have constants that weren't
5320 present before. */
5321 temp = 0;
5322 switch (GET_RTX_CLASS (code))
5323 {
5324 case RTX_UNARY:
5325 if (op0_mode == VOIDmode)
5326 op0_mode = GET_MODE (XEXP (x, 0));
5327 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5328 break;
5329 case RTX_COMPARE:
5330 case RTX_COMM_COMPARE:
5331 {
5332 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5333 if (cmp_mode == VOIDmode)
5334 {
5335 cmp_mode = GET_MODE (XEXP (x, 1));
5336 if (cmp_mode == VOIDmode)
5337 cmp_mode = op0_mode;
5338 }
5339 temp = simplify_relational_operation (code, mode, cmp_mode,
5340 XEXP (x, 0), XEXP (x, 1));
5341 }
5342 break;
5343 case RTX_COMM_ARITH:
5344 case RTX_BIN_ARITH:
5345 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5346 break;
5347 case RTX_BITFIELD_OPS:
5348 case RTX_TERNARY:
5349 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5350 XEXP (x, 1), XEXP (x, 2));
5351 break;
5352 default:
5353 break;
5354 }
5355
5356 if (temp)
5357 {
5358 x = temp;
5359 code = GET_CODE (temp);
5360 op0_mode = VOIDmode;
5361 mode = GET_MODE (temp);
5362 }
5363
5364 /* First see if we can apply the inverse distributive law. */
5365 if (code == PLUS || code == MINUS
5366 || code == AND || code == IOR || code == XOR)
5367 {
5368 x = apply_distributive_law (x);
5369 code = GET_CODE (x);
5370 op0_mode = VOIDmode;
5371 }
5372
5373 /* If CODE is an associative operation not otherwise handled, see if we
5374 can associate some operands. This can win if they are constants or
5375 if they are logically related (i.e. (a & b) & a). */
5376 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5377 || code == AND || code == IOR || code == XOR
5378 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5379 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5380 || (flag_associative_math && FLOAT_MODE_P (mode))))
5381 {
5382 if (GET_CODE (XEXP (x, 0)) == code)
5383 {
5384 rtx other = XEXP (XEXP (x, 0), 0);
5385 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5386 rtx inner_op1 = XEXP (x, 1);
5387 rtx inner;
5388
5389 /* Make sure we pass the constant operand if any as the second
5390 one if this is a commutative operation. */
5391 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5392 {
5393 rtx tem = inner_op0;
5394 inner_op0 = inner_op1;
5395 inner_op1 = tem;
5396 }
5397 inner = simplify_binary_operation (code == MINUS ? PLUS
5398 : code == DIV ? MULT
5399 : code,
5400 mode, inner_op0, inner_op1);
5401
5402 /* For commutative operations, try the other pair if that one
5403 didn't simplify. */
5404 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5405 {
5406 other = XEXP (XEXP (x, 0), 1);
5407 inner = simplify_binary_operation (code, mode,
5408 XEXP (XEXP (x, 0), 0),
5409 XEXP (x, 1));
5410 }
5411
5412 if (inner)
5413 return simplify_gen_binary (code, mode, other, inner);
5414 }
5415 }
5416
5417 /* A little bit of algebraic simplification here. */
5418 switch (code)
5419 {
5420 case MEM:
5421 /* Ensure that our address has any ASHIFTs converted to MULT in case
5422 address-recognizing predicates are called later. */
5423 temp = make_compound_operation (XEXP (x, 0), MEM);
5424 SUBST (XEXP (x, 0), temp);
5425 break;
5426
5427 case SUBREG:
5428 if (op0_mode == VOIDmode)
5429 op0_mode = GET_MODE (SUBREG_REG (x));
5430
5431 /* See if this can be moved to simplify_subreg. */
5432 if (CONSTANT_P (SUBREG_REG (x))
5433 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5434 /* Don't call gen_lowpart if the inner mode
5435 is VOIDmode and we cannot simplify it, as SUBREG without
5436 inner mode is invalid. */
5437 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5438 || gen_lowpart_common (mode, SUBREG_REG (x))))
5439 return gen_lowpart (mode, SUBREG_REG (x));
5440
5441 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5442 break;
5443 {
5444 rtx temp;
5445 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5446 SUBREG_BYTE (x));
5447 if (temp)
5448 return temp;
5449 }
5450
5451 /* Don't change the mode of the MEM if that would change the meaning
5452 of the address. */
5453 if (MEM_P (SUBREG_REG (x))
5454 && (MEM_VOLATILE_P (SUBREG_REG (x))
5455 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
5456 return gen_rtx_CLOBBER (mode, const0_rtx);
5457
5458 /* Note that we cannot do any narrowing for non-constants since
5459 we might have been counting on using the fact that some bits were
5460 zero. We now do this in the SET. */
5461
5462 break;
5463
5464 case NEG:
5465 temp = expand_compound_operation (XEXP (x, 0));
5466
5467 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5468 replaced by (lshiftrt X C). This will convert
5469 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5470
5471 if (GET_CODE (temp) == ASHIFTRT
5472 && CONST_INT_P (XEXP (temp, 1))
5473 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
5474 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5475 INTVAL (XEXP (temp, 1)));
5476
5477 /* If X has only a single bit that might be nonzero, say, bit I, convert
5478 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5479 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5480 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5481 or a SUBREG of one since we'd be making the expression more
5482 complex if it was just a register. */
5483
5484 if (!REG_P (temp)
5485 && ! (GET_CODE (temp) == SUBREG
5486 && REG_P (SUBREG_REG (temp)))
5487 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5488 {
5489 rtx temp1 = simplify_shift_const
5490 (NULL_RTX, ASHIFTRT, mode,
5491 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5492 GET_MODE_BITSIZE (mode) - 1 - i),
5493 GET_MODE_BITSIZE (mode) - 1 - i);
5494
5495 /* If all we did was surround TEMP with the two shifts, we
5496 haven't improved anything, so don't use it. Otherwise,
5497 we are better off with TEMP1. */
5498 if (GET_CODE (temp1) != ASHIFTRT
5499 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5500 || XEXP (XEXP (temp1, 0), 0) != temp)
5501 return temp1;
5502 }
5503 break;
5504
5505 case TRUNCATE:
5506 /* We can't handle truncation to a partial integer mode here
5507 because we don't know the real bitsize of the partial
5508 integer mode. */
5509 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5510 break;
5511
5512 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5513 SUBST (XEXP (x, 0),
5514 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5515 GET_MODE_MASK (mode), 0));
5516
5517 /* We can truncate a constant value and return it. */
5518 if (CONST_INT_P (XEXP (x, 0)))
5519 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5520
5521 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5522 whose value is a comparison can be replaced with a subreg if
5523 STORE_FLAG_VALUE permits. */
5524 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5525 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5526 && (temp = get_last_value (XEXP (x, 0)))
5527 && COMPARISON_P (temp))
5528 return gen_lowpart (mode, XEXP (x, 0));
5529 break;
5530
5531 case CONST:
5532 /* (const (const X)) can become (const X). Do it this way rather than
5533 returning the inner CONST since CONST can be shared with a
5534 REG_EQUAL note. */
5535 if (GET_CODE (XEXP (x, 0)) == CONST)
5536 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5537 break;
5538
5539 #ifdef HAVE_lo_sum
5540 case LO_SUM:
5541 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5542 can add in an offset. find_split_point will split this address up
5543 again if it doesn't match. */
5544 if (GET_CODE (XEXP (x, 0)) == HIGH
5545 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5546 return XEXP (x, 1);
5547 break;
5548 #endif
5549
5550 case PLUS:
5551 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5552 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5553 bit-field and can be replaced by either a sign_extend or a
5554 sign_extract. The `and' may be a zero_extend and the two
5555 <c>, -<c> constants may be reversed. */
5556 if (GET_CODE (XEXP (x, 0)) == XOR
5557 && CONST_INT_P (XEXP (x, 1))
5558 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5559 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5560 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5561 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5562 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5563 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5564 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5565 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5566 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5567 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5568 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5569 == (unsigned int) i + 1))))
5570 return simplify_shift_const
5571 (NULL_RTX, ASHIFTRT, mode,
5572 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5573 XEXP (XEXP (XEXP (x, 0), 0), 0),
5574 GET_MODE_BITSIZE (mode) - (i + 1)),
5575 GET_MODE_BITSIZE (mode) - (i + 1));
5576
5577 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5578 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5579 the bitsize of the mode - 1. This allows simplification of
5580 "a = (b & 8) == 0;" */
5581 if (XEXP (x, 1) == constm1_rtx
5582 && !REG_P (XEXP (x, 0))
5583 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5584 && REG_P (SUBREG_REG (XEXP (x, 0))))
5585 && nonzero_bits (XEXP (x, 0), mode) == 1)
5586 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5587 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5588 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5589 GET_MODE_BITSIZE (mode) - 1),
5590 GET_MODE_BITSIZE (mode) - 1);
5591
5592 /* If we are adding two things that have no bits in common, convert
5593 the addition into an IOR. This will often be further simplified,
5594 for example in cases like ((a & 1) + (a & 2)), which can
5595 become a & 3. */
5596
5597 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5598 && (nonzero_bits (XEXP (x, 0), mode)
5599 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5600 {
5601 /* Try to simplify the expression further. */
5602 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5603 temp = combine_simplify_rtx (tor, mode, in_dest);
5604
5605 /* If we could, great. If not, do not go ahead with the IOR
5606 replacement, since PLUS appears in many special purpose
5607 address arithmetic instructions. */
5608 if (GET_CODE (temp) != CLOBBER && temp != tor)
5609 return temp;
5610 }
5611 break;
5612
5613 case MINUS:
5614 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5615 (and <foo> (const_int pow2-1)) */
5616 if (GET_CODE (XEXP (x, 1)) == AND
5617 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5618 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5619 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5620 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5621 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5622 break;
5623
5624 case MULT:
5625 /* If we have (mult (plus A B) C), apply the distributive law and then
5626 the inverse distributive law to see if things simplify. This
5627 occurs mostly in addresses, often when unrolling loops. */
5628
5629 if (GET_CODE (XEXP (x, 0)) == PLUS)
5630 {
5631 rtx result = distribute_and_simplify_rtx (x, 0);
5632 if (result)
5633 return result;
5634 }
5635
5636 /* Try simplify a*(b/c) as (a*b)/c. */
5637 if (FLOAT_MODE_P (mode) && flag_associative_math
5638 && GET_CODE (XEXP (x, 0)) == DIV)
5639 {
5640 rtx tem = simplify_binary_operation (MULT, mode,
5641 XEXP (XEXP (x, 0), 0),
5642 XEXP (x, 1));
5643 if (tem)
5644 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5645 }
5646 break;
5647
5648 case UDIV:
5649 /* If this is a divide by a power of two, treat it as a shift if
5650 its first operand is a shift. */
5651 if (CONST_INT_P (XEXP (x, 1))
5652 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5653 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5654 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5655 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5656 || GET_CODE (XEXP (x, 0)) == ROTATE
5657 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5658 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5659 break;
5660
5661 case EQ: case NE:
5662 case GT: case GTU: case GE: case GEU:
5663 case LT: case LTU: case LE: case LEU:
5664 case UNEQ: case LTGT:
5665 case UNGT: case UNGE:
5666 case UNLT: case UNLE:
5667 case UNORDERED: case ORDERED:
5668 /* If the first operand is a condition code, we can't do anything
5669 with it. */
5670 if (GET_CODE (XEXP (x, 0)) == COMPARE
5671 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5672 && ! CC0_P (XEXP (x, 0))))
5673 {
5674 rtx op0 = XEXP (x, 0);
5675 rtx op1 = XEXP (x, 1);
5676 enum rtx_code new_code;
5677
5678 if (GET_CODE (op0) == COMPARE)
5679 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5680
5681 /* Simplify our comparison, if possible. */
5682 new_code = simplify_comparison (code, &op0, &op1);
5683
5684 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5685 if only the low-order bit is possibly nonzero in X (such as when
5686 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5687 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5688 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5689 (plus X 1).
5690
5691 Remove any ZERO_EXTRACT we made when thinking this was a
5692 comparison. It may now be simpler to use, e.g., an AND. If a
5693 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5694 the call to make_compound_operation in the SET case. */
5695
5696 if (STORE_FLAG_VALUE == 1
5697 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5698 && op1 == const0_rtx
5699 && mode == GET_MODE (op0)
5700 && nonzero_bits (op0, mode) == 1)
5701 return gen_lowpart (mode,
5702 expand_compound_operation (op0));
5703
5704 else if (STORE_FLAG_VALUE == 1
5705 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5706 && op1 == const0_rtx
5707 && mode == GET_MODE (op0)
5708 && (num_sign_bit_copies (op0, mode)
5709 == GET_MODE_BITSIZE (mode)))
5710 {
5711 op0 = expand_compound_operation (op0);
5712 return simplify_gen_unary (NEG, mode,
5713 gen_lowpart (mode, op0),
5714 mode);
5715 }
5716
5717 else if (STORE_FLAG_VALUE == 1
5718 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5719 && op1 == const0_rtx
5720 && mode == GET_MODE (op0)
5721 && nonzero_bits (op0, mode) == 1)
5722 {
5723 op0 = expand_compound_operation (op0);
5724 return simplify_gen_binary (XOR, mode,
5725 gen_lowpart (mode, op0),
5726 const1_rtx);
5727 }
5728
5729 else if (STORE_FLAG_VALUE == 1
5730 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5731 && op1 == const0_rtx
5732 && mode == GET_MODE (op0)
5733 && (num_sign_bit_copies (op0, mode)
5734 == GET_MODE_BITSIZE (mode)))
5735 {
5736 op0 = expand_compound_operation (op0);
5737 return plus_constant (gen_lowpart (mode, op0), 1);
5738 }
5739
5740 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5741 those above. */
5742 if (STORE_FLAG_VALUE == -1
5743 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5744 && op1 == const0_rtx
5745 && (num_sign_bit_copies (op0, mode)
5746 == GET_MODE_BITSIZE (mode)))
5747 return gen_lowpart (mode,
5748 expand_compound_operation (op0));
5749
5750 else if (STORE_FLAG_VALUE == -1
5751 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5752 && op1 == const0_rtx
5753 && mode == GET_MODE (op0)
5754 && nonzero_bits (op0, mode) == 1)
5755 {
5756 op0 = expand_compound_operation (op0);
5757 return simplify_gen_unary (NEG, mode,
5758 gen_lowpart (mode, op0),
5759 mode);
5760 }
5761
5762 else if (STORE_FLAG_VALUE == -1
5763 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5764 && op1 == const0_rtx
5765 && mode == GET_MODE (op0)
5766 && (num_sign_bit_copies (op0, mode)
5767 == GET_MODE_BITSIZE (mode)))
5768 {
5769 op0 = expand_compound_operation (op0);
5770 return simplify_gen_unary (NOT, mode,
5771 gen_lowpart (mode, op0),
5772 mode);
5773 }
5774
5775 /* If X is 0/1, (eq X 0) is X-1. */
5776 else if (STORE_FLAG_VALUE == -1
5777 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5778 && op1 == const0_rtx
5779 && mode == GET_MODE (op0)
5780 && nonzero_bits (op0, mode) == 1)
5781 {
5782 op0 = expand_compound_operation (op0);
5783 return plus_constant (gen_lowpart (mode, op0), -1);
5784 }
5785
5786 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5787 one bit that might be nonzero, we can convert (ne x 0) to
5788 (ashift x c) where C puts the bit in the sign bit. Remove any
5789 AND with STORE_FLAG_VALUE when we are done, since we are only
5790 going to test the sign bit. */
5791 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5792 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5793 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5794 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5795 && op1 == const0_rtx
5796 && mode == GET_MODE (op0)
5797 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5798 {
5799 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5800 expand_compound_operation (op0),
5801 GET_MODE_BITSIZE (mode) - 1 - i);
5802 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5803 return XEXP (x, 0);
5804 else
5805 return x;
5806 }
5807
5808 /* If the code changed, return a whole new comparison. */
5809 if (new_code != code)
5810 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5811
5812 /* Otherwise, keep this operation, but maybe change its operands.
5813 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5814 SUBST (XEXP (x, 0), op0);
5815 SUBST (XEXP (x, 1), op1);
5816 }
5817 break;
5818
5819 case IF_THEN_ELSE:
5820 return simplify_if_then_else (x);
5821
5822 case ZERO_EXTRACT:
5823 case SIGN_EXTRACT:
5824 case ZERO_EXTEND:
5825 case SIGN_EXTEND:
5826 /* If we are processing SET_DEST, we are done. */
5827 if (in_dest)
5828 return x;
5829
5830 return expand_compound_operation (x);
5831
5832 case SET:
5833 return simplify_set (x);
5834
5835 case AND:
5836 case IOR:
5837 return simplify_logical (x);
5838
5839 case ASHIFT:
5840 case LSHIFTRT:
5841 case ASHIFTRT:
5842 case ROTATE:
5843 case ROTATERT:
5844 /* If this is a shift by a constant amount, simplify it. */
5845 if (CONST_INT_P (XEXP (x, 1)))
5846 return simplify_shift_const (x, code, mode, XEXP (x, 0),
5847 INTVAL (XEXP (x, 1)));
5848
5849 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5850 SUBST (XEXP (x, 1),
5851 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5852 ((unsigned HOST_WIDE_INT) 1
5853 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5854 - 1,
5855 0));
5856 break;
5857
5858 default:
5859 break;
5860 }
5861
5862 return x;
5863 }
5864 \f
5865 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5866
5867 static rtx
5868 simplify_if_then_else (rtx x)
5869 {
5870 enum machine_mode mode = GET_MODE (x);
5871 rtx cond = XEXP (x, 0);
5872 rtx true_rtx = XEXP (x, 1);
5873 rtx false_rtx = XEXP (x, 2);
5874 enum rtx_code true_code = GET_CODE (cond);
5875 int comparison_p = COMPARISON_P (cond);
5876 rtx temp;
5877 int i;
5878 enum rtx_code false_code;
5879 rtx reversed;
5880
5881 /* Simplify storing of the truth value. */
5882 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5883 return simplify_gen_relational (true_code, mode, VOIDmode,
5884 XEXP (cond, 0), XEXP (cond, 1));
5885
5886 /* Also when the truth value has to be reversed. */
5887 if (comparison_p
5888 && true_rtx == const0_rtx && false_rtx == const_true_rtx
5889 && (reversed = reversed_comparison (cond, mode)))
5890 return reversed;
5891
5892 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5893 in it is being compared against certain values. Get the true and false
5894 comparisons and see if that says anything about the value of each arm. */
5895
5896 if (comparison_p
5897 && ((false_code = reversed_comparison_code (cond, NULL))
5898 != UNKNOWN)
5899 && REG_P (XEXP (cond, 0)))
5900 {
5901 HOST_WIDE_INT nzb;
5902 rtx from = XEXP (cond, 0);
5903 rtx true_val = XEXP (cond, 1);
5904 rtx false_val = true_val;
5905 int swapped = 0;
5906
5907 /* If FALSE_CODE is EQ, swap the codes and arms. */
5908
5909 if (false_code == EQ)
5910 {
5911 swapped = 1, true_code = EQ, false_code = NE;
5912 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5913 }
5914
5915 /* If we are comparing against zero and the expression being tested has
5916 only a single bit that might be nonzero, that is its value when it is
5917 not equal to zero. Similarly if it is known to be -1 or 0. */
5918
5919 if (true_code == EQ && true_val == const0_rtx
5920 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5921 {
5922 false_code = EQ;
5923 false_val = GEN_INT (trunc_int_for_mode (nzb, GET_MODE (from)));
5924 }
5925 else if (true_code == EQ && true_val == const0_rtx
5926 && (num_sign_bit_copies (from, GET_MODE (from))
5927 == GET_MODE_BITSIZE (GET_MODE (from))))
5928 {
5929 false_code = EQ;
5930 false_val = constm1_rtx;
5931 }
5932
5933 /* Now simplify an arm if we know the value of the register in the
5934 branch and it is used in the arm. Be careful due to the potential
5935 of locally-shared RTL. */
5936
5937 if (reg_mentioned_p (from, true_rtx))
5938 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5939 from, true_val),
5940 pc_rtx, pc_rtx, 0, 0);
5941 if (reg_mentioned_p (from, false_rtx))
5942 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5943 from, false_val),
5944 pc_rtx, pc_rtx, 0, 0);
5945
5946 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5947 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5948
5949 true_rtx = XEXP (x, 1);
5950 false_rtx = XEXP (x, 2);
5951 true_code = GET_CODE (cond);
5952 }
5953
5954 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5955 reversed, do so to avoid needing two sets of patterns for
5956 subtract-and-branch insns. Similarly if we have a constant in the true
5957 arm, the false arm is the same as the first operand of the comparison, or
5958 the false arm is more complicated than the true arm. */
5959
5960 if (comparison_p
5961 && reversed_comparison_code (cond, NULL) != UNKNOWN
5962 && (true_rtx == pc_rtx
5963 || (CONSTANT_P (true_rtx)
5964 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
5965 || true_rtx == const0_rtx
5966 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
5967 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
5968 && !OBJECT_P (false_rtx))
5969 || reg_mentioned_p (true_rtx, false_rtx)
5970 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
5971 {
5972 true_code = reversed_comparison_code (cond, NULL);
5973 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
5974 SUBST (XEXP (x, 1), false_rtx);
5975 SUBST (XEXP (x, 2), true_rtx);
5976
5977 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5978 cond = XEXP (x, 0);
5979
5980 /* It is possible that the conditional has been simplified out. */
5981 true_code = GET_CODE (cond);
5982 comparison_p = COMPARISON_P (cond);
5983 }
5984
5985 /* If the two arms are identical, we don't need the comparison. */
5986
5987 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
5988 return true_rtx;
5989
5990 /* Convert a == b ? b : a to "a". */
5991 if (true_code == EQ && ! side_effects_p (cond)
5992 && !HONOR_NANS (mode)
5993 && rtx_equal_p (XEXP (cond, 0), false_rtx)
5994 && rtx_equal_p (XEXP (cond, 1), true_rtx))
5995 return false_rtx;
5996 else if (true_code == NE && ! side_effects_p (cond)
5997 && !HONOR_NANS (mode)
5998 && rtx_equal_p (XEXP (cond, 0), true_rtx)
5999 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6000 return true_rtx;
6001
6002 /* Look for cases where we have (abs x) or (neg (abs X)). */
6003
6004 if (GET_MODE_CLASS (mode) == MODE_INT
6005 && comparison_p
6006 && XEXP (cond, 1) == const0_rtx
6007 && GET_CODE (false_rtx) == NEG
6008 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6009 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6010 && ! side_effects_p (true_rtx))
6011 switch (true_code)
6012 {
6013 case GT:
6014 case GE:
6015 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6016 case LT:
6017 case LE:
6018 return
6019 simplify_gen_unary (NEG, mode,
6020 simplify_gen_unary (ABS, mode, true_rtx, mode),
6021 mode);
6022 default:
6023 break;
6024 }
6025
6026 /* Look for MIN or MAX. */
6027
6028 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6029 && comparison_p
6030 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6031 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6032 && ! side_effects_p (cond))
6033 switch (true_code)
6034 {
6035 case GE:
6036 case GT:
6037 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6038 case LE:
6039 case LT:
6040 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6041 case GEU:
6042 case GTU:
6043 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6044 case LEU:
6045 case LTU:
6046 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6047 default:
6048 break;
6049 }
6050
6051 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6052 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6053 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6054 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6055 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6056 neither 1 or -1, but it isn't worth checking for. */
6057
6058 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6059 && comparison_p
6060 && GET_MODE_CLASS (mode) == MODE_INT
6061 && ! side_effects_p (x))
6062 {
6063 rtx t = make_compound_operation (true_rtx, SET);
6064 rtx f = make_compound_operation (false_rtx, SET);
6065 rtx cond_op0 = XEXP (cond, 0);
6066 rtx cond_op1 = XEXP (cond, 1);
6067 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6068 enum machine_mode m = mode;
6069 rtx z = 0, c1 = NULL_RTX;
6070
6071 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6072 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6073 || GET_CODE (t) == ASHIFT
6074 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6075 && rtx_equal_p (XEXP (t, 0), f))
6076 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6077
6078 /* If an identity-zero op is commutative, check whether there
6079 would be a match if we swapped the operands. */
6080 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6081 || GET_CODE (t) == XOR)
6082 && rtx_equal_p (XEXP (t, 1), f))
6083 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6084 else if (GET_CODE (t) == SIGN_EXTEND
6085 && (GET_CODE (XEXP (t, 0)) == PLUS
6086 || GET_CODE (XEXP (t, 0)) == MINUS
6087 || GET_CODE (XEXP (t, 0)) == IOR
6088 || GET_CODE (XEXP (t, 0)) == XOR
6089 || GET_CODE (XEXP (t, 0)) == ASHIFT
6090 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6091 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6092 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6093 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6094 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6095 && (num_sign_bit_copies (f, GET_MODE (f))
6096 > (unsigned int)
6097 (GET_MODE_BITSIZE (mode)
6098 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6099 {
6100 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6101 extend_op = SIGN_EXTEND;
6102 m = GET_MODE (XEXP (t, 0));
6103 }
6104 else if (GET_CODE (t) == SIGN_EXTEND
6105 && (GET_CODE (XEXP (t, 0)) == PLUS
6106 || GET_CODE (XEXP (t, 0)) == IOR
6107 || GET_CODE (XEXP (t, 0)) == XOR)
6108 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6109 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6110 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6111 && (num_sign_bit_copies (f, GET_MODE (f))
6112 > (unsigned int)
6113 (GET_MODE_BITSIZE (mode)
6114 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6115 {
6116 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6117 extend_op = SIGN_EXTEND;
6118 m = GET_MODE (XEXP (t, 0));
6119 }
6120 else if (GET_CODE (t) == ZERO_EXTEND
6121 && (GET_CODE (XEXP (t, 0)) == PLUS
6122 || GET_CODE (XEXP (t, 0)) == MINUS
6123 || GET_CODE (XEXP (t, 0)) == IOR
6124 || GET_CODE (XEXP (t, 0)) == XOR
6125 || GET_CODE (XEXP (t, 0)) == ASHIFT
6126 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6127 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6128 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6129 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6130 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6131 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6132 && ((nonzero_bits (f, GET_MODE (f))
6133 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6134 == 0))
6135 {
6136 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6137 extend_op = ZERO_EXTEND;
6138 m = GET_MODE (XEXP (t, 0));
6139 }
6140 else if (GET_CODE (t) == ZERO_EXTEND
6141 && (GET_CODE (XEXP (t, 0)) == PLUS
6142 || GET_CODE (XEXP (t, 0)) == IOR
6143 || GET_CODE (XEXP (t, 0)) == XOR)
6144 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6145 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6146 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6147 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6148 && ((nonzero_bits (f, GET_MODE (f))
6149 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6150 == 0))
6151 {
6152 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6153 extend_op = ZERO_EXTEND;
6154 m = GET_MODE (XEXP (t, 0));
6155 }
6156
6157 if (z)
6158 {
6159 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6160 cond_op0, cond_op1),
6161 pc_rtx, pc_rtx, 0, 0);
6162 temp = simplify_gen_binary (MULT, m, temp,
6163 simplify_gen_binary (MULT, m, c1,
6164 const_true_rtx));
6165 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
6166 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6167
6168 if (extend_op != UNKNOWN)
6169 temp = simplify_gen_unary (extend_op, mode, temp, m);
6170
6171 return temp;
6172 }
6173 }
6174
6175 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6176 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6177 negation of a single bit, we can convert this operation to a shift. We
6178 can actually do this more generally, but it doesn't seem worth it. */
6179
6180 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6181 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6182 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6183 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6184 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6185 == GET_MODE_BITSIZE (mode))
6186 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6187 return
6188 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6189 gen_lowpart (mode, XEXP (cond, 0)), i);
6190
6191 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6192 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6193 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6194 && GET_MODE (XEXP (cond, 0)) == mode
6195 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6196 == nonzero_bits (XEXP (cond, 0), mode)
6197 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6198 return XEXP (cond, 0);
6199
6200 return x;
6201 }
6202 \f
6203 /* Simplify X, a SET expression. Return the new expression. */
6204
6205 static rtx
6206 simplify_set (rtx x)
6207 {
6208 rtx src = SET_SRC (x);
6209 rtx dest = SET_DEST (x);
6210 enum machine_mode mode
6211 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6212 rtx other_insn;
6213 rtx *cc_use;
6214
6215 /* (set (pc) (return)) gets written as (return). */
6216 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
6217 return src;
6218
6219 /* Now that we know for sure which bits of SRC we are using, see if we can
6220 simplify the expression for the object knowing that we only need the
6221 low-order bits. */
6222
6223 if (GET_MODE_CLASS (mode) == MODE_INT
6224 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
6225 {
6226 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6227 SUBST (SET_SRC (x), src);
6228 }
6229
6230 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6231 the comparison result and try to simplify it unless we already have used
6232 undobuf.other_insn. */
6233 if ((GET_MODE_CLASS (mode) == MODE_CC
6234 || GET_CODE (src) == COMPARE
6235 || CC0_P (dest))
6236 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6237 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6238 && COMPARISON_P (*cc_use)
6239 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6240 {
6241 enum rtx_code old_code = GET_CODE (*cc_use);
6242 enum rtx_code new_code;
6243 rtx op0, op1, tmp;
6244 int other_changed = 0;
6245 rtx inner_compare = NULL_RTX;
6246 enum machine_mode compare_mode = GET_MODE (dest);
6247
6248 if (GET_CODE (src) == COMPARE)
6249 {
6250 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6251 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6252 {
6253 inner_compare = op0;
6254 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6255 }
6256 }
6257 else
6258 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6259
6260 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6261 op0, op1);
6262 if (!tmp)
6263 new_code = old_code;
6264 else if (!CONSTANT_P (tmp))
6265 {
6266 new_code = GET_CODE (tmp);
6267 op0 = XEXP (tmp, 0);
6268 op1 = XEXP (tmp, 1);
6269 }
6270 else
6271 {
6272 rtx pat = PATTERN (other_insn);
6273 undobuf.other_insn = other_insn;
6274 SUBST (*cc_use, tmp);
6275
6276 /* Attempt to simplify CC user. */
6277 if (GET_CODE (pat) == SET)
6278 {
6279 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6280 if (new_rtx != NULL_RTX)
6281 SUBST (SET_SRC (pat), new_rtx);
6282 }
6283
6284 /* Convert X into a no-op move. */
6285 SUBST (SET_DEST (x), pc_rtx);
6286 SUBST (SET_SRC (x), pc_rtx);
6287 return x;
6288 }
6289
6290 /* Simplify our comparison, if possible. */
6291 new_code = simplify_comparison (new_code, &op0, &op1);
6292
6293 #ifdef SELECT_CC_MODE
6294 /* If this machine has CC modes other than CCmode, check to see if we
6295 need to use a different CC mode here. */
6296 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6297 compare_mode = GET_MODE (op0);
6298 else if (inner_compare
6299 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6300 && new_code == old_code
6301 && op0 == XEXP (inner_compare, 0)
6302 && op1 == XEXP (inner_compare, 1))
6303 compare_mode = GET_MODE (inner_compare);
6304 else
6305 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6306
6307 #ifndef HAVE_cc0
6308 /* If the mode changed, we have to change SET_DEST, the mode in the
6309 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6310 a hard register, just build new versions with the proper mode. If it
6311 is a pseudo, we lose unless it is only time we set the pseudo, in
6312 which case we can safely change its mode. */
6313 if (compare_mode != GET_MODE (dest))
6314 {
6315 if (can_change_dest_mode (dest, 0, compare_mode))
6316 {
6317 unsigned int regno = REGNO (dest);
6318 rtx new_dest;
6319
6320 if (regno < FIRST_PSEUDO_REGISTER)
6321 new_dest = gen_rtx_REG (compare_mode, regno);
6322 else
6323 {
6324 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6325 new_dest = regno_reg_rtx[regno];
6326 }
6327
6328 SUBST (SET_DEST (x), new_dest);
6329 SUBST (XEXP (*cc_use, 0), new_dest);
6330 other_changed = 1;
6331
6332 dest = new_dest;
6333 }
6334 }
6335 #endif /* cc0 */
6336 #endif /* SELECT_CC_MODE */
6337
6338 /* If the code changed, we have to build a new comparison in
6339 undobuf.other_insn. */
6340 if (new_code != old_code)
6341 {
6342 int other_changed_previously = other_changed;
6343 unsigned HOST_WIDE_INT mask;
6344 rtx old_cc_use = *cc_use;
6345
6346 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6347 dest, const0_rtx));
6348 other_changed = 1;
6349
6350 /* If the only change we made was to change an EQ into an NE or
6351 vice versa, OP0 has only one bit that might be nonzero, and OP1
6352 is zero, check if changing the user of the condition code will
6353 produce a valid insn. If it won't, we can keep the original code
6354 in that insn by surrounding our operation with an XOR. */
6355
6356 if (((old_code == NE && new_code == EQ)
6357 || (old_code == EQ && new_code == NE))
6358 && ! other_changed_previously && op1 == const0_rtx
6359 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
6360 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6361 {
6362 rtx pat = PATTERN (other_insn), note = 0;
6363
6364 if ((recog_for_combine (&pat, other_insn, &note) < 0
6365 && ! check_asm_operands (pat)))
6366 {
6367 *cc_use = old_cc_use;
6368 other_changed = 0;
6369
6370 op0 = simplify_gen_binary (XOR, GET_MODE (op0),
6371 op0, GEN_INT (mask));
6372 }
6373 }
6374 }
6375
6376 if (other_changed)
6377 undobuf.other_insn = other_insn;
6378
6379 /* Otherwise, if we didn't previously have a COMPARE in the
6380 correct mode, we need one. */
6381 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6382 {
6383 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6384 src = SET_SRC (x);
6385 }
6386 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6387 {
6388 SUBST (SET_SRC (x), op0);
6389 src = SET_SRC (x);
6390 }
6391 /* Otherwise, update the COMPARE if needed. */
6392 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6393 {
6394 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6395 src = SET_SRC (x);
6396 }
6397 }
6398 else
6399 {
6400 /* Get SET_SRC in a form where we have placed back any
6401 compound expressions. Then do the checks below. */
6402 src = make_compound_operation (src, SET);
6403 SUBST (SET_SRC (x), src);
6404 }
6405
6406 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6407 and X being a REG or (subreg (reg)), we may be able to convert this to
6408 (set (subreg:m2 x) (op)).
6409
6410 We can always do this if M1 is narrower than M2 because that means that
6411 we only care about the low bits of the result.
6412
6413 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6414 perform a narrower operation than requested since the high-order bits will
6415 be undefined. On machine where it is defined, this transformation is safe
6416 as long as M1 and M2 have the same number of words. */
6417
6418 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6419 && !OBJECT_P (SUBREG_REG (src))
6420 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6421 / UNITS_PER_WORD)
6422 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6423 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6424 #ifndef WORD_REGISTER_OPERATIONS
6425 && (GET_MODE_SIZE (GET_MODE (src))
6426 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6427 #endif
6428 #ifdef CANNOT_CHANGE_MODE_CLASS
6429 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6430 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6431 GET_MODE (SUBREG_REG (src)),
6432 GET_MODE (src)))
6433 #endif
6434 && (REG_P (dest)
6435 || (GET_CODE (dest) == SUBREG
6436 && REG_P (SUBREG_REG (dest)))))
6437 {
6438 SUBST (SET_DEST (x),
6439 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6440 dest));
6441 SUBST (SET_SRC (x), SUBREG_REG (src));
6442
6443 src = SET_SRC (x), dest = SET_DEST (x);
6444 }
6445
6446 #ifdef HAVE_cc0
6447 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6448 in SRC. */
6449 if (dest == cc0_rtx
6450 && GET_CODE (src) == SUBREG
6451 && subreg_lowpart_p (src)
6452 && (GET_MODE_BITSIZE (GET_MODE (src))
6453 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
6454 {
6455 rtx inner = SUBREG_REG (src);
6456 enum machine_mode inner_mode = GET_MODE (inner);
6457
6458 /* Here we make sure that we don't have a sign bit on. */
6459 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
6460 && (nonzero_bits (inner, inner_mode)
6461 < ((unsigned HOST_WIDE_INT) 1
6462 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
6463 {
6464 SUBST (SET_SRC (x), inner);
6465 src = SET_SRC (x);
6466 }
6467 }
6468 #endif
6469
6470 #ifdef LOAD_EXTEND_OP
6471 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6472 would require a paradoxical subreg. Replace the subreg with a
6473 zero_extend to avoid the reload that would otherwise be required. */
6474
6475 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6476 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6477 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6478 && SUBREG_BYTE (src) == 0
6479 && (GET_MODE_SIZE (GET_MODE (src))
6480 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6481 && MEM_P (SUBREG_REG (src)))
6482 {
6483 SUBST (SET_SRC (x),
6484 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6485 GET_MODE (src), SUBREG_REG (src)));
6486
6487 src = SET_SRC (x);
6488 }
6489 #endif
6490
6491 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6492 are comparing an item known to be 0 or -1 against 0, use a logical
6493 operation instead. Check for one of the arms being an IOR of the other
6494 arm with some value. We compute three terms to be IOR'ed together. In
6495 practice, at most two will be nonzero. Then we do the IOR's. */
6496
6497 if (GET_CODE (dest) != PC
6498 && GET_CODE (src) == IF_THEN_ELSE
6499 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6500 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6501 && XEXP (XEXP (src, 0), 1) == const0_rtx
6502 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6503 #ifdef HAVE_conditional_move
6504 && ! can_conditionally_move_p (GET_MODE (src))
6505 #endif
6506 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6507 GET_MODE (XEXP (XEXP (src, 0), 0)))
6508 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
6509 && ! side_effects_p (src))
6510 {
6511 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6512 ? XEXP (src, 1) : XEXP (src, 2));
6513 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6514 ? XEXP (src, 2) : XEXP (src, 1));
6515 rtx term1 = const0_rtx, term2, term3;
6516
6517 if (GET_CODE (true_rtx) == IOR
6518 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6519 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6520 else if (GET_CODE (true_rtx) == IOR
6521 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6522 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6523 else if (GET_CODE (false_rtx) == IOR
6524 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6525 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6526 else if (GET_CODE (false_rtx) == IOR
6527 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6528 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6529
6530 term2 = simplify_gen_binary (AND, GET_MODE (src),
6531 XEXP (XEXP (src, 0), 0), true_rtx);
6532 term3 = simplify_gen_binary (AND, GET_MODE (src),
6533 simplify_gen_unary (NOT, GET_MODE (src),
6534 XEXP (XEXP (src, 0), 0),
6535 GET_MODE (src)),
6536 false_rtx);
6537
6538 SUBST (SET_SRC (x),
6539 simplify_gen_binary (IOR, GET_MODE (src),
6540 simplify_gen_binary (IOR, GET_MODE (src),
6541 term1, term2),
6542 term3));
6543
6544 src = SET_SRC (x);
6545 }
6546
6547 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6548 whole thing fail. */
6549 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6550 return src;
6551 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6552 return dest;
6553 else
6554 /* Convert this into a field assignment operation, if possible. */
6555 return make_field_assignment (x);
6556 }
6557 \f
6558 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6559 result. */
6560
6561 static rtx
6562 simplify_logical (rtx x)
6563 {
6564 enum machine_mode mode = GET_MODE (x);
6565 rtx op0 = XEXP (x, 0);
6566 rtx op1 = XEXP (x, 1);
6567
6568 switch (GET_CODE (x))
6569 {
6570 case AND:
6571 /* We can call simplify_and_const_int only if we don't lose
6572 any (sign) bits when converting INTVAL (op1) to
6573 "unsigned HOST_WIDE_INT". */
6574 if (CONST_INT_P (op1)
6575 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6576 || INTVAL (op1) > 0))
6577 {
6578 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6579 if (GET_CODE (x) != AND)
6580 return x;
6581
6582 op0 = XEXP (x, 0);
6583 op1 = XEXP (x, 1);
6584 }
6585
6586 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6587 apply the distributive law and then the inverse distributive
6588 law to see if things simplify. */
6589 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6590 {
6591 rtx result = distribute_and_simplify_rtx (x, 0);
6592 if (result)
6593 return result;
6594 }
6595 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6596 {
6597 rtx result = distribute_and_simplify_rtx (x, 1);
6598 if (result)
6599 return result;
6600 }
6601 break;
6602
6603 case IOR:
6604 /* If we have (ior (and A B) C), apply the distributive law and then
6605 the inverse distributive law to see if things simplify. */
6606
6607 if (GET_CODE (op0) == AND)
6608 {
6609 rtx result = distribute_and_simplify_rtx (x, 0);
6610 if (result)
6611 return result;
6612 }
6613
6614 if (GET_CODE (op1) == AND)
6615 {
6616 rtx result = distribute_and_simplify_rtx (x, 1);
6617 if (result)
6618 return result;
6619 }
6620 break;
6621
6622 default:
6623 gcc_unreachable ();
6624 }
6625
6626 return x;
6627 }
6628 \f
6629 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6630 operations" because they can be replaced with two more basic operations.
6631 ZERO_EXTEND is also considered "compound" because it can be replaced with
6632 an AND operation, which is simpler, though only one operation.
6633
6634 The function expand_compound_operation is called with an rtx expression
6635 and will convert it to the appropriate shifts and AND operations,
6636 simplifying at each stage.
6637
6638 The function make_compound_operation is called to convert an expression
6639 consisting of shifts and ANDs into the equivalent compound expression.
6640 It is the inverse of this function, loosely speaking. */
6641
6642 static rtx
6643 expand_compound_operation (rtx x)
6644 {
6645 unsigned HOST_WIDE_INT pos = 0, len;
6646 int unsignedp = 0;
6647 unsigned int modewidth;
6648 rtx tem;
6649
6650 switch (GET_CODE (x))
6651 {
6652 case ZERO_EXTEND:
6653 unsignedp = 1;
6654 case SIGN_EXTEND:
6655 /* We can't necessarily use a const_int for a multiword mode;
6656 it depends on implicitly extending the value.
6657 Since we don't know the right way to extend it,
6658 we can't tell whether the implicit way is right.
6659
6660 Even for a mode that is no wider than a const_int,
6661 we can't win, because we need to sign extend one of its bits through
6662 the rest of it, and we don't know which bit. */
6663 if (CONST_INT_P (XEXP (x, 0)))
6664 return x;
6665
6666 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6667 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6668 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6669 reloaded. If not for that, MEM's would very rarely be safe.
6670
6671 Reject MODEs bigger than a word, because we might not be able
6672 to reference a two-register group starting with an arbitrary register
6673 (and currently gen_lowpart might crash for a SUBREG). */
6674
6675 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6676 return x;
6677
6678 /* Reject MODEs that aren't scalar integers because turning vector
6679 or complex modes into shifts causes problems. */
6680
6681 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6682 return x;
6683
6684 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
6685 /* If the inner object has VOIDmode (the only way this can happen
6686 is if it is an ASM_OPERANDS), we can't do anything since we don't
6687 know how much masking to do. */
6688 if (len == 0)
6689 return x;
6690
6691 break;
6692
6693 case ZERO_EXTRACT:
6694 unsignedp = 1;
6695
6696 /* ... fall through ... */
6697
6698 case SIGN_EXTRACT:
6699 /* If the operand is a CLOBBER, just return it. */
6700 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6701 return XEXP (x, 0);
6702
6703 if (!CONST_INT_P (XEXP (x, 1))
6704 || !CONST_INT_P (XEXP (x, 2))
6705 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6706 return x;
6707
6708 /* Reject MODEs that aren't scalar integers because turning vector
6709 or complex modes into shifts causes problems. */
6710
6711 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6712 return x;
6713
6714 len = INTVAL (XEXP (x, 1));
6715 pos = INTVAL (XEXP (x, 2));
6716
6717 /* This should stay within the object being extracted, fail otherwise. */
6718 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
6719 return x;
6720
6721 if (BITS_BIG_ENDIAN)
6722 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
6723
6724 break;
6725
6726 default:
6727 return x;
6728 }
6729 /* Convert sign extension to zero extension, if we know that the high
6730 bit is not set, as this is easier to optimize. It will be converted
6731 back to cheaper alternative in make_extraction. */
6732 if (GET_CODE (x) == SIGN_EXTEND
6733 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6734 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6735 & ~(((unsigned HOST_WIDE_INT)
6736 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6737 >> 1))
6738 == 0)))
6739 {
6740 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6741 rtx temp2 = expand_compound_operation (temp);
6742
6743 /* Make sure this is a profitable operation. */
6744 if (rtx_cost (x, SET, optimize_this_for_speed_p)
6745 > rtx_cost (temp2, SET, optimize_this_for_speed_p))
6746 return temp2;
6747 else if (rtx_cost (x, SET, optimize_this_for_speed_p)
6748 > rtx_cost (temp, SET, optimize_this_for_speed_p))
6749 return temp;
6750 else
6751 return x;
6752 }
6753
6754 /* We can optimize some special cases of ZERO_EXTEND. */
6755 if (GET_CODE (x) == ZERO_EXTEND)
6756 {
6757 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6758 know that the last value didn't have any inappropriate bits
6759 set. */
6760 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6761 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6762 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6763 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6764 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6765 return XEXP (XEXP (x, 0), 0);
6766
6767 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6768 if (GET_CODE (XEXP (x, 0)) == SUBREG
6769 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6770 && subreg_lowpart_p (XEXP (x, 0))
6771 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6772 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6773 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6774 return SUBREG_REG (XEXP (x, 0));
6775
6776 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6777 is a comparison and STORE_FLAG_VALUE permits. This is like
6778 the first case, but it works even when GET_MODE (x) is larger
6779 than HOST_WIDE_INT. */
6780 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6781 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6782 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6783 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6784 <= HOST_BITS_PER_WIDE_INT)
6785 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6786 return XEXP (XEXP (x, 0), 0);
6787
6788 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6789 if (GET_CODE (XEXP (x, 0)) == SUBREG
6790 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6791 && subreg_lowpart_p (XEXP (x, 0))
6792 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6793 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6794 <= HOST_BITS_PER_WIDE_INT)
6795 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6796 return SUBREG_REG (XEXP (x, 0));
6797
6798 }
6799
6800 /* If we reach here, we want to return a pair of shifts. The inner
6801 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6802 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6803 logical depending on the value of UNSIGNEDP.
6804
6805 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6806 converted into an AND of a shift.
6807
6808 We must check for the case where the left shift would have a negative
6809 count. This can happen in a case like (x >> 31) & 255 on machines
6810 that can't shift by a constant. On those machines, we would first
6811 combine the shift with the AND to produce a variable-position
6812 extraction. Then the constant of 31 would be substituted in
6813 to produce such a position. */
6814
6815 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
6816 if (modewidth >= pos + len)
6817 {
6818 enum machine_mode mode = GET_MODE (x);
6819 tem = gen_lowpart (mode, XEXP (x, 0));
6820 if (!tem || GET_CODE (tem) == CLOBBER)
6821 return x;
6822 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6823 tem, modewidth - pos - len);
6824 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6825 mode, tem, modewidth - len);
6826 }
6827 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6828 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6829 simplify_shift_const (NULL_RTX, LSHIFTRT,
6830 GET_MODE (x),
6831 XEXP (x, 0), pos),
6832 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
6833 else
6834 /* Any other cases we can't handle. */
6835 return x;
6836
6837 /* If we couldn't do this for some reason, return the original
6838 expression. */
6839 if (GET_CODE (tem) == CLOBBER)
6840 return x;
6841
6842 return tem;
6843 }
6844 \f
6845 /* X is a SET which contains an assignment of one object into
6846 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6847 or certain SUBREGS). If possible, convert it into a series of
6848 logical operations.
6849
6850 We half-heartedly support variable positions, but do not at all
6851 support variable lengths. */
6852
6853 static const_rtx
6854 expand_field_assignment (const_rtx x)
6855 {
6856 rtx inner;
6857 rtx pos; /* Always counts from low bit. */
6858 int len;
6859 rtx mask, cleared, masked;
6860 enum machine_mode compute_mode;
6861
6862 /* Loop until we find something we can't simplify. */
6863 while (1)
6864 {
6865 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6866 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6867 {
6868 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6869 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6870 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6871 }
6872 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6873 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
6874 {
6875 inner = XEXP (SET_DEST (x), 0);
6876 len = INTVAL (XEXP (SET_DEST (x), 1));
6877 pos = XEXP (SET_DEST (x), 2);
6878
6879 /* A constant position should stay within the width of INNER. */
6880 if (CONST_INT_P (pos)
6881 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6882 break;
6883
6884 if (BITS_BIG_ENDIAN)
6885 {
6886 if (CONST_INT_P (pos))
6887 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6888 - INTVAL (pos));
6889 else if (GET_CODE (pos) == MINUS
6890 && CONST_INT_P (XEXP (pos, 1))
6891 && (INTVAL (XEXP (pos, 1))
6892 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6893 /* If position is ADJUST - X, new position is X. */
6894 pos = XEXP (pos, 0);
6895 else
6896 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6897 GEN_INT (GET_MODE_BITSIZE (
6898 GET_MODE (inner))
6899 - len),
6900 pos);
6901 }
6902 }
6903
6904 /* A SUBREG between two modes that occupy the same numbers of words
6905 can be done by moving the SUBREG to the source. */
6906 else if (GET_CODE (SET_DEST (x)) == SUBREG
6907 /* We need SUBREGs to compute nonzero_bits properly. */
6908 && nonzero_sign_valid
6909 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6910 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6911 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6912 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6913 {
6914 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6915 gen_lowpart
6916 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6917 SET_SRC (x)));
6918 continue;
6919 }
6920 else
6921 break;
6922
6923 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6924 inner = SUBREG_REG (inner);
6925
6926 compute_mode = GET_MODE (inner);
6927
6928 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6929 if (! SCALAR_INT_MODE_P (compute_mode))
6930 {
6931 enum machine_mode imode;
6932
6933 /* Don't do anything for vector or complex integral types. */
6934 if (! FLOAT_MODE_P (compute_mode))
6935 break;
6936
6937 /* Try to find an integral mode to pun with. */
6938 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6939 if (imode == BLKmode)
6940 break;
6941
6942 compute_mode = imode;
6943 inner = gen_lowpart (imode, inner);
6944 }
6945
6946 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6947 if (len >= HOST_BITS_PER_WIDE_INT)
6948 break;
6949
6950 /* Now compute the equivalent expression. Make a copy of INNER
6951 for the SET_DEST in case it is a MEM into which we will substitute;
6952 we don't want shared RTL in that case. */
6953 mask = GEN_INT (((unsigned HOST_WIDE_INT) 1 << len) - 1);
6954 cleared = simplify_gen_binary (AND, compute_mode,
6955 simplify_gen_unary (NOT, compute_mode,
6956 simplify_gen_binary (ASHIFT,
6957 compute_mode,
6958 mask, pos),
6959 compute_mode),
6960 inner);
6961 masked = simplify_gen_binary (ASHIFT, compute_mode,
6962 simplify_gen_binary (
6963 AND, compute_mode,
6964 gen_lowpart (compute_mode, SET_SRC (x)),
6965 mask),
6966 pos);
6967
6968 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6969 simplify_gen_binary (IOR, compute_mode,
6970 cleared, masked));
6971 }
6972
6973 return x;
6974 }
6975 \f
6976 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
6977 it is an RTX that represents a variable starting position; otherwise,
6978 POS is the (constant) starting bit position (counted from the LSB).
6979
6980 UNSIGNEDP is nonzero for an unsigned reference and zero for a
6981 signed reference.
6982
6983 IN_DEST is nonzero if this is a reference in the destination of a
6984 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
6985 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6986 be used.
6987
6988 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
6989 ZERO_EXTRACT should be built even for bits starting at bit 0.
6990
6991 MODE is the desired mode of the result (if IN_DEST == 0).
6992
6993 The result is an RTX for the extraction or NULL_RTX if the target
6994 can't handle it. */
6995
6996 static rtx
6997 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6998 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6999 int in_dest, int in_compare)
7000 {
7001 /* This mode describes the size of the storage area
7002 to fetch the overall value from. Within that, we
7003 ignore the POS lowest bits, etc. */
7004 enum machine_mode is_mode = GET_MODE (inner);
7005 enum machine_mode inner_mode;
7006 enum machine_mode wanted_inner_mode;
7007 enum machine_mode wanted_inner_reg_mode = word_mode;
7008 enum machine_mode pos_mode = word_mode;
7009 enum machine_mode extraction_mode = word_mode;
7010 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7011 rtx new_rtx = 0;
7012 rtx orig_pos_rtx = pos_rtx;
7013 HOST_WIDE_INT orig_pos;
7014
7015 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7016 {
7017 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7018 consider just the QI as the memory to extract from.
7019 The subreg adds or removes high bits; its mode is
7020 irrelevant to the meaning of this extraction,
7021 since POS and LEN count from the lsb. */
7022 if (MEM_P (SUBREG_REG (inner)))
7023 is_mode = GET_MODE (SUBREG_REG (inner));
7024 inner = SUBREG_REG (inner);
7025 }
7026 else if (GET_CODE (inner) == ASHIFT
7027 && CONST_INT_P (XEXP (inner, 1))
7028 && pos_rtx == 0 && pos == 0
7029 && len > UINTVAL (XEXP (inner, 1)))
7030 {
7031 /* We're extracting the least significant bits of an rtx
7032 (ashift X (const_int C)), where LEN > C. Extract the
7033 least significant (LEN - C) bits of X, giving an rtx
7034 whose mode is MODE, then shift it left C times. */
7035 new_rtx = make_extraction (mode, XEXP (inner, 0),
7036 0, 0, len - INTVAL (XEXP (inner, 1)),
7037 unsignedp, in_dest, in_compare);
7038 if (new_rtx != 0)
7039 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7040 }
7041
7042 inner_mode = GET_MODE (inner);
7043
7044 if (pos_rtx && CONST_INT_P (pos_rtx))
7045 pos = INTVAL (pos_rtx), pos_rtx = 0;
7046
7047 /* See if this can be done without an extraction. We never can if the
7048 width of the field is not the same as that of some integer mode. For
7049 registers, we can only avoid the extraction if the position is at the
7050 low-order bit and this is either not in the destination or we have the
7051 appropriate STRICT_LOW_PART operation available.
7052
7053 For MEM, we can avoid an extract if the field starts on an appropriate
7054 boundary and we can change the mode of the memory reference. */
7055
7056 if (tmode != BLKmode
7057 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7058 && !MEM_P (inner)
7059 && (inner_mode == tmode
7060 || !REG_P (inner)
7061 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
7062 GET_MODE_BITSIZE (inner_mode))
7063 || reg_truncated_to_mode (tmode, inner))
7064 && (! in_dest
7065 || (REG_P (inner)
7066 && have_insn_for (STRICT_LOW_PART, tmode))))
7067 || (MEM_P (inner) && pos_rtx == 0
7068 && (pos
7069 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7070 : BITS_PER_UNIT)) == 0
7071 /* We can't do this if we are widening INNER_MODE (it
7072 may not be aligned, for one thing). */
7073 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
7074 && (inner_mode == tmode
7075 || (! mode_dependent_address_p (XEXP (inner, 0))
7076 && ! MEM_VOLATILE_P (inner))))))
7077 {
7078 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7079 field. If the original and current mode are the same, we need not
7080 adjust the offset. Otherwise, we do if bytes big endian.
7081
7082 If INNER is not a MEM, get a piece consisting of just the field
7083 of interest (in this case POS % BITS_PER_WORD must be 0). */
7084
7085 if (MEM_P (inner))
7086 {
7087 HOST_WIDE_INT offset;
7088
7089 /* POS counts from lsb, but make OFFSET count in memory order. */
7090 if (BYTES_BIG_ENDIAN)
7091 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
7092 else
7093 offset = pos / BITS_PER_UNIT;
7094
7095 new_rtx = adjust_address_nv (inner, tmode, offset);
7096 }
7097 else if (REG_P (inner))
7098 {
7099 if (tmode != inner_mode)
7100 {
7101 /* We can't call gen_lowpart in a DEST since we
7102 always want a SUBREG (see below) and it would sometimes
7103 return a new hard register. */
7104 if (pos || in_dest)
7105 {
7106 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7107
7108 if (WORDS_BIG_ENDIAN
7109 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7110 final_word = ((GET_MODE_SIZE (inner_mode)
7111 - GET_MODE_SIZE (tmode))
7112 / UNITS_PER_WORD) - final_word;
7113
7114 final_word *= UNITS_PER_WORD;
7115 if (BYTES_BIG_ENDIAN &&
7116 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7117 final_word += (GET_MODE_SIZE (inner_mode)
7118 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7119
7120 /* Avoid creating invalid subregs, for example when
7121 simplifying (x>>32)&255. */
7122 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7123 return NULL_RTX;
7124
7125 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7126 }
7127 else
7128 new_rtx = gen_lowpart (tmode, inner);
7129 }
7130 else
7131 new_rtx = inner;
7132 }
7133 else
7134 new_rtx = force_to_mode (inner, tmode,
7135 len >= HOST_BITS_PER_WIDE_INT
7136 ? ~(unsigned HOST_WIDE_INT) 0
7137 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7138 0);
7139
7140 /* If this extraction is going into the destination of a SET,
7141 make a STRICT_LOW_PART unless we made a MEM. */
7142
7143 if (in_dest)
7144 return (MEM_P (new_rtx) ? new_rtx
7145 : (GET_CODE (new_rtx) != SUBREG
7146 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7147 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7148
7149 if (mode == tmode)
7150 return new_rtx;
7151
7152 if (CONST_INT_P (new_rtx)
7153 || GET_CODE (new_rtx) == CONST_DOUBLE)
7154 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7155 mode, new_rtx, tmode);
7156
7157 /* If we know that no extraneous bits are set, and that the high
7158 bit is not set, convert the extraction to the cheaper of
7159 sign and zero extension, that are equivalent in these cases. */
7160 if (flag_expensive_optimizations
7161 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
7162 && ((nonzero_bits (new_rtx, tmode)
7163 & ~(((unsigned HOST_WIDE_INT)
7164 GET_MODE_MASK (tmode))
7165 >> 1))
7166 == 0)))
7167 {
7168 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7169 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7170
7171 /* Prefer ZERO_EXTENSION, since it gives more information to
7172 backends. */
7173 if (rtx_cost (temp, SET, optimize_this_for_speed_p)
7174 <= rtx_cost (temp1, SET, optimize_this_for_speed_p))
7175 return temp;
7176 return temp1;
7177 }
7178
7179 /* Otherwise, sign- or zero-extend unless we already are in the
7180 proper mode. */
7181
7182 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7183 mode, new_rtx));
7184 }
7185
7186 /* Unless this is a COMPARE or we have a funny memory reference,
7187 don't do anything with zero-extending field extracts starting at
7188 the low-order bit since they are simple AND operations. */
7189 if (pos_rtx == 0 && pos == 0 && ! in_dest
7190 && ! in_compare && unsignedp)
7191 return 0;
7192
7193 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7194 if the position is not a constant and the length is not 1. In all
7195 other cases, we would only be going outside our object in cases when
7196 an original shift would have been undefined. */
7197 if (MEM_P (inner)
7198 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
7199 || (pos_rtx != 0 && len != 1)))
7200 return 0;
7201
7202 /* Get the mode to use should INNER not be a MEM, the mode for the position,
7203 and the mode for the result. */
7204 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
7205 {
7206 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
7207 pos_mode = mode_for_extraction (EP_insv, 2);
7208 extraction_mode = mode_for_extraction (EP_insv, 3);
7209 }
7210
7211 if (! in_dest && unsignedp
7212 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
7213 {
7214 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
7215 pos_mode = mode_for_extraction (EP_extzv, 3);
7216 extraction_mode = mode_for_extraction (EP_extzv, 0);
7217 }
7218
7219 if (! in_dest && ! unsignedp
7220 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
7221 {
7222 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
7223 pos_mode = mode_for_extraction (EP_extv, 3);
7224 extraction_mode = mode_for_extraction (EP_extv, 0);
7225 }
7226
7227 /* Never narrow an object, since that might not be safe. */
7228
7229 if (mode != VOIDmode
7230 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7231 extraction_mode = mode;
7232
7233 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
7234 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7235 pos_mode = GET_MODE (pos_rtx);
7236
7237 /* If this is not from memory, the desired mode is the preferred mode
7238 for an extraction pattern's first input operand, or word_mode if there
7239 is none. */
7240 if (!MEM_P (inner))
7241 wanted_inner_mode = wanted_inner_reg_mode;
7242 else
7243 {
7244 /* Be careful not to go beyond the extracted object and maintain the
7245 natural alignment of the memory. */
7246 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7247 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7248 > GET_MODE_BITSIZE (wanted_inner_mode))
7249 {
7250 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7251 gcc_assert (wanted_inner_mode != VOIDmode);
7252 }
7253
7254 /* If we have to change the mode of memory and cannot, the desired mode
7255 is EXTRACTION_MODE. */
7256 if (inner_mode != wanted_inner_mode
7257 && (mode_dependent_address_p (XEXP (inner, 0))
7258 || MEM_VOLATILE_P (inner)
7259 || pos_rtx))
7260 wanted_inner_mode = extraction_mode;
7261 }
7262
7263 orig_pos = pos;
7264
7265 if (BITS_BIG_ENDIAN)
7266 {
7267 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7268 BITS_BIG_ENDIAN style. If position is constant, compute new
7269 position. Otherwise, build subtraction.
7270 Note that POS is relative to the mode of the original argument.
7271 If it's a MEM we need to recompute POS relative to that.
7272 However, if we're extracting from (or inserting into) a register,
7273 we want to recompute POS relative to wanted_inner_mode. */
7274 int width = (MEM_P (inner)
7275 ? GET_MODE_BITSIZE (is_mode)
7276 : GET_MODE_BITSIZE (wanted_inner_mode));
7277
7278 if (pos_rtx == 0)
7279 pos = width - len - pos;
7280 else
7281 pos_rtx
7282 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
7283 /* POS may be less than 0 now, but we check for that below.
7284 Note that it can only be less than 0 if !MEM_P (inner). */
7285 }
7286
7287 /* If INNER has a wider mode, and this is a constant extraction, try to
7288 make it smaller and adjust the byte to point to the byte containing
7289 the value. */
7290 if (wanted_inner_mode != VOIDmode
7291 && inner_mode != wanted_inner_mode
7292 && ! pos_rtx
7293 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7294 && MEM_P (inner)
7295 && ! mode_dependent_address_p (XEXP (inner, 0))
7296 && ! MEM_VOLATILE_P (inner))
7297 {
7298 int offset = 0;
7299
7300 /* The computations below will be correct if the machine is big
7301 endian in both bits and bytes or little endian in bits and bytes.
7302 If it is mixed, we must adjust. */
7303
7304 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7305 adjust OFFSET to compensate. */
7306 if (BYTES_BIG_ENDIAN
7307 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7308 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7309
7310 /* We can now move to the desired byte. */
7311 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7312 * GET_MODE_SIZE (wanted_inner_mode);
7313 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7314
7315 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7316 && is_mode != wanted_inner_mode)
7317 offset = (GET_MODE_SIZE (is_mode)
7318 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7319
7320 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7321 }
7322
7323 /* If INNER is not memory, get it into the proper mode. If we are changing
7324 its mode, POS must be a constant and smaller than the size of the new
7325 mode. */
7326 else if (!MEM_P (inner))
7327 {
7328 /* On the LHS, don't create paradoxical subregs implicitely truncating
7329 the register unless TRULY_NOOP_TRUNCATION. */
7330 if (in_dest
7331 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (inner)),
7332 GET_MODE_BITSIZE (wanted_inner_mode)))
7333 return NULL_RTX;
7334
7335 if (GET_MODE (inner) != wanted_inner_mode
7336 && (pos_rtx != 0
7337 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7338 return NULL_RTX;
7339
7340 if (orig_pos < 0)
7341 return NULL_RTX;
7342
7343 inner = force_to_mode (inner, wanted_inner_mode,
7344 pos_rtx
7345 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7346 ? ~(unsigned HOST_WIDE_INT) 0
7347 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7348 << orig_pos),
7349 0);
7350 }
7351
7352 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7353 have to zero extend. Otherwise, we can just use a SUBREG. */
7354 if (pos_rtx != 0
7355 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7356 {
7357 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
7358
7359 /* If we know that no extraneous bits are set, and that the high
7360 bit is not set, convert extraction to cheaper one - either
7361 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7362 cases. */
7363 if (flag_expensive_optimizations
7364 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
7365 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7366 & ~(((unsigned HOST_WIDE_INT)
7367 GET_MODE_MASK (GET_MODE (pos_rtx)))
7368 >> 1))
7369 == 0)))
7370 {
7371 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
7372
7373 /* Prefer ZERO_EXTENSION, since it gives more information to
7374 backends. */
7375 if (rtx_cost (temp1, SET, optimize_this_for_speed_p)
7376 < rtx_cost (temp, SET, optimize_this_for_speed_p))
7377 temp = temp1;
7378 }
7379 pos_rtx = temp;
7380 }
7381 else if (pos_rtx != 0
7382 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7383 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
7384
7385 /* Make POS_RTX unless we already have it and it is correct. If we don't
7386 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7387 be a CONST_INT. */
7388 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7389 pos_rtx = orig_pos_rtx;
7390
7391 else if (pos_rtx == 0)
7392 pos_rtx = GEN_INT (pos);
7393
7394 /* Make the required operation. See if we can use existing rtx. */
7395 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7396 extraction_mode, inner, GEN_INT (len), pos_rtx);
7397 if (! in_dest)
7398 new_rtx = gen_lowpart (mode, new_rtx);
7399
7400 return new_rtx;
7401 }
7402 \f
7403 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7404 with any other operations in X. Return X without that shift if so. */
7405
7406 static rtx
7407 extract_left_shift (rtx x, int count)
7408 {
7409 enum rtx_code code = GET_CODE (x);
7410 enum machine_mode mode = GET_MODE (x);
7411 rtx tem;
7412
7413 switch (code)
7414 {
7415 case ASHIFT:
7416 /* This is the shift itself. If it is wide enough, we will return
7417 either the value being shifted if the shift count is equal to
7418 COUNT or a shift for the difference. */
7419 if (CONST_INT_P (XEXP (x, 1))
7420 && INTVAL (XEXP (x, 1)) >= count)
7421 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7422 INTVAL (XEXP (x, 1)) - count);
7423 break;
7424
7425 case NEG: case NOT:
7426 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7427 return simplify_gen_unary (code, mode, tem, mode);
7428
7429 break;
7430
7431 case PLUS: case IOR: case XOR: case AND:
7432 /* If we can safely shift this constant and we find the inner shift,
7433 make a new operation. */
7434 if (CONST_INT_P (XEXP (x, 1))
7435 && (UINTVAL (XEXP (x, 1))
7436 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7437 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7438 return simplify_gen_binary (code, mode, tem,
7439 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
7440
7441 break;
7442
7443 default:
7444 break;
7445 }
7446
7447 return 0;
7448 }
7449 \f
7450 /* Look at the expression rooted at X. Look for expressions
7451 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7452 Form these expressions.
7453
7454 Return the new rtx, usually just X.
7455
7456 Also, for machines like the VAX that don't have logical shift insns,
7457 try to convert logical to arithmetic shift operations in cases where
7458 they are equivalent. This undoes the canonicalizations to logical
7459 shifts done elsewhere.
7460
7461 We try, as much as possible, to re-use rtl expressions to save memory.
7462
7463 IN_CODE says what kind of expression we are processing. Normally, it is
7464 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7465 being kludges), it is MEM. When processing the arguments of a comparison
7466 or a COMPARE against zero, it is COMPARE. */
7467
7468 static rtx
7469 make_compound_operation (rtx x, enum rtx_code in_code)
7470 {
7471 enum rtx_code code = GET_CODE (x);
7472 enum machine_mode mode = GET_MODE (x);
7473 int mode_width = GET_MODE_BITSIZE (mode);
7474 rtx rhs, lhs;
7475 enum rtx_code next_code;
7476 int i, j;
7477 rtx new_rtx = 0;
7478 rtx tem;
7479 const char *fmt;
7480
7481 /* Select the code to be used in recursive calls. Once we are inside an
7482 address, we stay there. If we have a comparison, set to COMPARE,
7483 but once inside, go back to our default of SET. */
7484
7485 next_code = (code == MEM ? MEM
7486 : ((code == PLUS || code == MINUS)
7487 && SCALAR_INT_MODE_P (mode)) ? MEM
7488 : ((code == COMPARE || COMPARISON_P (x))
7489 && XEXP (x, 1) == const0_rtx) ? COMPARE
7490 : in_code == COMPARE ? SET : in_code);
7491
7492 /* Process depending on the code of this operation. If NEW is set
7493 nonzero, it will be returned. */
7494
7495 switch (code)
7496 {
7497 case ASHIFT:
7498 /* Convert shifts by constants into multiplications if inside
7499 an address. */
7500 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7501 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7502 && INTVAL (XEXP (x, 1)) >= 0
7503 && SCALAR_INT_MODE_P (mode))
7504 {
7505 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7506 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7507
7508 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7509 if (GET_CODE (new_rtx) == NEG)
7510 {
7511 new_rtx = XEXP (new_rtx, 0);
7512 multval = -multval;
7513 }
7514 multval = trunc_int_for_mode (multval, mode);
7515 new_rtx = gen_rtx_MULT (mode, new_rtx, GEN_INT (multval));
7516 }
7517 break;
7518
7519 case PLUS:
7520 lhs = XEXP (x, 0);
7521 rhs = XEXP (x, 1);
7522 lhs = make_compound_operation (lhs, next_code);
7523 rhs = make_compound_operation (rhs, next_code);
7524 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7525 && SCALAR_INT_MODE_P (mode))
7526 {
7527 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7528 XEXP (lhs, 1));
7529 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7530 }
7531 else if (GET_CODE (lhs) == MULT
7532 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7533 {
7534 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7535 simplify_gen_unary (NEG, mode,
7536 XEXP (lhs, 1),
7537 mode));
7538 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7539 }
7540 else
7541 {
7542 SUBST (XEXP (x, 0), lhs);
7543 SUBST (XEXP (x, 1), rhs);
7544 goto maybe_swap;
7545 }
7546 x = gen_lowpart (mode, new_rtx);
7547 goto maybe_swap;
7548
7549 case MINUS:
7550 lhs = XEXP (x, 0);
7551 rhs = XEXP (x, 1);
7552 lhs = make_compound_operation (lhs, next_code);
7553 rhs = make_compound_operation (rhs, next_code);
7554 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7555 && SCALAR_INT_MODE_P (mode))
7556 {
7557 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7558 XEXP (rhs, 1));
7559 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7560 }
7561 else if (GET_CODE (rhs) == MULT
7562 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7563 {
7564 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7565 simplify_gen_unary (NEG, mode,
7566 XEXP (rhs, 1),
7567 mode));
7568 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7569 }
7570 else
7571 {
7572 SUBST (XEXP (x, 0), lhs);
7573 SUBST (XEXP (x, 1), rhs);
7574 return x;
7575 }
7576 return gen_lowpart (mode, new_rtx);
7577
7578 case AND:
7579 /* If the second operand is not a constant, we can't do anything
7580 with it. */
7581 if (!CONST_INT_P (XEXP (x, 1)))
7582 break;
7583
7584 /* If the constant is a power of two minus one and the first operand
7585 is a logical right shift, make an extraction. */
7586 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7587 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7588 {
7589 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7590 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7591 0, in_code == COMPARE);
7592 }
7593
7594 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7595 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7596 && subreg_lowpart_p (XEXP (x, 0))
7597 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7598 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7599 {
7600 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7601 next_code);
7602 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7603 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7604 0, in_code == COMPARE);
7605 }
7606 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7607 else if ((GET_CODE (XEXP (x, 0)) == XOR
7608 || GET_CODE (XEXP (x, 0)) == IOR)
7609 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7610 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7611 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7612 {
7613 /* Apply the distributive law, and then try to make extractions. */
7614 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7615 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7616 XEXP (x, 1)),
7617 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7618 XEXP (x, 1)));
7619 new_rtx = make_compound_operation (new_rtx, in_code);
7620 }
7621
7622 /* If we are have (and (rotate X C) M) and C is larger than the number
7623 of bits in M, this is an extraction. */
7624
7625 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7626 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7627 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7628 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7629 {
7630 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7631 new_rtx = make_extraction (mode, new_rtx,
7632 (GET_MODE_BITSIZE (mode)
7633 - INTVAL (XEXP (XEXP (x, 0), 1))),
7634 NULL_RTX, i, 1, 0, in_code == COMPARE);
7635 }
7636
7637 /* On machines without logical shifts, if the operand of the AND is
7638 a logical shift and our mask turns off all the propagated sign
7639 bits, we can replace the logical shift with an arithmetic shift. */
7640 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7641 && !have_insn_for (LSHIFTRT, mode)
7642 && have_insn_for (ASHIFTRT, mode)
7643 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7644 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7645 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7646 && mode_width <= HOST_BITS_PER_WIDE_INT)
7647 {
7648 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7649
7650 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7651 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7652 SUBST (XEXP (x, 0),
7653 gen_rtx_ASHIFTRT (mode,
7654 make_compound_operation
7655 (XEXP (XEXP (x, 0), 0), next_code),
7656 XEXP (XEXP (x, 0), 1)));
7657 }
7658
7659 /* If the constant is one less than a power of two, this might be
7660 representable by an extraction even if no shift is present.
7661 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7662 we are in a COMPARE. */
7663 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7664 new_rtx = make_extraction (mode,
7665 make_compound_operation (XEXP (x, 0),
7666 next_code),
7667 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7668
7669 /* If we are in a comparison and this is an AND with a power of two,
7670 convert this into the appropriate bit extract. */
7671 else if (in_code == COMPARE
7672 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7673 new_rtx = make_extraction (mode,
7674 make_compound_operation (XEXP (x, 0),
7675 next_code),
7676 i, NULL_RTX, 1, 1, 0, 1);
7677
7678 break;
7679
7680 case LSHIFTRT:
7681 /* If the sign bit is known to be zero, replace this with an
7682 arithmetic shift. */
7683 if (have_insn_for (ASHIFTRT, mode)
7684 && ! have_insn_for (LSHIFTRT, mode)
7685 && mode_width <= HOST_BITS_PER_WIDE_INT
7686 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7687 {
7688 new_rtx = gen_rtx_ASHIFTRT (mode,
7689 make_compound_operation (XEXP (x, 0),
7690 next_code),
7691 XEXP (x, 1));
7692 break;
7693 }
7694
7695 /* ... fall through ... */
7696
7697 case ASHIFTRT:
7698 lhs = XEXP (x, 0);
7699 rhs = XEXP (x, 1);
7700
7701 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7702 this is a SIGN_EXTRACT. */
7703 if (CONST_INT_P (rhs)
7704 && GET_CODE (lhs) == ASHIFT
7705 && CONST_INT_P (XEXP (lhs, 1))
7706 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7707 && INTVAL (rhs) < mode_width)
7708 {
7709 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7710 new_rtx = make_extraction (mode, new_rtx,
7711 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7712 NULL_RTX, mode_width - INTVAL (rhs),
7713 code == LSHIFTRT, 0, in_code == COMPARE);
7714 break;
7715 }
7716
7717 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7718 If so, try to merge the shifts into a SIGN_EXTEND. We could
7719 also do this for some cases of SIGN_EXTRACT, but it doesn't
7720 seem worth the effort; the case checked for occurs on Alpha. */
7721
7722 if (!OBJECT_P (lhs)
7723 && ! (GET_CODE (lhs) == SUBREG
7724 && (OBJECT_P (SUBREG_REG (lhs))))
7725 && CONST_INT_P (rhs)
7726 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7727 && INTVAL (rhs) < mode_width
7728 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7729 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7730 0, NULL_RTX, mode_width - INTVAL (rhs),
7731 code == LSHIFTRT, 0, in_code == COMPARE);
7732
7733 break;
7734
7735 case SUBREG:
7736 /* Call ourselves recursively on the inner expression. If we are
7737 narrowing the object and it has a different RTL code from
7738 what it originally did, do this SUBREG as a force_to_mode. */
7739 {
7740 rtx inner = SUBREG_REG (x), simplified;
7741
7742 tem = make_compound_operation (inner, in_code);
7743
7744 simplified
7745 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7746 if (simplified)
7747 tem = simplified;
7748
7749 if (GET_CODE (tem) != GET_CODE (inner)
7750 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7751 && subreg_lowpart_p (x))
7752 {
7753 rtx newer
7754 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7755
7756 /* If we have something other than a SUBREG, we might have
7757 done an expansion, so rerun ourselves. */
7758 if (GET_CODE (newer) != SUBREG)
7759 newer = make_compound_operation (newer, in_code);
7760
7761 /* force_to_mode can expand compounds. If it just re-expanded the
7762 compound, use gen_lowpart to convert to the desired mode. */
7763 if (rtx_equal_p (newer, x)
7764 /* Likewise if it re-expanded the compound only partially.
7765 This happens for SUBREG of ZERO_EXTRACT if they extract
7766 the same number of bits. */
7767 || (GET_CODE (newer) == SUBREG
7768 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
7769 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
7770 && GET_CODE (inner) == AND
7771 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
7772 return gen_lowpart (GET_MODE (x), tem);
7773
7774 return newer;
7775 }
7776
7777 if (simplified)
7778 return tem;
7779 }
7780 break;
7781
7782 default:
7783 break;
7784 }
7785
7786 if (new_rtx)
7787 {
7788 x = gen_lowpart (mode, new_rtx);
7789 code = GET_CODE (x);
7790 }
7791
7792 /* Now recursively process each operand of this operation. */
7793 fmt = GET_RTX_FORMAT (code);
7794 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7795 if (fmt[i] == 'e')
7796 {
7797 new_rtx = make_compound_operation (XEXP (x, i), next_code);
7798 SUBST (XEXP (x, i), new_rtx);
7799 }
7800 else if (fmt[i] == 'E')
7801 for (j = 0; j < XVECLEN (x, i); j++)
7802 {
7803 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7804 SUBST (XVECEXP (x, i, j), new_rtx);
7805 }
7806
7807 maybe_swap:
7808 /* If this is a commutative operation, the changes to the operands
7809 may have made it noncanonical. */
7810 if (COMMUTATIVE_ARITH_P (x)
7811 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7812 {
7813 tem = XEXP (x, 0);
7814 SUBST (XEXP (x, 0), XEXP (x, 1));
7815 SUBST (XEXP (x, 1), tem);
7816 }
7817
7818 return x;
7819 }
7820 \f
7821 /* Given M see if it is a value that would select a field of bits
7822 within an item, but not the entire word. Return -1 if not.
7823 Otherwise, return the starting position of the field, where 0 is the
7824 low-order bit.
7825
7826 *PLEN is set to the length of the field. */
7827
7828 static int
7829 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7830 {
7831 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7832 int pos = m ? ctz_hwi (m) : -1;
7833 int len = 0;
7834
7835 if (pos >= 0)
7836 /* Now shift off the low-order zero bits and see if we have a
7837 power of two minus 1. */
7838 len = exact_log2 ((m >> pos) + 1);
7839
7840 if (len <= 0)
7841 pos = -1;
7842
7843 *plen = len;
7844 return pos;
7845 }
7846 \f
7847 /* If X refers to a register that equals REG in value, replace these
7848 references with REG. */
7849 static rtx
7850 canon_reg_for_combine (rtx x, rtx reg)
7851 {
7852 rtx op0, op1, op2;
7853 const char *fmt;
7854 int i;
7855 bool copied;
7856
7857 enum rtx_code code = GET_CODE (x);
7858 switch (GET_RTX_CLASS (code))
7859 {
7860 case RTX_UNARY:
7861 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7862 if (op0 != XEXP (x, 0))
7863 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7864 GET_MODE (reg));
7865 break;
7866
7867 case RTX_BIN_ARITH:
7868 case RTX_COMM_ARITH:
7869 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7870 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7871 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7872 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7873 break;
7874
7875 case RTX_COMPARE:
7876 case RTX_COMM_COMPARE:
7877 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7878 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7879 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7880 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7881 GET_MODE (op0), op0, op1);
7882 break;
7883
7884 case RTX_TERNARY:
7885 case RTX_BITFIELD_OPS:
7886 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7887 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7888 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7889 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7890 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7891 GET_MODE (op0), op0, op1, op2);
7892
7893 case RTX_OBJ:
7894 if (REG_P (x))
7895 {
7896 if (rtx_equal_p (get_last_value (reg), x)
7897 || rtx_equal_p (reg, get_last_value (x)))
7898 return reg;
7899 else
7900 break;
7901 }
7902
7903 /* fall through */
7904
7905 default:
7906 fmt = GET_RTX_FORMAT (code);
7907 copied = false;
7908 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7909 if (fmt[i] == 'e')
7910 {
7911 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7912 if (op != XEXP (x, i))
7913 {
7914 if (!copied)
7915 {
7916 copied = true;
7917 x = copy_rtx (x);
7918 }
7919 XEXP (x, i) = op;
7920 }
7921 }
7922 else if (fmt[i] == 'E')
7923 {
7924 int j;
7925 for (j = 0; j < XVECLEN (x, i); j++)
7926 {
7927 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7928 if (op != XVECEXP (x, i, j))
7929 {
7930 if (!copied)
7931 {
7932 copied = true;
7933 x = copy_rtx (x);
7934 }
7935 XVECEXP (x, i, j) = op;
7936 }
7937 }
7938 }
7939
7940 break;
7941 }
7942
7943 return x;
7944 }
7945
7946 /* Return X converted to MODE. If the value is already truncated to
7947 MODE we can just return a subreg even though in the general case we
7948 would need an explicit truncation. */
7949
7950 static rtx
7951 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
7952 {
7953 if (!CONST_INT_P (x)
7954 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
7955 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
7956 GET_MODE_BITSIZE (GET_MODE (x)))
7957 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
7958 {
7959 /* Bit-cast X into an integer mode. */
7960 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
7961 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
7962 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
7963 x, GET_MODE (x));
7964 }
7965
7966 return gen_lowpart (mode, x);
7967 }
7968
7969 /* See if X can be simplified knowing that we will only refer to it in
7970 MODE and will only refer to those bits that are nonzero in MASK.
7971 If other bits are being computed or if masking operations are done
7972 that select a superset of the bits in MASK, they can sometimes be
7973 ignored.
7974
7975 Return a possibly simplified expression, but always convert X to
7976 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
7977
7978 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7979 are all off in X. This is used when X will be complemented, by either
7980 NOT, NEG, or XOR. */
7981
7982 static rtx
7983 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
7984 int just_select)
7985 {
7986 enum rtx_code code = GET_CODE (x);
7987 int next_select = just_select || code == XOR || code == NOT || code == NEG;
7988 enum machine_mode op_mode;
7989 unsigned HOST_WIDE_INT fuller_mask, nonzero;
7990 rtx op0, op1, temp;
7991
7992 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
7993 code below will do the wrong thing since the mode of such an
7994 expression is VOIDmode.
7995
7996 Also do nothing if X is a CLOBBER; this can happen if X was
7997 the return value from a call to gen_lowpart. */
7998 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
7999 return x;
8000
8001 /* We want to perform the operation is its present mode unless we know
8002 that the operation is valid in MODE, in which case we do the operation
8003 in MODE. */
8004 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8005 && have_insn_for (code, mode))
8006 ? mode : GET_MODE (x));
8007
8008 /* It is not valid to do a right-shift in a narrower mode
8009 than the one it came in with. */
8010 if ((code == LSHIFTRT || code == ASHIFTRT)
8011 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
8012 op_mode = GET_MODE (x);
8013
8014 /* Truncate MASK to fit OP_MODE. */
8015 if (op_mode)
8016 mask &= GET_MODE_MASK (op_mode);
8017
8018 /* When we have an arithmetic operation, or a shift whose count we
8019 do not know, we need to assume that all bits up to the highest-order
8020 bit in MASK will be needed. This is how we form such a mask. */
8021 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8022 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8023 else
8024 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8025 - 1);
8026
8027 /* Determine what bits of X are guaranteed to be (non)zero. */
8028 nonzero = nonzero_bits (x, mode);
8029
8030 /* If none of the bits in X are needed, return a zero. */
8031 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8032 x = const0_rtx;
8033
8034 /* If X is a CONST_INT, return a new one. Do this here since the
8035 test below will fail. */
8036 if (CONST_INT_P (x))
8037 {
8038 if (SCALAR_INT_MODE_P (mode))
8039 return gen_int_mode (INTVAL (x) & mask, mode);
8040 else
8041 {
8042 x = GEN_INT (INTVAL (x) & mask);
8043 return gen_lowpart_common (mode, x);
8044 }
8045 }
8046
8047 /* If X is narrower than MODE and we want all the bits in X's mode, just
8048 get X in the proper mode. */
8049 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8050 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8051 return gen_lowpart (mode, x);
8052
8053 /* We can ignore the effect of a SUBREG if it narrows the mode or
8054 if the constant masks to zero all the bits the mode doesn't have. */
8055 if (GET_CODE (x) == SUBREG
8056 && subreg_lowpart_p (x)
8057 && ((GET_MODE_SIZE (GET_MODE (x))
8058 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8059 || (0 == (mask
8060 & GET_MODE_MASK (GET_MODE (x))
8061 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8062 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8063
8064 /* The arithmetic simplifications here only work for scalar integer modes. */
8065 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8066 return gen_lowpart_or_truncate (mode, x);
8067
8068 switch (code)
8069 {
8070 case CLOBBER:
8071 /* If X is a (clobber (const_int)), return it since we know we are
8072 generating something that won't match. */
8073 return x;
8074
8075 case SIGN_EXTEND:
8076 case ZERO_EXTEND:
8077 case ZERO_EXTRACT:
8078 case SIGN_EXTRACT:
8079 x = expand_compound_operation (x);
8080 if (GET_CODE (x) != code)
8081 return force_to_mode (x, mode, mask, next_select);
8082 break;
8083
8084 case TRUNCATE:
8085 /* Similarly for a truncate. */
8086 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8087
8088 case AND:
8089 /* If this is an AND with a constant, convert it into an AND
8090 whose constant is the AND of that constant with MASK. If it
8091 remains an AND of MASK, delete it since it is redundant. */
8092
8093 if (CONST_INT_P (XEXP (x, 1)))
8094 {
8095 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8096 mask & INTVAL (XEXP (x, 1)));
8097
8098 /* If X is still an AND, see if it is an AND with a mask that
8099 is just some low-order bits. If so, and it is MASK, we don't
8100 need it. */
8101
8102 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8103 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8104 == mask))
8105 x = XEXP (x, 0);
8106
8107 /* If it remains an AND, try making another AND with the bits
8108 in the mode mask that aren't in MASK turned on. If the
8109 constant in the AND is wide enough, this might make a
8110 cheaper constant. */
8111
8112 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8113 && GET_MODE_MASK (GET_MODE (x)) != mask
8114 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
8115 {
8116 unsigned HOST_WIDE_INT cval
8117 = UINTVAL (XEXP (x, 1))
8118 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8119 int width = GET_MODE_BITSIZE (GET_MODE (x));
8120 rtx y;
8121
8122 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
8123 number, sign extend it. */
8124 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
8125 && (cval & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8126 cval |= (unsigned HOST_WIDE_INT) -1 << width;
8127
8128 y = simplify_gen_binary (AND, GET_MODE (x),
8129 XEXP (x, 0), GEN_INT (cval));
8130 if (rtx_cost (y, SET, optimize_this_for_speed_p)
8131 < rtx_cost (x, SET, optimize_this_for_speed_p))
8132 x = y;
8133 }
8134
8135 break;
8136 }
8137
8138 goto binop;
8139
8140 case PLUS:
8141 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8142 low-order bits (as in an alignment operation) and FOO is already
8143 aligned to that boundary, mask C1 to that boundary as well.
8144 This may eliminate that PLUS and, later, the AND. */
8145
8146 {
8147 unsigned int width = GET_MODE_BITSIZE (mode);
8148 unsigned HOST_WIDE_INT smask = mask;
8149
8150 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8151 number, sign extend it. */
8152
8153 if (width < HOST_BITS_PER_WIDE_INT
8154 && (smask & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8155 smask |= (unsigned HOST_WIDE_INT) (-1) << width;
8156
8157 if (CONST_INT_P (XEXP (x, 1))
8158 && exact_log2 (- smask) >= 0
8159 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8160 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8161 return force_to_mode (plus_constant (XEXP (x, 0),
8162 (INTVAL (XEXP (x, 1)) & smask)),
8163 mode, smask, next_select);
8164 }
8165
8166 /* ... fall through ... */
8167
8168 case MULT:
8169 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8170 most significant bit in MASK since carries from those bits will
8171 affect the bits we are interested in. */
8172 mask = fuller_mask;
8173 goto binop;
8174
8175 case MINUS:
8176 /* If X is (minus C Y) where C's least set bit is larger than any bit
8177 in the mask, then we may replace with (neg Y). */
8178 if (CONST_INT_P (XEXP (x, 0))
8179 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
8180 & -INTVAL (XEXP (x, 0))))
8181 > mask))
8182 {
8183 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8184 GET_MODE (x));
8185 return force_to_mode (x, mode, mask, next_select);
8186 }
8187
8188 /* Similarly, if C contains every bit in the fuller_mask, then we may
8189 replace with (not Y). */
8190 if (CONST_INT_P (XEXP (x, 0))
8191 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8192 {
8193 x = simplify_gen_unary (NOT, GET_MODE (x),
8194 XEXP (x, 1), GET_MODE (x));
8195 return force_to_mode (x, mode, mask, next_select);
8196 }
8197
8198 mask = fuller_mask;
8199 goto binop;
8200
8201 case IOR:
8202 case XOR:
8203 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8204 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8205 operation which may be a bitfield extraction. Ensure that the
8206 constant we form is not wider than the mode of X. */
8207
8208 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8209 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8210 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8211 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8212 && CONST_INT_P (XEXP (x, 1))
8213 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8214 + floor_log2 (INTVAL (XEXP (x, 1))))
8215 < GET_MODE_BITSIZE (GET_MODE (x)))
8216 && (UINTVAL (XEXP (x, 1))
8217 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8218 {
8219 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
8220 << INTVAL (XEXP (XEXP (x, 0), 1)));
8221 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8222 XEXP (XEXP (x, 0), 0), temp);
8223 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8224 XEXP (XEXP (x, 0), 1));
8225 return force_to_mode (x, mode, mask, next_select);
8226 }
8227
8228 binop:
8229 /* For most binary operations, just propagate into the operation and
8230 change the mode if we have an operation of that mode. */
8231
8232 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8233 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8234
8235 /* If we ended up truncating both operands, truncate the result of the
8236 operation instead. */
8237 if (GET_CODE (op0) == TRUNCATE
8238 && GET_CODE (op1) == TRUNCATE)
8239 {
8240 op0 = XEXP (op0, 0);
8241 op1 = XEXP (op1, 0);
8242 }
8243
8244 op0 = gen_lowpart_or_truncate (op_mode, op0);
8245 op1 = gen_lowpart_or_truncate (op_mode, op1);
8246
8247 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8248 x = simplify_gen_binary (code, op_mode, op0, op1);
8249 break;
8250
8251 case ASHIFT:
8252 /* For left shifts, do the same, but just for the first operand.
8253 However, we cannot do anything with shifts where we cannot
8254 guarantee that the counts are smaller than the size of the mode
8255 because such a count will have a different meaning in a
8256 wider mode. */
8257
8258 if (! (CONST_INT_P (XEXP (x, 1))
8259 && INTVAL (XEXP (x, 1)) >= 0
8260 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
8261 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8262 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8263 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
8264 break;
8265
8266 /* If the shift count is a constant and we can do arithmetic in
8267 the mode of the shift, refine which bits we need. Otherwise, use the
8268 conservative form of the mask. */
8269 if (CONST_INT_P (XEXP (x, 1))
8270 && INTVAL (XEXP (x, 1)) >= 0
8271 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
8272 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
8273 mask >>= INTVAL (XEXP (x, 1));
8274 else
8275 mask = fuller_mask;
8276
8277 op0 = gen_lowpart_or_truncate (op_mode,
8278 force_to_mode (XEXP (x, 0), op_mode,
8279 mask, next_select));
8280
8281 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8282 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8283 break;
8284
8285 case LSHIFTRT:
8286 /* Here we can only do something if the shift count is a constant,
8287 this shift constant is valid for the host, and we can do arithmetic
8288 in OP_MODE. */
8289
8290 if (CONST_INT_P (XEXP (x, 1))
8291 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8292 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
8293 {
8294 rtx inner = XEXP (x, 0);
8295 unsigned HOST_WIDE_INT inner_mask;
8296
8297 /* Select the mask of the bits we need for the shift operand. */
8298 inner_mask = mask << INTVAL (XEXP (x, 1));
8299
8300 /* We can only change the mode of the shift if we can do arithmetic
8301 in the mode of the shift and INNER_MASK is no wider than the
8302 width of X's mode. */
8303 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8304 op_mode = GET_MODE (x);
8305
8306 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8307
8308 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8309 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8310 }
8311
8312 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8313 shift and AND produces only copies of the sign bit (C2 is one less
8314 than a power of two), we can do this with just a shift. */
8315
8316 if (GET_CODE (x) == LSHIFTRT
8317 && CONST_INT_P (XEXP (x, 1))
8318 /* The shift puts one of the sign bit copies in the least significant
8319 bit. */
8320 && ((INTVAL (XEXP (x, 1))
8321 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8322 >= GET_MODE_BITSIZE (GET_MODE (x)))
8323 && exact_log2 (mask + 1) >= 0
8324 /* Number of bits left after the shift must be more than the mask
8325 needs. */
8326 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8327 <= GET_MODE_BITSIZE (GET_MODE (x)))
8328 /* Must be more sign bit copies than the mask needs. */
8329 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8330 >= exact_log2 (mask + 1)))
8331 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8332 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
8333 - exact_log2 (mask + 1)));
8334
8335 goto shiftrt;
8336
8337 case ASHIFTRT:
8338 /* If we are just looking for the sign bit, we don't need this shift at
8339 all, even if it has a variable count. */
8340 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
8341 && (mask == ((unsigned HOST_WIDE_INT) 1
8342 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8343 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8344
8345 /* If this is a shift by a constant, get a mask that contains those bits
8346 that are not copies of the sign bit. We then have two cases: If
8347 MASK only includes those bits, this can be a logical shift, which may
8348 allow simplifications. If MASK is a single-bit field not within
8349 those bits, we are requesting a copy of the sign bit and hence can
8350 shift the sign bit to the appropriate location. */
8351
8352 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8353 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8354 {
8355 int i;
8356
8357 /* If the considered data is wider than HOST_WIDE_INT, we can't
8358 represent a mask for all its bits in a single scalar.
8359 But we only care about the lower bits, so calculate these. */
8360
8361 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8362 {
8363 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8364
8365 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8366 is the number of bits a full-width mask would have set.
8367 We need only shift if these are fewer than nonzero can
8368 hold. If not, we must keep all bits set in nonzero. */
8369
8370 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8371 < HOST_BITS_PER_WIDE_INT)
8372 nonzero >>= INTVAL (XEXP (x, 1))
8373 + HOST_BITS_PER_WIDE_INT
8374 - GET_MODE_BITSIZE (GET_MODE (x)) ;
8375 }
8376 else
8377 {
8378 nonzero = GET_MODE_MASK (GET_MODE (x));
8379 nonzero >>= INTVAL (XEXP (x, 1));
8380 }
8381
8382 if ((mask & ~nonzero) == 0)
8383 {
8384 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8385 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8386 if (GET_CODE (x) != ASHIFTRT)
8387 return force_to_mode (x, mode, mask, next_select);
8388 }
8389
8390 else if ((i = exact_log2 (mask)) >= 0)
8391 {
8392 x = simplify_shift_const
8393 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8394 GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
8395
8396 if (GET_CODE (x) != ASHIFTRT)
8397 return force_to_mode (x, mode, mask, next_select);
8398 }
8399 }
8400
8401 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8402 even if the shift count isn't a constant. */
8403 if (mask == 1)
8404 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8405 XEXP (x, 0), XEXP (x, 1));
8406
8407 shiftrt:
8408
8409 /* If this is a zero- or sign-extension operation that just affects bits
8410 we don't care about, remove it. Be sure the call above returned
8411 something that is still a shift. */
8412
8413 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8414 && CONST_INT_P (XEXP (x, 1))
8415 && INTVAL (XEXP (x, 1)) >= 0
8416 && (INTVAL (XEXP (x, 1))
8417 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
8418 && GET_CODE (XEXP (x, 0)) == ASHIFT
8419 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8420 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8421 next_select);
8422
8423 break;
8424
8425 case ROTATE:
8426 case ROTATERT:
8427 /* If the shift count is constant and we can do computations
8428 in the mode of X, compute where the bits we care about are.
8429 Otherwise, we can't do anything. Don't change the mode of
8430 the shift or propagate MODE into the shift, though. */
8431 if (CONST_INT_P (XEXP (x, 1))
8432 && INTVAL (XEXP (x, 1)) >= 0)
8433 {
8434 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8435 GET_MODE (x), GEN_INT (mask),
8436 XEXP (x, 1));
8437 if (temp && CONST_INT_P (temp))
8438 SUBST (XEXP (x, 0),
8439 force_to_mode (XEXP (x, 0), GET_MODE (x),
8440 INTVAL (temp), next_select));
8441 }
8442 break;
8443
8444 case NEG:
8445 /* If we just want the low-order bit, the NEG isn't needed since it
8446 won't change the low-order bit. */
8447 if (mask == 1)
8448 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8449
8450 /* We need any bits less significant than the most significant bit in
8451 MASK since carries from those bits will affect the bits we are
8452 interested in. */
8453 mask = fuller_mask;
8454 goto unop;
8455
8456 case NOT:
8457 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8458 same as the XOR case above. Ensure that the constant we form is not
8459 wider than the mode of X. */
8460
8461 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8462 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8463 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8464 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8465 < GET_MODE_BITSIZE (GET_MODE (x)))
8466 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8467 {
8468 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8469 GET_MODE (x));
8470 temp = simplify_gen_binary (XOR, GET_MODE (x),
8471 XEXP (XEXP (x, 0), 0), temp);
8472 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8473 temp, XEXP (XEXP (x, 0), 1));
8474
8475 return force_to_mode (x, mode, mask, next_select);
8476 }
8477
8478 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8479 use the full mask inside the NOT. */
8480 mask = fuller_mask;
8481
8482 unop:
8483 op0 = gen_lowpart_or_truncate (op_mode,
8484 force_to_mode (XEXP (x, 0), mode, mask,
8485 next_select));
8486 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8487 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8488 break;
8489
8490 case NE:
8491 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8492 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8493 which is equal to STORE_FLAG_VALUE. */
8494 if ((mask & ~STORE_FLAG_VALUE) == 0
8495 && XEXP (x, 1) == const0_rtx
8496 && GET_MODE (XEXP (x, 0)) == mode
8497 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8498 && (nonzero_bits (XEXP (x, 0), mode)
8499 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8500 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8501
8502 break;
8503
8504 case IF_THEN_ELSE:
8505 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8506 written in a narrower mode. We play it safe and do not do so. */
8507
8508 SUBST (XEXP (x, 1),
8509 gen_lowpart_or_truncate (GET_MODE (x),
8510 force_to_mode (XEXP (x, 1), mode,
8511 mask, next_select)));
8512 SUBST (XEXP (x, 2),
8513 gen_lowpart_or_truncate (GET_MODE (x),
8514 force_to_mode (XEXP (x, 2), mode,
8515 mask, next_select)));
8516 break;
8517
8518 default:
8519 break;
8520 }
8521
8522 /* Ensure we return a value of the proper mode. */
8523 return gen_lowpart_or_truncate (mode, x);
8524 }
8525 \f
8526 /* Return nonzero if X is an expression that has one of two values depending on
8527 whether some other value is zero or nonzero. In that case, we return the
8528 value that is being tested, *PTRUE is set to the value if the rtx being
8529 returned has a nonzero value, and *PFALSE is set to the other alternative.
8530
8531 If we return zero, we set *PTRUE and *PFALSE to X. */
8532
8533 static rtx
8534 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8535 {
8536 enum machine_mode mode = GET_MODE (x);
8537 enum rtx_code code = GET_CODE (x);
8538 rtx cond0, cond1, true0, true1, false0, false1;
8539 unsigned HOST_WIDE_INT nz;
8540
8541 /* If we are comparing a value against zero, we are done. */
8542 if ((code == NE || code == EQ)
8543 && XEXP (x, 1) == const0_rtx)
8544 {
8545 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8546 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8547 return XEXP (x, 0);
8548 }
8549
8550 /* If this is a unary operation whose operand has one of two values, apply
8551 our opcode to compute those values. */
8552 else if (UNARY_P (x)
8553 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8554 {
8555 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8556 *pfalse = simplify_gen_unary (code, mode, false0,
8557 GET_MODE (XEXP (x, 0)));
8558 return cond0;
8559 }
8560
8561 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8562 make can't possibly match and would suppress other optimizations. */
8563 else if (code == COMPARE)
8564 ;
8565
8566 /* If this is a binary operation, see if either side has only one of two
8567 values. If either one does or if both do and they are conditional on
8568 the same value, compute the new true and false values. */
8569 else if (BINARY_P (x))
8570 {
8571 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8572 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8573
8574 if ((cond0 != 0 || cond1 != 0)
8575 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8576 {
8577 /* If if_then_else_cond returned zero, then true/false are the
8578 same rtl. We must copy one of them to prevent invalid rtl
8579 sharing. */
8580 if (cond0 == 0)
8581 true0 = copy_rtx (true0);
8582 else if (cond1 == 0)
8583 true1 = copy_rtx (true1);
8584
8585 if (COMPARISON_P (x))
8586 {
8587 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8588 true0, true1);
8589 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8590 false0, false1);
8591 }
8592 else
8593 {
8594 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8595 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8596 }
8597
8598 return cond0 ? cond0 : cond1;
8599 }
8600
8601 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8602 operands is zero when the other is nonzero, and vice-versa,
8603 and STORE_FLAG_VALUE is 1 or -1. */
8604
8605 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8606 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8607 || code == UMAX)
8608 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8609 {
8610 rtx op0 = XEXP (XEXP (x, 0), 1);
8611 rtx op1 = XEXP (XEXP (x, 1), 1);
8612
8613 cond0 = XEXP (XEXP (x, 0), 0);
8614 cond1 = XEXP (XEXP (x, 1), 0);
8615
8616 if (COMPARISON_P (cond0)
8617 && COMPARISON_P (cond1)
8618 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8619 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8620 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8621 || ((swap_condition (GET_CODE (cond0))
8622 == reversed_comparison_code (cond1, NULL))
8623 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8624 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8625 && ! side_effects_p (x))
8626 {
8627 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8628 *pfalse = simplify_gen_binary (MULT, mode,
8629 (code == MINUS
8630 ? simplify_gen_unary (NEG, mode,
8631 op1, mode)
8632 : op1),
8633 const_true_rtx);
8634 return cond0;
8635 }
8636 }
8637
8638 /* Similarly for MULT, AND and UMIN, except that for these the result
8639 is always zero. */
8640 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8641 && (code == MULT || code == AND || code == UMIN)
8642 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8643 {
8644 cond0 = XEXP (XEXP (x, 0), 0);
8645 cond1 = XEXP (XEXP (x, 1), 0);
8646
8647 if (COMPARISON_P (cond0)
8648 && COMPARISON_P (cond1)
8649 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8650 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8651 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8652 || ((swap_condition (GET_CODE (cond0))
8653 == reversed_comparison_code (cond1, NULL))
8654 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8655 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8656 && ! side_effects_p (x))
8657 {
8658 *ptrue = *pfalse = const0_rtx;
8659 return cond0;
8660 }
8661 }
8662 }
8663
8664 else if (code == IF_THEN_ELSE)
8665 {
8666 /* If we have IF_THEN_ELSE already, extract the condition and
8667 canonicalize it if it is NE or EQ. */
8668 cond0 = XEXP (x, 0);
8669 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8670 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8671 return XEXP (cond0, 0);
8672 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8673 {
8674 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8675 return XEXP (cond0, 0);
8676 }
8677 else
8678 return cond0;
8679 }
8680
8681 /* If X is a SUBREG, we can narrow both the true and false values
8682 if the inner expression, if there is a condition. */
8683 else if (code == SUBREG
8684 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8685 &true0, &false0)))
8686 {
8687 true0 = simplify_gen_subreg (mode, true0,
8688 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8689 false0 = simplify_gen_subreg (mode, false0,
8690 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8691 if (true0 && false0)
8692 {
8693 *ptrue = true0;
8694 *pfalse = false0;
8695 return cond0;
8696 }
8697 }
8698
8699 /* If X is a constant, this isn't special and will cause confusions
8700 if we treat it as such. Likewise if it is equivalent to a constant. */
8701 else if (CONSTANT_P (x)
8702 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8703 ;
8704
8705 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8706 will be least confusing to the rest of the compiler. */
8707 else if (mode == BImode)
8708 {
8709 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8710 return x;
8711 }
8712
8713 /* If X is known to be either 0 or -1, those are the true and
8714 false values when testing X. */
8715 else if (x == constm1_rtx || x == const0_rtx
8716 || (mode != VOIDmode
8717 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
8718 {
8719 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8720 return x;
8721 }
8722
8723 /* Likewise for 0 or a single bit. */
8724 else if (SCALAR_INT_MODE_P (mode)
8725 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8726 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8727 {
8728 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8729 return x;
8730 }
8731
8732 /* Otherwise fail; show no condition with true and false values the same. */
8733 *ptrue = *pfalse = x;
8734 return 0;
8735 }
8736 \f
8737 /* Return the value of expression X given the fact that condition COND
8738 is known to be true when applied to REG as its first operand and VAL
8739 as its second. X is known to not be shared and so can be modified in
8740 place.
8741
8742 We only handle the simplest cases, and specifically those cases that
8743 arise with IF_THEN_ELSE expressions. */
8744
8745 static rtx
8746 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8747 {
8748 enum rtx_code code = GET_CODE (x);
8749 rtx temp;
8750 const char *fmt;
8751 int i, j;
8752
8753 if (side_effects_p (x))
8754 return x;
8755
8756 /* If either operand of the condition is a floating point value,
8757 then we have to avoid collapsing an EQ comparison. */
8758 if (cond == EQ
8759 && rtx_equal_p (x, reg)
8760 && ! FLOAT_MODE_P (GET_MODE (x))
8761 && ! FLOAT_MODE_P (GET_MODE (val)))
8762 return val;
8763
8764 if (cond == UNEQ && rtx_equal_p (x, reg))
8765 return val;
8766
8767 /* If X is (abs REG) and we know something about REG's relationship
8768 with zero, we may be able to simplify this. */
8769
8770 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8771 switch (cond)
8772 {
8773 case GE: case GT: case EQ:
8774 return XEXP (x, 0);
8775 case LT: case LE:
8776 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8777 XEXP (x, 0),
8778 GET_MODE (XEXP (x, 0)));
8779 default:
8780 break;
8781 }
8782
8783 /* The only other cases we handle are MIN, MAX, and comparisons if the
8784 operands are the same as REG and VAL. */
8785
8786 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8787 {
8788 if (rtx_equal_p (XEXP (x, 0), val))
8789 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8790
8791 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8792 {
8793 if (COMPARISON_P (x))
8794 {
8795 if (comparison_dominates_p (cond, code))
8796 return const_true_rtx;
8797
8798 code = reversed_comparison_code (x, NULL);
8799 if (code != UNKNOWN
8800 && comparison_dominates_p (cond, code))
8801 return const0_rtx;
8802 else
8803 return x;
8804 }
8805 else if (code == SMAX || code == SMIN
8806 || code == UMIN || code == UMAX)
8807 {
8808 int unsignedp = (code == UMIN || code == UMAX);
8809
8810 /* Do not reverse the condition when it is NE or EQ.
8811 This is because we cannot conclude anything about
8812 the value of 'SMAX (x, y)' when x is not equal to y,
8813 but we can when x equals y. */
8814 if ((code == SMAX || code == UMAX)
8815 && ! (cond == EQ || cond == NE))
8816 cond = reverse_condition (cond);
8817
8818 switch (cond)
8819 {
8820 case GE: case GT:
8821 return unsignedp ? x : XEXP (x, 1);
8822 case LE: case LT:
8823 return unsignedp ? x : XEXP (x, 0);
8824 case GEU: case GTU:
8825 return unsignedp ? XEXP (x, 1) : x;
8826 case LEU: case LTU:
8827 return unsignedp ? XEXP (x, 0) : x;
8828 default:
8829 break;
8830 }
8831 }
8832 }
8833 }
8834 else if (code == SUBREG)
8835 {
8836 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8837 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8838
8839 if (SUBREG_REG (x) != r)
8840 {
8841 /* We must simplify subreg here, before we lose track of the
8842 original inner_mode. */
8843 new_rtx = simplify_subreg (GET_MODE (x), r,
8844 inner_mode, SUBREG_BYTE (x));
8845 if (new_rtx)
8846 return new_rtx;
8847 else
8848 SUBST (SUBREG_REG (x), r);
8849 }
8850
8851 return x;
8852 }
8853 /* We don't have to handle SIGN_EXTEND here, because even in the
8854 case of replacing something with a modeless CONST_INT, a
8855 CONST_INT is already (supposed to be) a valid sign extension for
8856 its narrower mode, which implies it's already properly
8857 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8858 story is different. */
8859 else if (code == ZERO_EXTEND)
8860 {
8861 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8862 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8863
8864 if (XEXP (x, 0) != r)
8865 {
8866 /* We must simplify the zero_extend here, before we lose
8867 track of the original inner_mode. */
8868 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8869 r, inner_mode);
8870 if (new_rtx)
8871 return new_rtx;
8872 else
8873 SUBST (XEXP (x, 0), r);
8874 }
8875
8876 return x;
8877 }
8878
8879 fmt = GET_RTX_FORMAT (code);
8880 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8881 {
8882 if (fmt[i] == 'e')
8883 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8884 else if (fmt[i] == 'E')
8885 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8886 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8887 cond, reg, val));
8888 }
8889
8890 return x;
8891 }
8892 \f
8893 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8894 assignment as a field assignment. */
8895
8896 static int
8897 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8898 {
8899 if (x == y || rtx_equal_p (x, y))
8900 return 1;
8901
8902 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8903 return 0;
8904
8905 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8906 Note that all SUBREGs of MEM are paradoxical; otherwise they
8907 would have been rewritten. */
8908 if (MEM_P (x) && GET_CODE (y) == SUBREG
8909 && MEM_P (SUBREG_REG (y))
8910 && rtx_equal_p (SUBREG_REG (y),
8911 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8912 return 1;
8913
8914 if (MEM_P (y) && GET_CODE (x) == SUBREG
8915 && MEM_P (SUBREG_REG (x))
8916 && rtx_equal_p (SUBREG_REG (x),
8917 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8918 return 1;
8919
8920 /* We used to see if get_last_value of X and Y were the same but that's
8921 not correct. In one direction, we'll cause the assignment to have
8922 the wrong destination and in the case, we'll import a register into this
8923 insn that might have already have been dead. So fail if none of the
8924 above cases are true. */
8925 return 0;
8926 }
8927 \f
8928 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8929 Return that assignment if so.
8930
8931 We only handle the most common cases. */
8932
8933 static rtx
8934 make_field_assignment (rtx x)
8935 {
8936 rtx dest = SET_DEST (x);
8937 rtx src = SET_SRC (x);
8938 rtx assign;
8939 rtx rhs, lhs;
8940 HOST_WIDE_INT c1;
8941 HOST_WIDE_INT pos;
8942 unsigned HOST_WIDE_INT len;
8943 rtx other;
8944 enum machine_mode mode;
8945
8946 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8947 a clear of a one-bit field. We will have changed it to
8948 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
8949 for a SUBREG. */
8950
8951 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8952 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
8953 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8954 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8955 {
8956 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8957 1, 1, 1, 0);
8958 if (assign != 0)
8959 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8960 return x;
8961 }
8962
8963 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
8964 && subreg_lowpart_p (XEXP (src, 0))
8965 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
8966 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
8967 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
8968 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
8969 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
8970 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8971 {
8972 assign = make_extraction (VOIDmode, dest, 0,
8973 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
8974 1, 1, 1, 0);
8975 if (assign != 0)
8976 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8977 return x;
8978 }
8979
8980 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8981 one-bit field. */
8982 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
8983 && XEXP (XEXP (src, 0), 0) == const1_rtx
8984 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8985 {
8986 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8987 1, 1, 1, 0);
8988 if (assign != 0)
8989 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
8990 return x;
8991 }
8992
8993 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8994 SRC is an AND with all bits of that field set, then we can discard
8995 the AND. */
8996 if (GET_CODE (dest) == ZERO_EXTRACT
8997 && CONST_INT_P (XEXP (dest, 1))
8998 && GET_CODE (src) == AND
8999 && CONST_INT_P (XEXP (src, 1)))
9000 {
9001 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9002 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9003 unsigned HOST_WIDE_INT ze_mask;
9004
9005 if (width >= HOST_BITS_PER_WIDE_INT)
9006 ze_mask = -1;
9007 else
9008 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9009
9010 /* Complete overlap. We can remove the source AND. */
9011 if ((and_mask & ze_mask) == ze_mask)
9012 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9013
9014 /* Partial overlap. We can reduce the source AND. */
9015 if ((and_mask & ze_mask) != and_mask)
9016 {
9017 mode = GET_MODE (src);
9018 src = gen_rtx_AND (mode, XEXP (src, 0),
9019 gen_int_mode (and_mask & ze_mask, mode));
9020 return gen_rtx_SET (VOIDmode, dest, src);
9021 }
9022 }
9023
9024 /* The other case we handle is assignments into a constant-position
9025 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9026 a mask that has all one bits except for a group of zero bits and
9027 OTHER is known to have zeros where C1 has ones, this is such an
9028 assignment. Compute the position and length from C1. Shift OTHER
9029 to the appropriate position, force it to the required mode, and
9030 make the extraction. Check for the AND in both operands. */
9031
9032 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9033 return x;
9034
9035 rhs = expand_compound_operation (XEXP (src, 0));
9036 lhs = expand_compound_operation (XEXP (src, 1));
9037
9038 if (GET_CODE (rhs) == AND
9039 && CONST_INT_P (XEXP (rhs, 1))
9040 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9041 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9042 else if (GET_CODE (lhs) == AND
9043 && CONST_INT_P (XEXP (lhs, 1))
9044 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9045 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9046 else
9047 return x;
9048
9049 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9050 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
9051 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9052 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9053 return x;
9054
9055 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9056 if (assign == 0)
9057 return x;
9058
9059 /* The mode to use for the source is the mode of the assignment, or of
9060 what is inside a possible STRICT_LOW_PART. */
9061 mode = (GET_CODE (assign) == STRICT_LOW_PART
9062 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9063
9064 /* Shift OTHER right POS places and make it the source, restricting it
9065 to the proper length and mode. */
9066
9067 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9068 GET_MODE (src),
9069 other, pos),
9070 dest);
9071 src = force_to_mode (src, mode,
9072 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
9073 ? ~(unsigned HOST_WIDE_INT) 0
9074 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9075 0);
9076
9077 /* If SRC is masked by an AND that does not make a difference in
9078 the value being stored, strip it. */
9079 if (GET_CODE (assign) == ZERO_EXTRACT
9080 && CONST_INT_P (XEXP (assign, 1))
9081 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9082 && GET_CODE (src) == AND
9083 && CONST_INT_P (XEXP (src, 1))
9084 && UINTVAL (XEXP (src, 1))
9085 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9086 src = XEXP (src, 0);
9087
9088 return gen_rtx_SET (VOIDmode, assign, src);
9089 }
9090 \f
9091 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9092 if so. */
9093
9094 static rtx
9095 apply_distributive_law (rtx x)
9096 {
9097 enum rtx_code code = GET_CODE (x);
9098 enum rtx_code inner_code;
9099 rtx lhs, rhs, other;
9100 rtx tem;
9101
9102 /* Distributivity is not true for floating point as it can change the
9103 value. So we don't do it unless -funsafe-math-optimizations. */
9104 if (FLOAT_MODE_P (GET_MODE (x))
9105 && ! flag_unsafe_math_optimizations)
9106 return x;
9107
9108 /* The outer operation can only be one of the following: */
9109 if (code != IOR && code != AND && code != XOR
9110 && code != PLUS && code != MINUS)
9111 return x;
9112
9113 lhs = XEXP (x, 0);
9114 rhs = XEXP (x, 1);
9115
9116 /* If either operand is a primitive we can't do anything, so get out
9117 fast. */
9118 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9119 return x;
9120
9121 lhs = expand_compound_operation (lhs);
9122 rhs = expand_compound_operation (rhs);
9123 inner_code = GET_CODE (lhs);
9124 if (inner_code != GET_CODE (rhs))
9125 return x;
9126
9127 /* See if the inner and outer operations distribute. */
9128 switch (inner_code)
9129 {
9130 case LSHIFTRT:
9131 case ASHIFTRT:
9132 case AND:
9133 case IOR:
9134 /* These all distribute except over PLUS. */
9135 if (code == PLUS || code == MINUS)
9136 return x;
9137 break;
9138
9139 case MULT:
9140 if (code != PLUS && code != MINUS)
9141 return x;
9142 break;
9143
9144 case ASHIFT:
9145 /* This is also a multiply, so it distributes over everything. */
9146 break;
9147
9148 case SUBREG:
9149 /* Non-paradoxical SUBREGs distributes over all operations,
9150 provided the inner modes and byte offsets are the same, this
9151 is an extraction of a low-order part, we don't convert an fp
9152 operation to int or vice versa, this is not a vector mode,
9153 and we would not be converting a single-word operation into a
9154 multi-word operation. The latter test is not required, but
9155 it prevents generating unneeded multi-word operations. Some
9156 of the previous tests are redundant given the latter test,
9157 but are retained because they are required for correctness.
9158
9159 We produce the result slightly differently in this case. */
9160
9161 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
9162 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
9163 || ! subreg_lowpart_p (lhs)
9164 || (GET_MODE_CLASS (GET_MODE (lhs))
9165 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
9166 || (GET_MODE_SIZE (GET_MODE (lhs))
9167 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
9168 || VECTOR_MODE_P (GET_MODE (lhs))
9169 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
9170 /* Result might need to be truncated. Don't change mode if
9171 explicit truncation is needed. */
9172 || !TRULY_NOOP_TRUNCATION
9173 (GET_MODE_BITSIZE (GET_MODE (x)),
9174 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
9175 return x;
9176
9177 tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
9178 SUBREG_REG (lhs), SUBREG_REG (rhs));
9179 return gen_lowpart (GET_MODE (x), tem);
9180
9181 default:
9182 return x;
9183 }
9184
9185 /* Set LHS and RHS to the inner operands (A and B in the example
9186 above) and set OTHER to the common operand (C in the example).
9187 There is only one way to do this unless the inner operation is
9188 commutative. */
9189 if (COMMUTATIVE_ARITH_P (lhs)
9190 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9191 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9192 else if (COMMUTATIVE_ARITH_P (lhs)
9193 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9194 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9195 else if (COMMUTATIVE_ARITH_P (lhs)
9196 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9197 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9198 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9199 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9200 else
9201 return x;
9202
9203 /* Form the new inner operation, seeing if it simplifies first. */
9204 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9205
9206 /* There is one exception to the general way of distributing:
9207 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9208 if (code == XOR && inner_code == IOR)
9209 {
9210 inner_code = AND;
9211 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9212 }
9213
9214 /* We may be able to continuing distributing the result, so call
9215 ourselves recursively on the inner operation before forming the
9216 outer operation, which we return. */
9217 return simplify_gen_binary (inner_code, GET_MODE (x),
9218 apply_distributive_law (tem), other);
9219 }
9220
9221 /* See if X is of the form (* (+ A B) C), and if so convert to
9222 (+ (* A C) (* B C)) and try to simplify.
9223
9224 Most of the time, this results in no change. However, if some of
9225 the operands are the same or inverses of each other, simplifications
9226 will result.
9227
9228 For example, (and (ior A B) (not B)) can occur as the result of
9229 expanding a bit field assignment. When we apply the distributive
9230 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9231 which then simplifies to (and (A (not B))).
9232
9233 Note that no checks happen on the validity of applying the inverse
9234 distributive law. This is pointless since we can do it in the
9235 few places where this routine is called.
9236
9237 N is the index of the term that is decomposed (the arithmetic operation,
9238 i.e. (+ A B) in the first example above). !N is the index of the term that
9239 is distributed, i.e. of C in the first example above. */
9240 static rtx
9241 distribute_and_simplify_rtx (rtx x, int n)
9242 {
9243 enum machine_mode mode;
9244 enum rtx_code outer_code, inner_code;
9245 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9246
9247 /* Distributivity is not true for floating point as it can change the
9248 value. So we don't do it unless -funsafe-math-optimizations. */
9249 if (FLOAT_MODE_P (GET_MODE (x))
9250 && ! flag_unsafe_math_optimizations)
9251 return NULL_RTX;
9252
9253 decomposed = XEXP (x, n);
9254 if (!ARITHMETIC_P (decomposed))
9255 return NULL_RTX;
9256
9257 mode = GET_MODE (x);
9258 outer_code = GET_CODE (x);
9259 distributed = XEXP (x, !n);
9260
9261 inner_code = GET_CODE (decomposed);
9262 inner_op0 = XEXP (decomposed, 0);
9263 inner_op1 = XEXP (decomposed, 1);
9264
9265 /* Special case (and (xor B C) (not A)), which is equivalent to
9266 (xor (ior A B) (ior A C)) */
9267 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9268 {
9269 distributed = XEXP (distributed, 0);
9270 outer_code = IOR;
9271 }
9272
9273 if (n == 0)
9274 {
9275 /* Distribute the second term. */
9276 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9277 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9278 }
9279 else
9280 {
9281 /* Distribute the first term. */
9282 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9283 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9284 }
9285
9286 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9287 new_op0, new_op1));
9288 if (GET_CODE (tmp) != outer_code
9289 && rtx_cost (tmp, SET, optimize_this_for_speed_p)
9290 < rtx_cost (x, SET, optimize_this_for_speed_p))
9291 return tmp;
9292
9293 return NULL_RTX;
9294 }
9295 \f
9296 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9297 in MODE. Return an equivalent form, if different from (and VAROP
9298 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9299
9300 static rtx
9301 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
9302 unsigned HOST_WIDE_INT constop)
9303 {
9304 unsigned HOST_WIDE_INT nonzero;
9305 unsigned HOST_WIDE_INT orig_constop;
9306 rtx orig_varop;
9307 int i;
9308
9309 orig_varop = varop;
9310 orig_constop = constop;
9311 if (GET_CODE (varop) == CLOBBER)
9312 return NULL_RTX;
9313
9314 /* Simplify VAROP knowing that we will be only looking at some of the
9315 bits in it.
9316
9317 Note by passing in CONSTOP, we guarantee that the bits not set in
9318 CONSTOP are not significant and will never be examined. We must
9319 ensure that is the case by explicitly masking out those bits
9320 before returning. */
9321 varop = force_to_mode (varop, mode, constop, 0);
9322
9323 /* If VAROP is a CLOBBER, we will fail so return it. */
9324 if (GET_CODE (varop) == CLOBBER)
9325 return varop;
9326
9327 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9328 to VAROP and return the new constant. */
9329 if (CONST_INT_P (varop))
9330 return gen_int_mode (INTVAL (varop) & constop, mode);
9331
9332 /* See what bits may be nonzero in VAROP. Unlike the general case of
9333 a call to nonzero_bits, here we don't care about bits outside
9334 MODE. */
9335
9336 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9337
9338 /* Turn off all bits in the constant that are known to already be zero.
9339 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9340 which is tested below. */
9341
9342 constop &= nonzero;
9343
9344 /* If we don't have any bits left, return zero. */
9345 if (constop == 0)
9346 return const0_rtx;
9347
9348 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9349 a power of two, we can replace this with an ASHIFT. */
9350 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9351 && (i = exact_log2 (constop)) >= 0)
9352 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9353
9354 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9355 or XOR, then try to apply the distributive law. This may eliminate
9356 operations if either branch can be simplified because of the AND.
9357 It may also make some cases more complex, but those cases probably
9358 won't match a pattern either with or without this. */
9359
9360 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9361 return
9362 gen_lowpart
9363 (mode,
9364 apply_distributive_law
9365 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9366 simplify_and_const_int (NULL_RTX,
9367 GET_MODE (varop),
9368 XEXP (varop, 0),
9369 constop),
9370 simplify_and_const_int (NULL_RTX,
9371 GET_MODE (varop),
9372 XEXP (varop, 1),
9373 constop))));
9374
9375 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9376 the AND and see if one of the operands simplifies to zero. If so, we
9377 may eliminate it. */
9378
9379 if (GET_CODE (varop) == PLUS
9380 && exact_log2 (constop + 1) >= 0)
9381 {
9382 rtx o0, o1;
9383
9384 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9385 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9386 if (o0 == const0_rtx)
9387 return o1;
9388 if (o1 == const0_rtx)
9389 return o0;
9390 }
9391
9392 /* Make a SUBREG if necessary. If we can't make it, fail. */
9393 varop = gen_lowpart (mode, varop);
9394 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9395 return NULL_RTX;
9396
9397 /* If we are only masking insignificant bits, return VAROP. */
9398 if (constop == nonzero)
9399 return varop;
9400
9401 if (varop == orig_varop && constop == orig_constop)
9402 return NULL_RTX;
9403
9404 /* Otherwise, return an AND. */
9405 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9406 }
9407
9408
9409 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9410 in MODE.
9411
9412 Return an equivalent form, if different from X. Otherwise, return X. If
9413 X is zero, we are to always construct the equivalent form. */
9414
9415 static rtx
9416 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
9417 unsigned HOST_WIDE_INT constop)
9418 {
9419 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9420 if (tem)
9421 return tem;
9422
9423 if (!x)
9424 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9425 gen_int_mode (constop, mode));
9426 if (GET_MODE (x) != mode)
9427 x = gen_lowpart (mode, x);
9428 return x;
9429 }
9430 \f
9431 /* Given a REG, X, compute which bits in X can be nonzero.
9432 We don't care about bits outside of those defined in MODE.
9433
9434 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9435 a shift, AND, or zero_extract, we can do better. */
9436
9437 static rtx
9438 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
9439 const_rtx known_x ATTRIBUTE_UNUSED,
9440 enum machine_mode known_mode ATTRIBUTE_UNUSED,
9441 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9442 unsigned HOST_WIDE_INT *nonzero)
9443 {
9444 rtx tem;
9445 reg_stat_type *rsp;
9446
9447 /* If X is a register whose nonzero bits value is current, use it.
9448 Otherwise, if X is a register whose value we can find, use that
9449 value. Otherwise, use the previously-computed global nonzero bits
9450 for this register. */
9451
9452 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
9453 if (rsp->last_set_value != 0
9454 && (rsp->last_set_mode == mode
9455 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9456 && GET_MODE_CLASS (mode) == MODE_INT))
9457 && ((rsp->last_set_label >= label_tick_ebb_start
9458 && rsp->last_set_label < label_tick)
9459 || (rsp->last_set_label == label_tick
9460 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9461 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9462 && REG_N_SETS (REGNO (x)) == 1
9463 && !REGNO_REG_SET_P
9464 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9465 {
9466 *nonzero &= rsp->last_set_nonzero_bits;
9467 return NULL;
9468 }
9469
9470 tem = get_last_value (x);
9471
9472 if (tem)
9473 {
9474 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9475 /* If X is narrower than MODE and TEM is a non-negative
9476 constant that would appear negative in the mode of X,
9477 sign-extend it for use in reg_nonzero_bits because some
9478 machines (maybe most) will actually do the sign-extension
9479 and this is the conservative approach.
9480
9481 ??? For 2.5, try to tighten up the MD files in this regard
9482 instead of this kludge. */
9483
9484 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
9485 && CONST_INT_P (tem)
9486 && INTVAL (tem) > 0
9487 && 0 != (UINTVAL (tem)
9488 & ((unsigned HOST_WIDE_INT) 1
9489 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
9490 tem = GEN_INT (UINTVAL (tem)
9491 | ((unsigned HOST_WIDE_INT) (-1)
9492 << GET_MODE_BITSIZE (GET_MODE (x))));
9493 #endif
9494 return tem;
9495 }
9496 else if (nonzero_sign_valid && rsp->nonzero_bits)
9497 {
9498 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9499
9500 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
9501 /* We don't know anything about the upper bits. */
9502 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9503 *nonzero &= mask;
9504 }
9505
9506 return NULL;
9507 }
9508
9509 /* Return the number of bits at the high-order end of X that are known to
9510 be equal to the sign bit. X will be used in mode MODE; if MODE is
9511 VOIDmode, X will be used in its own mode. The returned value will always
9512 be between 1 and the number of bits in MODE. */
9513
9514 static rtx
9515 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
9516 const_rtx known_x ATTRIBUTE_UNUSED,
9517 enum machine_mode known_mode
9518 ATTRIBUTE_UNUSED,
9519 unsigned int known_ret ATTRIBUTE_UNUSED,
9520 unsigned int *result)
9521 {
9522 rtx tem;
9523 reg_stat_type *rsp;
9524
9525 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
9526 if (rsp->last_set_value != 0
9527 && rsp->last_set_mode == mode
9528 && ((rsp->last_set_label >= label_tick_ebb_start
9529 && rsp->last_set_label < label_tick)
9530 || (rsp->last_set_label == label_tick
9531 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9532 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9533 && REG_N_SETS (REGNO (x)) == 1
9534 && !REGNO_REG_SET_P
9535 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9536 {
9537 *result = rsp->last_set_sign_bit_copies;
9538 return NULL;
9539 }
9540
9541 tem = get_last_value (x);
9542 if (tem != 0)
9543 return tem;
9544
9545 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9546 && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
9547 *result = rsp->sign_bit_copies;
9548
9549 return NULL;
9550 }
9551 \f
9552 /* Return the number of "extended" bits there are in X, when interpreted
9553 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9554 unsigned quantities, this is the number of high-order zero bits.
9555 For signed quantities, this is the number of copies of the sign bit
9556 minus 1. In both case, this function returns the number of "spare"
9557 bits. For example, if two quantities for which this function returns
9558 at least 1 are added, the addition is known not to overflow.
9559
9560 This function will always return 0 unless called during combine, which
9561 implies that it must be called from a define_split. */
9562
9563 unsigned int
9564 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
9565 {
9566 if (nonzero_sign_valid == 0)
9567 return 0;
9568
9569 return (unsignedp
9570 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9571 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
9572 - floor_log2 (nonzero_bits (x, mode)))
9573 : 0)
9574 : num_sign_bit_copies (x, mode) - 1);
9575 }
9576 \f
9577 /* This function is called from `simplify_shift_const' to merge two
9578 outer operations. Specifically, we have already found that we need
9579 to perform operation *POP0 with constant *PCONST0 at the outermost
9580 position. We would now like to also perform OP1 with constant CONST1
9581 (with *POP0 being done last).
9582
9583 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9584 the resulting operation. *PCOMP_P is set to 1 if we would need to
9585 complement the innermost operand, otherwise it is unchanged.
9586
9587 MODE is the mode in which the operation will be done. No bits outside
9588 the width of this mode matter. It is assumed that the width of this mode
9589 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9590
9591 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9592 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9593 result is simply *PCONST0.
9594
9595 If the resulting operation cannot be expressed as one operation, we
9596 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9597
9598 static int
9599 merge_outer_ops (enum rtx_code *pop0, HOST_WIDE_INT *pconst0, enum rtx_code op1, HOST_WIDE_INT const1, enum machine_mode mode, int *pcomp_p)
9600 {
9601 enum rtx_code op0 = *pop0;
9602 HOST_WIDE_INT const0 = *pconst0;
9603
9604 const0 &= GET_MODE_MASK (mode);
9605 const1 &= GET_MODE_MASK (mode);
9606
9607 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9608 if (op0 == AND)
9609 const1 &= const0;
9610
9611 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9612 if OP0 is SET. */
9613
9614 if (op1 == UNKNOWN || op0 == SET)
9615 return 1;
9616
9617 else if (op0 == UNKNOWN)
9618 op0 = op1, const0 = const1;
9619
9620 else if (op0 == op1)
9621 {
9622 switch (op0)
9623 {
9624 case AND:
9625 const0 &= const1;
9626 break;
9627 case IOR:
9628 const0 |= const1;
9629 break;
9630 case XOR:
9631 const0 ^= const1;
9632 break;
9633 case PLUS:
9634 const0 += const1;
9635 break;
9636 case NEG:
9637 op0 = UNKNOWN;
9638 break;
9639 default:
9640 break;
9641 }
9642 }
9643
9644 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9645 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9646 return 0;
9647
9648 /* If the two constants aren't the same, we can't do anything. The
9649 remaining six cases can all be done. */
9650 else if (const0 != const1)
9651 return 0;
9652
9653 else
9654 switch (op0)
9655 {
9656 case IOR:
9657 if (op1 == AND)
9658 /* (a & b) | b == b */
9659 op0 = SET;
9660 else /* op1 == XOR */
9661 /* (a ^ b) | b == a | b */
9662 {;}
9663 break;
9664
9665 case XOR:
9666 if (op1 == AND)
9667 /* (a & b) ^ b == (~a) & b */
9668 op0 = AND, *pcomp_p = 1;
9669 else /* op1 == IOR */
9670 /* (a | b) ^ b == a & ~b */
9671 op0 = AND, const0 = ~const0;
9672 break;
9673
9674 case AND:
9675 if (op1 == IOR)
9676 /* (a | b) & b == b */
9677 op0 = SET;
9678 else /* op1 == XOR */
9679 /* (a ^ b) & b) == (~a) & b */
9680 *pcomp_p = 1;
9681 break;
9682 default:
9683 break;
9684 }
9685
9686 /* Check for NO-OP cases. */
9687 const0 &= GET_MODE_MASK (mode);
9688 if (const0 == 0
9689 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9690 op0 = UNKNOWN;
9691 else if (const0 == 0 && op0 == AND)
9692 op0 = SET;
9693 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9694 && op0 == AND)
9695 op0 = UNKNOWN;
9696
9697 *pop0 = op0;
9698
9699 /* ??? Slightly redundant with the above mask, but not entirely.
9700 Moving this above means we'd have to sign-extend the mode mask
9701 for the final test. */
9702 if (op0 != UNKNOWN && op0 != NEG)
9703 *pconst0 = trunc_int_for_mode (const0, mode);
9704
9705 return 1;
9706 }
9707 \f
9708 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9709 the shift in. The original shift operation CODE is performed on OP in
9710 ORIG_MODE. Return the wider mode MODE if we can perform the operation
9711 in that mode. Return ORIG_MODE otherwise. We can also assume that the
9712 result of the shift is subject to operation OUTER_CODE with operand
9713 OUTER_CONST. */
9714
9715 static enum machine_mode
9716 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9717 enum machine_mode orig_mode, enum machine_mode mode,
9718 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9719 {
9720 if (orig_mode == mode)
9721 return mode;
9722 gcc_assert (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (orig_mode));
9723
9724 /* In general we can't perform in wider mode for right shift and rotate. */
9725 switch (code)
9726 {
9727 case ASHIFTRT:
9728 /* We can still widen if the bits brought in from the left are identical
9729 to the sign bit of ORIG_MODE. */
9730 if (num_sign_bit_copies (op, mode)
9731 > (unsigned) (GET_MODE_BITSIZE (mode)
9732 - GET_MODE_BITSIZE (orig_mode)))
9733 return mode;
9734 return orig_mode;
9735
9736 case LSHIFTRT:
9737 /* Similarly here but with zero bits. */
9738 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9739 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9740 return mode;
9741
9742 /* We can also widen if the bits brought in will be masked off. This
9743 operation is performed in ORIG_MODE. */
9744 if (outer_code == AND)
9745 {
9746 int care_bits = low_bitmask_len (orig_mode, outer_const);
9747
9748 if (care_bits >= 0
9749 && GET_MODE_BITSIZE (orig_mode) - care_bits >= count)
9750 return mode;
9751 }
9752 /* fall through */
9753
9754 case ROTATE:
9755 return orig_mode;
9756
9757 case ROTATERT:
9758 gcc_unreachable ();
9759
9760 default:
9761 return mode;
9762 }
9763 }
9764
9765 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
9766 The result of the shift is RESULT_MODE. Return NULL_RTX if we cannot
9767 simplify it. Otherwise, return a simplified value.
9768
9769 The shift is normally computed in the widest mode we find in VAROP, as
9770 long as it isn't a different number of words than RESULT_MODE. Exceptions
9771 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9772
9773 static rtx
9774 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
9775 rtx varop, int orig_count)
9776 {
9777 enum rtx_code orig_code = code;
9778 rtx orig_varop = varop;
9779 int count;
9780 enum machine_mode mode = result_mode;
9781 enum machine_mode shift_mode, tmode;
9782 unsigned int mode_words
9783 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9784 /* We form (outer_op (code varop count) (outer_const)). */
9785 enum rtx_code outer_op = UNKNOWN;
9786 HOST_WIDE_INT outer_const = 0;
9787 int complement_p = 0;
9788 rtx new_rtx, x;
9789
9790 /* Make sure and truncate the "natural" shift on the way in. We don't
9791 want to do this inside the loop as it makes it more difficult to
9792 combine shifts. */
9793 if (SHIFT_COUNT_TRUNCATED)
9794 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9795
9796 /* If we were given an invalid count, don't do anything except exactly
9797 what was requested. */
9798
9799 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
9800 return NULL_RTX;
9801
9802 count = orig_count;
9803
9804 /* Unless one of the branches of the `if' in this loop does a `continue',
9805 we will `break' the loop after the `if'. */
9806
9807 while (count != 0)
9808 {
9809 /* If we have an operand of (clobber (const_int 0)), fail. */
9810 if (GET_CODE (varop) == CLOBBER)
9811 return NULL_RTX;
9812
9813 /* Convert ROTATERT to ROTATE. */
9814 if (code == ROTATERT)
9815 {
9816 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9817 code = ROTATE;
9818 if (VECTOR_MODE_P (result_mode))
9819 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9820 else
9821 count = bitsize - count;
9822 }
9823
9824 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9825 mode, outer_op, outer_const);
9826
9827 /* Handle cases where the count is greater than the size of the mode
9828 minus 1. For ASHIFT, use the size minus one as the count (this can
9829 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9830 take the count modulo the size. For other shifts, the result is
9831 zero.
9832
9833 Since these shifts are being produced by the compiler by combining
9834 multiple operations, each of which are defined, we know what the
9835 result is supposed to be. */
9836
9837 if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
9838 {
9839 if (code == ASHIFTRT)
9840 count = GET_MODE_BITSIZE (shift_mode) - 1;
9841 else if (code == ROTATE || code == ROTATERT)
9842 count %= GET_MODE_BITSIZE (shift_mode);
9843 else
9844 {
9845 /* We can't simply return zero because there may be an
9846 outer op. */
9847 varop = const0_rtx;
9848 count = 0;
9849 break;
9850 }
9851 }
9852
9853 /* If we discovered we had to complement VAROP, leave. Making a NOT
9854 here would cause an infinite loop. */
9855 if (complement_p)
9856 break;
9857
9858 /* An arithmetic right shift of a quantity known to be -1 or 0
9859 is a no-op. */
9860 if (code == ASHIFTRT
9861 && (num_sign_bit_copies (varop, shift_mode)
9862 == GET_MODE_BITSIZE (shift_mode)))
9863 {
9864 count = 0;
9865 break;
9866 }
9867
9868 /* If we are doing an arithmetic right shift and discarding all but
9869 the sign bit copies, this is equivalent to doing a shift by the
9870 bitsize minus one. Convert it into that shift because it will often
9871 allow other simplifications. */
9872
9873 if (code == ASHIFTRT
9874 && (count + num_sign_bit_copies (varop, shift_mode)
9875 >= GET_MODE_BITSIZE (shift_mode)))
9876 count = GET_MODE_BITSIZE (shift_mode) - 1;
9877
9878 /* We simplify the tests below and elsewhere by converting
9879 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9880 `make_compound_operation' will convert it to an ASHIFTRT for
9881 those machines (such as VAX) that don't have an LSHIFTRT. */
9882 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9883 && code == ASHIFTRT
9884 && ((nonzero_bits (varop, shift_mode)
9885 & ((unsigned HOST_WIDE_INT) 1
9886 << (GET_MODE_BITSIZE (shift_mode) - 1))) == 0))
9887 code = LSHIFTRT;
9888
9889 if (((code == LSHIFTRT
9890 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9891 && !(nonzero_bits (varop, shift_mode) >> count))
9892 || (code == ASHIFT
9893 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9894 && !((nonzero_bits (varop, shift_mode) << count)
9895 & GET_MODE_MASK (shift_mode))))
9896 && !side_effects_p (varop))
9897 varop = const0_rtx;
9898
9899 switch (GET_CODE (varop))
9900 {
9901 case SIGN_EXTEND:
9902 case ZERO_EXTEND:
9903 case SIGN_EXTRACT:
9904 case ZERO_EXTRACT:
9905 new_rtx = expand_compound_operation (varop);
9906 if (new_rtx != varop)
9907 {
9908 varop = new_rtx;
9909 continue;
9910 }
9911 break;
9912
9913 case MEM:
9914 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9915 minus the width of a smaller mode, we can do this with a
9916 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9917 if ((code == ASHIFTRT || code == LSHIFTRT)
9918 && ! mode_dependent_address_p (XEXP (varop, 0))
9919 && ! MEM_VOLATILE_P (varop)
9920 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9921 MODE_INT, 1)) != BLKmode)
9922 {
9923 new_rtx = adjust_address_nv (varop, tmode,
9924 BYTES_BIG_ENDIAN ? 0
9925 : count / BITS_PER_UNIT);
9926
9927 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9928 : ZERO_EXTEND, mode, new_rtx);
9929 count = 0;
9930 continue;
9931 }
9932 break;
9933
9934 case SUBREG:
9935 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9936 the same number of words as what we've seen so far. Then store
9937 the widest mode in MODE. */
9938 if (subreg_lowpart_p (varop)
9939 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9940 > GET_MODE_SIZE (GET_MODE (varop)))
9941 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9942 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9943 == mode_words
9944 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
9945 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
9946 {
9947 varop = SUBREG_REG (varop);
9948 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9949 mode = GET_MODE (varop);
9950 continue;
9951 }
9952 break;
9953
9954 case MULT:
9955 /* Some machines use MULT instead of ASHIFT because MULT
9956 is cheaper. But it is still better on those machines to
9957 merge two shifts into one. */
9958 if (CONST_INT_P (XEXP (varop, 1))
9959 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
9960 {
9961 varop
9962 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9963 XEXP (varop, 0),
9964 GEN_INT (exact_log2 (
9965 UINTVAL (XEXP (varop, 1)))));
9966 continue;
9967 }
9968 break;
9969
9970 case UDIV:
9971 /* Similar, for when divides are cheaper. */
9972 if (CONST_INT_P (XEXP (varop, 1))
9973 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
9974 {
9975 varop
9976 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
9977 XEXP (varop, 0),
9978 GEN_INT (exact_log2 (
9979 UINTVAL (XEXP (varop, 1)))));
9980 continue;
9981 }
9982 break;
9983
9984 case ASHIFTRT:
9985 /* If we are extracting just the sign bit of an arithmetic
9986 right shift, that shift is not needed. However, the sign
9987 bit of a wider mode may be different from what would be
9988 interpreted as the sign bit in a narrower mode, so, if
9989 the result is narrower, don't discard the shift. */
9990 if (code == LSHIFTRT
9991 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9992 && (GET_MODE_BITSIZE (result_mode)
9993 >= GET_MODE_BITSIZE (GET_MODE (varop))))
9994 {
9995 varop = XEXP (varop, 0);
9996 continue;
9997 }
9998
9999 /* ... fall through ... */
10000
10001 case LSHIFTRT:
10002 case ASHIFT:
10003 case ROTATE:
10004 /* Here we have two nested shifts. The result is usually the
10005 AND of a new shift with a mask. We compute the result below. */
10006 if (CONST_INT_P (XEXP (varop, 1))
10007 && INTVAL (XEXP (varop, 1)) >= 0
10008 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
10009 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
10010 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10011 && !VECTOR_MODE_P (result_mode))
10012 {
10013 enum rtx_code first_code = GET_CODE (varop);
10014 unsigned int first_count = INTVAL (XEXP (varop, 1));
10015 unsigned HOST_WIDE_INT mask;
10016 rtx mask_rtx;
10017
10018 /* We have one common special case. We can't do any merging if
10019 the inner code is an ASHIFTRT of a smaller mode. However, if
10020 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10021 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10022 we can convert it to
10023 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
10024 This simplifies certain SIGN_EXTEND operations. */
10025 if (code == ASHIFT && first_code == ASHIFTRT
10026 && count == (GET_MODE_BITSIZE (result_mode)
10027 - GET_MODE_BITSIZE (GET_MODE (varop))))
10028 {
10029 /* C3 has the low-order C1 bits zero. */
10030
10031 mask = GET_MODE_MASK (mode)
10032 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10033
10034 varop = simplify_and_const_int (NULL_RTX, result_mode,
10035 XEXP (varop, 0), mask);
10036 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10037 varop, count);
10038 count = first_count;
10039 code = ASHIFTRT;
10040 continue;
10041 }
10042
10043 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10044 than C1 high-order bits equal to the sign bit, we can convert
10045 this to either an ASHIFT or an ASHIFTRT depending on the
10046 two counts.
10047
10048 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10049
10050 if (code == ASHIFTRT && first_code == ASHIFT
10051 && GET_MODE (varop) == shift_mode
10052 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10053 > first_count))
10054 {
10055 varop = XEXP (varop, 0);
10056 count -= first_count;
10057 if (count < 0)
10058 {
10059 count = -count;
10060 code = ASHIFT;
10061 }
10062
10063 continue;
10064 }
10065
10066 /* There are some cases we can't do. If CODE is ASHIFTRT,
10067 we can only do this if FIRST_CODE is also ASHIFTRT.
10068
10069 We can't do the case when CODE is ROTATE and FIRST_CODE is
10070 ASHIFTRT.
10071
10072 If the mode of this shift is not the mode of the outer shift,
10073 we can't do this if either shift is a right shift or ROTATE.
10074
10075 Finally, we can't do any of these if the mode is too wide
10076 unless the codes are the same.
10077
10078 Handle the case where the shift codes are the same
10079 first. */
10080
10081 if (code == first_code)
10082 {
10083 if (GET_MODE (varop) != result_mode
10084 && (code == ASHIFTRT || code == LSHIFTRT
10085 || code == ROTATE))
10086 break;
10087
10088 count += first_count;
10089 varop = XEXP (varop, 0);
10090 continue;
10091 }
10092
10093 if (code == ASHIFTRT
10094 || (code == ROTATE && first_code == ASHIFTRT)
10095 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
10096 || (GET_MODE (varop) != result_mode
10097 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10098 || first_code == ROTATE
10099 || code == ROTATE)))
10100 break;
10101
10102 /* To compute the mask to apply after the shift, shift the
10103 nonzero bits of the inner shift the same way the
10104 outer shift will. */
10105
10106 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
10107
10108 mask_rtx
10109 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10110 GEN_INT (count));
10111
10112 /* Give up if we can't compute an outer operation to use. */
10113 if (mask_rtx == 0
10114 || !CONST_INT_P (mask_rtx)
10115 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10116 INTVAL (mask_rtx),
10117 result_mode, &complement_p))
10118 break;
10119
10120 /* If the shifts are in the same direction, we add the
10121 counts. Otherwise, we subtract them. */
10122 if ((code == ASHIFTRT || code == LSHIFTRT)
10123 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10124 count += first_count;
10125 else
10126 count -= first_count;
10127
10128 /* If COUNT is positive, the new shift is usually CODE,
10129 except for the two exceptions below, in which case it is
10130 FIRST_CODE. If the count is negative, FIRST_CODE should
10131 always be used */
10132 if (count > 0
10133 && ((first_code == ROTATE && code == ASHIFT)
10134 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10135 code = first_code;
10136 else if (count < 0)
10137 code = first_code, count = -count;
10138
10139 varop = XEXP (varop, 0);
10140 continue;
10141 }
10142
10143 /* If we have (A << B << C) for any shift, we can convert this to
10144 (A << C << B). This wins if A is a constant. Only try this if
10145 B is not a constant. */
10146
10147 else if (GET_CODE (varop) == code
10148 && CONST_INT_P (XEXP (varop, 0))
10149 && !CONST_INT_P (XEXP (varop, 1)))
10150 {
10151 rtx new_rtx = simplify_const_binary_operation (code, mode,
10152 XEXP (varop, 0),
10153 GEN_INT (count));
10154 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10155 count = 0;
10156 continue;
10157 }
10158 break;
10159
10160 case NOT:
10161 if (VECTOR_MODE_P (mode))
10162 break;
10163
10164 /* Make this fit the case below. */
10165 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
10166 GEN_INT (GET_MODE_MASK (mode)));
10167 continue;
10168
10169 case IOR:
10170 case AND:
10171 case XOR:
10172 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10173 with C the size of VAROP - 1 and the shift is logical if
10174 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10175 we have an (le X 0) operation. If we have an arithmetic shift
10176 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10177 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10178
10179 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10180 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10181 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10182 && (code == LSHIFTRT || code == ASHIFTRT)
10183 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
10184 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10185 {
10186 count = 0;
10187 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10188 const0_rtx);
10189
10190 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10191 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10192
10193 continue;
10194 }
10195
10196 /* If we have (shift (logical)), move the logical to the outside
10197 to allow it to possibly combine with another logical and the
10198 shift to combine with another shift. This also canonicalizes to
10199 what a ZERO_EXTRACT looks like. Also, some machines have
10200 (and (shift)) insns. */
10201
10202 if (CONST_INT_P (XEXP (varop, 1))
10203 /* We can't do this if we have (ashiftrt (xor)) and the
10204 constant has its sign bit set in shift_mode. */
10205 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10206 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10207 shift_mode))
10208 && (new_rtx = simplify_const_binary_operation (code, result_mode,
10209 XEXP (varop, 1),
10210 GEN_INT (count))) != 0
10211 && CONST_INT_P (new_rtx)
10212 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10213 INTVAL (new_rtx), result_mode, &complement_p))
10214 {
10215 varop = XEXP (varop, 0);
10216 continue;
10217 }
10218
10219 /* If we can't do that, try to simplify the shift in each arm of the
10220 logical expression, make a new logical expression, and apply
10221 the inverse distributive law. This also can't be done
10222 for some (ashiftrt (xor)). */
10223 if (CONST_INT_P (XEXP (varop, 1))
10224 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10225 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10226 shift_mode)))
10227 {
10228 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10229 XEXP (varop, 0), count);
10230 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10231 XEXP (varop, 1), count);
10232
10233 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10234 lhs, rhs);
10235 varop = apply_distributive_law (varop);
10236
10237 count = 0;
10238 continue;
10239 }
10240 break;
10241
10242 case EQ:
10243 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10244 says that the sign bit can be tested, FOO has mode MODE, C is
10245 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
10246 that may be nonzero. */
10247 if (code == LSHIFTRT
10248 && XEXP (varop, 1) == const0_rtx
10249 && GET_MODE (XEXP (varop, 0)) == result_mode
10250 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10251 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
10252 && STORE_FLAG_VALUE == -1
10253 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10254 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10255 &complement_p))
10256 {
10257 varop = XEXP (varop, 0);
10258 count = 0;
10259 continue;
10260 }
10261 break;
10262
10263 case NEG:
10264 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10265 than the number of bits in the mode is equivalent to A. */
10266 if (code == LSHIFTRT
10267 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10268 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10269 {
10270 varop = XEXP (varop, 0);
10271 count = 0;
10272 continue;
10273 }
10274
10275 /* NEG commutes with ASHIFT since it is multiplication. Move the
10276 NEG outside to allow shifts to combine. */
10277 if (code == ASHIFT
10278 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10279 &complement_p))
10280 {
10281 varop = XEXP (varop, 0);
10282 continue;
10283 }
10284 break;
10285
10286 case PLUS:
10287 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10288 is one less than the number of bits in the mode is
10289 equivalent to (xor A 1). */
10290 if (code == LSHIFTRT
10291 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10292 && XEXP (varop, 1) == constm1_rtx
10293 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10294 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10295 &complement_p))
10296 {
10297 count = 0;
10298 varop = XEXP (varop, 0);
10299 continue;
10300 }
10301
10302 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10303 that might be nonzero in BAR are those being shifted out and those
10304 bits are known zero in FOO, we can replace the PLUS with FOO.
10305 Similarly in the other operand order. This code occurs when
10306 we are computing the size of a variable-size array. */
10307
10308 if ((code == ASHIFTRT || code == LSHIFTRT)
10309 && count < HOST_BITS_PER_WIDE_INT
10310 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10311 && (nonzero_bits (XEXP (varop, 1), result_mode)
10312 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10313 {
10314 varop = XEXP (varop, 0);
10315 continue;
10316 }
10317 else if ((code == ASHIFTRT || code == LSHIFTRT)
10318 && count < HOST_BITS_PER_WIDE_INT
10319 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
10320 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10321 >> count)
10322 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10323 & nonzero_bits (XEXP (varop, 1),
10324 result_mode)))
10325 {
10326 varop = XEXP (varop, 1);
10327 continue;
10328 }
10329
10330 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10331 if (code == ASHIFT
10332 && CONST_INT_P (XEXP (varop, 1))
10333 && (new_rtx = simplify_const_binary_operation (ASHIFT, result_mode,
10334 XEXP (varop, 1),
10335 GEN_INT (count))) != 0
10336 && CONST_INT_P (new_rtx)
10337 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10338 INTVAL (new_rtx), result_mode, &complement_p))
10339 {
10340 varop = XEXP (varop, 0);
10341 continue;
10342 }
10343
10344 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10345 signbit', and attempt to change the PLUS to an XOR and move it to
10346 the outer operation as is done above in the AND/IOR/XOR case
10347 leg for shift(logical). See details in logical handling above
10348 for reasoning in doing so. */
10349 if (code == LSHIFTRT
10350 && CONST_INT_P (XEXP (varop, 1))
10351 && mode_signbit_p (result_mode, XEXP (varop, 1))
10352 && (new_rtx = simplify_const_binary_operation (code, result_mode,
10353 XEXP (varop, 1),
10354 GEN_INT (count))) != 0
10355 && CONST_INT_P (new_rtx)
10356 && merge_outer_ops (&outer_op, &outer_const, XOR,
10357 INTVAL (new_rtx), result_mode, &complement_p))
10358 {
10359 varop = XEXP (varop, 0);
10360 continue;
10361 }
10362
10363 break;
10364
10365 case MINUS:
10366 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10367 with C the size of VAROP - 1 and the shift is logical if
10368 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10369 we have a (gt X 0) operation. If the shift is arithmetic with
10370 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10371 we have a (neg (gt X 0)) operation. */
10372
10373 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10374 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10375 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
10376 && (code == LSHIFTRT || code == ASHIFTRT)
10377 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10378 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10379 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10380 {
10381 count = 0;
10382 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10383 const0_rtx);
10384
10385 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10386 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10387
10388 continue;
10389 }
10390 break;
10391
10392 case TRUNCATE:
10393 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10394 if the truncate does not affect the value. */
10395 if (code == LSHIFTRT
10396 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10397 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10398 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10399 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
10400 - GET_MODE_BITSIZE (GET_MODE (varop)))))
10401 {
10402 rtx varop_inner = XEXP (varop, 0);
10403
10404 varop_inner
10405 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10406 XEXP (varop_inner, 0),
10407 GEN_INT
10408 (count + INTVAL (XEXP (varop_inner, 1))));
10409 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10410 count = 0;
10411 continue;
10412 }
10413 break;
10414
10415 default:
10416 break;
10417 }
10418
10419 break;
10420 }
10421
10422 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10423 outer_op, outer_const);
10424
10425 /* We have now finished analyzing the shift. The result should be
10426 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10427 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10428 to the result of the shift. OUTER_CONST is the relevant constant,
10429 but we must turn off all bits turned off in the shift. */
10430
10431 if (outer_op == UNKNOWN
10432 && orig_code == code && orig_count == count
10433 && varop == orig_varop
10434 && shift_mode == GET_MODE (varop))
10435 return NULL_RTX;
10436
10437 /* Make a SUBREG if necessary. If we can't make it, fail. */
10438 varop = gen_lowpart (shift_mode, varop);
10439 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10440 return NULL_RTX;
10441
10442 /* If we have an outer operation and we just made a shift, it is
10443 possible that we could have simplified the shift were it not
10444 for the outer operation. So try to do the simplification
10445 recursively. */
10446
10447 if (outer_op != UNKNOWN)
10448 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10449 else
10450 x = NULL_RTX;
10451
10452 if (x == NULL_RTX)
10453 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10454
10455 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10456 turn off all the bits that the shift would have turned off. */
10457 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10458 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10459 GET_MODE_MASK (result_mode) >> orig_count);
10460
10461 /* Do the remainder of the processing in RESULT_MODE. */
10462 x = gen_lowpart_or_truncate (result_mode, x);
10463
10464 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10465 operation. */
10466 if (complement_p)
10467 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10468
10469 if (outer_op != UNKNOWN)
10470 {
10471 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10472 && GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
10473 outer_const = trunc_int_for_mode (outer_const, result_mode);
10474
10475 if (outer_op == AND)
10476 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10477 else if (outer_op == SET)
10478 {
10479 /* This means that we have determined that the result is
10480 equivalent to a constant. This should be rare. */
10481 if (!side_effects_p (x))
10482 x = GEN_INT (outer_const);
10483 }
10484 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10485 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10486 else
10487 x = simplify_gen_binary (outer_op, result_mode, x,
10488 GEN_INT (outer_const));
10489 }
10490
10491 return x;
10492 }
10493
10494 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10495 The result of the shift is RESULT_MODE. If we cannot simplify it,
10496 return X or, if it is NULL, synthesize the expression with
10497 simplify_gen_binary. Otherwise, return a simplified value.
10498
10499 The shift is normally computed in the widest mode we find in VAROP, as
10500 long as it isn't a different number of words than RESULT_MODE. Exceptions
10501 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10502
10503 static rtx
10504 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
10505 rtx varop, int count)
10506 {
10507 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10508 if (tem)
10509 return tem;
10510
10511 if (!x)
10512 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10513 if (GET_MODE (x) != result_mode)
10514 x = gen_lowpart (result_mode, x);
10515 return x;
10516 }
10517
10518 \f
10519 /* Like recog, but we receive the address of a pointer to a new pattern.
10520 We try to match the rtx that the pointer points to.
10521 If that fails, we may try to modify or replace the pattern,
10522 storing the replacement into the same pointer object.
10523
10524 Modifications include deletion or addition of CLOBBERs.
10525
10526 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10527 the CLOBBERs are placed.
10528
10529 The value is the final insn code from the pattern ultimately matched,
10530 or -1. */
10531
10532 static int
10533 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
10534 {
10535 rtx pat = *pnewpat;
10536 int insn_code_number;
10537 int num_clobbers_to_add = 0;
10538 int i;
10539 rtx notes = 0;
10540 rtx old_notes, old_pat;
10541
10542 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10543 we use to indicate that something didn't match. If we find such a
10544 thing, force rejection. */
10545 if (GET_CODE (pat) == PARALLEL)
10546 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10547 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10548 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10549 return -1;
10550
10551 old_pat = PATTERN (insn);
10552 old_notes = REG_NOTES (insn);
10553 PATTERN (insn) = pat;
10554 REG_NOTES (insn) = 0;
10555
10556 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10557 if (dump_file && (dump_flags & TDF_DETAILS))
10558 {
10559 if (insn_code_number < 0)
10560 fputs ("Failed to match this instruction:\n", dump_file);
10561 else
10562 fputs ("Successfully matched this instruction:\n", dump_file);
10563 print_rtl_single (dump_file, pat);
10564 }
10565
10566 /* If it isn't, there is the possibility that we previously had an insn
10567 that clobbered some register as a side effect, but the combined
10568 insn doesn't need to do that. So try once more without the clobbers
10569 unless this represents an ASM insn. */
10570
10571 if (insn_code_number < 0 && ! check_asm_operands (pat)
10572 && GET_CODE (pat) == PARALLEL)
10573 {
10574 int pos;
10575
10576 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10577 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10578 {
10579 if (i != pos)
10580 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10581 pos++;
10582 }
10583
10584 SUBST_INT (XVECLEN (pat, 0), pos);
10585
10586 if (pos == 1)
10587 pat = XVECEXP (pat, 0, 0);
10588
10589 PATTERN (insn) = pat;
10590 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10591 if (dump_file && (dump_flags & TDF_DETAILS))
10592 {
10593 if (insn_code_number < 0)
10594 fputs ("Failed to match this instruction:\n", dump_file);
10595 else
10596 fputs ("Successfully matched this instruction:\n", dump_file);
10597 print_rtl_single (dump_file, pat);
10598 }
10599 }
10600 PATTERN (insn) = old_pat;
10601 REG_NOTES (insn) = old_notes;
10602
10603 /* Recognize all noop sets, these will be killed by followup pass. */
10604 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10605 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10606
10607 /* If we had any clobbers to add, make a new pattern than contains
10608 them. Then check to make sure that all of them are dead. */
10609 if (num_clobbers_to_add)
10610 {
10611 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10612 rtvec_alloc (GET_CODE (pat) == PARALLEL
10613 ? (XVECLEN (pat, 0)
10614 + num_clobbers_to_add)
10615 : num_clobbers_to_add + 1));
10616
10617 if (GET_CODE (pat) == PARALLEL)
10618 for (i = 0; i < XVECLEN (pat, 0); i++)
10619 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10620 else
10621 XVECEXP (newpat, 0, 0) = pat;
10622
10623 add_clobbers (newpat, insn_code_number);
10624
10625 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10626 i < XVECLEN (newpat, 0); i++)
10627 {
10628 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10629 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10630 return -1;
10631 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10632 {
10633 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10634 notes = alloc_reg_note (REG_UNUSED,
10635 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10636 }
10637 }
10638 pat = newpat;
10639 }
10640
10641 *pnewpat = pat;
10642 *pnotes = notes;
10643
10644 return insn_code_number;
10645 }
10646 \f
10647 /* Like gen_lowpart_general but for use by combine. In combine it
10648 is not possible to create any new pseudoregs. However, it is
10649 safe to create invalid memory addresses, because combine will
10650 try to recognize them and all they will do is make the combine
10651 attempt fail.
10652
10653 If for some reason this cannot do its job, an rtx
10654 (clobber (const_int 0)) is returned.
10655 An insn containing that will not be recognized. */
10656
10657 static rtx
10658 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
10659 {
10660 enum machine_mode imode = GET_MODE (x);
10661 unsigned int osize = GET_MODE_SIZE (omode);
10662 unsigned int isize = GET_MODE_SIZE (imode);
10663 rtx result;
10664
10665 if (omode == imode)
10666 return x;
10667
10668 /* Return identity if this is a CONST or symbolic reference. */
10669 if (omode == Pmode
10670 && (GET_CODE (x) == CONST
10671 || GET_CODE (x) == SYMBOL_REF
10672 || GET_CODE (x) == LABEL_REF))
10673 return x;
10674
10675 /* We can only support MODE being wider than a word if X is a
10676 constant integer or has a mode the same size. */
10677 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10678 && ! ((imode == VOIDmode
10679 && (CONST_INT_P (x)
10680 || GET_CODE (x) == CONST_DOUBLE))
10681 || isize == osize))
10682 goto fail;
10683
10684 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10685 won't know what to do. So we will strip off the SUBREG here and
10686 process normally. */
10687 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10688 {
10689 x = SUBREG_REG (x);
10690
10691 /* For use in case we fall down into the address adjustments
10692 further below, we need to adjust the known mode and size of
10693 x; imode and isize, since we just adjusted x. */
10694 imode = GET_MODE (x);
10695
10696 if (imode == omode)
10697 return x;
10698
10699 isize = GET_MODE_SIZE (imode);
10700 }
10701
10702 result = gen_lowpart_common (omode, x);
10703
10704 if (result)
10705 return result;
10706
10707 if (MEM_P (x))
10708 {
10709 int offset = 0;
10710
10711 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10712 address. */
10713 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10714 goto fail;
10715
10716 /* If we want to refer to something bigger than the original memref,
10717 generate a paradoxical subreg instead. That will force a reload
10718 of the original memref X. */
10719 if (isize < osize)
10720 return gen_rtx_SUBREG (omode, x, 0);
10721
10722 if (WORDS_BIG_ENDIAN)
10723 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10724
10725 /* Adjust the address so that the address-after-the-data is
10726 unchanged. */
10727 if (BYTES_BIG_ENDIAN)
10728 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10729
10730 return adjust_address_nv (x, omode, offset);
10731 }
10732
10733 /* If X is a comparison operator, rewrite it in a new mode. This
10734 probably won't match, but may allow further simplifications. */
10735 else if (COMPARISON_P (x))
10736 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10737
10738 /* If we couldn't simplify X any other way, just enclose it in a
10739 SUBREG. Normally, this SUBREG won't match, but some patterns may
10740 include an explicit SUBREG or we may simplify it further in combine. */
10741 else
10742 {
10743 int offset = 0;
10744 rtx res;
10745
10746 offset = subreg_lowpart_offset (omode, imode);
10747 if (imode == VOIDmode)
10748 {
10749 imode = int_mode_for_mode (omode);
10750 x = gen_lowpart_common (imode, x);
10751 if (x == NULL)
10752 goto fail;
10753 }
10754 res = simplify_gen_subreg (omode, x, imode, offset);
10755 if (res)
10756 return res;
10757 }
10758
10759 fail:
10760 return gen_rtx_CLOBBER (omode, const0_rtx);
10761 }
10762 \f
10763 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10764 comparison code that will be tested.
10765
10766 The result is a possibly different comparison code to use. *POP0 and
10767 *POP1 may be updated.
10768
10769 It is possible that we might detect that a comparison is either always
10770 true or always false. However, we do not perform general constant
10771 folding in combine, so this knowledge isn't useful. Such tautologies
10772 should have been detected earlier. Hence we ignore all such cases. */
10773
10774 static enum rtx_code
10775 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
10776 {
10777 rtx op0 = *pop0;
10778 rtx op1 = *pop1;
10779 rtx tem, tem1;
10780 int i;
10781 enum machine_mode mode, tmode;
10782
10783 /* Try a few ways of applying the same transformation to both operands. */
10784 while (1)
10785 {
10786 #ifndef WORD_REGISTER_OPERATIONS
10787 /* The test below this one won't handle SIGN_EXTENDs on these machines,
10788 so check specially. */
10789 if (code != GTU && code != GEU && code != LTU && code != LEU
10790 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10791 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10792 && GET_CODE (XEXP (op1, 0)) == ASHIFT
10793 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10794 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10795 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10796 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10797 && CONST_INT_P (XEXP (op0, 1))
10798 && XEXP (op0, 1) == XEXP (op1, 1)
10799 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10800 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
10801 && (INTVAL (XEXP (op0, 1))
10802 == (GET_MODE_BITSIZE (GET_MODE (op0))
10803 - (GET_MODE_BITSIZE
10804 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10805 {
10806 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10807 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10808 }
10809 #endif
10810
10811 /* If both operands are the same constant shift, see if we can ignore the
10812 shift. We can if the shift is a rotate or if the bits shifted out of
10813 this shift are known to be zero for both inputs and if the type of
10814 comparison is compatible with the shift. */
10815 if (GET_CODE (op0) == GET_CODE (op1)
10816 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10817 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10818 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10819 && (code != GT && code != LT && code != GE && code != LE))
10820 || (GET_CODE (op0) == ASHIFTRT
10821 && (code != GTU && code != LTU
10822 && code != GEU && code != LEU)))
10823 && CONST_INT_P (XEXP (op0, 1))
10824 && INTVAL (XEXP (op0, 1)) >= 0
10825 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10826 && XEXP (op0, 1) == XEXP (op1, 1))
10827 {
10828 enum machine_mode mode = GET_MODE (op0);
10829 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10830 int shift_count = INTVAL (XEXP (op0, 1));
10831
10832 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10833 mask &= (mask >> shift_count) << shift_count;
10834 else if (GET_CODE (op0) == ASHIFT)
10835 mask = (mask & (mask << shift_count)) >> shift_count;
10836
10837 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10838 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10839 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10840 else
10841 break;
10842 }
10843
10844 /* If both operands are AND's of a paradoxical SUBREG by constant, the
10845 SUBREGs are of the same mode, and, in both cases, the AND would
10846 be redundant if the comparison was done in the narrower mode,
10847 do the comparison in the narrower mode (e.g., we are AND'ing with 1
10848 and the operand's possibly nonzero bits are 0xffffff01; in that case
10849 if we only care about QImode, we don't need the AND). This case
10850 occurs if the output mode of an scc insn is not SImode and
10851 STORE_FLAG_VALUE == 1 (e.g., the 386).
10852
10853 Similarly, check for a case where the AND's are ZERO_EXTEND
10854 operations from some narrower mode even though a SUBREG is not
10855 present. */
10856
10857 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10858 && CONST_INT_P (XEXP (op0, 1))
10859 && CONST_INT_P (XEXP (op1, 1)))
10860 {
10861 rtx inner_op0 = XEXP (op0, 0);
10862 rtx inner_op1 = XEXP (op1, 0);
10863 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10864 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10865 int changed = 0;
10866
10867 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10868 && (GET_MODE_SIZE (GET_MODE (inner_op0))
10869 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10870 && (GET_MODE (SUBREG_REG (inner_op0))
10871 == GET_MODE (SUBREG_REG (inner_op1)))
10872 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10873 <= HOST_BITS_PER_WIDE_INT)
10874 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10875 GET_MODE (SUBREG_REG (inner_op0)))))
10876 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10877 GET_MODE (SUBREG_REG (inner_op1))))))
10878 {
10879 op0 = SUBREG_REG (inner_op0);
10880 op1 = SUBREG_REG (inner_op1);
10881
10882 /* The resulting comparison is always unsigned since we masked
10883 off the original sign bit. */
10884 code = unsigned_condition (code);
10885
10886 changed = 1;
10887 }
10888
10889 else if (c0 == c1)
10890 for (tmode = GET_CLASS_NARROWEST_MODE
10891 (GET_MODE_CLASS (GET_MODE (op0)));
10892 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10893 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10894 {
10895 op0 = gen_lowpart (tmode, inner_op0);
10896 op1 = gen_lowpart (tmode, inner_op1);
10897 code = unsigned_condition (code);
10898 changed = 1;
10899 break;
10900 }
10901
10902 if (! changed)
10903 break;
10904 }
10905
10906 /* If both operands are NOT, we can strip off the outer operation
10907 and adjust the comparison code for swapped operands; similarly for
10908 NEG, except that this must be an equality comparison. */
10909 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10910 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10911 && (code == EQ || code == NE)))
10912 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10913
10914 else
10915 break;
10916 }
10917
10918 /* If the first operand is a constant, swap the operands and adjust the
10919 comparison code appropriately, but don't do this if the second operand
10920 is already a constant integer. */
10921 if (swap_commutative_operands_p (op0, op1))
10922 {
10923 tem = op0, op0 = op1, op1 = tem;
10924 code = swap_condition (code);
10925 }
10926
10927 /* We now enter a loop during which we will try to simplify the comparison.
10928 For the most part, we only are concerned with comparisons with zero,
10929 but some things may really be comparisons with zero but not start
10930 out looking that way. */
10931
10932 while (CONST_INT_P (op1))
10933 {
10934 enum machine_mode mode = GET_MODE (op0);
10935 unsigned int mode_width = GET_MODE_BITSIZE (mode);
10936 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10937 int equality_comparison_p;
10938 int sign_bit_comparison_p;
10939 int unsigned_comparison_p;
10940 HOST_WIDE_INT const_op;
10941
10942 /* We only want to handle integral modes. This catches VOIDmode,
10943 CCmode, and the floating-point modes. An exception is that we
10944 can handle VOIDmode if OP0 is a COMPARE or a comparison
10945 operation. */
10946
10947 if (GET_MODE_CLASS (mode) != MODE_INT
10948 && ! (mode == VOIDmode
10949 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
10950 break;
10951
10952 /* Get the constant we are comparing against and turn off all bits
10953 not on in our mode. */
10954 const_op = INTVAL (op1);
10955 if (mode != VOIDmode)
10956 const_op = trunc_int_for_mode (const_op, mode);
10957 op1 = GEN_INT (const_op);
10958
10959 /* If we are comparing against a constant power of two and the value
10960 being compared can only have that single bit nonzero (e.g., it was
10961 `and'ed with that bit), we can replace this with a comparison
10962 with zero. */
10963 if (const_op
10964 && (code == EQ || code == NE || code == GE || code == GEU
10965 || code == LT || code == LTU)
10966 && mode_width <= HOST_BITS_PER_WIDE_INT
10967 && exact_log2 (const_op) >= 0
10968 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10969 {
10970 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10971 op1 = const0_rtx, const_op = 0;
10972 }
10973
10974 /* Similarly, if we are comparing a value known to be either -1 or
10975 0 with -1, change it to the opposite comparison against zero. */
10976
10977 if (const_op == -1
10978 && (code == EQ || code == NE || code == GT || code == LE
10979 || code == GEU || code == LTU)
10980 && num_sign_bit_copies (op0, mode) == mode_width)
10981 {
10982 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10983 op1 = const0_rtx, const_op = 0;
10984 }
10985
10986 /* Do some canonicalizations based on the comparison code. We prefer
10987 comparisons against zero and then prefer equality comparisons.
10988 If we can reduce the size of a constant, we will do that too. */
10989
10990 switch (code)
10991 {
10992 case LT:
10993 /* < C is equivalent to <= (C - 1) */
10994 if (const_op > 0)
10995 {
10996 const_op -= 1;
10997 op1 = GEN_INT (const_op);
10998 code = LE;
10999 /* ... fall through to LE case below. */
11000 }
11001 else
11002 break;
11003
11004 case LE:
11005 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11006 if (const_op < 0)
11007 {
11008 const_op += 1;
11009 op1 = GEN_INT (const_op);
11010 code = LT;
11011 }
11012
11013 /* If we are doing a <= 0 comparison on a value known to have
11014 a zero sign bit, we can replace this with == 0. */
11015 else if (const_op == 0
11016 && mode_width <= HOST_BITS_PER_WIDE_INT
11017 && (nonzero_bits (op0, mode)
11018 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11019 == 0)
11020 code = EQ;
11021 break;
11022
11023 case GE:
11024 /* >= C is equivalent to > (C - 1). */
11025 if (const_op > 0)
11026 {
11027 const_op -= 1;
11028 op1 = GEN_INT (const_op);
11029 code = GT;
11030 /* ... fall through to GT below. */
11031 }
11032 else
11033 break;
11034
11035 case GT:
11036 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11037 if (const_op < 0)
11038 {
11039 const_op += 1;
11040 op1 = GEN_INT (const_op);
11041 code = GE;
11042 }
11043
11044 /* If we are doing a > 0 comparison on a value known to have
11045 a zero sign bit, we can replace this with != 0. */
11046 else if (const_op == 0
11047 && mode_width <= HOST_BITS_PER_WIDE_INT
11048 && (nonzero_bits (op0, mode)
11049 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11050 == 0)
11051 code = NE;
11052 break;
11053
11054 case LTU:
11055 /* < C is equivalent to <= (C - 1). */
11056 if (const_op > 0)
11057 {
11058 const_op -= 1;
11059 op1 = GEN_INT (const_op);
11060 code = LEU;
11061 /* ... fall through ... */
11062 }
11063
11064 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11065 else if (mode_width <= HOST_BITS_PER_WIDE_INT
11066 && (unsigned HOST_WIDE_INT) const_op
11067 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11068 {
11069 const_op = 0, op1 = const0_rtx;
11070 code = GE;
11071 break;
11072 }
11073 else
11074 break;
11075
11076 case LEU:
11077 /* unsigned <= 0 is equivalent to == 0 */
11078 if (const_op == 0)
11079 code = EQ;
11080
11081 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11082 else if (mode_width <= HOST_BITS_PER_WIDE_INT
11083 && (unsigned HOST_WIDE_INT) const_op
11084 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11085 {
11086 const_op = 0, op1 = const0_rtx;
11087 code = GE;
11088 }
11089 break;
11090
11091 case GEU:
11092 /* >= C is equivalent to > (C - 1). */
11093 if (const_op > 1)
11094 {
11095 const_op -= 1;
11096 op1 = GEN_INT (const_op);
11097 code = GTU;
11098 /* ... fall through ... */
11099 }
11100
11101 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11102 else if (mode_width <= HOST_BITS_PER_WIDE_INT
11103 && (unsigned HOST_WIDE_INT) const_op
11104 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11105 {
11106 const_op = 0, op1 = const0_rtx;
11107 code = LT;
11108 break;
11109 }
11110 else
11111 break;
11112
11113 case GTU:
11114 /* unsigned > 0 is equivalent to != 0 */
11115 if (const_op == 0)
11116 code = NE;
11117
11118 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11119 else if (mode_width <= HOST_BITS_PER_WIDE_INT
11120 && (unsigned HOST_WIDE_INT) const_op
11121 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11122 {
11123 const_op = 0, op1 = const0_rtx;
11124 code = LT;
11125 }
11126 break;
11127
11128 default:
11129 break;
11130 }
11131
11132 /* Compute some predicates to simplify code below. */
11133
11134 equality_comparison_p = (code == EQ || code == NE);
11135 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11136 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11137 || code == GEU);
11138
11139 /* If this is a sign bit comparison and we can do arithmetic in
11140 MODE, say that we will only be needing the sign bit of OP0. */
11141 if (sign_bit_comparison_p
11142 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11143 op0 = force_to_mode (op0, mode,
11144 (unsigned HOST_WIDE_INT) 1
11145 << (GET_MODE_BITSIZE (mode) - 1),
11146 0);
11147
11148 /* Now try cases based on the opcode of OP0. If none of the cases
11149 does a "continue", we exit this loop immediately after the
11150 switch. */
11151
11152 switch (GET_CODE (op0))
11153 {
11154 case ZERO_EXTRACT:
11155 /* If we are extracting a single bit from a variable position in
11156 a constant that has only a single bit set and are comparing it
11157 with zero, we can convert this into an equality comparison
11158 between the position and the location of the single bit. */
11159 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11160 have already reduced the shift count modulo the word size. */
11161 if (!SHIFT_COUNT_TRUNCATED
11162 && CONST_INT_P (XEXP (op0, 0))
11163 && XEXP (op0, 1) == const1_rtx
11164 && equality_comparison_p && const_op == 0
11165 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11166 {
11167 if (BITS_BIG_ENDIAN)
11168 {
11169 enum machine_mode new_mode
11170 = mode_for_extraction (EP_extzv, 1);
11171 if (new_mode == MAX_MACHINE_MODE)
11172 i = BITS_PER_WORD - 1 - i;
11173 else
11174 {
11175 mode = new_mode;
11176 i = (GET_MODE_BITSIZE (mode) - 1 - i);
11177 }
11178 }
11179
11180 op0 = XEXP (op0, 2);
11181 op1 = GEN_INT (i);
11182 const_op = i;
11183
11184 /* Result is nonzero iff shift count is equal to I. */
11185 code = reverse_condition (code);
11186 continue;
11187 }
11188
11189 /* ... fall through ... */
11190
11191 case SIGN_EXTRACT:
11192 tem = expand_compound_operation (op0);
11193 if (tem != op0)
11194 {
11195 op0 = tem;
11196 continue;
11197 }
11198 break;
11199
11200 case NOT:
11201 /* If testing for equality, we can take the NOT of the constant. */
11202 if (equality_comparison_p
11203 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11204 {
11205 op0 = XEXP (op0, 0);
11206 op1 = tem;
11207 continue;
11208 }
11209
11210 /* If just looking at the sign bit, reverse the sense of the
11211 comparison. */
11212 if (sign_bit_comparison_p)
11213 {
11214 op0 = XEXP (op0, 0);
11215 code = (code == GE ? LT : GE);
11216 continue;
11217 }
11218 break;
11219
11220 case NEG:
11221 /* If testing for equality, we can take the NEG of the constant. */
11222 if (equality_comparison_p
11223 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11224 {
11225 op0 = XEXP (op0, 0);
11226 op1 = tem;
11227 continue;
11228 }
11229
11230 /* The remaining cases only apply to comparisons with zero. */
11231 if (const_op != 0)
11232 break;
11233
11234 /* When X is ABS or is known positive,
11235 (neg X) is < 0 if and only if X != 0. */
11236
11237 if (sign_bit_comparison_p
11238 && (GET_CODE (XEXP (op0, 0)) == ABS
11239 || (mode_width <= HOST_BITS_PER_WIDE_INT
11240 && (nonzero_bits (XEXP (op0, 0), mode)
11241 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11242 == 0)))
11243 {
11244 op0 = XEXP (op0, 0);
11245 code = (code == LT ? NE : EQ);
11246 continue;
11247 }
11248
11249 /* If we have NEG of something whose two high-order bits are the
11250 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11251 if (num_sign_bit_copies (op0, mode) >= 2)
11252 {
11253 op0 = XEXP (op0, 0);
11254 code = swap_condition (code);
11255 continue;
11256 }
11257 break;
11258
11259 case ROTATE:
11260 /* If we are testing equality and our count is a constant, we
11261 can perform the inverse operation on our RHS. */
11262 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11263 && (tem = simplify_binary_operation (ROTATERT, mode,
11264 op1, XEXP (op0, 1))) != 0)
11265 {
11266 op0 = XEXP (op0, 0);
11267 op1 = tem;
11268 continue;
11269 }
11270
11271 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11272 a particular bit. Convert it to an AND of a constant of that
11273 bit. This will be converted into a ZERO_EXTRACT. */
11274 if (const_op == 0 && sign_bit_comparison_p
11275 && CONST_INT_P (XEXP (op0, 1))
11276 && mode_width <= HOST_BITS_PER_WIDE_INT)
11277 {
11278 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11279 ((unsigned HOST_WIDE_INT) 1
11280 << (mode_width - 1
11281 - INTVAL (XEXP (op0, 1)))));
11282 code = (code == LT ? NE : EQ);
11283 continue;
11284 }
11285
11286 /* Fall through. */
11287
11288 case ABS:
11289 /* ABS is ignorable inside an equality comparison with zero. */
11290 if (const_op == 0 && equality_comparison_p)
11291 {
11292 op0 = XEXP (op0, 0);
11293 continue;
11294 }
11295 break;
11296
11297 case SIGN_EXTEND:
11298 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11299 (compare FOO CONST) if CONST fits in FOO's mode and we
11300 are either testing inequality or have an unsigned
11301 comparison with ZERO_EXTEND or a signed comparison with
11302 SIGN_EXTEND. But don't do it if we don't have a compare
11303 insn of the given mode, since we'd have to revert it
11304 later on, and then we wouldn't know whether to sign- or
11305 zero-extend. */
11306 mode = GET_MODE (XEXP (op0, 0));
11307 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11308 && ! unsigned_comparison_p
11309 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11310 && ((unsigned HOST_WIDE_INT) const_op
11311 < (((unsigned HOST_WIDE_INT) 1
11312 << (GET_MODE_BITSIZE (mode) - 1))))
11313 && have_insn_for (COMPARE, mode))
11314 {
11315 op0 = XEXP (op0, 0);
11316 continue;
11317 }
11318 break;
11319
11320 case SUBREG:
11321 /* Check for the case where we are comparing A - C1 with C2, that is
11322
11323 (subreg:MODE (plus (A) (-C1))) op (C2)
11324
11325 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11326 comparison in the wider mode. One of the following two conditions
11327 must be true in order for this to be valid:
11328
11329 1. The mode extension results in the same bit pattern being added
11330 on both sides and the comparison is equality or unsigned. As
11331 C2 has been truncated to fit in MODE, the pattern can only be
11332 all 0s or all 1s.
11333
11334 2. The mode extension results in the sign bit being copied on
11335 each side.
11336
11337 The difficulty here is that we have predicates for A but not for
11338 (A - C1) so we need to check that C1 is within proper bounds so
11339 as to perturbate A as little as possible. */
11340
11341 if (mode_width <= HOST_BITS_PER_WIDE_INT
11342 && subreg_lowpart_p (op0)
11343 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
11344 && GET_CODE (SUBREG_REG (op0)) == PLUS
11345 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11346 {
11347 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11348 rtx a = XEXP (SUBREG_REG (op0), 0);
11349 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11350
11351 if ((c1 > 0
11352 && (unsigned HOST_WIDE_INT) c1
11353 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11354 && (equality_comparison_p || unsigned_comparison_p)
11355 /* (A - C1) zero-extends if it is positive and sign-extends
11356 if it is negative, C2 both zero- and sign-extends. */
11357 && ((0 == (nonzero_bits (a, inner_mode)
11358 & ~GET_MODE_MASK (mode))
11359 && const_op >= 0)
11360 /* (A - C1) sign-extends if it is positive and 1-extends
11361 if it is negative, C2 both sign- and 1-extends. */
11362 || (num_sign_bit_copies (a, inner_mode)
11363 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
11364 - mode_width)
11365 && const_op < 0)))
11366 || ((unsigned HOST_WIDE_INT) c1
11367 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11368 /* (A - C1) always sign-extends, like C2. */
11369 && num_sign_bit_copies (a, inner_mode)
11370 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
11371 - (mode_width - 1))))
11372 {
11373 op0 = SUBREG_REG (op0);
11374 continue;
11375 }
11376 }
11377
11378 /* If the inner mode is narrower and we are extracting the low part,
11379 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11380 if (subreg_lowpart_p (op0)
11381 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
11382 /* Fall through */ ;
11383 else
11384 break;
11385
11386 /* ... fall through ... */
11387
11388 case ZERO_EXTEND:
11389 mode = GET_MODE (XEXP (op0, 0));
11390 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11391 && (unsigned_comparison_p || equality_comparison_p)
11392 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11393 && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
11394 && have_insn_for (COMPARE, mode))
11395 {
11396 op0 = XEXP (op0, 0);
11397 continue;
11398 }
11399 break;
11400
11401 case PLUS:
11402 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11403 this for equality comparisons due to pathological cases involving
11404 overflows. */
11405 if (equality_comparison_p
11406 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11407 op1, XEXP (op0, 1))))
11408 {
11409 op0 = XEXP (op0, 0);
11410 op1 = tem;
11411 continue;
11412 }
11413
11414 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11415 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11416 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11417 {
11418 op0 = XEXP (XEXP (op0, 0), 0);
11419 code = (code == LT ? EQ : NE);
11420 continue;
11421 }
11422 break;
11423
11424 case MINUS:
11425 /* We used to optimize signed comparisons against zero, but that
11426 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11427 arrive here as equality comparisons, or (GEU, LTU) are
11428 optimized away. No need to special-case them. */
11429
11430 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11431 (eq B (minus A C)), whichever simplifies. We can only do
11432 this for equality comparisons due to pathological cases involving
11433 overflows. */
11434 if (equality_comparison_p
11435 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11436 XEXP (op0, 1), op1)))
11437 {
11438 op0 = XEXP (op0, 0);
11439 op1 = tem;
11440 continue;
11441 }
11442
11443 if (equality_comparison_p
11444 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11445 XEXP (op0, 0), op1)))
11446 {
11447 op0 = XEXP (op0, 1);
11448 op1 = tem;
11449 continue;
11450 }
11451
11452 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11453 of bits in X minus 1, is one iff X > 0. */
11454 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11455 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11456 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11457 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11458 {
11459 op0 = XEXP (op0, 1);
11460 code = (code == GE ? LE : GT);
11461 continue;
11462 }
11463 break;
11464
11465 case XOR:
11466 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11467 if C is zero or B is a constant. */
11468 if (equality_comparison_p
11469 && 0 != (tem = simplify_binary_operation (XOR, mode,
11470 XEXP (op0, 1), op1)))
11471 {
11472 op0 = XEXP (op0, 0);
11473 op1 = tem;
11474 continue;
11475 }
11476 break;
11477
11478 case EQ: case NE:
11479 case UNEQ: case LTGT:
11480 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11481 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11482 case UNORDERED: case ORDERED:
11483 /* We can't do anything if OP0 is a condition code value, rather
11484 than an actual data value. */
11485 if (const_op != 0
11486 || CC0_P (XEXP (op0, 0))
11487 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11488 break;
11489
11490 /* Get the two operands being compared. */
11491 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11492 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11493 else
11494 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11495
11496 /* Check for the cases where we simply want the result of the
11497 earlier test or the opposite of that result. */
11498 if (code == NE || code == EQ
11499 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
11500 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11501 && (STORE_FLAG_VALUE
11502 & (((unsigned HOST_WIDE_INT) 1
11503 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
11504 && (code == LT || code == GE)))
11505 {
11506 enum rtx_code new_code;
11507 if (code == LT || code == NE)
11508 new_code = GET_CODE (op0);
11509 else
11510 new_code = reversed_comparison_code (op0, NULL);
11511
11512 if (new_code != UNKNOWN)
11513 {
11514 code = new_code;
11515 op0 = tem;
11516 op1 = tem1;
11517 continue;
11518 }
11519 }
11520 break;
11521
11522 case IOR:
11523 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11524 iff X <= 0. */
11525 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11526 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11527 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11528 {
11529 op0 = XEXP (op0, 1);
11530 code = (code == GE ? GT : LE);
11531 continue;
11532 }
11533 break;
11534
11535 case AND:
11536 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11537 will be converted to a ZERO_EXTRACT later. */
11538 if (const_op == 0 && equality_comparison_p
11539 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11540 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11541 {
11542 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11543 XEXP (XEXP (op0, 0), 1));
11544 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11545 continue;
11546 }
11547
11548 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11549 zero and X is a comparison and C1 and C2 describe only bits set
11550 in STORE_FLAG_VALUE, we can compare with X. */
11551 if (const_op == 0 && equality_comparison_p
11552 && mode_width <= HOST_BITS_PER_WIDE_INT
11553 && CONST_INT_P (XEXP (op0, 1))
11554 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11555 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11556 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11557 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11558 {
11559 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11560 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11561 if ((~STORE_FLAG_VALUE & mask) == 0
11562 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11563 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11564 && COMPARISON_P (tem))))
11565 {
11566 op0 = XEXP (XEXP (op0, 0), 0);
11567 continue;
11568 }
11569 }
11570
11571 /* If we are doing an equality comparison of an AND of a bit equal
11572 to the sign bit, replace this with a LT or GE comparison of
11573 the underlying value. */
11574 if (equality_comparison_p
11575 && const_op == 0
11576 && CONST_INT_P (XEXP (op0, 1))
11577 && mode_width <= HOST_BITS_PER_WIDE_INT
11578 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11579 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11580 {
11581 op0 = XEXP (op0, 0);
11582 code = (code == EQ ? GE : LT);
11583 continue;
11584 }
11585
11586 /* If this AND operation is really a ZERO_EXTEND from a narrower
11587 mode, the constant fits within that mode, and this is either an
11588 equality or unsigned comparison, try to do this comparison in
11589 the narrower mode.
11590
11591 Note that in:
11592
11593 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11594 -> (ne:DI (reg:SI 4) (const_int 0))
11595
11596 unless TRULY_NOOP_TRUNCATION allows it or the register is
11597 known to hold a value of the required mode the
11598 transformation is invalid. */
11599 if ((equality_comparison_p || unsigned_comparison_p)
11600 && CONST_INT_P (XEXP (op0, 1))
11601 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11602 & GET_MODE_MASK (mode))
11603 + 1)) >= 0
11604 && const_op >> i == 0
11605 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11606 && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
11607 GET_MODE_BITSIZE (GET_MODE (op0)))
11608 || (REG_P (XEXP (op0, 0))
11609 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11610 {
11611 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11612 continue;
11613 }
11614
11615 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11616 fits in both M1 and M2 and the SUBREG is either paradoxical
11617 or represents the low part, permute the SUBREG and the AND
11618 and try again. */
11619 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11620 {
11621 unsigned HOST_WIDE_INT c1;
11622 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11623 /* Require an integral mode, to avoid creating something like
11624 (AND:SF ...). */
11625 if (SCALAR_INT_MODE_P (tmode)
11626 /* It is unsafe to commute the AND into the SUBREG if the
11627 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11628 not defined. As originally written the upper bits
11629 have a defined value due to the AND operation.
11630 However, if we commute the AND inside the SUBREG then
11631 they no longer have defined values and the meaning of
11632 the code has been changed. */
11633 && (0
11634 #ifdef WORD_REGISTER_OPERATIONS
11635 || (mode_width > GET_MODE_BITSIZE (tmode)
11636 && mode_width <= BITS_PER_WORD)
11637 #endif
11638 || (mode_width <= GET_MODE_BITSIZE (tmode)
11639 && subreg_lowpart_p (XEXP (op0, 0))))
11640 && CONST_INT_P (XEXP (op0, 1))
11641 && mode_width <= HOST_BITS_PER_WIDE_INT
11642 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
11643 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11644 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11645 && c1 != mask
11646 && c1 != GET_MODE_MASK (tmode))
11647 {
11648 op0 = simplify_gen_binary (AND, tmode,
11649 SUBREG_REG (XEXP (op0, 0)),
11650 gen_int_mode (c1, tmode));
11651 op0 = gen_lowpart (mode, op0);
11652 continue;
11653 }
11654 }
11655
11656 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11657 if (const_op == 0 && equality_comparison_p
11658 && XEXP (op0, 1) == const1_rtx
11659 && GET_CODE (XEXP (op0, 0)) == NOT)
11660 {
11661 op0 = simplify_and_const_int (NULL_RTX, mode,
11662 XEXP (XEXP (op0, 0), 0), 1);
11663 code = (code == NE ? EQ : NE);
11664 continue;
11665 }
11666
11667 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11668 (eq (and (lshiftrt X) 1) 0).
11669 Also handle the case where (not X) is expressed using xor. */
11670 if (const_op == 0 && equality_comparison_p
11671 && XEXP (op0, 1) == const1_rtx
11672 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11673 {
11674 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11675 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11676
11677 if (GET_CODE (shift_op) == NOT
11678 || (GET_CODE (shift_op) == XOR
11679 && CONST_INT_P (XEXP (shift_op, 1))
11680 && CONST_INT_P (shift_count)
11681 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
11682 && (UINTVAL (XEXP (shift_op, 1))
11683 == (unsigned HOST_WIDE_INT) 1
11684 << INTVAL (shift_count))))
11685 {
11686 op0
11687 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11688 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11689 code = (code == NE ? EQ : NE);
11690 continue;
11691 }
11692 }
11693 break;
11694
11695 case ASHIFT:
11696 /* If we have (compare (ashift FOO N) (const_int C)) and
11697 the high order N bits of FOO (N+1 if an inequality comparison)
11698 are known to be zero, we can do this by comparing FOO with C
11699 shifted right N bits so long as the low-order N bits of C are
11700 zero. */
11701 if (CONST_INT_P (XEXP (op0, 1))
11702 && INTVAL (XEXP (op0, 1)) >= 0
11703 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11704 < HOST_BITS_PER_WIDE_INT)
11705 && (((unsigned HOST_WIDE_INT) const_op
11706 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11707 - 1)) == 0)
11708 && mode_width <= HOST_BITS_PER_WIDE_INT
11709 && (nonzero_bits (XEXP (op0, 0), mode)
11710 & ~(mask >> (INTVAL (XEXP (op0, 1))
11711 + ! equality_comparison_p))) == 0)
11712 {
11713 /* We must perform a logical shift, not an arithmetic one,
11714 as we want the top N bits of C to be zero. */
11715 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11716
11717 temp >>= INTVAL (XEXP (op0, 1));
11718 op1 = gen_int_mode (temp, mode);
11719 op0 = XEXP (op0, 0);
11720 continue;
11721 }
11722
11723 /* If we are doing a sign bit comparison, it means we are testing
11724 a particular bit. Convert it to the appropriate AND. */
11725 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11726 && mode_width <= HOST_BITS_PER_WIDE_INT)
11727 {
11728 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11729 ((unsigned HOST_WIDE_INT) 1
11730 << (mode_width - 1
11731 - INTVAL (XEXP (op0, 1)))));
11732 code = (code == LT ? NE : EQ);
11733 continue;
11734 }
11735
11736 /* If this an equality comparison with zero and we are shifting
11737 the low bit to the sign bit, we can convert this to an AND of the
11738 low-order bit. */
11739 if (const_op == 0 && equality_comparison_p
11740 && CONST_INT_P (XEXP (op0, 1))
11741 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11742 {
11743 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
11744 continue;
11745 }
11746 break;
11747
11748 case ASHIFTRT:
11749 /* If this is an equality comparison with zero, we can do this
11750 as a logical shift, which might be much simpler. */
11751 if (equality_comparison_p && const_op == 0
11752 && CONST_INT_P (XEXP (op0, 1)))
11753 {
11754 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11755 XEXP (op0, 0),
11756 INTVAL (XEXP (op0, 1)));
11757 continue;
11758 }
11759
11760 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11761 do the comparison in a narrower mode. */
11762 if (! unsigned_comparison_p
11763 && CONST_INT_P (XEXP (op0, 1))
11764 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11765 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11766 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11767 MODE_INT, 1)) != BLKmode
11768 && (((unsigned HOST_WIDE_INT) const_op
11769 + (GET_MODE_MASK (tmode) >> 1) + 1)
11770 <= GET_MODE_MASK (tmode)))
11771 {
11772 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11773 continue;
11774 }
11775
11776 /* Likewise if OP0 is a PLUS of a sign extension with a
11777 constant, which is usually represented with the PLUS
11778 between the shifts. */
11779 if (! unsigned_comparison_p
11780 && CONST_INT_P (XEXP (op0, 1))
11781 && GET_CODE (XEXP (op0, 0)) == PLUS
11782 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11783 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11784 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11785 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11786 MODE_INT, 1)) != BLKmode
11787 && (((unsigned HOST_WIDE_INT) const_op
11788 + (GET_MODE_MASK (tmode) >> 1) + 1)
11789 <= GET_MODE_MASK (tmode)))
11790 {
11791 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11792 rtx add_const = XEXP (XEXP (op0, 0), 1);
11793 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11794 add_const, XEXP (op0, 1));
11795
11796 op0 = simplify_gen_binary (PLUS, tmode,
11797 gen_lowpart (tmode, inner),
11798 new_const);
11799 continue;
11800 }
11801
11802 /* ... fall through ... */
11803 case LSHIFTRT:
11804 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11805 the low order N bits of FOO are known to be zero, we can do this
11806 by comparing FOO with C shifted left N bits so long as no
11807 overflow occurs. Even if the low order N bits of FOO aren't known
11808 to be zero, if the comparison is >= or < we can use the same
11809 optimization and for > or <= by setting all the low
11810 order N bits in the comparison constant. */
11811 if (CONST_INT_P (XEXP (op0, 1))
11812 && INTVAL (XEXP (op0, 1)) > 0
11813 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11814 && mode_width <= HOST_BITS_PER_WIDE_INT
11815 && (((unsigned HOST_WIDE_INT) const_op
11816 + (GET_CODE (op0) != LSHIFTRT
11817 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11818 + 1)
11819 : 0))
11820 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11821 {
11822 unsigned HOST_WIDE_INT low_bits
11823 = (nonzero_bits (XEXP (op0, 0), mode)
11824 & (((unsigned HOST_WIDE_INT) 1
11825 << INTVAL (XEXP (op0, 1))) - 1));
11826 if (low_bits == 0 || !equality_comparison_p)
11827 {
11828 /* If the shift was logical, then we must make the condition
11829 unsigned. */
11830 if (GET_CODE (op0) == LSHIFTRT)
11831 code = unsigned_condition (code);
11832
11833 const_op <<= INTVAL (XEXP (op0, 1));
11834 if (low_bits != 0
11835 && (code == GT || code == GTU
11836 || code == LE || code == LEU))
11837 const_op
11838 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
11839 op1 = GEN_INT (const_op);
11840 op0 = XEXP (op0, 0);
11841 continue;
11842 }
11843 }
11844
11845 /* If we are using this shift to extract just the sign bit, we
11846 can replace this with an LT or GE comparison. */
11847 if (const_op == 0
11848 && (equality_comparison_p || sign_bit_comparison_p)
11849 && CONST_INT_P (XEXP (op0, 1))
11850 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11851 {
11852 op0 = XEXP (op0, 0);
11853 code = (code == NE || code == GT ? LT : GE);
11854 continue;
11855 }
11856 break;
11857
11858 default:
11859 break;
11860 }
11861
11862 break;
11863 }
11864
11865 /* Now make any compound operations involved in this comparison. Then,
11866 check for an outmost SUBREG on OP0 that is not doing anything or is
11867 paradoxical. The latter transformation must only be performed when
11868 it is known that the "extra" bits will be the same in op0 and op1 or
11869 that they don't matter. There are three cases to consider:
11870
11871 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11872 care bits and we can assume they have any convenient value. So
11873 making the transformation is safe.
11874
11875 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11876 In this case the upper bits of op0 are undefined. We should not make
11877 the simplification in that case as we do not know the contents of
11878 those bits.
11879
11880 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11881 UNKNOWN. In that case we know those bits are zeros or ones. We must
11882 also be sure that they are the same as the upper bits of op1.
11883
11884 We can never remove a SUBREG for a non-equality comparison because
11885 the sign bit is in a different place in the underlying object. */
11886
11887 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11888 op1 = make_compound_operation (op1, SET);
11889
11890 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11891 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11892 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11893 && (code == NE || code == EQ))
11894 {
11895 if (GET_MODE_SIZE (GET_MODE (op0))
11896 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11897 {
11898 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
11899 implemented. */
11900 if (REG_P (SUBREG_REG (op0)))
11901 {
11902 op0 = SUBREG_REG (op0);
11903 op1 = gen_lowpart (GET_MODE (op0), op1);
11904 }
11905 }
11906 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11907 <= HOST_BITS_PER_WIDE_INT)
11908 && (nonzero_bits (SUBREG_REG (op0),
11909 GET_MODE (SUBREG_REG (op0)))
11910 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11911 {
11912 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11913
11914 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11915 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11916 op0 = SUBREG_REG (op0), op1 = tem;
11917 }
11918 }
11919
11920 /* We now do the opposite procedure: Some machines don't have compare
11921 insns in all modes. If OP0's mode is an integer mode smaller than a
11922 word and we can't do a compare in that mode, see if there is a larger
11923 mode for which we can do the compare. There are a number of cases in
11924 which we can use the wider mode. */
11925
11926 mode = GET_MODE (op0);
11927 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11928 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11929 && ! have_insn_for (COMPARE, mode))
11930 for (tmode = GET_MODE_WIDER_MODE (mode);
11931 (tmode != VOIDmode
11932 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11933 tmode = GET_MODE_WIDER_MODE (tmode))
11934 if (have_insn_for (COMPARE, tmode))
11935 {
11936 int zero_extended;
11937
11938 /* If this is a test for negative, we can make an explicit
11939 test of the sign bit. Test this first so we can use
11940 a paradoxical subreg to extend OP0. */
11941
11942 if (op1 == const0_rtx && (code == LT || code == GE)
11943 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11944 {
11945 op0 = simplify_gen_binary (AND, tmode,
11946 gen_lowpart (tmode, op0),
11947 GEN_INT ((unsigned HOST_WIDE_INT) 1
11948 << (GET_MODE_BITSIZE (mode)
11949 - 1)));
11950 code = (code == LT) ? NE : EQ;
11951 break;
11952 }
11953
11954 /* If the only nonzero bits in OP0 and OP1 are those in the
11955 narrower mode and this is an equality or unsigned comparison,
11956 we can use the wider mode. Similarly for sign-extended
11957 values, in which case it is true for all comparisons. */
11958 zero_extended = ((code == EQ || code == NE
11959 || code == GEU || code == GTU
11960 || code == LEU || code == LTU)
11961 && (nonzero_bits (op0, tmode)
11962 & ~GET_MODE_MASK (mode)) == 0
11963 && ((CONST_INT_P (op1)
11964 || (nonzero_bits (op1, tmode)
11965 & ~GET_MODE_MASK (mode)) == 0)));
11966
11967 if (zero_extended
11968 || ((num_sign_bit_copies (op0, tmode)
11969 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11970 - GET_MODE_BITSIZE (mode)))
11971 && (num_sign_bit_copies (op1, tmode)
11972 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11973 - GET_MODE_BITSIZE (mode)))))
11974 {
11975 /* If OP0 is an AND and we don't have an AND in MODE either,
11976 make a new AND in the proper mode. */
11977 if (GET_CODE (op0) == AND
11978 && !have_insn_for (AND, mode))
11979 op0 = simplify_gen_binary (AND, tmode,
11980 gen_lowpart (tmode,
11981 XEXP (op0, 0)),
11982 gen_lowpart (tmode,
11983 XEXP (op0, 1)));
11984 else
11985 {
11986 if (zero_extended)
11987 {
11988 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
11989 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
11990 }
11991 else
11992 {
11993 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
11994 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
11995 }
11996 break;
11997 }
11998 }
11999 }
12000
12001 #ifdef CANONICALIZE_COMPARISON
12002 /* If this machine only supports a subset of valid comparisons, see if we
12003 can convert an unsupported one into a supported one. */
12004 CANONICALIZE_COMPARISON (code, op0, op1);
12005 #endif
12006
12007 *pop0 = op0;
12008 *pop1 = op1;
12009
12010 return code;
12011 }
12012 \f
12013 /* Utility function for record_value_for_reg. Count number of
12014 rtxs in X. */
12015 static int
12016 count_rtxs (rtx x)
12017 {
12018 enum rtx_code code = GET_CODE (x);
12019 const char *fmt;
12020 int i, j, ret = 1;
12021
12022 if (GET_RTX_CLASS (code) == '2'
12023 || GET_RTX_CLASS (code) == 'c')
12024 {
12025 rtx x0 = XEXP (x, 0);
12026 rtx x1 = XEXP (x, 1);
12027
12028 if (x0 == x1)
12029 return 1 + 2 * count_rtxs (x0);
12030
12031 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
12032 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
12033 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12034 return 2 + 2 * count_rtxs (x0)
12035 + count_rtxs (x == XEXP (x1, 0)
12036 ? XEXP (x1, 1) : XEXP (x1, 0));
12037
12038 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
12039 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
12040 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12041 return 2 + 2 * count_rtxs (x1)
12042 + count_rtxs (x == XEXP (x0, 0)
12043 ? XEXP (x0, 1) : XEXP (x0, 0));
12044 }
12045
12046 fmt = GET_RTX_FORMAT (code);
12047 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12048 if (fmt[i] == 'e')
12049 ret += count_rtxs (XEXP (x, i));
12050 else if (fmt[i] == 'E')
12051 for (j = 0; j < XVECLEN (x, i); j++)
12052 ret += count_rtxs (XVECEXP (x, i, j));
12053
12054 return ret;
12055 }
12056 \f
12057 /* Utility function for following routine. Called when X is part of a value
12058 being stored into last_set_value. Sets last_set_table_tick
12059 for each register mentioned. Similar to mention_regs in cse.c */
12060
12061 static void
12062 update_table_tick (rtx x)
12063 {
12064 enum rtx_code code = GET_CODE (x);
12065 const char *fmt = GET_RTX_FORMAT (code);
12066 int i, j;
12067
12068 if (code == REG)
12069 {
12070 unsigned int regno = REGNO (x);
12071 unsigned int endregno = END_REGNO (x);
12072 unsigned int r;
12073
12074 for (r = regno; r < endregno; r++)
12075 {
12076 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, r);
12077 rsp->last_set_table_tick = label_tick;
12078 }
12079
12080 return;
12081 }
12082
12083 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12084 if (fmt[i] == 'e')
12085 {
12086 /* Check for identical subexpressions. If x contains
12087 identical subexpression we only have to traverse one of
12088 them. */
12089 if (i == 0 && ARITHMETIC_P (x))
12090 {
12091 /* Note that at this point x1 has already been
12092 processed. */
12093 rtx x0 = XEXP (x, 0);
12094 rtx x1 = XEXP (x, 1);
12095
12096 /* If x0 and x1 are identical then there is no need to
12097 process x0. */
12098 if (x0 == x1)
12099 break;
12100
12101 /* If x0 is identical to a subexpression of x1 then while
12102 processing x1, x0 has already been processed. Thus we
12103 are done with x. */
12104 if (ARITHMETIC_P (x1)
12105 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12106 break;
12107
12108 /* If x1 is identical to a subexpression of x0 then we
12109 still have to process the rest of x0. */
12110 if (ARITHMETIC_P (x0)
12111 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12112 {
12113 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12114 break;
12115 }
12116 }
12117
12118 update_table_tick (XEXP (x, i));
12119 }
12120 else if (fmt[i] == 'E')
12121 for (j = 0; j < XVECLEN (x, i); j++)
12122 update_table_tick (XVECEXP (x, i, j));
12123 }
12124
12125 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12126 are saying that the register is clobbered and we no longer know its
12127 value. If INSN is zero, don't update reg_stat[].last_set; this is
12128 only permitted with VALUE also zero and is used to invalidate the
12129 register. */
12130
12131 static void
12132 record_value_for_reg (rtx reg, rtx insn, rtx value)
12133 {
12134 unsigned int regno = REGNO (reg);
12135 unsigned int endregno = END_REGNO (reg);
12136 unsigned int i;
12137 reg_stat_type *rsp;
12138
12139 /* If VALUE contains REG and we have a previous value for REG, substitute
12140 the previous value. */
12141 if (value && insn && reg_overlap_mentioned_p (reg, value))
12142 {
12143 rtx tem;
12144
12145 /* Set things up so get_last_value is allowed to see anything set up to
12146 our insn. */
12147 subst_low_luid = DF_INSN_LUID (insn);
12148 tem = get_last_value (reg);
12149
12150 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12151 it isn't going to be useful and will take a lot of time to process,
12152 so just use the CLOBBER. */
12153
12154 if (tem)
12155 {
12156 if (ARITHMETIC_P (tem)
12157 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12158 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12159 tem = XEXP (tem, 0);
12160 else if (count_occurrences (value, reg, 1) >= 2)
12161 {
12162 /* If there are two or more occurrences of REG in VALUE,
12163 prevent the value from growing too much. */
12164 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12165 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12166 }
12167
12168 value = replace_rtx (copy_rtx (value), reg, tem);
12169 }
12170 }
12171
12172 /* For each register modified, show we don't know its value, that
12173 we don't know about its bitwise content, that its value has been
12174 updated, and that we don't know the location of the death of the
12175 register. */
12176 for (i = regno; i < endregno; i++)
12177 {
12178 rsp = VEC_index (reg_stat_type, reg_stat, i);
12179
12180 if (insn)
12181 rsp->last_set = insn;
12182
12183 rsp->last_set_value = 0;
12184 rsp->last_set_mode = VOIDmode;
12185 rsp->last_set_nonzero_bits = 0;
12186 rsp->last_set_sign_bit_copies = 0;
12187 rsp->last_death = 0;
12188 rsp->truncated_to_mode = VOIDmode;
12189 }
12190
12191 /* Mark registers that are being referenced in this value. */
12192 if (value)
12193 update_table_tick (value);
12194
12195 /* Now update the status of each register being set.
12196 If someone is using this register in this block, set this register
12197 to invalid since we will get confused between the two lives in this
12198 basic block. This makes using this register always invalid. In cse, we
12199 scan the table to invalidate all entries using this register, but this
12200 is too much work for us. */
12201
12202 for (i = regno; i < endregno; i++)
12203 {
12204 rsp = VEC_index (reg_stat_type, reg_stat, i);
12205 rsp->last_set_label = label_tick;
12206 if (!insn
12207 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12208 rsp->last_set_invalid = 1;
12209 else
12210 rsp->last_set_invalid = 0;
12211 }
12212
12213 /* The value being assigned might refer to X (like in "x++;"). In that
12214 case, we must replace it with (clobber (const_int 0)) to prevent
12215 infinite loops. */
12216 rsp = VEC_index (reg_stat_type, reg_stat, regno);
12217 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12218 {
12219 value = copy_rtx (value);
12220 if (!get_last_value_validate (&value, insn, label_tick, 1))
12221 value = 0;
12222 }
12223
12224 /* For the main register being modified, update the value, the mode, the
12225 nonzero bits, and the number of sign bit copies. */
12226
12227 rsp->last_set_value = value;
12228
12229 if (value)
12230 {
12231 enum machine_mode mode = GET_MODE (reg);
12232 subst_low_luid = DF_INSN_LUID (insn);
12233 rsp->last_set_mode = mode;
12234 if (GET_MODE_CLASS (mode) == MODE_INT
12235 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12236 mode = nonzero_bits_mode;
12237 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12238 rsp->last_set_sign_bit_copies
12239 = num_sign_bit_copies (value, GET_MODE (reg));
12240 }
12241 }
12242
12243 /* Called via note_stores from record_dead_and_set_regs to handle one
12244 SET or CLOBBER in an insn. DATA is the instruction in which the
12245 set is occurring. */
12246
12247 static void
12248 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12249 {
12250 rtx record_dead_insn = (rtx) data;
12251
12252 if (GET_CODE (dest) == SUBREG)
12253 dest = SUBREG_REG (dest);
12254
12255 if (!record_dead_insn)
12256 {
12257 if (REG_P (dest))
12258 record_value_for_reg (dest, NULL_RTX, NULL_RTX);
12259 return;
12260 }
12261
12262 if (REG_P (dest))
12263 {
12264 /* If we are setting the whole register, we know its value. Otherwise
12265 show that we don't know the value. We can handle SUBREG in
12266 some cases. */
12267 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12268 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12269 else if (GET_CODE (setter) == SET
12270 && GET_CODE (SET_DEST (setter)) == SUBREG
12271 && SUBREG_REG (SET_DEST (setter)) == dest
12272 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
12273 && subreg_lowpart_p (SET_DEST (setter)))
12274 record_value_for_reg (dest, record_dead_insn,
12275 gen_lowpart (GET_MODE (dest),
12276 SET_SRC (setter)));
12277 else
12278 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12279 }
12280 else if (MEM_P (dest)
12281 /* Ignore pushes, they clobber nothing. */
12282 && ! push_operand (dest, GET_MODE (dest)))
12283 mem_last_set = DF_INSN_LUID (record_dead_insn);
12284 }
12285
12286 /* Update the records of when each REG was most recently set or killed
12287 for the things done by INSN. This is the last thing done in processing
12288 INSN in the combiner loop.
12289
12290 We update reg_stat[], in particular fields last_set, last_set_value,
12291 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12292 last_death, and also the similar information mem_last_set (which insn
12293 most recently modified memory) and last_call_luid (which insn was the
12294 most recent subroutine call). */
12295
12296 static void
12297 record_dead_and_set_regs (rtx insn)
12298 {
12299 rtx link;
12300 unsigned int i;
12301
12302 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12303 {
12304 if (REG_NOTE_KIND (link) == REG_DEAD
12305 && REG_P (XEXP (link, 0)))
12306 {
12307 unsigned int regno = REGNO (XEXP (link, 0));
12308 unsigned int endregno = END_REGNO (XEXP (link, 0));
12309
12310 for (i = regno; i < endregno; i++)
12311 {
12312 reg_stat_type *rsp;
12313
12314 rsp = VEC_index (reg_stat_type, reg_stat, i);
12315 rsp->last_death = insn;
12316 }
12317 }
12318 else if (REG_NOTE_KIND (link) == REG_INC)
12319 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12320 }
12321
12322 if (CALL_P (insn))
12323 {
12324 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
12325 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
12326 {
12327 reg_stat_type *rsp;
12328
12329 rsp = VEC_index (reg_stat_type, reg_stat, i);
12330 rsp->last_set_invalid = 1;
12331 rsp->last_set = insn;
12332 rsp->last_set_value = 0;
12333 rsp->last_set_mode = VOIDmode;
12334 rsp->last_set_nonzero_bits = 0;
12335 rsp->last_set_sign_bit_copies = 0;
12336 rsp->last_death = 0;
12337 rsp->truncated_to_mode = VOIDmode;
12338 }
12339
12340 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12341
12342 /* We can't combine into a call pattern. Remember, though, that
12343 the return value register is set at this LUID. We could
12344 still replace a register with the return value from the
12345 wrong subroutine call! */
12346 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12347 }
12348 else
12349 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12350 }
12351
12352 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12353 register present in the SUBREG, so for each such SUBREG go back and
12354 adjust nonzero and sign bit information of the registers that are
12355 known to have some zero/sign bits set.
12356
12357 This is needed because when combine blows the SUBREGs away, the
12358 information on zero/sign bits is lost and further combines can be
12359 missed because of that. */
12360
12361 static void
12362 record_promoted_value (rtx insn, rtx subreg)
12363 {
12364 struct insn_link *links;
12365 rtx set;
12366 unsigned int regno = REGNO (SUBREG_REG (subreg));
12367 enum machine_mode mode = GET_MODE (subreg);
12368
12369 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
12370 return;
12371
12372 for (links = LOG_LINKS (insn); links;)
12373 {
12374 reg_stat_type *rsp;
12375
12376 insn = links->insn;
12377 set = single_set (insn);
12378
12379 if (! set || !REG_P (SET_DEST (set))
12380 || REGNO (SET_DEST (set)) != regno
12381 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12382 {
12383 links = links->next;
12384 continue;
12385 }
12386
12387 rsp = VEC_index (reg_stat_type, reg_stat, regno);
12388 if (rsp->last_set == insn)
12389 {
12390 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
12391 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12392 }
12393
12394 if (REG_P (SET_SRC (set)))
12395 {
12396 regno = REGNO (SET_SRC (set));
12397 links = LOG_LINKS (insn);
12398 }
12399 else
12400 break;
12401 }
12402 }
12403
12404 /* Check if X, a register, is known to contain a value already
12405 truncated to MODE. In this case we can use a subreg to refer to
12406 the truncated value even though in the generic case we would need
12407 an explicit truncation. */
12408
12409 static bool
12410 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
12411 {
12412 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
12413 enum machine_mode truncated = rsp->truncated_to_mode;
12414
12415 if (truncated == 0
12416 || rsp->truncation_label < label_tick_ebb_start)
12417 return false;
12418 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12419 return true;
12420 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
12421 GET_MODE_BITSIZE (truncated)))
12422 return true;
12423 return false;
12424 }
12425
12426 /* Callback for for_each_rtx. If *P is a hard reg or a subreg record the mode
12427 that the register is accessed in. For non-TRULY_NOOP_TRUNCATION targets we
12428 might be able to turn a truncate into a subreg using this information.
12429 Return -1 if traversing *P is complete or 0 otherwise. */
12430
12431 static int
12432 record_truncated_value (rtx *p, void *data ATTRIBUTE_UNUSED)
12433 {
12434 rtx x = *p;
12435 enum machine_mode truncated_mode;
12436 reg_stat_type *rsp;
12437
12438 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12439 {
12440 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12441 truncated_mode = GET_MODE (x);
12442
12443 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12444 return -1;
12445
12446 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
12447 GET_MODE_BITSIZE (original_mode)))
12448 return -1;
12449
12450 x = SUBREG_REG (x);
12451 }
12452 /* ??? For hard-regs we now record everything. We might be able to
12453 optimize this using last_set_mode. */
12454 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12455 truncated_mode = GET_MODE (x);
12456 else
12457 return 0;
12458
12459 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
12460 if (rsp->truncated_to_mode == 0
12461 || rsp->truncation_label < label_tick_ebb_start
12462 || (GET_MODE_SIZE (truncated_mode)
12463 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12464 {
12465 rsp->truncated_to_mode = truncated_mode;
12466 rsp->truncation_label = label_tick;
12467 }
12468
12469 return -1;
12470 }
12471
12472 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12473 the modes they are used in. This can help truning TRUNCATEs into
12474 SUBREGs. */
12475
12476 static void
12477 record_truncated_values (rtx *x, void *data ATTRIBUTE_UNUSED)
12478 {
12479 for_each_rtx (x, record_truncated_value, NULL);
12480 }
12481
12482 /* Scan X for promoted SUBREGs. For each one found,
12483 note what it implies to the registers used in it. */
12484
12485 static void
12486 check_promoted_subreg (rtx insn, rtx x)
12487 {
12488 if (GET_CODE (x) == SUBREG
12489 && SUBREG_PROMOTED_VAR_P (x)
12490 && REG_P (SUBREG_REG (x)))
12491 record_promoted_value (insn, x);
12492 else
12493 {
12494 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12495 int i, j;
12496
12497 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12498 switch (format[i])
12499 {
12500 case 'e':
12501 check_promoted_subreg (insn, XEXP (x, i));
12502 break;
12503 case 'V':
12504 case 'E':
12505 if (XVEC (x, i) != 0)
12506 for (j = 0; j < XVECLEN (x, i); j++)
12507 check_promoted_subreg (insn, XVECEXP (x, i, j));
12508 break;
12509 }
12510 }
12511 }
12512 \f
12513 /* Verify that all the registers and memory references mentioned in *LOC are
12514 still valid. *LOC was part of a value set in INSN when label_tick was
12515 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12516 the invalid references with (clobber (const_int 0)) and return 1. This
12517 replacement is useful because we often can get useful information about
12518 the form of a value (e.g., if it was produced by a shift that always
12519 produces -1 or 0) even though we don't know exactly what registers it
12520 was produced from. */
12521
12522 static int
12523 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
12524 {
12525 rtx x = *loc;
12526 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12527 int len = GET_RTX_LENGTH (GET_CODE (x));
12528 int i, j;
12529
12530 if (REG_P (x))
12531 {
12532 unsigned int regno = REGNO (x);
12533 unsigned int endregno = END_REGNO (x);
12534 unsigned int j;
12535
12536 for (j = regno; j < endregno; j++)
12537 {
12538 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, j);
12539 if (rsp->last_set_invalid
12540 /* If this is a pseudo-register that was only set once and not
12541 live at the beginning of the function, it is always valid. */
12542 || (! (regno >= FIRST_PSEUDO_REGISTER
12543 && REG_N_SETS (regno) == 1
12544 && (!REGNO_REG_SET_P
12545 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno)))
12546 && rsp->last_set_label > tick))
12547 {
12548 if (replace)
12549 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12550 return replace;
12551 }
12552 }
12553
12554 return 1;
12555 }
12556 /* If this is a memory reference, make sure that there were no stores after
12557 it that might have clobbered the value. We don't have alias info, so we
12558 assume any store invalidates it. Moreover, we only have local UIDs, so
12559 we also assume that there were stores in the intervening basic blocks. */
12560 else if (MEM_P (x) && !MEM_READONLY_P (x)
12561 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12562 {
12563 if (replace)
12564 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12565 return replace;
12566 }
12567
12568 for (i = 0; i < len; i++)
12569 {
12570 if (fmt[i] == 'e')
12571 {
12572 /* Check for identical subexpressions. If x contains
12573 identical subexpression we only have to traverse one of
12574 them. */
12575 if (i == 1 && ARITHMETIC_P (x))
12576 {
12577 /* Note that at this point x0 has already been checked
12578 and found valid. */
12579 rtx x0 = XEXP (x, 0);
12580 rtx x1 = XEXP (x, 1);
12581
12582 /* If x0 and x1 are identical then x is also valid. */
12583 if (x0 == x1)
12584 return 1;
12585
12586 /* If x1 is identical to a subexpression of x0 then
12587 while checking x0, x1 has already been checked. Thus
12588 it is valid and so as x. */
12589 if (ARITHMETIC_P (x0)
12590 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12591 return 1;
12592
12593 /* If x0 is identical to a subexpression of x1 then x is
12594 valid iff the rest of x1 is valid. */
12595 if (ARITHMETIC_P (x1)
12596 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12597 return
12598 get_last_value_validate (&XEXP (x1,
12599 x0 == XEXP (x1, 0) ? 1 : 0),
12600 insn, tick, replace);
12601 }
12602
12603 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12604 replace) == 0)
12605 return 0;
12606 }
12607 else if (fmt[i] == 'E')
12608 for (j = 0; j < XVECLEN (x, i); j++)
12609 if (get_last_value_validate (&XVECEXP (x, i, j),
12610 insn, tick, replace) == 0)
12611 return 0;
12612 }
12613
12614 /* If we haven't found a reason for it to be invalid, it is valid. */
12615 return 1;
12616 }
12617
12618 /* Get the last value assigned to X, if known. Some registers
12619 in the value may be replaced with (clobber (const_int 0)) if their value
12620 is known longer known reliably. */
12621
12622 static rtx
12623 get_last_value (const_rtx x)
12624 {
12625 unsigned int regno;
12626 rtx value;
12627 reg_stat_type *rsp;
12628
12629 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12630 then convert it to the desired mode. If this is a paradoxical SUBREG,
12631 we cannot predict what values the "extra" bits might have. */
12632 if (GET_CODE (x) == SUBREG
12633 && subreg_lowpart_p (x)
12634 && (GET_MODE_SIZE (GET_MODE (x))
12635 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
12636 && (value = get_last_value (SUBREG_REG (x))) != 0)
12637 return gen_lowpart (GET_MODE (x), value);
12638
12639 if (!REG_P (x))
12640 return 0;
12641
12642 regno = REGNO (x);
12643 rsp = VEC_index (reg_stat_type, reg_stat, regno);
12644 value = rsp->last_set_value;
12645
12646 /* If we don't have a value, or if it isn't for this basic block and
12647 it's either a hard register, set more than once, or it's a live
12648 at the beginning of the function, return 0.
12649
12650 Because if it's not live at the beginning of the function then the reg
12651 is always set before being used (is never used without being set).
12652 And, if it's set only once, and it's always set before use, then all
12653 uses must have the same last value, even if it's not from this basic
12654 block. */
12655
12656 if (value == 0
12657 || (rsp->last_set_label < label_tick_ebb_start
12658 && (regno < FIRST_PSEUDO_REGISTER
12659 || REG_N_SETS (regno) != 1
12660 || REGNO_REG_SET_P
12661 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
12662 return 0;
12663
12664 /* If the value was set in a later insn than the ones we are processing,
12665 we can't use it even if the register was only set once. */
12666 if (rsp->last_set_label == label_tick
12667 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12668 return 0;
12669
12670 /* If the value has all its registers valid, return it. */
12671 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12672 return value;
12673
12674 /* Otherwise, make a copy and replace any invalid register with
12675 (clobber (const_int 0)). If that fails for some reason, return 0. */
12676
12677 value = copy_rtx (value);
12678 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12679 return value;
12680
12681 return 0;
12682 }
12683 \f
12684 /* Return nonzero if expression X refers to a REG or to memory
12685 that is set in an instruction more recent than FROM_LUID. */
12686
12687 static int
12688 use_crosses_set_p (const_rtx x, int from_luid)
12689 {
12690 const char *fmt;
12691 int i;
12692 enum rtx_code code = GET_CODE (x);
12693
12694 if (code == REG)
12695 {
12696 unsigned int regno = REGNO (x);
12697 unsigned endreg = END_REGNO (x);
12698
12699 #ifdef PUSH_ROUNDING
12700 /* Don't allow uses of the stack pointer to be moved,
12701 because we don't know whether the move crosses a push insn. */
12702 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12703 return 1;
12704 #endif
12705 for (; regno < endreg; regno++)
12706 {
12707 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
12708 if (rsp->last_set
12709 && rsp->last_set_label == label_tick
12710 && DF_INSN_LUID (rsp->last_set) > from_luid)
12711 return 1;
12712 }
12713 return 0;
12714 }
12715
12716 if (code == MEM && mem_last_set > from_luid)
12717 return 1;
12718
12719 fmt = GET_RTX_FORMAT (code);
12720
12721 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12722 {
12723 if (fmt[i] == 'E')
12724 {
12725 int j;
12726 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12727 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12728 return 1;
12729 }
12730 else if (fmt[i] == 'e'
12731 && use_crosses_set_p (XEXP (x, i), from_luid))
12732 return 1;
12733 }
12734 return 0;
12735 }
12736 \f
12737 /* Define three variables used for communication between the following
12738 routines. */
12739
12740 static unsigned int reg_dead_regno, reg_dead_endregno;
12741 static int reg_dead_flag;
12742
12743 /* Function called via note_stores from reg_dead_at_p.
12744
12745 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12746 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12747
12748 static void
12749 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12750 {
12751 unsigned int regno, endregno;
12752
12753 if (!REG_P (dest))
12754 return;
12755
12756 regno = REGNO (dest);
12757 endregno = END_REGNO (dest);
12758 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12759 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12760 }
12761
12762 /* Return nonzero if REG is known to be dead at INSN.
12763
12764 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
12765 referencing REG, it is dead. If we hit a SET referencing REG, it is
12766 live. Otherwise, see if it is live or dead at the start of the basic
12767 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
12768 must be assumed to be always live. */
12769
12770 static int
12771 reg_dead_at_p (rtx reg, rtx insn)
12772 {
12773 basic_block block;
12774 unsigned int i;
12775
12776 /* Set variables for reg_dead_at_p_1. */
12777 reg_dead_regno = REGNO (reg);
12778 reg_dead_endregno = END_REGNO (reg);
12779
12780 reg_dead_flag = 0;
12781
12782 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
12783 we allow the machine description to decide whether use-and-clobber
12784 patterns are OK. */
12785 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12786 {
12787 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12788 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12789 return 0;
12790 }
12791
12792 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12793 beginning of basic block. */
12794 block = BLOCK_FOR_INSN (insn);
12795 for (;;)
12796 {
12797 if (INSN_P (insn))
12798 {
12799 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12800 if (reg_dead_flag)
12801 return reg_dead_flag == 1 ? 1 : 0;
12802
12803 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12804 return 1;
12805 }
12806
12807 if (insn == BB_HEAD (block))
12808 break;
12809
12810 insn = PREV_INSN (insn);
12811 }
12812
12813 /* Look at live-in sets for the basic block that we were in. */
12814 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12815 if (REGNO_REG_SET_P (df_get_live_in (block), i))
12816 return 0;
12817
12818 return 1;
12819 }
12820 \f
12821 /* Note hard registers in X that are used. */
12822
12823 static void
12824 mark_used_regs_combine (rtx x)
12825 {
12826 RTX_CODE code = GET_CODE (x);
12827 unsigned int regno;
12828 int i;
12829
12830 switch (code)
12831 {
12832 case LABEL_REF:
12833 case SYMBOL_REF:
12834 case CONST_INT:
12835 case CONST:
12836 case CONST_DOUBLE:
12837 case CONST_VECTOR:
12838 case PC:
12839 case ADDR_VEC:
12840 case ADDR_DIFF_VEC:
12841 case ASM_INPUT:
12842 #ifdef HAVE_cc0
12843 /* CC0 must die in the insn after it is set, so we don't need to take
12844 special note of it here. */
12845 case CC0:
12846 #endif
12847 return;
12848
12849 case CLOBBER:
12850 /* If we are clobbering a MEM, mark any hard registers inside the
12851 address as used. */
12852 if (MEM_P (XEXP (x, 0)))
12853 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12854 return;
12855
12856 case REG:
12857 regno = REGNO (x);
12858 /* A hard reg in a wide mode may really be multiple registers.
12859 If so, mark all of them just like the first. */
12860 if (regno < FIRST_PSEUDO_REGISTER)
12861 {
12862 /* None of this applies to the stack, frame or arg pointers. */
12863 if (regno == STACK_POINTER_REGNUM
12864 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
12865 || regno == HARD_FRAME_POINTER_REGNUM
12866 #endif
12867 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12868 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12869 #endif
12870 || regno == FRAME_POINTER_REGNUM)
12871 return;
12872
12873 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12874 }
12875 return;
12876
12877 case SET:
12878 {
12879 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12880 the address. */
12881 rtx testreg = SET_DEST (x);
12882
12883 while (GET_CODE (testreg) == SUBREG
12884 || GET_CODE (testreg) == ZERO_EXTRACT
12885 || GET_CODE (testreg) == STRICT_LOW_PART)
12886 testreg = XEXP (testreg, 0);
12887
12888 if (MEM_P (testreg))
12889 mark_used_regs_combine (XEXP (testreg, 0));
12890
12891 mark_used_regs_combine (SET_SRC (x));
12892 }
12893 return;
12894
12895 default:
12896 break;
12897 }
12898
12899 /* Recursively scan the operands of this expression. */
12900
12901 {
12902 const char *fmt = GET_RTX_FORMAT (code);
12903
12904 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12905 {
12906 if (fmt[i] == 'e')
12907 mark_used_regs_combine (XEXP (x, i));
12908 else if (fmt[i] == 'E')
12909 {
12910 int j;
12911
12912 for (j = 0; j < XVECLEN (x, i); j++)
12913 mark_used_regs_combine (XVECEXP (x, i, j));
12914 }
12915 }
12916 }
12917 }
12918 \f
12919 /* Remove register number REGNO from the dead registers list of INSN.
12920
12921 Return the note used to record the death, if there was one. */
12922
12923 rtx
12924 remove_death (unsigned int regno, rtx insn)
12925 {
12926 rtx note = find_regno_note (insn, REG_DEAD, regno);
12927
12928 if (note)
12929 remove_note (insn, note);
12930
12931 return note;
12932 }
12933
12934 /* For each register (hardware or pseudo) used within expression X, if its
12935 death is in an instruction with luid between FROM_LUID (inclusive) and
12936 TO_INSN (exclusive), put a REG_DEAD note for that register in the
12937 list headed by PNOTES.
12938
12939 That said, don't move registers killed by maybe_kill_insn.
12940
12941 This is done when X is being merged by combination into TO_INSN. These
12942 notes will then be distributed as needed. */
12943
12944 static void
12945 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
12946 rtx *pnotes)
12947 {
12948 const char *fmt;
12949 int len, i;
12950 enum rtx_code code = GET_CODE (x);
12951
12952 if (code == REG)
12953 {
12954 unsigned int regno = REGNO (x);
12955 rtx where_dead = VEC_index (reg_stat_type, reg_stat, regno)->last_death;
12956
12957 /* Don't move the register if it gets killed in between from and to. */
12958 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12959 && ! reg_referenced_p (x, maybe_kill_insn))
12960 return;
12961
12962 if (where_dead
12963 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
12964 && DF_INSN_LUID (where_dead) >= from_luid
12965 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
12966 {
12967 rtx note = remove_death (regno, where_dead);
12968
12969 /* It is possible for the call above to return 0. This can occur
12970 when last_death points to I2 or I1 that we combined with.
12971 In that case make a new note.
12972
12973 We must also check for the case where X is a hard register
12974 and NOTE is a death note for a range of hard registers
12975 including X. In that case, we must put REG_DEAD notes for
12976 the remaining registers in place of NOTE. */
12977
12978 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12979 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12980 > GET_MODE_SIZE (GET_MODE (x))))
12981 {
12982 unsigned int deadregno = REGNO (XEXP (note, 0));
12983 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
12984 unsigned int ourend = END_HARD_REGNO (x);
12985 unsigned int i;
12986
12987 for (i = deadregno; i < deadend; i++)
12988 if (i < regno || i >= ourend)
12989 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
12990 }
12991
12992 /* If we didn't find any note, or if we found a REG_DEAD note that
12993 covers only part of the given reg, and we have a multi-reg hard
12994 register, then to be safe we must check for REG_DEAD notes
12995 for each register other than the first. They could have
12996 their own REG_DEAD notes lying around. */
12997 else if ((note == 0
12998 || (note != 0
12999 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13000 < GET_MODE_SIZE (GET_MODE (x)))))
13001 && regno < FIRST_PSEUDO_REGISTER
13002 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13003 {
13004 unsigned int ourend = END_HARD_REGNO (x);
13005 unsigned int i, offset;
13006 rtx oldnotes = 0;
13007
13008 if (note)
13009 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13010 else
13011 offset = 1;
13012
13013 for (i = regno + offset; i < ourend; i++)
13014 move_deaths (regno_reg_rtx[i],
13015 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13016 }
13017
13018 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13019 {
13020 XEXP (note, 1) = *pnotes;
13021 *pnotes = note;
13022 }
13023 else
13024 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13025 }
13026
13027 return;
13028 }
13029
13030 else if (GET_CODE (x) == SET)
13031 {
13032 rtx dest = SET_DEST (x);
13033
13034 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13035
13036 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13037 that accesses one word of a multi-word item, some
13038 piece of everything register in the expression is used by
13039 this insn, so remove any old death. */
13040 /* ??? So why do we test for equality of the sizes? */
13041
13042 if (GET_CODE (dest) == ZERO_EXTRACT
13043 || GET_CODE (dest) == STRICT_LOW_PART
13044 || (GET_CODE (dest) == SUBREG
13045 && (((GET_MODE_SIZE (GET_MODE (dest))
13046 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13047 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13048 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13049 {
13050 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13051 return;
13052 }
13053
13054 /* If this is some other SUBREG, we know it replaces the entire
13055 value, so use that as the destination. */
13056 if (GET_CODE (dest) == SUBREG)
13057 dest = SUBREG_REG (dest);
13058
13059 /* If this is a MEM, adjust deaths of anything used in the address.
13060 For a REG (the only other possibility), the entire value is
13061 being replaced so the old value is not used in this insn. */
13062
13063 if (MEM_P (dest))
13064 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13065 to_insn, pnotes);
13066 return;
13067 }
13068
13069 else if (GET_CODE (x) == CLOBBER)
13070 return;
13071
13072 len = GET_RTX_LENGTH (code);
13073 fmt = GET_RTX_FORMAT (code);
13074
13075 for (i = 0; i < len; i++)
13076 {
13077 if (fmt[i] == 'E')
13078 {
13079 int j;
13080 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13081 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13082 to_insn, pnotes);
13083 }
13084 else if (fmt[i] == 'e')
13085 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13086 }
13087 }
13088 \f
13089 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13090 pattern of an insn. X must be a REG. */
13091
13092 static int
13093 reg_bitfield_target_p (rtx x, rtx body)
13094 {
13095 int i;
13096
13097 if (GET_CODE (body) == SET)
13098 {
13099 rtx dest = SET_DEST (body);
13100 rtx target;
13101 unsigned int regno, tregno, endregno, endtregno;
13102
13103 if (GET_CODE (dest) == ZERO_EXTRACT)
13104 target = XEXP (dest, 0);
13105 else if (GET_CODE (dest) == STRICT_LOW_PART)
13106 target = SUBREG_REG (XEXP (dest, 0));
13107 else
13108 return 0;
13109
13110 if (GET_CODE (target) == SUBREG)
13111 target = SUBREG_REG (target);
13112
13113 if (!REG_P (target))
13114 return 0;
13115
13116 tregno = REGNO (target), regno = REGNO (x);
13117 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13118 return target == x;
13119
13120 endtregno = end_hard_regno (GET_MODE (target), tregno);
13121 endregno = end_hard_regno (GET_MODE (x), regno);
13122
13123 return endregno > tregno && regno < endtregno;
13124 }
13125
13126 else if (GET_CODE (body) == PARALLEL)
13127 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13128 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13129 return 1;
13130
13131 return 0;
13132 }
13133 \f
13134 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13135 as appropriate. I3 and I2 are the insns resulting from the combination
13136 insns including FROM (I2 may be zero).
13137
13138 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13139 not need REG_DEAD notes because they are being substituted for. This
13140 saves searching in the most common cases.
13141
13142 Each note in the list is either ignored or placed on some insns, depending
13143 on the type of note. */
13144
13145 static void
13146 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
13147 rtx elim_i1, rtx elim_i0)
13148 {
13149 rtx note, next_note;
13150 rtx tem;
13151
13152 for (note = notes; note; note = next_note)
13153 {
13154 rtx place = 0, place2 = 0;
13155
13156 next_note = XEXP (note, 1);
13157 switch (REG_NOTE_KIND (note))
13158 {
13159 case REG_BR_PROB:
13160 case REG_BR_PRED:
13161 /* Doesn't matter much where we put this, as long as it's somewhere.
13162 It is preferable to keep these notes on branches, which is most
13163 likely to be i3. */
13164 place = i3;
13165 break;
13166
13167 case REG_NON_LOCAL_GOTO:
13168 if (JUMP_P (i3))
13169 place = i3;
13170 else
13171 {
13172 gcc_assert (i2 && JUMP_P (i2));
13173 place = i2;
13174 }
13175 break;
13176
13177 case REG_EH_REGION:
13178 /* These notes must remain with the call or trapping instruction. */
13179 if (CALL_P (i3))
13180 place = i3;
13181 else if (i2 && CALL_P (i2))
13182 place = i2;
13183 else
13184 {
13185 gcc_assert (cfun->can_throw_non_call_exceptions);
13186 if (may_trap_p (i3))
13187 place = i3;
13188 else if (i2 && may_trap_p (i2))
13189 place = i2;
13190 /* ??? Otherwise assume we've combined things such that we
13191 can now prove that the instructions can't trap. Drop the
13192 note in this case. */
13193 }
13194 break;
13195
13196 case REG_NORETURN:
13197 case REG_SETJMP:
13198 /* These notes must remain with the call. It should not be
13199 possible for both I2 and I3 to be a call. */
13200 if (CALL_P (i3))
13201 place = i3;
13202 else
13203 {
13204 gcc_assert (i2 && CALL_P (i2));
13205 place = i2;
13206 }
13207 break;
13208
13209 case REG_UNUSED:
13210 /* Any clobbers for i3 may still exist, and so we must process
13211 REG_UNUSED notes from that insn.
13212
13213 Any clobbers from i2 or i1 can only exist if they were added by
13214 recog_for_combine. In that case, recog_for_combine created the
13215 necessary REG_UNUSED notes. Trying to keep any original
13216 REG_UNUSED notes from these insns can cause incorrect output
13217 if it is for the same register as the original i3 dest.
13218 In that case, we will notice that the register is set in i3,
13219 and then add a REG_UNUSED note for the destination of i3, which
13220 is wrong. However, it is possible to have REG_UNUSED notes from
13221 i2 or i1 for register which were both used and clobbered, so
13222 we keep notes from i2 or i1 if they will turn into REG_DEAD
13223 notes. */
13224
13225 /* If this register is set or clobbered in I3, put the note there
13226 unless there is one already. */
13227 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13228 {
13229 if (from_insn != i3)
13230 break;
13231
13232 if (! (REG_P (XEXP (note, 0))
13233 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13234 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13235 place = i3;
13236 }
13237 /* Otherwise, if this register is used by I3, then this register
13238 now dies here, so we must put a REG_DEAD note here unless there
13239 is one already. */
13240 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13241 && ! (REG_P (XEXP (note, 0))
13242 ? find_regno_note (i3, REG_DEAD,
13243 REGNO (XEXP (note, 0)))
13244 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13245 {
13246 PUT_REG_NOTE_KIND (note, REG_DEAD);
13247 place = i3;
13248 }
13249 break;
13250
13251 case REG_EQUAL:
13252 case REG_EQUIV:
13253 case REG_NOALIAS:
13254 /* These notes say something about results of an insn. We can
13255 only support them if they used to be on I3 in which case they
13256 remain on I3. Otherwise they are ignored.
13257
13258 If the note refers to an expression that is not a constant, we
13259 must also ignore the note since we cannot tell whether the
13260 equivalence is still true. It might be possible to do
13261 slightly better than this (we only have a problem if I2DEST
13262 or I1DEST is present in the expression), but it doesn't
13263 seem worth the trouble. */
13264
13265 if (from_insn == i3
13266 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13267 place = i3;
13268 break;
13269
13270 case REG_INC:
13271 /* These notes say something about how a register is used. They must
13272 be present on any use of the register in I2 or I3. */
13273 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13274 place = i3;
13275
13276 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13277 {
13278 if (place)
13279 place2 = i2;
13280 else
13281 place = i2;
13282 }
13283 break;
13284
13285 case REG_LABEL_TARGET:
13286 case REG_LABEL_OPERAND:
13287 /* This can show up in several ways -- either directly in the
13288 pattern, or hidden off in the constant pool with (or without?)
13289 a REG_EQUAL note. */
13290 /* ??? Ignore the without-reg_equal-note problem for now. */
13291 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13292 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13293 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13294 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
13295 place = i3;
13296
13297 if (i2
13298 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13299 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13300 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13301 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
13302 {
13303 if (place)
13304 place2 = i2;
13305 else
13306 place = i2;
13307 }
13308
13309 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13310 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13311 there. */
13312 if (place && JUMP_P (place)
13313 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13314 && (JUMP_LABEL (place) == NULL
13315 || JUMP_LABEL (place) == XEXP (note, 0)))
13316 {
13317 rtx label = JUMP_LABEL (place);
13318
13319 if (!label)
13320 JUMP_LABEL (place) = XEXP (note, 0);
13321 else if (LABEL_P (label))
13322 LABEL_NUSES (label)--;
13323 }
13324
13325 if (place2 && JUMP_P (place2)
13326 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13327 && (JUMP_LABEL (place2) == NULL
13328 || JUMP_LABEL (place2) == XEXP (note, 0)))
13329 {
13330 rtx label = JUMP_LABEL (place2);
13331
13332 if (!label)
13333 JUMP_LABEL (place2) = XEXP (note, 0);
13334 else if (LABEL_P (label))
13335 LABEL_NUSES (label)--;
13336 place2 = 0;
13337 }
13338 break;
13339
13340 case REG_NONNEG:
13341 /* This note says something about the value of a register prior
13342 to the execution of an insn. It is too much trouble to see
13343 if the note is still correct in all situations. It is better
13344 to simply delete it. */
13345 break;
13346
13347 case REG_DEAD:
13348 /* If we replaced the right hand side of FROM_INSN with a
13349 REG_EQUAL note, the original use of the dying register
13350 will not have been combined into I3 and I2. In such cases,
13351 FROM_INSN is guaranteed to be the first of the combined
13352 instructions, so we simply need to search back before
13353 FROM_INSN for the previous use or set of this register,
13354 then alter the notes there appropriately.
13355
13356 If the register is used as an input in I3, it dies there.
13357 Similarly for I2, if it is nonzero and adjacent to I3.
13358
13359 If the register is not used as an input in either I3 or I2
13360 and it is not one of the registers we were supposed to eliminate,
13361 there are two possibilities. We might have a non-adjacent I2
13362 or we might have somehow eliminated an additional register
13363 from a computation. For example, we might have had A & B where
13364 we discover that B will always be zero. In this case we will
13365 eliminate the reference to A.
13366
13367 In both cases, we must search to see if we can find a previous
13368 use of A and put the death note there. */
13369
13370 if (from_insn
13371 && from_insn == i2mod
13372 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13373 tem = from_insn;
13374 else
13375 {
13376 if (from_insn
13377 && CALL_P (from_insn)
13378 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13379 place = from_insn;
13380 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13381 place = i3;
13382 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13383 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13384 place = i2;
13385 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13386 && !(i2mod
13387 && reg_overlap_mentioned_p (XEXP (note, 0),
13388 i2mod_old_rhs)))
13389 || rtx_equal_p (XEXP (note, 0), elim_i1)
13390 || rtx_equal_p (XEXP (note, 0), elim_i0))
13391 break;
13392 tem = i3;
13393 }
13394
13395 if (place == 0)
13396 {
13397 basic_block bb = this_basic_block;
13398
13399 for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
13400 {
13401 if (!NONDEBUG_INSN_P (tem))
13402 {
13403 if (tem == BB_HEAD (bb))
13404 break;
13405 continue;
13406 }
13407
13408 /* If the register is being set at TEM, see if that is all
13409 TEM is doing. If so, delete TEM. Otherwise, make this
13410 into a REG_UNUSED note instead. Don't delete sets to
13411 global register vars. */
13412 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13413 || !global_regs[REGNO (XEXP (note, 0))])
13414 && reg_set_p (XEXP (note, 0), PATTERN (tem)))
13415 {
13416 rtx set = single_set (tem);
13417 rtx inner_dest = 0;
13418 #ifdef HAVE_cc0
13419 rtx cc0_setter = NULL_RTX;
13420 #endif
13421
13422 if (set != 0)
13423 for (inner_dest = SET_DEST (set);
13424 (GET_CODE (inner_dest) == STRICT_LOW_PART
13425 || GET_CODE (inner_dest) == SUBREG
13426 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13427 inner_dest = XEXP (inner_dest, 0))
13428 ;
13429
13430 /* Verify that it was the set, and not a clobber that
13431 modified the register.
13432
13433 CC0 targets must be careful to maintain setter/user
13434 pairs. If we cannot delete the setter due to side
13435 effects, mark the user with an UNUSED note instead
13436 of deleting it. */
13437
13438 if (set != 0 && ! side_effects_p (SET_SRC (set))
13439 && rtx_equal_p (XEXP (note, 0), inner_dest)
13440 #ifdef HAVE_cc0
13441 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13442 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
13443 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13444 #endif
13445 )
13446 {
13447 /* Move the notes and links of TEM elsewhere.
13448 This might delete other dead insns recursively.
13449 First set the pattern to something that won't use
13450 any register. */
13451 rtx old_notes = REG_NOTES (tem);
13452
13453 PATTERN (tem) = pc_rtx;
13454 REG_NOTES (tem) = NULL;
13455
13456 distribute_notes (old_notes, tem, tem, NULL_RTX,
13457 NULL_RTX, NULL_RTX, NULL_RTX);
13458 distribute_links (LOG_LINKS (tem));
13459
13460 SET_INSN_DELETED (tem);
13461 if (tem == i2)
13462 i2 = NULL_RTX;
13463
13464 #ifdef HAVE_cc0
13465 /* Delete the setter too. */
13466 if (cc0_setter)
13467 {
13468 PATTERN (cc0_setter) = pc_rtx;
13469 old_notes = REG_NOTES (cc0_setter);
13470 REG_NOTES (cc0_setter) = NULL;
13471
13472 distribute_notes (old_notes, cc0_setter,
13473 cc0_setter, NULL_RTX,
13474 NULL_RTX, NULL_RTX, NULL_RTX);
13475 distribute_links (LOG_LINKS (cc0_setter));
13476
13477 SET_INSN_DELETED (cc0_setter);
13478 if (cc0_setter == i2)
13479 i2 = NULL_RTX;
13480 }
13481 #endif
13482 }
13483 else
13484 {
13485 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13486
13487 /* If there isn't already a REG_UNUSED note, put one
13488 here. Do not place a REG_DEAD note, even if
13489 the register is also used here; that would not
13490 match the algorithm used in lifetime analysis
13491 and can cause the consistency check in the
13492 scheduler to fail. */
13493 if (! find_regno_note (tem, REG_UNUSED,
13494 REGNO (XEXP (note, 0))))
13495 place = tem;
13496 break;
13497 }
13498 }
13499 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
13500 || (CALL_P (tem)
13501 && find_reg_fusage (tem, USE, XEXP (note, 0))))
13502 {
13503 place = tem;
13504
13505 /* If we are doing a 3->2 combination, and we have a
13506 register which formerly died in i3 and was not used
13507 by i2, which now no longer dies in i3 and is used in
13508 i2 but does not die in i2, and place is between i2
13509 and i3, then we may need to move a link from place to
13510 i2. */
13511 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13512 && from_insn
13513 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13514 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13515 {
13516 struct insn_link *links = LOG_LINKS (place);
13517 LOG_LINKS (place) = NULL;
13518 distribute_links (links);
13519 }
13520 break;
13521 }
13522
13523 if (tem == BB_HEAD (bb))
13524 break;
13525 }
13526
13527 }
13528
13529 /* If the register is set or already dead at PLACE, we needn't do
13530 anything with this note if it is still a REG_DEAD note.
13531 We check here if it is set at all, not if is it totally replaced,
13532 which is what `dead_or_set_p' checks, so also check for it being
13533 set partially. */
13534
13535 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13536 {
13537 unsigned int regno = REGNO (XEXP (note, 0));
13538 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
13539
13540 if (dead_or_set_p (place, XEXP (note, 0))
13541 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13542 {
13543 /* Unless the register previously died in PLACE, clear
13544 last_death. [I no longer understand why this is
13545 being done.] */
13546 if (rsp->last_death != place)
13547 rsp->last_death = 0;
13548 place = 0;
13549 }
13550 else
13551 rsp->last_death = place;
13552
13553 /* If this is a death note for a hard reg that is occupying
13554 multiple registers, ensure that we are still using all
13555 parts of the object. If we find a piece of the object
13556 that is unused, we must arrange for an appropriate REG_DEAD
13557 note to be added for it. However, we can't just emit a USE
13558 and tag the note to it, since the register might actually
13559 be dead; so we recourse, and the recursive call then finds
13560 the previous insn that used this register. */
13561
13562 if (place && regno < FIRST_PSEUDO_REGISTER
13563 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13564 {
13565 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13566 int all_used = 1;
13567 unsigned int i;
13568
13569 for (i = regno; i < endregno; i++)
13570 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13571 && ! find_regno_fusage (place, USE, i))
13572 || dead_or_set_regno_p (place, i))
13573 all_used = 0;
13574
13575 if (! all_used)
13576 {
13577 /* Put only REG_DEAD notes for pieces that are
13578 not already dead or set. */
13579
13580 for (i = regno; i < endregno;
13581 i += hard_regno_nregs[i][reg_raw_mode[i]])
13582 {
13583 rtx piece = regno_reg_rtx[i];
13584 basic_block bb = this_basic_block;
13585
13586 if (! dead_or_set_p (place, piece)
13587 && ! reg_bitfield_target_p (piece,
13588 PATTERN (place)))
13589 {
13590 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13591 NULL_RTX);
13592
13593 distribute_notes (new_note, place, place,
13594 NULL_RTX, NULL_RTX, NULL_RTX,
13595 NULL_RTX);
13596 }
13597 else if (! refers_to_regno_p (i, i + 1,
13598 PATTERN (place), 0)
13599 && ! find_regno_fusage (place, USE, i))
13600 for (tem = PREV_INSN (place); ;
13601 tem = PREV_INSN (tem))
13602 {
13603 if (!NONDEBUG_INSN_P (tem))
13604 {
13605 if (tem == BB_HEAD (bb))
13606 break;
13607 continue;
13608 }
13609 if (dead_or_set_p (tem, piece)
13610 || reg_bitfield_target_p (piece,
13611 PATTERN (tem)))
13612 {
13613 add_reg_note (tem, REG_UNUSED, piece);
13614 break;
13615 }
13616 }
13617
13618 }
13619
13620 place = 0;
13621 }
13622 }
13623 }
13624 break;
13625
13626 default:
13627 /* Any other notes should not be present at this point in the
13628 compilation. */
13629 gcc_unreachable ();
13630 }
13631
13632 if (place)
13633 {
13634 XEXP (note, 1) = REG_NOTES (place);
13635 REG_NOTES (place) = note;
13636 }
13637
13638 if (place2)
13639 add_reg_note (place2, REG_NOTE_KIND (note), XEXP (note, 0));
13640 }
13641 }
13642 \f
13643 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13644 I3, I2, and I1 to new locations. This is also called to add a link
13645 pointing at I3 when I3's destination is changed. */
13646
13647 static void
13648 distribute_links (struct insn_link *links)
13649 {
13650 struct insn_link *link, *next_link;
13651
13652 for (link = links; link; link = next_link)
13653 {
13654 rtx place = 0;
13655 rtx insn;
13656 rtx set, reg;
13657
13658 next_link = link->next;
13659
13660 /* If the insn that this link points to is a NOTE or isn't a single
13661 set, ignore it. In the latter case, it isn't clear what we
13662 can do other than ignore the link, since we can't tell which
13663 register it was for. Such links wouldn't be used by combine
13664 anyway.
13665
13666 It is not possible for the destination of the target of the link to
13667 have been changed by combine. The only potential of this is if we
13668 replace I3, I2, and I1 by I3 and I2. But in that case the
13669 destination of I2 also remains unchanged. */
13670
13671 if (NOTE_P (link->insn)
13672 || (set = single_set (link->insn)) == 0)
13673 continue;
13674
13675 reg = SET_DEST (set);
13676 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13677 || GET_CODE (reg) == STRICT_LOW_PART)
13678 reg = XEXP (reg, 0);
13679
13680 /* A LOG_LINK is defined as being placed on the first insn that uses
13681 a register and points to the insn that sets the register. Start
13682 searching at the next insn after the target of the link and stop
13683 when we reach a set of the register or the end of the basic block.
13684
13685 Note that this correctly handles the link that used to point from
13686 I3 to I2. Also note that not much searching is typically done here
13687 since most links don't point very far away. */
13688
13689 for (insn = NEXT_INSN (link->insn);
13690 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
13691 || BB_HEAD (this_basic_block->next_bb) != insn));
13692 insn = NEXT_INSN (insn))
13693 if (DEBUG_INSN_P (insn))
13694 continue;
13695 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13696 {
13697 if (reg_referenced_p (reg, PATTERN (insn)))
13698 place = insn;
13699 break;
13700 }
13701 else if (CALL_P (insn)
13702 && find_reg_fusage (insn, USE, reg))
13703 {
13704 place = insn;
13705 break;
13706 }
13707 else if (INSN_P (insn) && reg_set_p (reg, insn))
13708 break;
13709
13710 /* If we found a place to put the link, place it there unless there
13711 is already a link to the same insn as LINK at that point. */
13712
13713 if (place)
13714 {
13715 struct insn_link *link2;
13716
13717 FOR_EACH_LOG_LINK (link2, place)
13718 if (link2->insn == link->insn)
13719 break;
13720
13721 if (link2 == NULL)
13722 {
13723 link->next = LOG_LINKS (place);
13724 LOG_LINKS (place) = link;
13725
13726 /* Set added_links_insn to the earliest insn we added a
13727 link to. */
13728 if (added_links_insn == 0
13729 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13730 added_links_insn = place;
13731 }
13732 }
13733 }
13734 }
13735 \f
13736 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
13737 Check whether the expression pointer to by LOC is a register or
13738 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
13739 Otherwise return zero. */
13740
13741 static int
13742 unmentioned_reg_p_1 (rtx *loc, void *expr)
13743 {
13744 rtx x = *loc;
13745
13746 if (x != NULL_RTX
13747 && (REG_P (x) || MEM_P (x))
13748 && ! reg_mentioned_p (x, (rtx) expr))
13749 return 1;
13750 return 0;
13751 }
13752
13753 /* Check for any register or memory mentioned in EQUIV that is not
13754 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
13755 of EXPR where some registers may have been replaced by constants. */
13756
13757 static bool
13758 unmentioned_reg_p (rtx equiv, rtx expr)
13759 {
13760 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
13761 }
13762 \f
13763 void
13764 dump_combine_stats (FILE *file)
13765 {
13766 fprintf
13767 (file,
13768 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13769 combine_attempts, combine_merges, combine_extras, combine_successes);
13770 }
13771
13772 void
13773 dump_combine_total_stats (FILE *file)
13774 {
13775 fprintf
13776 (file,
13777 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13778 total_attempts, total_merges, total_extras, total_successes);
13779 }
13780 \f
13781 static bool
13782 gate_handle_combine (void)
13783 {
13784 return (optimize > 0);
13785 }
13786
13787 /* Try combining insns through substitution. */
13788 static unsigned int
13789 rest_of_handle_combine (void)
13790 {
13791 int rebuild_jump_labels_after_combine;
13792
13793 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13794 df_note_add_problem ();
13795 df_analyze ();
13796
13797 regstat_init_n_sets_and_refs ();
13798
13799 rebuild_jump_labels_after_combine
13800 = combine_instructions (get_insns (), max_reg_num ());
13801
13802 /* Combining insns may have turned an indirect jump into a
13803 direct jump. Rebuild the JUMP_LABEL fields of jumping
13804 instructions. */
13805 if (rebuild_jump_labels_after_combine)
13806 {
13807 timevar_push (TV_JUMP);
13808 rebuild_jump_labels (get_insns ());
13809 cleanup_cfg (0);
13810 timevar_pop (TV_JUMP);
13811 }
13812
13813 regstat_free_n_sets_and_refs ();
13814 return 0;
13815 }
13816
13817 struct rtl_opt_pass pass_combine =
13818 {
13819 {
13820 RTL_PASS,
13821 "combine", /* name */
13822 gate_handle_combine, /* gate */
13823 rest_of_handle_combine, /* execute */
13824 NULL, /* sub */
13825 NULL, /* next */
13826 0, /* static_pass_number */
13827 TV_COMBINE, /* tv_id */
13828 PROP_cfglayout, /* properties_required */
13829 0, /* properties_provided */
13830 0, /* properties_destroyed */
13831 0, /* todo_flags_start */
13832 TODO_dump_func |
13833 TODO_df_finish | TODO_verify_rtl_sharing |
13834 TODO_ggc_collect, /* todo_flags_finish */
13835 }
13836 };