]>
Commit | Line | Data |
---|---|---|
7afe21cc | 1 | /* Common subexpression elimination for GNU compiler. |
747215f1 | 2 | Copyright (C) 1987, 88, 89, 92-7, 1998, 1999 Free Software Foundation, Inc. |
7afe21cc RK |
3 | |
4 | This file is part of GNU CC. | |
5 | ||
6 | GNU CC is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GNU CC is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU CC; see the file COPYING. If not, write to | |
940d9d63 RK |
18 | the Free Software Foundation, 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
7afe21cc RK |
20 | |
21 | ||
22 | #include "config.h" | |
670ee920 KG |
23 | /* stdio.h must precede rtl.h for FFS. */ |
24 | #include "system.h" | |
50b2596f | 25 | #include <setjmp.h> |
9c3b4c8b | 26 | |
7afe21cc RK |
27 | #include "rtl.h" |
28 | #include "regs.h" | |
29 | #include "hard-reg-set.h" | |
30 | #include "flags.h" | |
31 | #include "real.h" | |
32 | #include "insn-config.h" | |
33 | #include "recog.h" | |
49ad7cfa | 34 | #include "function.h" |
956d6950 | 35 | #include "expr.h" |
50b2596f KG |
36 | #include "toplev.h" |
37 | #include "output.h" | |
30f72379 | 38 | #include "splay-tree.h" |
1497faf6 | 39 | #include "ggc.h" |
7afe21cc RK |
40 | |
41 | /* The basic idea of common subexpression elimination is to go | |
42 | through the code, keeping a record of expressions that would | |
43 | have the same value at the current scan point, and replacing | |
44 | expressions encountered with the cheapest equivalent expression. | |
45 | ||
46 | It is too complicated to keep track of the different possibilities | |
47 | when control paths merge; so, at each label, we forget all that is | |
48 | known and start fresh. This can be described as processing each | |
49 | basic block separately. Note, however, that these are not quite | |
50 | the same as the basic blocks found by a later pass and used for | |
51 | data flow analysis and register packing. We do not need to start fresh | |
52 | after a conditional jump instruction if there is no label there. | |
53 | ||
54 | We use two data structures to record the equivalent expressions: | |
55 | a hash table for most expressions, and several vectors together | |
56 | with "quantity numbers" to record equivalent (pseudo) registers. | |
57 | ||
58 | The use of the special data structure for registers is desirable | |
59 | because it is faster. It is possible because registers references | |
60 | contain a fairly small number, the register number, taken from | |
61 | a contiguously allocated series, and two register references are | |
62 | identical if they have the same number. General expressions | |
63 | do not have any such thing, so the only way to retrieve the | |
64 | information recorded on an expression other than a register | |
65 | is to keep it in a hash table. | |
66 | ||
67 | Registers and "quantity numbers": | |
68 | ||
69 | At the start of each basic block, all of the (hardware and pseudo) | |
70 | registers used in the function are given distinct quantity | |
71 | numbers to indicate their contents. During scan, when the code | |
72 | copies one register into another, we copy the quantity number. | |
73 | When a register is loaded in any other way, we allocate a new | |
74 | quantity number to describe the value generated by this operation. | |
75 | `reg_qty' records what quantity a register is currently thought | |
76 | of as containing. | |
77 | ||
78 | All real quantity numbers are greater than or equal to `max_reg'. | |
79 | If register N has not been assigned a quantity, reg_qty[N] will equal N. | |
80 | ||
81 | Quantity numbers below `max_reg' do not exist and none of the `qty_...' | |
82 | variables should be referenced with an index below `max_reg'. | |
83 | ||
84 | We also maintain a bidirectional chain of registers for each | |
85 | quantity number. `qty_first_reg', `qty_last_reg', | |
86 | `reg_next_eqv' and `reg_prev_eqv' hold these chains. | |
87 | ||
88 | The first register in a chain is the one whose lifespan is least local. | |
89 | Among equals, it is the one that was seen first. | |
90 | We replace any equivalent register with that one. | |
91 | ||
92 | If two registers have the same quantity number, it must be true that | |
93 | REG expressions with `qty_mode' must be in the hash table for both | |
94 | registers and must be in the same class. | |
95 | ||
96 | The converse is not true. Since hard registers may be referenced in | |
97 | any mode, two REG expressions might be equivalent in the hash table | |
98 | but not have the same quantity number if the quantity number of one | |
99 | of the registers is not the same mode as those expressions. | |
100 | ||
101 | Constants and quantity numbers | |
102 | ||
103 | When a quantity has a known constant value, that value is stored | |
104 | in the appropriate element of qty_const. This is in addition to | |
105 | putting the constant in the hash table as is usual for non-regs. | |
106 | ||
d45cf215 | 107 | Whether a reg or a constant is preferred is determined by the configuration |
7afe21cc RK |
108 | macro CONST_COSTS and will often depend on the constant value. In any |
109 | event, expressions containing constants can be simplified, by fold_rtx. | |
110 | ||
111 | When a quantity has a known nearly constant value (such as an address | |
112 | of a stack slot), that value is stored in the appropriate element | |
113 | of qty_const. | |
114 | ||
115 | Integer constants don't have a machine mode. However, cse | |
116 | determines the intended machine mode from the destination | |
117 | of the instruction that moves the constant. The machine mode | |
118 | is recorded in the hash table along with the actual RTL | |
119 | constant expression so that different modes are kept separate. | |
120 | ||
121 | Other expressions: | |
122 | ||
123 | To record known equivalences among expressions in general | |
124 | we use a hash table called `table'. It has a fixed number of buckets | |
125 | that contain chains of `struct table_elt' elements for expressions. | |
126 | These chains connect the elements whose expressions have the same | |
127 | hash codes. | |
128 | ||
129 | Other chains through the same elements connect the elements which | |
130 | currently have equivalent values. | |
131 | ||
132 | Register references in an expression are canonicalized before hashing | |
133 | the expression. This is done using `reg_qty' and `qty_first_reg'. | |
134 | The hash code of a register reference is computed using the quantity | |
135 | number, not the register number. | |
136 | ||
137 | When the value of an expression changes, it is necessary to remove from the | |
138 | hash table not just that expression but all expressions whose values | |
139 | could be different as a result. | |
140 | ||
141 | 1. If the value changing is in memory, except in special cases | |
142 | ANYTHING referring to memory could be changed. That is because | |
143 | nobody knows where a pointer does not point. | |
144 | The function `invalidate_memory' removes what is necessary. | |
145 | ||
146 | The special cases are when the address is constant or is | |
147 | a constant plus a fixed register such as the frame pointer | |
148 | or a static chain pointer. When such addresses are stored in, | |
149 | we can tell exactly which other such addresses must be invalidated | |
150 | due to overlap. `invalidate' does this. | |
151 | All expressions that refer to non-constant | |
152 | memory addresses are also invalidated. `invalidate_memory' does this. | |
153 | ||
154 | 2. If the value changing is a register, all expressions | |
155 | containing references to that register, and only those, | |
156 | must be removed. | |
157 | ||
158 | Because searching the entire hash table for expressions that contain | |
159 | a register is very slow, we try to figure out when it isn't necessary. | |
160 | Precisely, this is necessary only when expressions have been | |
161 | entered in the hash table using this register, and then the value has | |
162 | changed, and then another expression wants to be added to refer to | |
163 | the register's new value. This sequence of circumstances is rare | |
164 | within any one basic block. | |
165 | ||
166 | The vectors `reg_tick' and `reg_in_table' are used to detect this case. | |
167 | reg_tick[i] is incremented whenever a value is stored in register i. | |
168 | reg_in_table[i] holds -1 if no references to register i have been | |
169 | entered in the table; otherwise, it contains the value reg_tick[i] had | |
170 | when the references were entered. If we want to enter a reference | |
171 | and reg_in_table[i] != reg_tick[i], we must scan and remove old references. | |
172 | Until we want to enter a new entry, the mere fact that the two vectors | |
173 | don't match makes the entries be ignored if anyone tries to match them. | |
174 | ||
175 | Registers themselves are entered in the hash table as well as in | |
176 | the equivalent-register chains. However, the vectors `reg_tick' | |
177 | and `reg_in_table' do not apply to expressions which are simple | |
178 | register references. These expressions are removed from the table | |
179 | immediately when they become invalid, and this can be done even if | |
180 | we do not immediately search for all the expressions that refer to | |
181 | the register. | |
182 | ||
183 | A CLOBBER rtx in an instruction invalidates its operand for further | |
184 | reuse. A CLOBBER or SET rtx whose operand is a MEM:BLK | |
185 | invalidates everything that resides in memory. | |
186 | ||
187 | Related expressions: | |
188 | ||
189 | Constant expressions that differ only by an additive integer | |
190 | are called related. When a constant expression is put in | |
191 | the table, the related expression with no constant term | |
192 | is also entered. These are made to point at each other | |
193 | so that it is possible to find out if there exists any | |
194 | register equivalent to an expression related to a given expression. */ | |
195 | ||
196 | /* One plus largest register number used in this function. */ | |
197 | ||
198 | static int max_reg; | |
199 | ||
556c714b JW |
200 | /* One plus largest instruction UID used in this function at time of |
201 | cse_main call. */ | |
202 | ||
203 | static int max_insn_uid; | |
204 | ||
7afe21cc RK |
205 | /* Length of vectors indexed by quantity number. |
206 | We know in advance we will not need a quantity number this big. */ | |
207 | ||
208 | static int max_qty; | |
209 | ||
210 | /* Next quantity number to be allocated. | |
211 | This is 1 + the largest number needed so far. */ | |
212 | ||
213 | static int next_qty; | |
214 | ||
71d306d1 | 215 | /* Indexed by quantity number, gives the first (or last) register |
7afe21cc RK |
216 | in the chain of registers that currently contain this quantity. */ |
217 | ||
218 | static int *qty_first_reg; | |
219 | static int *qty_last_reg; | |
220 | ||
221 | /* Index by quantity number, gives the mode of the quantity. */ | |
222 | ||
223 | static enum machine_mode *qty_mode; | |
224 | ||
225 | /* Indexed by quantity number, gives the rtx of the constant value of the | |
226 | quantity, or zero if it does not have a known value. | |
227 | A sum of the frame pointer (or arg pointer) plus a constant | |
228 | can also be entered here. */ | |
229 | ||
230 | static rtx *qty_const; | |
231 | ||
232 | /* Indexed by qty number, gives the insn that stored the constant value | |
233 | recorded in `qty_const'. */ | |
234 | ||
235 | static rtx *qty_const_insn; | |
236 | ||
237 | /* The next three variables are used to track when a comparison between a | |
238 | quantity and some constant or register has been passed. In that case, we | |
239 | know the results of the comparison in case we see it again. These variables | |
240 | record a comparison that is known to be true. */ | |
241 | ||
242 | /* Indexed by qty number, gives the rtx code of a comparison with a known | |
243 | result involving this quantity. If none, it is UNKNOWN. */ | |
244 | static enum rtx_code *qty_comparison_code; | |
245 | ||
246 | /* Indexed by qty number, gives the constant being compared against in a | |
247 | comparison of known result. If no such comparison, it is undefined. | |
248 | If the comparison is not with a constant, it is zero. */ | |
249 | ||
250 | static rtx *qty_comparison_const; | |
251 | ||
252 | /* Indexed by qty number, gives the quantity being compared against in a | |
253 | comparison of known result. If no such comparison, if it undefined. | |
254 | If the comparison is not with a register, it is -1. */ | |
255 | ||
256 | static int *qty_comparison_qty; | |
257 | ||
258 | #ifdef HAVE_cc0 | |
259 | /* For machines that have a CC0, we do not record its value in the hash | |
260 | table since its use is guaranteed to be the insn immediately following | |
261 | its definition and any other insn is presumed to invalidate it. | |
262 | ||
263 | Instead, we store below the value last assigned to CC0. If it should | |
264 | happen to be a constant, it is stored in preference to the actual | |
265 | assigned value. In case it is a constant, we store the mode in which | |
266 | the constant should be interpreted. */ | |
267 | ||
268 | static rtx prev_insn_cc0; | |
269 | static enum machine_mode prev_insn_cc0_mode; | |
270 | #endif | |
271 | ||
272 | /* Previous actual insn. 0 if at first insn of basic block. */ | |
273 | ||
274 | static rtx prev_insn; | |
275 | ||
276 | /* Insn being scanned. */ | |
277 | ||
278 | static rtx this_insn; | |
279 | ||
71d306d1 DE |
280 | /* Index by register number, gives the number of the next (or |
281 | previous) register in the chain of registers sharing the same | |
7afe21cc RK |
282 | value. |
283 | ||
284 | Or -1 if this register is at the end of the chain. | |
285 | ||
286 | If reg_qty[N] == N, reg_next_eqv[N] is undefined. */ | |
287 | ||
288 | static int *reg_next_eqv; | |
289 | static int *reg_prev_eqv; | |
290 | ||
30f72379 MM |
291 | struct cse_reg_info { |
292 | union { | |
293 | /* The number of times the register has been altered in the current | |
294 | basic block. */ | |
295 | int reg_tick; | |
296 | ||
297 | /* The next cse_reg_info structure in the free list. */ | |
298 | struct cse_reg_info* next; | |
299 | } variant; | |
300 | ||
301 | /* The REG_TICK value at which rtx's containing this register are | |
302 | valid in the hash table. If this does not equal the current | |
303 | reg_tick value, such expressions existing in the hash table are | |
304 | invalid. */ | |
305 | int reg_in_table; | |
306 | ||
307 | /* The quantity number of the register's current contents. */ | |
308 | int reg_qty; | |
309 | }; | |
7afe21cc | 310 | |
30f72379 MM |
311 | /* A free list of cse_reg_info entries. */ |
312 | static struct cse_reg_info *cse_reg_info_free_list; | |
7afe21cc | 313 | |
30f72379 MM |
314 | /* A mapping from registers to cse_reg_info data structures. */ |
315 | static splay_tree cse_reg_info_tree; | |
7afe21cc | 316 | |
30f72379 MM |
317 | /* The last lookup we did into the cse_reg_info_tree. This allows us |
318 | to cache repeated lookups. */ | |
319 | static int cached_regno; | |
320 | static struct cse_reg_info *cached_cse_reg_info; | |
7afe21cc RK |
321 | |
322 | /* A HARD_REG_SET containing all the hard registers for which there is | |
323 | currently a REG expression in the hash table. Note the difference | |
324 | from the above variables, which indicate if the REG is mentioned in some | |
325 | expression in the table. */ | |
326 | ||
327 | static HARD_REG_SET hard_regs_in_table; | |
328 | ||
329 | /* A HARD_REG_SET containing all the hard registers that are invalidated | |
330 | by a CALL_INSN. */ | |
331 | ||
332 | static HARD_REG_SET regs_invalidated_by_call; | |
333 | ||
7afe21cc RK |
334 | /* CUID of insn that starts the basic block currently being cse-processed. */ |
335 | ||
336 | static int cse_basic_block_start; | |
337 | ||
338 | /* CUID of insn that ends the basic block currently being cse-processed. */ | |
339 | ||
340 | static int cse_basic_block_end; | |
341 | ||
342 | /* Vector mapping INSN_UIDs to cuids. | |
d45cf215 | 343 | The cuids are like uids but increase monotonically always. |
7afe21cc RK |
344 | We use them to see whether a reg is used outside a given basic block. */ |
345 | ||
906c4e36 | 346 | static int *uid_cuid; |
7afe21cc | 347 | |
164c8956 RK |
348 | /* Highest UID in UID_CUID. */ |
349 | static int max_uid; | |
350 | ||
7afe21cc RK |
351 | /* Get the cuid of an insn. */ |
352 | ||
353 | #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)]) | |
354 | ||
355 | /* Nonzero if cse has altered conditional jump insns | |
356 | in such a way that jump optimization should be redone. */ | |
357 | ||
358 | static int cse_jumps_altered; | |
359 | ||
a5dfb4ee RK |
360 | /* Nonzero if we put a LABEL_REF into the hash table. Since we may have put |
361 | it into an INSN without a REG_LABEL, we have to rerun jump after CSE | |
362 | to put in the note. */ | |
363 | static int recorded_label_ref; | |
364 | ||
7afe21cc RK |
365 | /* canon_hash stores 1 in do_not_record |
366 | if it notices a reference to CC0, PC, or some other volatile | |
367 | subexpression. */ | |
368 | ||
369 | static int do_not_record; | |
370 | ||
7bac1be0 RK |
371 | #ifdef LOAD_EXTEND_OP |
372 | ||
373 | /* Scratch rtl used when looking for load-extended copy of a MEM. */ | |
374 | static rtx memory_extend_rtx; | |
375 | #endif | |
376 | ||
7afe21cc RK |
377 | /* canon_hash stores 1 in hash_arg_in_memory |
378 | if it notices a reference to memory within the expression being hashed. */ | |
379 | ||
380 | static int hash_arg_in_memory; | |
381 | ||
382 | /* canon_hash stores 1 in hash_arg_in_struct | |
383 | if it notices a reference to memory that's part of a structure. */ | |
384 | ||
385 | static int hash_arg_in_struct; | |
386 | ||
387 | /* The hash table contains buckets which are chains of `struct table_elt's, | |
388 | each recording one expression's information. | |
389 | That expression is in the `exp' field. | |
390 | ||
391 | Those elements with the same hash code are chained in both directions | |
392 | through the `next_same_hash' and `prev_same_hash' fields. | |
393 | ||
394 | Each set of expressions with equivalent values | |
395 | are on a two-way chain through the `next_same_value' | |
396 | and `prev_same_value' fields, and all point with | |
397 | the `first_same_value' field at the first element in | |
398 | that chain. The chain is in order of increasing cost. | |
399 | Each element's cost value is in its `cost' field. | |
400 | ||
401 | The `in_memory' field is nonzero for elements that | |
402 | involve any reference to memory. These elements are removed | |
403 | whenever a write is done to an unidentified location in memory. | |
404 | To be safe, we assume that a memory address is unidentified unless | |
405 | the address is either a symbol constant or a constant plus | |
406 | the frame pointer or argument pointer. | |
407 | ||
408 | The `in_struct' field is nonzero for elements that | |
409 | involve any reference to memory inside a structure or array. | |
410 | ||
411 | The `related_value' field is used to connect related expressions | |
412 | (that differ by adding an integer). | |
413 | The related expressions are chained in a circular fashion. | |
414 | `related_value' is zero for expressions for which this | |
415 | chain is not useful. | |
416 | ||
417 | The `cost' field stores the cost of this element's expression. | |
418 | ||
419 | The `is_const' flag is set if the element is a constant (including | |
420 | a fixed address). | |
421 | ||
422 | The `flag' field is used as a temporary during some search routines. | |
423 | ||
424 | The `mode' field is usually the same as GET_MODE (`exp'), but | |
425 | if `exp' is a CONST_INT and has no machine mode then the `mode' | |
426 | field is the mode it was being used as. Each constant is | |
427 | recorded separately for each mode it is used with. */ | |
428 | ||
429 | ||
430 | struct table_elt | |
431 | { | |
432 | rtx exp; | |
433 | struct table_elt *next_same_hash; | |
434 | struct table_elt *prev_same_hash; | |
435 | struct table_elt *next_same_value; | |
436 | struct table_elt *prev_same_value; | |
437 | struct table_elt *first_same_value; | |
438 | struct table_elt *related_value; | |
439 | int cost; | |
440 | enum machine_mode mode; | |
441 | char in_memory; | |
442 | char in_struct; | |
443 | char is_const; | |
444 | char flag; | |
445 | }; | |
446 | ||
7afe21cc RK |
447 | /* We don't want a lot of buckets, because we rarely have very many |
448 | things stored in the hash table, and a lot of buckets slows | |
449 | down a lot of loops that happen frequently. */ | |
450 | #define NBUCKETS 31 | |
451 | ||
452 | /* Compute hash code of X in mode M. Special-case case where X is a pseudo | |
453 | register (hard registers may require `do_not_record' to be set). */ | |
454 | ||
455 | #define HASH(X, M) \ | |
456 | (GET_CODE (X) == REG && REGNO (X) >= FIRST_PSEUDO_REGISTER \ | |
30f72379 | 457 | ? (((unsigned) REG << 7) + (unsigned) REG_QTY (REGNO (X))) % NBUCKETS \ |
7afe21cc RK |
458 | : canon_hash (X, M) % NBUCKETS) |
459 | ||
460 | /* Determine whether register number N is considered a fixed register for CSE. | |
461 | It is desirable to replace other regs with fixed regs, to reduce need for | |
462 | non-fixed hard regs. | |
463 | A reg wins if it is either the frame pointer or designated as fixed, | |
464 | but not if it is an overlapping register. */ | |
465 | #ifdef OVERLAPPING_REGNO_P | |
466 | #define FIXED_REGNO_P(N) \ | |
8bc169f2 | 467 | (((N) == FRAME_POINTER_REGNUM || (N) == HARD_FRAME_POINTER_REGNUM \ |
6ab832bc | 468 | || fixed_regs[N] || global_regs[N]) \ |
7afe21cc RK |
469 | && ! OVERLAPPING_REGNO_P ((N))) |
470 | #else | |
471 | #define FIXED_REGNO_P(N) \ | |
8bc169f2 | 472 | ((N) == FRAME_POINTER_REGNUM || (N) == HARD_FRAME_POINTER_REGNUM \ |
6ab832bc | 473 | || fixed_regs[N] || global_regs[N]) |
7afe21cc RK |
474 | #endif |
475 | ||
476 | /* Compute cost of X, as stored in the `cost' field of a table_elt. Fixed | |
ac07e066 RK |
477 | hard registers and pointers into the frame are the cheapest with a cost |
478 | of 0. Next come pseudos with a cost of one and other hard registers with | |
479 | a cost of 2. Aside from these special cases, call `rtx_cost'. */ | |
480 | ||
6ab832bc | 481 | #define CHEAP_REGNO(N) \ |
8bc169f2 DE |
482 | ((N) == FRAME_POINTER_REGNUM || (N) == HARD_FRAME_POINTER_REGNUM \ |
483 | || (N) == STACK_POINTER_REGNUM || (N) == ARG_POINTER_REGNUM \ | |
484 | || ((N) >= FIRST_VIRTUAL_REGISTER && (N) <= LAST_VIRTUAL_REGISTER) \ | |
485 | || ((N) < FIRST_PSEUDO_REGISTER \ | |
e7bb59fa | 486 | && FIXED_REGNO_P (N) && REGNO_REG_CLASS (N) != NO_REGS)) |
7afe21cc | 487 | |
6ab832bc RK |
488 | /* A register is cheap if it is a user variable assigned to the register |
489 | or if its register number always corresponds to a cheap register. */ | |
490 | ||
491 | #define CHEAP_REG(N) \ | |
492 | ((REG_USERVAR_P (N) && REGNO (N) < FIRST_PSEUDO_REGISTER) \ | |
493 | || CHEAP_REGNO (REGNO (N))) | |
494 | ||
38734e55 ILT |
495 | #define COST(X) \ |
496 | (GET_CODE (X) == REG \ | |
497 | ? (CHEAP_REG (X) ? 0 \ | |
498 | : REGNO (X) >= FIRST_PSEUDO_REGISTER ? 1 \ | |
499 | : 2) \ | |
954a5693 | 500 | : notreg_cost(X)) |
7afe21cc | 501 | |
30f72379 MM |
502 | /* Get the info associated with register N. */ |
503 | ||
504 | #define GET_CSE_REG_INFO(N) \ | |
505 | (((N) == cached_regno && cached_cse_reg_info) \ | |
506 | ? cached_cse_reg_info : get_cse_reg_info ((N))) | |
507 | ||
508 | /* Get the number of times this register has been updated in this | |
509 | basic block. */ | |
510 | ||
511 | #define REG_TICK(N) ((GET_CSE_REG_INFO (N))->variant.reg_tick) | |
512 | ||
513 | /* Get the point at which REG was recorded in the table. */ | |
514 | ||
515 | #define REG_IN_TABLE(N) ((GET_CSE_REG_INFO (N))->reg_in_table) | |
516 | ||
517 | /* Get the quantity number for REG. */ | |
518 | ||
519 | #define REG_QTY(N) ((GET_CSE_REG_INFO (N))->reg_qty) | |
520 | ||
7afe21cc RK |
521 | /* Determine if the quantity number for register X represents a valid index |
522 | into the `qty_...' variables. */ | |
523 | ||
30f72379 | 524 | #define REGNO_QTY_VALID_P(N) (REG_QTY (N) != (N)) |
7afe21cc | 525 | |
2f541799 MM |
526 | #ifdef ADDRESS_COST |
527 | /* The ADDRESS_COST macro does not deal with ADDRESSOF nodes. But, | |
528 | during CSE, such nodes are present. Using an ADDRESSOF node which | |
529 | refers to the address of a REG is a good thing because we can then | |
530 | turn (MEM (ADDRESSSOF (REG))) into just plain REG. */ | |
531 | #define CSE_ADDRESS_COST(RTX) \ | |
532 | ((GET_CODE (RTX) == ADDRESSOF && REG_P (XEXP ((RTX), 0))) \ | |
533 | ? -1 : ADDRESS_COST(RTX)) | |
534 | #endif | |
535 | ||
7afe21cc RK |
536 | static struct table_elt *table[NBUCKETS]; |
537 | ||
538 | /* Chain of `struct table_elt's made so far for this function | |
539 | but currently removed from the table. */ | |
540 | ||
541 | static struct table_elt *free_element_chain; | |
542 | ||
543 | /* Number of `struct table_elt' structures made so far for this function. */ | |
544 | ||
545 | static int n_elements_made; | |
546 | ||
547 | /* Maximum value `n_elements_made' has had so far in this compilation | |
548 | for functions previously processed. */ | |
549 | ||
550 | static int max_elements_made; | |
551 | ||
552 | /* Surviving equivalence class when two equivalence classes are merged | |
553 | by recording the effects of a jump in the last insn. Zero if the | |
554 | last insn was not a conditional jump. */ | |
555 | ||
556 | static struct table_elt *last_jump_equiv_class; | |
557 | ||
558 | /* Set to the cost of a constant pool reference if one was found for a | |
559 | symbolic constant. If this was found, it means we should try to | |
560 | convert constants into constant pool entries if they don't fit in | |
561 | the insn. */ | |
562 | ||
563 | static int constant_pool_entries_cost; | |
564 | ||
6cd4575e RK |
565 | /* Define maximum length of a branch path. */ |
566 | ||
567 | #define PATHLENGTH 10 | |
568 | ||
569 | /* This data describes a block that will be processed by cse_basic_block. */ | |
570 | ||
571 | struct cse_basic_block_data { | |
572 | /* Lowest CUID value of insns in block. */ | |
573 | int low_cuid; | |
574 | /* Highest CUID value of insns in block. */ | |
575 | int high_cuid; | |
576 | /* Total number of SETs in block. */ | |
577 | int nsets; | |
578 | /* Last insn in the block. */ | |
579 | rtx last; | |
580 | /* Size of current branch path, if any. */ | |
581 | int path_size; | |
582 | /* Current branch path, indicating which branches will be taken. */ | |
583 | struct branch_path { | |
0f41302f | 584 | /* The branch insn. */ |
6cd4575e RK |
585 | rtx branch; |
586 | /* Whether it should be taken or not. AROUND is the same as taken | |
587 | except that it is used when the destination label is not preceded | |
588 | by a BARRIER. */ | |
589 | enum taken {TAKEN, NOT_TAKEN, AROUND} status; | |
590 | } path[PATHLENGTH]; | |
591 | }; | |
592 | ||
7afe21cc RK |
593 | /* Nonzero if X has the form (PLUS frame-pointer integer). We check for |
594 | virtual regs here because the simplify_*_operation routines are called | |
595 | by integrate.c, which is called before virtual register instantiation. */ | |
596 | ||
597 | #define FIXED_BASE_PLUS_P(X) \ | |
8bc169f2 DE |
598 | ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx \ |
599 | || (X) == arg_pointer_rtx \ | |
7afe21cc RK |
600 | || (X) == virtual_stack_vars_rtx \ |
601 | || (X) == virtual_incoming_args_rtx \ | |
602 | || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \ | |
603 | && (XEXP (X, 0) == frame_pointer_rtx \ | |
8bc169f2 | 604 | || XEXP (X, 0) == hard_frame_pointer_rtx \ |
7afe21cc RK |
605 | || XEXP (X, 0) == arg_pointer_rtx \ |
606 | || XEXP (X, 0) == virtual_stack_vars_rtx \ | |
e9a25f70 JL |
607 | || XEXP (X, 0) == virtual_incoming_args_rtx)) \ |
608 | || GET_CODE (X) == ADDRESSOF) | |
7afe21cc | 609 | |
6f90e075 JW |
610 | /* Similar, but also allows reference to the stack pointer. |
611 | ||
612 | This used to include FIXED_BASE_PLUS_P, however, we can't assume that | |
613 | arg_pointer_rtx by itself is nonzero, because on at least one machine, | |
614 | the i960, the arg pointer is zero when it is unused. */ | |
7afe21cc RK |
615 | |
616 | #define NONZERO_BASE_PLUS_P(X) \ | |
8bc169f2 | 617 | ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx \ |
6f90e075 JW |
618 | || (X) == virtual_stack_vars_rtx \ |
619 | || (X) == virtual_incoming_args_rtx \ | |
620 | || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \ | |
621 | && (XEXP (X, 0) == frame_pointer_rtx \ | |
8bc169f2 | 622 | || XEXP (X, 0) == hard_frame_pointer_rtx \ |
6f90e075 JW |
623 | || XEXP (X, 0) == arg_pointer_rtx \ |
624 | || XEXP (X, 0) == virtual_stack_vars_rtx \ | |
625 | || XEXP (X, 0) == virtual_incoming_args_rtx)) \ | |
7afe21cc RK |
626 | || (X) == stack_pointer_rtx \ |
627 | || (X) == virtual_stack_dynamic_rtx \ | |
628 | || (X) == virtual_outgoing_args_rtx \ | |
629 | || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \ | |
630 | && (XEXP (X, 0) == stack_pointer_rtx \ | |
631 | || XEXP (X, 0) == virtual_stack_dynamic_rtx \ | |
e9a25f70 JL |
632 | || XEXP (X, 0) == virtual_outgoing_args_rtx)) \ |
633 | || GET_CODE (X) == ADDRESSOF) | |
7afe21cc | 634 | |
954a5693 | 635 | static int notreg_cost PROTO((rtx)); |
6cd4575e RK |
636 | static void new_basic_block PROTO((void)); |
637 | static void make_new_qty PROTO((int)); | |
638 | static void make_regs_eqv PROTO((int, int)); | |
639 | static void delete_reg_equiv PROTO((int)); | |
640 | static int mention_regs PROTO((rtx)); | |
641 | static int insert_regs PROTO((rtx, struct table_elt *, int)); | |
642 | static void free_element PROTO((struct table_elt *)); | |
2197a88a | 643 | static void remove_from_table PROTO((struct table_elt *, unsigned)); |
6cd4575e | 644 | static struct table_elt *get_element PROTO((void)); |
2197a88a RK |
645 | static struct table_elt *lookup PROTO((rtx, unsigned, enum machine_mode)), |
646 | *lookup_for_remove PROTO((rtx, unsigned, enum machine_mode)); | |
6cd4575e | 647 | static rtx lookup_as_function PROTO((rtx, enum rtx_code)); |
2197a88a | 648 | static struct table_elt *insert PROTO((rtx, struct table_elt *, unsigned, |
6cd4575e RK |
649 | enum machine_mode)); |
650 | static void merge_equiv_classes PROTO((struct table_elt *, | |
651 | struct table_elt *)); | |
68c1e173 | 652 | static void invalidate PROTO((rtx, enum machine_mode)); |
9ae8ffe7 | 653 | static int cse_rtx_varies_p PROTO((rtx)); |
6cd4575e | 654 | static void remove_invalid_refs PROTO((int)); |
34c73909 | 655 | static void remove_invalid_subreg_refs PROTO((int, int, enum machine_mode)); |
6cd4575e | 656 | static void rehash_using_reg PROTO((rtx)); |
9ae8ffe7 | 657 | static void invalidate_memory PROTO((void)); |
6cd4575e RK |
658 | static void invalidate_for_call PROTO((void)); |
659 | static rtx use_related_value PROTO((rtx, struct table_elt *)); | |
2197a88a RK |
660 | static unsigned canon_hash PROTO((rtx, enum machine_mode)); |
661 | static unsigned safe_hash PROTO((rtx, enum machine_mode)); | |
6cd4575e | 662 | static int exp_equiv_p PROTO((rtx, rtx, int, int)); |
f451db89 | 663 | static void set_nonvarying_address_components PROTO((rtx, int, rtx *, |
6500fb43 RK |
664 | HOST_WIDE_INT *, |
665 | HOST_WIDE_INT *)); | |
6cd4575e | 666 | static int refers_to_p PROTO((rtx, rtx)); |
6cd4575e RK |
667 | static rtx canon_reg PROTO((rtx, rtx)); |
668 | static void find_best_addr PROTO((rtx, rtx *)); | |
669 | static enum rtx_code find_comparison_args PROTO((enum rtx_code, rtx *, rtx *, | |
670 | enum machine_mode *, | |
671 | enum machine_mode *)); | |
96b0e481 RK |
672 | static rtx cse_gen_binary PROTO((enum rtx_code, enum machine_mode, |
673 | rtx, rtx)); | |
674 | static rtx simplify_plus_minus PROTO((enum rtx_code, enum machine_mode, | |
675 | rtx, rtx)); | |
6cd4575e RK |
676 | static rtx fold_rtx PROTO((rtx, rtx)); |
677 | static rtx equiv_constant PROTO((rtx)); | |
678 | static void record_jump_equiv PROTO((rtx, int)); | |
679 | static void record_jump_cond PROTO((enum rtx_code, enum machine_mode, | |
680 | rtx, rtx, int)); | |
7bd8b2a8 | 681 | static void cse_insn PROTO((rtx, rtx)); |
9ae8ffe7 JL |
682 | static int note_mem_written PROTO((rtx)); |
683 | static void invalidate_from_clobbers PROTO((rtx)); | |
6cd4575e RK |
684 | static rtx cse_process_notes PROTO((rtx, rtx)); |
685 | static void cse_around_loop PROTO((rtx)); | |
686 | static void invalidate_skipped_set PROTO((rtx, rtx)); | |
687 | static void invalidate_skipped_block PROTO((rtx)); | |
688 | static void cse_check_loop_start PROTO((rtx, rtx)); | |
689 | static void cse_set_around_loop PROTO((rtx, rtx, rtx)); | |
690 | static rtx cse_basic_block PROTO((rtx, rtx, struct branch_path *, int)); | |
79644f06 | 691 | static void count_reg_usage PROTO((rtx, int *, rtx, int)); |
a0153051 | 692 | extern void dump_class PROTO((struct table_elt*)); |
1a87eea2 | 693 | static void check_fold_consts PROTO((PTR)); |
30f72379 MM |
694 | static struct cse_reg_info* get_cse_reg_info PROTO((int)); |
695 | static void free_cse_reg_info PROTO((splay_tree_value)); | |
01e752d3 | 696 | static void flush_hash_table PROTO((void)); |
7afe21cc | 697 | \f |
a4c6502a MM |
698 | /* Dump the expressions in the equivalence class indicated by CLASSP. |
699 | This function is used only for debugging. */ | |
a0153051 | 700 | void |
a4c6502a MM |
701 | dump_class (classp) |
702 | struct table_elt *classp; | |
703 | { | |
704 | struct table_elt *elt; | |
705 | ||
706 | fprintf (stderr, "Equivalence chain for "); | |
707 | print_rtl (stderr, classp->exp); | |
708 | fprintf (stderr, ": \n"); | |
709 | ||
710 | for (elt = classp->first_same_value; elt; elt = elt->next_same_value) | |
711 | { | |
712 | print_rtl (stderr, elt->exp); | |
713 | fprintf (stderr, "\n"); | |
714 | } | |
715 | } | |
716 | ||
7afe21cc RK |
717 | /* Return an estimate of the cost of computing rtx X. |
718 | One use is in cse, to decide which expression to keep in the hash table. | |
719 | Another is in rtl generation, to pick the cheapest way to multiply. | |
720 | Other uses like the latter are expected in the future. */ | |
721 | ||
954a5693 RK |
722 | /* Internal function, to compute cost when X is not a register; called |
723 | from COST macro to keep it simple. */ | |
724 | ||
725 | static int | |
726 | notreg_cost (x) | |
727 | rtx x; | |
728 | { | |
729 | return ((GET_CODE (x) == SUBREG | |
730 | && GET_CODE (SUBREG_REG (x)) == REG | |
731 | && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT | |
732 | && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT | |
733 | && (GET_MODE_SIZE (GET_MODE (x)) | |
734 | < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))) | |
735 | && subreg_lowpart_p (x) | |
736 | && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (x)), | |
737 | GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))) | |
738 | ? (CHEAP_REG (SUBREG_REG (x)) ? 0 | |
739 | : (REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER ? 1 | |
740 | : 2)) | |
741 | : rtx_cost (x, SET) * 2); | |
742 | } | |
743 | ||
7afe21cc RK |
744 | /* Return the right cost to give to an operation |
745 | to make the cost of the corresponding register-to-register instruction | |
746 | N times that of a fast register-to-register instruction. */ | |
747 | ||
748 | #define COSTS_N_INSNS(N) ((N) * 4 - 2) | |
749 | ||
750 | int | |
e5f6a288 | 751 | rtx_cost (x, outer_code) |
7afe21cc | 752 | rtx x; |
79c9824e | 753 | enum rtx_code outer_code ATTRIBUTE_UNUSED; |
7afe21cc RK |
754 | { |
755 | register int i, j; | |
756 | register enum rtx_code code; | |
6f7d635c | 757 | register const char *fmt; |
7afe21cc RK |
758 | register int total; |
759 | ||
760 | if (x == 0) | |
761 | return 0; | |
762 | ||
763 | /* Compute the default costs of certain things. | |
764 | Note that RTX_COSTS can override the defaults. */ | |
765 | ||
766 | code = GET_CODE (x); | |
767 | switch (code) | |
768 | { | |
769 | case MULT: | |
770 | /* Count multiplication by 2**n as a shift, | |
771 | because if we are considering it, we would output it as a shift. */ | |
772 | if (GET_CODE (XEXP (x, 1)) == CONST_INT | |
773 | && exact_log2 (INTVAL (XEXP (x, 1))) >= 0) | |
774 | total = 2; | |
775 | else | |
776 | total = COSTS_N_INSNS (5); | |
777 | break; | |
778 | case DIV: | |
779 | case UDIV: | |
780 | case MOD: | |
781 | case UMOD: | |
782 | total = COSTS_N_INSNS (7); | |
783 | break; | |
784 | case USE: | |
785 | /* Used in loop.c and combine.c as a marker. */ | |
786 | total = 0; | |
787 | break; | |
538b78e7 RS |
788 | case ASM_OPERANDS: |
789 | /* We don't want these to be used in substitutions because | |
790 | we have no way of validating the resulting insn. So assign | |
791 | anything containing an ASM_OPERANDS a very high cost. */ | |
792 | total = 1000; | |
793 | break; | |
7afe21cc RK |
794 | default: |
795 | total = 2; | |
796 | } | |
797 | ||
798 | switch (code) | |
799 | { | |
800 | case REG: | |
6ab832bc | 801 | return ! CHEAP_REG (x); |
ac07e066 | 802 | |
7afe21cc | 803 | case SUBREG: |
fc3ffe83 RK |
804 | /* If we can't tie these modes, make this expensive. The larger |
805 | the mode, the more expensive it is. */ | |
806 | if (! MODES_TIEABLE_P (GET_MODE (x), GET_MODE (SUBREG_REG (x)))) | |
807 | return COSTS_N_INSNS (2 | |
808 | + GET_MODE_SIZE (GET_MODE (x)) / UNITS_PER_WORD); | |
7afe21cc RK |
809 | return 2; |
810 | #ifdef RTX_COSTS | |
e5f6a288 | 811 | RTX_COSTS (x, code, outer_code); |
7afe21cc | 812 | #endif |
47a0b68f | 813 | #ifdef CONST_COSTS |
e5f6a288 | 814 | CONST_COSTS (x, code, outer_code); |
47a0b68f | 815 | #endif |
8625fab5 KG |
816 | |
817 | default: | |
818 | #ifdef DEFAULT_RTX_COSTS | |
819 | DEFAULT_RTX_COSTS(x, code, outer_code); | |
820 | #endif | |
821 | break; | |
7afe21cc RK |
822 | } |
823 | ||
824 | /* Sum the costs of the sub-rtx's, plus cost of this operation, | |
825 | which is already in total. */ | |
826 | ||
827 | fmt = GET_RTX_FORMAT (code); | |
828 | for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) | |
829 | if (fmt[i] == 'e') | |
e5f6a288 | 830 | total += rtx_cost (XEXP (x, i), code); |
7afe21cc RK |
831 | else if (fmt[i] == 'E') |
832 | for (j = 0; j < XVECLEN (x, i); j++) | |
e5f6a288 | 833 | total += rtx_cost (XVECEXP (x, i, j), code); |
7afe21cc RK |
834 | |
835 | return total; | |
836 | } | |
837 | \f | |
30f72379 MM |
838 | static struct cse_reg_info * |
839 | get_cse_reg_info (regno) | |
840 | int regno; | |
841 | { | |
842 | struct cse_reg_info *cri; | |
843 | splay_tree_node n; | |
844 | ||
845 | /* See if we already have this entry. */ | |
846 | n = splay_tree_lookup (cse_reg_info_tree, | |
847 | (splay_tree_key) regno); | |
848 | if (n) | |
849 | cri = (struct cse_reg_info *) (n->value); | |
850 | else | |
851 | { | |
852 | /* Get a new cse_reg_info structure. */ | |
853 | if (cse_reg_info_free_list) | |
854 | { | |
855 | cri = cse_reg_info_free_list; | |
856 | cse_reg_info_free_list = cri->variant.next; | |
857 | } | |
858 | else | |
859 | cri = (struct cse_reg_info *) xmalloc (sizeof (struct cse_reg_info)); | |
860 | ||
861 | /* Initialize it. */ | |
862 | cri->variant.reg_tick = 0; | |
863 | cri->reg_in_table = -1; | |
864 | cri->reg_qty = regno; | |
865 | ||
866 | splay_tree_insert (cse_reg_info_tree, | |
867 | (splay_tree_key) regno, | |
868 | (splay_tree_value) cri); | |
869 | } | |
870 | ||
871 | /* Cache this lookup; we tend to be looking up information about the | |
872 | same register several times in a row. */ | |
873 | cached_regno = regno; | |
874 | cached_cse_reg_info = cri; | |
875 | ||
876 | return cri; | |
877 | } | |
878 | ||
879 | static void | |
880 | free_cse_reg_info (v) | |
881 | splay_tree_value v; | |
882 | { | |
883 | struct cse_reg_info *cri = (struct cse_reg_info *) v; | |
884 | ||
885 | cri->variant.next = cse_reg_info_free_list; | |
886 | cse_reg_info_free_list = cri; | |
887 | } | |
888 | ||
7afe21cc RK |
889 | /* Clear the hash table and initialize each register with its own quantity, |
890 | for a new basic block. */ | |
891 | ||
892 | static void | |
893 | new_basic_block () | |
894 | { | |
895 | register int i; | |
896 | ||
897 | next_qty = max_reg; | |
898 | ||
30f72379 MM |
899 | if (cse_reg_info_tree) |
900 | { | |
901 | splay_tree_delete (cse_reg_info_tree); | |
902 | cached_cse_reg_info = 0; | |
903 | } | |
904 | ||
905 | cse_reg_info_tree = splay_tree_new (splay_tree_compare_ints, 0, | |
906 | free_cse_reg_info); | |
7afe21cc | 907 | |
7afe21cc RK |
908 | CLEAR_HARD_REG_SET (hard_regs_in_table); |
909 | ||
910 | /* The per-quantity values used to be initialized here, but it is | |
911 | much faster to initialize each as it is made in `make_new_qty'. */ | |
912 | ||
913 | for (i = 0; i < NBUCKETS; i++) | |
914 | { | |
915 | register struct table_elt *this, *next; | |
916 | for (this = table[i]; this; this = next) | |
917 | { | |
918 | next = this->next_same_hash; | |
919 | free_element (this); | |
920 | } | |
921 | } | |
922 | ||
4c9a05bc | 923 | bzero ((char *) table, sizeof table); |
7afe21cc RK |
924 | |
925 | prev_insn = 0; | |
926 | ||
927 | #ifdef HAVE_cc0 | |
928 | prev_insn_cc0 = 0; | |
929 | #endif | |
930 | } | |
931 | ||
932 | /* Say that register REG contains a quantity not in any register before | |
933 | and initialize that quantity. */ | |
934 | ||
935 | static void | |
936 | make_new_qty (reg) | |
937 | register int reg; | |
938 | { | |
939 | register int q; | |
940 | ||
941 | if (next_qty >= max_qty) | |
942 | abort (); | |
943 | ||
30f72379 | 944 | q = REG_QTY (reg) = next_qty++; |
7afe21cc RK |
945 | qty_first_reg[q] = reg; |
946 | qty_last_reg[q] = reg; | |
947 | qty_const[q] = qty_const_insn[q] = 0; | |
948 | qty_comparison_code[q] = UNKNOWN; | |
949 | ||
950 | reg_next_eqv[reg] = reg_prev_eqv[reg] = -1; | |
951 | } | |
952 | ||
953 | /* Make reg NEW equivalent to reg OLD. | |
954 | OLD is not changing; NEW is. */ | |
955 | ||
956 | static void | |
957 | make_regs_eqv (new, old) | |
958 | register int new, old; | |
959 | { | |
960 | register int lastr, firstr; | |
30f72379 | 961 | register int q = REG_QTY (old); |
7afe21cc RK |
962 | |
963 | /* Nothing should become eqv until it has a "non-invalid" qty number. */ | |
964 | if (! REGNO_QTY_VALID_P (old)) | |
965 | abort (); | |
966 | ||
30f72379 | 967 | REG_QTY (new) = q; |
7afe21cc RK |
968 | firstr = qty_first_reg[q]; |
969 | lastr = qty_last_reg[q]; | |
970 | ||
971 | /* Prefer fixed hard registers to anything. Prefer pseudo regs to other | |
972 | hard regs. Among pseudos, if NEW will live longer than any other reg | |
973 | of the same qty, and that is beyond the current basic block, | |
974 | make it the new canonical replacement for this qty. */ | |
975 | if (! (firstr < FIRST_PSEUDO_REGISTER && FIXED_REGNO_P (firstr)) | |
976 | /* Certain fixed registers might be of the class NO_REGS. This means | |
977 | that not only can they not be allocated by the compiler, but | |
830a38ee | 978 | they cannot be used in substitutions or canonicalizations |
7afe21cc RK |
979 | either. */ |
980 | && (new >= FIRST_PSEUDO_REGISTER || REGNO_REG_CLASS (new) != NO_REGS) | |
981 | && ((new < FIRST_PSEUDO_REGISTER && FIXED_REGNO_P (new)) | |
982 | || (new >= FIRST_PSEUDO_REGISTER | |
983 | && (firstr < FIRST_PSEUDO_REGISTER | |
b1f21e0a MM |
984 | || ((uid_cuid[REGNO_LAST_UID (new)] > cse_basic_block_end |
985 | || (uid_cuid[REGNO_FIRST_UID (new)] | |
7afe21cc | 986 | < cse_basic_block_start)) |
b1f21e0a MM |
987 | && (uid_cuid[REGNO_LAST_UID (new)] |
988 | > uid_cuid[REGNO_LAST_UID (firstr)])))))) | |
7afe21cc RK |
989 | { |
990 | reg_prev_eqv[firstr] = new; | |
991 | reg_next_eqv[new] = firstr; | |
992 | reg_prev_eqv[new] = -1; | |
993 | qty_first_reg[q] = new; | |
994 | } | |
995 | else | |
996 | { | |
997 | /* If NEW is a hard reg (known to be non-fixed), insert at end. | |
998 | Otherwise, insert before any non-fixed hard regs that are at the | |
999 | end. Registers of class NO_REGS cannot be used as an | |
1000 | equivalent for anything. */ | |
1001 | while (lastr < FIRST_PSEUDO_REGISTER && reg_prev_eqv[lastr] >= 0 | |
1002 | && (REGNO_REG_CLASS (lastr) == NO_REGS || ! FIXED_REGNO_P (lastr)) | |
1003 | && new >= FIRST_PSEUDO_REGISTER) | |
1004 | lastr = reg_prev_eqv[lastr]; | |
1005 | reg_next_eqv[new] = reg_next_eqv[lastr]; | |
1006 | if (reg_next_eqv[lastr] >= 0) | |
1007 | reg_prev_eqv[reg_next_eqv[lastr]] = new; | |
1008 | else | |
1009 | qty_last_reg[q] = new; | |
1010 | reg_next_eqv[lastr] = new; | |
1011 | reg_prev_eqv[new] = lastr; | |
1012 | } | |
1013 | } | |
1014 | ||
1015 | /* Remove REG from its equivalence class. */ | |
1016 | ||
1017 | static void | |
1018 | delete_reg_equiv (reg) | |
1019 | register int reg; | |
1020 | { | |
30f72379 | 1021 | register int q = REG_QTY (reg); |
a4e262bc | 1022 | register int p, n; |
7afe21cc | 1023 | |
a4e262bc | 1024 | /* If invalid, do nothing. */ |
7afe21cc RK |
1025 | if (q == reg) |
1026 | return; | |
1027 | ||
a4e262bc RK |
1028 | p = reg_prev_eqv[reg]; |
1029 | n = reg_next_eqv[reg]; | |
1030 | ||
7afe21cc RK |
1031 | if (n != -1) |
1032 | reg_prev_eqv[n] = p; | |
1033 | else | |
1034 | qty_last_reg[q] = p; | |
1035 | if (p != -1) | |
1036 | reg_next_eqv[p] = n; | |
1037 | else | |
1038 | qty_first_reg[q] = n; | |
1039 | ||
30f72379 | 1040 | REG_QTY (reg) = reg; |
7afe21cc RK |
1041 | } |
1042 | ||
1043 | /* Remove any invalid expressions from the hash table | |
1044 | that refer to any of the registers contained in expression X. | |
1045 | ||
1046 | Make sure that newly inserted references to those registers | |
1047 | as subexpressions will be considered valid. | |
1048 | ||
1049 | mention_regs is not called when a register itself | |
1050 | is being stored in the table. | |
1051 | ||
1052 | Return 1 if we have done something that may have changed the hash code | |
1053 | of X. */ | |
1054 | ||
1055 | static int | |
1056 | mention_regs (x) | |
1057 | rtx x; | |
1058 | { | |
1059 | register enum rtx_code code; | |
1060 | register int i, j; | |
6f7d635c | 1061 | register const char *fmt; |
7afe21cc RK |
1062 | register int changed = 0; |
1063 | ||
1064 | if (x == 0) | |
e5f6a288 | 1065 | return 0; |
7afe21cc RK |
1066 | |
1067 | code = GET_CODE (x); | |
1068 | if (code == REG) | |
1069 | { | |
1070 | register int regno = REGNO (x); | |
1071 | register int endregno | |
1072 | = regno + (regno >= FIRST_PSEUDO_REGISTER ? 1 | |
1073 | : HARD_REGNO_NREGS (regno, GET_MODE (x))); | |
1074 | int i; | |
1075 | ||
1076 | for (i = regno; i < endregno; i++) | |
1077 | { | |
30f72379 | 1078 | if (REG_IN_TABLE (i) >= 0 && REG_IN_TABLE (i) != REG_TICK (i)) |
7afe21cc RK |
1079 | remove_invalid_refs (i); |
1080 | ||
30f72379 | 1081 | REG_IN_TABLE (i) = REG_TICK (i); |
7afe21cc RK |
1082 | } |
1083 | ||
1084 | return 0; | |
1085 | } | |
1086 | ||
34c73909 R |
1087 | /* If this is a SUBREG, we don't want to discard other SUBREGs of the same |
1088 | pseudo if they don't use overlapping words. We handle only pseudos | |
1089 | here for simplicity. */ | |
1090 | if (code == SUBREG && GET_CODE (SUBREG_REG (x)) == REG | |
1091 | && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER) | |
1092 | { | |
1093 | int i = REGNO (SUBREG_REG (x)); | |
1094 | ||
30f72379 | 1095 | if (REG_IN_TABLE (i) >= 0 && REG_IN_TABLE (i) != REG_TICK (i)) |
34c73909 R |
1096 | { |
1097 | /* If reg_tick has been incremented more than once since | |
1098 | reg_in_table was last set, that means that the entire | |
1099 | register has been set before, so discard anything memorized | |
1100 | for the entrire register, including all SUBREG expressions. */ | |
30f72379 | 1101 | if (REG_IN_TABLE (i) != REG_TICK (i) - 1) |
34c73909 R |
1102 | remove_invalid_refs (i); |
1103 | else | |
1104 | remove_invalid_subreg_refs (i, SUBREG_WORD (x), GET_MODE (x)); | |
1105 | } | |
1106 | ||
30f72379 | 1107 | REG_IN_TABLE (i) = REG_TICK (i); |
34c73909 R |
1108 | return 0; |
1109 | } | |
1110 | ||
7afe21cc RK |
1111 | /* If X is a comparison or a COMPARE and either operand is a register |
1112 | that does not have a quantity, give it one. This is so that a later | |
1113 | call to record_jump_equiv won't cause X to be assigned a different | |
1114 | hash code and not found in the table after that call. | |
1115 | ||
1116 | It is not necessary to do this here, since rehash_using_reg can | |
1117 | fix up the table later, but doing this here eliminates the need to | |
1118 | call that expensive function in the most common case where the only | |
1119 | use of the register is in the comparison. */ | |
1120 | ||
1121 | if (code == COMPARE || GET_RTX_CLASS (code) == '<') | |
1122 | { | |
1123 | if (GET_CODE (XEXP (x, 0)) == REG | |
1124 | && ! REGNO_QTY_VALID_P (REGNO (XEXP (x, 0)))) | |
906c4e36 | 1125 | if (insert_regs (XEXP (x, 0), NULL_PTR, 0)) |
7afe21cc RK |
1126 | { |
1127 | rehash_using_reg (XEXP (x, 0)); | |
1128 | changed = 1; | |
1129 | } | |
1130 | ||
1131 | if (GET_CODE (XEXP (x, 1)) == REG | |
1132 | && ! REGNO_QTY_VALID_P (REGNO (XEXP (x, 1)))) | |
906c4e36 | 1133 | if (insert_regs (XEXP (x, 1), NULL_PTR, 0)) |
7afe21cc RK |
1134 | { |
1135 | rehash_using_reg (XEXP (x, 1)); | |
1136 | changed = 1; | |
1137 | } | |
1138 | } | |
1139 | ||
1140 | fmt = GET_RTX_FORMAT (code); | |
1141 | for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) | |
1142 | if (fmt[i] == 'e') | |
1143 | changed |= mention_regs (XEXP (x, i)); | |
1144 | else if (fmt[i] == 'E') | |
1145 | for (j = 0; j < XVECLEN (x, i); j++) | |
1146 | changed |= mention_regs (XVECEXP (x, i, j)); | |
1147 | ||
1148 | return changed; | |
1149 | } | |
1150 | ||
1151 | /* Update the register quantities for inserting X into the hash table | |
1152 | with a value equivalent to CLASSP. | |
1153 | (If the class does not contain a REG, it is irrelevant.) | |
1154 | If MODIFIED is nonzero, X is a destination; it is being modified. | |
1155 | Note that delete_reg_equiv should be called on a register | |
1156 | before insert_regs is done on that register with MODIFIED != 0. | |
1157 | ||
1158 | Nonzero value means that elements of reg_qty have changed | |
1159 | so X's hash code may be different. */ | |
1160 | ||
1161 | static int | |
1162 | insert_regs (x, classp, modified) | |
1163 | rtx x; | |
1164 | struct table_elt *classp; | |
1165 | int modified; | |
1166 | { | |
1167 | if (GET_CODE (x) == REG) | |
1168 | { | |
1169 | register int regno = REGNO (x); | |
1170 | ||
1ff0c00d RK |
1171 | /* If REGNO is in the equivalence table already but is of the |
1172 | wrong mode for that equivalence, don't do anything here. */ | |
1173 | ||
1174 | if (REGNO_QTY_VALID_P (regno) | |
30f72379 | 1175 | && qty_mode[REG_QTY (regno)] != GET_MODE (x)) |
1ff0c00d RK |
1176 | return 0; |
1177 | ||
1178 | if (modified || ! REGNO_QTY_VALID_P (regno)) | |
7afe21cc RK |
1179 | { |
1180 | if (classp) | |
1181 | for (classp = classp->first_same_value; | |
1182 | classp != 0; | |
1183 | classp = classp->next_same_value) | |
1184 | if (GET_CODE (classp->exp) == REG | |
1185 | && GET_MODE (classp->exp) == GET_MODE (x)) | |
1186 | { | |
1187 | make_regs_eqv (regno, REGNO (classp->exp)); | |
1188 | return 1; | |
1189 | } | |
1190 | ||
1191 | make_new_qty (regno); | |
30f72379 | 1192 | qty_mode[REG_QTY (regno)] = GET_MODE (x); |
7afe21cc RK |
1193 | return 1; |
1194 | } | |
cdf4112f TG |
1195 | |
1196 | return 0; | |
7afe21cc | 1197 | } |
c610adec RK |
1198 | |
1199 | /* If X is a SUBREG, we will likely be inserting the inner register in the | |
1200 | table. If that register doesn't have an assigned quantity number at | |
1201 | this point but does later, the insertion that we will be doing now will | |
1202 | not be accessible because its hash code will have changed. So assign | |
1203 | a quantity number now. */ | |
1204 | ||
1205 | else if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == REG | |
1206 | && ! REGNO_QTY_VALID_P (REGNO (SUBREG_REG (x)))) | |
1207 | { | |
34c73909 R |
1208 | int regno = REGNO (SUBREG_REG (x)); |
1209 | ||
906c4e36 | 1210 | insert_regs (SUBREG_REG (x), NULL_PTR, 0); |
34c73909 R |
1211 | /* Mention_regs checks if REG_TICK is exactly one larger than |
1212 | REG_IN_TABLE to find out if there was only a single preceding | |
1213 | invalidation - for the SUBREG - or another one, which would be | |
1214 | for the full register. Since we don't invalidate the SUBREG | |
1215 | here first, we might have to bump up REG_TICK so that mention_regs | |
1216 | will do the right thing. */ | |
30f72379 MM |
1217 | if (REG_IN_TABLE (regno) >= 0 |
1218 | && REG_TICK (regno) == REG_IN_TABLE (regno) + 1) | |
1219 | REG_TICK (regno)++; | |
34c73909 | 1220 | mention_regs (x); |
c610adec RK |
1221 | return 1; |
1222 | } | |
7afe21cc RK |
1223 | else |
1224 | return mention_regs (x); | |
1225 | } | |
1226 | \f | |
1227 | /* Look in or update the hash table. */ | |
1228 | ||
1229 | /* Put the element ELT on the list of free elements. */ | |
1230 | ||
1231 | static void | |
1232 | free_element (elt) | |
1233 | struct table_elt *elt; | |
1234 | { | |
1235 | elt->next_same_hash = free_element_chain; | |
1236 | free_element_chain = elt; | |
1237 | } | |
1238 | ||
1239 | /* Return an element that is free for use. */ | |
1240 | ||
1241 | static struct table_elt * | |
1242 | get_element () | |
1243 | { | |
1244 | struct table_elt *elt = free_element_chain; | |
1245 | if (elt) | |
1246 | { | |
1247 | free_element_chain = elt->next_same_hash; | |
1248 | return elt; | |
1249 | } | |
1250 | n_elements_made++; | |
1251 | return (struct table_elt *) oballoc (sizeof (struct table_elt)); | |
1252 | } | |
1253 | ||
1254 | /* Remove table element ELT from use in the table. | |
1255 | HASH is its hash code, made using the HASH macro. | |
1256 | It's an argument because often that is known in advance | |
1257 | and we save much time not recomputing it. */ | |
1258 | ||
1259 | static void | |
1260 | remove_from_table (elt, hash) | |
1261 | register struct table_elt *elt; | |
2197a88a | 1262 | unsigned hash; |
7afe21cc RK |
1263 | { |
1264 | if (elt == 0) | |
1265 | return; | |
1266 | ||
1267 | /* Mark this element as removed. See cse_insn. */ | |
1268 | elt->first_same_value = 0; | |
1269 | ||
1270 | /* Remove the table element from its equivalence class. */ | |
1271 | ||
1272 | { | |
1273 | register struct table_elt *prev = elt->prev_same_value; | |
1274 | register struct table_elt *next = elt->next_same_value; | |
1275 | ||
1276 | if (next) next->prev_same_value = prev; | |
1277 | ||
1278 | if (prev) | |
1279 | prev->next_same_value = next; | |
1280 | else | |
1281 | { | |
1282 | register struct table_elt *newfirst = next; | |
1283 | while (next) | |
1284 | { | |
1285 | next->first_same_value = newfirst; | |
1286 | next = next->next_same_value; | |
1287 | } | |
1288 | } | |
1289 | } | |
1290 | ||
1291 | /* Remove the table element from its hash bucket. */ | |
1292 | ||
1293 | { | |
1294 | register struct table_elt *prev = elt->prev_same_hash; | |
1295 | register struct table_elt *next = elt->next_same_hash; | |
1296 | ||
1297 | if (next) next->prev_same_hash = prev; | |
1298 | ||
1299 | if (prev) | |
1300 | prev->next_same_hash = next; | |
1301 | else if (table[hash] == elt) | |
1302 | table[hash] = next; | |
1303 | else | |
1304 | { | |
1305 | /* This entry is not in the proper hash bucket. This can happen | |
1306 | when two classes were merged by `merge_equiv_classes'. Search | |
1307 | for the hash bucket that it heads. This happens only very | |
1308 | rarely, so the cost is acceptable. */ | |
1309 | for (hash = 0; hash < NBUCKETS; hash++) | |
1310 | if (table[hash] == elt) | |
1311 | table[hash] = next; | |
1312 | } | |
1313 | } | |
1314 | ||
1315 | /* Remove the table element from its related-value circular chain. */ | |
1316 | ||
1317 | if (elt->related_value != 0 && elt->related_value != elt) | |
1318 | { | |
1319 | register struct table_elt *p = elt->related_value; | |
1320 | while (p->related_value != elt) | |
1321 | p = p->related_value; | |
1322 | p->related_value = elt->related_value; | |
1323 | if (p->related_value == p) | |
1324 | p->related_value = 0; | |
1325 | } | |
1326 | ||
1327 | free_element (elt); | |
1328 | } | |
1329 | ||
1330 | /* Look up X in the hash table and return its table element, | |
1331 | or 0 if X is not in the table. | |
1332 | ||
1333 | MODE is the machine-mode of X, or if X is an integer constant | |
1334 | with VOIDmode then MODE is the mode with which X will be used. | |
1335 | ||
1336 | Here we are satisfied to find an expression whose tree structure | |
1337 | looks like X. */ | |
1338 | ||
1339 | static struct table_elt * | |
1340 | lookup (x, hash, mode) | |
1341 | rtx x; | |
2197a88a | 1342 | unsigned hash; |
7afe21cc RK |
1343 | enum machine_mode mode; |
1344 | { | |
1345 | register struct table_elt *p; | |
1346 | ||
1347 | for (p = table[hash]; p; p = p->next_same_hash) | |
1348 | if (mode == p->mode && ((x == p->exp && GET_CODE (x) == REG) | |
1349 | || exp_equiv_p (x, p->exp, GET_CODE (x) != REG, 0))) | |
1350 | return p; | |
1351 | ||
1352 | return 0; | |
1353 | } | |
1354 | ||
1355 | /* Like `lookup' but don't care whether the table element uses invalid regs. | |
1356 | Also ignore discrepancies in the machine mode of a register. */ | |
1357 | ||
1358 | static struct table_elt * | |
1359 | lookup_for_remove (x, hash, mode) | |
1360 | rtx x; | |
2197a88a | 1361 | unsigned hash; |
7afe21cc RK |
1362 | enum machine_mode mode; |
1363 | { | |
1364 | register struct table_elt *p; | |
1365 | ||
1366 | if (GET_CODE (x) == REG) | |
1367 | { | |
1368 | int regno = REGNO (x); | |
1369 | /* Don't check the machine mode when comparing registers; | |
1370 | invalidating (REG:SI 0) also invalidates (REG:DF 0). */ | |
1371 | for (p = table[hash]; p; p = p->next_same_hash) | |
1372 | if (GET_CODE (p->exp) == REG | |
1373 | && REGNO (p->exp) == regno) | |
1374 | return p; | |
1375 | } | |
1376 | else | |
1377 | { | |
1378 | for (p = table[hash]; p; p = p->next_same_hash) | |
1379 | if (mode == p->mode && (x == p->exp || exp_equiv_p (x, p->exp, 0, 0))) | |
1380 | return p; | |
1381 | } | |
1382 | ||
1383 | return 0; | |
1384 | } | |
1385 | ||
1386 | /* Look for an expression equivalent to X and with code CODE. | |
1387 | If one is found, return that expression. */ | |
1388 | ||
1389 | static rtx | |
1390 | lookup_as_function (x, code) | |
1391 | rtx x; | |
1392 | enum rtx_code code; | |
1393 | { | |
1394 | register struct table_elt *p = lookup (x, safe_hash (x, VOIDmode) % NBUCKETS, | |
1395 | GET_MODE (x)); | |
34c73909 R |
1396 | /* If we are looking for a CONST_INT, the mode doesn't really matter, as |
1397 | long as we are narrowing. So if we looked in vain for a mode narrower | |
1398 | than word_mode before, look for word_mode now. */ | |
1399 | if (p == 0 && code == CONST_INT | |
1400 | && GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (word_mode)) | |
1401 | { | |
1402 | x = copy_rtx (x); | |
1403 | PUT_MODE (x, word_mode); | |
1404 | p = lookup (x, safe_hash (x, VOIDmode) % NBUCKETS, word_mode); | |
1405 | } | |
1406 | ||
7afe21cc RK |
1407 | if (p == 0) |
1408 | return 0; | |
1409 | ||
1410 | for (p = p->first_same_value; p; p = p->next_same_value) | |
1411 | { | |
1412 | if (GET_CODE (p->exp) == code | |
1413 | /* Make sure this is a valid entry in the table. */ | |
1414 | && exp_equiv_p (p->exp, p->exp, 1, 0)) | |
1415 | return p->exp; | |
1416 | } | |
1417 | ||
1418 | return 0; | |
1419 | } | |
1420 | ||
1421 | /* Insert X in the hash table, assuming HASH is its hash code | |
1422 | and CLASSP is an element of the class it should go in | |
1423 | (or 0 if a new class should be made). | |
1424 | It is inserted at the proper position to keep the class in | |
1425 | the order cheapest first. | |
1426 | ||
1427 | MODE is the machine-mode of X, or if X is an integer constant | |
1428 | with VOIDmode then MODE is the mode with which X will be used. | |
1429 | ||
1430 | For elements of equal cheapness, the most recent one | |
1431 | goes in front, except that the first element in the list | |
1432 | remains first unless a cheaper element is added. The order of | |
1433 | pseudo-registers does not matter, as canon_reg will be called to | |
830a38ee | 1434 | find the cheapest when a register is retrieved from the table. |
7afe21cc RK |
1435 | |
1436 | The in_memory field in the hash table element is set to 0. | |
1437 | The caller must set it nonzero if appropriate. | |
1438 | ||
1439 | You should call insert_regs (X, CLASSP, MODIFY) before calling here, | |
1440 | and if insert_regs returns a nonzero value | |
1441 | you must then recompute its hash code before calling here. | |
1442 | ||
1443 | If necessary, update table showing constant values of quantities. */ | |
1444 | ||
1445 | #define CHEAPER(X,Y) ((X)->cost < (Y)->cost) | |
1446 | ||
1447 | static struct table_elt * | |
1448 | insert (x, classp, hash, mode) | |
1449 | register rtx x; | |
1450 | register struct table_elt *classp; | |
2197a88a | 1451 | unsigned hash; |
7afe21cc RK |
1452 | enum machine_mode mode; |
1453 | { | |
1454 | register struct table_elt *elt; | |
1455 | ||
1456 | /* If X is a register and we haven't made a quantity for it, | |
1457 | something is wrong. */ | |
1458 | if (GET_CODE (x) == REG && ! REGNO_QTY_VALID_P (REGNO (x))) | |
1459 | abort (); | |
1460 | ||
1461 | /* If X is a hard register, show it is being put in the table. */ | |
1462 | if (GET_CODE (x) == REG && REGNO (x) < FIRST_PSEUDO_REGISTER) | |
1463 | { | |
1464 | int regno = REGNO (x); | |
1465 | int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x)); | |
1466 | int i; | |
1467 | ||
1468 | for (i = regno; i < endregno; i++) | |
1469 | SET_HARD_REG_BIT (hard_regs_in_table, i); | |
1470 | } | |
1471 | ||
a5dfb4ee | 1472 | /* If X is a label, show we recorded it. */ |
970c9ace RK |
1473 | if (GET_CODE (x) == LABEL_REF |
1474 | || (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS | |
1475 | && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF)) | |
a5dfb4ee | 1476 | recorded_label_ref = 1; |
7afe21cc RK |
1477 | |
1478 | /* Put an element for X into the right hash bucket. */ | |
1479 | ||
1480 | elt = get_element (); | |
1481 | elt->exp = x; | |
1482 | elt->cost = COST (x); | |
1483 | elt->next_same_value = 0; | |
1484 | elt->prev_same_value = 0; | |
1485 | elt->next_same_hash = table[hash]; | |
1486 | elt->prev_same_hash = 0; | |
1487 | elt->related_value = 0; | |
1488 | elt->in_memory = 0; | |
1489 | elt->mode = mode; | |
1490 | elt->is_const = (CONSTANT_P (x) | |
1491 | /* GNU C++ takes advantage of this for `this' | |
1492 | (and other const values). */ | |
1493 | || (RTX_UNCHANGING_P (x) | |
1494 | && GET_CODE (x) == REG | |
1495 | && REGNO (x) >= FIRST_PSEUDO_REGISTER) | |
1496 | || FIXED_BASE_PLUS_P (x)); | |
1497 | ||
1498 | if (table[hash]) | |
1499 | table[hash]->prev_same_hash = elt; | |
1500 | table[hash] = elt; | |
1501 | ||
1502 | /* Put it into the proper value-class. */ | |
1503 | if (classp) | |
1504 | { | |
1505 | classp = classp->first_same_value; | |
1506 | if (CHEAPER (elt, classp)) | |
1507 | /* Insert at the head of the class */ | |
1508 | { | |
1509 | register struct table_elt *p; | |
1510 | elt->next_same_value = classp; | |
1511 | classp->prev_same_value = elt; | |
1512 | elt->first_same_value = elt; | |
1513 | ||
1514 | for (p = classp; p; p = p->next_same_value) | |
1515 | p->first_same_value = elt; | |
1516 | } | |
1517 | else | |
1518 | { | |
1519 | /* Insert not at head of the class. */ | |
1520 | /* Put it after the last element cheaper than X. */ | |
1521 | register struct table_elt *p, *next; | |
1522 | for (p = classp; (next = p->next_same_value) && CHEAPER (next, elt); | |
1523 | p = next); | |
1524 | /* Put it after P and before NEXT. */ | |
1525 | elt->next_same_value = next; | |
1526 | if (next) | |
1527 | next->prev_same_value = elt; | |
1528 | elt->prev_same_value = p; | |
1529 | p->next_same_value = elt; | |
1530 | elt->first_same_value = classp; | |
1531 | } | |
1532 | } | |
1533 | else | |
1534 | elt->first_same_value = elt; | |
1535 | ||
1536 | /* If this is a constant being set equivalent to a register or a register | |
1537 | being set equivalent to a constant, note the constant equivalence. | |
1538 | ||
1539 | If this is a constant, it cannot be equivalent to a different constant, | |
1540 | and a constant is the only thing that can be cheaper than a register. So | |
1541 | we know the register is the head of the class (before the constant was | |
1542 | inserted). | |
1543 | ||
1544 | If this is a register that is not already known equivalent to a | |
1545 | constant, we must check the entire class. | |
1546 | ||
1547 | If this is a register that is already known equivalent to an insn, | |
1548 | update `qty_const_insn' to show that `this_insn' is the latest | |
1549 | insn making that quantity equivalent to the constant. */ | |
1550 | ||
f353588a RK |
1551 | if (elt->is_const && classp && GET_CODE (classp->exp) == REG |
1552 | && GET_CODE (x) != REG) | |
7afe21cc | 1553 | { |
30f72379 MM |
1554 | qty_const[REG_QTY (REGNO (classp->exp))] |
1555 | = gen_lowpart_if_possible (qty_mode[REG_QTY (REGNO (classp->exp))], x); | |
1556 | qty_const_insn[REG_QTY (REGNO (classp->exp))] = this_insn; | |
7afe21cc RK |
1557 | } |
1558 | ||
30f72379 | 1559 | else if (GET_CODE (x) == REG && classp && ! qty_const[REG_QTY (REGNO (x))] |
f353588a | 1560 | && ! elt->is_const) |
7afe21cc RK |
1561 | { |
1562 | register struct table_elt *p; | |
1563 | ||
1564 | for (p = classp; p != 0; p = p->next_same_value) | |
1565 | { | |
f353588a | 1566 | if (p->is_const && GET_CODE (p->exp) != REG) |
7afe21cc | 1567 | { |
30f72379 | 1568 | qty_const[REG_QTY (REGNO (x))] |
7afe21cc | 1569 | = gen_lowpart_if_possible (GET_MODE (x), p->exp); |
30f72379 | 1570 | qty_const_insn[REG_QTY (REGNO (x))] = this_insn; |
7afe21cc RK |
1571 | break; |
1572 | } | |
1573 | } | |
1574 | } | |
1575 | ||
30f72379 MM |
1576 | else if (GET_CODE (x) == REG && qty_const[REG_QTY (REGNO (x))] |
1577 | && GET_MODE (x) == qty_mode[REG_QTY (REGNO (x))]) | |
1578 | qty_const_insn[REG_QTY (REGNO (x))] = this_insn; | |
7afe21cc RK |
1579 | |
1580 | /* If this is a constant with symbolic value, | |
1581 | and it has a term with an explicit integer value, | |
1582 | link it up with related expressions. */ | |
1583 | if (GET_CODE (x) == CONST) | |
1584 | { | |
1585 | rtx subexp = get_related_value (x); | |
2197a88a | 1586 | unsigned subhash; |
7afe21cc RK |
1587 | struct table_elt *subelt, *subelt_prev; |
1588 | ||
1589 | if (subexp != 0) | |
1590 | { | |
1591 | /* Get the integer-free subexpression in the hash table. */ | |
1592 | subhash = safe_hash (subexp, mode) % NBUCKETS; | |
1593 | subelt = lookup (subexp, subhash, mode); | |
1594 | if (subelt == 0) | |
906c4e36 | 1595 | subelt = insert (subexp, NULL_PTR, subhash, mode); |
7afe21cc RK |
1596 | /* Initialize SUBELT's circular chain if it has none. */ |
1597 | if (subelt->related_value == 0) | |
1598 | subelt->related_value = subelt; | |
1599 | /* Find the element in the circular chain that precedes SUBELT. */ | |
1600 | subelt_prev = subelt; | |
1601 | while (subelt_prev->related_value != subelt) | |
1602 | subelt_prev = subelt_prev->related_value; | |
1603 | /* Put new ELT into SUBELT's circular chain just before SUBELT. | |
1604 | This way the element that follows SUBELT is the oldest one. */ | |
1605 | elt->related_value = subelt_prev->related_value; | |
1606 | subelt_prev->related_value = elt; | |
1607 | } | |
1608 | } | |
1609 | ||
1610 | return elt; | |
1611 | } | |
1612 | \f | |
1613 | /* Given two equivalence classes, CLASS1 and CLASS2, put all the entries from | |
1614 | CLASS2 into CLASS1. This is done when we have reached an insn which makes | |
1615 | the two classes equivalent. | |
1616 | ||
1617 | CLASS1 will be the surviving class; CLASS2 should not be used after this | |
1618 | call. | |
1619 | ||
1620 | Any invalid entries in CLASS2 will not be copied. */ | |
1621 | ||
1622 | static void | |
1623 | merge_equiv_classes (class1, class2) | |
1624 | struct table_elt *class1, *class2; | |
1625 | { | |
1626 | struct table_elt *elt, *next, *new; | |
1627 | ||
1628 | /* Ensure we start with the head of the classes. */ | |
1629 | class1 = class1->first_same_value; | |
1630 | class2 = class2->first_same_value; | |
1631 | ||
1632 | /* If they were already equal, forget it. */ | |
1633 | if (class1 == class2) | |
1634 | return; | |
1635 | ||
1636 | for (elt = class2; elt; elt = next) | |
1637 | { | |
2197a88a | 1638 | unsigned hash; |
7afe21cc RK |
1639 | rtx exp = elt->exp; |
1640 | enum machine_mode mode = elt->mode; | |
1641 | ||
1642 | next = elt->next_same_value; | |
1643 | ||
1644 | /* Remove old entry, make a new one in CLASS1's class. | |
1645 | Don't do this for invalid entries as we cannot find their | |
0f41302f | 1646 | hash code (it also isn't necessary). */ |
7afe21cc RK |
1647 | if (GET_CODE (exp) == REG || exp_equiv_p (exp, exp, 1, 0)) |
1648 | { | |
1649 | hash_arg_in_memory = 0; | |
1650 | hash_arg_in_struct = 0; | |
1651 | hash = HASH (exp, mode); | |
1652 | ||
1653 | if (GET_CODE (exp) == REG) | |
1654 | delete_reg_equiv (REGNO (exp)); | |
1655 | ||
1656 | remove_from_table (elt, hash); | |
1657 | ||
1658 | if (insert_regs (exp, class1, 0)) | |
8ae2b8f6 JW |
1659 | { |
1660 | rehash_using_reg (exp); | |
1661 | hash = HASH (exp, mode); | |
1662 | } | |
7afe21cc RK |
1663 | new = insert (exp, class1, hash, mode); |
1664 | new->in_memory = hash_arg_in_memory; | |
1665 | new->in_struct = hash_arg_in_struct; | |
1666 | } | |
1667 | } | |
1668 | } | |
1669 | \f | |
01e752d3 JL |
1670 | |
1671 | /* Flush the entire hash table. */ | |
1672 | ||
1673 | static void | |
1674 | flush_hash_table () | |
1675 | { | |
1676 | int i; | |
1677 | struct table_elt *p; | |
1678 | ||
1679 | for (i = 0; i < NBUCKETS; i++) | |
1680 | for (p = table[i]; p; p = table[i]) | |
1681 | { | |
1682 | /* Note that invalidate can remove elements | |
1683 | after P in the current hash chain. */ | |
1684 | if (GET_CODE (p->exp) == REG) | |
1685 | invalidate (p->exp, p->mode); | |
1686 | else | |
1687 | remove_from_table (p, i); | |
1688 | } | |
1689 | } | |
1690 | ||
1691 | ||
7afe21cc RK |
1692 | /* Remove from the hash table, or mark as invalid, |
1693 | all expressions whose values could be altered by storing in X. | |
1694 | X is a register, a subreg, or a memory reference with nonvarying address | |
1695 | (because, when a memory reference with a varying address is stored in, | |
1696 | all memory references are removed by invalidate_memory | |
1697 | so specific invalidation is superfluous). | |
bb4034b3 JW |
1698 | FULL_MODE, if not VOIDmode, indicates that this much should be invalidated |
1699 | instead of just the amount indicated by the mode of X. This is only used | |
1700 | for bitfield stores into memory. | |
7afe21cc RK |
1701 | |
1702 | A nonvarying address may be just a register or just | |
1703 | a symbol reference, or it may be either of those plus | |
1704 | a numeric offset. */ | |
1705 | ||
1706 | static void | |
bb4034b3 | 1707 | invalidate (x, full_mode) |
7afe21cc | 1708 | rtx x; |
bb4034b3 | 1709 | enum machine_mode full_mode; |
7afe21cc RK |
1710 | { |
1711 | register int i; | |
1712 | register struct table_elt *p; | |
7afe21cc RK |
1713 | |
1714 | /* If X is a register, dependencies on its contents | |
1715 | are recorded through the qty number mechanism. | |
1716 | Just change the qty number of the register, | |
1717 | mark it as invalid for expressions that refer to it, | |
1718 | and remove it itself. */ | |
1719 | ||
1720 | if (GET_CODE (x) == REG) | |
1721 | { | |
1722 | register int regno = REGNO (x); | |
2197a88a | 1723 | register unsigned hash = HASH (x, GET_MODE (x)); |
7afe21cc RK |
1724 | |
1725 | /* Remove REGNO from any quantity list it might be on and indicate | |
9ec36da5 | 1726 | that its value might have changed. If it is a pseudo, remove its |
7afe21cc RK |
1727 | entry from the hash table. |
1728 | ||
1729 | For a hard register, we do the first two actions above for any | |
1730 | additional hard registers corresponding to X. Then, if any of these | |
1731 | registers are in the table, we must remove any REG entries that | |
1732 | overlap these registers. */ | |
1733 | ||
1734 | delete_reg_equiv (regno); | |
30f72379 | 1735 | REG_TICK (regno)++; |
7afe21cc RK |
1736 | |
1737 | if (regno >= FIRST_PSEUDO_REGISTER) | |
85e4d983 RK |
1738 | { |
1739 | /* Because a register can be referenced in more than one mode, | |
1740 | we might have to remove more than one table entry. */ | |
1741 | ||
1742 | struct table_elt *elt; | |
1743 | ||
2d8b0f3a | 1744 | while ((elt = lookup_for_remove (x, hash, GET_MODE (x)))) |
85e4d983 RK |
1745 | remove_from_table (elt, hash); |
1746 | } | |
7afe21cc RK |
1747 | else |
1748 | { | |
54b1de55 RK |
1749 | HOST_WIDE_INT in_table |
1750 | = TEST_HARD_REG_BIT (hard_regs_in_table, regno); | |
7afe21cc RK |
1751 | int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x)); |
1752 | int tregno, tendregno; | |
1753 | register struct table_elt *p, *next; | |
1754 | ||
1755 | CLEAR_HARD_REG_BIT (hard_regs_in_table, regno); | |
1756 | ||
1757 | for (i = regno + 1; i < endregno; i++) | |
1758 | { | |
1759 | in_table |= TEST_HARD_REG_BIT (hard_regs_in_table, i); | |
1760 | CLEAR_HARD_REG_BIT (hard_regs_in_table, i); | |
1761 | delete_reg_equiv (i); | |
30f72379 | 1762 | REG_TICK (i)++; |
7afe21cc RK |
1763 | } |
1764 | ||
1765 | if (in_table) | |
1766 | for (hash = 0; hash < NBUCKETS; hash++) | |
1767 | for (p = table[hash]; p; p = next) | |
1768 | { | |
1769 | next = p->next_same_hash; | |
1770 | ||
1771 | if (GET_CODE (p->exp) != REG | |
1772 | || REGNO (p->exp) >= FIRST_PSEUDO_REGISTER) | |
1773 | continue; | |
1774 | ||
1775 | tregno = REGNO (p->exp); | |
1776 | tendregno | |
1777 | = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (p->exp)); | |
1778 | if (tendregno > regno && tregno < endregno) | |
925be47c | 1779 | remove_from_table (p, hash); |
7afe21cc RK |
1780 | } |
1781 | } | |
1782 | ||
1783 | return; | |
1784 | } | |
1785 | ||
1786 | if (GET_CODE (x) == SUBREG) | |
1787 | { | |
1788 | if (GET_CODE (SUBREG_REG (x)) != REG) | |
1789 | abort (); | |
bb4034b3 | 1790 | invalidate (SUBREG_REG (x), VOIDmode); |
7afe21cc RK |
1791 | return; |
1792 | } | |
1793 | ||
aac5cc16 RH |
1794 | /* If X is a parallel, invalidate all of its elements. */ |
1795 | ||
1796 | if (GET_CODE (x) == PARALLEL) | |
1797 | { | |
1798 | for (i = XVECLEN (x, 0) - 1; i >= 0 ; --i) | |
1799 | invalidate (XVECEXP (x, 0, i), VOIDmode); | |
1800 | return; | |
1801 | } | |
1802 | ||
1803 | /* If X is an expr_list, this is part of a disjoint return value; | |
1804 | extract the location in question ignoring the offset. */ | |
1805 | ||
1806 | if (GET_CODE (x) == EXPR_LIST) | |
1807 | { | |
1808 | invalidate (XEXP (x, 0), VOIDmode); | |
1809 | return; | |
1810 | } | |
1811 | ||
7afe21cc RK |
1812 | /* X is not a register; it must be a memory reference with |
1813 | a nonvarying address. Remove all hash table elements | |
1814 | that refer to overlapping pieces of memory. */ | |
1815 | ||
1816 | if (GET_CODE (x) != MEM) | |
1817 | abort (); | |
7afe21cc | 1818 | |
bb4034b3 JW |
1819 | if (full_mode == VOIDmode) |
1820 | full_mode = GET_MODE (x); | |
1821 | ||
7afe21cc RK |
1822 | for (i = 0; i < NBUCKETS; i++) |
1823 | { | |
1824 | register struct table_elt *next; | |
1825 | for (p = table[i]; p; p = next) | |
1826 | { | |
1827 | next = p->next_same_hash; | |
9ae8ffe7 JL |
1828 | /* Invalidate ASM_OPERANDS which reference memory (this is easier |
1829 | than checking all the aliases). */ | |
1830 | if (p->in_memory | |
1831 | && (GET_CODE (p->exp) != MEM | |
1832 | || true_dependence (x, full_mode, p->exp, cse_rtx_varies_p))) | |
7afe21cc RK |
1833 | remove_from_table (p, i); |
1834 | } | |
1835 | } | |
1836 | } | |
1837 | ||
1838 | /* Remove all expressions that refer to register REGNO, | |
1839 | since they are already invalid, and we are about to | |
1840 | mark that register valid again and don't want the old | |
1841 | expressions to reappear as valid. */ | |
1842 | ||
1843 | static void | |
1844 | remove_invalid_refs (regno) | |
1845 | int regno; | |
1846 | { | |
1847 | register int i; | |
1848 | register struct table_elt *p, *next; | |
1849 | ||
1850 | for (i = 0; i < NBUCKETS; i++) | |
1851 | for (p = table[i]; p; p = next) | |
1852 | { | |
1853 | next = p->next_same_hash; | |
1854 | if (GET_CODE (p->exp) != REG | |
906c4e36 | 1855 | && refers_to_regno_p (regno, regno + 1, p->exp, NULL_PTR)) |
7afe21cc RK |
1856 | remove_from_table (p, i); |
1857 | } | |
1858 | } | |
34c73909 R |
1859 | |
1860 | /* Likewise for a subreg with subreg_reg WORD and mode MODE. */ | |
1861 | static void | |
1862 | remove_invalid_subreg_refs (regno, word, mode) | |
1863 | int regno; | |
1864 | int word; | |
1865 | enum machine_mode mode; | |
1866 | { | |
1867 | register int i; | |
1868 | register struct table_elt *p, *next; | |
1869 | int end = word + (GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD; | |
1870 | ||
1871 | for (i = 0; i < NBUCKETS; i++) | |
1872 | for (p = table[i]; p; p = next) | |
1873 | { | |
1874 | rtx exp; | |
1875 | next = p->next_same_hash; | |
1876 | ||
1877 | exp = p->exp; | |
1878 | if (GET_CODE (p->exp) != REG | |
1879 | && (GET_CODE (exp) != SUBREG | |
1880 | || GET_CODE (SUBREG_REG (exp)) != REG | |
1881 | || REGNO (SUBREG_REG (exp)) != regno | |
1882 | || (((SUBREG_WORD (exp) | |
1883 | + (GET_MODE_SIZE (GET_MODE (exp)) - 1) / UNITS_PER_WORD) | |
1884 | >= word) | |
1885 | && SUBREG_WORD (exp) <= end)) | |
1886 | && refers_to_regno_p (regno, regno + 1, p->exp, NULL_PTR)) | |
1887 | remove_from_table (p, i); | |
1888 | } | |
1889 | } | |
7afe21cc RK |
1890 | \f |
1891 | /* Recompute the hash codes of any valid entries in the hash table that | |
1892 | reference X, if X is a register, or SUBREG_REG (X) if X is a SUBREG. | |
1893 | ||
1894 | This is called when we make a jump equivalence. */ | |
1895 | ||
1896 | static void | |
1897 | rehash_using_reg (x) | |
1898 | rtx x; | |
1899 | { | |
973838fd | 1900 | unsigned int i; |
7afe21cc | 1901 | struct table_elt *p, *next; |
2197a88a | 1902 | unsigned hash; |
7afe21cc RK |
1903 | |
1904 | if (GET_CODE (x) == SUBREG) | |
1905 | x = SUBREG_REG (x); | |
1906 | ||
1907 | /* If X is not a register or if the register is known not to be in any | |
1908 | valid entries in the table, we have no work to do. */ | |
1909 | ||
1910 | if (GET_CODE (x) != REG | |
30f72379 MM |
1911 | || REG_IN_TABLE (REGNO (x)) < 0 |
1912 | || REG_IN_TABLE (REGNO (x)) != REG_TICK (REGNO (x))) | |
7afe21cc RK |
1913 | return; |
1914 | ||
1915 | /* Scan all hash chains looking for valid entries that mention X. | |
1916 | If we find one and it is in the wrong hash chain, move it. We can skip | |
1917 | objects that are registers, since they are handled specially. */ | |
1918 | ||
1919 | for (i = 0; i < NBUCKETS; i++) | |
1920 | for (p = table[i]; p; p = next) | |
1921 | { | |
1922 | next = p->next_same_hash; | |
1923 | if (GET_CODE (p->exp) != REG && reg_mentioned_p (x, p->exp) | |
538b78e7 | 1924 | && exp_equiv_p (p->exp, p->exp, 1, 0) |
7afe21cc RK |
1925 | && i != (hash = safe_hash (p->exp, p->mode) % NBUCKETS)) |
1926 | { | |
1927 | if (p->next_same_hash) | |
1928 | p->next_same_hash->prev_same_hash = p->prev_same_hash; | |
1929 | ||
1930 | if (p->prev_same_hash) | |
1931 | p->prev_same_hash->next_same_hash = p->next_same_hash; | |
1932 | else | |
1933 | table[i] = p->next_same_hash; | |
1934 | ||
1935 | p->next_same_hash = table[hash]; | |
1936 | p->prev_same_hash = 0; | |
1937 | if (table[hash]) | |
1938 | table[hash]->prev_same_hash = p; | |
1939 | table[hash] = p; | |
1940 | } | |
1941 | } | |
1942 | } | |
1943 | \f | |
7afe21cc RK |
1944 | /* Remove from the hash table any expression that is a call-clobbered |
1945 | register. Also update their TICK values. */ | |
1946 | ||
1947 | static void | |
1948 | invalidate_for_call () | |
1949 | { | |
1950 | int regno, endregno; | |
1951 | int i; | |
2197a88a | 1952 | unsigned hash; |
7afe21cc RK |
1953 | struct table_elt *p, *next; |
1954 | int in_table = 0; | |
1955 | ||
1956 | /* Go through all the hard registers. For each that is clobbered in | |
1957 | a CALL_INSN, remove the register from quantity chains and update | |
1958 | reg_tick if defined. Also see if any of these registers is currently | |
1959 | in the table. */ | |
1960 | ||
1961 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1962 | if (TEST_HARD_REG_BIT (regs_invalidated_by_call, regno)) | |
1963 | { | |
1964 | delete_reg_equiv (regno); | |
30f72379 MM |
1965 | if (REG_TICK (regno) >= 0) |
1966 | REG_TICK (regno)++; | |
7afe21cc | 1967 | |
0e227018 | 1968 | in_table |= (TEST_HARD_REG_BIT (hard_regs_in_table, regno) != 0); |
7afe21cc RK |
1969 | } |
1970 | ||
1971 | /* In the case where we have no call-clobbered hard registers in the | |
1972 | table, we are done. Otherwise, scan the table and remove any | |
1973 | entry that overlaps a call-clobbered register. */ | |
1974 | ||
1975 | if (in_table) | |
1976 | for (hash = 0; hash < NBUCKETS; hash++) | |
1977 | for (p = table[hash]; p; p = next) | |
1978 | { | |
1979 | next = p->next_same_hash; | |
1980 | ||
9ae8ffe7 JL |
1981 | if (p->in_memory) |
1982 | { | |
1983 | remove_from_table (p, hash); | |
1984 | continue; | |
1985 | } | |
1986 | ||
7afe21cc RK |
1987 | if (GET_CODE (p->exp) != REG |
1988 | || REGNO (p->exp) >= FIRST_PSEUDO_REGISTER) | |
1989 | continue; | |
1990 | ||
1991 | regno = REGNO (p->exp); | |
1992 | endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (p->exp)); | |
1993 | ||
1994 | for (i = regno; i < endregno; i++) | |
1995 | if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i)) | |
1996 | { | |
1997 | remove_from_table (p, hash); | |
1998 | break; | |
1999 | } | |
2000 | } | |
2001 | } | |
2002 | \f | |
2003 | /* Given an expression X of type CONST, | |
2004 | and ELT which is its table entry (or 0 if it | |
2005 | is not in the hash table), | |
2006 | return an alternate expression for X as a register plus integer. | |
2007 | If none can be found, return 0. */ | |
2008 | ||
2009 | static rtx | |
2010 | use_related_value (x, elt) | |
2011 | rtx x; | |
2012 | struct table_elt *elt; | |
2013 | { | |
2014 | register struct table_elt *relt = 0; | |
2015 | register struct table_elt *p, *q; | |
906c4e36 | 2016 | HOST_WIDE_INT offset; |
7afe21cc RK |
2017 | |
2018 | /* First, is there anything related known? | |
2019 | If we have a table element, we can tell from that. | |
2020 | Otherwise, must look it up. */ | |
2021 | ||
2022 | if (elt != 0 && elt->related_value != 0) | |
2023 | relt = elt; | |
2024 | else if (elt == 0 && GET_CODE (x) == CONST) | |
2025 | { | |
2026 | rtx subexp = get_related_value (x); | |
2027 | if (subexp != 0) | |
2028 | relt = lookup (subexp, | |
2029 | safe_hash (subexp, GET_MODE (subexp)) % NBUCKETS, | |
2030 | GET_MODE (subexp)); | |
2031 | } | |
2032 | ||
2033 | if (relt == 0) | |
2034 | return 0; | |
2035 | ||
2036 | /* Search all related table entries for one that has an | |
2037 | equivalent register. */ | |
2038 | ||
2039 | p = relt; | |
2040 | while (1) | |
2041 | { | |
2042 | /* This loop is strange in that it is executed in two different cases. | |
2043 | The first is when X is already in the table. Then it is searching | |
2044 | the RELATED_VALUE list of X's class (RELT). The second case is when | |
2045 | X is not in the table. Then RELT points to a class for the related | |
2046 | value. | |
2047 | ||
2048 | Ensure that, whatever case we are in, that we ignore classes that have | |
2049 | the same value as X. */ | |
2050 | ||
2051 | if (rtx_equal_p (x, p->exp)) | |
2052 | q = 0; | |
2053 | else | |
2054 | for (q = p->first_same_value; q; q = q->next_same_value) | |
2055 | if (GET_CODE (q->exp) == REG) | |
2056 | break; | |
2057 | ||
2058 | if (q) | |
2059 | break; | |
2060 | ||
2061 | p = p->related_value; | |
2062 | ||
2063 | /* We went all the way around, so there is nothing to be found. | |
2064 | Alternatively, perhaps RELT was in the table for some other reason | |
2065 | and it has no related values recorded. */ | |
2066 | if (p == relt || p == 0) | |
2067 | break; | |
2068 | } | |
2069 | ||
2070 | if (q == 0) | |
2071 | return 0; | |
2072 | ||
2073 | offset = (get_integer_term (x) - get_integer_term (p->exp)); | |
2074 | /* Note: OFFSET may be 0 if P->xexp and X are related by commutativity. */ | |
2075 | return plus_constant (q->exp, offset); | |
2076 | } | |
2077 | \f | |
2078 | /* Hash an rtx. We are careful to make sure the value is never negative. | |
2079 | Equivalent registers hash identically. | |
2080 | MODE is used in hashing for CONST_INTs only; | |
2081 | otherwise the mode of X is used. | |
2082 | ||
2083 | Store 1 in do_not_record if any subexpression is volatile. | |
2084 | ||
2085 | Store 1 in hash_arg_in_memory if X contains a MEM rtx | |
2086 | which does not have the RTX_UNCHANGING_P bit set. | |
2087 | In this case, also store 1 in hash_arg_in_struct | |
2088 | if there is a MEM rtx which has the MEM_IN_STRUCT_P bit set. | |
2089 | ||
2090 | Note that cse_insn knows that the hash code of a MEM expression | |
2091 | is just (int) MEM plus the hash code of the address. */ | |
2092 | ||
2197a88a | 2093 | static unsigned |
7afe21cc RK |
2094 | canon_hash (x, mode) |
2095 | rtx x; | |
2096 | enum machine_mode mode; | |
2097 | { | |
2098 | register int i, j; | |
2197a88a | 2099 | register unsigned hash = 0; |
7afe21cc | 2100 | register enum rtx_code code; |
6f7d635c | 2101 | register const char *fmt; |
7afe21cc RK |
2102 | |
2103 | /* repeat is used to turn tail-recursion into iteration. */ | |
2104 | repeat: | |
2105 | if (x == 0) | |
2106 | return hash; | |
2107 | ||
2108 | code = GET_CODE (x); | |
2109 | switch (code) | |
2110 | { | |
2111 | case REG: | |
2112 | { | |
2113 | register int regno = REGNO (x); | |
2114 | ||
2115 | /* On some machines, we can't record any non-fixed hard register, | |
2116 | because extending its life will cause reload problems. We | |
9a794e50 RH |
2117 | consider ap, fp, and sp to be fixed for this purpose. |
2118 | ||
2119 | We also consider CCmode registers to be fixed for this purpose; | |
2120 | failure to do so leads to failure to simplify 0<100 type of | |
2121 | conditionals. | |
2122 | ||
0f41302f | 2123 | On all machines, we can't record any global registers. */ |
7afe21cc RK |
2124 | |
2125 | if (regno < FIRST_PSEUDO_REGISTER | |
2126 | && (global_regs[regno] | |
f95182a4 ILT |
2127 | || (SMALL_REGISTER_CLASSES |
2128 | && ! fixed_regs[regno] | |
7afe21cc | 2129 | && regno != FRAME_POINTER_REGNUM |
8bc169f2 | 2130 | && regno != HARD_FRAME_POINTER_REGNUM |
7afe21cc | 2131 | && regno != ARG_POINTER_REGNUM |
9a794e50 RH |
2132 | && regno != STACK_POINTER_REGNUM |
2133 | && GET_MODE_CLASS (GET_MODE (x)) != MODE_CC))) | |
7afe21cc RK |
2134 | { |
2135 | do_not_record = 1; | |
2136 | return 0; | |
2137 | } | |
30f72379 | 2138 | hash += ((unsigned) REG << 7) + (unsigned) REG_QTY (regno); |
2197a88a | 2139 | return hash; |
7afe21cc RK |
2140 | } |
2141 | ||
34c73909 R |
2142 | /* We handle SUBREG of a REG specially because the underlying |
2143 | reg changes its hash value with every value change; we don't | |
2144 | want to have to forget unrelated subregs when one subreg changes. */ | |
2145 | case SUBREG: | |
2146 | { | |
2147 | if (GET_CODE (SUBREG_REG (x)) == REG) | |
2148 | { | |
2149 | hash += (((unsigned) SUBREG << 7) | |
2150 | + REGNO (SUBREG_REG (x)) + SUBREG_WORD (x)); | |
2151 | return hash; | |
2152 | } | |
2153 | break; | |
2154 | } | |
2155 | ||
7afe21cc | 2156 | case CONST_INT: |
2197a88a RK |
2157 | { |
2158 | unsigned HOST_WIDE_INT tem = INTVAL (x); | |
2159 | hash += ((unsigned) CONST_INT << 7) + (unsigned) mode + tem; | |
2160 | return hash; | |
2161 | } | |
7afe21cc RK |
2162 | |
2163 | case CONST_DOUBLE: | |
2164 | /* This is like the general case, except that it only counts | |
2165 | the integers representing the constant. */ | |
2197a88a | 2166 | hash += (unsigned) code + (unsigned) GET_MODE (x); |
969c8517 RK |
2167 | if (GET_MODE (x) != VOIDmode) |
2168 | for (i = 2; i < GET_RTX_LENGTH (CONST_DOUBLE); i++) | |
2169 | { | |
ef178af3 | 2170 | unsigned HOST_WIDE_INT tem = XWINT (x, i); |
969c8517 RK |
2171 | hash += tem; |
2172 | } | |
2173 | else | |
2174 | hash += ((unsigned) CONST_DOUBLE_LOW (x) | |
2175 | + (unsigned) CONST_DOUBLE_HIGH (x)); | |
7afe21cc RK |
2176 | return hash; |
2177 | ||
2178 | /* Assume there is only one rtx object for any given label. */ | |
2179 | case LABEL_REF: | |
3c543775 | 2180 | hash |
7bcac048 | 2181 | += ((unsigned) LABEL_REF << 7) + (unsigned long) XEXP (x, 0); |
2197a88a | 2182 | return hash; |
7afe21cc RK |
2183 | |
2184 | case SYMBOL_REF: | |
3c543775 | 2185 | hash |
7bcac048 | 2186 | += ((unsigned) SYMBOL_REF << 7) + (unsigned long) XSTR (x, 0); |
2197a88a | 2187 | return hash; |
7afe21cc RK |
2188 | |
2189 | case MEM: | |
2190 | if (MEM_VOLATILE_P (x)) | |
2191 | { | |
2192 | do_not_record = 1; | |
2193 | return 0; | |
2194 | } | |
9ad91d71 | 2195 | if (! RTX_UNCHANGING_P (x) || FIXED_BASE_PLUS_P (XEXP (x, 0))) |
7afe21cc RK |
2196 | { |
2197 | hash_arg_in_memory = 1; | |
2198 | if (MEM_IN_STRUCT_P (x)) hash_arg_in_struct = 1; | |
2199 | } | |
2200 | /* Now that we have already found this special case, | |
2201 | might as well speed it up as much as possible. */ | |
2197a88a | 2202 | hash += (unsigned) MEM; |
7afe21cc RK |
2203 | x = XEXP (x, 0); |
2204 | goto repeat; | |
2205 | ||
2206 | case PRE_DEC: | |
2207 | case PRE_INC: | |
2208 | case POST_DEC: | |
2209 | case POST_INC: | |
2210 | case PC: | |
2211 | case CC0: | |
2212 | case CALL: | |
2213 | case UNSPEC_VOLATILE: | |
2214 | do_not_record = 1; | |
2215 | return 0; | |
2216 | ||
2217 | case ASM_OPERANDS: | |
2218 | if (MEM_VOLATILE_P (x)) | |
2219 | { | |
2220 | do_not_record = 1; | |
2221 | return 0; | |
2222 | } | |
e9a25f70 JL |
2223 | break; |
2224 | ||
2225 | default: | |
2226 | break; | |
7afe21cc RK |
2227 | } |
2228 | ||
2229 | i = GET_RTX_LENGTH (code) - 1; | |
2197a88a | 2230 | hash += (unsigned) code + (unsigned) GET_MODE (x); |
7afe21cc RK |
2231 | fmt = GET_RTX_FORMAT (code); |
2232 | for (; i >= 0; i--) | |
2233 | { | |
2234 | if (fmt[i] == 'e') | |
2235 | { | |
2236 | rtx tem = XEXP (x, i); | |
7afe21cc RK |
2237 | |
2238 | /* If we are about to do the last recursive call | |
2239 | needed at this level, change it into iteration. | |
2240 | This function is called enough to be worth it. */ | |
2241 | if (i == 0) | |
2242 | { | |
2243 | x = tem; | |
2244 | goto repeat; | |
2245 | } | |
2246 | hash += canon_hash (tem, 0); | |
2247 | } | |
2248 | else if (fmt[i] == 'E') | |
2249 | for (j = 0; j < XVECLEN (x, i); j++) | |
2250 | hash += canon_hash (XVECEXP (x, i, j), 0); | |
2251 | else if (fmt[i] == 's') | |
2252 | { | |
2197a88a | 2253 | register unsigned char *p = (unsigned char *) XSTR (x, i); |
7afe21cc RK |
2254 | if (p) |
2255 | while (*p) | |
2197a88a | 2256 | hash += *p++; |
7afe21cc RK |
2257 | } |
2258 | else if (fmt[i] == 'i') | |
2259 | { | |
2197a88a RK |
2260 | register unsigned tem = XINT (x, i); |
2261 | hash += tem; | |
7afe21cc | 2262 | } |
8f985ec4 | 2263 | else if (fmt[i] == '0' || fmt[i] == 't') |
e9a25f70 | 2264 | /* unused */; |
7afe21cc RK |
2265 | else |
2266 | abort (); | |
2267 | } | |
2268 | return hash; | |
2269 | } | |
2270 | ||
2271 | /* Like canon_hash but with no side effects. */ | |
2272 | ||
2197a88a | 2273 | static unsigned |
7afe21cc RK |
2274 | safe_hash (x, mode) |
2275 | rtx x; | |
2276 | enum machine_mode mode; | |
2277 | { | |
2278 | int save_do_not_record = do_not_record; | |
2279 | int save_hash_arg_in_memory = hash_arg_in_memory; | |
2280 | int save_hash_arg_in_struct = hash_arg_in_struct; | |
2197a88a | 2281 | unsigned hash = canon_hash (x, mode); |
7afe21cc RK |
2282 | hash_arg_in_memory = save_hash_arg_in_memory; |
2283 | hash_arg_in_struct = save_hash_arg_in_struct; | |
2284 | do_not_record = save_do_not_record; | |
2285 | return hash; | |
2286 | } | |
2287 | \f | |
2288 | /* Return 1 iff X and Y would canonicalize into the same thing, | |
2289 | without actually constructing the canonicalization of either one. | |
2290 | If VALIDATE is nonzero, | |
2291 | we assume X is an expression being processed from the rtl | |
2292 | and Y was found in the hash table. We check register refs | |
2293 | in Y for being marked as valid. | |
2294 | ||
2295 | If EQUAL_VALUES is nonzero, we allow a register to match a constant value | |
2296 | that is known to be in the register. Ordinarily, we don't allow them | |
2297 | to match, because letting them match would cause unpredictable results | |
2298 | in all the places that search a hash table chain for an equivalent | |
2299 | for a given value. A possible equivalent that has different structure | |
2300 | has its hash code computed from different data. Whether the hash code | |
38e01259 | 2301 | is the same as that of the given value is pure luck. */ |
7afe21cc RK |
2302 | |
2303 | static int | |
2304 | exp_equiv_p (x, y, validate, equal_values) | |
2305 | rtx x, y; | |
2306 | int validate; | |
2307 | int equal_values; | |
2308 | { | |
906c4e36 | 2309 | register int i, j; |
7afe21cc | 2310 | register enum rtx_code code; |
6f7d635c | 2311 | register const char *fmt; |
7afe21cc RK |
2312 | |
2313 | /* Note: it is incorrect to assume an expression is equivalent to itself | |
2314 | if VALIDATE is nonzero. */ | |
2315 | if (x == y && !validate) | |
2316 | return 1; | |
2317 | if (x == 0 || y == 0) | |
2318 | return x == y; | |
2319 | ||
2320 | code = GET_CODE (x); | |
2321 | if (code != GET_CODE (y)) | |
2322 | { | |
2323 | if (!equal_values) | |
2324 | return 0; | |
2325 | ||
2326 | /* If X is a constant and Y is a register or vice versa, they may be | |
2327 | equivalent. We only have to validate if Y is a register. */ | |
2328 | if (CONSTANT_P (x) && GET_CODE (y) == REG | |
2329 | && REGNO_QTY_VALID_P (REGNO (y)) | |
30f72379 MM |
2330 | && GET_MODE (y) == qty_mode[REG_QTY (REGNO (y))] |
2331 | && rtx_equal_p (x, qty_const[REG_QTY (REGNO (y))]) | |
2332 | && (! validate || REG_IN_TABLE (REGNO (y)) == REG_TICK (REGNO (y)))) | |
7afe21cc RK |
2333 | return 1; |
2334 | ||
2335 | if (CONSTANT_P (y) && code == REG | |
2336 | && REGNO_QTY_VALID_P (REGNO (x)) | |
30f72379 MM |
2337 | && GET_MODE (x) == qty_mode[REG_QTY (REGNO (x))] |
2338 | && rtx_equal_p (y, qty_const[REG_QTY (REGNO (x))])) | |
7afe21cc RK |
2339 | return 1; |
2340 | ||
2341 | return 0; | |
2342 | } | |
2343 | ||
2344 | /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */ | |
2345 | if (GET_MODE (x) != GET_MODE (y)) | |
2346 | return 0; | |
2347 | ||
2348 | switch (code) | |
2349 | { | |
2350 | case PC: | |
2351 | case CC0: | |
2352 | return x == y; | |
2353 | ||
2354 | case CONST_INT: | |
58c8c593 | 2355 | return INTVAL (x) == INTVAL (y); |
7afe21cc RK |
2356 | |
2357 | case LABEL_REF: | |
7afe21cc RK |
2358 | return XEXP (x, 0) == XEXP (y, 0); |
2359 | ||
f54d4924 RK |
2360 | case SYMBOL_REF: |
2361 | return XSTR (x, 0) == XSTR (y, 0); | |
2362 | ||
7afe21cc RK |
2363 | case REG: |
2364 | { | |
2365 | int regno = REGNO (y); | |
2366 | int endregno | |
2367 | = regno + (regno >= FIRST_PSEUDO_REGISTER ? 1 | |
2368 | : HARD_REGNO_NREGS (regno, GET_MODE (y))); | |
2369 | int i; | |
2370 | ||
2371 | /* If the quantities are not the same, the expressions are not | |
2372 | equivalent. If there are and we are not to validate, they | |
2373 | are equivalent. Otherwise, ensure all regs are up-to-date. */ | |
2374 | ||
30f72379 | 2375 | if (REG_QTY (REGNO (x)) != REG_QTY (regno)) |
7afe21cc RK |
2376 | return 0; |
2377 | ||
2378 | if (! validate) | |
2379 | return 1; | |
2380 | ||
2381 | for (i = regno; i < endregno; i++) | |
30f72379 | 2382 | if (REG_IN_TABLE (i) != REG_TICK (i)) |
7afe21cc RK |
2383 | return 0; |
2384 | ||
2385 | return 1; | |
2386 | } | |
2387 | ||
2388 | /* For commutative operations, check both orders. */ | |
2389 | case PLUS: | |
2390 | case MULT: | |
2391 | case AND: | |
2392 | case IOR: | |
2393 | case XOR: | |
2394 | case NE: | |
2395 | case EQ: | |
2396 | return ((exp_equiv_p (XEXP (x, 0), XEXP (y, 0), validate, equal_values) | |
2397 | && exp_equiv_p (XEXP (x, 1), XEXP (y, 1), | |
2398 | validate, equal_values)) | |
2399 | || (exp_equiv_p (XEXP (x, 0), XEXP (y, 1), | |
2400 | validate, equal_values) | |
2401 | && exp_equiv_p (XEXP (x, 1), XEXP (y, 0), | |
2402 | validate, equal_values))); | |
e9a25f70 JL |
2403 | |
2404 | default: | |
2405 | break; | |
7afe21cc RK |
2406 | } |
2407 | ||
2408 | /* Compare the elements. If any pair of corresponding elements | |
2409 | fail to match, return 0 for the whole things. */ | |
2410 | ||
2411 | fmt = GET_RTX_FORMAT (code); | |
2412 | for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) | |
2413 | { | |
906c4e36 | 2414 | switch (fmt[i]) |
7afe21cc | 2415 | { |
906c4e36 | 2416 | case 'e': |
7afe21cc RK |
2417 | if (! exp_equiv_p (XEXP (x, i), XEXP (y, i), validate, equal_values)) |
2418 | return 0; | |
906c4e36 RK |
2419 | break; |
2420 | ||
2421 | case 'E': | |
7afe21cc RK |
2422 | if (XVECLEN (x, i) != XVECLEN (y, i)) |
2423 | return 0; | |
2424 | for (j = 0; j < XVECLEN (x, i); j++) | |
2425 | if (! exp_equiv_p (XVECEXP (x, i, j), XVECEXP (y, i, j), | |
2426 | validate, equal_values)) | |
2427 | return 0; | |
906c4e36 RK |
2428 | break; |
2429 | ||
2430 | case 's': | |
7afe21cc RK |
2431 | if (strcmp (XSTR (x, i), XSTR (y, i))) |
2432 | return 0; | |
906c4e36 RK |
2433 | break; |
2434 | ||
2435 | case 'i': | |
7afe21cc RK |
2436 | if (XINT (x, i) != XINT (y, i)) |
2437 | return 0; | |
906c4e36 RK |
2438 | break; |
2439 | ||
2440 | case 'w': | |
2441 | if (XWINT (x, i) != XWINT (y, i)) | |
2442 | return 0; | |
2443 | break; | |
2444 | ||
2445 | case '0': | |
8f985ec4 | 2446 | case 't': |
906c4e36 RK |
2447 | break; |
2448 | ||
2449 | default: | |
2450 | abort (); | |
7afe21cc | 2451 | } |
906c4e36 RK |
2452 | } |
2453 | ||
7afe21cc RK |
2454 | return 1; |
2455 | } | |
2456 | \f | |
2457 | /* Return 1 iff any subexpression of X matches Y. | |
2458 | Here we do not require that X or Y be valid (for registers referred to) | |
2459 | for being in the hash table. */ | |
2460 | ||
6cd4575e | 2461 | static int |
7afe21cc RK |
2462 | refers_to_p (x, y) |
2463 | rtx x, y; | |
2464 | { | |
2465 | register int i; | |
2466 | register enum rtx_code code; | |
6f7d635c | 2467 | register const char *fmt; |
7afe21cc RK |
2468 | |
2469 | repeat: | |
2470 | if (x == y) | |
2471 | return 1; | |
2472 | if (x == 0 || y == 0) | |
2473 | return 0; | |
2474 | ||
2475 | code = GET_CODE (x); | |
2476 | /* If X as a whole has the same code as Y, they may match. | |
2477 | If so, return 1. */ | |
2478 | if (code == GET_CODE (y)) | |
2479 | { | |
2480 | if (exp_equiv_p (x, y, 0, 1)) | |
2481 | return 1; | |
2482 | } | |
2483 | ||
2484 | /* X does not match, so try its subexpressions. */ | |
2485 | ||
2486 | fmt = GET_RTX_FORMAT (code); | |
2487 | for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) | |
2488 | if (fmt[i] == 'e') | |
2489 | { | |
2490 | if (i == 0) | |
2491 | { | |
2492 | x = XEXP (x, 0); | |
2493 | goto repeat; | |
2494 | } | |
2495 | else | |
2496 | if (refers_to_p (XEXP (x, i), y)) | |
2497 | return 1; | |
2498 | } | |
2499 | else if (fmt[i] == 'E') | |
2500 | { | |
2501 | int j; | |
2502 | for (j = 0; j < XVECLEN (x, i); j++) | |
2503 | if (refers_to_p (XVECEXP (x, i, j), y)) | |
2504 | return 1; | |
2505 | } | |
2506 | ||
2507 | return 0; | |
2508 | } | |
2509 | \f | |
f451db89 JL |
2510 | /* Given ADDR and SIZE (a memory address, and the size of the memory reference), |
2511 | set PBASE, PSTART, and PEND which correspond to the base of the address, | |
2512 | the starting offset, and ending offset respectively. | |
2513 | ||
bb4034b3 | 2514 | ADDR is known to be a nonvarying address. */ |
f451db89 | 2515 | |
bb4034b3 JW |
2516 | /* ??? Despite what the comments say, this function is in fact frequently |
2517 | passed varying addresses. This does not appear to cause any problems. */ | |
f451db89 JL |
2518 | |
2519 | static void | |
2520 | set_nonvarying_address_components (addr, size, pbase, pstart, pend) | |
2521 | rtx addr; | |
2522 | int size; | |
2523 | rtx *pbase; | |
6500fb43 | 2524 | HOST_WIDE_INT *pstart, *pend; |
f451db89 JL |
2525 | { |
2526 | rtx base; | |
c85663b1 | 2527 | HOST_WIDE_INT start, end; |
f451db89 JL |
2528 | |
2529 | base = addr; | |
2530 | start = 0; | |
2531 | end = 0; | |
2532 | ||
e5e809f4 JL |
2533 | if (flag_pic && GET_CODE (base) == PLUS |
2534 | && XEXP (base, 0) == pic_offset_table_rtx) | |
2535 | base = XEXP (base, 1); | |
2536 | ||
f451db89 JL |
2537 | /* Registers with nonvarying addresses usually have constant equivalents; |
2538 | but the frame pointer register is also possible. */ | |
2539 | if (GET_CODE (base) == REG | |
2540 | && qty_const != 0 | |
2541 | && REGNO_QTY_VALID_P (REGNO (base)) | |
30f72379 MM |
2542 | && qty_mode[REG_QTY (REGNO (base))] == GET_MODE (base) |
2543 | && qty_const[REG_QTY (REGNO (base))] != 0) | |
2544 | base = qty_const[REG_QTY (REGNO (base))]; | |
f451db89 JL |
2545 | else if (GET_CODE (base) == PLUS |
2546 | && GET_CODE (XEXP (base, 1)) == CONST_INT | |
2547 | && GET_CODE (XEXP (base, 0)) == REG | |
2548 | && qty_const != 0 | |
2549 | && REGNO_QTY_VALID_P (REGNO (XEXP (base, 0))) | |
30f72379 | 2550 | && (qty_mode[REG_QTY (REGNO (XEXP (base, 0)))] |
f451db89 | 2551 | == GET_MODE (XEXP (base, 0))) |
30f72379 | 2552 | && qty_const[REG_QTY (REGNO (XEXP (base, 0)))]) |
f451db89 JL |
2553 | { |
2554 | start = INTVAL (XEXP (base, 1)); | |
30f72379 | 2555 | base = qty_const[REG_QTY (REGNO (XEXP (base, 0)))]; |
f451db89 | 2556 | } |
9c6b0bae | 2557 | /* This can happen as the result of virtual register instantiation, |
abc95ed3 | 2558 | if the initial offset is too large to be a valid address. */ |
9c6b0bae RK |
2559 | else if (GET_CODE (base) == PLUS |
2560 | && GET_CODE (XEXP (base, 0)) == REG | |
2561 | && GET_CODE (XEXP (base, 1)) == REG | |
2562 | && qty_const != 0 | |
2563 | && REGNO_QTY_VALID_P (REGNO (XEXP (base, 0))) | |
30f72379 | 2564 | && (qty_mode[REG_QTY (REGNO (XEXP (base, 0)))] |
9c6b0bae | 2565 | == GET_MODE (XEXP (base, 0))) |
30f72379 | 2566 | && qty_const[REG_QTY (REGNO (XEXP (base, 0)))] |
9c6b0bae | 2567 | && REGNO_QTY_VALID_P (REGNO (XEXP (base, 1))) |
30f72379 | 2568 | && (qty_mode[REG_QTY (REGNO (XEXP (base, 1)))] |
9c6b0bae | 2569 | == GET_MODE (XEXP (base, 1))) |
30f72379 | 2570 | && qty_const[REG_QTY (REGNO (XEXP (base, 1)))]) |
9c6b0bae | 2571 | { |
30f72379 MM |
2572 | rtx tem = qty_const[REG_QTY (REGNO (XEXP (base, 1)))]; |
2573 | base = qty_const[REG_QTY (REGNO (XEXP (base, 0)))]; | |
9c6b0bae RK |
2574 | |
2575 | /* One of the two values must be a constant. */ | |
2576 | if (GET_CODE (base) != CONST_INT) | |
2577 | { | |
2578 | if (GET_CODE (tem) != CONST_INT) | |
2579 | abort (); | |
2580 | start = INTVAL (tem); | |
2581 | } | |
2582 | else | |
2583 | { | |
2584 | start = INTVAL (base); | |
2585 | base = tem; | |
2586 | } | |
2587 | } | |
f451db89 | 2588 | |
c85663b1 RK |
2589 | /* Handle everything that we can find inside an address that has been |
2590 | viewed as constant. */ | |
f451db89 | 2591 | |
c85663b1 | 2592 | while (1) |
f451db89 | 2593 | { |
c85663b1 RK |
2594 | /* If no part of this switch does a "continue", the code outside |
2595 | will exit this loop. */ | |
2596 | ||
2597 | switch (GET_CODE (base)) | |
2598 | { | |
2599 | case LO_SUM: | |
2600 | /* By definition, operand1 of a LO_SUM is the associated constant | |
2601 | address. Use the associated constant address as the base | |
2602 | instead. */ | |
2603 | base = XEXP (base, 1); | |
2604 | continue; | |
2605 | ||
2606 | case CONST: | |
2607 | /* Strip off CONST. */ | |
2608 | base = XEXP (base, 0); | |
2609 | continue; | |
2610 | ||
2611 | case PLUS: | |
2612 | if (GET_CODE (XEXP (base, 1)) == CONST_INT) | |
2613 | { | |
2614 | start += INTVAL (XEXP (base, 1)); | |
2615 | base = XEXP (base, 0); | |
2616 | continue; | |
2617 | } | |
2618 | break; | |
2619 | ||
2620 | case AND: | |
2621 | /* Handle the case of an AND which is the negative of a power of | |
2622 | two. This is used to represent unaligned memory operations. */ | |
2623 | if (GET_CODE (XEXP (base, 1)) == CONST_INT | |
2624 | && exact_log2 (- INTVAL (XEXP (base, 1))) > 0) | |
2625 | { | |
2626 | set_nonvarying_address_components (XEXP (base, 0), size, | |
2627 | pbase, pstart, pend); | |
2628 | ||
2629 | /* Assume the worst misalignment. START is affected, but not | |
2630 | END, so compensate but adjusting SIZE. Don't lose any | |
2631 | constant we already had. */ | |
2632 | ||
2633 | size = *pend - *pstart - INTVAL (XEXP (base, 1)) - 1; | |
89046535 RK |
2634 | start += *pstart + INTVAL (XEXP (base, 1)) + 1; |
2635 | end += *pend; | |
c85663b1 RK |
2636 | base = *pbase; |
2637 | } | |
2638 | break; | |
e9a25f70 JL |
2639 | |
2640 | default: | |
2641 | break; | |
c85663b1 RK |
2642 | } |
2643 | ||
2644 | break; | |
f451db89 JL |
2645 | } |
2646 | ||
336d6f0a RK |
2647 | if (GET_CODE (base) == CONST_INT) |
2648 | { | |
2649 | start += INTVAL (base); | |
2650 | base = const0_rtx; | |
2651 | } | |
2652 | ||
f451db89 JL |
2653 | end = start + size; |
2654 | ||
2655 | /* Set the return values. */ | |
2656 | *pbase = base; | |
2657 | *pstart = start; | |
2658 | *pend = end; | |
2659 | } | |
2660 | ||
9ae8ffe7 JL |
2661 | /* Return 1 if X has a value that can vary even between two |
2662 | executions of the program. 0 means X can be compared reliably | |
2663 | against certain constants or near-constants. */ | |
7afe21cc RK |
2664 | |
2665 | static int | |
9ae8ffe7 JL |
2666 | cse_rtx_varies_p (x) |
2667 | register rtx x; | |
7afe21cc RK |
2668 | { |
2669 | /* We need not check for X and the equivalence class being of the same | |
2670 | mode because if X is equivalent to a constant in some mode, it | |
2671 | doesn't vary in any mode. */ | |
2672 | ||
9ae8ffe7 JL |
2673 | if (GET_CODE (x) == REG |
2674 | && REGNO_QTY_VALID_P (REGNO (x)) | |
30f72379 MM |
2675 | && GET_MODE (x) == qty_mode[REG_QTY (REGNO (x))] |
2676 | && qty_const[REG_QTY (REGNO (x))] != 0) | |
7afe21cc RK |
2677 | return 0; |
2678 | ||
9ae8ffe7 JL |
2679 | if (GET_CODE (x) == PLUS |
2680 | && GET_CODE (XEXP (x, 1)) == CONST_INT | |
2681 | && GET_CODE (XEXP (x, 0)) == REG | |
2682 | && REGNO_QTY_VALID_P (REGNO (XEXP (x, 0))) | |
2683 | && (GET_MODE (XEXP (x, 0)) | |
30f72379 MM |
2684 | == qty_mode[REG_QTY (REGNO (XEXP (x, 0)))]) |
2685 | && qty_const[REG_QTY (REGNO (XEXP (x, 0)))]) | |
7afe21cc RK |
2686 | return 0; |
2687 | ||
9c6b0bae RK |
2688 | /* This can happen as the result of virtual register instantiation, if |
2689 | the initial constant is too large to be a valid address. This gives | |
2690 | us a three instruction sequence, load large offset into a register, | |
2691 | load fp minus a constant into a register, then a MEM which is the | |
2692 | sum of the two `constant' registers. */ | |
9ae8ffe7 JL |
2693 | if (GET_CODE (x) == PLUS |
2694 | && GET_CODE (XEXP (x, 0)) == REG | |
2695 | && GET_CODE (XEXP (x, 1)) == REG | |
2696 | && REGNO_QTY_VALID_P (REGNO (XEXP (x, 0))) | |
2697 | && (GET_MODE (XEXP (x, 0)) | |
30f72379 MM |
2698 | == qty_mode[REG_QTY (REGNO (XEXP (x, 0)))]) |
2699 | && qty_const[REG_QTY (REGNO (XEXP (x, 0)))] | |
9ae8ffe7 JL |
2700 | && REGNO_QTY_VALID_P (REGNO (XEXP (x, 1))) |
2701 | && (GET_MODE (XEXP (x, 1)) | |
30f72379 MM |
2702 | == qty_mode[REG_QTY (REGNO (XEXP (x, 1)))]) |
2703 | && qty_const[REG_QTY (REGNO (XEXP (x, 1)))]) | |
9c6b0bae RK |
2704 | return 0; |
2705 | ||
9ae8ffe7 | 2706 | return rtx_varies_p (x); |
7afe21cc RK |
2707 | } |
2708 | \f | |
2709 | /* Canonicalize an expression: | |
2710 | replace each register reference inside it | |
2711 | with the "oldest" equivalent register. | |
2712 | ||
2713 | If INSN is non-zero and we are replacing a pseudo with a hard register | |
7722328e RK |
2714 | or vice versa, validate_change is used to ensure that INSN remains valid |
2715 | after we make our substitution. The calls are made with IN_GROUP non-zero | |
2716 | so apply_change_group must be called upon the outermost return from this | |
2717 | function (unless INSN is zero). The result of apply_change_group can | |
2718 | generally be discarded since the changes we are making are optional. */ | |
7afe21cc RK |
2719 | |
2720 | static rtx | |
2721 | canon_reg (x, insn) | |
2722 | rtx x; | |
2723 | rtx insn; | |
2724 | { | |
2725 | register int i; | |
2726 | register enum rtx_code code; | |
6f7d635c | 2727 | register const char *fmt; |
7afe21cc RK |
2728 | |
2729 | if (x == 0) | |
2730 | return x; | |
2731 | ||
2732 | code = GET_CODE (x); | |
2733 | switch (code) | |
2734 | { | |
2735 | case PC: | |
2736 | case CC0: | |
2737 | case CONST: | |
2738 | case CONST_INT: | |
2739 | case CONST_DOUBLE: | |
2740 | case SYMBOL_REF: | |
2741 | case LABEL_REF: | |
2742 | case ADDR_VEC: | |
2743 | case ADDR_DIFF_VEC: | |
2744 | return x; | |
2745 | ||
2746 | case REG: | |
2747 | { | |
2748 | register int first; | |
2749 | ||
2750 | /* Never replace a hard reg, because hard regs can appear | |
2751 | in more than one machine mode, and we must preserve the mode | |
2752 | of each occurrence. Also, some hard regs appear in | |
2753 | MEMs that are shared and mustn't be altered. Don't try to | |
2754 | replace any reg that maps to a reg of class NO_REGS. */ | |
2755 | if (REGNO (x) < FIRST_PSEUDO_REGISTER | |
2756 | || ! REGNO_QTY_VALID_P (REGNO (x))) | |
2757 | return x; | |
2758 | ||
30f72379 | 2759 | first = qty_first_reg[REG_QTY (REGNO (x))]; |
7afe21cc RK |
2760 | return (first >= FIRST_PSEUDO_REGISTER ? regno_reg_rtx[first] |
2761 | : REGNO_REG_CLASS (first) == NO_REGS ? x | |
30f72379 | 2762 | : gen_rtx_REG (qty_mode[REG_QTY (REGNO (x))], first)); |
7afe21cc | 2763 | } |
e9a25f70 JL |
2764 | |
2765 | default: | |
2766 | break; | |
7afe21cc RK |
2767 | } |
2768 | ||
2769 | fmt = GET_RTX_FORMAT (code); | |
2770 | for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) | |
2771 | { | |
2772 | register int j; | |
2773 | ||
2774 | if (fmt[i] == 'e') | |
2775 | { | |
2776 | rtx new = canon_reg (XEXP (x, i), insn); | |
58873255 | 2777 | int insn_code; |
7afe21cc RK |
2778 | |
2779 | /* If replacing pseudo with hard reg or vice versa, ensure the | |
178c39f6 | 2780 | insn remains valid. Likewise if the insn has MATCH_DUPs. */ |
aee9dc31 RS |
2781 | if (insn != 0 && new != 0 |
2782 | && GET_CODE (new) == REG && GET_CODE (XEXP (x, i)) == REG | |
178c39f6 RK |
2783 | && (((REGNO (new) < FIRST_PSEUDO_REGISTER) |
2784 | != (REGNO (XEXP (x, i)) < FIRST_PSEUDO_REGISTER)) | |
58873255 | 2785 | || (insn_code = recog_memoized (insn)) < 0 |
a995e389 | 2786 | || insn_data[insn_code].n_dups > 0)) |
77fa0940 | 2787 | validate_change (insn, &XEXP (x, i), new, 1); |
7afe21cc RK |
2788 | else |
2789 | XEXP (x, i) = new; | |
2790 | } | |
2791 | else if (fmt[i] == 'E') | |
2792 | for (j = 0; j < XVECLEN (x, i); j++) | |
2793 | XVECEXP (x, i, j) = canon_reg (XVECEXP (x, i, j), insn); | |
2794 | } | |
2795 | ||
2796 | return x; | |
2797 | } | |
2798 | \f | |
a2cabb29 | 2799 | /* LOC is a location within INSN that is an operand address (the contents of |
7afe21cc RK |
2800 | a MEM). Find the best equivalent address to use that is valid for this |
2801 | insn. | |
2802 | ||
2803 | On most CISC machines, complicated address modes are costly, and rtx_cost | |
2804 | is a good approximation for that cost. However, most RISC machines have | |
2805 | only a few (usually only one) memory reference formats. If an address is | |
2806 | valid at all, it is often just as cheap as any other address. Hence, for | |
2807 | RISC machines, we use the configuration macro `ADDRESS_COST' to compare the | |
2808 | costs of various addresses. For two addresses of equal cost, choose the one | |
2809 | with the highest `rtx_cost' value as that has the potential of eliminating | |
2810 | the most insns. For equal costs, we choose the first in the equivalence | |
2811 | class. Note that we ignore the fact that pseudo registers are cheaper | |
2812 | than hard registers here because we would also prefer the pseudo registers. | |
2813 | */ | |
2814 | ||
6cd4575e | 2815 | static void |
7afe21cc RK |
2816 | find_best_addr (insn, loc) |
2817 | rtx insn; | |
2818 | rtx *loc; | |
2819 | { | |
7a87758d | 2820 | struct table_elt *elt; |
7afe21cc | 2821 | rtx addr = *loc; |
7a87758d AS |
2822 | #ifdef ADDRESS_COST |
2823 | struct table_elt *p; | |
7afe21cc | 2824 | int found_better = 1; |
7a87758d | 2825 | #endif |
7afe21cc RK |
2826 | int save_do_not_record = do_not_record; |
2827 | int save_hash_arg_in_memory = hash_arg_in_memory; | |
2828 | int save_hash_arg_in_struct = hash_arg_in_struct; | |
7afe21cc RK |
2829 | int addr_volatile; |
2830 | int regno; | |
2197a88a | 2831 | unsigned hash; |
7afe21cc RK |
2832 | |
2833 | /* Do not try to replace constant addresses or addresses of local and | |
2834 | argument slots. These MEM expressions are made only once and inserted | |
2835 | in many instructions, as well as being used to control symbol table | |
2836 | output. It is not safe to clobber them. | |
2837 | ||
2838 | There are some uncommon cases where the address is already in a register | |
2839 | for some reason, but we cannot take advantage of that because we have | |
2840 | no easy way to unshare the MEM. In addition, looking up all stack | |
2841 | addresses is costly. */ | |
2842 | if ((GET_CODE (addr) == PLUS | |
2843 | && GET_CODE (XEXP (addr, 0)) == REG | |
2844 | && GET_CODE (XEXP (addr, 1)) == CONST_INT | |
2845 | && (regno = REGNO (XEXP (addr, 0)), | |
8bc169f2 DE |
2846 | regno == FRAME_POINTER_REGNUM || regno == HARD_FRAME_POINTER_REGNUM |
2847 | || regno == ARG_POINTER_REGNUM)) | |
7afe21cc | 2848 | || (GET_CODE (addr) == REG |
8bc169f2 DE |
2849 | && (regno = REGNO (addr), regno == FRAME_POINTER_REGNUM |
2850 | || regno == HARD_FRAME_POINTER_REGNUM | |
2851 | || regno == ARG_POINTER_REGNUM)) | |
e9a25f70 | 2852 | || GET_CODE (addr) == ADDRESSOF |
7afe21cc RK |
2853 | || CONSTANT_ADDRESS_P (addr)) |
2854 | return; | |
2855 | ||
2856 | /* If this address is not simply a register, try to fold it. This will | |
2857 | sometimes simplify the expression. Many simplifications | |
2858 | will not be valid, but some, usually applying the associative rule, will | |
2859 | be valid and produce better code. */ | |
8c87f107 RK |
2860 | if (GET_CODE (addr) != REG) |
2861 | { | |
2862 | rtx folded = fold_rtx (copy_rtx (addr), NULL_RTX); | |
2863 | ||
2864 | if (1 | |
2865 | #ifdef ADDRESS_COST | |
2f541799 MM |
2866 | && (CSE_ADDRESS_COST (folded) < CSE_ADDRESS_COST (addr) |
2867 | || (CSE_ADDRESS_COST (folded) == CSE_ADDRESS_COST (addr) | |
9a252d29 | 2868 | && rtx_cost (folded, MEM) > rtx_cost (addr, MEM))) |
8c87f107 | 2869 | #else |
9a252d29 | 2870 | && rtx_cost (folded, MEM) < rtx_cost (addr, MEM) |
8c87f107 RK |
2871 | #endif |
2872 | && validate_change (insn, loc, folded, 0)) | |
2873 | addr = folded; | |
2874 | } | |
7afe21cc | 2875 | |
42495ca0 RK |
2876 | /* If this address is not in the hash table, we can't look for equivalences |
2877 | of the whole address. Also, ignore if volatile. */ | |
2878 | ||
7afe21cc | 2879 | do_not_record = 0; |
2197a88a | 2880 | hash = HASH (addr, Pmode); |
7afe21cc RK |
2881 | addr_volatile = do_not_record; |
2882 | do_not_record = save_do_not_record; | |
2883 | hash_arg_in_memory = save_hash_arg_in_memory; | |
2884 | hash_arg_in_struct = save_hash_arg_in_struct; | |
2885 | ||
2886 | if (addr_volatile) | |
2887 | return; | |
2888 | ||
2197a88a | 2889 | elt = lookup (addr, hash, Pmode); |
7afe21cc | 2890 | |
7afe21cc | 2891 | #ifndef ADDRESS_COST |
42495ca0 RK |
2892 | if (elt) |
2893 | { | |
2d8b0f3a | 2894 | int our_cost = elt->cost; |
42495ca0 RK |
2895 | |
2896 | /* Find the lowest cost below ours that works. */ | |
2897 | for (elt = elt->first_same_value; elt; elt = elt->next_same_value) | |
2898 | if (elt->cost < our_cost | |
2899 | && (GET_CODE (elt->exp) == REG | |
2900 | || exp_equiv_p (elt->exp, elt->exp, 1, 0)) | |
2901 | && validate_change (insn, loc, | |
906c4e36 | 2902 | canon_reg (copy_rtx (elt->exp), NULL_RTX), 0)) |
42495ca0 RK |
2903 | return; |
2904 | } | |
2905 | #else | |
7afe21cc | 2906 | |
42495ca0 RK |
2907 | if (elt) |
2908 | { | |
2909 | /* We need to find the best (under the criteria documented above) entry | |
2910 | in the class that is valid. We use the `flag' field to indicate | |
2911 | choices that were invalid and iterate until we can't find a better | |
2912 | one that hasn't already been tried. */ | |
7afe21cc | 2913 | |
42495ca0 RK |
2914 | for (p = elt->first_same_value; p; p = p->next_same_value) |
2915 | p->flag = 0; | |
7afe21cc | 2916 | |
42495ca0 RK |
2917 | while (found_better) |
2918 | { | |
2f541799 | 2919 | int best_addr_cost = CSE_ADDRESS_COST (*loc); |
42495ca0 RK |
2920 | int best_rtx_cost = (elt->cost + 1) >> 1; |
2921 | struct table_elt *best_elt = elt; | |
2922 | ||
2923 | found_better = 0; | |
2924 | for (p = elt->first_same_value; p; p = p->next_same_value) | |
2f541799 | 2925 | if (! p->flag) |
42495ca0 | 2926 | { |
2f541799 MM |
2927 | if ((GET_CODE (p->exp) == REG |
2928 | || exp_equiv_p (p->exp, p->exp, 1, 0)) | |
2929 | && (CSE_ADDRESS_COST (p->exp) < best_addr_cost | |
2930 | || (CSE_ADDRESS_COST (p->exp) == best_addr_cost | |
2931 | && (p->cost + 1) >> 1 > best_rtx_cost))) | |
2932 | { | |
2933 | found_better = 1; | |
2934 | best_addr_cost = CSE_ADDRESS_COST (p->exp); | |
2935 | best_rtx_cost = (p->cost + 1) >> 1; | |
2936 | best_elt = p; | |
2937 | } | |
42495ca0 | 2938 | } |
7afe21cc | 2939 | |
42495ca0 RK |
2940 | if (found_better) |
2941 | { | |
2942 | if (validate_change (insn, loc, | |
906c4e36 RK |
2943 | canon_reg (copy_rtx (best_elt->exp), |
2944 | NULL_RTX), 0)) | |
42495ca0 RK |
2945 | return; |
2946 | else | |
2947 | best_elt->flag = 1; | |
2948 | } | |
2949 | } | |
2950 | } | |
7afe21cc | 2951 | |
42495ca0 RK |
2952 | /* If the address is a binary operation with the first operand a register |
2953 | and the second a constant, do the same as above, but looking for | |
2954 | equivalences of the register. Then try to simplify before checking for | |
2955 | the best address to use. This catches a few cases: First is when we | |
2956 | have REG+const and the register is another REG+const. We can often merge | |
2957 | the constants and eliminate one insn and one register. It may also be | |
2958 | that a machine has a cheap REG+REG+const. Finally, this improves the | |
2959 | code on the Alpha for unaligned byte stores. */ | |
2960 | ||
2961 | if (flag_expensive_optimizations | |
2962 | && (GET_RTX_CLASS (GET_CODE (*loc)) == '2' | |
2963 | || GET_RTX_CLASS (GET_CODE (*loc)) == 'c') | |
2964 | && GET_CODE (XEXP (*loc, 0)) == REG | |
2965 | && GET_CODE (XEXP (*loc, 1)) == CONST_INT) | |
7afe21cc | 2966 | { |
42495ca0 RK |
2967 | rtx c = XEXP (*loc, 1); |
2968 | ||
2969 | do_not_record = 0; | |
2197a88a | 2970 | hash = HASH (XEXP (*loc, 0), Pmode); |
42495ca0 RK |
2971 | do_not_record = save_do_not_record; |
2972 | hash_arg_in_memory = save_hash_arg_in_memory; | |
2973 | hash_arg_in_struct = save_hash_arg_in_struct; | |
2974 | ||
2197a88a | 2975 | elt = lookup (XEXP (*loc, 0), hash, Pmode); |
42495ca0 RK |
2976 | if (elt == 0) |
2977 | return; | |
2978 | ||
2979 | /* We need to find the best (under the criteria documented above) entry | |
2980 | in the class that is valid. We use the `flag' field to indicate | |
2981 | choices that were invalid and iterate until we can't find a better | |
2982 | one that hasn't already been tried. */ | |
7afe21cc | 2983 | |
7afe21cc | 2984 | for (p = elt->first_same_value; p; p = p->next_same_value) |
42495ca0 | 2985 | p->flag = 0; |
7afe21cc | 2986 | |
42495ca0 | 2987 | while (found_better) |
7afe21cc | 2988 | { |
2f541799 | 2989 | int best_addr_cost = CSE_ADDRESS_COST (*loc); |
42495ca0 RK |
2990 | int best_rtx_cost = (COST (*loc) + 1) >> 1; |
2991 | struct table_elt *best_elt = elt; | |
2992 | rtx best_rtx = *loc; | |
f6516aee JW |
2993 | int count; |
2994 | ||
2995 | /* This is at worst case an O(n^2) algorithm, so limit our search | |
2996 | to the first 32 elements on the list. This avoids trouble | |
2997 | compiling code with very long basic blocks that can easily | |
2998 | call cse_gen_binary so many times that we run out of memory. */ | |
42495ca0 RK |
2999 | |
3000 | found_better = 0; | |
f6516aee JW |
3001 | for (p = elt->first_same_value, count = 0; |
3002 | p && count < 32; | |
3003 | p = p->next_same_value, count++) | |
42495ca0 RK |
3004 | if (! p->flag |
3005 | && (GET_CODE (p->exp) == REG | |
3006 | || exp_equiv_p (p->exp, p->exp, 1, 0))) | |
3007 | { | |
96b0e481 | 3008 | rtx new = cse_gen_binary (GET_CODE (*loc), Pmode, p->exp, c); |
42495ca0 | 3009 | |
2f541799 MM |
3010 | if ((CSE_ADDRESS_COST (new) < best_addr_cost |
3011 | || (CSE_ADDRESS_COST (new) == best_addr_cost | |
42495ca0 RK |
3012 | && (COST (new) + 1) >> 1 > best_rtx_cost))) |
3013 | { | |
3014 | found_better = 1; | |
2f541799 | 3015 | best_addr_cost = CSE_ADDRESS_COST (new); |
42495ca0 RK |
3016 | best_rtx_cost = (COST (new) + 1) >> 1; |
3017 | best_elt = p; | |
3018 | best_rtx = new; | |
3019 | } | |
3020 | } | |
3021 | ||
3022 | if (found_better) | |
3023 | { | |
3024 | if (validate_change (insn, loc, | |
906c4e36 RK |
3025 | canon_reg (copy_rtx (best_rtx), |
3026 | NULL_RTX), 0)) | |
42495ca0 RK |
3027 | return; |
3028 | else | |
3029 | best_elt->flag = 1; | |
3030 | } | |
7afe21cc RK |
3031 | } |
3032 | } | |
3033 | #endif | |
3034 | } | |
3035 | \f | |
3036 | /* Given an operation (CODE, *PARG1, *PARG2), where code is a comparison | |
3037 | operation (EQ, NE, GT, etc.), follow it back through the hash table and | |
3038 | what values are being compared. | |
3039 | ||
3040 | *PARG1 and *PARG2 are updated to contain the rtx representing the values | |
3041 | actually being compared. For example, if *PARG1 was (cc0) and *PARG2 | |
3042 | was (const_int 0), *PARG1 and *PARG2 will be set to the objects that were | |
3043 | compared to produce cc0. | |
3044 | ||
3045 | The return value is the comparison operator and is either the code of | |
3046 | A or the code corresponding to the inverse of the comparison. */ | |
3047 | ||
3048 | static enum rtx_code | |
13c9910f | 3049 | find_comparison_args (code, parg1, parg2, pmode1, pmode2) |
7afe21cc RK |
3050 | enum rtx_code code; |
3051 | rtx *parg1, *parg2; | |
13c9910f | 3052 | enum machine_mode *pmode1, *pmode2; |
7afe21cc RK |
3053 | { |
3054 | rtx arg1, arg2; | |
3055 | ||
3056 | arg1 = *parg1, arg2 = *parg2; | |
3057 | ||
3058 | /* If ARG2 is const0_rtx, see what ARG1 is equivalent to. */ | |
3059 | ||
b2796a4b | 3060 | while (arg2 == CONST0_RTX (GET_MODE (arg1))) |
7afe21cc RK |
3061 | { |
3062 | /* Set non-zero when we find something of interest. */ | |
3063 | rtx x = 0; | |
3064 | int reverse_code = 0; | |
3065 | struct table_elt *p = 0; | |
3066 | ||
3067 | /* If arg1 is a COMPARE, extract the comparison arguments from it. | |
3068 | On machines with CC0, this is the only case that can occur, since | |
3069 | fold_rtx will return the COMPARE or item being compared with zero | |
3070 | when given CC0. */ | |
3071 | ||
3072 | if (GET_CODE (arg1) == COMPARE && arg2 == const0_rtx) | |
3073 | x = arg1; | |
3074 | ||
3075 | /* If ARG1 is a comparison operator and CODE is testing for | |
3076 | STORE_FLAG_VALUE, get the inner arguments. */ | |
3077 | ||
3078 | else if (GET_RTX_CLASS (GET_CODE (arg1)) == '<') | |
3079 | { | |
c610adec RK |
3080 | if (code == NE |
3081 | || (GET_MODE_CLASS (GET_MODE (arg1)) == MODE_INT | |
3082 | && code == LT && STORE_FLAG_VALUE == -1) | |
3083 | #ifdef FLOAT_STORE_FLAG_VALUE | |
3084 | || (GET_MODE_CLASS (GET_MODE (arg1)) == MODE_FLOAT | |
3085 | && FLOAT_STORE_FLAG_VALUE < 0) | |
3086 | #endif | |
3087 | ) | |
7afe21cc | 3088 | x = arg1; |
c610adec RK |
3089 | else if (code == EQ |
3090 | || (GET_MODE_CLASS (GET_MODE (arg1)) == MODE_INT | |
3091 | && code == GE && STORE_FLAG_VALUE == -1) | |
3092 | #ifdef FLOAT_STORE_FLAG_VALUE | |
3093 | || (GET_MODE_CLASS (GET_MODE (arg1)) == MODE_FLOAT | |
3094 | && FLOAT_STORE_FLAG_VALUE < 0) | |
3095 | #endif | |
3096 | ) | |
7afe21cc RK |
3097 | x = arg1, reverse_code = 1; |
3098 | } | |
3099 | ||
3100 | /* ??? We could also check for | |
3101 | ||
3102 | (ne (and (eq (...) (const_int 1))) (const_int 0)) | |
3103 | ||
3104 | and related forms, but let's wait until we see them occurring. */ | |
3105 | ||
3106 | if (x == 0) | |
3107 | /* Look up ARG1 in the hash table and see if it has an equivalence | |
3108 | that lets us see what is being compared. */ | |
3109 | p = lookup (arg1, safe_hash (arg1, GET_MODE (arg1)) % NBUCKETS, | |
3110 | GET_MODE (arg1)); | |
3111 | if (p) p = p->first_same_value; | |
3112 | ||
3113 | for (; p; p = p->next_same_value) | |
3114 | { | |
3115 | enum machine_mode inner_mode = GET_MODE (p->exp); | |
3116 | ||
3117 | /* If the entry isn't valid, skip it. */ | |
3118 | if (! exp_equiv_p (p->exp, p->exp, 1, 0)) | |
3119 | continue; | |
3120 | ||
3121 | if (GET_CODE (p->exp) == COMPARE | |
3122 | /* Another possibility is that this machine has a compare insn | |
3123 | that includes the comparison code. In that case, ARG1 would | |
3124 | be equivalent to a comparison operation that would set ARG1 to | |
3125 | either STORE_FLAG_VALUE or zero. If this is an NE operation, | |
3126 | ORIG_CODE is the actual comparison being done; if it is an EQ, | |
3127 | we must reverse ORIG_CODE. On machine with a negative value | |
3128 | for STORE_FLAG_VALUE, also look at LT and GE operations. */ | |
3129 | || ((code == NE | |
3130 | || (code == LT | |
c610adec | 3131 | && GET_MODE_CLASS (inner_mode) == MODE_INT |
906c4e36 RK |
3132 | && (GET_MODE_BITSIZE (inner_mode) |
3133 | <= HOST_BITS_PER_WIDE_INT) | |
7afe21cc | 3134 | && (STORE_FLAG_VALUE |
906c4e36 RK |
3135 | & ((HOST_WIDE_INT) 1 |
3136 | << (GET_MODE_BITSIZE (inner_mode) - 1)))) | |
c610adec RK |
3137 | #ifdef FLOAT_STORE_FLAG_VALUE |
3138 | || (code == LT | |
3139 | && GET_MODE_CLASS (inner_mode) == MODE_FLOAT | |
3140 | && FLOAT_STORE_FLAG_VALUE < 0) | |
3141 | #endif | |
3142 | ) | |
7afe21cc RK |
3143 | && GET_RTX_CLASS (GET_CODE (p->exp)) == '<')) |
3144 | { | |
3145 | x = p->exp; | |
3146 | break; | |
3147 | } | |
3148 | else if ((code == EQ | |
3149 | || (code == GE | |
c610adec | 3150 | && GET_MODE_CLASS (inner_mode) == MODE_INT |
906c4e36 RK |
3151 | && (GET_MODE_BITSIZE (inner_mode) |
3152 | <= HOST_BITS_PER_WIDE_INT) | |
7afe21cc | 3153 | && (STORE_FLAG_VALUE |
906c4e36 RK |
3154 | & ((HOST_WIDE_INT) 1 |
3155 | << (GET_MODE_BITSIZE (inner_mode) - 1)))) | |
c610adec RK |
3156 | #ifdef FLOAT_STORE_FLAG_VALUE |
3157 | || (code == GE | |
3158 | && GET_MODE_CLASS (inner_mode) == MODE_FLOAT | |
3159 | && FLOAT_STORE_FLAG_VALUE < 0) | |
3160 | #endif | |
3161 | ) | |
7afe21cc RK |
3162 | && GET_RTX_CLASS (GET_CODE (p->exp)) == '<') |
3163 | { | |
3164 | reverse_code = 1; | |
3165 | x = p->exp; | |
3166 | break; | |
3167 | } | |
3168 | ||
3169 | /* If this is fp + constant, the equivalent is a better operand since | |
3170 | it may let us predict the value of the comparison. */ | |
3171 | else if (NONZERO_BASE_PLUS_P (p->exp)) | |
3172 | { | |
3173 | arg1 = p->exp; | |
3174 | continue; | |
3175 | } | |
3176 | } | |
3177 | ||
3178 | /* If we didn't find a useful equivalence for ARG1, we are done. | |
3179 | Otherwise, set up for the next iteration. */ | |
3180 | if (x == 0) | |
3181 | break; | |
3182 | ||
3183 | arg1 = XEXP (x, 0), arg2 = XEXP (x, 1); | |
3184 | if (GET_RTX_CLASS (GET_CODE (x)) == '<') | |
3185 | code = GET_CODE (x); | |
3186 | ||
3187 | if (reverse_code) | |
3188 | code = reverse_condition (code); | |
3189 | } | |
3190 | ||
13c9910f RS |
3191 | /* Return our results. Return the modes from before fold_rtx |
3192 | because fold_rtx might produce const_int, and then it's too late. */ | |
3193 | *pmode1 = GET_MODE (arg1), *pmode2 = GET_MODE (arg2); | |
7afe21cc RK |
3194 | *parg1 = fold_rtx (arg1, 0), *parg2 = fold_rtx (arg2, 0); |
3195 | ||
3196 | return code; | |
3197 | } | |
3198 | \f | |
3199 | /* Try to simplify a unary operation CODE whose output mode is to be | |
3200 | MODE with input operand OP whose mode was originally OP_MODE. | |
3201 | Return zero if no simplification can be made. */ | |
3202 | ||
3203 | rtx | |
3204 | simplify_unary_operation (code, mode, op, op_mode) | |
3205 | enum rtx_code code; | |
3206 | enum machine_mode mode; | |
3207 | rtx op; | |
3208 | enum machine_mode op_mode; | |
3209 | { | |
3210 | register int width = GET_MODE_BITSIZE (mode); | |
3211 | ||
3212 | /* The order of these tests is critical so that, for example, we don't | |
3213 | check the wrong mode (input vs. output) for a conversion operation, | |
3214 | such as FIX. At some point, this should be simplified. */ | |
3215 | ||
62c0ea12 | 3216 | #if !defined(REAL_IS_NOT_DOUBLE) || defined(REAL_ARITHMETIC) |
7afe21cc | 3217 | |
62c0ea12 RK |
3218 | if (code == FLOAT && GET_MODE (op) == VOIDmode |
3219 | && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT)) | |
7afe21cc | 3220 | { |
62c0ea12 | 3221 | HOST_WIDE_INT hv, lv; |
7afe21cc RK |
3222 | REAL_VALUE_TYPE d; |
3223 | ||
62c0ea12 RK |
3224 | if (GET_CODE (op) == CONST_INT) |
3225 | lv = INTVAL (op), hv = INTVAL (op) < 0 ? -1 : 0; | |
3226 | else | |
7ac4a266 | 3227 | lv = CONST_DOUBLE_LOW (op), hv = CONST_DOUBLE_HIGH (op); |
7afe21cc RK |
3228 | |
3229 | #ifdef REAL_ARITHMETIC | |
2ebcccf3 | 3230 | REAL_VALUE_FROM_INT (d, lv, hv, mode); |
7afe21cc | 3231 | #else |
62c0ea12 | 3232 | if (hv < 0) |
7afe21cc | 3233 | { |
62c0ea12 | 3234 | d = (double) (~ hv); |
906c4e36 RK |
3235 | d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) |
3236 | * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))); | |
62c0ea12 | 3237 | d += (double) (unsigned HOST_WIDE_INT) (~ lv); |
7afe21cc RK |
3238 | d = (- d - 1.0); |
3239 | } | |
3240 | else | |
3241 | { | |
62c0ea12 | 3242 | d = (double) hv; |
906c4e36 RK |
3243 | d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) |
3244 | * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))); | |
62c0ea12 | 3245 | d += (double) (unsigned HOST_WIDE_INT) lv; |
7afe21cc RK |
3246 | } |
3247 | #endif /* REAL_ARITHMETIC */ | |
940fd0b5 | 3248 | d = real_value_truncate (mode, d); |
7afe21cc RK |
3249 | return CONST_DOUBLE_FROM_REAL_VALUE (d, mode); |
3250 | } | |
62c0ea12 RK |
3251 | else if (code == UNSIGNED_FLOAT && GET_MODE (op) == VOIDmode |
3252 | && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT)) | |
7afe21cc | 3253 | { |
62c0ea12 | 3254 | HOST_WIDE_INT hv, lv; |
7afe21cc RK |
3255 | REAL_VALUE_TYPE d; |
3256 | ||
62c0ea12 RK |
3257 | if (GET_CODE (op) == CONST_INT) |
3258 | lv = INTVAL (op), hv = INTVAL (op) < 0 ? -1 : 0; | |
3259 | else | |
7ac4a266 | 3260 | lv = CONST_DOUBLE_LOW (op), hv = CONST_DOUBLE_HIGH (op); |
62c0ea12 | 3261 | |
a9c6464d RK |
3262 | if (op_mode == VOIDmode) |
3263 | { | |
3264 | /* We don't know how to interpret negative-looking numbers in | |
3265 | this case, so don't try to fold those. */ | |
3266 | if (hv < 0) | |
3267 | return 0; | |
3268 | } | |
3269 | else if (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT * 2) | |
62c0ea12 RK |
3270 | ; |
3271 | else | |
3272 | hv = 0, lv &= GET_MODE_MASK (op_mode); | |
3273 | ||
7afe21cc | 3274 | #ifdef REAL_ARITHMETIC |
2ebcccf3 | 3275 | REAL_VALUE_FROM_UNSIGNED_INT (d, lv, hv, mode); |
7afe21cc | 3276 | #else |
62c0ea12 | 3277 | |
138cec59 | 3278 | d = (double) (unsigned HOST_WIDE_INT) hv; |
906c4e36 RK |
3279 | d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) |
3280 | * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))); | |
62c0ea12 | 3281 | d += (double) (unsigned HOST_WIDE_INT) lv; |
7afe21cc | 3282 | #endif /* REAL_ARITHMETIC */ |
940fd0b5 | 3283 | d = real_value_truncate (mode, d); |
7afe21cc RK |
3284 | return CONST_DOUBLE_FROM_REAL_VALUE (d, mode); |
3285 | } | |
3286 | #endif | |
3287 | ||
f89e32e9 RK |
3288 | if (GET_CODE (op) == CONST_INT |
3289 | && width <= HOST_BITS_PER_WIDE_INT && width > 0) | |
7afe21cc | 3290 | { |
906c4e36 RK |
3291 | register HOST_WIDE_INT arg0 = INTVAL (op); |
3292 | register HOST_WIDE_INT val; | |
7afe21cc RK |
3293 | |
3294 | switch (code) | |
3295 | { | |
3296 | case NOT: | |
3297 | val = ~ arg0; | |
3298 | break; | |
3299 | ||
3300 | case NEG: | |
3301 | val = - arg0; | |
3302 | break; | |
3303 | ||
3304 | case ABS: | |
3305 | val = (arg0 >= 0 ? arg0 : - arg0); | |
3306 | break; | |
3307 | ||
3308 | case FFS: | |
3309 | /* Don't use ffs here. Instead, get low order bit and then its | |
3310 | number. If arg0 is zero, this will return 0, as desired. */ | |
3311 | arg0 &= GET_MODE_MASK (mode); | |
3312 | val = exact_log2 (arg0 & (- arg0)) + 1; | |
3313 | break; | |
3314 | ||
3315 | case TRUNCATE: | |
3316 | val = arg0; | |
3317 | break; | |
3318 | ||
3319 | case ZERO_EXTEND: | |
3320 | if (op_mode == VOIDmode) | |
3321 | op_mode = mode; | |
82a5e898 | 3322 | if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT) |
d80e9fd7 RS |
3323 | { |
3324 | /* If we were really extending the mode, | |
3325 | we would have to distinguish between zero-extension | |
3326 | and sign-extension. */ | |
3327 | if (width != GET_MODE_BITSIZE (op_mode)) | |
3328 | abort (); | |
3329 | val = arg0; | |
3330 | } | |
82a5e898 CH |
3331 | else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT) |
3332 | val = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode)); | |
7afe21cc RK |
3333 | else |
3334 | return 0; | |
3335 | break; | |
3336 | ||
3337 | case SIGN_EXTEND: | |
3338 | if (op_mode == VOIDmode) | |
3339 | op_mode = mode; | |
82a5e898 | 3340 | if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT) |
d80e9fd7 RS |
3341 | { |
3342 | /* If we were really extending the mode, | |
3343 | we would have to distinguish between zero-extension | |
3344 | and sign-extension. */ | |
3345 | if (width != GET_MODE_BITSIZE (op_mode)) | |
3346 | abort (); | |
3347 | val = arg0; | |
3348 | } | |
f12564b4 | 3349 | else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT) |
7afe21cc | 3350 | { |
82a5e898 CH |
3351 | val |
3352 | = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode)); | |
3353 | if (val | |
3354 | & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (op_mode) - 1))) | |
3355 | val -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode); | |
7afe21cc RK |
3356 | } |
3357 | else | |
3358 | return 0; | |
3359 | break; | |
3360 | ||
d45cf215 RS |
3361 | case SQRT: |
3362 | return 0; | |
3363 | ||
7afe21cc RK |
3364 | default: |
3365 | abort (); | |
3366 | } | |
3367 | ||
7e4ce834 | 3368 | val = trunc_int_for_mode (val, mode); |
737e7965 | 3369 | |
906c4e36 | 3370 | return GEN_INT (val); |
7afe21cc RK |
3371 | } |
3372 | ||
3373 | /* We can do some operations on integer CONST_DOUBLEs. Also allow | |
0f41302f | 3374 | for a DImode operation on a CONST_INT. */ |
8e0ac43b | 3375 | else if (GET_MODE (op) == VOIDmode && width <= HOST_BITS_PER_INT * 2 |
7afe21cc RK |
3376 | && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT)) |
3377 | { | |
906c4e36 | 3378 | HOST_WIDE_INT l1, h1, lv, hv; |
7afe21cc RK |
3379 | |
3380 | if (GET_CODE (op) == CONST_DOUBLE) | |
3381 | l1 = CONST_DOUBLE_LOW (op), h1 = CONST_DOUBLE_HIGH (op); | |
3382 | else | |
3383 | l1 = INTVAL (op), h1 = l1 < 0 ? -1 : 0; | |
3384 | ||
3385 | switch (code) | |
3386 | { | |
3387 | case NOT: | |
3388 | lv = ~ l1; | |
3389 | hv = ~ h1; | |
3390 | break; | |
3391 | ||
3392 | case NEG: | |
3393 | neg_double (l1, h1, &lv, &hv); | |
3394 | break; | |
3395 | ||
3396 | case ABS: | |
3397 | if (h1 < 0) | |
3398 | neg_double (l1, h1, &lv, &hv); | |
3399 | else | |
3400 | lv = l1, hv = h1; | |
3401 | break; | |
3402 | ||
3403 | case FFS: | |
3404 | hv = 0; | |
3405 | if (l1 == 0) | |
906c4e36 | 3406 | lv = HOST_BITS_PER_WIDE_INT + exact_log2 (h1 & (-h1)) + 1; |
7afe21cc RK |
3407 | else |
3408 | lv = exact_log2 (l1 & (-l1)) + 1; | |
3409 | break; | |
3410 | ||
3411 | case TRUNCATE: | |
8e0ac43b | 3412 | /* This is just a change-of-mode, so do nothing. */ |
d50d63c0 | 3413 | lv = l1, hv = h1; |
7afe21cc RK |
3414 | break; |
3415 | ||
f72aed24 RS |
3416 | case ZERO_EXTEND: |
3417 | if (op_mode == VOIDmode | |
906c4e36 | 3418 | || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT) |
f72aed24 RS |
3419 | return 0; |
3420 | ||
3421 | hv = 0; | |
3422 | lv = l1 & GET_MODE_MASK (op_mode); | |
3423 | break; | |
3424 | ||
3425 | case SIGN_EXTEND: | |
3426 | if (op_mode == VOIDmode | |
906c4e36 | 3427 | || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT) |
f72aed24 RS |
3428 | return 0; |
3429 | else | |
3430 | { | |
3431 | lv = l1 & GET_MODE_MASK (op_mode); | |
906c4e36 RK |
3432 | if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT |
3433 | && (lv & ((HOST_WIDE_INT) 1 | |
3434 | << (GET_MODE_BITSIZE (op_mode) - 1))) != 0) | |
3435 | lv -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode); | |
f72aed24 | 3436 | |
906c4e36 | 3437 | hv = (lv < 0) ? ~ (HOST_WIDE_INT) 0 : 0; |
f72aed24 RS |
3438 | } |
3439 | break; | |
3440 | ||
d45cf215 RS |
3441 | case SQRT: |
3442 | return 0; | |
3443 | ||
7afe21cc RK |
3444 | default: |
3445 | return 0; | |
3446 | } | |
3447 | ||
3448 | return immed_double_const (lv, hv, mode); | |
3449 | } | |
3450 | ||
3451 | #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC) | |
3452 | else if (GET_CODE (op) == CONST_DOUBLE | |
3453 | && GET_MODE_CLASS (mode) == MODE_FLOAT) | |
3454 | { | |
3455 | REAL_VALUE_TYPE d; | |
3456 | jmp_buf handler; | |
3457 | rtx x; | |
3458 | ||
3459 | if (setjmp (handler)) | |
3460 | /* There used to be a warning here, but that is inadvisable. | |
3461 | People may want to cause traps, and the natural way | |
3462 | to do it should not get a warning. */ | |
3463 | return 0; | |
3464 | ||
3465 | set_float_handler (handler); | |
3466 | ||
3467 | REAL_VALUE_FROM_CONST_DOUBLE (d, op); | |
3468 | ||
3469 | switch (code) | |
3470 | { | |
3471 | case NEG: | |
3472 | d = REAL_VALUE_NEGATE (d); | |
3473 | break; | |
3474 | ||
3475 | case ABS: | |
8b3686ed | 3476 | if (REAL_VALUE_NEGATIVE (d)) |
7afe21cc RK |
3477 | d = REAL_VALUE_NEGATE (d); |
3478 | break; | |
3479 | ||
3480 | case FLOAT_TRUNCATE: | |
d3159aee | 3481 | d = real_value_truncate (mode, d); |
7afe21cc RK |
3482 | break; |
3483 | ||
3484 | case FLOAT_EXTEND: | |
3485 | /* All this does is change the mode. */ | |
3486 | break; | |
3487 | ||
3488 | case FIX: | |
d3159aee | 3489 | d = REAL_VALUE_RNDZINT (d); |
7afe21cc RK |
3490 | break; |
3491 | ||
3492 | case UNSIGNED_FIX: | |
d3159aee | 3493 | d = REAL_VALUE_UNSIGNED_RNDZINT (d); |
7afe21cc RK |
3494 | break; |
3495 | ||
d45cf215 RS |
3496 | case SQRT: |
3497 | return 0; | |
3498 | ||
7afe21cc RK |
3499 | default: |
3500 | abort (); | |
3501 | } | |
3502 | ||
560c94a2 | 3503 | x = CONST_DOUBLE_FROM_REAL_VALUE (d, mode); |
906c4e36 | 3504 | set_float_handler (NULL_PTR); |
7afe21cc RK |
3505 | return x; |
3506 | } | |
8e0ac43b RK |
3507 | |
3508 | else if (GET_CODE (op) == CONST_DOUBLE | |
3509 | && GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT | |
3510 | && GET_MODE_CLASS (mode) == MODE_INT | |
906c4e36 | 3511 | && width <= HOST_BITS_PER_WIDE_INT && width > 0) |
7afe21cc RK |
3512 | { |
3513 | REAL_VALUE_TYPE d; | |
3514 | jmp_buf handler; | |
906c4e36 | 3515 | HOST_WIDE_INT val; |
7afe21cc RK |
3516 | |
3517 | if (setjmp (handler)) | |
3518 | return 0; | |
3519 | ||
3520 | set_float_handler (handler); | |
3521 | ||
3522 | REAL_VALUE_FROM_CONST_DOUBLE (d, op); | |
3523 | ||
3524 | switch (code) | |
3525 | { | |
3526 | case FIX: | |
3527 | val = REAL_VALUE_FIX (d); | |
3528 | break; | |
3529 | ||
3530 | case UNSIGNED_FIX: | |
3531 | val = REAL_VALUE_UNSIGNED_FIX (d); | |
3532 | break; | |
3533 | ||
3534 | default: | |
3535 | abort (); | |
3536 | } | |
3537 | ||
906c4e36 | 3538 | set_float_handler (NULL_PTR); |
7afe21cc | 3539 | |
7e4ce834 | 3540 | val = trunc_int_for_mode (val, mode); |
ad89d6f6 | 3541 | |
906c4e36 | 3542 | return GEN_INT (val); |
7afe21cc RK |
3543 | } |
3544 | #endif | |
a6acbe15 RS |
3545 | /* This was formerly used only for non-IEEE float. |
3546 | eggert@twinsun.com says it is safe for IEEE also. */ | |
3547 | else | |
7afe21cc RK |
3548 | { |
3549 | /* There are some simplifications we can do even if the operands | |
a6acbe15 | 3550 | aren't constant. */ |
7afe21cc RK |
3551 | switch (code) |
3552 | { | |
3553 | case NEG: | |
3554 | case NOT: | |
3555 | /* (not (not X)) == X, similarly for NEG. */ | |
3556 | if (GET_CODE (op) == code) | |
3557 | return XEXP (op, 0); | |
3558 | break; | |
3559 | ||
3560 | case SIGN_EXTEND: | |
3561 | /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2)))) | |
3562 | becomes just the MINUS if its mode is MODE. This allows | |
3563 | folding switch statements on machines using casesi (such as | |
3564 | the Vax). */ | |
3565 | if (GET_CODE (op) == TRUNCATE | |
3566 | && GET_MODE (XEXP (op, 0)) == mode | |
3567 | && GET_CODE (XEXP (op, 0)) == MINUS | |
3568 | && GET_CODE (XEXP (XEXP (op, 0), 0)) == LABEL_REF | |
3569 | && GET_CODE (XEXP (XEXP (op, 0), 1)) == LABEL_REF) | |
3570 | return XEXP (op, 0); | |
cceb347c RK |
3571 | |
3572 | #ifdef POINTERS_EXTEND_UNSIGNED | |
3573 | if (! POINTERS_EXTEND_UNSIGNED | |
3574 | && mode == Pmode && GET_MODE (op) == ptr_mode | |
3575 | && CONSTANT_P (op)) | |
3576 | return convert_memory_address (Pmode, op); | |
3577 | #endif | |
3578 | break; | |
3579 | ||
3580 | #ifdef POINTERS_EXTEND_UNSIGNED | |
3581 | case ZERO_EXTEND: | |
3582 | if (POINTERS_EXTEND_UNSIGNED | |
3583 | && mode == Pmode && GET_MODE (op) == ptr_mode | |
3584 | && CONSTANT_P (op)) | |
3585 | return convert_memory_address (Pmode, op); | |
7afe21cc | 3586 | break; |
cceb347c | 3587 | #endif |
e9a25f70 JL |
3588 | |
3589 | default: | |
3590 | break; | |
7afe21cc RK |
3591 | } |
3592 | ||
3593 | return 0; | |
3594 | } | |
7afe21cc RK |
3595 | } |
3596 | \f | |
3597 | /* Simplify a binary operation CODE with result mode MODE, operating on OP0 | |
3598 | and OP1. Return 0 if no simplification is possible. | |
3599 | ||
3600 | Don't use this for relational operations such as EQ or LT. | |
3601 | Use simplify_relational_operation instead. */ | |
3602 | ||
3603 | rtx | |
3604 | simplify_binary_operation (code, mode, op0, op1) | |
3605 | enum rtx_code code; | |
3606 | enum machine_mode mode; | |
3607 | rtx op0, op1; | |
3608 | { | |
906c4e36 RK |
3609 | register HOST_WIDE_INT arg0, arg1, arg0s, arg1s; |
3610 | HOST_WIDE_INT val; | |
7afe21cc | 3611 | int width = GET_MODE_BITSIZE (mode); |
96b0e481 | 3612 | rtx tem; |
7afe21cc RK |
3613 | |
3614 | /* Relational operations don't work here. We must know the mode | |
3615 | of the operands in order to do the comparison correctly. | |
3616 | Assuming a full word can give incorrect results. | |
3617 | Consider comparing 128 with -128 in QImode. */ | |
3618 | ||
3619 | if (GET_RTX_CLASS (code) == '<') | |
3620 | abort (); | |
3621 | ||
3622 | #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC) | |
3623 | if (GET_MODE_CLASS (mode) == MODE_FLOAT | |
3624 | && GET_CODE (op0) == CONST_DOUBLE && GET_CODE (op1) == CONST_DOUBLE | |
3625 | && mode == GET_MODE (op0) && mode == GET_MODE (op1)) | |
3626 | { | |
3627 | REAL_VALUE_TYPE f0, f1, value; | |
3628 | jmp_buf handler; | |
3629 | ||
3630 | if (setjmp (handler)) | |
3631 | return 0; | |
3632 | ||
3633 | set_float_handler (handler); | |
3634 | ||
3635 | REAL_VALUE_FROM_CONST_DOUBLE (f0, op0); | |
3636 | REAL_VALUE_FROM_CONST_DOUBLE (f1, op1); | |
5352b11a RS |
3637 | f0 = real_value_truncate (mode, f0); |
3638 | f1 = real_value_truncate (mode, f1); | |
7afe21cc RK |
3639 | |
3640 | #ifdef REAL_ARITHMETIC | |
956d6950 JL |
3641 | #ifndef REAL_INFINITY |
3642 | if (code == DIV && REAL_VALUES_EQUAL (f1, dconst0)) | |
3643 | return 0; | |
3644 | #endif | |
d3159aee | 3645 | REAL_ARITHMETIC (value, rtx_to_tree_code (code), f0, f1); |
7afe21cc RK |
3646 | #else |
3647 | switch (code) | |
3648 | { | |
3649 | case PLUS: | |
3650 | value = f0 + f1; | |
3651 | break; | |
3652 | case MINUS: | |
3653 | value = f0 - f1; | |
3654 | break; | |
3655 | case MULT: | |
3656 | value = f0 * f1; | |
3657 | break; | |
3658 | case DIV: | |
3659 | #ifndef REAL_INFINITY | |
3660 | if (f1 == 0) | |
21d12b80 | 3661 | return 0; |
7afe21cc RK |
3662 | #endif |
3663 | value = f0 / f1; | |
3664 | break; | |
3665 | case SMIN: | |
3666 | value = MIN (f0, f1); | |
3667 | break; | |
3668 | case SMAX: | |
3669 | value = MAX (f0, f1); | |
3670 | break; | |
3671 | default: | |
3672 | abort (); | |
3673 | } | |
3674 | #endif | |
3675 | ||
5352b11a | 3676 | value = real_value_truncate (mode, value); |
831522a4 | 3677 | set_float_handler (NULL_PTR); |
560c94a2 | 3678 | return CONST_DOUBLE_FROM_REAL_VALUE (value, mode); |
7afe21cc | 3679 | } |
6076248a | 3680 | #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */ |
7afe21cc RK |
3681 | |
3682 | /* We can fold some multi-word operations. */ | |
6076248a | 3683 | if (GET_MODE_CLASS (mode) == MODE_INT |
33085906 | 3684 | && width == HOST_BITS_PER_WIDE_INT * 2 |
fe873240 | 3685 | && (GET_CODE (op0) == CONST_DOUBLE || GET_CODE (op0) == CONST_INT) |
6076248a | 3686 | && (GET_CODE (op1) == CONST_DOUBLE || GET_CODE (op1) == CONST_INT)) |
7afe21cc | 3687 | { |
906c4e36 | 3688 | HOST_WIDE_INT l1, l2, h1, h2, lv, hv; |
7afe21cc | 3689 | |
fe873240 RK |
3690 | if (GET_CODE (op0) == CONST_DOUBLE) |
3691 | l1 = CONST_DOUBLE_LOW (op0), h1 = CONST_DOUBLE_HIGH (op0); | |
3692 | else | |
3693 | l1 = INTVAL (op0), h1 = l1 < 0 ? -1 : 0; | |
7afe21cc RK |
3694 | |
3695 | if (GET_CODE (op1) == CONST_DOUBLE) | |
3696 | l2 = CONST_DOUBLE_LOW (op1), h2 = CONST_DOUBLE_HIGH (op1); | |
3697 | else | |
3698 | l2 = INTVAL (op1), h2 = l2 < 0 ? -1 : 0; | |
3699 | ||
3700 | switch (code) | |
3701 | { | |
3702 | case MINUS: | |
3703 | /* A - B == A + (-B). */ | |
3704 | neg_double (l2, h2, &lv, &hv); | |
3705 | l2 = lv, h2 = hv; | |
3706 | ||
0f41302f | 3707 | /* .. fall through ... */ |
7afe21cc RK |
3708 | |
3709 | case PLUS: | |
3710 | add_double (l1, h1, l2, h2, &lv, &hv); | |
3711 | break; | |
3712 | ||
3713 | case MULT: | |
3714 | mul_double (l1, h1, l2, h2, &lv, &hv); | |
3715 | break; | |
3716 | ||
3717 | case DIV: case MOD: case UDIV: case UMOD: | |
3718 | /* We'd need to include tree.h to do this and it doesn't seem worth | |
3719 | it. */ | |
3720 | return 0; | |
3721 | ||
3722 | case AND: | |
3723 | lv = l1 & l2, hv = h1 & h2; | |
3724 | break; | |
3725 | ||
3726 | case IOR: | |
3727 | lv = l1 | l2, hv = h1 | h2; | |
3728 | break; | |
3729 | ||
3730 | case XOR: | |
3731 | lv = l1 ^ l2, hv = h1 ^ h2; | |
3732 | break; | |
3733 | ||
3734 | case SMIN: | |
906c4e36 RK |
3735 | if (h1 < h2 |
3736 | || (h1 == h2 | |
3737 | && ((unsigned HOST_WIDE_INT) l1 | |
3738 | < (unsigned HOST_WIDE_INT) l2))) | |
7afe21cc RK |
3739 | lv = l1, hv = h1; |
3740 | else | |
3741 | lv = l2, hv = h2; | |
3742 | break; | |
3743 | ||
3744 | case SMAX: | |
906c4e36 RK |
3745 | if (h1 > h2 |
3746 | || (h1 == h2 | |
3747 | && ((unsigned HOST_WIDE_INT) l1 | |
3748 | > (unsigned HOST_WIDE_INT) l2))) | |
7afe21cc RK |
3749 | lv = l1, hv = h1; |
3750 | else | |
3751 | lv = l2, hv = h2; | |
3752 | break; | |
3753 | ||
3754 | case UMIN: | |
906c4e36 RK |
3755 | if ((unsigned HOST_WIDE_INT) h1 < (unsigned HOST_WIDE_INT) h2 |
3756 | || (h1 == h2 | |
3757 | && ((unsigned HOST_WIDE_INT) l1 | |
3758 | < (unsigned HOST_WIDE_INT) l2))) | |
7afe21cc RK |
3759 | lv = l1, hv = h1; |
3760 | else | |
3761 | lv = l2, hv = h2; | |
3762 | break; | |
3763 | ||
3764 | case UMAX: | |
906c4e36 RK |
3765 | if ((unsigned HOST_WIDE_INT) h1 > (unsigned HOST_WIDE_INT) h2 |
3766 | || (h1 == h2 | |
3767 | && ((unsigned HOST_WIDE_INT) l1 | |
3768 | > (unsigned HOST_WIDE_INT) l2))) | |
7afe21cc RK |
3769 | lv = l1, hv = h1; |
3770 | else | |
3771 | lv = l2, hv = h2; | |
3772 | break; | |
3773 | ||
3774 | case LSHIFTRT: case ASHIFTRT: | |
45620ed4 | 3775 | case ASHIFT: |
7afe21cc RK |
3776 | case ROTATE: case ROTATERT: |
3777 | #ifdef SHIFT_COUNT_TRUNCATED | |
85c0a556 RK |
3778 | if (SHIFT_COUNT_TRUNCATED) |
3779 | l2 &= (GET_MODE_BITSIZE (mode) - 1), h2 = 0; | |
7afe21cc RK |
3780 | #endif |
3781 | ||
3782 | if (h2 != 0 || l2 < 0 || l2 >= GET_MODE_BITSIZE (mode)) | |
3783 | return 0; | |
3784 | ||
3785 | if (code == LSHIFTRT || code == ASHIFTRT) | |
3786 | rshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv, | |
3787 | code == ASHIFTRT); | |
45620ed4 RK |
3788 | else if (code == ASHIFT) |
3789 | lshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv, 1); | |
7afe21cc RK |
3790 | else if (code == ROTATE) |
3791 | lrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv); | |
3792 | else /* code == ROTATERT */ | |
3793 | rrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv); | |
3794 | break; | |
3795 | ||
3796 | default: | |
3797 | return 0; | |
3798 | } | |
3799 | ||
3800 | return immed_double_const (lv, hv, mode); | |
3801 | } | |
7afe21cc RK |
3802 | |
3803 | if (GET_CODE (op0) != CONST_INT || GET_CODE (op1) != CONST_INT | |
906c4e36 | 3804 | || width > HOST_BITS_PER_WIDE_INT || width == 0) |
7afe21cc RK |
3805 | { |
3806 | /* Even if we can't compute a constant result, | |
3807 | there are some cases worth simplifying. */ | |
3808 | ||
3809 | switch (code) | |
3810 | { | |
3811 | case PLUS: | |
3812 | /* In IEEE floating point, x+0 is not the same as x. Similarly | |
3813 | for the other optimizations below. */ | |
3814 | if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT | |
a83afb65 | 3815 | && FLOAT_MODE_P (mode) && ! flag_fast_math) |
7afe21cc RK |
3816 | break; |
3817 | ||
3818 | if (op1 == CONST0_RTX (mode)) | |
3819 | return op0; | |
3820 | ||
7afe21cc RK |
3821 | /* ((-a) + b) -> (b - a) and similarly for (a + (-b)) */ |
3822 | if (GET_CODE (op0) == NEG) | |
96b0e481 | 3823 | return cse_gen_binary (MINUS, mode, op1, XEXP (op0, 0)); |
7afe21cc | 3824 | else if (GET_CODE (op1) == NEG) |
96b0e481 | 3825 | return cse_gen_binary (MINUS, mode, op0, XEXP (op1, 0)); |
7afe21cc | 3826 | |
96b0e481 RK |
3827 | /* Handle both-operands-constant cases. We can only add |
3828 | CONST_INTs to constants since the sum of relocatable symbols | |
fe873240 RK |
3829 | can't be handled by most assemblers. Don't add CONST_INT |
3830 | to CONST_INT since overflow won't be computed properly if wider | |
3831 | than HOST_BITS_PER_WIDE_INT. */ | |
7afe21cc | 3832 | |
fe873240 RK |
3833 | if (CONSTANT_P (op0) && GET_MODE (op0) != VOIDmode |
3834 | && GET_CODE (op1) == CONST_INT) | |
96b0e481 | 3835 | return plus_constant (op0, INTVAL (op1)); |
fe873240 RK |
3836 | else if (CONSTANT_P (op1) && GET_MODE (op1) != VOIDmode |
3837 | && GET_CODE (op0) == CONST_INT) | |
96b0e481 | 3838 | return plus_constant (op1, INTVAL (op0)); |
7afe21cc | 3839 | |
30d69925 RK |
3840 | /* See if this is something like X * C - X or vice versa or |
3841 | if the multiplication is written as a shift. If so, we can | |
3842 | distribute and make a new multiply, shift, or maybe just | |
3843 | have X (if C is 2 in the example above). But don't make | |
3844 | real multiply if we didn't have one before. */ | |
3845 | ||
3846 | if (! FLOAT_MODE_P (mode)) | |
3847 | { | |
3848 | HOST_WIDE_INT coeff0 = 1, coeff1 = 1; | |
3849 | rtx lhs = op0, rhs = op1; | |
3850 | int had_mult = 0; | |
3851 | ||
3852 | if (GET_CODE (lhs) == NEG) | |
3853 | coeff0 = -1, lhs = XEXP (lhs, 0); | |
3854 | else if (GET_CODE (lhs) == MULT | |
3855 | && GET_CODE (XEXP (lhs, 1)) == CONST_INT) | |
3856 | { | |
3857 | coeff0 = INTVAL (XEXP (lhs, 1)), lhs = XEXP (lhs, 0); | |
3858 | had_mult = 1; | |
3859 | } | |
3860 | else if (GET_CODE (lhs) == ASHIFT | |
3861 | && GET_CODE (XEXP (lhs, 1)) == CONST_INT | |
3862 | && INTVAL (XEXP (lhs, 1)) >= 0 | |
3863 | && INTVAL (XEXP (lhs, 1)) < HOST_BITS_PER_WIDE_INT) | |
3864 | { | |
3865 | coeff0 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (lhs, 1)); | |
3866 | lhs = XEXP (lhs, 0); | |
3867 | } | |
3868 | ||
3869 | if (GET_CODE (rhs) == NEG) | |
3870 | coeff1 = -1, rhs = XEXP (rhs, 0); | |
3871 | else if (GET_CODE (rhs) == MULT | |
3872 | && GET_CODE (XEXP (rhs, 1)) == CONST_INT) | |
3873 | { | |
3874 | coeff1 = INTVAL (XEXP (rhs, 1)), rhs = XEXP (rhs, 0); | |
3875 | had_mult = 1; | |
3876 | } | |
3877 | else if (GET_CODE (rhs) == ASHIFT | |
3878 | && GET_CODE (XEXP (rhs, 1)) == CONST_INT | |
3879 | && INTVAL (XEXP (rhs, 1)) >= 0 | |
3880 | && INTVAL (XEXP (rhs, 1)) < HOST_BITS_PER_WIDE_INT) | |
3881 | { | |
3882 | coeff1 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (rhs, 1)); | |
3883 | rhs = XEXP (rhs, 0); | |
3884 | } | |
3885 | ||
3886 | if (rtx_equal_p (lhs, rhs)) | |
3887 | { | |
3888 | tem = cse_gen_binary (MULT, mode, lhs, | |
3889 | GEN_INT (coeff0 + coeff1)); | |
3890 | return (GET_CODE (tem) == MULT && ! had_mult) ? 0 : tem; | |
3891 | } | |
3892 | } | |
3893 | ||
96b0e481 RK |
3894 | /* If one of the operands is a PLUS or a MINUS, see if we can |
3895 | simplify this by the associative law. | |
3896 | Don't use the associative law for floating point. | |
3897 | The inaccuracy makes it nonassociative, | |
3898 | and subtle programs can break if operations are associated. */ | |
7afe21cc | 3899 | |
cbf6a543 | 3900 | if (INTEGRAL_MODE_P (mode) |
96b0e481 RK |
3901 | && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS |
3902 | || GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS) | |
3903 | && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0) | |
3904 | return tem; | |
7afe21cc RK |
3905 | break; |
3906 | ||
3907 | case COMPARE: | |
3908 | #ifdef HAVE_cc0 | |
3909 | /* Convert (compare FOO (const_int 0)) to FOO unless we aren't | |
3910 | using cc0, in which case we want to leave it as a COMPARE | |
3911 | so we can distinguish it from a register-register-copy. | |
3912 | ||
3913 | In IEEE floating point, x-0 is not the same as x. */ | |
3914 | ||
3915 | if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT | |
a83afb65 | 3916 | || ! FLOAT_MODE_P (mode) || flag_fast_math) |
7afe21cc RK |
3917 | && op1 == CONST0_RTX (mode)) |
3918 | return op0; | |
3919 | #else | |
3920 | /* Do nothing here. */ | |
3921 | #endif | |
3922 | break; | |
3923 | ||
3924 | case MINUS: | |
21648b45 RK |
3925 | /* None of these optimizations can be done for IEEE |
3926 | floating point. */ | |
3927 | if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT | |
a83afb65 | 3928 | && FLOAT_MODE_P (mode) && ! flag_fast_math) |
21648b45 RK |
3929 | break; |
3930 | ||
a83afb65 RK |
3931 | /* We can't assume x-x is 0 even with non-IEEE floating point, |
3932 | but since it is zero except in very strange circumstances, we | |
3933 | will treat it as zero with -ffast-math. */ | |
7afe21cc RK |
3934 | if (rtx_equal_p (op0, op1) |
3935 | && ! side_effects_p (op0) | |
a83afb65 RK |
3936 | && (! FLOAT_MODE_P (mode) || flag_fast_math)) |
3937 | return CONST0_RTX (mode); | |
7afe21cc RK |
3938 | |
3939 | /* Change subtraction from zero into negation. */ | |
3940 | if (op0 == CONST0_RTX (mode)) | |
38a448ca | 3941 | return gen_rtx_NEG (mode, op1); |
7afe21cc | 3942 | |
96b0e481 RK |
3943 | /* (-1 - a) is ~a. */ |
3944 | if (op0 == constm1_rtx) | |
38a448ca | 3945 | return gen_rtx_NOT (mode, op1); |
96b0e481 | 3946 | |
7afe21cc RK |
3947 | /* Subtracting 0 has no effect. */ |
3948 | if (op1 == CONST0_RTX (mode)) | |
3949 | return op0; | |
3950 | ||
30d69925 RK |
3951 | /* See if this is something like X * C - X or vice versa or |
3952 | if the multiplication is written as a shift. If so, we can | |
3953 | distribute and make a new multiply, shift, or maybe just | |
3954 | have X (if C is 2 in the example above). But don't make | |
3955 | real multiply if we didn't have one before. */ | |
3956 | ||
3957 | if (! FLOAT_MODE_P (mode)) | |
3958 | { | |
3959 | HOST_WIDE_INT coeff0 = 1, coeff1 = 1; | |
3960 | rtx lhs = op0, rhs = op1; | |
3961 | int had_mult = 0; | |
3962 | ||
3963 | if (GET_CODE (lhs) == NEG) | |
3964 | coeff0 = -1, lhs = XEXP (lhs, 0); | |
3965 | else if (GET_CODE (lhs) == MULT | |
3966 | && GET_CODE (XEXP (lhs, 1)) == CONST_INT) | |
3967 | { | |
3968 | coeff0 = INTVAL (XEXP (lhs, 1)), lhs = XEXP (lhs, 0); | |
3969 | had_mult = 1; | |
3970 | } | |
3971 | else if (GET_CODE (lhs) == ASHIFT | |
3972 | && GET_CODE (XEXP (lhs, 1)) == CONST_INT | |
3973 | && INTVAL (XEXP (lhs, 1)) >= 0 | |
3974 | && INTVAL (XEXP (lhs, 1)) < HOST_BITS_PER_WIDE_INT) | |
3975 | { | |
3976 | coeff0 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (lhs, 1)); | |
3977 | lhs = XEXP (lhs, 0); | |
3978 | } | |
3979 | ||
3980 | if (GET_CODE (rhs) == NEG) | |
3981 | coeff1 = - 1, rhs = XEXP (rhs, 0); | |
3982 | else if (GET_CODE (rhs) == MULT | |
3983 | && GET_CODE (XEXP (rhs, 1)) == CONST_INT) | |
3984 | { | |
3985 | coeff1 = INTVAL (XEXP (rhs, 1)), rhs = XEXP (rhs, 0); | |
3986 | had_mult = 1; | |
3987 | } | |
3988 | else if (GET_CODE (rhs) == ASHIFT | |
3989 | && GET_CODE (XEXP (rhs, 1)) == CONST_INT | |
3990 | && INTVAL (XEXP (rhs, 1)) >= 0 | |
3991 | && INTVAL (XEXP (rhs, 1)) < HOST_BITS_PER_WIDE_INT) | |
3992 | { | |
3993 | coeff1 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (rhs, 1)); | |
3994 | rhs = XEXP (rhs, 0); | |
3995 | } | |
3996 | ||
3997 | if (rtx_equal_p (lhs, rhs)) | |
3998 | { | |
3999 | tem = cse_gen_binary (MULT, mode, lhs, | |
4000 | GEN_INT (coeff0 - coeff1)); | |
4001 | return (GET_CODE (tem) == MULT && ! had_mult) ? 0 : tem; | |
4002 | } | |
4003 | } | |
4004 | ||
7afe21cc RK |
4005 | /* (a - (-b)) -> (a + b). */ |
4006 | if (GET_CODE (op1) == NEG) | |
96b0e481 | 4007 | return cse_gen_binary (PLUS, mode, op0, XEXP (op1, 0)); |
7afe21cc | 4008 | |
96b0e481 RK |
4009 | /* If one of the operands is a PLUS or a MINUS, see if we can |
4010 | simplify this by the associative law. | |
4011 | Don't use the associative law for floating point. | |
7afe21cc RK |
4012 | The inaccuracy makes it nonassociative, |
4013 | and subtle programs can break if operations are associated. */ | |
7afe21cc | 4014 | |
cbf6a543 | 4015 | if (INTEGRAL_MODE_P (mode) |
96b0e481 RK |
4016 | && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS |
4017 | || GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS) | |
4018 | && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0) | |
4019 | return tem; | |
7afe21cc RK |
4020 | |
4021 | /* Don't let a relocatable value get a negative coeff. */ | |
b5a09c41 | 4022 | if (GET_CODE (op1) == CONST_INT && GET_MODE (op0) != VOIDmode) |
7afe21cc | 4023 | return plus_constant (op0, - INTVAL (op1)); |
29d72c4b TG |
4024 | |
4025 | /* (x - (x & y)) -> (x & ~y) */ | |
4026 | if (GET_CODE (op1) == AND) | |
4027 | { | |
4028 | if (rtx_equal_p (op0, XEXP (op1, 0))) | |
c5c76735 JL |
4029 | return cse_gen_binary (AND, mode, op0, |
4030 | gen_rtx_NOT (mode, XEXP (op1, 1))); | |
29d72c4b | 4031 | if (rtx_equal_p (op0, XEXP (op1, 1))) |
c5c76735 JL |
4032 | return cse_gen_binary (AND, mode, op0, |
4033 | gen_rtx_NOT (mode, XEXP (op1, 0))); | |
29d72c4b | 4034 | } |
7afe21cc RK |
4035 | break; |
4036 | ||
4037 | case MULT: | |
4038 | if (op1 == constm1_rtx) | |
4039 | { | |
96b0e481 | 4040 | tem = simplify_unary_operation (NEG, mode, op0, mode); |
7afe21cc | 4041 | |
38a448ca | 4042 | return tem ? tem : gen_rtx_NEG (mode, op0); |
7afe21cc RK |
4043 | } |
4044 | ||
4045 | /* In IEEE floating point, x*0 is not always 0. */ | |
4046 | if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT | |
a83afb65 | 4047 | || ! FLOAT_MODE_P (mode) || flag_fast_math) |
7afe21cc RK |
4048 | && op1 == CONST0_RTX (mode) |
4049 | && ! side_effects_p (op0)) | |
4050 | return op1; | |
4051 | ||
4052 | /* In IEEE floating point, x*1 is not equivalent to x for nans. | |
4053 | However, ANSI says we can drop signals, | |
4054 | so we can do this anyway. */ | |
4055 | if (op1 == CONST1_RTX (mode)) | |
4056 | return op0; | |
4057 | ||
c407b802 RK |
4058 | /* Convert multiply by constant power of two into shift unless |
4059 | we are still generating RTL. This test is a kludge. */ | |
7afe21cc | 4060 | if (GET_CODE (op1) == CONST_INT |
c407b802 | 4061 | && (val = exact_log2 (INTVAL (op1))) >= 0 |
2d917903 JW |
4062 | /* If the mode is larger than the host word size, and the |
4063 | uppermost bit is set, then this isn't a power of two due | |
4064 | to implicit sign extension. */ | |
4065 | && (width <= HOST_BITS_PER_WIDE_INT | |
4066 | || val != HOST_BITS_PER_WIDE_INT - 1) | |
c407b802 | 4067 | && ! rtx_equal_function_value_matters) |
38a448ca | 4068 | return gen_rtx_ASHIFT (mode, op0, GEN_INT (val)); |
7afe21cc RK |
4069 | |
4070 | if (GET_CODE (op1) == CONST_DOUBLE | |
4071 | && GET_MODE_CLASS (GET_MODE (op1)) == MODE_FLOAT) | |
4072 | { | |
4073 | REAL_VALUE_TYPE d; | |
5a3d4bef RK |
4074 | jmp_buf handler; |
4075 | int op1is2, op1ism1; | |
4076 | ||
4077 | if (setjmp (handler)) | |
4078 | return 0; | |
4079 | ||
4080 | set_float_handler (handler); | |
7afe21cc | 4081 | REAL_VALUE_FROM_CONST_DOUBLE (d, op1); |
5a3d4bef RK |
4082 | op1is2 = REAL_VALUES_EQUAL (d, dconst2); |
4083 | op1ism1 = REAL_VALUES_EQUAL (d, dconstm1); | |
4084 | set_float_handler (NULL_PTR); | |
7afe21cc RK |
4085 | |
4086 | /* x*2 is x+x and x*(-1) is -x */ | |
5a3d4bef | 4087 | if (op1is2 && GET_MODE (op0) == mode) |
38a448ca | 4088 | return gen_rtx_PLUS (mode, op0, copy_rtx (op0)); |
7afe21cc | 4089 | |
5a3d4bef | 4090 | else if (op1ism1 && GET_MODE (op0) == mode) |
38a448ca | 4091 | return gen_rtx_NEG (mode, op0); |
7afe21cc RK |
4092 | } |
4093 | break; | |
4094 | ||
4095 | case IOR: | |
4096 | if (op1 == const0_rtx) | |
4097 | return op0; | |
4098 | if (GET_CODE (op1) == CONST_INT | |
4099 | && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode)) | |
4100 | return op1; | |
4101 | if (rtx_equal_p (op0, op1) && ! side_effects_p (op0)) | |
4102 | return op0; | |
4103 | /* A | (~A) -> -1 */ | |
4104 | if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1)) | |
4105 | || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0))) | |
31dcf83f | 4106 | && ! side_effects_p (op0) |
8e7e5365 | 4107 | && GET_MODE_CLASS (mode) != MODE_CC) |
7afe21cc RK |
4108 | return constm1_rtx; |
4109 | break; | |
4110 | ||
4111 | case XOR: | |
4112 | if (op1 == const0_rtx) | |
4113 | return op0; | |
4114 | if (GET_CODE (op1) == CONST_INT | |
4115 | && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode)) | |
38a448ca | 4116 | return gen_rtx_NOT (mode, op0); |
31dcf83f | 4117 | if (op0 == op1 && ! side_effects_p (op0) |
8e7e5365 | 4118 | && GET_MODE_CLASS (mode) != MODE_CC) |
7afe21cc RK |
4119 | return const0_rtx; |
4120 | break; | |
4121 | ||
4122 | case AND: | |
4123 | if (op1 == const0_rtx && ! side_effects_p (op0)) | |
4124 | return const0_rtx; | |
4125 | if (GET_CODE (op1) == CONST_INT | |
4126 | && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode)) | |
4127 | return op0; | |
31dcf83f | 4128 | if (op0 == op1 && ! side_effects_p (op0) |
8e7e5365 | 4129 | && GET_MODE_CLASS (mode) != MODE_CC) |
7afe21cc RK |
4130 | return op0; |
4131 | /* A & (~A) -> 0 */ | |
4132 | if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1)) | |
4133 | || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0))) | |
709ab4fc | 4134 | && ! side_effects_p (op0) |
8e7e5365 | 4135 | && GET_MODE_CLASS (mode) != MODE_CC) |
7afe21cc RK |
4136 | return const0_rtx; |
4137 | break; | |
4138 | ||
4139 | case UDIV: | |
4140 | /* Convert divide by power of two into shift (divide by 1 handled | |
4141 | below). */ | |
4142 | if (GET_CODE (op1) == CONST_INT | |
4143 | && (arg1 = exact_log2 (INTVAL (op1))) > 0) | |
38a448ca | 4144 | return gen_rtx_LSHIFTRT (mode, op0, GEN_INT (arg1)); |
7afe21cc | 4145 | |
0f41302f | 4146 | /* ... fall through ... */ |
7afe21cc RK |
4147 | |
4148 | case DIV: | |
4149 | if (op1 == CONST1_RTX (mode)) | |
4150 | return op0; | |
e7a522ba RS |
4151 | |
4152 | /* In IEEE floating point, 0/x is not always 0. */ | |
4153 | if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT | |
a83afb65 | 4154 | || ! FLOAT_MODE_P (mode) || flag_fast_math) |
e7a522ba RS |
4155 | && op0 == CONST0_RTX (mode) |
4156 | && ! side_effects_p (op1)) | |
7afe21cc | 4157 | return op0; |
e7a522ba | 4158 | |
7afe21cc | 4159 | #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC) |
a83afb65 RK |
4160 | /* Change division by a constant into multiplication. Only do |
4161 | this with -ffast-math until an expert says it is safe in | |
4162 | general. */ | |
7afe21cc RK |
4163 | else if (GET_CODE (op1) == CONST_DOUBLE |
4164 | && GET_MODE_CLASS (GET_MODE (op1)) == MODE_FLOAT | |
a83afb65 RK |
4165 | && op1 != CONST0_RTX (mode) |
4166 | && flag_fast_math) | |
7afe21cc RK |
4167 | { |
4168 | REAL_VALUE_TYPE d; | |
4169 | REAL_VALUE_FROM_CONST_DOUBLE (d, op1); | |
a83afb65 RK |
4170 | |
4171 | if (! REAL_VALUES_EQUAL (d, dconst0)) | |
4172 | { | |
7afe21cc | 4173 | #if defined (REAL_ARITHMETIC) |
a83afb65 | 4174 | REAL_ARITHMETIC (d, rtx_to_tree_code (DIV), dconst1, d); |
38a448ca RH |
4175 | return gen_rtx_MULT (mode, op0, |
4176 | CONST_DOUBLE_FROM_REAL_VALUE (d, mode)); | |
7afe21cc | 4177 | #else |
c5c76735 JL |
4178 | return |
4179 | gen_rtx_MULT (mode, op0, | |
4180 | CONST_DOUBLE_FROM_REAL_VALUE (1./d, mode)); | |
7afe21cc | 4181 | #endif |
a83afb65 RK |
4182 | } |
4183 | } | |
7afe21cc RK |
4184 | #endif |
4185 | break; | |
4186 | ||
4187 | case UMOD: | |
4188 | /* Handle modulus by power of two (mod with 1 handled below). */ | |
4189 | if (GET_CODE (op1) == CONST_INT | |
4190 | && exact_log2 (INTVAL (op1)) > 0) | |
38a448ca | 4191 | return gen_rtx_AND (mode, op0, GEN_INT (INTVAL (op1) - 1)); |
7afe21cc | 4192 | |
0f41302f | 4193 | /* ... fall through ... */ |
7afe21cc RK |
4194 | |
4195 | case MOD: | |
4196 | if ((op0 == const0_rtx || op1 == const1_rtx) | |
4197 | && ! side_effects_p (op0) && ! side_effects_p (op1)) | |
4198 | return const0_rtx; | |
4199 | break; | |
4200 | ||
4201 | case ROTATERT: | |
4202 | case ROTATE: | |
4203 | /* Rotating ~0 always results in ~0. */ | |
906c4e36 | 4204 | if (GET_CODE (op0) == CONST_INT && width <= HOST_BITS_PER_WIDE_INT |
6a651371 | 4205 | && (unsigned HOST_WIDE_INT) INTVAL (op0) == GET_MODE_MASK (mode) |
7afe21cc RK |
4206 | && ! side_effects_p (op1)) |
4207 | return op0; | |
4208 | ||
0f41302f | 4209 | /* ... fall through ... */ |
7afe21cc | 4210 | |
7afe21cc RK |
4211 | case ASHIFT: |
4212 | case ASHIFTRT: | |
4213 | case LSHIFTRT: | |
4214 | if (op1 == const0_rtx) | |
4215 | return op0; | |
4216 | if (op0 == const0_rtx && ! side_effects_p (op1)) | |
4217 | return op0; | |
4218 | break; | |
4219 | ||
4220 | case SMIN: | |
906c4e36 RK |
4221 | if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (op1) == CONST_INT |
4222 | && INTVAL (op1) == (HOST_WIDE_INT) 1 << (width -1) | |
7afe21cc RK |
4223 | && ! side_effects_p (op0)) |
4224 | return op1; | |
4225 | else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0)) | |
4226 | return op0; | |
4227 | break; | |
4228 | ||
4229 | case SMAX: | |
906c4e36 | 4230 | if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (op1) == CONST_INT |
6a651371 | 4231 | && ((unsigned HOST_WIDE_INT) INTVAL (op1) |
dbbe6445 | 4232 | == (unsigned HOST_WIDE_INT) GET_MODE_MASK (mode) >> 1) |
7afe21cc RK |
4233 | && ! side_effects_p (op0)) |
4234 | return op1; | |
4235 | else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0)) | |
4236 | return op0; | |
4237 | break; | |
4238 | ||
4239 | case UMIN: | |
4240 | if (op1 == const0_rtx && ! side_effects_p (op0)) | |
4241 | return op1; | |
4242 | else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0)) | |
4243 | return op0; | |
4244 | break; | |
4245 | ||
4246 | case UMAX: | |
4247 | if (op1 == constm1_rtx && ! side_effects_p (op0)) | |
4248 | return op1; | |
4249 | else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0)) | |
4250 | return op0; | |
4251 | break; | |
4252 | ||
4253 | default: | |
4254 | abort (); | |
4255 | } | |
4256 | ||
4257 | return 0; | |
4258 | } | |
4259 | ||
4260 | /* Get the integer argument values in two forms: | |
4261 | zero-extended in ARG0, ARG1 and sign-extended in ARG0S, ARG1S. */ | |
4262 | ||
4263 | arg0 = INTVAL (op0); | |
4264 | arg1 = INTVAL (op1); | |
4265 | ||
906c4e36 | 4266 | if (width < HOST_BITS_PER_WIDE_INT) |
7afe21cc | 4267 | { |
906c4e36 RK |
4268 | arg0 &= ((HOST_WIDE_INT) 1 << width) - 1; |
4269 | arg1 &= ((HOST_WIDE_INT) 1 << width) - 1; | |
7afe21cc RK |
4270 | |
4271 | arg0s = arg0; | |
906c4e36 RK |
4272 | if (arg0s & ((HOST_WIDE_INT) 1 << (width - 1))) |
4273 | arg0s |= ((HOST_WIDE_INT) (-1) << width); | |
7afe21cc RK |
4274 | |
4275 | arg1s = arg1; | |
906c4e36 RK |
4276 | if (arg1s & ((HOST_WIDE_INT) 1 << (width - 1))) |
4277 | arg1s |= ((HOST_WIDE_INT) (-1) << width); | |
7afe21cc RK |
4278 | } |
4279 | else | |
4280 | { | |
4281 | arg0s = arg0; | |
4282 | arg1s = arg1; | |
4283 | } | |
4284 | ||
4285 | /* Compute the value of the arithmetic. */ | |
4286 | ||
4287 | switch (code) | |
4288 | { | |
4289 | case PLUS: | |
538b78e7 | 4290 | val = arg0s + arg1s; |
7afe21cc RK |
4291 | break; |
4292 | ||
4293 | case MINUS: | |
538b78e7 | 4294 | val = arg0s - arg1s; |
7afe21cc RK |
4295 | break; |
4296 | ||
4297 | case MULT: | |
4298 | val = arg0s * arg1s; | |
4299 | break; | |
4300 | ||
4301 | case DIV: | |
4302 | if (arg1s == 0) | |
4303 | return 0; | |
4304 | val = arg0s / arg1s; | |
4305 | break; | |
4306 | ||
4307 | case MOD: | |
4308 | if (arg1s == 0) | |
4309 | return 0; | |
4310 | val = arg0s % arg1s; | |
4311 | break; | |
4312 | ||
4313 | case UDIV: | |
4314 | if (arg1 == 0) | |
4315 | return 0; | |
906c4e36 | 4316 | val = (unsigned HOST_WIDE_INT) arg0 / arg1; |
7afe21cc RK |
4317 | break; |
4318 | ||
4319 | case UMOD: | |
4320 | if (arg1 == 0) | |
4321 | return 0; | |
906c4e36 | 4322 | val = (unsigned HOST_WIDE_INT) arg0 % arg1; |
7afe21cc RK |
4323 | break; |
4324 | ||
4325 | case AND: | |
4326 | val = arg0 & arg1; | |
4327 | break; | |
4328 | ||
4329 | case IOR: | |
4330 | val = arg0 | arg1; | |
4331 | break; | |
4332 | ||
4333 | case XOR: | |
4334 | val = arg0 ^ arg1; | |
4335 | break; | |
4336 | ||
4337 | case LSHIFTRT: | |
4338 | /* If shift count is undefined, don't fold it; let the machine do | |
4339 | what it wants. But truncate it if the machine will do that. */ | |
4340 | if (arg1 < 0) | |
4341 | return 0; | |
4342 | ||
4343 | #ifdef SHIFT_COUNT_TRUNCATED | |
85c0a556 | 4344 | if (SHIFT_COUNT_TRUNCATED) |
4d61f8c5 | 4345 | arg1 %= width; |
7afe21cc RK |
4346 | #endif |
4347 | ||
906c4e36 | 4348 | val = ((unsigned HOST_WIDE_INT) arg0) >> arg1; |
7afe21cc RK |
4349 | break; |
4350 | ||
4351 | case ASHIFT: | |
7afe21cc RK |
4352 | if (arg1 < 0) |
4353 | return 0; | |
4354 | ||
4355 | #ifdef SHIFT_COUNT_TRUNCATED | |
85c0a556 | 4356 | if (SHIFT_COUNT_TRUNCATED) |
4d61f8c5 | 4357 | arg1 %= width; |
7afe21cc RK |
4358 | #endif |
4359 | ||
906c4e36 | 4360 | val = ((unsigned HOST_WIDE_INT) arg0) << arg1; |
7afe21cc RK |
4361 | break; |
4362 | ||
4363 | case ASHIFTRT: | |
4364 | if (arg1 < 0) | |
4365 | return 0; | |
4366 | ||
4367 | #ifdef SHIFT_COUNT_TRUNCATED | |
85c0a556 | 4368 | if (SHIFT_COUNT_TRUNCATED) |
4d61f8c5 | 4369 | arg1 %= width; |
7afe21cc RK |
4370 | #endif |
4371 | ||
7afe21cc | 4372 | val = arg0s >> arg1; |
2166571b RS |
4373 | |
4374 | /* Bootstrap compiler may not have sign extended the right shift. | |
4375 | Manually extend the sign to insure bootstrap cc matches gcc. */ | |
4376 | if (arg0s < 0 && arg1 > 0) | |
4377 | val |= ((HOST_WIDE_INT) -1) << (HOST_BITS_PER_WIDE_INT - arg1); | |
4378 | ||
7afe21cc RK |
4379 | break; |
4380 | ||
4381 | case ROTATERT: | |
4382 | if (arg1 < 0) | |
4383 | return 0; | |
4384 | ||
4385 | arg1 %= width; | |
906c4e36 RK |
4386 | val = ((((unsigned HOST_WIDE_INT) arg0) << (width - arg1)) |
4387 | | (((unsigned HOST_WIDE_INT) arg0) >> arg1)); | |
7afe21cc RK |
4388 | break; |
4389 | ||
4390 | case ROTATE: | |
4391 | if (arg1 < 0) | |
4392 | return 0; | |
4393 | ||
4394 | arg1 %= width; | |
906c4e36 RK |
4395 | val = ((((unsigned HOST_WIDE_INT) arg0) << arg1) |
4396 | | (((unsigned HOST_WIDE_INT) arg0) >> (width - arg1))); | |
7afe21cc RK |
4397 | break; |
4398 | ||
4399 | case COMPARE: | |
4400 | /* Do nothing here. */ | |
4401 | return 0; | |
4402 | ||
830a38ee RS |
4403 | case SMIN: |
4404 | val = arg0s <= arg1s ? arg0s : arg1s; | |
4405 | break; | |
4406 | ||
4407 | case UMIN: | |
906c4e36 RK |
4408 | val = ((unsigned HOST_WIDE_INT) arg0 |
4409 | <= (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1); | |
830a38ee RS |
4410 | break; |
4411 | ||
4412 | case SMAX: | |
4413 | val = arg0s > arg1s ? arg0s : arg1s; | |
4414 | break; | |
4415 | ||
4416 | case UMAX: | |
906c4e36 RK |
4417 | val = ((unsigned HOST_WIDE_INT) arg0 |
4418 | > (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1); | |
830a38ee RS |
4419 | break; |
4420 | ||
7afe21cc RK |
4421 | default: |
4422 | abort (); | |
4423 | } | |
4424 | ||
7e4ce834 | 4425 | val = trunc_int_for_mode (val, mode); |
ad89d6f6 | 4426 | |
906c4e36 | 4427 | return GEN_INT (val); |
7afe21cc RK |
4428 | } |
4429 | \f | |
96b0e481 RK |
4430 | /* Simplify a PLUS or MINUS, at least one of whose operands may be another |
4431 | PLUS or MINUS. | |
4432 | ||
4433 | Rather than test for specific case, we do this by a brute-force method | |
4434 | and do all possible simplifications until no more changes occur. Then | |
4435 | we rebuild the operation. */ | |
4436 | ||
4437 | static rtx | |
4438 | simplify_plus_minus (code, mode, op0, op1) | |
4439 | enum rtx_code code; | |
4440 | enum machine_mode mode; | |
4441 | rtx op0, op1; | |
4442 | { | |
4443 | rtx ops[8]; | |
4444 | int negs[8]; | |
4445 | rtx result, tem; | |
fb5c8ce6 | 4446 | int n_ops = 2, input_ops = 2, input_consts = 0, n_consts = 0; |
96b0e481 | 4447 | int first = 1, negate = 0, changed; |
fb5c8ce6 | 4448 | int i, j; |
96b0e481 | 4449 | |
4c9a05bc | 4450 | bzero ((char *) ops, sizeof ops); |
96b0e481 RK |
4451 | |
4452 | /* Set up the two operands and then expand them until nothing has been | |
4453 | changed. If we run out of room in our array, give up; this should | |
4454 | almost never happen. */ | |
4455 | ||
4456 | ops[0] = op0, ops[1] = op1, negs[0] = 0, negs[1] = (code == MINUS); | |
4457 | ||
4458 | changed = 1; | |
4459 | while (changed) | |
4460 | { | |
4461 | changed = 0; | |
4462 | ||
4463 | for (i = 0; i < n_ops; i++) | |
4464 | switch (GET_CODE (ops[i])) | |
4465 | { | |
4466 | case PLUS: | |
4467 | case MINUS: | |
4468 | if (n_ops == 7) | |
4469 | return 0; | |
4470 | ||
4471 | ops[n_ops] = XEXP (ops[i], 1); | |
4472 | negs[n_ops++] = GET_CODE (ops[i]) == MINUS ? !negs[i] : negs[i]; | |
4473 | ops[i] = XEXP (ops[i], 0); | |
b7d9299b | 4474 | input_ops++; |
96b0e481 RK |
4475 | changed = 1; |
4476 | break; | |
4477 | ||
4478 | case NEG: | |
4479 | ops[i] = XEXP (ops[i], 0); | |
4480 | negs[i] = ! negs[i]; | |
4481 | changed = 1; | |
4482 | break; | |
4483 | ||
4484 | case CONST: | |
4485 | ops[i] = XEXP (ops[i], 0); | |
fb5c8ce6 | 4486 | input_consts++; |
96b0e481 RK |
4487 | changed = 1; |
4488 | break; | |
4489 | ||
4490 | case NOT: | |
4491 | /* ~a -> (-a - 1) */ | |
4492 | if (n_ops != 7) | |
4493 | { | |
4494 | ops[n_ops] = constm1_rtx; | |
5931019b | 4495 | negs[n_ops++] = negs[i]; |
96b0e481 RK |
4496 | ops[i] = XEXP (ops[i], 0); |
4497 | negs[i] = ! negs[i]; | |
4498 | changed = 1; | |
4499 | } | |
4500 | break; | |
4501 | ||
4502 | case CONST_INT: | |
4503 | if (negs[i]) | |
4504 | ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0, changed = 1; | |
4505 | break; | |
e9a25f70 JL |
4506 | |
4507 | default: | |
4508 | break; | |
96b0e481 RK |
4509 | } |
4510 | } | |
4511 | ||
4512 | /* If we only have two operands, we can't do anything. */ | |
4513 | if (n_ops <= 2) | |
4514 | return 0; | |
4515 | ||
4516 | /* Now simplify each pair of operands until nothing changes. The first | |
4517 | time through just simplify constants against each other. */ | |
4518 | ||
4519 | changed = 1; | |
4520 | while (changed) | |
4521 | { | |
4522 | changed = first; | |
4523 | ||
4524 | for (i = 0; i < n_ops - 1; i++) | |
4525 | for (j = i + 1; j < n_ops; j++) | |
4526 | if (ops[i] != 0 && ops[j] != 0 | |
4527 | && (! first || (CONSTANT_P (ops[i]) && CONSTANT_P (ops[j])))) | |
4528 | { | |
4529 | rtx lhs = ops[i], rhs = ops[j]; | |
4530 | enum rtx_code ncode = PLUS; | |
4531 | ||
4532 | if (negs[i] && ! negs[j]) | |
4533 | lhs = ops[j], rhs = ops[i], ncode = MINUS; | |
4534 | else if (! negs[i] && negs[j]) | |
4535 | ncode = MINUS; | |
4536 | ||
4537 | tem = simplify_binary_operation (ncode, mode, lhs, rhs); | |
b7d9299b | 4538 | if (tem) |
96b0e481 RK |
4539 | { |
4540 | ops[i] = tem, ops[j] = 0; | |
4541 | negs[i] = negs[i] && negs[j]; | |
4542 | if (GET_CODE (tem) == NEG) | |
4543 | ops[i] = XEXP (tem, 0), negs[i] = ! negs[i]; | |
4544 | ||
4545 | if (GET_CODE (ops[i]) == CONST_INT && negs[i]) | |
4546 | ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0; | |
4547 | changed = 1; | |
4548 | } | |
4549 | } | |
4550 | ||
4551 | first = 0; | |
4552 | } | |
4553 | ||
4554 | /* Pack all the operands to the lower-numbered entries and give up if | |
91a60f37 | 4555 | we didn't reduce the number of operands we had. Make sure we |
fb5c8ce6 RK |
4556 | count a CONST as two operands. If we have the same number of |
4557 | operands, but have made more CONSTs than we had, this is also | |
4558 | an improvement, so accept it. */ | |
91a60f37 | 4559 | |
fb5c8ce6 | 4560 | for (i = 0, j = 0; j < n_ops; j++) |
96b0e481 | 4561 | if (ops[j] != 0) |
91a60f37 RK |
4562 | { |
4563 | ops[i] = ops[j], negs[i++] = negs[j]; | |
4564 | if (GET_CODE (ops[j]) == CONST) | |
fb5c8ce6 | 4565 | n_consts++; |
91a60f37 | 4566 | } |
96b0e481 | 4567 | |
fb5c8ce6 RK |
4568 | if (i + n_consts > input_ops |
4569 | || (i + n_consts == input_ops && n_consts <= input_consts)) | |
96b0e481 RK |
4570 | return 0; |
4571 | ||
4572 | n_ops = i; | |
4573 | ||
4574 | /* If we have a CONST_INT, put it last. */ | |
4575 | for (i = 0; i < n_ops - 1; i++) | |
4576 | if (GET_CODE (ops[i]) == CONST_INT) | |
4577 | { | |
4578 | tem = ops[n_ops - 1], ops[n_ops - 1] = ops[i] , ops[i] = tem; | |
4579 | j = negs[n_ops - 1], negs[n_ops - 1] = negs[i], negs[i] = j; | |
4580 | } | |
4581 | ||
4582 | /* Put a non-negated operand first. If there aren't any, make all | |
4583 | operands positive and negate the whole thing later. */ | |
4584 | for (i = 0; i < n_ops && negs[i]; i++) | |
4585 | ; | |
4586 | ||
4587 | if (i == n_ops) | |
4588 | { | |
4589 | for (i = 0; i < n_ops; i++) | |
4590 | negs[i] = 0; | |
4591 | negate = 1; | |
4592 | } | |
4593 | else if (i != 0) | |
4594 | { | |
4595 | tem = ops[0], ops[0] = ops[i], ops[i] = tem; | |
4596 | j = negs[0], negs[0] = negs[i], negs[i] = j; | |
4597 | } | |
4598 | ||
4599 | /* Now make the result by performing the requested operations. */ | |
4600 | result = ops[0]; | |
4601 | for (i = 1; i < n_ops; i++) | |
4602 | result = cse_gen_binary (negs[i] ? MINUS : PLUS, mode, result, ops[i]); | |
4603 | ||
38a448ca | 4604 | return negate ? gen_rtx_NEG (mode, result) : result; |
96b0e481 RK |
4605 | } |
4606 | \f | |
4607 | /* Make a binary operation by properly ordering the operands and | |
4608 | seeing if the expression folds. */ | |
4609 | ||
4610 | static rtx | |
4611 | cse_gen_binary (code, mode, op0, op1) | |
4612 | enum rtx_code code; | |
4613 | enum machine_mode mode; | |
4614 | rtx op0, op1; | |
4615 | { | |
4616 | rtx tem; | |
4617 | ||
4618 | /* Put complex operands first and constants second if commutative. */ | |
4619 | if (GET_RTX_CLASS (code) == 'c' | |
4620 | && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT) | |
4621 | || (GET_RTX_CLASS (GET_CODE (op0)) == 'o' | |
4622 | && GET_RTX_CLASS (GET_CODE (op1)) != 'o') | |
4623 | || (GET_CODE (op0) == SUBREG | |
4624 | && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o' | |
4625 | && GET_RTX_CLASS (GET_CODE (op1)) != 'o'))) | |
4626 | tem = op0, op0 = op1, op1 = tem; | |
4627 | ||
4628 | /* If this simplifies, do it. */ | |
4629 | tem = simplify_binary_operation (code, mode, op0, op1); | |
4630 | ||
4631 | if (tem) | |
4632 | return tem; | |
4633 | ||
4634 | /* Handle addition and subtraction of CONST_INT specially. Otherwise, | |
4635 | just form the operation. */ | |
4636 | ||
4637 | if (code == PLUS && GET_CODE (op1) == CONST_INT | |
4638 | && GET_MODE (op0) != VOIDmode) | |
4639 | return plus_constant (op0, INTVAL (op1)); | |
4640 | else if (code == MINUS && GET_CODE (op1) == CONST_INT | |
4641 | && GET_MODE (op0) != VOIDmode) | |
4642 | return plus_constant (op0, - INTVAL (op1)); | |
4643 | else | |
38a448ca | 4644 | return gen_rtx_fmt_ee (code, mode, op0, op1); |
96b0e481 RK |
4645 | } |
4646 | \f | |
1a87eea2 KG |
4647 | struct cfc_args |
4648 | { | |
4649 | /* Input */ | |
4650 | rtx op0, op1; | |
4651 | /* Output */ | |
4652 | int equal, op0lt, op1lt; | |
4653 | }; | |
4654 | ||
4655 | static void | |
4656 | check_fold_consts (data) | |
4657 | PTR data; | |
4658 | { | |
4659 | struct cfc_args * args = (struct cfc_args *) data; | |
4660 | REAL_VALUE_TYPE d0, d1; | |
4661 | ||
4662 | REAL_VALUE_FROM_CONST_DOUBLE (d0, args->op0); | |
4663 | REAL_VALUE_FROM_CONST_DOUBLE (d1, args->op1); | |
4664 | args->equal = REAL_VALUES_EQUAL (d0, d1); | |
4665 | args->op0lt = REAL_VALUES_LESS (d0, d1); | |
4666 | args->op1lt = REAL_VALUES_LESS (d1, d0); | |
4667 | } | |
4668 | ||
7afe21cc | 4669 | /* Like simplify_binary_operation except used for relational operators. |
a432f20d RK |
4670 | MODE is the mode of the operands, not that of the result. If MODE |
4671 | is VOIDmode, both operands must also be VOIDmode and we compare the | |
4672 | operands in "infinite precision". | |
4673 | ||
4674 | If no simplification is possible, this function returns zero. Otherwise, | |
4675 | it returns either const_true_rtx or const0_rtx. */ | |
7afe21cc RK |
4676 | |
4677 | rtx | |
4678 | simplify_relational_operation (code, mode, op0, op1) | |
4679 | enum rtx_code code; | |
4680 | enum machine_mode mode; | |
4681 | rtx op0, op1; | |
4682 | { | |
a432f20d RK |
4683 | int equal, op0lt, op0ltu, op1lt, op1ltu; |
4684 | rtx tem; | |
7afe21cc RK |
4685 | |
4686 | /* If op0 is a compare, extract the comparison arguments from it. */ | |
4687 | if (GET_CODE (op0) == COMPARE && op1 == const0_rtx) | |
4688 | op1 = XEXP (op0, 1), op0 = XEXP (op0, 0); | |
4689 | ||
28bad1cb RK |
4690 | /* We can't simplify MODE_CC values since we don't know what the |
4691 | actual comparison is. */ | |
4692 | if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC | |
4693 | #ifdef HAVE_cc0 | |
4694 | || op0 == cc0_rtx | |
4695 | #endif | |
4696 | ) | |
31dcf83f RS |
4697 | return 0; |
4698 | ||
a432f20d RK |
4699 | /* For integer comparisons of A and B maybe we can simplify A - B and can |
4700 | then simplify a comparison of that with zero. If A and B are both either | |
4701 | a register or a CONST_INT, this can't help; testing for these cases will | |
4702 | prevent infinite recursion here and speed things up. | |
4703 | ||
c27b5c62 JW |
4704 | If CODE is an unsigned comparison, then we can never do this optimization, |
4705 | because it gives an incorrect result if the subtraction wraps around zero. | |
4706 | ANSI C defines unsigned operations such that they never overflow, and | |
4707 | thus such cases can not be ignored. */ | |
a432f20d RK |
4708 | |
4709 | if (INTEGRAL_MODE_P (mode) && op1 != const0_rtx | |
4710 | && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == CONST_INT) | |
4711 | && (GET_CODE (op1) == REG || GET_CODE (op1) == CONST_INT)) | |
4712 | && 0 != (tem = simplify_binary_operation (MINUS, mode, op0, op1)) | |
c27b5c62 | 4713 | && code != GTU && code != GEU && code != LTU && code != LEU) |
a432f20d RK |
4714 | return simplify_relational_operation (signed_condition (code), |
4715 | mode, tem, const0_rtx); | |
4716 | ||
4717 | /* For non-IEEE floating-point, if the two operands are equal, we know the | |
4718 | result. */ | |
4719 | if (rtx_equal_p (op0, op1) | |
4720 | && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT | |
4721 | || ! FLOAT_MODE_P (GET_MODE (op0)) || flag_fast_math)) | |
4722 | equal = 1, op0lt = 0, op0ltu = 0, op1lt = 0, op1ltu = 0; | |
4723 | ||
4724 | /* If the operands are floating-point constants, see if we can fold | |
4725 | the result. */ | |
6076248a | 4726 | #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC) |
a432f20d RK |
4727 | else if (GET_CODE (op0) == CONST_DOUBLE && GET_CODE (op1) == CONST_DOUBLE |
4728 | && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT) | |
4729 | { | |
1a87eea2 KG |
4730 | struct cfc_args args; |
4731 | ||
4732 | /* Setup input for check_fold_consts() */ | |
4733 | args.op0 = op0; | |
4734 | args.op1 = op1; | |
a432f20d | 4735 | |
1a87eea2 KG |
4736 | if (do_float_handler(check_fold_consts, (PTR) &args) == 0) |
4737 | /* We got an exception from check_fold_consts() */ | |
a432f20d | 4738 | return 0; |
7afe21cc | 4739 | |
1a87eea2 KG |
4740 | /* Receive output from check_fold_consts() */ |
4741 | equal = args.equal; | |
4742 | op0lt = op0ltu = args.op0lt; | |
4743 | op1lt = op1ltu = args.op1lt; | |
a432f20d RK |
4744 | } |
4745 | #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */ | |
7afe21cc | 4746 | |
a432f20d RK |
4747 | /* Otherwise, see if the operands are both integers. */ |
4748 | else if ((GET_MODE_CLASS (mode) == MODE_INT || mode == VOIDmode) | |
4749 | && (GET_CODE (op0) == CONST_DOUBLE || GET_CODE (op0) == CONST_INT) | |
4750 | && (GET_CODE (op1) == CONST_DOUBLE || GET_CODE (op1) == CONST_INT)) | |
4751 | { | |
4752 | int width = GET_MODE_BITSIZE (mode); | |
64812ded RK |
4753 | HOST_WIDE_INT l0s, h0s, l1s, h1s; |
4754 | unsigned HOST_WIDE_INT l0u, h0u, l1u, h1u; | |
7afe21cc | 4755 | |
a432f20d RK |
4756 | /* Get the two words comprising each integer constant. */ |
4757 | if (GET_CODE (op0) == CONST_DOUBLE) | |
4758 | { | |
4759 | l0u = l0s = CONST_DOUBLE_LOW (op0); | |
4760 | h0u = h0s = CONST_DOUBLE_HIGH (op0); | |
7afe21cc | 4761 | } |
a432f20d | 4762 | else |
6076248a | 4763 | { |
a432f20d | 4764 | l0u = l0s = INTVAL (op0); |
cb3bb2a7 | 4765 | h0u = h0s = l0s < 0 ? -1 : 0; |
a432f20d | 4766 | } |
6076248a | 4767 | |
a432f20d RK |
4768 | if (GET_CODE (op1) == CONST_DOUBLE) |
4769 | { | |
4770 | l1u = l1s = CONST_DOUBLE_LOW (op1); | |
4771 | h1u = h1s = CONST_DOUBLE_HIGH (op1); | |
4772 | } | |
4773 | else | |
4774 | { | |
4775 | l1u = l1s = INTVAL (op1); | |
cb3bb2a7 | 4776 | h1u = h1s = l1s < 0 ? -1 : 0; |
a432f20d RK |
4777 | } |
4778 | ||
4779 | /* If WIDTH is nonzero and smaller than HOST_BITS_PER_WIDE_INT, | |
4780 | we have to sign or zero-extend the values. */ | |
4781 | if (width != 0 && width <= HOST_BITS_PER_WIDE_INT) | |
4782 | h0u = h1u = 0, h0s = l0s < 0 ? -1 : 0, h1s = l1s < 0 ? -1 : 0; | |
6076248a | 4783 | |
a432f20d RK |
4784 | if (width != 0 && width < HOST_BITS_PER_WIDE_INT) |
4785 | { | |
4786 | l0u &= ((HOST_WIDE_INT) 1 << width) - 1; | |
4787 | l1u &= ((HOST_WIDE_INT) 1 << width) - 1; | |
6076248a | 4788 | |
a432f20d RK |
4789 | if (l0s & ((HOST_WIDE_INT) 1 << (width - 1))) |
4790 | l0s |= ((HOST_WIDE_INT) (-1) << width); | |
6076248a | 4791 | |
a432f20d RK |
4792 | if (l1s & ((HOST_WIDE_INT) 1 << (width - 1))) |
4793 | l1s |= ((HOST_WIDE_INT) (-1) << width); | |
6076248a RK |
4794 | } |
4795 | ||
a432f20d RK |
4796 | equal = (h0u == h1u && l0u == l1u); |
4797 | op0lt = (h0s < h1s || (h0s == h1s && l0s < l1s)); | |
4798 | op1lt = (h1s < h0s || (h1s == h0s && l1s < l0s)); | |
4799 | op0ltu = (h0u < h1u || (h0u == h1u && l0u < l1u)); | |
4800 | op1ltu = (h1u < h0u || (h1u == h0u && l1u < l0u)); | |
4801 | } | |
4802 | ||
4803 | /* Otherwise, there are some code-specific tests we can make. */ | |
4804 | else | |
4805 | { | |
7afe21cc RK |
4806 | switch (code) |
4807 | { | |
4808 | case EQ: | |
a432f20d RK |
4809 | /* References to the frame plus a constant or labels cannot |
4810 | be zero, but a SYMBOL_REF can due to #pragma weak. */ | |
4811 | if (((NONZERO_BASE_PLUS_P (op0) && op1 == const0_rtx) | |
4812 | || GET_CODE (op0) == LABEL_REF) | |
1a7c818b | 4813 | #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM |
a432f20d RK |
4814 | /* On some machines, the ap reg can be 0 sometimes. */ |
4815 | && op0 != arg_pointer_rtx | |
7afe21cc | 4816 | #endif |
a432f20d RK |
4817 | ) |
4818 | return const0_rtx; | |
4819 | break; | |
7afe21cc RK |
4820 | |
4821 | case NE: | |
a432f20d RK |
4822 | if (((NONZERO_BASE_PLUS_P (op0) && op1 == const0_rtx) |
4823 | || GET_CODE (op0) == LABEL_REF) | |
1a7c818b | 4824 | #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM |
a432f20d | 4825 | && op0 != arg_pointer_rtx |
7afe21cc | 4826 | #endif |
a432f20d | 4827 | ) |
7afe21cc RK |
4828 | return const_true_rtx; |
4829 | break; | |
4830 | ||
4831 | case GEU: | |
a432f20d RK |
4832 | /* Unsigned values are never negative. */ |
4833 | if (op1 == const0_rtx) | |
7afe21cc RK |
4834 | return const_true_rtx; |
4835 | break; | |
4836 | ||
4837 | case LTU: | |
a432f20d | 4838 | if (op1 == const0_rtx) |
7afe21cc RK |
4839 | return const0_rtx; |
4840 | break; | |
4841 | ||
4842 | case LEU: | |
4843 | /* Unsigned values are never greater than the largest | |
4844 | unsigned value. */ | |
4845 | if (GET_CODE (op1) == CONST_INT | |
6a651371 | 4846 | && (unsigned HOST_WIDE_INT) INTVAL (op1) == GET_MODE_MASK (mode) |
a432f20d RK |
4847 | && INTEGRAL_MODE_P (mode)) |
4848 | return const_true_rtx; | |
7afe21cc RK |
4849 | break; |
4850 | ||
4851 | case GTU: | |
4852 | if (GET_CODE (op1) == CONST_INT | |
6a651371 | 4853 | && (unsigned HOST_WIDE_INT) INTVAL (op1) == GET_MODE_MASK (mode) |
cbf6a543 | 4854 | && INTEGRAL_MODE_P (mode)) |
7afe21cc RK |
4855 | return const0_rtx; |
4856 | break; | |
e9a25f70 JL |
4857 | |
4858 | default: | |
4859 | break; | |
7afe21cc RK |
4860 | } |
4861 | ||
4862 | return 0; | |
4863 | } | |
4864 | ||
a432f20d RK |
4865 | /* If we reach here, EQUAL, OP0LT, OP0LTU, OP1LT, and OP1LTU are set |
4866 | as appropriate. */ | |
7afe21cc RK |
4867 | switch (code) |
4868 | { | |
7afe21cc | 4869 | case EQ: |
a432f20d RK |
4870 | return equal ? const_true_rtx : const0_rtx; |
4871 | case NE: | |
4872 | return ! equal ? const_true_rtx : const0_rtx; | |
7afe21cc | 4873 | case LT: |
a432f20d | 4874 | return op0lt ? const_true_rtx : const0_rtx; |
7afe21cc | 4875 | case GT: |
a432f20d | 4876 | return op1lt ? const_true_rtx : const0_rtx; |
7afe21cc | 4877 | case LTU: |
a432f20d | 4878 | return op0ltu ? const_true_rtx : const0_rtx; |
7afe21cc | 4879 | case GTU: |
a432f20d RK |
4880 | return op1ltu ? const_true_rtx : const0_rtx; |
4881 | case LE: | |
4882 | return equal || op0lt ? const_true_rtx : const0_rtx; | |
4883 | case GE: | |
4884 | return equal || op1lt ? const_true_rtx : const0_rtx; | |
4885 | case LEU: | |
4886 | return equal || op0ltu ? const_true_rtx : const0_rtx; | |
4887 | case GEU: | |
4888 | return equal || op1ltu ? const_true_rtx : const0_rtx; | |
e9a25f70 JL |
4889 | default: |
4890 | abort (); | |
7afe21cc | 4891 | } |
7afe21cc RK |
4892 | } |
4893 | \f | |
4894 | /* Simplify CODE, an operation with result mode MODE and three operands, | |
4895 | OP0, OP1, and OP2. OP0_MODE was the mode of OP0 before it became | |
4896 | a constant. Return 0 if no simplifications is possible. */ | |
4897 | ||
4898 | rtx | |
4899 | simplify_ternary_operation (code, mode, op0_mode, op0, op1, op2) | |
4900 | enum rtx_code code; | |
4901 | enum machine_mode mode, op0_mode; | |
4902 | rtx op0, op1, op2; | |
4903 | { | |
4904 | int width = GET_MODE_BITSIZE (mode); | |
4905 | ||
4906 | /* VOIDmode means "infinite" precision. */ | |
4907 | if (width == 0) | |
906c4e36 | 4908 | width = HOST_BITS_PER_WIDE_INT; |
7afe21cc RK |
4909 | |
4910 | switch (code) | |
4911 | { | |
4912 | case SIGN_EXTRACT: | |
4913 | case ZERO_EXTRACT: | |
4914 | if (GET_CODE (op0) == CONST_INT | |
4915 | && GET_CODE (op1) == CONST_INT | |
4916 | && GET_CODE (op2) == CONST_INT | |
4917 | && INTVAL (op1) + INTVAL (op2) <= GET_MODE_BITSIZE (op0_mode) | |
906c4e36 | 4918 | && width <= HOST_BITS_PER_WIDE_INT) |
7afe21cc RK |
4919 | { |
4920 | /* Extracting a bit-field from a constant */ | |
906c4e36 | 4921 | HOST_WIDE_INT val = INTVAL (op0); |
7afe21cc | 4922 | |
f76b9db2 ILT |
4923 | if (BITS_BIG_ENDIAN) |
4924 | val >>= (GET_MODE_BITSIZE (op0_mode) | |
4925 | - INTVAL (op2) - INTVAL (op1)); | |
4926 | else | |
4927 | val >>= INTVAL (op2); | |
4928 | ||
906c4e36 | 4929 | if (HOST_BITS_PER_WIDE_INT != INTVAL (op1)) |
7afe21cc RK |
4930 | { |
4931 | /* First zero-extend. */ | |
906c4e36 | 4932 | val &= ((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1; |
7afe21cc | 4933 | /* If desired, propagate sign bit. */ |
906c4e36 RK |
4934 | if (code == SIGN_EXTRACT |
4935 | && (val & ((HOST_WIDE_INT) 1 << (INTVAL (op1) - 1)))) | |
4936 | val |= ~ (((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1); | |
7afe21cc RK |
4937 | } |
4938 | ||
4939 | /* Clear the bits that don't belong in our mode, | |
4940 | unless they and our sign bit are all one. | |
4941 | So we get either a reasonable negative value or a reasonable | |
4942 | unsigned value for this mode. */ | |
906c4e36 RK |
4943 | if (width < HOST_BITS_PER_WIDE_INT |
4944 | && ((val & ((HOST_WIDE_INT) (-1) << (width - 1))) | |
4945 | != ((HOST_WIDE_INT) (-1) << (width - 1)))) | |
4946 | val &= ((HOST_WIDE_INT) 1 << width) - 1; | |
7afe21cc | 4947 | |
906c4e36 | 4948 | return GEN_INT (val); |
7afe21cc RK |
4949 | } |
4950 | break; | |
4951 | ||
4952 | case IF_THEN_ELSE: | |
4953 | if (GET_CODE (op0) == CONST_INT) | |
4954 | return op0 != const0_rtx ? op1 : op2; | |
3bf1b082 JW |
4955 | |
4956 | /* Convert a == b ? b : a to "a". */ | |
4957 | if (GET_CODE (op0) == NE && ! side_effects_p (op0) | |
4958 | && rtx_equal_p (XEXP (op0, 0), op1) | |
4959 | && rtx_equal_p (XEXP (op0, 1), op2)) | |
4960 | return op1; | |
4961 | else if (GET_CODE (op0) == EQ && ! side_effects_p (op0) | |
4962 | && rtx_equal_p (XEXP (op0, 1), op1) | |
4963 | && rtx_equal_p (XEXP (op0, 0), op2)) | |
4964 | return op2; | |
e82ad93d | 4965 | else if (GET_RTX_CLASS (GET_CODE (op0)) == '<' && ! side_effects_p (op0)) |
ed1ecb19 JL |
4966 | { |
4967 | rtx temp; | |
4968 | temp = simplify_relational_operation (GET_CODE (op0), op0_mode, | |
4969 | XEXP (op0, 0), XEXP (op0, 1)); | |
4970 | /* See if any simplifications were possible. */ | |
4971 | if (temp == const0_rtx) | |
4972 | return op2; | |
4973 | else if (temp == const1_rtx) | |
4974 | return op1; | |
4975 | } | |
7afe21cc RK |
4976 | break; |
4977 | ||
4978 | default: | |
4979 | abort (); | |
4980 | } | |
4981 | ||
4982 | return 0; | |
4983 | } | |
4984 | \f | |
4985 | /* If X is a nontrivial arithmetic operation on an argument | |
4986 | for which a constant value can be determined, return | |
4987 | the result of operating on that value, as a constant. | |
4988 | Otherwise, return X, possibly with one or more operands | |
4989 | modified by recursive calls to this function. | |
4990 | ||
e7bb59fa RK |
4991 | If X is a register whose contents are known, we do NOT |
4992 | return those contents here. equiv_constant is called to | |
4993 | perform that task. | |
7afe21cc RK |
4994 | |
4995 | INSN is the insn that we may be modifying. If it is 0, make a copy | |
4996 | of X before modifying it. */ | |
4997 | ||
4998 | static rtx | |
4999 | fold_rtx (x, insn) | |
5000 | rtx x; | |
5001 | rtx insn; | |
5002 | { | |
5003 | register enum rtx_code code; | |
5004 | register enum machine_mode mode; | |
6f7d635c | 5005 | register const char *fmt; |
906c4e36 | 5006 | register int i; |
7afe21cc RK |
5007 | rtx new = 0; |
5008 | int copied = 0; | |
5009 | int must_swap = 0; | |
5010 | ||
5011 | /* Folded equivalents of first two operands of X. */ | |
5012 | rtx folded_arg0; | |
5013 | rtx folded_arg1; | |
5014 | ||
5015 | /* Constant equivalents of first three operands of X; | |
5016 | 0 when no such equivalent is known. */ | |
5017 | rtx const_arg0; | |
5018 | rtx const_arg1; | |
5019 | rtx const_arg2; | |
5020 | ||
5021 | /* The mode of the first operand of X. We need this for sign and zero | |
5022 | extends. */ | |
5023 | enum machine_mode mode_arg0; | |
5024 | ||
5025 | if (x == 0) | |
5026 | return x; | |
5027 | ||
5028 | mode = GET_MODE (x); | |
5029 | code = GET_CODE (x); | |
5030 | switch (code) | |
5031 | { | |
5032 | case CONST: | |
5033 | case CONST_INT: | |
5034 | case CONST_DOUBLE: | |
5035 | case SYMBOL_REF: | |
5036 | case LABEL_REF: | |
5037 | case REG: | |
5038 | /* No use simplifying an EXPR_LIST | |
5039 | since they are used only for lists of args | |
5040 | in a function call's REG_EQUAL note. */ | |
5041 | case EXPR_LIST: | |
956d6950 JL |
5042 | /* Changing anything inside an ADDRESSOF is incorrect; we don't |
5043 | want to (e.g.,) make (addressof (const_int 0)) just because | |
5044 | the location is known to be zero. */ | |
5045 | case ADDRESSOF: | |
7afe21cc RK |
5046 | return x; |
5047 | ||
5048 | #ifdef HAVE_cc0 | |
5049 | case CC0: | |
5050 | return prev_insn_cc0; | |
5051 | #endif | |
5052 | ||
5053 | case PC: | |
5054 | /* If the next insn is a CODE_LABEL followed by a jump table, | |
5055 | PC's value is a LABEL_REF pointing to that label. That | |
5056 | lets us fold switch statements on the Vax. */ | |
5057 | if (insn && GET_CODE (insn) == JUMP_INSN) | |
5058 | { | |
5059 | rtx next = next_nonnote_insn (insn); | |
5060 | ||
5061 | if (next && GET_CODE (next) == CODE_LABEL | |
5062 | && NEXT_INSN (next) != 0 | |
5063 | && GET_CODE (NEXT_INSN (next)) == JUMP_INSN | |
5064 | && (GET_CODE (PATTERN (NEXT_INSN (next))) == ADDR_VEC | |
5065 | || GET_CODE (PATTERN (NEXT_INSN (next))) == ADDR_DIFF_VEC)) | |
38a448ca | 5066 | return gen_rtx_LABEL_REF (Pmode, next); |
7afe21cc RK |
5067 | } |
5068 | break; | |
5069 | ||
5070 | case SUBREG: | |
c610adec RK |
5071 | /* See if we previously assigned a constant value to this SUBREG. */ |
5072 | if ((new = lookup_as_function (x, CONST_INT)) != 0 | |
5073 | || (new = lookup_as_function (x, CONST_DOUBLE)) != 0) | |
7afe21cc RK |
5074 | return new; |
5075 | ||
4b980e20 RK |
5076 | /* If this is a paradoxical SUBREG, we have no idea what value the |
5077 | extra bits would have. However, if the operand is equivalent | |
5078 | to a SUBREG whose operand is the same as our mode, and all the | |
5079 | modes are within a word, we can just use the inner operand | |
31c85c78 RK |
5080 | because these SUBREGs just say how to treat the register. |
5081 | ||
5082 | Similarly if we find an integer constant. */ | |
4b980e20 | 5083 | |
e5f6a288 | 5084 | if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))) |
4b980e20 RK |
5085 | { |
5086 | enum machine_mode imode = GET_MODE (SUBREG_REG (x)); | |
5087 | struct table_elt *elt; | |
5088 | ||
5089 | if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD | |
5090 | && GET_MODE_SIZE (imode) <= UNITS_PER_WORD | |
5091 | && (elt = lookup (SUBREG_REG (x), HASH (SUBREG_REG (x), imode), | |
5092 | imode)) != 0) | |
31c85c78 RK |
5093 | for (elt = elt->first_same_value; |
5094 | elt; elt = elt->next_same_value) | |
5095 | { | |
5096 | if (CONSTANT_P (elt->exp) | |
5097 | && GET_MODE (elt->exp) == VOIDmode) | |
5098 | return elt->exp; | |
5099 | ||
4b980e20 RK |
5100 | if (GET_CODE (elt->exp) == SUBREG |
5101 | && GET_MODE (SUBREG_REG (elt->exp)) == mode | |
906c4e36 | 5102 | && exp_equiv_p (elt->exp, elt->exp, 1, 0)) |
4b980e20 RK |
5103 | return copy_rtx (SUBREG_REG (elt->exp)); |
5104 | } | |
5105 | ||
5106 | return x; | |
5107 | } | |
e5f6a288 | 5108 | |
7afe21cc RK |
5109 | /* Fold SUBREG_REG. If it changed, see if we can simplify the SUBREG. |
5110 | We might be able to if the SUBREG is extracting a single word in an | |
5111 | integral mode or extracting the low part. */ | |
5112 | ||
5113 | folded_arg0 = fold_rtx (SUBREG_REG (x), insn); | |
5114 | const_arg0 = equiv_constant (folded_arg0); | |
5115 | if (const_arg0) | |
5116 | folded_arg0 = const_arg0; | |
5117 | ||
5118 | if (folded_arg0 != SUBREG_REG (x)) | |
5119 | { | |
5120 | new = 0; | |
5121 | ||
5122 | if (GET_MODE_CLASS (mode) == MODE_INT | |
5123 | && GET_MODE_SIZE (mode) == UNITS_PER_WORD | |
5124 | && GET_MODE (SUBREG_REG (x)) != VOIDmode) | |
5125 | new = operand_subword (folded_arg0, SUBREG_WORD (x), 0, | |
5126 | GET_MODE (SUBREG_REG (x))); | |
5127 | if (new == 0 && subreg_lowpart_p (x)) | |
5128 | new = gen_lowpart_if_possible (mode, folded_arg0); | |
5129 | if (new) | |
5130 | return new; | |
5131 | } | |
e5f6a288 RK |
5132 | |
5133 | /* If this is a narrowing SUBREG and our operand is a REG, see if | |
858a47b1 | 5134 | we can find an equivalence for REG that is an arithmetic operation |
e5f6a288 RK |
5135 | in a wider mode where both operands are paradoxical SUBREGs |
5136 | from objects of our result mode. In that case, we couldn't report | |
5137 | an equivalent value for that operation, since we don't know what the | |
5138 | extra bits will be. But we can find an equivalence for this SUBREG | |
5139 | by folding that operation is the narrow mode. This allows us to | |
5140 | fold arithmetic in narrow modes when the machine only supports | |
4b980e20 RK |
5141 | word-sized arithmetic. |
5142 | ||
5143 | Also look for a case where we have a SUBREG whose operand is the | |
5144 | same as our result. If both modes are smaller than a word, we | |
5145 | are simply interpreting a register in different modes and we | |
5146 | can use the inner value. */ | |
e5f6a288 RK |
5147 | |
5148 | if (GET_CODE (folded_arg0) == REG | |
e8d76a39 RS |
5149 | && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (folded_arg0)) |
5150 | && subreg_lowpart_p (x)) | |
e5f6a288 RK |
5151 | { |
5152 | struct table_elt *elt; | |
5153 | ||
5154 | /* We can use HASH here since we know that canon_hash won't be | |
5155 | called. */ | |
5156 | elt = lookup (folded_arg0, | |
5157 | HASH (folded_arg0, GET_MODE (folded_arg0)), | |
5158 | GET_MODE (folded_arg0)); | |
5159 | ||
5160 | if (elt) | |
5161 | elt = elt->first_same_value; | |
5162 | ||
5163 | for (; elt; elt = elt->next_same_value) | |
5164 | { | |
e8d76a39 RS |
5165 | enum rtx_code eltcode = GET_CODE (elt->exp); |
5166 | ||
e5f6a288 RK |
5167 | /* Just check for unary and binary operations. */ |
5168 | if (GET_RTX_CLASS (GET_CODE (elt->exp)) == '1' | |
5169 | && GET_CODE (elt->exp) != SIGN_EXTEND | |
5170 | && GET_CODE (elt->exp) != ZERO_EXTEND | |
5171 | && GET_CODE (XEXP (elt->exp, 0)) == SUBREG | |
5172 | && GET_MODE (SUBREG_REG (XEXP (elt->exp, 0))) == mode) | |
5173 | { | |
5174 | rtx op0 = SUBREG_REG (XEXP (elt->exp, 0)); | |
5175 | ||
5176 | if (GET_CODE (op0) != REG && ! CONSTANT_P (op0)) | |
906c4e36 | 5177 | op0 = fold_rtx (op0, NULL_RTX); |
e5f6a288 RK |
5178 | |
5179 | op0 = equiv_constant (op0); | |
5180 | if (op0) | |
5181 | new = simplify_unary_operation (GET_CODE (elt->exp), mode, | |
5182 | op0, mode); | |
5183 | } | |
5184 | else if ((GET_RTX_CLASS (GET_CODE (elt->exp)) == '2' | |
5185 | || GET_RTX_CLASS (GET_CODE (elt->exp)) == 'c') | |
e8d76a39 RS |
5186 | && eltcode != DIV && eltcode != MOD |
5187 | && eltcode != UDIV && eltcode != UMOD | |
5188 | && eltcode != ASHIFTRT && eltcode != LSHIFTRT | |
5189 | && eltcode != ROTATE && eltcode != ROTATERT | |
e5f6a288 RK |
5190 | && ((GET_CODE (XEXP (elt->exp, 0)) == SUBREG |
5191 | && (GET_MODE (SUBREG_REG (XEXP (elt->exp, 0))) | |
5192 | == mode)) | |
5193 | || CONSTANT_P (XEXP (elt->exp, 0))) | |
5194 | && ((GET_CODE (XEXP (elt->exp, 1)) == SUBREG | |
5195 | && (GET_MODE (SUBREG_REG (XEXP (elt->exp, 1))) | |
5196 | == mode)) | |
5197 | || CONSTANT_P (XEXP (elt->exp, 1)))) | |
5198 | { | |
5199 | rtx op0 = gen_lowpart_common (mode, XEXP (elt->exp, 0)); | |
5200 | rtx op1 = gen_lowpart_common (mode, XEXP (elt->exp, 1)); | |
5201 | ||
5202 | if (op0 && GET_CODE (op0) != REG && ! CONSTANT_P (op0)) | |
906c4e36 | 5203 | op0 = fold_rtx (op0, NULL_RTX); |
e5f6a288 RK |
5204 | |
5205 | if (op0) | |
5206 | op0 = equiv_constant (op0); | |
5207 | ||
5208 | if (op1 && GET_CODE (op1) != REG && ! CONSTANT_P (op1)) | |
906c4e36 | 5209 | op1 = fold_rtx (op1, NULL_RTX); |
e5f6a288 RK |
5210 | |
5211 | if (op1) | |
5212 | op1 = equiv_constant (op1); | |
5213 | ||
76fb0b60 RS |
5214 | /* If we are looking for the low SImode part of |
5215 | (ashift:DI c (const_int 32)), it doesn't work | |
5216 | to compute that in SImode, because a 32-bit shift | |
5217 | in SImode is unpredictable. We know the value is 0. */ | |
5218 | if (op0 && op1 | |
45620ed4 | 5219 | && GET_CODE (elt->exp) == ASHIFT |
76fb0b60 RS |
5220 | && GET_CODE (op1) == CONST_INT |
5221 | && INTVAL (op1) >= GET_MODE_BITSIZE (mode)) | |
5222 | { | |
5223 | if (INTVAL (op1) < GET_MODE_BITSIZE (GET_MODE (elt->exp))) | |
5224 | ||
5225 | /* If the count fits in the inner mode's width, | |
5226 | but exceeds the outer mode's width, | |
5227 | the value will get truncated to 0 | |
5228 | by the subreg. */ | |
5229 | new = const0_rtx; | |
5230 | else | |
5231 | /* If the count exceeds even the inner mode's width, | |
5232 | don't fold this expression. */ | |
5233 | new = 0; | |
5234 | } | |
5235 | else if (op0 && op1) | |
e5f6a288 RK |
5236 | new = simplify_binary_operation (GET_CODE (elt->exp), mode, |
5237 | op0, op1); | |
5238 | } | |
5239 | ||
4b980e20 RK |
5240 | else if (GET_CODE (elt->exp) == SUBREG |
5241 | && GET_MODE (SUBREG_REG (elt->exp)) == mode | |
5242 | && (GET_MODE_SIZE (GET_MODE (folded_arg0)) | |
5243 | <= UNITS_PER_WORD) | |
906c4e36 | 5244 | && exp_equiv_p (elt->exp, elt->exp, 1, 0)) |
4b980e20 RK |
5245 | new = copy_rtx (SUBREG_REG (elt->exp)); |
5246 | ||
e5f6a288 RK |
5247 | if (new) |
5248 | return new; | |
5249 | } | |
5250 | } | |
5251 | ||
7afe21cc RK |
5252 | return x; |
5253 | ||
5254 | case NOT: | |
5255 | case NEG: | |
5256 | /* If we have (NOT Y), see if Y is known to be (NOT Z). | |
5257 | If so, (NOT Y) simplifies to Z. Similarly for NEG. */ | |
5258 | new = lookup_as_function (XEXP (x, 0), code); | |
5259 | if (new) | |
5260 | return fold_rtx (copy_rtx (XEXP (new, 0)), insn); | |
5261 | break; | |
13c9910f | 5262 | |
7afe21cc RK |
5263 | case MEM: |
5264 | /* If we are not actually processing an insn, don't try to find the | |
5265 | best address. Not only don't we care, but we could modify the | |
5266 | MEM in an invalid way since we have no insn to validate against. */ | |
5267 | if (insn != 0) | |
5268 | find_best_addr (insn, &XEXP (x, 0)); | |
5269 | ||
5270 | { | |
5271 | /* Even if we don't fold in the insn itself, | |
5272 | we can safely do so here, in hopes of getting a constant. */ | |
906c4e36 | 5273 | rtx addr = fold_rtx (XEXP (x, 0), NULL_RTX); |
7afe21cc | 5274 | rtx base = 0; |
906c4e36 | 5275 | HOST_WIDE_INT offset = 0; |
7afe21cc RK |
5276 | |
5277 | if (GET_CODE (addr) == REG | |
5278 | && REGNO_QTY_VALID_P (REGNO (addr)) | |
30f72379 MM |
5279 | && GET_MODE (addr) == qty_mode[REG_QTY (REGNO (addr))] |
5280 | && qty_const[REG_QTY (REGNO (addr))] != 0) | |
5281 | addr = qty_const[REG_QTY (REGNO (addr))]; | |
7afe21cc RK |
5282 | |
5283 | /* If address is constant, split it into a base and integer offset. */ | |
5284 | if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == LABEL_REF) | |
5285 | base = addr; | |
5286 | else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS | |
5287 | && GET_CODE (XEXP (XEXP (addr, 0), 1)) == CONST_INT) | |
5288 | { | |
5289 | base = XEXP (XEXP (addr, 0), 0); | |
5290 | offset = INTVAL (XEXP (XEXP (addr, 0), 1)); | |
5291 | } | |
5292 | else if (GET_CODE (addr) == LO_SUM | |
5293 | && GET_CODE (XEXP (addr, 1)) == SYMBOL_REF) | |
5294 | base = XEXP (addr, 1); | |
e9a25f70 | 5295 | else if (GET_CODE (addr) == ADDRESSOF) |
956d6950 | 5296 | return change_address (x, VOIDmode, addr); |
7afe21cc RK |
5297 | |
5298 | /* If this is a constant pool reference, we can fold it into its | |
5299 | constant to allow better value tracking. */ | |
5300 | if (base && GET_CODE (base) == SYMBOL_REF | |
5301 | && CONSTANT_POOL_ADDRESS_P (base)) | |
5302 | { | |
5303 | rtx constant = get_pool_constant (base); | |
5304 | enum machine_mode const_mode = get_pool_mode (base); | |
5305 | rtx new; | |
5306 | ||
5307 | if (CONSTANT_P (constant) && GET_CODE (constant) != CONST_INT) | |
5308 | constant_pool_entries_cost = COST (constant); | |
5309 | ||
5310 | /* If we are loading the full constant, we have an equivalence. */ | |
5311 | if (offset == 0 && mode == const_mode) | |
5312 | return constant; | |
5313 | ||
9faa82d8 | 5314 | /* If this actually isn't a constant (weird!), we can't do |
7afe21cc RK |
5315 | anything. Otherwise, handle the two most common cases: |
5316 | extracting a word from a multi-word constant, and extracting | |
5317 | the low-order bits. Other cases don't seem common enough to | |
5318 | worry about. */ | |
5319 | if (! CONSTANT_P (constant)) | |
5320 | return x; | |
5321 | ||
5322 | if (GET_MODE_CLASS (mode) == MODE_INT | |
5323 | && GET_MODE_SIZE (mode) == UNITS_PER_WORD | |
5324 | && offset % UNITS_PER_WORD == 0 | |
5325 | && (new = operand_subword (constant, | |
5326 | offset / UNITS_PER_WORD, | |
5327 | 0, const_mode)) != 0) | |
5328 | return new; | |
5329 | ||
5330 | if (((BYTES_BIG_ENDIAN | |
5331 | && offset == GET_MODE_SIZE (GET_MODE (constant)) - 1) | |
5332 | || (! BYTES_BIG_ENDIAN && offset == 0)) | |
5333 | && (new = gen_lowpart_if_possible (mode, constant)) != 0) | |
5334 | return new; | |
5335 | } | |
5336 | ||
5337 | /* If this is a reference to a label at a known position in a jump | |
5338 | table, we also know its value. */ | |
5339 | if (base && GET_CODE (base) == LABEL_REF) | |
5340 | { | |
5341 | rtx label = XEXP (base, 0); | |
5342 | rtx table_insn = NEXT_INSN (label); | |
5343 | ||
5344 | if (table_insn && GET_CODE (table_insn) == JUMP_INSN | |
5345 | && GET_CODE (PATTERN (table_insn)) == ADDR_VEC) | |
5346 | { | |
5347 | rtx table = PATTERN (table_insn); | |
5348 | ||
5349 | if (offset >= 0 | |
5350 | && (offset / GET_MODE_SIZE (GET_MODE (table)) | |
5351 | < XVECLEN (table, 0))) | |
5352 | return XVECEXP (table, 0, | |
5353 | offset / GET_MODE_SIZE (GET_MODE (table))); | |
5354 | } | |
5355 | if (table_insn && GET_CODE (table_insn) == JUMP_INSN | |
5356 | && GET_CODE (PATTERN (table_insn)) == ADDR_DIFF_VEC) | |
5357 | { | |
5358 | rtx table = PATTERN (table_insn); | |
5359 | ||
5360 | if (offset >= 0 | |
5361 | && (offset / GET_MODE_SIZE (GET_MODE (table)) | |
5362 | < XVECLEN (table, 1))) | |
5363 | { | |
5364 | offset /= GET_MODE_SIZE (GET_MODE (table)); | |
38a448ca RH |
5365 | new = gen_rtx_MINUS (Pmode, XVECEXP (table, 1, offset), |
5366 | XEXP (table, 0)); | |
7afe21cc RK |
5367 | |
5368 | if (GET_MODE (table) != Pmode) | |
38a448ca | 5369 | new = gen_rtx_TRUNCATE (GET_MODE (table), new); |
7afe21cc | 5370 | |
67a37737 RK |
5371 | /* Indicate this is a constant. This isn't a |
5372 | valid form of CONST, but it will only be used | |
5373 | to fold the next insns and then discarded, so | |
ac7ef8d5 FS |
5374 | it should be safe. |
5375 | ||
5376 | Note this expression must be explicitly discarded, | |
5377 | by cse_insn, else it may end up in a REG_EQUAL note | |
5378 | and "escape" to cause problems elsewhere. */ | |
38a448ca | 5379 | return gen_rtx_CONST (GET_MODE (new), new); |
7afe21cc RK |
5380 | } |
5381 | } | |
5382 | } | |
5383 | ||
5384 | return x; | |
5385 | } | |
9255709c RK |
5386 | |
5387 | case ASM_OPERANDS: | |
5388 | for (i = XVECLEN (x, 3) - 1; i >= 0; i--) | |
5389 | validate_change (insn, &XVECEXP (x, 3, i), | |
5390 | fold_rtx (XVECEXP (x, 3, i), insn), 0); | |
5391 | break; | |
e9a25f70 JL |
5392 | |
5393 | default: | |
5394 | break; | |
7afe21cc RK |
5395 | } |
5396 | ||
5397 | const_arg0 = 0; | |
5398 | const_arg1 = 0; | |
5399 | const_arg2 = 0; | |
5400 | mode_arg0 = VOIDmode; | |
5401 | ||
5402 | /* Try folding our operands. | |
5403 | Then see which ones have constant values known. */ | |
5404 | ||
5405 | fmt = GET_RTX_FORMAT (code); | |
5406 | for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) | |
5407 | if (fmt[i] == 'e') | |
5408 | { | |
5409 | rtx arg = XEXP (x, i); | |
5410 | rtx folded_arg = arg, const_arg = 0; | |
5411 | enum machine_mode mode_arg = GET_MODE (arg); | |
5412 | rtx cheap_arg, expensive_arg; | |
5413 | rtx replacements[2]; | |
5414 | int j; | |
5415 | ||
5416 | /* Most arguments are cheap, so handle them specially. */ | |
5417 | switch (GET_CODE (arg)) | |
5418 | { | |
5419 | case REG: | |
5420 | /* This is the same as calling equiv_constant; it is duplicated | |
5421 | here for speed. */ | |
5422 | if (REGNO_QTY_VALID_P (REGNO (arg)) | |
30f72379 MM |
5423 | && qty_const[REG_QTY (REGNO (arg))] != 0 |
5424 | && GET_CODE (qty_const[REG_QTY (REGNO (arg))]) != REG | |
5425 | && GET_CODE (qty_const[REG_QTY (REGNO (arg))]) != PLUS) | |
7afe21cc RK |
5426 | const_arg |
5427 | = gen_lowpart_if_possible (GET_MODE (arg), | |
30f72379 | 5428 | qty_const[REG_QTY (REGNO (arg))]); |
7afe21cc RK |
5429 | break; |
5430 | ||
5431 | case CONST: | |
5432 | case CONST_INT: | |
5433 | case SYMBOL_REF: | |
5434 | case LABEL_REF: | |
5435 | case CONST_DOUBLE: | |
5436 | const_arg = arg; | |
5437 | break; | |
5438 | ||
5439 | #ifdef HAVE_cc0 | |
5440 | case CC0: | |
5441 | folded_arg = prev_insn_cc0; | |
5442 | mode_arg = prev_insn_cc0_mode; | |
5443 | const_arg = equiv_constant (folded_arg); | |
5444 | break; | |
5445 | #endif | |
5446 | ||
5447 | default: | |
5448 | folded_arg = fold_rtx (arg, insn); | |
5449 | const_arg = equiv_constant (folded_arg); | |
5450 | } | |
5451 | ||
5452 | /* For the first three operands, see if the operand | |
5453 | is constant or equivalent to a constant. */ | |
5454 | switch (i) | |
5455 | { | |
5456 | case 0: | |
5457 | folded_arg0 = folded_arg; | |
5458 | const_arg0 = const_arg; | |
5459 | mode_arg0 = mode_arg; | |
5460 | break; | |
5461 | case 1: | |
5462 | folded_arg1 = folded_arg; | |
5463 | const_arg1 = const_arg; | |
5464 | break; | |
5465 | case 2: | |
5466 | const_arg2 = const_arg; | |
5467 | break; | |
5468 | } | |
5469 | ||
5470 | /* Pick the least expensive of the folded argument and an | |
5471 | equivalent constant argument. */ | |
5472 | if (const_arg == 0 || const_arg == folded_arg | |
5473 | || COST (const_arg) > COST (folded_arg)) | |
5474 | cheap_arg = folded_arg, expensive_arg = const_arg; | |
5475 | else | |
5476 | cheap_arg = const_arg, expensive_arg = folded_arg; | |
5477 | ||
5478 | /* Try to replace the operand with the cheapest of the two | |
5479 | possibilities. If it doesn't work and this is either of the first | |
5480 | two operands of a commutative operation, try swapping them. | |
5481 | If THAT fails, try the more expensive, provided it is cheaper | |
5482 | than what is already there. */ | |
5483 | ||
5484 | if (cheap_arg == XEXP (x, i)) | |
5485 | continue; | |
5486 | ||
5487 | if (insn == 0 && ! copied) | |
5488 | { | |
5489 | x = copy_rtx (x); | |
5490 | copied = 1; | |
5491 | } | |
5492 | ||
5493 | replacements[0] = cheap_arg, replacements[1] = expensive_arg; | |
5494 | for (j = 0; | |
5495 | j < 2 && replacements[j] | |
5496 | && COST (replacements[j]) < COST (XEXP (x, i)); | |
5497 | j++) | |
5498 | { | |
5499 | if (validate_change (insn, &XEXP (x, i), replacements[j], 0)) | |
5500 | break; | |
5501 | ||
5502 | if (code == NE || code == EQ || GET_RTX_CLASS (code) == 'c') | |
5503 | { | |
5504 | validate_change (insn, &XEXP (x, i), XEXP (x, 1 - i), 1); | |
5505 | validate_change (insn, &XEXP (x, 1 - i), replacements[j], 1); | |
5506 | ||
5507 | if (apply_change_group ()) | |
5508 | { | |
5509 | /* Swap them back to be invalid so that this loop can | |
5510 | continue and flag them to be swapped back later. */ | |
5511 | rtx tem; | |
5512 | ||
5513 | tem = XEXP (x, 0); XEXP (x, 0) = XEXP (x, 1); | |
5514 | XEXP (x, 1) = tem; | |
5515 | must_swap = 1; | |
5516 | break; | |
5517 | } | |
5518 | } | |
5519 | } | |
5520 | } | |
5521 | ||
2d8b0f3a JL |
5522 | else |
5523 | { | |
5524 | if (fmt[i] == 'E') | |
5525 | /* Don't try to fold inside of a vector of expressions. | |
5526 | Doing nothing is harmless. */ | |
5527 | {;} | |
5528 | } | |
7afe21cc RK |
5529 | |
5530 | /* If a commutative operation, place a constant integer as the second | |
5531 | operand unless the first operand is also a constant integer. Otherwise, | |
5532 | place any constant second unless the first operand is also a constant. */ | |
5533 | ||
5534 | if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c') | |
5535 | { | |
5536 | if (must_swap || (const_arg0 | |
5537 | && (const_arg1 == 0 | |
5538 | || (GET_CODE (const_arg0) == CONST_INT | |
5539 | && GET_CODE (const_arg1) != CONST_INT)))) | |
5540 | { | |
5541 | register rtx tem = XEXP (x, 0); | |
5542 | ||
5543 | if (insn == 0 && ! copied) | |
5544 | { | |
5545 | x = copy_rtx (x); | |
5546 | copied = 1; | |
5547 | } | |
5548 | ||
5549 | validate_change (insn, &XEXP (x, 0), XEXP (x, 1), 1); | |
5550 | validate_change (insn, &XEXP (x, 1), tem, 1); | |
5551 | if (apply_change_group ()) | |
5552 | { | |
5553 | tem = const_arg0, const_arg0 = const_arg1, const_arg1 = tem; | |
5554 | tem = folded_arg0, folded_arg0 = folded_arg1, folded_arg1 = tem; | |
5555 | } | |
5556 | } | |
5557 | } | |
5558 | ||
5559 | /* If X is an arithmetic operation, see if we can simplify it. */ | |
5560 | ||
5561 | switch (GET_RTX_CLASS (code)) | |
5562 | { | |
5563 | case '1': | |
67a37737 RK |
5564 | { |
5565 | int is_const = 0; | |
5566 | ||
5567 | /* We can't simplify extension ops unless we know the | |
5568 | original mode. */ | |
5569 | if ((code == ZERO_EXTEND || code == SIGN_EXTEND) | |
5570 | && mode_arg0 == VOIDmode) | |
5571 | break; | |
5572 | ||
5573 | /* If we had a CONST, strip it off and put it back later if we | |
5574 | fold. */ | |
5575 | if (const_arg0 != 0 && GET_CODE (const_arg0) == CONST) | |
5576 | is_const = 1, const_arg0 = XEXP (const_arg0, 0); | |
5577 | ||
5578 | new = simplify_unary_operation (code, mode, | |
5579 | const_arg0 ? const_arg0 : folded_arg0, | |
5580 | mode_arg0); | |
5581 | if (new != 0 && is_const) | |
38a448ca | 5582 | new = gen_rtx_CONST (mode, new); |
67a37737 | 5583 | } |
7afe21cc RK |
5584 | break; |
5585 | ||
5586 | case '<': | |
5587 | /* See what items are actually being compared and set FOLDED_ARG[01] | |
5588 | to those values and CODE to the actual comparison code. If any are | |
5589 | constant, set CONST_ARG0 and CONST_ARG1 appropriately. We needn't | |
5590 | do anything if both operands are already known to be constant. */ | |
5591 | ||
5592 | if (const_arg0 == 0 || const_arg1 == 0) | |
5593 | { | |
5594 | struct table_elt *p0, *p1; | |
c610adec | 5595 | rtx true = const_true_rtx, false = const0_rtx; |
13c9910f | 5596 | enum machine_mode mode_arg1; |
c610adec RK |
5597 | |
5598 | #ifdef FLOAT_STORE_FLAG_VALUE | |
c7c955ee | 5599 | if (GET_MODE_CLASS (mode) == MODE_FLOAT) |
c610adec | 5600 | { |
560c94a2 RK |
5601 | true = CONST_DOUBLE_FROM_REAL_VALUE (FLOAT_STORE_FLAG_VALUE, |
5602 | mode); | |
c610adec RK |
5603 | false = CONST0_RTX (mode); |
5604 | } | |
5605 | #endif | |
7afe21cc | 5606 | |
13c9910f RS |
5607 | code = find_comparison_args (code, &folded_arg0, &folded_arg1, |
5608 | &mode_arg0, &mode_arg1); | |
7afe21cc RK |
5609 | const_arg0 = equiv_constant (folded_arg0); |
5610 | const_arg1 = equiv_constant (folded_arg1); | |
5611 | ||
13c9910f RS |
5612 | /* If the mode is VOIDmode or a MODE_CC mode, we don't know |
5613 | what kinds of things are being compared, so we can't do | |
5614 | anything with this comparison. */ | |
7afe21cc RK |
5615 | |
5616 | if (mode_arg0 == VOIDmode || GET_MODE_CLASS (mode_arg0) == MODE_CC) | |
5617 | break; | |
5618 | ||
0f41302f MS |
5619 | /* If we do not now have two constants being compared, see |
5620 | if we can nevertheless deduce some things about the | |
5621 | comparison. */ | |
7afe21cc RK |
5622 | if (const_arg0 == 0 || const_arg1 == 0) |
5623 | { | |
0f41302f MS |
5624 | /* Is FOLDED_ARG0 frame-pointer plus a constant? Or |
5625 | non-explicit constant? These aren't zero, but we | |
5626 | don't know their sign. */ | |
7afe21cc RK |
5627 | if (const_arg1 == const0_rtx |
5628 | && (NONZERO_BASE_PLUS_P (folded_arg0) | |
5629 | #if 0 /* Sad to say, on sysvr4, #pragma weak can make a symbol address | |
5630 | come out as 0. */ | |
5631 | || GET_CODE (folded_arg0) == SYMBOL_REF | |
5632 | #endif | |
5633 | || GET_CODE (folded_arg0) == LABEL_REF | |
5634 | || GET_CODE (folded_arg0) == CONST)) | |
5635 | { | |
5636 | if (code == EQ) | |
c610adec | 5637 | return false; |
7afe21cc | 5638 | else if (code == NE) |
c610adec | 5639 | return true; |
7afe21cc RK |
5640 | } |
5641 | ||
5642 | /* See if the two operands are the same. We don't do this | |
5643 | for IEEE floating-point since we can't assume x == x | |
5644 | since x might be a NaN. */ | |
5645 | ||
5646 | if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT | |
a83afb65 | 5647 | || ! FLOAT_MODE_P (mode_arg0) || flag_fast_math) |
7afe21cc RK |
5648 | && (folded_arg0 == folded_arg1 |
5649 | || (GET_CODE (folded_arg0) == REG | |
5650 | && GET_CODE (folded_arg1) == REG | |
30f72379 MM |
5651 | && (REG_QTY (REGNO (folded_arg0)) |
5652 | == REG_QTY (REGNO (folded_arg1)))) | |
7afe21cc RK |
5653 | || ((p0 = lookup (folded_arg0, |
5654 | (safe_hash (folded_arg0, mode_arg0) | |
5655 | % NBUCKETS), mode_arg0)) | |
5656 | && (p1 = lookup (folded_arg1, | |
5657 | (safe_hash (folded_arg1, mode_arg0) | |
5658 | % NBUCKETS), mode_arg0)) | |
5659 | && p0->first_same_value == p1->first_same_value))) | |
5660 | return ((code == EQ || code == LE || code == GE | |
5661 | || code == LEU || code == GEU) | |
c610adec | 5662 | ? true : false); |
7afe21cc RK |
5663 | |
5664 | /* If FOLDED_ARG0 is a register, see if the comparison we are | |
5665 | doing now is either the same as we did before or the reverse | |
5666 | (we only check the reverse if not floating-point). */ | |
5667 | else if (GET_CODE (folded_arg0) == REG) | |
5668 | { | |
30f72379 | 5669 | int qty = REG_QTY (REGNO (folded_arg0)); |
7afe21cc RK |
5670 | |
5671 | if (REGNO_QTY_VALID_P (REGNO (folded_arg0)) | |
5672 | && (comparison_dominates_p (qty_comparison_code[qty], code) | |
5673 | || (comparison_dominates_p (qty_comparison_code[qty], | |
5674 | reverse_condition (code)) | |
cbf6a543 | 5675 | && ! FLOAT_MODE_P (mode_arg0))) |
7afe21cc RK |
5676 | && (rtx_equal_p (qty_comparison_const[qty], folded_arg1) |
5677 | || (const_arg1 | |
5678 | && rtx_equal_p (qty_comparison_const[qty], | |
5679 | const_arg1)) | |
5680 | || (GET_CODE (folded_arg1) == REG | |
30f72379 | 5681 | && (REG_QTY (REGNO (folded_arg1)) |
7afe21cc RK |
5682 | == qty_comparison_qty[qty])))) |
5683 | return (comparison_dominates_p (qty_comparison_code[qty], | |
5684 | code) | |
c610adec | 5685 | ? true : false); |
7afe21cc RK |
5686 | } |
5687 | } | |
5688 | } | |
5689 | ||
5690 | /* If we are comparing against zero, see if the first operand is | |
5691 | equivalent to an IOR with a constant. If so, we may be able to | |
5692 | determine the result of this comparison. */ | |
5693 | ||
5694 | if (const_arg1 == const0_rtx) | |
5695 | { | |
5696 | rtx y = lookup_as_function (folded_arg0, IOR); | |
5697 | rtx inner_const; | |
5698 | ||
5699 | if (y != 0 | |
5700 | && (inner_const = equiv_constant (XEXP (y, 1))) != 0 | |
5701 | && GET_CODE (inner_const) == CONST_INT | |
5702 | && INTVAL (inner_const) != 0) | |
5703 | { | |
5704 | int sign_bitnum = GET_MODE_BITSIZE (mode_arg0) - 1; | |
906c4e36 RK |
5705 | int has_sign = (HOST_BITS_PER_WIDE_INT >= sign_bitnum |
5706 | && (INTVAL (inner_const) | |
5707 | & ((HOST_WIDE_INT) 1 << sign_bitnum))); | |
c610adec RK |
5708 | rtx true = const_true_rtx, false = const0_rtx; |
5709 | ||
5710 | #ifdef FLOAT_STORE_FLAG_VALUE | |
c7c955ee | 5711 | if (GET_MODE_CLASS (mode) == MODE_FLOAT) |
c610adec | 5712 | { |
560c94a2 RK |
5713 | true = CONST_DOUBLE_FROM_REAL_VALUE (FLOAT_STORE_FLAG_VALUE, |
5714 | mode); | |
c610adec RK |
5715 | false = CONST0_RTX (mode); |
5716 | } | |
5717 | #endif | |
7afe21cc RK |
5718 | |
5719 | switch (code) | |
5720 | { | |
5721 | case EQ: | |
c610adec | 5722 | return false; |
7afe21cc | 5723 | case NE: |
c610adec | 5724 | return true; |
7afe21cc RK |
5725 | case LT: case LE: |
5726 | if (has_sign) | |
c610adec | 5727 | return true; |
7afe21cc RK |
5728 | break; |
5729 | case GT: case GE: | |
5730 | if (has_sign) | |
c610adec | 5731 | return false; |
7afe21cc | 5732 | break; |
e9a25f70 JL |
5733 | default: |
5734 | break; | |
7afe21cc RK |
5735 | } |
5736 | } | |
5737 | } | |
5738 | ||
5739 | new = simplify_relational_operation (code, mode_arg0, | |
5740 | const_arg0 ? const_arg0 : folded_arg0, | |
5741 | const_arg1 ? const_arg1 : folded_arg1); | |
c610adec RK |
5742 | #ifdef FLOAT_STORE_FLAG_VALUE |
5743 | if (new != 0 && GET_MODE_CLASS (mode) == MODE_FLOAT) | |
5744 | new = ((new == const0_rtx) ? CONST0_RTX (mode) | |
560c94a2 | 5745 | : CONST_DOUBLE_FROM_REAL_VALUE (FLOAT_STORE_FLAG_VALUE, mode)); |
c610adec | 5746 | #endif |
7afe21cc RK |
5747 | break; |
5748 | ||
5749 | case '2': | |
5750 | case 'c': | |
5751 | switch (code) | |
5752 | { | |
5753 | case PLUS: | |
5754 | /* If the second operand is a LABEL_REF, see if the first is a MINUS | |
5755 | with that LABEL_REF as its second operand. If so, the result is | |
5756 | the first operand of that MINUS. This handles switches with an | |
5757 | ADDR_DIFF_VEC table. */ | |
5758 | if (const_arg1 && GET_CODE (const_arg1) == LABEL_REF) | |
5759 | { | |
e650cbda RK |
5760 | rtx y |
5761 | = GET_CODE (folded_arg0) == MINUS ? folded_arg0 | |
5762 | : lookup_as_function (folded_arg0, MINUS); | |
7afe21cc RK |
5763 | |
5764 | if (y != 0 && GET_CODE (XEXP (y, 1)) == LABEL_REF | |
5765 | && XEXP (XEXP (y, 1), 0) == XEXP (const_arg1, 0)) | |
5766 | return XEXP (y, 0); | |
67a37737 RK |
5767 | |
5768 | /* Now try for a CONST of a MINUS like the above. */ | |
e650cbda RK |
5769 | if ((y = (GET_CODE (folded_arg0) == CONST ? folded_arg0 |
5770 | : lookup_as_function (folded_arg0, CONST))) != 0 | |
67a37737 RK |
5771 | && GET_CODE (XEXP (y, 0)) == MINUS |
5772 | && GET_CODE (XEXP (XEXP (y, 0), 1)) == LABEL_REF | |
5773 | && XEXP (XEXP (XEXP (y, 0),1), 0) == XEXP (const_arg1, 0)) | |
5774 | return XEXP (XEXP (y, 0), 0); | |
7afe21cc | 5775 | } |
c2cc0778 | 5776 | |
e650cbda RK |
5777 | /* Likewise if the operands are in the other order. */ |
5778 | if (const_arg0 && GET_CODE (const_arg0) == LABEL_REF) | |
5779 | { | |
5780 | rtx y | |
5781 | = GET_CODE (folded_arg1) == MINUS ? folded_arg1 | |
5782 | : lookup_as_function (folded_arg1, MINUS); | |
5783 | ||
5784 | if (y != 0 && GET_CODE (XEXP (y, 1)) == LABEL_REF | |
5785 | && XEXP (XEXP (y, 1), 0) == XEXP (const_arg0, 0)) | |
5786 | return XEXP (y, 0); | |
5787 | ||
5788 | /* Now try for a CONST of a MINUS like the above. */ | |
5789 | if ((y = (GET_CODE (folded_arg1) == CONST ? folded_arg1 | |
5790 | : lookup_as_function (folded_arg1, CONST))) != 0 | |
5791 | && GET_CODE (XEXP (y, 0)) == MINUS | |
5792 | && GET_CODE (XEXP (XEXP (y, 0), 1)) == LABEL_REF | |
5793 | && XEXP (XEXP (XEXP (y, 0),1), 0) == XEXP (const_arg0, 0)) | |
5794 | return XEXP (XEXP (y, 0), 0); | |
5795 | } | |
5796 | ||
c2cc0778 RK |
5797 | /* If second operand is a register equivalent to a negative |
5798 | CONST_INT, see if we can find a register equivalent to the | |
5799 | positive constant. Make a MINUS if so. Don't do this for | |
5d595063 | 5800 | a non-negative constant since we might then alternate between |
c2cc0778 | 5801 | chosing positive and negative constants. Having the positive |
5d595063 RK |
5802 | constant previously-used is the more common case. Be sure |
5803 | the resulting constant is non-negative; if const_arg1 were | |
5804 | the smallest negative number this would overflow: depending | |
5805 | on the mode, this would either just be the same value (and | |
5806 | hence not save anything) or be incorrect. */ | |
5807 | if (const_arg1 != 0 && GET_CODE (const_arg1) == CONST_INT | |
5808 | && INTVAL (const_arg1) < 0 | |
4741f6ad JL |
5809 | /* This used to test |
5810 | ||
5811 | - INTVAL (const_arg1) >= 0 | |
5812 | ||
5813 | But The Sun V5.0 compilers mis-compiled that test. So | |
5814 | instead we test for the problematic value in a more direct | |
5815 | manner and hope the Sun compilers get it correct. */ | |
5c45a8ac KG |
5816 | && INTVAL (const_arg1) != |
5817 | ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)) | |
5d595063 | 5818 | && GET_CODE (folded_arg1) == REG) |
c2cc0778 RK |
5819 | { |
5820 | rtx new_const = GEN_INT (- INTVAL (const_arg1)); | |
5821 | struct table_elt *p | |
5822 | = lookup (new_const, safe_hash (new_const, mode) % NBUCKETS, | |
5823 | mode); | |
5824 | ||
5825 | if (p) | |
5826 | for (p = p->first_same_value; p; p = p->next_same_value) | |
5827 | if (GET_CODE (p->exp) == REG) | |
5828 | return cse_gen_binary (MINUS, mode, folded_arg0, | |
5829 | canon_reg (p->exp, NULL_RTX)); | |
5830 | } | |
13c9910f RS |
5831 | goto from_plus; |
5832 | ||
5833 | case MINUS: | |
5834 | /* If we have (MINUS Y C), see if Y is known to be (PLUS Z C2). | |
5835 | If so, produce (PLUS Z C2-C). */ | |
5836 | if (const_arg1 != 0 && GET_CODE (const_arg1) == CONST_INT) | |
5837 | { | |
5838 | rtx y = lookup_as_function (XEXP (x, 0), PLUS); | |
5839 | if (y && GET_CODE (XEXP (y, 1)) == CONST_INT) | |
f3becefd RK |
5840 | return fold_rtx (plus_constant (copy_rtx (y), |
5841 | -INTVAL (const_arg1)), | |
a3b5c94a | 5842 | NULL_RTX); |
13c9910f | 5843 | } |
7afe21cc | 5844 | |
0f41302f | 5845 | /* ... fall through ... */ |
7afe21cc | 5846 | |
13c9910f | 5847 | from_plus: |
7afe21cc RK |
5848 | case SMIN: case SMAX: case UMIN: case UMAX: |
5849 | case IOR: case AND: case XOR: | |
5850 | case MULT: case DIV: case UDIV: | |
5851 | case ASHIFT: case LSHIFTRT: case ASHIFTRT: | |
5852 | /* If we have (<op> <reg> <const_int>) for an associative OP and REG | |
5853 | is known to be of similar form, we may be able to replace the | |
5854 | operation with a combined operation. This may eliminate the | |
5855 | intermediate operation if every use is simplified in this way. | |
5856 | Note that the similar optimization done by combine.c only works | |
5857 | if the intermediate operation's result has only one reference. */ | |
5858 | ||
5859 | if (GET_CODE (folded_arg0) == REG | |
5860 | && const_arg1 && GET_CODE (const_arg1) == CONST_INT) | |
5861 | { | |
5862 | int is_shift | |
5863 | = (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT); | |
5864 | rtx y = lookup_as_function (folded_arg0, code); | |
5865 | rtx inner_const; | |
5866 | enum rtx_code associate_code; | |
5867 | rtx new_const; | |
5868 | ||
5869 | if (y == 0 | |
5870 | || 0 == (inner_const | |
5871 | = equiv_constant (fold_rtx (XEXP (y, 1), 0))) | |
5872 | || GET_CODE (inner_const) != CONST_INT | |
5873 | /* If we have compiled a statement like | |
5874 | "if (x == (x & mask1))", and now are looking at | |
5875 | "x & mask2", we will have a case where the first operand | |
5876 | of Y is the same as our first operand. Unless we detect | |
5877 | this case, an infinite loop will result. */ | |
5878 | || XEXP (y, 0) == folded_arg0) | |
5879 | break; | |
5880 | ||
5881 | /* Don't associate these operations if they are a PLUS with the | |
5882 | same constant and it is a power of two. These might be doable | |
5883 | with a pre- or post-increment. Similarly for two subtracts of | |
5884 | identical powers of two with post decrement. */ | |
5885 | ||
5886 | if (code == PLUS && INTVAL (const_arg1) == INTVAL (inner_const) | |
940da324 JL |
5887 | && ((HAVE_PRE_INCREMENT |
5888 | && exact_log2 (INTVAL (const_arg1)) >= 0) | |
5889 | || (HAVE_POST_INCREMENT | |
5890 | && exact_log2 (INTVAL (const_arg1)) >= 0) | |
5891 | || (HAVE_PRE_DECREMENT | |
5892 | && exact_log2 (- INTVAL (const_arg1)) >= 0) | |
5893 | || (HAVE_POST_DECREMENT | |
5894 | && exact_log2 (- INTVAL (const_arg1)) >= 0))) | |
7afe21cc RK |
5895 | break; |
5896 | ||
5897 | /* Compute the code used to compose the constants. For example, | |
5898 | A/C1/C2 is A/(C1 * C2), so if CODE == DIV, we want MULT. */ | |
5899 | ||
5900 | associate_code | |
5901 | = (code == MULT || code == DIV || code == UDIV ? MULT | |
5902 | : is_shift || code == PLUS || code == MINUS ? PLUS : code); | |
5903 | ||
5904 | new_const = simplify_binary_operation (associate_code, mode, | |
5905 | const_arg1, inner_const); | |
5906 | ||
5907 | if (new_const == 0) | |
5908 | break; | |
5909 | ||
5910 | /* If we are associating shift operations, don't let this | |
4908e508 RS |
5911 | produce a shift of the size of the object or larger. |
5912 | This could occur when we follow a sign-extend by a right | |
5913 | shift on a machine that does a sign-extend as a pair | |
5914 | of shifts. */ | |
7afe21cc RK |
5915 | |
5916 | if (is_shift && GET_CODE (new_const) == CONST_INT | |
4908e508 RS |
5917 | && INTVAL (new_const) >= GET_MODE_BITSIZE (mode)) |
5918 | { | |
5919 | /* As an exception, we can turn an ASHIFTRT of this | |
5920 | form into a shift of the number of bits - 1. */ | |
5921 | if (code == ASHIFTRT) | |
5922 | new_const = GEN_INT (GET_MODE_BITSIZE (mode) - 1); | |
5923 | else | |
5924 | break; | |
5925 | } | |
7afe21cc RK |
5926 | |
5927 | y = copy_rtx (XEXP (y, 0)); | |
5928 | ||
5929 | /* If Y contains our first operand (the most common way this | |
5930 | can happen is if Y is a MEM), we would do into an infinite | |
5931 | loop if we tried to fold it. So don't in that case. */ | |
5932 | ||
5933 | if (! reg_mentioned_p (folded_arg0, y)) | |
5934 | y = fold_rtx (y, insn); | |
5935 | ||
96b0e481 | 5936 | return cse_gen_binary (code, mode, y, new_const); |
7afe21cc | 5937 | } |
e9a25f70 JL |
5938 | break; |
5939 | ||
5940 | default: | |
5941 | break; | |
7afe21cc RK |
5942 | } |
5943 | ||
5944 | new = simplify_binary_operation (code, mode, | |
5945 | const_arg0 ? const_arg0 : folded_arg0, | |
5946 | const_arg1 ? const_arg1 : folded_arg1); | |
5947 | break; | |
5948 | ||
5949 | case 'o': | |
5950 | /* (lo_sum (high X) X) is simply X. */ | |
5951 | if (code == LO_SUM && const_arg0 != 0 | |
5952 | && GET_CODE (const_arg0) == HIGH | |
5953 | && rtx_equal_p (XEXP (const_arg0, 0), const_arg1)) | |
5954 | return const_arg1; | |
5955 | break; | |
5956 | ||
5957 | case '3': | |
5958 | case 'b': | |
5959 | new = simplify_ternary_operation (code, mode, mode_arg0, | |
5960 | const_arg0 ? const_arg0 : folded_arg0, | |
5961 | const_arg1 ? const_arg1 : folded_arg1, | |
5962 | const_arg2 ? const_arg2 : XEXP (x, 2)); | |
5963 | break; | |
ee5332b8 RH |
5964 | |
5965 | case 'x': | |
5966 | /* Always eliminate CONSTANT_P_RTX at this stage. */ | |
5967 | if (code == CONSTANT_P_RTX) | |
5968 | return (const_arg0 ? const1_rtx : const0_rtx); | |
5969 | break; | |
7afe21cc RK |
5970 | } |
5971 | ||
5972 | return new ? new : x; | |
5973 | } | |
5974 | \f | |
5975 | /* Return a constant value currently equivalent to X. | |
5976 | Return 0 if we don't know one. */ | |
5977 | ||
5978 | static rtx | |
5979 | equiv_constant (x) | |
5980 | rtx x; | |
5981 | { | |
5982 | if (GET_CODE (x) == REG | |
5983 | && REGNO_QTY_VALID_P (REGNO (x)) | |
30f72379 MM |
5984 | && qty_const[REG_QTY (REGNO (x))]) |
5985 | x = gen_lowpart_if_possible (GET_MODE (x), qty_const[REG_QTY (REGNO (x))]); | |
7afe21cc | 5986 | |
2ce5e1b4 | 5987 | if (x == 0 || CONSTANT_P (x)) |
7afe21cc RK |
5988 | return x; |
5989 | ||
fc3ffe83 RK |
5990 | /* If X is a MEM, try to fold it outside the context of any insn to see if |
5991 | it might be equivalent to a constant. That handles the case where it | |
5992 | is a constant-pool reference. Then try to look it up in the hash table | |
5993 | in case it is something whose value we have seen before. */ | |
5994 | ||
5995 | if (GET_CODE (x) == MEM) | |
5996 | { | |
5997 | struct table_elt *elt; | |
5998 | ||
906c4e36 | 5999 | x = fold_rtx (x, NULL_RTX); |
fc3ffe83 RK |
6000 | if (CONSTANT_P (x)) |
6001 | return x; | |
6002 | ||
6003 | elt = lookup (x, safe_hash (x, GET_MODE (x)) % NBUCKETS, GET_MODE (x)); | |
6004 | if (elt == 0) | |
6005 | return 0; | |
6006 | ||
6007 | for (elt = elt->first_same_value; elt; elt = elt->next_same_value) | |
6008 | if (elt->is_const && CONSTANT_P (elt->exp)) | |
6009 | return elt->exp; | |
6010 | } | |
6011 | ||
7afe21cc RK |
6012 | return 0; |
6013 | } | |
6014 | \f | |
6015 | /* Assuming that X is an rtx (e.g., MEM, REG or SUBREG) for a fixed-point | |
6016 | number, return an rtx (MEM, SUBREG, or CONST_INT) that refers to the | |
6017 | least-significant part of X. | |
6018 | MODE specifies how big a part of X to return. | |
6019 | ||
6020 | If the requested operation cannot be done, 0 is returned. | |
6021 | ||
6022 | This is similar to gen_lowpart in emit-rtl.c. */ | |
6023 | ||
6024 | rtx | |
6025 | gen_lowpart_if_possible (mode, x) | |
6026 | enum machine_mode mode; | |
6027 | register rtx x; | |
6028 | { | |
6029 | rtx result = gen_lowpart_common (mode, x); | |
6030 | ||
6031 | if (result) | |
6032 | return result; | |
6033 | else if (GET_CODE (x) == MEM) | |
6034 | { | |
6035 | /* This is the only other case we handle. */ | |
6036 | register int offset = 0; | |
6037 | rtx new; | |
6038 | ||
f76b9db2 ILT |
6039 | if (WORDS_BIG_ENDIAN) |
6040 | offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD) | |
6041 | - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD)); | |
6042 | if (BYTES_BIG_ENDIAN) | |
6043 | /* Adjust the address so that the address-after-the-data is | |
6044 | unchanged. */ | |
6045 | offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)) | |
6046 | - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x)))); | |
38a448ca | 6047 | new = gen_rtx_MEM (mode, plus_constant (XEXP (x, 0), offset)); |
7afe21cc RK |
6048 | if (! memory_address_p (mode, XEXP (new, 0))) |
6049 | return 0; | |
7afe21cc | 6050 | RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x); |
c6df88cb | 6051 | MEM_COPY_ATTRIBUTES (new, x); |
7afe21cc RK |
6052 | return new; |
6053 | } | |
6054 | else | |
6055 | return 0; | |
6056 | } | |
6057 | \f | |
6058 | /* Given INSN, a jump insn, TAKEN indicates if we are following the "taken" | |
6059 | branch. It will be zero if not. | |
6060 | ||
6061 | In certain cases, this can cause us to add an equivalence. For example, | |
6062 | if we are following the taken case of | |
6063 | if (i == 2) | |
6064 | we can add the fact that `i' and '2' are now equivalent. | |
6065 | ||
6066 | In any case, we can record that this comparison was passed. If the same | |
6067 | comparison is seen later, we will know its value. */ | |
6068 | ||
6069 | static void | |
6070 | record_jump_equiv (insn, taken) | |
6071 | rtx insn; | |
6072 | int taken; | |
6073 | { | |
6074 | int cond_known_true; | |
6075 | rtx op0, op1; | |
13c9910f | 6076 | enum machine_mode mode, mode0, mode1; |
7afe21cc RK |
6077 | int reversed_nonequality = 0; |
6078 | enum rtx_code code; | |
6079 | ||
6080 | /* Ensure this is the right kind of insn. */ | |
6081 | if (! condjump_p (insn) || simplejump_p (insn)) | |
6082 | return; | |
6083 | ||
6084 | /* See if this jump condition is known true or false. */ | |
6085 | if (taken) | |
6086 | cond_known_true = (XEXP (SET_SRC (PATTERN (insn)), 2) == pc_rtx); | |
6087 | else | |
6088 | cond_known_true = (XEXP (SET_SRC (PATTERN (insn)), 1) == pc_rtx); | |
6089 | ||
6090 | /* Get the type of comparison being done and the operands being compared. | |
6091 | If we had to reverse a non-equality condition, record that fact so we | |
6092 | know that it isn't valid for floating-point. */ | |
6093 | code = GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 0)); | |
6094 | op0 = fold_rtx (XEXP (XEXP (SET_SRC (PATTERN (insn)), 0), 0), insn); | |
6095 | op1 = fold_rtx (XEXP (XEXP (SET_SRC (PATTERN (insn)), 0), 1), insn); | |
6096 | ||
13c9910f | 6097 | code = find_comparison_args (code, &op0, &op1, &mode0, &mode1); |
7afe21cc RK |
6098 | if (! cond_known_true) |
6099 | { | |
6100 | reversed_nonequality = (code != EQ && code != NE); | |
6101 | code = reverse_condition (code); | |
6102 | } | |
6103 | ||
6104 | /* The mode is the mode of the non-constant. */ | |
13c9910f RS |
6105 | mode = mode0; |
6106 | if (mode1 != VOIDmode) | |
6107 | mode = mode1; | |
7afe21cc RK |
6108 | |
6109 | record_jump_cond (code, mode, op0, op1, reversed_nonequality); | |
6110 | } | |
6111 | ||
6112 | /* We know that comparison CODE applied to OP0 and OP1 in MODE is true. | |
6113 | REVERSED_NONEQUALITY is nonzero if CODE had to be swapped. | |
6114 | Make any useful entries we can with that information. Called from | |
6115 | above function and called recursively. */ | |
6116 | ||
6117 | static void | |
6118 | record_jump_cond (code, mode, op0, op1, reversed_nonequality) | |
6119 | enum rtx_code code; | |
6120 | enum machine_mode mode; | |
6121 | rtx op0, op1; | |
6122 | int reversed_nonequality; | |
6123 | { | |
2197a88a | 6124 | unsigned op0_hash, op1_hash; |
7afe21cc RK |
6125 | int op0_in_memory, op0_in_struct, op1_in_memory, op1_in_struct; |
6126 | struct table_elt *op0_elt, *op1_elt; | |
6127 | ||
6128 | /* If OP0 and OP1 are known equal, and either is a paradoxical SUBREG, | |
6129 | we know that they are also equal in the smaller mode (this is also | |
6130 | true for all smaller modes whether or not there is a SUBREG, but | |
ac7ef8d5 | 6131 | is not worth testing for with no SUBREG). */ |
7afe21cc | 6132 | |
2e794ee8 | 6133 | /* Note that GET_MODE (op0) may not equal MODE. */ |
7afe21cc | 6134 | if (code == EQ && GET_CODE (op0) == SUBREG |
2e794ee8 RS |
6135 | && (GET_MODE_SIZE (GET_MODE (op0)) |
6136 | > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))) | |
7afe21cc RK |
6137 | { |
6138 | enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0)); | |
6139 | rtx tem = gen_lowpart_if_possible (inner_mode, op1); | |
6140 | ||
6141 | record_jump_cond (code, mode, SUBREG_REG (op0), | |
38a448ca | 6142 | tem ? tem : gen_rtx_SUBREG (inner_mode, op1, 0), |
7afe21cc RK |
6143 | reversed_nonequality); |
6144 | } | |
6145 | ||
6146 | if (code == EQ && GET_CODE (op1) == SUBREG | |
2e794ee8 RS |
6147 | && (GET_MODE_SIZE (GET_MODE (op1)) |
6148 | > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op1))))) | |
7afe21cc RK |
6149 | { |
6150 | enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op1)); | |
6151 | rtx tem = gen_lowpart_if_possible (inner_mode, op0); | |
6152 | ||
6153 | record_jump_cond (code, mode, SUBREG_REG (op1), | |
38a448ca | 6154 | tem ? tem : gen_rtx_SUBREG (inner_mode, op0, 0), |
7afe21cc RK |
6155 | reversed_nonequality); |
6156 | } | |
6157 | ||
6158 | /* Similarly, if this is an NE comparison, and either is a SUBREG | |
6159 | making a smaller mode, we know the whole thing is also NE. */ | |
6160 | ||
2e794ee8 RS |
6161 | /* Note that GET_MODE (op0) may not equal MODE; |
6162 | if we test MODE instead, we can get an infinite recursion | |
6163 | alternating between two modes each wider than MODE. */ | |
6164 | ||
7afe21cc RK |
6165 | if (code == NE && GET_CODE (op0) == SUBREG |
6166 | && subreg_lowpart_p (op0) | |
2e794ee8 RS |
6167 | && (GET_MODE_SIZE (GET_MODE (op0)) |
6168 | < GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))) | |
7afe21cc RK |
6169 | { |
6170 | enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0)); | |
6171 | rtx tem = gen_lowpart_if_possible (inner_mode, op1); | |
6172 | ||
6173 | record_jump_cond (code, mode, SUBREG_REG (op0), | |
38a448ca | 6174 | tem ? tem : gen_rtx_SUBREG (inner_mode, op1, 0), |
7afe21cc RK |
6175 | reversed_nonequality); |
6176 | } | |
6177 | ||
6178 | if (code == NE && GET_CODE (op1) == SUBREG | |
6179 | && subreg_lowpart_p (op1) | |
2e794ee8 RS |
6180 | && (GET_MODE_SIZE (GET_MODE (op1)) |
6181 | < GET_MODE_SIZE (GET_MODE (SUBREG_REG (op1))))) | |
7afe21cc RK |
6182 | { |
6183 | enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op1)); | |
6184 | rtx tem = gen_lowpart_if_possible (inner_mode, op0); | |
6185 | ||
6186 | record_jump_cond (code, mode, SUBREG_REG (op1), | |
38a448ca | 6187 | tem ? tem : gen_rtx_SUBREG (inner_mode, op0, 0), |
7afe21cc RK |
6188 | reversed_nonequality); |
6189 | } | |
6190 | ||
6191 | /* Hash both operands. */ | |
6192 | ||
6193 | do_not_record = 0; | |
6194 | hash_arg_in_memory = 0; | |
6195 | hash_arg_in_struct = 0; | |
2197a88a | 6196 | op0_hash = HASH (op0, mode); |
7afe21cc RK |
6197 | op0_in_memory = hash_arg_in_memory; |
6198 | op0_in_struct = hash_arg_in_struct; | |
6199 | ||
6200 | if (do_not_record) | |
6201 | return; | |
6202 | ||
6203 | do_not_record = 0; | |
6204 | hash_arg_in_memory = 0; | |
6205 | hash_arg_in_struct = 0; | |
2197a88a | 6206 | op1_hash = HASH (op1, mode); |
7afe21cc RK |
6207 | op1_in_memory = hash_arg_in_memory; |
6208 | op1_in_struct = hash_arg_in_struct; | |
6209 | ||
6210 | if (do_not_record) | |
6211 | return; | |
6212 | ||
6213 | /* Look up both operands. */ | |
2197a88a RK |
6214 | op0_elt = lookup (op0, op0_hash, mode); |
6215 | op1_elt = lookup (op1, op1_hash, mode); | |
7afe21cc | 6216 | |
af3869c1 RK |
6217 | /* If both operands are already equivalent or if they are not in the |
6218 | table but are identical, do nothing. */ | |
6219 | if ((op0_elt != 0 && op1_elt != 0 | |
6220 | && op0_elt->first_same_value == op1_elt->first_same_value) | |
6221 | || op0 == op1 || rtx_equal_p (op0, op1)) | |
6222 | return; | |
6223 | ||
7afe21cc | 6224 | /* If we aren't setting two things equal all we can do is save this |
b2796a4b RK |
6225 | comparison. Similarly if this is floating-point. In the latter |
6226 | case, OP1 might be zero and both -0.0 and 0.0 are equal to it. | |
6227 | If we record the equality, we might inadvertently delete code | |
6228 | whose intent was to change -0 to +0. */ | |
6229 | ||
cbf6a543 | 6230 | if (code != EQ || FLOAT_MODE_P (GET_MODE (op0))) |
7afe21cc RK |
6231 | { |
6232 | /* If we reversed a floating-point comparison, if OP0 is not a | |
6233 | register, or if OP1 is neither a register or constant, we can't | |
6234 | do anything. */ | |
6235 | ||
6236 | if (GET_CODE (op1) != REG) | |
6237 | op1 = equiv_constant (op1); | |
6238 | ||
cbf6a543 | 6239 | if ((reversed_nonequality && FLOAT_MODE_P (mode)) |
7afe21cc RK |
6240 | || GET_CODE (op0) != REG || op1 == 0) |
6241 | return; | |
6242 | ||
6243 | /* Put OP0 in the hash table if it isn't already. This gives it a | |
6244 | new quantity number. */ | |
6245 | if (op0_elt == 0) | |
6246 | { | |
906c4e36 | 6247 | if (insert_regs (op0, NULL_PTR, 0)) |
7afe21cc RK |
6248 | { |
6249 | rehash_using_reg (op0); | |
2197a88a | 6250 | op0_hash = HASH (op0, mode); |
2bb81c86 RK |
6251 | |
6252 | /* If OP0 is contained in OP1, this changes its hash code | |
6253 | as well. Faster to rehash than to check, except | |
6254 | for the simple case of a constant. */ | |
6255 | if (! CONSTANT_P (op1)) | |
2197a88a | 6256 | op1_hash = HASH (op1,mode); |
7afe21cc RK |
6257 | } |
6258 | ||
2197a88a | 6259 | op0_elt = insert (op0, NULL_PTR, op0_hash, mode); |
7afe21cc RK |
6260 | op0_elt->in_memory = op0_in_memory; |
6261 | op0_elt->in_struct = op0_in_struct; | |
6262 | } | |
6263 | ||
30f72379 | 6264 | qty_comparison_code[REG_QTY (REGNO (op0))] = code; |
7afe21cc RK |
6265 | if (GET_CODE (op1) == REG) |
6266 | { | |
5d5ea909 | 6267 | /* Look it up again--in case op0 and op1 are the same. */ |
2197a88a | 6268 | op1_elt = lookup (op1, op1_hash, mode); |
5d5ea909 | 6269 | |
7afe21cc RK |
6270 | /* Put OP1 in the hash table so it gets a new quantity number. */ |
6271 | if (op1_elt == 0) | |
6272 | { | |
906c4e36 | 6273 | if (insert_regs (op1, NULL_PTR, 0)) |
7afe21cc RK |
6274 | { |
6275 | rehash_using_reg (op1); | |
2197a88a | 6276 | op1_hash = HASH (op1, mode); |
7afe21cc RK |
6277 | } |
6278 | ||
2197a88a | 6279 | op1_elt = insert (op1, NULL_PTR, op1_hash, mode); |
7afe21cc RK |
6280 | op1_elt->in_memory = op1_in_memory; |
6281 | op1_elt->in_struct = op1_in_struct; | |
6282 | } | |
6283 | ||
30f72379 MM |
6284 | qty_comparison_qty[REG_QTY (REGNO (op0))] = REG_QTY (REGNO (op1)); |
6285 | qty_comparison_const[REG_QTY (REGNO (op0))] = 0; | |
7afe21cc RK |
6286 | } |
6287 | else | |
6288 | { | |
30f72379 MM |
6289 | qty_comparison_qty[REG_QTY (REGNO (op0))] = -1; |
6290 | qty_comparison_const[REG_QTY (REGNO (op0))] = op1; | |
7afe21cc RK |
6291 | } |
6292 | ||
6293 | return; | |
6294 | } | |
6295 | ||
eb5ad42a RS |
6296 | /* If either side is still missing an equivalence, make it now, |
6297 | then merge the equivalences. */ | |
7afe21cc | 6298 | |
7afe21cc RK |
6299 | if (op0_elt == 0) |
6300 | { | |
eb5ad42a | 6301 | if (insert_regs (op0, NULL_PTR, 0)) |
7afe21cc RK |
6302 | { |
6303 | rehash_using_reg (op0); | |
2197a88a | 6304 | op0_hash = HASH (op0, mode); |
7afe21cc RK |
6305 | } |
6306 | ||
2197a88a | 6307 | op0_elt = insert (op0, NULL_PTR, op0_hash, mode); |
7afe21cc RK |
6308 | op0_elt->in_memory = op0_in_memory; |
6309 | op0_elt->in_struct = op0_in_struct; | |
7afe21cc RK |
6310 | } |
6311 | ||
6312 | if (op1_elt == 0) | |
6313 | { | |
eb5ad42a | 6314 | if (insert_regs (op1, NULL_PTR, 0)) |
7afe21cc RK |
6315 | { |
6316 | rehash_using_reg (op1); | |
2197a88a | 6317 | op1_hash = HASH (op1, mode); |
7afe21cc RK |
6318 | } |
6319 | ||
2197a88a | 6320 | op1_elt = insert (op1, NULL_PTR, op1_hash, mode); |
7afe21cc RK |
6321 | op1_elt->in_memory = op1_in_memory; |
6322 | op1_elt->in_struct = op1_in_struct; | |
7afe21cc | 6323 | } |
eb5ad42a RS |
6324 | |
6325 | merge_equiv_classes (op0_elt, op1_elt); | |
6326 | last_jump_equiv_class = op0_elt; | |
7afe21cc RK |
6327 | } |
6328 | \f | |
6329 | /* CSE processing for one instruction. | |
6330 | First simplify sources and addresses of all assignments | |
6331 | in the instruction, using previously-computed equivalents values. | |
6332 | Then install the new sources and destinations in the table | |
6333 | of available values. | |
6334 | ||
1ed0205e VM |
6335 | If LIBCALL_INSN is nonzero, don't record any equivalence made in |
6336 | the insn. It means that INSN is inside libcall block. In this | |
6337 | case LIBCALL_INSN is the corresponding insn with REG_LIBCALL. */ | |
7afe21cc RK |
6338 | |
6339 | /* Data on one SET contained in the instruction. */ | |
6340 | ||
6341 | struct set | |
6342 | { | |
6343 | /* The SET rtx itself. */ | |
6344 | rtx rtl; | |
6345 | /* The SET_SRC of the rtx (the original value, if it is changing). */ | |
6346 | rtx src; | |
6347 | /* The hash-table element for the SET_SRC of the SET. */ | |
6348 | struct table_elt *src_elt; | |
2197a88a RK |
6349 | /* Hash value for the SET_SRC. */ |
6350 | unsigned src_hash; | |
6351 | /* Hash value for the SET_DEST. */ | |
6352 | unsigned dest_hash; | |
7afe21cc RK |
6353 | /* The SET_DEST, with SUBREG, etc., stripped. */ |
6354 | rtx inner_dest; | |
6355 | /* Place where the pointer to the INNER_DEST was found. */ | |
6356 | rtx *inner_dest_loc; | |
6357 | /* Nonzero if the SET_SRC is in memory. */ | |
6358 | char src_in_memory; | |
6359 | /* Nonzero if the SET_SRC is in a structure. */ | |
6360 | char src_in_struct; | |
6361 | /* Nonzero if the SET_SRC contains something | |
6362 | whose value cannot be predicted and understood. */ | |
6363 | char src_volatile; | |
6364 | /* Original machine mode, in case it becomes a CONST_INT. */ | |
6365 | enum machine_mode mode; | |
6366 | /* A constant equivalent for SET_SRC, if any. */ | |
6367 | rtx src_const; | |
2197a88a RK |
6368 | /* Hash value of constant equivalent for SET_SRC. */ |
6369 | unsigned src_const_hash; | |
7afe21cc RK |
6370 | /* Table entry for constant equivalent for SET_SRC, if any. */ |
6371 | struct table_elt *src_const_elt; | |
6372 | }; | |
6373 | ||
6374 | static void | |
7bd8b2a8 | 6375 | cse_insn (insn, libcall_insn) |
7afe21cc | 6376 | rtx insn; |
7bd8b2a8 | 6377 | rtx libcall_insn; |
7afe21cc RK |
6378 | { |
6379 | register rtx x = PATTERN (insn); | |
7afe21cc | 6380 | register int i; |
92f9aa51 | 6381 | rtx tem; |
7afe21cc RK |
6382 | register int n_sets = 0; |
6383 | ||
2d8b0f3a | 6384 | #ifdef HAVE_cc0 |
7afe21cc RK |
6385 | /* Records what this insn does to set CC0. */ |
6386 | rtx this_insn_cc0 = 0; | |
135d84b8 | 6387 | enum machine_mode this_insn_cc0_mode = VOIDmode; |
2d8b0f3a | 6388 | #endif |
7afe21cc RK |
6389 | |
6390 | rtx src_eqv = 0; | |
6391 | struct table_elt *src_eqv_elt = 0; | |
6a651371 KG |
6392 | int src_eqv_volatile = 0; |
6393 | int src_eqv_in_memory = 0; | |
6394 | int src_eqv_in_struct = 0; | |
6395 | unsigned src_eqv_hash = 0; | |
7afe21cc | 6396 | |
6a651371 | 6397 | struct set *sets = NULL_PTR; |
7afe21cc RK |
6398 | |
6399 | this_insn = insn; | |
7afe21cc RK |
6400 | |
6401 | /* Find all the SETs and CLOBBERs in this instruction. | |
6402 | Record all the SETs in the array `set' and count them. | |
6403 | Also determine whether there is a CLOBBER that invalidates | |
6404 | all memory references, or all references at varying addresses. */ | |
6405 | ||
f1e7c95f RK |
6406 | if (GET_CODE (insn) == CALL_INSN) |
6407 | { | |
6408 | for (tem = CALL_INSN_FUNCTION_USAGE (insn); tem; tem = XEXP (tem, 1)) | |
6409 | if (GET_CODE (XEXP (tem, 0)) == CLOBBER) | |
bb4034b3 | 6410 | invalidate (SET_DEST (XEXP (tem, 0)), VOIDmode); |
f1e7c95f RK |
6411 | } |
6412 | ||
7afe21cc RK |
6413 | if (GET_CODE (x) == SET) |
6414 | { | |
6415 | sets = (struct set *) alloca (sizeof (struct set)); | |
6416 | sets[0].rtl = x; | |
6417 | ||
6418 | /* Ignore SETs that are unconditional jumps. | |
6419 | They never need cse processing, so this does not hurt. | |
6420 | The reason is not efficiency but rather | |
6421 | so that we can test at the end for instructions | |
6422 | that have been simplified to unconditional jumps | |
6423 | and not be misled by unchanged instructions | |
6424 | that were unconditional jumps to begin with. */ | |
6425 | if (SET_DEST (x) == pc_rtx | |
6426 | && GET_CODE (SET_SRC (x)) == LABEL_REF) | |
6427 | ; | |
6428 | ||
6429 | /* Don't count call-insns, (set (reg 0) (call ...)), as a set. | |
6430 | The hard function value register is used only once, to copy to | |
6431 | someplace else, so it isn't worth cse'ing (and on 80386 is unsafe)! | |
6432 | Ensure we invalidate the destination register. On the 80386 no | |
7722328e | 6433 | other code would invalidate it since it is a fixed_reg. |
0f41302f | 6434 | We need not check the return of apply_change_group; see canon_reg. */ |
7afe21cc RK |
6435 | |
6436 | else if (GET_CODE (SET_SRC (x)) == CALL) | |
6437 | { | |
6438 | canon_reg (SET_SRC (x), insn); | |
77fa0940 | 6439 | apply_change_group (); |
7afe21cc | 6440 | fold_rtx (SET_SRC (x), insn); |
bb4034b3 | 6441 | invalidate (SET_DEST (x), VOIDmode); |
7afe21cc RK |
6442 | } |
6443 | else | |
6444 | n_sets = 1; | |
6445 | } | |
6446 | else if (GET_CODE (x) == PARALLEL) | |
6447 | { | |
6448 | register int lim = XVECLEN (x, 0); | |
6449 | ||
6450 | sets = (struct set *) alloca (lim * sizeof (struct set)); | |
6451 | ||
6452 | /* Find all regs explicitly clobbered in this insn, | |
6453 | and ensure they are not replaced with any other regs | |
6454 | elsewhere in this insn. | |
6455 | When a reg that is clobbered is also used for input, | |
6456 | we should presume that that is for a reason, | |
6457 | and we should not substitute some other register | |
6458 | which is not supposed to be clobbered. | |
6459 | Therefore, this loop cannot be merged into the one below | |
830a38ee | 6460 | because a CALL may precede a CLOBBER and refer to the |
7afe21cc RK |
6461 | value clobbered. We must not let a canonicalization do |
6462 | anything in that case. */ | |
6463 | for (i = 0; i < lim; i++) | |
6464 | { | |
6465 | register rtx y = XVECEXP (x, 0, i); | |
2708da92 RS |
6466 | if (GET_CODE (y) == CLOBBER) |
6467 | { | |
6468 | rtx clobbered = XEXP (y, 0); | |
6469 | ||
6470 | if (GET_CODE (clobbered) == REG | |
6471 | || GET_CODE (clobbered) == SUBREG) | |
bb4034b3 | 6472 | invalidate (clobbered, VOIDmode); |
2708da92 RS |
6473 | else if (GET_CODE (clobbered) == STRICT_LOW_PART |
6474 | || GET_CODE (clobbered) == ZERO_EXTRACT) | |
bb4034b3 | 6475 | invalidate (XEXP (clobbered, 0), GET_MODE (clobbered)); |
2708da92 | 6476 | } |
7afe21cc RK |
6477 | } |
6478 | ||
6479 | for (i = 0; i < lim; i++) | |
6480 | { | |
6481 | register rtx y = XVECEXP (x, 0, i); | |
6482 | if (GET_CODE (y) == SET) | |
6483 | { | |
7722328e RK |
6484 | /* As above, we ignore unconditional jumps and call-insns and |
6485 | ignore the result of apply_change_group. */ | |
7afe21cc RK |
6486 | if (GET_CODE (SET_SRC (y)) == CALL) |
6487 | { | |
6488 | canon_reg (SET_SRC (y), insn); | |
77fa0940 | 6489 | apply_change_group (); |
7afe21cc | 6490 | fold_rtx (SET_SRC (y), insn); |
bb4034b3 | 6491 | invalidate (SET_DEST (y), VOIDmode); |
7afe21cc RK |
6492 | } |
6493 | else if (SET_DEST (y) == pc_rtx | |
6494 | && GET_CODE (SET_SRC (y)) == LABEL_REF) | |
6495 | ; | |
6496 | else | |
6497 | sets[n_sets++].rtl = y; | |
6498 | } | |
6499 | else if (GET_CODE (y) == CLOBBER) | |
6500 | { | |
9ae8ffe7 | 6501 | /* If we clobber memory, canon the address. |
7afe21cc RK |
6502 | This does nothing when a register is clobbered |
6503 | because we have already invalidated the reg. */ | |
6504 | if (GET_CODE (XEXP (y, 0)) == MEM) | |
9ae8ffe7 | 6505 | canon_reg (XEXP (y, 0), NULL_RTX); |
7afe21cc RK |
6506 | } |
6507 | else if (GET_CODE (y) == USE | |
6508 | && ! (GET_CODE (XEXP (y, 0)) == REG | |
6509 | && REGNO (XEXP (y, 0)) < FIRST_PSEUDO_REGISTER)) | |
906c4e36 | 6510 | canon_reg (y, NULL_RTX); |
7afe21cc RK |
6511 | else if (GET_CODE (y) == CALL) |
6512 | { | |
7722328e RK |
6513 | /* The result of apply_change_group can be ignored; see |
6514 | canon_reg. */ | |
7afe21cc | 6515 | canon_reg (y, insn); |
77fa0940 | 6516 | apply_change_group (); |
7afe21cc RK |
6517 | fold_rtx (y, insn); |
6518 | } | |
6519 | } | |
6520 | } | |
6521 | else if (GET_CODE (x) == CLOBBER) | |
6522 | { | |
6523 | if (GET_CODE (XEXP (x, 0)) == MEM) | |
9ae8ffe7 | 6524 | canon_reg (XEXP (x, 0), NULL_RTX); |
7afe21cc RK |
6525 | } |
6526 | ||
6527 | /* Canonicalize a USE of a pseudo register or memory location. */ | |
6528 | else if (GET_CODE (x) == USE | |
6529 | && ! (GET_CODE (XEXP (x, 0)) == REG | |
6530 | && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)) | |
906c4e36 | 6531 | canon_reg (XEXP (x, 0), NULL_RTX); |
7afe21cc RK |
6532 | else if (GET_CODE (x) == CALL) |
6533 | { | |
7722328e | 6534 | /* The result of apply_change_group can be ignored; see canon_reg. */ |
7afe21cc | 6535 | canon_reg (x, insn); |
77fa0940 | 6536 | apply_change_group (); |
7afe21cc RK |
6537 | fold_rtx (x, insn); |
6538 | } | |
6539 | ||
7b3ab05e JW |
6540 | /* Store the equivalent value in SRC_EQV, if different, or if the DEST |
6541 | is a STRICT_LOW_PART. The latter condition is necessary because SRC_EQV | |
6542 | is handled specially for this case, and if it isn't set, then there will | |
9faa82d8 | 6543 | be no equivalence for the destination. */ |
92f9aa51 RK |
6544 | if (n_sets == 1 && REG_NOTES (insn) != 0 |
6545 | && (tem = find_reg_note (insn, REG_EQUAL, NULL_RTX)) != 0 | |
7b3ab05e JW |
6546 | && (! rtx_equal_p (XEXP (tem, 0), SET_SRC (sets[0].rtl)) |
6547 | || GET_CODE (SET_DEST (sets[0].rtl)) == STRICT_LOW_PART)) | |
92f9aa51 | 6548 | src_eqv = canon_reg (XEXP (tem, 0), NULL_RTX); |
7afe21cc RK |
6549 | |
6550 | /* Canonicalize sources and addresses of destinations. | |
6551 | We do this in a separate pass to avoid problems when a MATCH_DUP is | |
6552 | present in the insn pattern. In that case, we want to ensure that | |
6553 | we don't break the duplicate nature of the pattern. So we will replace | |
6554 | both operands at the same time. Otherwise, we would fail to find an | |
6555 | equivalent substitution in the loop calling validate_change below. | |
7afe21cc RK |
6556 | |
6557 | We used to suppress canonicalization of DEST if it appears in SRC, | |
77fa0940 | 6558 | but we don't do this any more. */ |
7afe21cc RK |
6559 | |
6560 | for (i = 0; i < n_sets; i++) | |
6561 | { | |
6562 | rtx dest = SET_DEST (sets[i].rtl); | |
6563 | rtx src = SET_SRC (sets[i].rtl); | |
6564 | rtx new = canon_reg (src, insn); | |
58873255 | 6565 | int insn_code; |
7afe21cc | 6566 | |
77fa0940 RK |
6567 | if ((GET_CODE (new) == REG && GET_CODE (src) == REG |
6568 | && ((REGNO (new) < FIRST_PSEUDO_REGISTER) | |
6569 | != (REGNO (src) < FIRST_PSEUDO_REGISTER))) | |
58873255 | 6570 | || (insn_code = recog_memoized (insn)) < 0 |
a995e389 | 6571 | || insn_data[insn_code].n_dups > 0) |
77fa0940 | 6572 | validate_change (insn, &SET_SRC (sets[i].rtl), new, 1); |
7afe21cc RK |
6573 | else |
6574 | SET_SRC (sets[i].rtl) = new; | |
6575 | ||
6576 | if (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT) | |
6577 | { | |
6578 | validate_change (insn, &XEXP (dest, 1), | |
77fa0940 | 6579 | canon_reg (XEXP (dest, 1), insn), 1); |
7afe21cc | 6580 | validate_change (insn, &XEXP (dest, 2), |
77fa0940 | 6581 | canon_reg (XEXP (dest, 2), insn), 1); |
7afe21cc RK |
6582 | } |
6583 | ||
6584 | while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART | |
6585 | || GET_CODE (dest) == ZERO_EXTRACT | |
6586 | || GET_CODE (dest) == SIGN_EXTRACT) | |
6587 | dest = XEXP (dest, 0); | |
6588 | ||
6589 | if (GET_CODE (dest) == MEM) | |
6590 | canon_reg (dest, insn); | |
6591 | } | |
6592 | ||
77fa0940 RK |
6593 | /* Now that we have done all the replacements, we can apply the change |
6594 | group and see if they all work. Note that this will cause some | |
6595 | canonicalizations that would have worked individually not to be applied | |
6596 | because some other canonicalization didn't work, but this should not | |
7722328e RK |
6597 | occur often. |
6598 | ||
6599 | The result of apply_change_group can be ignored; see canon_reg. */ | |
77fa0940 RK |
6600 | |
6601 | apply_change_group (); | |
6602 | ||
7afe21cc RK |
6603 | /* Set sets[i].src_elt to the class each source belongs to. |
6604 | Detect assignments from or to volatile things | |
6605 | and set set[i] to zero so they will be ignored | |
6606 | in the rest of this function. | |
6607 | ||
6608 | Nothing in this loop changes the hash table or the register chains. */ | |
6609 | ||
6610 | for (i = 0; i < n_sets; i++) | |
6611 | { | |
6612 | register rtx src, dest; | |
6613 | register rtx src_folded; | |
6614 | register struct table_elt *elt = 0, *p; | |
6615 | enum machine_mode mode; | |
6616 | rtx src_eqv_here; | |
6617 | rtx src_const = 0; | |
6618 | rtx src_related = 0; | |
6619 | struct table_elt *src_const_elt = 0; | |
6620 | int src_cost = 10000, src_eqv_cost = 10000, src_folded_cost = 10000; | |
6621 | int src_related_cost = 10000, src_elt_cost = 10000; | |
6622 | /* Set non-zero if we need to call force_const_mem on with the | |
6623 | contents of src_folded before using it. */ | |
6624 | int src_folded_force_flag = 0; | |
6625 | ||
6626 | dest = SET_DEST (sets[i].rtl); | |
6627 | src = SET_SRC (sets[i].rtl); | |
6628 | ||
6629 | /* If SRC is a constant that has no machine mode, | |
6630 | hash it with the destination's machine mode. | |
6631 | This way we can keep different modes separate. */ | |
6632 | ||
6633 | mode = GET_MODE (src) == VOIDmode ? GET_MODE (dest) : GET_MODE (src); | |
6634 | sets[i].mode = mode; | |
6635 | ||
6636 | if (src_eqv) | |
6637 | { | |
6638 | enum machine_mode eqvmode = mode; | |
6639 | if (GET_CODE (dest) == STRICT_LOW_PART) | |
6640 | eqvmode = GET_MODE (SUBREG_REG (XEXP (dest, 0))); | |
6641 | do_not_record = 0; | |
6642 | hash_arg_in_memory = 0; | |
6643 | hash_arg_in_struct = 0; | |
6644 | src_eqv = fold_rtx (src_eqv, insn); | |
2197a88a | 6645 | src_eqv_hash = HASH (src_eqv, eqvmode); |
7afe21cc RK |
6646 | |
6647 | /* Find the equivalence class for the equivalent expression. */ | |
6648 | ||
6649 | if (!do_not_record) | |
2197a88a | 6650 | src_eqv_elt = lookup (src_eqv, src_eqv_hash, eqvmode); |
7afe21cc RK |
6651 | |
6652 | src_eqv_volatile = do_not_record; | |
6653 | src_eqv_in_memory = hash_arg_in_memory; | |
6654 | src_eqv_in_struct = hash_arg_in_struct; | |
6655 | } | |
6656 | ||
6657 | /* If this is a STRICT_LOW_PART assignment, src_eqv corresponds to the | |
6658 | value of the INNER register, not the destination. So it is not | |
3826a3da | 6659 | a valid substitution for the source. But save it for later. */ |
7afe21cc RK |
6660 | if (GET_CODE (dest) == STRICT_LOW_PART) |
6661 | src_eqv_here = 0; | |
6662 | else | |
6663 | src_eqv_here = src_eqv; | |
6664 | ||
6665 | /* Simplify and foldable subexpressions in SRC. Then get the fully- | |
6666 | simplified result, which may not necessarily be valid. */ | |
6667 | src_folded = fold_rtx (src, insn); | |
6668 | ||
e6a125a0 RK |
6669 | #if 0 |
6670 | /* ??? This caused bad code to be generated for the m68k port with -O2. | |
6671 | Suppose src is (CONST_INT -1), and that after truncation src_folded | |
6672 | is (CONST_INT 3). Suppose src_folded is then used for src_const. | |
6673 | At the end we will add src and src_const to the same equivalence | |
6674 | class. We now have 3 and -1 on the same equivalence class. This | |
6675 | causes later instructions to be mis-optimized. */ | |
7afe21cc RK |
6676 | /* If storing a constant in a bitfield, pre-truncate the constant |
6677 | so we will be able to record it later. */ | |
6678 | if (GET_CODE (SET_DEST (sets[i].rtl)) == ZERO_EXTRACT | |
6679 | || GET_CODE (SET_DEST (sets[i].rtl)) == SIGN_EXTRACT) | |
6680 | { | |
6681 | rtx width = XEXP (SET_DEST (sets[i].rtl), 1); | |
6682 | ||
6683 | if (GET_CODE (src) == CONST_INT | |
6684 | && GET_CODE (width) == CONST_INT | |
906c4e36 RK |
6685 | && INTVAL (width) < HOST_BITS_PER_WIDE_INT |
6686 | && (INTVAL (src) & ((HOST_WIDE_INT) (-1) << INTVAL (width)))) | |
6687 | src_folded | |
6688 | = GEN_INT (INTVAL (src) & (((HOST_WIDE_INT) 1 | |
6689 | << INTVAL (width)) - 1)); | |
7afe21cc | 6690 | } |
e6a125a0 | 6691 | #endif |
7afe21cc RK |
6692 | |
6693 | /* Compute SRC's hash code, and also notice if it | |
6694 | should not be recorded at all. In that case, | |
6695 | prevent any further processing of this assignment. */ | |
6696 | do_not_record = 0; | |
6697 | hash_arg_in_memory = 0; | |
6698 | hash_arg_in_struct = 0; | |
6699 | ||
6700 | sets[i].src = src; | |
2197a88a | 6701 | sets[i].src_hash = HASH (src, mode); |
7afe21cc RK |
6702 | sets[i].src_volatile = do_not_record; |
6703 | sets[i].src_in_memory = hash_arg_in_memory; | |
6704 | sets[i].src_in_struct = hash_arg_in_struct; | |
6705 | ||
50196afa RK |
6706 | /* If SRC is a MEM, there is a REG_EQUIV note for SRC, and DEST is |
6707 | a pseudo that is set more than once, do not record SRC. Using | |
6708 | SRC as a replacement for anything else will be incorrect in that | |
6709 | situation. Note that this usually occurs only for stack slots, | |
956d6950 | 6710 | in which case all the RTL would be referring to SRC, so we don't |
50196afa RK |
6711 | lose any optimization opportunities by not having SRC in the |
6712 | hash table. */ | |
6713 | ||
6714 | if (GET_CODE (src) == MEM | |
6715 | && find_reg_note (insn, REG_EQUIV, src) != 0 | |
6716 | && GET_CODE (dest) == REG | |
6717 | && REGNO (dest) >= FIRST_PSEUDO_REGISTER | |
b1f21e0a | 6718 | && REG_N_SETS (REGNO (dest)) != 1) |
50196afa RK |
6719 | sets[i].src_volatile = 1; |
6720 | ||
0dadecf6 RK |
6721 | #if 0 |
6722 | /* It is no longer clear why we used to do this, but it doesn't | |
6723 | appear to still be needed. So let's try without it since this | |
6724 | code hurts cse'ing widened ops. */ | |
7afe21cc RK |
6725 | /* If source is a perverse subreg (such as QI treated as an SI), |
6726 | treat it as volatile. It may do the work of an SI in one context | |
6727 | where the extra bits are not being used, but cannot replace an SI | |
6728 | in general. */ | |
6729 | if (GET_CODE (src) == SUBREG | |
6730 | && (GET_MODE_SIZE (GET_MODE (src)) | |
6731 | > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))) | |
6732 | sets[i].src_volatile = 1; | |
0dadecf6 | 6733 | #endif |
7afe21cc RK |
6734 | |
6735 | /* Locate all possible equivalent forms for SRC. Try to replace | |
6736 | SRC in the insn with each cheaper equivalent. | |
6737 | ||
6738 | We have the following types of equivalents: SRC itself, a folded | |
6739 | version, a value given in a REG_EQUAL note, or a value related | |
6740 | to a constant. | |
6741 | ||
6742 | Each of these equivalents may be part of an additional class | |
6743 | of equivalents (if more than one is in the table, they must be in | |
6744 | the same class; we check for this). | |
6745 | ||
6746 | If the source is volatile, we don't do any table lookups. | |
6747 | ||
6748 | We note any constant equivalent for possible later use in a | |
6749 | REG_NOTE. */ | |
6750 | ||
6751 | if (!sets[i].src_volatile) | |
2197a88a | 6752 | elt = lookup (src, sets[i].src_hash, mode); |
7afe21cc RK |
6753 | |
6754 | sets[i].src_elt = elt; | |
6755 | ||
6756 | if (elt && src_eqv_here && src_eqv_elt) | |
6757 | { | |
6758 | if (elt->first_same_value != src_eqv_elt->first_same_value) | |
6759 | { | |
6760 | /* The REG_EQUAL is indicating that two formerly distinct | |
6761 | classes are now equivalent. So merge them. */ | |
6762 | merge_equiv_classes (elt, src_eqv_elt); | |
2197a88a RK |
6763 | src_eqv_hash = HASH (src_eqv, elt->mode); |
6764 | src_eqv_elt = lookup (src_eqv, src_eqv_hash, elt->mode); | |
7afe21cc RK |
6765 | } |
6766 | ||
6767 | src_eqv_here = 0; | |
6768 | } | |
6769 | ||
6770 | else if (src_eqv_elt) | |
6771 | elt = src_eqv_elt; | |
6772 | ||
6773 | /* Try to find a constant somewhere and record it in `src_const'. | |
6774 | Record its table element, if any, in `src_const_elt'. Look in | |
6775 | any known equivalences first. (If the constant is not in the | |
2197a88a | 6776 | table, also set `sets[i].src_const_hash'). */ |
7afe21cc RK |
6777 | if (elt) |
6778 | for (p = elt->first_same_value; p; p = p->next_same_value) | |
6779 | if (p->is_const) | |
6780 | { | |
6781 | src_const = p->exp; | |
6782 | src_const_elt = elt; | |
6783 | break; | |
6784 | } | |
6785 | ||
6786 | if (src_const == 0 | |
6787 | && (CONSTANT_P (src_folded) | |
6788 | /* Consider (minus (label_ref L1) (label_ref L2)) as | |
6789 | "constant" here so we will record it. This allows us | |
6790 | to fold switch statements when an ADDR_DIFF_VEC is used. */ | |
6791 | || (GET_CODE (src_folded) == MINUS | |
6792 | && GET_CODE (XEXP (src_folded, 0)) == LABEL_REF | |
6793 | && GET_CODE (XEXP (src_folded, 1)) == LABEL_REF))) | |
6794 | src_const = src_folded, src_const_elt = elt; | |
6795 | else if (src_const == 0 && src_eqv_here && CONSTANT_P (src_eqv_here)) | |
6796 | src_const = src_eqv_here, src_const_elt = src_eqv_elt; | |
6797 | ||
6798 | /* If we don't know if the constant is in the table, get its | |
6799 | hash code and look it up. */ | |
6800 | if (src_const && src_const_elt == 0) | |
6801 | { | |
2197a88a RK |
6802 | sets[i].src_const_hash = HASH (src_const, mode); |
6803 | src_const_elt = lookup (src_const, sets[i].src_const_hash, mode); | |
7afe21cc RK |
6804 | } |
6805 | ||
6806 | sets[i].src_const = src_const; | |
6807 | sets[i].src_const_elt = src_const_elt; | |
6808 | ||
6809 | /* If the constant and our source are both in the table, mark them as | |
6810 | equivalent. Otherwise, if a constant is in the table but the source | |
6811 | isn't, set ELT to it. */ | |
6812 | if (src_const_elt && elt | |
6813 | && src_const_elt->first_same_value != elt->first_same_value) | |
6814 | merge_equiv_classes (elt, src_const_elt); | |
6815 | else if (src_const_elt && elt == 0) | |
6816 | elt = src_const_elt; | |
6817 | ||
6818 | /* See if there is a register linearly related to a constant | |
6819 | equivalent of SRC. */ | |
6820 | if (src_const | |
6821 | && (GET_CODE (src_const) == CONST | |
6822 | || (src_const_elt && src_const_elt->related_value != 0))) | |
6823 | { | |
6824 | src_related = use_related_value (src_const, src_const_elt); | |
6825 | if (src_related) | |
6826 | { | |
6827 | struct table_elt *src_related_elt | |
6828 | = lookup (src_related, HASH (src_related, mode), mode); | |
6829 | if (src_related_elt && elt) | |
6830 | { | |
6831 | if (elt->first_same_value | |
6832 | != src_related_elt->first_same_value) | |
6833 | /* This can occur when we previously saw a CONST | |
6834 | involving a SYMBOL_REF and then see the SYMBOL_REF | |
6835 | twice. Merge the involved classes. */ | |
6836 | merge_equiv_classes (elt, src_related_elt); | |
6837 | ||
6838 | src_related = 0; | |
6839 | src_related_elt = 0; | |
6840 | } | |
6841 | else if (src_related_elt && elt == 0) | |
6842 | elt = src_related_elt; | |
6843 | } | |
6844 | } | |
6845 | ||
e4600702 RK |
6846 | /* See if we have a CONST_INT that is already in a register in a |
6847 | wider mode. */ | |
6848 | ||
6849 | if (src_const && src_related == 0 && GET_CODE (src_const) == CONST_INT | |
6850 | && GET_MODE_CLASS (mode) == MODE_INT | |
6851 | && GET_MODE_BITSIZE (mode) < BITS_PER_WORD) | |
6852 | { | |
6853 | enum machine_mode wider_mode; | |
6854 | ||
6855 | for (wider_mode = GET_MODE_WIDER_MODE (mode); | |
6856 | GET_MODE_BITSIZE (wider_mode) <= BITS_PER_WORD | |
6857 | && src_related == 0; | |
6858 | wider_mode = GET_MODE_WIDER_MODE (wider_mode)) | |
6859 | { | |
6860 | struct table_elt *const_elt | |
6861 | = lookup (src_const, HASH (src_const, wider_mode), wider_mode); | |
6862 | ||
6863 | if (const_elt == 0) | |
6864 | continue; | |
6865 | ||
6866 | for (const_elt = const_elt->first_same_value; | |
6867 | const_elt; const_elt = const_elt->next_same_value) | |
6868 | if (GET_CODE (const_elt->exp) == REG) | |
6869 | { | |
6870 | src_related = gen_lowpart_if_possible (mode, | |
6871 | const_elt->exp); | |
6872 | break; | |
6873 | } | |
6874 | } | |
6875 | } | |
6876 | ||
d45cf215 RS |
6877 | /* Another possibility is that we have an AND with a constant in |
6878 | a mode narrower than a word. If so, it might have been generated | |
6879 | as part of an "if" which would narrow the AND. If we already | |
6880 | have done the AND in a wider mode, we can use a SUBREG of that | |
6881 | value. */ | |
6882 | ||
6883 | if (flag_expensive_optimizations && ! src_related | |
6884 | && GET_CODE (src) == AND && GET_CODE (XEXP (src, 1)) == CONST_INT | |
6885 | && GET_MODE_SIZE (mode) < UNITS_PER_WORD) | |
6886 | { | |
6887 | enum machine_mode tmode; | |
38a448ca | 6888 | rtx new_and = gen_rtx_AND (VOIDmode, NULL_RTX, XEXP (src, 1)); |
d45cf215 RS |
6889 | |
6890 | for (tmode = GET_MODE_WIDER_MODE (mode); | |
6891 | GET_MODE_SIZE (tmode) <= UNITS_PER_WORD; | |
6892 | tmode = GET_MODE_WIDER_MODE (tmode)) | |
6893 | { | |
6894 | rtx inner = gen_lowpart_if_possible (tmode, XEXP (src, 0)); | |
6895 | struct table_elt *larger_elt; | |
6896 | ||
6897 | if (inner) | |
6898 | { | |
6899 | PUT_MODE (new_and, tmode); | |
6900 | XEXP (new_and, 0) = inner; | |
6901 | larger_elt = lookup (new_and, HASH (new_and, tmode), tmode); | |
6902 | if (larger_elt == 0) | |
6903 | continue; | |
6904 | ||
6905 | for (larger_elt = larger_elt->first_same_value; | |
6906 | larger_elt; larger_elt = larger_elt->next_same_value) | |
6907 | if (GET_CODE (larger_elt->exp) == REG) | |
6908 | { | |
6909 | src_related | |
6910 | = gen_lowpart_if_possible (mode, larger_elt->exp); | |
6911 | break; | |
6912 | } | |
6913 | ||
6914 | if (src_related) | |
6915 | break; | |
6916 | } | |
6917 | } | |
6918 | } | |
7bac1be0 RK |
6919 | |
6920 | #ifdef LOAD_EXTEND_OP | |
6921 | /* See if a MEM has already been loaded with a widening operation; | |
6922 | if it has, we can use a subreg of that. Many CISC machines | |
6923 | also have such operations, but this is only likely to be | |
6924 | beneficial these machines. */ | |
6925 | ||
6926 | if (flag_expensive_optimizations && src_related == 0 | |
6927 | && (GET_MODE_SIZE (mode) < UNITS_PER_WORD) | |
6928 | && GET_MODE_CLASS (mode) == MODE_INT | |
6929 | && GET_CODE (src) == MEM && ! do_not_record | |
6930 | && LOAD_EXTEND_OP (mode) != NIL) | |
6931 | { | |
6932 | enum machine_mode tmode; | |
6933 | ||
6934 | /* Set what we are trying to extend and the operation it might | |
6935 | have been extended with. */ | |
6936 | PUT_CODE (memory_extend_rtx, LOAD_EXTEND_OP (mode)); | |
6937 | XEXP (memory_extend_rtx, 0) = src; | |
6938 | ||
6939 | for (tmode = GET_MODE_WIDER_MODE (mode); | |
6940 | GET_MODE_SIZE (tmode) <= UNITS_PER_WORD; | |
6941 | tmode = GET_MODE_WIDER_MODE (tmode)) | |
6942 | { | |
6943 | struct table_elt *larger_elt; | |
6944 | ||
6945 | PUT_MODE (memory_extend_rtx, tmode); | |
6946 | larger_elt = lookup (memory_extend_rtx, | |
6947 | HASH (memory_extend_rtx, tmode), tmode); | |
6948 | if (larger_elt == 0) | |
6949 | continue; | |
6950 | ||
6951 | for (larger_elt = larger_elt->first_same_value; | |
6952 | larger_elt; larger_elt = larger_elt->next_same_value) | |
6953 | if (GET_CODE (larger_elt->exp) == REG) | |
6954 | { | |
6955 | src_related = gen_lowpart_if_possible (mode, | |
6956 | larger_elt->exp); | |
6957 | break; | |
6958 | } | |
6959 | ||
6960 | if (src_related) | |
6961 | break; | |
6962 | } | |
6963 | } | |
6964 | #endif /* LOAD_EXTEND_OP */ | |
6965 | ||
7afe21cc RK |
6966 | if (src == src_folded) |
6967 | src_folded = 0; | |
6968 | ||
6969 | /* At this point, ELT, if non-zero, points to a class of expressions | |
6970 | equivalent to the source of this SET and SRC, SRC_EQV, SRC_FOLDED, | |
6971 | and SRC_RELATED, if non-zero, each contain additional equivalent | |
6972 | expressions. Prune these latter expressions by deleting expressions | |
6973 | already in the equivalence class. | |
6974 | ||
6975 | Check for an equivalent identical to the destination. If found, | |
6976 | this is the preferred equivalent since it will likely lead to | |
6977 | elimination of the insn. Indicate this by placing it in | |
6978 | `src_related'. */ | |
6979 | ||
6980 | if (elt) elt = elt->first_same_value; | |
6981 | for (p = elt; p; p = p->next_same_value) | |
6982 | { | |
6983 | enum rtx_code code = GET_CODE (p->exp); | |
6984 | ||
6985 | /* If the expression is not valid, ignore it. Then we do not | |
6986 | have to check for validity below. In most cases, we can use | |
6987 | `rtx_equal_p', since canonicalization has already been done. */ | |
6988 | if (code != REG && ! exp_equiv_p (p->exp, p->exp, 1, 0)) | |
6989 | continue; | |
6990 | ||
5a03c8c4 RK |
6991 | /* Also skip paradoxical subregs, unless that's what we're |
6992 | looking for. */ | |
6993 | if (code == SUBREG | |
6994 | && (GET_MODE_SIZE (GET_MODE (p->exp)) | |
6995 | > GET_MODE_SIZE (GET_MODE (SUBREG_REG (p->exp)))) | |
6996 | && ! (src != 0 | |
6997 | && GET_CODE (src) == SUBREG | |
6998 | && GET_MODE (src) == GET_MODE (p->exp) | |
6999 | && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))) | |
7000 | < GET_MODE_SIZE (GET_MODE (SUBREG_REG (p->exp)))))) | |
7001 | continue; | |
7002 | ||
7afe21cc RK |
7003 | if (src && GET_CODE (src) == code && rtx_equal_p (src, p->exp)) |
7004 | src = 0; | |
7005 | else if (src_folded && GET_CODE (src_folded) == code | |
7006 | && rtx_equal_p (src_folded, p->exp)) | |
7007 | src_folded = 0; | |
7008 | else if (src_eqv_here && GET_CODE (src_eqv_here) == code | |
7009 | && rtx_equal_p (src_eqv_here, p->exp)) | |
7010 | src_eqv_here = 0; | |
7011 | else if (src_related && GET_CODE (src_related) == code | |
7012 | && rtx_equal_p (src_related, p->exp)) | |
7013 | src_related = 0; | |
7014 | ||
7015 | /* This is the same as the destination of the insns, we want | |
7016 | to prefer it. Copy it to src_related. The code below will | |
7017 | then give it a negative cost. */ | |
7018 | if (GET_CODE (dest) == code && rtx_equal_p (p->exp, dest)) | |
7019 | src_related = dest; | |
7020 | ||
7021 | } | |
7022 | ||
7023 | /* Find the cheapest valid equivalent, trying all the available | |
7024 | possibilities. Prefer items not in the hash table to ones | |
7025 | that are when they are equal cost. Note that we can never | |
7026 | worsen an insn as the current contents will also succeed. | |
05c33dd8 | 7027 | If we find an equivalent identical to the destination, use it as best, |
0f41302f | 7028 | since this insn will probably be eliminated in that case. */ |
7afe21cc RK |
7029 | if (src) |
7030 | { | |
7031 | if (rtx_equal_p (src, dest)) | |
7032 | src_cost = -1; | |
7033 | else | |
7034 | src_cost = COST (src); | |
7035 | } | |
7036 | ||
7037 | if (src_eqv_here) | |
7038 | { | |
7039 | if (rtx_equal_p (src_eqv_here, dest)) | |
7040 | src_eqv_cost = -1; | |
7041 | else | |
7042 | src_eqv_cost = COST (src_eqv_here); | |
7043 | } | |
7044 | ||
7045 | if (src_folded) | |
7046 | { | |
7047 | if (rtx_equal_p (src_folded, dest)) | |
7048 | src_folded_cost = -1; | |
7049 | else | |
7050 | src_folded_cost = COST (src_folded); | |
7051 | } | |
7052 | ||
7053 | if (src_related) | |
7054 | { | |
7055 | if (rtx_equal_p (src_related, dest)) | |
7056 | src_related_cost = -1; | |
7057 | else | |
7058 | src_related_cost = COST (src_related); | |
7059 | } | |
7060 | ||
7061 | /* If this was an indirect jump insn, a known label will really be | |
7062 | cheaper even though it looks more expensive. */ | |
7063 | if (dest == pc_rtx && src_const && GET_CODE (src_const) == LABEL_REF) | |
7064 | src_folded = src_const, src_folded_cost = -1; | |
7065 | ||
7066 | /* Terminate loop when replacement made. This must terminate since | |
7067 | the current contents will be tested and will always be valid. */ | |
7068 | while (1) | |
7069 | { | |
7bd8b2a8 | 7070 | rtx trial, old_src; |
7afe21cc RK |
7071 | |
7072 | /* Skip invalid entries. */ | |
7073 | while (elt && GET_CODE (elt->exp) != REG | |
7074 | && ! exp_equiv_p (elt->exp, elt->exp, 1, 0)) | |
7075 | elt = elt->next_same_value; | |
5a03c8c4 RK |
7076 | |
7077 | /* A paradoxical subreg would be bad here: it'll be the right | |
7078 | size, but later may be adjusted so that the upper bits aren't | |
7079 | what we want. So reject it. */ | |
7080 | if (elt != 0 | |
7081 | && GET_CODE (elt->exp) == SUBREG | |
7082 | && (GET_MODE_SIZE (GET_MODE (elt->exp)) | |
7083 | > GET_MODE_SIZE (GET_MODE (SUBREG_REG (elt->exp)))) | |
7084 | /* It is okay, though, if the rtx we're trying to match | |
7085 | will ignore any of the bits we can't predict. */ | |
7086 | && ! (src != 0 | |
7087 | && GET_CODE (src) == SUBREG | |
7088 | && GET_MODE (src) == GET_MODE (elt->exp) | |
7089 | && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))) | |
7090 | < GET_MODE_SIZE (GET_MODE (SUBREG_REG (elt->exp)))))) | |
7091 | { | |
7092 | elt = elt->next_same_value; | |
7093 | continue; | |
7094 | } | |
7afe21cc RK |
7095 | |
7096 | if (elt) src_elt_cost = elt->cost; | |
7097 | ||
7098 | /* Find cheapest and skip it for the next time. For items | |
7099 | of equal cost, use this order: | |
7100 | src_folded, src, src_eqv, src_related and hash table entry. */ | |
7101 | if (src_folded_cost <= src_cost | |
7102 | && src_folded_cost <= src_eqv_cost | |
7103 | && src_folded_cost <= src_related_cost | |
7104 | && src_folded_cost <= src_elt_cost) | |
7105 | { | |
7106 | trial = src_folded, src_folded_cost = 10000; | |
7107 | if (src_folded_force_flag) | |
7108 | trial = force_const_mem (mode, trial); | |
7109 | } | |
7110 | else if (src_cost <= src_eqv_cost | |
7111 | && src_cost <= src_related_cost | |
7112 | && src_cost <= src_elt_cost) | |
7113 | trial = src, src_cost = 10000; | |
7114 | else if (src_eqv_cost <= src_related_cost | |
7115 | && src_eqv_cost <= src_elt_cost) | |
0af62b41 | 7116 | trial = copy_rtx (src_eqv_here), src_eqv_cost = 10000; |
7afe21cc | 7117 | else if (src_related_cost <= src_elt_cost) |
0af62b41 | 7118 | trial = copy_rtx (src_related), src_related_cost = 10000; |
7afe21cc RK |
7119 | else |
7120 | { | |
05c33dd8 | 7121 | trial = copy_rtx (elt->exp); |
7afe21cc RK |
7122 | elt = elt->next_same_value; |
7123 | src_elt_cost = 10000; | |
7124 | } | |
7125 | ||
7126 | /* We don't normally have an insn matching (set (pc) (pc)), so | |
7127 | check for this separately here. We will delete such an | |
7128 | insn below. | |
7129 | ||
7130 | Tablejump insns contain a USE of the table, so simply replacing | |
7131 | the operand with the constant won't match. This is simply an | |
7132 | unconditional branch, however, and is therefore valid. Just | |
7133 | insert the substitution here and we will delete and re-emit | |
7134 | the insn later. */ | |
7135 | ||
7bd8b2a8 JL |
7136 | /* Keep track of the original SET_SRC so that we can fix notes |
7137 | on libcall instructions. */ | |
7138 | old_src = SET_SRC (sets[i].rtl); | |
7139 | ||
7afe21cc RK |
7140 | if (n_sets == 1 && dest == pc_rtx |
7141 | && (trial == pc_rtx | |
7142 | || (GET_CODE (trial) == LABEL_REF | |
7143 | && ! condjump_p (insn)))) | |
7144 | { | |
7145 | /* If TRIAL is a label in front of a jump table, we are | |
7146 | really falling through the switch (this is how casesi | |
7147 | insns work), so we must branch around the table. */ | |
7148 | if (GET_CODE (trial) == CODE_LABEL | |
7149 | && NEXT_INSN (trial) != 0 | |
7150 | && GET_CODE (NEXT_INSN (trial)) == JUMP_INSN | |
7151 | && (GET_CODE (PATTERN (NEXT_INSN (trial))) == ADDR_DIFF_VEC | |
7152 | || GET_CODE (PATTERN (NEXT_INSN (trial))) == ADDR_VEC)) | |
7153 | ||
38a448ca | 7154 | trial = gen_rtx_LABEL_REF (Pmode, get_label_after (trial)); |
7afe21cc RK |
7155 | |
7156 | SET_SRC (sets[i].rtl) = trial; | |
44333223 | 7157 | cse_jumps_altered = 1; |
7afe21cc RK |
7158 | break; |
7159 | } | |
7160 | ||
7161 | /* Look for a substitution that makes a valid insn. */ | |
7162 | else if (validate_change (insn, &SET_SRC (sets[i].rtl), trial, 0)) | |
05c33dd8 | 7163 | { |
7bd8b2a8 JL |
7164 | /* If we just made a substitution inside a libcall, then we |
7165 | need to make the same substitution in any notes attached | |
7166 | to the RETVAL insn. */ | |
1ed0205e VM |
7167 | if (libcall_insn |
7168 | && (GET_CODE (old_src) == REG | |
7169 | || GET_CODE (old_src) == SUBREG | |
7170 | || GET_CODE (old_src) == MEM)) | |
7bd8b2a8 JL |
7171 | replace_rtx (REG_NOTES (libcall_insn), old_src, |
7172 | canon_reg (SET_SRC (sets[i].rtl), insn)); | |
7173 | ||
7722328e RK |
7174 | /* The result of apply_change_group can be ignored; see |
7175 | canon_reg. */ | |
7176 | ||
7177 | validate_change (insn, &SET_SRC (sets[i].rtl), | |
7178 | canon_reg (SET_SRC (sets[i].rtl), insn), | |
7179 | 1); | |
6702af89 | 7180 | apply_change_group (); |
05c33dd8 RK |
7181 | break; |
7182 | } | |
7afe21cc RK |
7183 | |
7184 | /* If we previously found constant pool entries for | |
7185 | constants and this is a constant, try making a | |
7186 | pool entry. Put it in src_folded unless we already have done | |
7187 | this since that is where it likely came from. */ | |
7188 | ||
7189 | else if (constant_pool_entries_cost | |
7190 | && CONSTANT_P (trial) | |
1bbd065b RK |
7191 | && ! (GET_CODE (trial) == CONST |
7192 | && GET_CODE (XEXP (trial, 0)) == TRUNCATE) | |
7193 | && (src_folded == 0 | |
7194 | || (GET_CODE (src_folded) != MEM | |
7195 | && ! src_folded_force_flag)) | |
9ae8ffe7 JL |
7196 | && GET_MODE_CLASS (mode) != MODE_CC |
7197 | && mode != VOIDmode) | |
7afe21cc RK |
7198 | { |
7199 | src_folded_force_flag = 1; | |
7200 | src_folded = trial; | |
7201 | src_folded_cost = constant_pool_entries_cost; | |
7202 | } | |
7203 | } | |
7204 | ||
7205 | src = SET_SRC (sets[i].rtl); | |
7206 | ||
7207 | /* In general, it is good to have a SET with SET_SRC == SET_DEST. | |
7208 | However, there is an important exception: If both are registers | |
7209 | that are not the head of their equivalence class, replace SET_SRC | |
7210 | with the head of the class. If we do not do this, we will have | |
7211 | both registers live over a portion of the basic block. This way, | |
7212 | their lifetimes will likely abut instead of overlapping. */ | |
7213 | if (GET_CODE (dest) == REG | |
7214 | && REGNO_QTY_VALID_P (REGNO (dest)) | |
30f72379 MM |
7215 | && qty_mode[REG_QTY (REGNO (dest))] == GET_MODE (dest) |
7216 | && qty_first_reg[REG_QTY (REGNO (dest))] != REGNO (dest) | |
7afe21cc RK |
7217 | && GET_CODE (src) == REG && REGNO (src) == REGNO (dest) |
7218 | /* Don't do this if the original insn had a hard reg as | |
c5c76735 | 7219 | SET_SRC or SET_DEST. */ |
7afe21cc | 7220 | && (GET_CODE (sets[i].src) != REG |
c5c76735 JL |
7221 | || REGNO (sets[i].src) >= FIRST_PSEUDO_REGISTER) |
7222 | && (GET_CODE (dest) != REG || REGNO (dest) >= FIRST_PSEUDO_REGISTER)) | |
7afe21cc RK |
7223 | /* We can't call canon_reg here because it won't do anything if |
7224 | SRC is a hard register. */ | |
7225 | { | |
30f72379 | 7226 | int first = qty_first_reg[REG_QTY (REGNO (src))]; |
759bd8b7 R |
7227 | rtx new_src |
7228 | = (first >= FIRST_PSEUDO_REGISTER | |
7229 | ? regno_reg_rtx[first] : gen_rtx_REG (GET_MODE (src), first)); | |
7230 | ||
7231 | /* We must use validate-change even for this, because this | |
7232 | might be a special no-op instruction, suitable only to | |
7233 | tag notes onto. */ | |
7234 | if (validate_change (insn, &SET_SRC (sets[i].rtl), new_src, 0)) | |
7235 | { | |
7236 | src = new_src; | |
7237 | /* If we had a constant that is cheaper than what we are now | |
7238 | setting SRC to, use that constant. We ignored it when we | |
7239 | thought we could make this into a no-op. */ | |
7240 | if (src_const && COST (src_const) < COST (src) | |
7241 | && validate_change (insn, &SET_SRC (sets[i].rtl), src_const, | |
7242 | 0)) | |
7243 | src = src_const; | |
7244 | } | |
7afe21cc RK |
7245 | } |
7246 | ||
7247 | /* If we made a change, recompute SRC values. */ | |
7248 | if (src != sets[i].src) | |
7249 | { | |
7250 | do_not_record = 0; | |
7251 | hash_arg_in_memory = 0; | |
7252 | hash_arg_in_struct = 0; | |
7253 | sets[i].src = src; | |
2197a88a | 7254 | sets[i].src_hash = HASH (src, mode); |
7afe21cc RK |
7255 | sets[i].src_volatile = do_not_record; |
7256 | sets[i].src_in_memory = hash_arg_in_memory; | |
7257 | sets[i].src_in_struct = hash_arg_in_struct; | |
2197a88a | 7258 | sets[i].src_elt = lookup (src, sets[i].src_hash, mode); |
7afe21cc RK |
7259 | } |
7260 | ||
7261 | /* If this is a single SET, we are setting a register, and we have an | |
7262 | equivalent constant, we want to add a REG_NOTE. We don't want | |
7263 | to write a REG_EQUAL note for a constant pseudo since verifying that | |
d45cf215 | 7264 | that pseudo hasn't been eliminated is a pain. Such a note also |
ac7ef8d5 FS |
7265 | won't help anything. |
7266 | ||
7267 | Avoid a REG_EQUAL note for (CONST (MINUS (LABEL_REF) (LABEL_REF))) | |
7268 | which can be created for a reference to a compile time computable | |
7269 | entry in a jump table. */ | |
7270 | ||
7afe21cc | 7271 | if (n_sets == 1 && src_const && GET_CODE (dest) == REG |
ac7ef8d5 FS |
7272 | && GET_CODE (src_const) != REG |
7273 | && ! (GET_CODE (src_const) == CONST | |
7274 | && GET_CODE (XEXP (src_const, 0)) == MINUS | |
7275 | && GET_CODE (XEXP (XEXP (src_const, 0), 0)) == LABEL_REF | |
7276 | && GET_CODE (XEXP (XEXP (src_const, 0), 1)) == LABEL_REF)) | |
7afe21cc | 7277 | { |
92f9aa51 | 7278 | tem = find_reg_note (insn, REG_EQUAL, NULL_RTX); |
7afe21cc | 7279 | |
51e2a951 AS |
7280 | /* Make sure that the rtx is not shared with any other insn. */ |
7281 | src_const = copy_rtx (src_const); | |
7282 | ||
7afe21cc RK |
7283 | /* Record the actual constant value in a REG_EQUAL note, making |
7284 | a new one if one does not already exist. */ | |
7285 | if (tem) | |
7286 | XEXP (tem, 0) = src_const; | |
7287 | else | |
38a448ca RH |
7288 | REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, |
7289 | src_const, REG_NOTES (insn)); | |
7afe21cc RK |
7290 | |
7291 | /* If storing a constant value in a register that | |
7292 | previously held the constant value 0, | |
7293 | record this fact with a REG_WAS_0 note on this insn. | |
7294 | ||
7295 | Note that the *register* is required to have previously held 0, | |
7296 | not just any register in the quantity and we must point to the | |
7297 | insn that set that register to zero. | |
7298 | ||
7299 | Rather than track each register individually, we just see if | |
7300 | the last set for this quantity was for this register. */ | |
7301 | ||
7302 | if (REGNO_QTY_VALID_P (REGNO (dest)) | |
30f72379 | 7303 | && qty_const[REG_QTY (REGNO (dest))] == const0_rtx) |
7afe21cc RK |
7304 | { |
7305 | /* See if we previously had a REG_WAS_0 note. */ | |
906c4e36 | 7306 | rtx note = find_reg_note (insn, REG_WAS_0, NULL_RTX); |
30f72379 | 7307 | rtx const_insn = qty_const_insn[REG_QTY (REGNO (dest))]; |
7afe21cc RK |
7308 | |
7309 | if ((tem = single_set (const_insn)) != 0 | |
7310 | && rtx_equal_p (SET_DEST (tem), dest)) | |
7311 | { | |
7312 | if (note) | |
7313 | XEXP (note, 0) = const_insn; | |
7314 | else | |
c5c76735 JL |
7315 | REG_NOTES (insn) |
7316 | = gen_rtx_INSN_LIST (REG_WAS_0, const_insn, | |
7317 | REG_NOTES (insn)); | |
7afe21cc RK |
7318 | } |
7319 | } | |
7320 | } | |
7321 | ||
7322 | /* Now deal with the destination. */ | |
7323 | do_not_record = 0; | |
7324 | sets[i].inner_dest_loc = &SET_DEST (sets[0].rtl); | |
7325 | ||
7326 | /* Look within any SIGN_EXTRACT or ZERO_EXTRACT | |
7327 | to the MEM or REG within it. */ | |
7328 | while (GET_CODE (dest) == SIGN_EXTRACT | |
7329 | || GET_CODE (dest) == ZERO_EXTRACT | |
7330 | || GET_CODE (dest) == SUBREG | |
7331 | || GET_CODE (dest) == STRICT_LOW_PART) | |
7332 | { | |
7333 | sets[i].inner_dest_loc = &XEXP (dest, 0); | |
7334 | dest = XEXP (dest, 0); | |
7335 | } | |
7336 | ||
7337 | sets[i].inner_dest = dest; | |
7338 | ||
7339 | if (GET_CODE (dest) == MEM) | |
7340 | { | |
9ae8ffe7 JL |
7341 | #ifdef PUSH_ROUNDING |
7342 | /* Stack pushes invalidate the stack pointer. */ | |
7343 | rtx addr = XEXP (dest, 0); | |
7344 | if ((GET_CODE (addr) == PRE_DEC || GET_CODE (addr) == PRE_INC | |
7345 | || GET_CODE (addr) == POST_DEC || GET_CODE (addr) == POST_INC) | |
7346 | && XEXP (addr, 0) == stack_pointer_rtx) | |
7347 | invalidate (stack_pointer_rtx, Pmode); | |
7348 | #endif | |
7afe21cc | 7349 | dest = fold_rtx (dest, insn); |
7afe21cc RK |
7350 | } |
7351 | ||
7352 | /* Compute the hash code of the destination now, | |
7353 | before the effects of this instruction are recorded, | |
7354 | since the register values used in the address computation | |
7355 | are those before this instruction. */ | |
2197a88a | 7356 | sets[i].dest_hash = HASH (dest, mode); |
7afe21cc RK |
7357 | |
7358 | /* Don't enter a bit-field in the hash table | |
7359 | because the value in it after the store | |
7360 | may not equal what was stored, due to truncation. */ | |
7361 | ||
7362 | if (GET_CODE (SET_DEST (sets[i].rtl)) == ZERO_EXTRACT | |
7363 | || GET_CODE (SET_DEST (sets[i].rtl)) == SIGN_EXTRACT) | |
7364 | { | |
7365 | rtx width = XEXP (SET_DEST (sets[i].rtl), 1); | |
7366 | ||
7367 | if (src_const != 0 && GET_CODE (src_const) == CONST_INT | |
7368 | && GET_CODE (width) == CONST_INT | |
906c4e36 RK |
7369 | && INTVAL (width) < HOST_BITS_PER_WIDE_INT |
7370 | && ! (INTVAL (src_const) | |
7371 | & ((HOST_WIDE_INT) (-1) << INTVAL (width)))) | |
7afe21cc RK |
7372 | /* Exception: if the value is constant, |
7373 | and it won't be truncated, record it. */ | |
7374 | ; | |
7375 | else | |
7376 | { | |
7377 | /* This is chosen so that the destination will be invalidated | |
7378 | but no new value will be recorded. | |
7379 | We must invalidate because sometimes constant | |
7380 | values can be recorded for bitfields. */ | |
7381 | sets[i].src_elt = 0; | |
7382 | sets[i].src_volatile = 1; | |
7383 | src_eqv = 0; | |
7384 | src_eqv_elt = 0; | |
7385 | } | |
7386 | } | |
7387 | ||
7388 | /* If only one set in a JUMP_INSN and it is now a no-op, we can delete | |
7389 | the insn. */ | |
7390 | else if (n_sets == 1 && dest == pc_rtx && src == pc_rtx) | |
7391 | { | |
ef178af3 ZW |
7392 | /* One less use of the label this insn used to jump to. */ |
7393 | if (JUMP_LABEL (insn) != 0) | |
7394 | --LABEL_NUSES (JUMP_LABEL (insn)); | |
7afe21cc RK |
7395 | PUT_CODE (insn, NOTE); |
7396 | NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED; | |
7397 | NOTE_SOURCE_FILE (insn) = 0; | |
7398 | cse_jumps_altered = 1; | |
7afe21cc RK |
7399 | /* No more processing for this set. */ |
7400 | sets[i].rtl = 0; | |
7401 | } | |
7402 | ||
7403 | /* If this SET is now setting PC to a label, we know it used to | |
7404 | be a conditional or computed branch. So we see if we can follow | |
7405 | it. If it was a computed branch, delete it and re-emit. */ | |
7406 | else if (dest == pc_rtx && GET_CODE (src) == LABEL_REF) | |
7407 | { | |
7408 | rtx p; | |
7409 | ||
7410 | /* If this is not in the format for a simple branch and | |
7411 | we are the only SET in it, re-emit it. */ | |
7412 | if (! simplejump_p (insn) && n_sets == 1) | |
7413 | { | |
7414 | rtx new = emit_jump_insn_before (gen_jump (XEXP (src, 0)), insn); | |
7415 | JUMP_LABEL (new) = XEXP (src, 0); | |
7416 | LABEL_NUSES (XEXP (src, 0))++; | |
7417 | delete_insn (insn); | |
7418 | insn = new; | |
7419 | } | |
31dcf83f RS |
7420 | else |
7421 | /* Otherwise, force rerecognition, since it probably had | |
7422 | a different pattern before. | |
7423 | This shouldn't really be necessary, since whatever | |
7424 | changed the source value above should have done this. | |
7425 | Until the right place is found, might as well do this here. */ | |
7426 | INSN_CODE (insn) = -1; | |
7afe21cc RK |
7427 | |
7428 | /* Now that we've converted this jump to an unconditional jump, | |
7429 | there is dead code after it. Delete the dead code until we | |
7430 | reach a BARRIER, the end of the function, or a label. Do | |
7431 | not delete NOTEs except for NOTE_INSN_DELETED since later | |
7432 | phases assume these notes are retained. */ | |
7433 | ||
312f6255 GK |
7434 | never_reached_warning (insn); |
7435 | ||
7afe21cc RK |
7436 | p = insn; |
7437 | ||
7438 | while (NEXT_INSN (p) != 0 | |
7439 | && GET_CODE (NEXT_INSN (p)) != BARRIER | |
7440 | && GET_CODE (NEXT_INSN (p)) != CODE_LABEL) | |
7441 | { | |
eec9ef57 JL |
7442 | /* Note, we must update P with the return value from |
7443 | delete_insn, otherwise we could get an infinite loop | |
7444 | if NEXT_INSN (p) had INSN_DELETED_P set. */ | |
7afe21cc RK |
7445 | if (GET_CODE (NEXT_INSN (p)) != NOTE |
7446 | || NOTE_LINE_NUMBER (NEXT_INSN (p)) == NOTE_INSN_DELETED) | |
778e0677 | 7447 | p = PREV_INSN (delete_insn (NEXT_INSN (p))); |
7afe21cc RK |
7448 | else |
7449 | p = NEXT_INSN (p); | |
7450 | } | |
7451 | ||
7452 | /* If we don't have a BARRIER immediately after INSN, put one there. | |
7453 | Much code assumes that there are no NOTEs between a JUMP_INSN and | |
7454 | BARRIER. */ | |
7455 | ||
7456 | if (NEXT_INSN (insn) == 0 | |
7457 | || GET_CODE (NEXT_INSN (insn)) != BARRIER) | |
783e5bca | 7458 | emit_barrier_before (NEXT_INSN (insn)); |
7afe21cc RK |
7459 | |
7460 | /* We might have two BARRIERs separated by notes. Delete the second | |
7461 | one if so. */ | |
7462 | ||
538b78e7 RS |
7463 | if (p != insn && NEXT_INSN (p) != 0 |
7464 | && GET_CODE (NEXT_INSN (p)) == BARRIER) | |
7afe21cc RK |
7465 | delete_insn (NEXT_INSN (p)); |
7466 | ||
7467 | cse_jumps_altered = 1; | |
7468 | sets[i].rtl = 0; | |
7469 | } | |
7470 | ||
c2a47e48 RK |
7471 | /* If destination is volatile, invalidate it and then do no further |
7472 | processing for this assignment. */ | |
7afe21cc RK |
7473 | |
7474 | else if (do_not_record) | |
c2a47e48 RK |
7475 | { |
7476 | if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG | |
7477 | || GET_CODE (dest) == MEM) | |
bb4034b3 | 7478 | invalidate (dest, VOIDmode); |
2708da92 RS |
7479 | else if (GET_CODE (dest) == STRICT_LOW_PART |
7480 | || GET_CODE (dest) == ZERO_EXTRACT) | |
bb4034b3 | 7481 | invalidate (XEXP (dest, 0), GET_MODE (dest)); |
c2a47e48 RK |
7482 | sets[i].rtl = 0; |
7483 | } | |
7afe21cc RK |
7484 | |
7485 | if (sets[i].rtl != 0 && dest != SET_DEST (sets[i].rtl)) | |
2197a88a | 7486 | sets[i].dest_hash = HASH (SET_DEST (sets[i].rtl), mode); |
7afe21cc RK |
7487 | |
7488 | #ifdef HAVE_cc0 | |
7489 | /* If setting CC0, record what it was set to, or a constant, if it | |
7490 | is equivalent to a constant. If it is being set to a floating-point | |
7491 | value, make a COMPARE with the appropriate constant of 0. If we | |
7492 | don't do this, later code can interpret this as a test against | |
7493 | const0_rtx, which can cause problems if we try to put it into an | |
7494 | insn as a floating-point operand. */ | |
7495 | if (dest == cc0_rtx) | |
7496 | { | |
7497 | this_insn_cc0 = src_const && mode != VOIDmode ? src_const : src; | |
7498 | this_insn_cc0_mode = mode; | |
cbf6a543 | 7499 | if (FLOAT_MODE_P (mode)) |
38a448ca RH |
7500 | this_insn_cc0 = gen_rtx_COMPARE (VOIDmode, this_insn_cc0, |
7501 | CONST0_RTX (mode)); | |
7afe21cc RK |
7502 | } |
7503 | #endif | |
7504 | } | |
7505 | ||
7506 | /* Now enter all non-volatile source expressions in the hash table | |
7507 | if they are not already present. | |
7508 | Record their equivalence classes in src_elt. | |
7509 | This way we can insert the corresponding destinations into | |
7510 | the same classes even if the actual sources are no longer in them | |
7511 | (having been invalidated). */ | |
7512 | ||
7513 | if (src_eqv && src_eqv_elt == 0 && sets[0].rtl != 0 && ! src_eqv_volatile | |
7514 | && ! rtx_equal_p (src_eqv, SET_DEST (sets[0].rtl))) | |
7515 | { | |
7516 | register struct table_elt *elt; | |
7517 | register struct table_elt *classp = sets[0].src_elt; | |
7518 | rtx dest = SET_DEST (sets[0].rtl); | |
7519 | enum machine_mode eqvmode = GET_MODE (dest); | |
7520 | ||
7521 | if (GET_CODE (dest) == STRICT_LOW_PART) | |
7522 | { | |
7523 | eqvmode = GET_MODE (SUBREG_REG (XEXP (dest, 0))); | |
7524 | classp = 0; | |
7525 | } | |
7526 | if (insert_regs (src_eqv, classp, 0)) | |
8ae2b8f6 JW |
7527 | { |
7528 | rehash_using_reg (src_eqv); | |
7529 | src_eqv_hash = HASH (src_eqv, eqvmode); | |
7530 | } | |
2197a88a | 7531 | elt = insert (src_eqv, classp, src_eqv_hash, eqvmode); |
7afe21cc RK |
7532 | elt->in_memory = src_eqv_in_memory; |
7533 | elt->in_struct = src_eqv_in_struct; | |
7534 | src_eqv_elt = elt; | |
f7911249 JW |
7535 | |
7536 | /* Check to see if src_eqv_elt is the same as a set source which | |
7537 | does not yet have an elt, and if so set the elt of the set source | |
7538 | to src_eqv_elt. */ | |
7539 | for (i = 0; i < n_sets; i++) | |
7540 | if (sets[i].rtl && sets[i].src_elt == 0 | |
7541 | && rtx_equal_p (SET_SRC (sets[i].rtl), src_eqv)) | |
7542 | sets[i].src_elt = src_eqv_elt; | |
7afe21cc RK |
7543 | } |
7544 | ||
7545 | for (i = 0; i < n_sets; i++) | |
7546 | if (sets[i].rtl && ! sets[i].src_volatile | |
7547 | && ! rtx_equal_p (SET_SRC (sets[i].rtl), SET_DEST (sets[i].rtl))) | |
7548 | { | |
7549 | if (GET_CODE (SET_DEST (sets[i].rtl)) == STRICT_LOW_PART) | |
7550 | { | |
7551 | /* REG_EQUAL in setting a STRICT_LOW_PART | |
7552 | gives an equivalent for the entire destination register, | |
7553 | not just for the subreg being stored in now. | |
7554 | This is a more interesting equivalence, so we arrange later | |
7555 | to treat the entire reg as the destination. */ | |
7556 | sets[i].src_elt = src_eqv_elt; | |
2197a88a | 7557 | sets[i].src_hash = src_eqv_hash; |
7afe21cc RK |
7558 | } |
7559 | else | |
7560 | { | |
7561 | /* Insert source and constant equivalent into hash table, if not | |
7562 | already present. */ | |
7563 | register struct table_elt *classp = src_eqv_elt; | |
7564 | register rtx src = sets[i].src; | |
7565 | register rtx dest = SET_DEST (sets[i].rtl); | |
7566 | enum machine_mode mode | |
7567 | = GET_MODE (src) == VOIDmode ? GET_MODE (dest) : GET_MODE (src); | |
7568 | ||
15c68354 R |
7569 | /* Don't put a hard register source into the table if this is |
7570 | the last insn of a libcall. */ | |
7571 | if (sets[i].src_elt == 0 | |
7572 | && (GET_CODE (src) != REG | |
7573 | || REGNO (src) >= FIRST_PSEUDO_REGISTER | |
7574 | || ! find_reg_note (insn, REG_RETVAL, NULL_RTX))) | |
7afe21cc RK |
7575 | { |
7576 | register struct table_elt *elt; | |
7577 | ||
7578 | /* Note that these insert_regs calls cannot remove | |
7579 | any of the src_elt's, because they would have failed to | |
7580 | match if not still valid. */ | |
7581 | if (insert_regs (src, classp, 0)) | |
8ae2b8f6 JW |
7582 | { |
7583 | rehash_using_reg (src); | |
7584 | sets[i].src_hash = HASH (src, mode); | |
7585 | } | |
2197a88a | 7586 | elt = insert (src, classp, sets[i].src_hash, mode); |
7afe21cc RK |
7587 | elt->in_memory = sets[i].src_in_memory; |
7588 | elt->in_struct = sets[i].src_in_struct; | |
7589 | sets[i].src_elt = classp = elt; | |
7590 | } | |
7591 | ||
7592 | if (sets[i].src_const && sets[i].src_const_elt == 0 | |
7593 | && src != sets[i].src_const | |
7594 | && ! rtx_equal_p (sets[i].src_const, src)) | |
7595 | sets[i].src_elt = insert (sets[i].src_const, classp, | |
2197a88a | 7596 | sets[i].src_const_hash, mode); |
7afe21cc RK |
7597 | } |
7598 | } | |
7599 | else if (sets[i].src_elt == 0) | |
7600 | /* If we did not insert the source into the hash table (e.g., it was | |
7601 | volatile), note the equivalence class for the REG_EQUAL value, if any, | |
7602 | so that the destination goes into that class. */ | |
7603 | sets[i].src_elt = src_eqv_elt; | |
7604 | ||
9ae8ffe7 | 7605 | invalidate_from_clobbers (x); |
77fa0940 RK |
7606 | |
7607 | /* Some registers are invalidated by subroutine calls. Memory is | |
7608 | invalidated by non-constant calls. */ | |
7609 | ||
7afe21cc RK |
7610 | if (GET_CODE (insn) == CALL_INSN) |
7611 | { | |
77fa0940 | 7612 | if (! CONST_CALL_P (insn)) |
9ae8ffe7 | 7613 | invalidate_memory (); |
7afe21cc RK |
7614 | invalidate_for_call (); |
7615 | } | |
7616 | ||
7617 | /* Now invalidate everything set by this instruction. | |
7618 | If a SUBREG or other funny destination is being set, | |
7619 | sets[i].rtl is still nonzero, so here we invalidate the reg | |
7620 | a part of which is being set. */ | |
7621 | ||
7622 | for (i = 0; i < n_sets; i++) | |
7623 | if (sets[i].rtl) | |
7624 | { | |
bb4034b3 JW |
7625 | /* We can't use the inner dest, because the mode associated with |
7626 | a ZERO_EXTRACT is significant. */ | |
7627 | register rtx dest = SET_DEST (sets[i].rtl); | |
7afe21cc RK |
7628 | |
7629 | /* Needed for registers to remove the register from its | |
7630 | previous quantity's chain. | |
7631 | Needed for memory if this is a nonvarying address, unless | |
7632 | we have just done an invalidate_memory that covers even those. */ | |
7633 | if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG | |
9ae8ffe7 | 7634 | || GET_CODE (dest) == MEM) |
bb4034b3 | 7635 | invalidate (dest, VOIDmode); |
2708da92 RS |
7636 | else if (GET_CODE (dest) == STRICT_LOW_PART |
7637 | || GET_CODE (dest) == ZERO_EXTRACT) | |
bb4034b3 | 7638 | invalidate (XEXP (dest, 0), GET_MODE (dest)); |
7afe21cc RK |
7639 | } |
7640 | ||
01e752d3 JL |
7641 | /* A volatile ASM invalidates everything. */ |
7642 | if (GET_CODE (insn) == INSN | |
7643 | && GET_CODE (PATTERN (insn)) == ASM_OPERANDS | |
7644 | && MEM_VOLATILE_P (PATTERN (insn))) | |
7645 | flush_hash_table (); | |
7646 | ||
7afe21cc RK |
7647 | /* Make sure registers mentioned in destinations |
7648 | are safe for use in an expression to be inserted. | |
7649 | This removes from the hash table | |
7650 | any invalid entry that refers to one of these registers. | |
7651 | ||
7652 | We don't care about the return value from mention_regs because | |
7653 | we are going to hash the SET_DEST values unconditionally. */ | |
7654 | ||
7655 | for (i = 0; i < n_sets; i++) | |
34c73909 R |
7656 | { |
7657 | if (sets[i].rtl) | |
7658 | { | |
7659 | rtx x = SET_DEST (sets[i].rtl); | |
7660 | ||
7661 | if (GET_CODE (x) != REG) | |
7662 | mention_regs (x); | |
7663 | else | |
7664 | { | |
7665 | /* We used to rely on all references to a register becoming | |
7666 | inaccessible when a register changes to a new quantity, | |
7667 | since that changes the hash code. However, that is not | |
7668 | safe, since after NBUCKETS new quantities we get a | |
7669 | hash 'collision' of a register with its own invalid | |
7670 | entries. And since SUBREGs have been changed not to | |
7671 | change their hash code with the hash code of the register, | |
7672 | it wouldn't work any longer at all. So we have to check | |
7673 | for any invalid references lying around now. | |
7674 | This code is similar to the REG case in mention_regs, | |
7675 | but it knows that reg_tick has been incremented, and | |
7676 | it leaves reg_in_table as -1 . */ | |
7677 | register int regno = REGNO (x); | |
7678 | register int endregno | |
7679 | = regno + (regno >= FIRST_PSEUDO_REGISTER ? 1 | |
7680 | : HARD_REGNO_NREGS (regno, GET_MODE (x))); | |
7681 | int i; | |
7682 | ||
7683 | for (i = regno; i < endregno; i++) | |
7684 | { | |
30f72379 | 7685 | if (REG_IN_TABLE (i) >= 0) |
34c73909 R |
7686 | { |
7687 | remove_invalid_refs (i); | |
30f72379 | 7688 | REG_IN_TABLE (i) = -1; |
34c73909 R |
7689 | } |
7690 | } | |
7691 | } | |
7692 | } | |
7693 | } | |
7afe21cc RK |
7694 | |
7695 | /* We may have just removed some of the src_elt's from the hash table. | |
7696 | So replace each one with the current head of the same class. */ | |
7697 | ||
7698 | for (i = 0; i < n_sets; i++) | |
7699 | if (sets[i].rtl) | |
7700 | { | |
7701 | if (sets[i].src_elt && sets[i].src_elt->first_same_value == 0) | |
7702 | /* If elt was removed, find current head of same class, | |
7703 | or 0 if nothing remains of that class. */ | |
7704 | { | |
7705 | register struct table_elt *elt = sets[i].src_elt; | |
7706 | ||
7707 | while (elt && elt->prev_same_value) | |
7708 | elt = elt->prev_same_value; | |
7709 | ||
7710 | while (elt && elt->first_same_value == 0) | |
7711 | elt = elt->next_same_value; | |
7712 | sets[i].src_elt = elt ? elt->first_same_value : 0; | |
7713 | } | |
7714 | } | |
7715 | ||
7716 | /* Now insert the destinations into their equivalence classes. */ | |
7717 | ||
7718 | for (i = 0; i < n_sets; i++) | |
7719 | if (sets[i].rtl) | |
7720 | { | |
7721 | register rtx dest = SET_DEST (sets[i].rtl); | |
9de2c71a | 7722 | rtx inner_dest = sets[i].inner_dest; |
7afe21cc RK |
7723 | register struct table_elt *elt; |
7724 | ||
7725 | /* Don't record value if we are not supposed to risk allocating | |
7726 | floating-point values in registers that might be wider than | |
7727 | memory. */ | |
7728 | if ((flag_float_store | |
7729 | && GET_CODE (dest) == MEM | |
cbf6a543 | 7730 | && FLOAT_MODE_P (GET_MODE (dest))) |
bc4ddc77 JW |
7731 | /* Don't record BLKmode values, because we don't know the |
7732 | size of it, and can't be sure that other BLKmode values | |
7733 | have the same or smaller size. */ | |
7734 | || GET_MODE (dest) == BLKmode | |
7afe21cc RK |
7735 | /* Don't record values of destinations set inside a libcall block |
7736 | since we might delete the libcall. Things should have been set | |
7737 | up so we won't want to reuse such a value, but we play it safe | |
7738 | here. */ | |
7bd8b2a8 | 7739 | || libcall_insn |
7afe21cc RK |
7740 | /* If we didn't put a REG_EQUAL value or a source into the hash |
7741 | table, there is no point is recording DEST. */ | |
1a8e9a8e RK |
7742 | || sets[i].src_elt == 0 |
7743 | /* If DEST is a paradoxical SUBREG and SRC is a ZERO_EXTEND | |
7744 | or SIGN_EXTEND, don't record DEST since it can cause | |
7745 | some tracking to be wrong. | |
7746 | ||
7747 | ??? Think about this more later. */ | |
7748 | || (GET_CODE (dest) == SUBREG | |
7749 | && (GET_MODE_SIZE (GET_MODE (dest)) | |
7750 | > GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))) | |
7751 | && (GET_CODE (sets[i].src) == SIGN_EXTEND | |
7752 | || GET_CODE (sets[i].src) == ZERO_EXTEND))) | |
7afe21cc RK |
7753 | continue; |
7754 | ||
7755 | /* STRICT_LOW_PART isn't part of the value BEING set, | |
7756 | and neither is the SUBREG inside it. | |
7757 | Note that in this case SETS[I].SRC_ELT is really SRC_EQV_ELT. */ | |
7758 | if (GET_CODE (dest) == STRICT_LOW_PART) | |
7759 | dest = SUBREG_REG (XEXP (dest, 0)); | |
7760 | ||
c610adec | 7761 | if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG) |
7afe21cc RK |
7762 | /* Registers must also be inserted into chains for quantities. */ |
7763 | if (insert_regs (dest, sets[i].src_elt, 1)) | |
8ae2b8f6 JW |
7764 | { |
7765 | /* If `insert_regs' changes something, the hash code must be | |
7766 | recalculated. */ | |
7767 | rehash_using_reg (dest); | |
7768 | sets[i].dest_hash = HASH (dest, GET_MODE (dest)); | |
7769 | } | |
7afe21cc | 7770 | |
9de2c71a MM |
7771 | if (GET_CODE (inner_dest) == MEM |
7772 | && GET_CODE (XEXP (inner_dest, 0)) == ADDRESSOF) | |
7773 | /* Given (SET (MEM (ADDRESSOF (X))) Y) we don't want to say | |
7774 | that (MEM (ADDRESSOF (X))) is equivalent to Y. | |
7775 | Consider the case in which the address of the MEM is | |
7776 | passed to a function, which alters the MEM. Then, if we | |
7777 | later use Y instead of the MEM we'll miss the update. */ | |
7778 | elt = insert (dest, 0, sets[i].dest_hash, GET_MODE (dest)); | |
7779 | else | |
7780 | elt = insert (dest, sets[i].src_elt, | |
7781 | sets[i].dest_hash, GET_MODE (dest)); | |
7782 | ||
c256df0b | 7783 | elt->in_memory = (GET_CODE (sets[i].inner_dest) == MEM |
9ad91d71 RK |
7784 | && (! RTX_UNCHANGING_P (sets[i].inner_dest) |
7785 | || FIXED_BASE_PLUS_P (XEXP (sets[i].inner_dest, | |
7786 | 0)))); | |
c256df0b | 7787 | |
7afe21cc RK |
7788 | if (elt->in_memory) |
7789 | { | |
7790 | /* This implicitly assumes a whole struct | |
7791 | need not have MEM_IN_STRUCT_P. | |
7792 | But a whole struct is *supposed* to have MEM_IN_STRUCT_P. */ | |
7793 | elt->in_struct = (MEM_IN_STRUCT_P (sets[i].inner_dest) | |
7794 | || sets[i].inner_dest != SET_DEST (sets[i].rtl)); | |
7795 | } | |
7796 | ||
fc3ffe83 RK |
7797 | /* If we have (set (subreg:m1 (reg:m2 foo) 0) (bar:m1)), M1 is no |
7798 | narrower than M2, and both M1 and M2 are the same number of words, | |
7799 | we are also doing (set (reg:m2 foo) (subreg:m2 (bar:m1) 0)) so | |
7800 | make that equivalence as well. | |
7afe21cc RK |
7801 | |
7802 | However, BAR may have equivalences for which gen_lowpart_if_possible | |
7803 | will produce a simpler value than gen_lowpart_if_possible applied to | |
7804 | BAR (e.g., if BAR was ZERO_EXTENDed from M2), so we will scan all | |
7805 | BAR's equivalences. If we don't get a simplified form, make | |
7806 | the SUBREG. It will not be used in an equivalence, but will | |
7807 | cause two similar assignments to be detected. | |
7808 | ||
7809 | Note the loop below will find SUBREG_REG (DEST) since we have | |
7810 | already entered SRC and DEST of the SET in the table. */ | |
7811 | ||
7812 | if (GET_CODE (dest) == SUBREG | |
6cdbaec4 RK |
7813 | && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))) - 1) |
7814 | / UNITS_PER_WORD) | |
7815 | == (GET_MODE_SIZE (GET_MODE (dest)) - 1)/ UNITS_PER_WORD) | |
7afe21cc RK |
7816 | && (GET_MODE_SIZE (GET_MODE (dest)) |
7817 | >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))) | |
7818 | && sets[i].src_elt != 0) | |
7819 | { | |
7820 | enum machine_mode new_mode = GET_MODE (SUBREG_REG (dest)); | |
7821 | struct table_elt *elt, *classp = 0; | |
7822 | ||
7823 | for (elt = sets[i].src_elt->first_same_value; elt; | |
7824 | elt = elt->next_same_value) | |
7825 | { | |
7826 | rtx new_src = 0; | |
2197a88a | 7827 | unsigned src_hash; |
7afe21cc RK |
7828 | struct table_elt *src_elt; |
7829 | ||
7830 | /* Ignore invalid entries. */ | |
7831 | if (GET_CODE (elt->exp) != REG | |
7832 | && ! exp_equiv_p (elt->exp, elt->exp, 1, 0)) | |
7833 | continue; | |
7834 | ||
7835 | new_src = gen_lowpart_if_possible (new_mode, elt->exp); | |
7836 | if (new_src == 0) | |
38a448ca | 7837 | new_src = gen_rtx_SUBREG (new_mode, elt->exp, 0); |
7afe21cc RK |
7838 | |
7839 | src_hash = HASH (new_src, new_mode); | |
7840 | src_elt = lookup (new_src, src_hash, new_mode); | |
7841 | ||
7842 | /* Put the new source in the hash table is if isn't | |
7843 | already. */ | |
7844 | if (src_elt == 0) | |
7845 | { | |
7846 | if (insert_regs (new_src, classp, 0)) | |
8ae2b8f6 JW |
7847 | { |
7848 | rehash_using_reg (new_src); | |
7849 | src_hash = HASH (new_src, new_mode); | |
7850 | } | |
7afe21cc RK |
7851 | src_elt = insert (new_src, classp, src_hash, new_mode); |
7852 | src_elt->in_memory = elt->in_memory; | |
7853 | src_elt->in_struct = elt->in_struct; | |
7854 | } | |
7855 | else if (classp && classp != src_elt->first_same_value) | |
7856 | /* Show that two things that we've seen before are | |
7857 | actually the same. */ | |
7858 | merge_equiv_classes (src_elt, classp); | |
7859 | ||
7860 | classp = src_elt->first_same_value; | |
da932f04 JL |
7861 | /* Ignore invalid entries. */ |
7862 | while (classp | |
7863 | && GET_CODE (classp->exp) != REG | |
7864 | && ! exp_equiv_p (classp->exp, classp->exp, 1, 0)) | |
7865 | classp = classp->next_same_value; | |
7afe21cc RK |
7866 | } |
7867 | } | |
7868 | } | |
7869 | ||
7870 | /* Special handling for (set REG0 REG1) | |
7871 | where REG0 is the "cheapest", cheaper than REG1. | |
7872 | After cse, REG1 will probably not be used in the sequel, | |
7873 | so (if easily done) change this insn to (set REG1 REG0) and | |
7874 | replace REG1 with REG0 in the previous insn that computed their value. | |
7875 | Then REG1 will become a dead store and won't cloud the situation | |
7876 | for later optimizations. | |
7877 | ||
7878 | Do not make this change if REG1 is a hard register, because it will | |
7879 | then be used in the sequel and we may be changing a two-operand insn | |
7880 | into a three-operand insn. | |
7881 | ||
50270076 R |
7882 | Also do not do this if we are operating on a copy of INSN. |
7883 | ||
7884 | Also don't do this if INSN ends a libcall; this would cause an unrelated | |
7885 | register to be set in the middle of a libcall, and we then get bad code | |
7886 | if the libcall is deleted. */ | |
7afe21cc RK |
7887 | |
7888 | if (n_sets == 1 && sets[0].rtl && GET_CODE (SET_DEST (sets[0].rtl)) == REG | |
7889 | && NEXT_INSN (PREV_INSN (insn)) == insn | |
7890 | && GET_CODE (SET_SRC (sets[0].rtl)) == REG | |
7891 | && REGNO (SET_SRC (sets[0].rtl)) >= FIRST_PSEUDO_REGISTER | |
7892 | && REGNO_QTY_VALID_P (REGNO (SET_SRC (sets[0].rtl))) | |
30f72379 | 7893 | && (qty_first_reg[REG_QTY (REGNO (SET_SRC (sets[0].rtl)))] |
50270076 R |
7894 | == REGNO (SET_DEST (sets[0].rtl))) |
7895 | && ! find_reg_note (insn, REG_RETVAL, NULL_RTX)) | |
7afe21cc RK |
7896 | { |
7897 | rtx prev = PREV_INSN (insn); | |
7898 | while (prev && GET_CODE (prev) == NOTE) | |
7899 | prev = PREV_INSN (prev); | |
7900 | ||
7901 | if (prev && GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SET | |
7902 | && SET_DEST (PATTERN (prev)) == SET_SRC (sets[0].rtl)) | |
7903 | { | |
7904 | rtx dest = SET_DEST (sets[0].rtl); | |
906c4e36 | 7905 | rtx note = find_reg_note (prev, REG_EQUIV, NULL_RTX); |
7afe21cc RK |
7906 | |
7907 | validate_change (prev, & SET_DEST (PATTERN (prev)), dest, 1); | |
7908 | validate_change (insn, & SET_DEST (sets[0].rtl), | |
7909 | SET_SRC (sets[0].rtl), 1); | |
7910 | validate_change (insn, & SET_SRC (sets[0].rtl), dest, 1); | |
7911 | apply_change_group (); | |
7912 | ||
7913 | /* If REG1 was equivalent to a constant, REG0 is not. */ | |
7914 | if (note) | |
7915 | PUT_REG_NOTE_KIND (note, REG_EQUAL); | |
7916 | ||
7917 | /* If there was a REG_WAS_0 note on PREV, remove it. Move | |
7918 | any REG_WAS_0 note on INSN to PREV. */ | |
906c4e36 | 7919 | note = find_reg_note (prev, REG_WAS_0, NULL_RTX); |
7afe21cc RK |
7920 | if (note) |
7921 | remove_note (prev, note); | |
7922 | ||
906c4e36 | 7923 | note = find_reg_note (insn, REG_WAS_0, NULL_RTX); |
7afe21cc RK |
7924 | if (note) |
7925 | { | |
7926 | remove_note (insn, note); | |
7927 | XEXP (note, 1) = REG_NOTES (prev); | |
7928 | REG_NOTES (prev) = note; | |
7929 | } | |
98369a0f RK |
7930 | |
7931 | /* If INSN has a REG_EQUAL note, and this note mentions REG0, | |
7932 | then we must delete it, because the value in REG0 has changed. */ | |
7933 | note = find_reg_note (insn, REG_EQUAL, NULL_RTX); | |
7934 | if (note && reg_mentioned_p (dest, XEXP (note, 0))) | |
7935 | remove_note (insn, note); | |
7afe21cc RK |
7936 | } |
7937 | } | |
7938 | ||
7939 | /* If this is a conditional jump insn, record any known equivalences due to | |
7940 | the condition being tested. */ | |
7941 | ||
7942 | last_jump_equiv_class = 0; | |
7943 | if (GET_CODE (insn) == JUMP_INSN | |
7944 | && n_sets == 1 && GET_CODE (x) == SET | |
7945 | && GET_CODE (SET_SRC (x)) == IF_THEN_ELSE) | |
7946 | record_jump_equiv (insn, 0); | |
7947 | ||
7948 | #ifdef HAVE_cc0 | |
7949 | /* If the previous insn set CC0 and this insn no longer references CC0, | |
7950 | delete the previous insn. Here we use the fact that nothing expects CC0 | |
7951 | to be valid over an insn, which is true until the final pass. */ | |
7952 | if (prev_insn && GET_CODE (prev_insn) == INSN | |
7953 | && (tem = single_set (prev_insn)) != 0 | |
7954 | && SET_DEST (tem) == cc0_rtx | |
7955 | && ! reg_mentioned_p (cc0_rtx, x)) | |
7956 | { | |
7957 | PUT_CODE (prev_insn, NOTE); | |
7958 | NOTE_LINE_NUMBER (prev_insn) = NOTE_INSN_DELETED; | |
7959 | NOTE_SOURCE_FILE (prev_insn) = 0; | |
7960 | } | |
7961 | ||
7962 | prev_insn_cc0 = this_insn_cc0; | |
7963 | prev_insn_cc0_mode = this_insn_cc0_mode; | |
7964 | #endif | |
7965 | ||
7966 | prev_insn = insn; | |
7967 | } | |
7968 | \f | |
a4c6502a | 7969 | /* Remove from the hash table all expressions that reference memory. */ |
7afe21cc | 7970 | static void |
9ae8ffe7 | 7971 | invalidate_memory () |
7afe21cc | 7972 | { |
9ae8ffe7 JL |
7973 | register int i; |
7974 | register struct table_elt *p, *next; | |
7afe21cc | 7975 | |
9ae8ffe7 JL |
7976 | for (i = 0; i < NBUCKETS; i++) |
7977 | for (p = table[i]; p; p = next) | |
7978 | { | |
7979 | next = p->next_same_hash; | |
7980 | if (p->in_memory) | |
7981 | remove_from_table (p, i); | |
7982 | } | |
7983 | } | |
7984 | ||
7985 | /* XXX ??? The name of this function bears little resemblance to | |
7986 | what this function actually does. FIXME. */ | |
7987 | static int | |
7988 | note_mem_written (addr) | |
7989 | register rtx addr; | |
7990 | { | |
7991 | /* Pushing or popping the stack invalidates just the stack pointer. */ | |
7992 | if ((GET_CODE (addr) == PRE_DEC || GET_CODE (addr) == PRE_INC | |
7993 | || GET_CODE (addr) == POST_DEC || GET_CODE (addr) == POST_INC) | |
7994 | && GET_CODE (XEXP (addr, 0)) == REG | |
7995 | && REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM) | |
7afe21cc | 7996 | { |
30f72379 MM |
7997 | if (REG_TICK (STACK_POINTER_REGNUM) >= 0) |
7998 | REG_TICK (STACK_POINTER_REGNUM)++; | |
9ae8ffe7 JL |
7999 | |
8000 | /* This should be *very* rare. */ | |
8001 | if (TEST_HARD_REG_BIT (hard_regs_in_table, STACK_POINTER_REGNUM)) | |
8002 | invalidate (stack_pointer_rtx, VOIDmode); | |
8003 | return 1; | |
7afe21cc | 8004 | } |
9ae8ffe7 | 8005 | return 0; |
7afe21cc RK |
8006 | } |
8007 | ||
8008 | /* Perform invalidation on the basis of everything about an insn | |
8009 | except for invalidating the actual places that are SET in it. | |
8010 | This includes the places CLOBBERed, and anything that might | |
8011 | alias with something that is SET or CLOBBERed. | |
8012 | ||
7afe21cc RK |
8013 | X is the pattern of the insn. */ |
8014 | ||
8015 | static void | |
9ae8ffe7 | 8016 | invalidate_from_clobbers (x) |
7afe21cc RK |
8017 | rtx x; |
8018 | { | |
7afe21cc RK |
8019 | if (GET_CODE (x) == CLOBBER) |
8020 | { | |
8021 | rtx ref = XEXP (x, 0); | |
9ae8ffe7 JL |
8022 | if (ref) |
8023 | { | |
8024 | if (GET_CODE (ref) == REG || GET_CODE (ref) == SUBREG | |
8025 | || GET_CODE (ref) == MEM) | |
8026 | invalidate (ref, VOIDmode); | |
8027 | else if (GET_CODE (ref) == STRICT_LOW_PART | |
8028 | || GET_CODE (ref) == ZERO_EXTRACT) | |
8029 | invalidate (XEXP (ref, 0), GET_MODE (ref)); | |
8030 | } | |
7afe21cc RK |
8031 | } |
8032 | else if (GET_CODE (x) == PARALLEL) | |
8033 | { | |
8034 | register int i; | |
8035 | for (i = XVECLEN (x, 0) - 1; i >= 0; i--) | |
8036 | { | |
8037 | register rtx y = XVECEXP (x, 0, i); | |
8038 | if (GET_CODE (y) == CLOBBER) | |
8039 | { | |
8040 | rtx ref = XEXP (y, 0); | |
9ae8ffe7 JL |
8041 | if (GET_CODE (ref) == REG || GET_CODE (ref) == SUBREG |
8042 | || GET_CODE (ref) == MEM) | |
8043 | invalidate (ref, VOIDmode); | |
8044 | else if (GET_CODE (ref) == STRICT_LOW_PART | |
8045 | || GET_CODE (ref) == ZERO_EXTRACT) | |
8046 | invalidate (XEXP (ref, 0), GET_MODE (ref)); | |
7afe21cc RK |
8047 | } |
8048 | } | |
8049 | } | |
8050 | } | |
8051 | \f | |
8052 | /* Process X, part of the REG_NOTES of an insn. Look at any REG_EQUAL notes | |
8053 | and replace any registers in them with either an equivalent constant | |
8054 | or the canonical form of the register. If we are inside an address, | |
8055 | only do this if the address remains valid. | |
8056 | ||
8057 | OBJECT is 0 except when within a MEM in which case it is the MEM. | |
8058 | ||
8059 | Return the replacement for X. */ | |
8060 | ||
8061 | static rtx | |
8062 | cse_process_notes (x, object) | |
8063 | rtx x; | |
8064 | rtx object; | |
8065 | { | |
8066 | enum rtx_code code = GET_CODE (x); | |
6f7d635c | 8067 | const char *fmt = GET_RTX_FORMAT (code); |
7afe21cc RK |
8068 | int i; |
8069 | ||
8070 | switch (code) | |
8071 | { | |
8072 | case CONST_INT: | |
8073 | case CONST: | |
8074 | case SYMBOL_REF: | |
8075 | case LABEL_REF: | |
8076 | case CONST_DOUBLE: | |
8077 | case PC: | |
8078 | case CC0: | |
8079 | case LO_SUM: | |
8080 | return x; | |
8081 | ||
8082 | case MEM: | |
8083 | XEXP (x, 0) = cse_process_notes (XEXP (x, 0), x); | |
8084 | return x; | |
8085 | ||
8086 | case EXPR_LIST: | |
8087 | case INSN_LIST: | |
8088 | if (REG_NOTE_KIND (x) == REG_EQUAL) | |
906c4e36 | 8089 | XEXP (x, 0) = cse_process_notes (XEXP (x, 0), NULL_RTX); |
7afe21cc | 8090 | if (XEXP (x, 1)) |
906c4e36 | 8091 | XEXP (x, 1) = cse_process_notes (XEXP (x, 1), NULL_RTX); |
7afe21cc RK |
8092 | return x; |
8093 | ||
e4890d45 RS |
8094 | case SIGN_EXTEND: |
8095 | case ZERO_EXTEND: | |
0b0ee36c | 8096 | case SUBREG: |
e4890d45 RS |
8097 | { |
8098 | rtx new = cse_process_notes (XEXP (x, 0), object); | |
8099 | /* We don't substitute VOIDmode constants into these rtx, | |
8100 | since they would impede folding. */ | |
8101 | if (GET_MODE (new) != VOIDmode) | |
8102 | validate_change (object, &XEXP (x, 0), new, 0); | |
8103 | return x; | |
8104 | } | |
8105 | ||
7afe21cc | 8106 | case REG: |
30f72379 | 8107 | i = REG_QTY (REGNO (x)); |
7afe21cc RK |
8108 | |
8109 | /* Return a constant or a constant register. */ | |
8110 | if (REGNO_QTY_VALID_P (REGNO (x)) | |
8111 | && qty_const[i] != 0 | |
8112 | && (CONSTANT_P (qty_const[i]) | |
8113 | || GET_CODE (qty_const[i]) == REG)) | |
8114 | { | |
8115 | rtx new = gen_lowpart_if_possible (GET_MODE (x), qty_const[i]); | |
8116 | if (new) | |
8117 | return new; | |
8118 | } | |
8119 | ||
8120 | /* Otherwise, canonicalize this register. */ | |
906c4e36 | 8121 | return canon_reg (x, NULL_RTX); |
e9a25f70 JL |
8122 | |
8123 | default: | |
8124 | break; | |
7afe21cc RK |
8125 | } |
8126 | ||
8127 | for (i = 0; i < GET_RTX_LENGTH (code); i++) | |
8128 | if (fmt[i] == 'e') | |
8129 | validate_change (object, &XEXP (x, i), | |
7fe34fdf | 8130 | cse_process_notes (XEXP (x, i), object), 0); |
7afe21cc RK |
8131 | |
8132 | return x; | |
8133 | } | |
8134 | \f | |
8135 | /* Find common subexpressions between the end test of a loop and the beginning | |
8136 | of the loop. LOOP_START is the CODE_LABEL at the start of a loop. | |
8137 | ||
8138 | Often we have a loop where an expression in the exit test is used | |
8139 | in the body of the loop. For example "while (*p) *q++ = *p++;". | |
8140 | Because of the way we duplicate the loop exit test in front of the loop, | |
8141 | however, we don't detect that common subexpression. This will be caught | |
8142 | when global cse is implemented, but this is a quite common case. | |
8143 | ||
8144 | This function handles the most common cases of these common expressions. | |
8145 | It is called after we have processed the basic block ending with the | |
8146 | NOTE_INSN_LOOP_END note that ends a loop and the previous JUMP_INSN | |
8147 | jumps to a label used only once. */ | |
8148 | ||
8149 | static void | |
8150 | cse_around_loop (loop_start) | |
8151 | rtx loop_start; | |
8152 | { | |
8153 | rtx insn; | |
8154 | int i; | |
8155 | struct table_elt *p; | |
8156 | ||
8157 | /* If the jump at the end of the loop doesn't go to the start, we don't | |
8158 | do anything. */ | |
8159 | for (insn = PREV_INSN (loop_start); | |
8160 | insn && (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) >= 0); | |
8161 | insn = PREV_INSN (insn)) | |
8162 | ; | |
8163 | ||
8164 | if (insn == 0 | |
8165 | || GET_CODE (insn) != NOTE | |
8166 | || NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG) | |
8167 | return; | |
8168 | ||
8169 | /* If the last insn of the loop (the end test) was an NE comparison, | |
8170 | we will interpret it as an EQ comparison, since we fell through | |
f72aed24 | 8171 | the loop. Any equivalences resulting from that comparison are |
7afe21cc RK |
8172 | therefore not valid and must be invalidated. */ |
8173 | if (last_jump_equiv_class) | |
8174 | for (p = last_jump_equiv_class->first_same_value; p; | |
8175 | p = p->next_same_value) | |
51723711 KG |
8176 | { |
8177 | if (GET_CODE (p->exp) == MEM || GET_CODE (p->exp) == REG | |
8178 | || (GET_CODE (p->exp) == SUBREG | |
8179 | && GET_CODE (SUBREG_REG (p->exp)) == REG)) | |
8180 | invalidate (p->exp, VOIDmode); | |
8181 | else if (GET_CODE (p->exp) == STRICT_LOW_PART | |
8182 | || GET_CODE (p->exp) == ZERO_EXTRACT) | |
8183 | invalidate (XEXP (p->exp, 0), GET_MODE (p->exp)); | |
8184 | } | |
7afe21cc RK |
8185 | |
8186 | /* Process insns starting after LOOP_START until we hit a CALL_INSN or | |
8187 | a CODE_LABEL (we could handle a CALL_INSN, but it isn't worth it). | |
8188 | ||
8189 | The only thing we do with SET_DEST is invalidate entries, so we | |
8190 | can safely process each SET in order. It is slightly less efficient | |
556c714b JW |
8191 | to do so, but we only want to handle the most common cases. |
8192 | ||
8193 | The gen_move_insn call in cse_set_around_loop may create new pseudos. | |
8194 | These pseudos won't have valid entries in any of the tables indexed | |
8195 | by register number, such as reg_qty. We avoid out-of-range array | |
8196 | accesses by not processing any instructions created after cse started. */ | |
7afe21cc RK |
8197 | |
8198 | for (insn = NEXT_INSN (loop_start); | |
8199 | GET_CODE (insn) != CALL_INSN && GET_CODE (insn) != CODE_LABEL | |
556c714b | 8200 | && INSN_UID (insn) < max_insn_uid |
7afe21cc RK |
8201 | && ! (GET_CODE (insn) == NOTE |
8202 | && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END); | |
8203 | insn = NEXT_INSN (insn)) | |
8204 | { | |
8205 | if (GET_RTX_CLASS (GET_CODE (insn)) == 'i' | |
8206 | && (GET_CODE (PATTERN (insn)) == SET | |
8207 | || GET_CODE (PATTERN (insn)) == CLOBBER)) | |
8208 | cse_set_around_loop (PATTERN (insn), insn, loop_start); | |
8209 | else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i' | |
8210 | && GET_CODE (PATTERN (insn)) == PARALLEL) | |
8211 | for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--) | |
8212 | if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET | |
8213 | || GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == CLOBBER) | |
8214 | cse_set_around_loop (XVECEXP (PATTERN (insn), 0, i), insn, | |
8215 | loop_start); | |
8216 | } | |
8217 | } | |
8218 | \f | |
8b3686ed RK |
8219 | /* Process one SET of an insn that was skipped. We ignore CLOBBERs |
8220 | since they are done elsewhere. This function is called via note_stores. */ | |
8221 | ||
8222 | static void | |
8223 | invalidate_skipped_set (dest, set) | |
8224 | rtx set; | |
8225 | rtx dest; | |
8226 | { | |
9ae8ffe7 JL |
8227 | enum rtx_code code = GET_CODE (dest); |
8228 | ||
8229 | if (code == MEM | |
8230 | && ! note_mem_written (dest) /* If this is not a stack push ... */ | |
8231 | /* There are times when an address can appear varying and be a PLUS | |
8232 | during this scan when it would be a fixed address were we to know | |
8233 | the proper equivalences. So invalidate all memory if there is | |
8234 | a BLKmode or nonscalar memory reference or a reference to a | |
8235 | variable address. */ | |
8236 | && (MEM_IN_STRUCT_P (dest) || GET_MODE (dest) == BLKmode | |
8237 | || cse_rtx_varies_p (XEXP (dest, 0)))) | |
8238 | { | |
8239 | invalidate_memory (); | |
8240 | return; | |
8241 | } | |
ffcf6393 | 8242 | |
f47c02fa RK |
8243 | if (GET_CODE (set) == CLOBBER |
8244 | #ifdef HAVE_cc0 | |
8245 | || dest == cc0_rtx | |
8246 | #endif | |
8247 | || dest == pc_rtx) | |
8248 | return; | |
8249 | ||
9ae8ffe7 | 8250 | if (code == STRICT_LOW_PART || code == ZERO_EXTRACT) |
bb4034b3 | 8251 | invalidate (XEXP (dest, 0), GET_MODE (dest)); |
9ae8ffe7 JL |
8252 | else if (code == REG || code == SUBREG || code == MEM) |
8253 | invalidate (dest, VOIDmode); | |
8b3686ed RK |
8254 | } |
8255 | ||
8256 | /* Invalidate all insns from START up to the end of the function or the | |
8257 | next label. This called when we wish to CSE around a block that is | |
8258 | conditionally executed. */ | |
8259 | ||
8260 | static void | |
8261 | invalidate_skipped_block (start) | |
8262 | rtx start; | |
8263 | { | |
8264 | rtx insn; | |
8b3686ed RK |
8265 | |
8266 | for (insn = start; insn && GET_CODE (insn) != CODE_LABEL; | |
8267 | insn = NEXT_INSN (insn)) | |
8268 | { | |
8269 | if (GET_RTX_CLASS (GET_CODE (insn)) != 'i') | |
8270 | continue; | |
8271 | ||
8b3686ed RK |
8272 | if (GET_CODE (insn) == CALL_INSN) |
8273 | { | |
9ae8ffe7 JL |
8274 | if (! CONST_CALL_P (insn)) |
8275 | invalidate_memory (); | |
8b3686ed | 8276 | invalidate_for_call (); |
8b3686ed RK |
8277 | } |
8278 | ||
97577254 | 8279 | invalidate_from_clobbers (PATTERN (insn)); |
8b3686ed | 8280 | note_stores (PATTERN (insn), invalidate_skipped_set); |
8b3686ed RK |
8281 | } |
8282 | } | |
8283 | \f | |
7afe21cc RK |
8284 | /* Used for communication between the following two routines; contains a |
8285 | value to be checked for modification. */ | |
8286 | ||
8287 | static rtx cse_check_loop_start_value; | |
8288 | ||
8289 | /* If modifying X will modify the value in CSE_CHECK_LOOP_START_VALUE, | |
8290 | indicate that fact by setting CSE_CHECK_LOOP_START_VALUE to 0. */ | |
8291 | ||
8292 | static void | |
8293 | cse_check_loop_start (x, set) | |
8294 | rtx x; | |
d6f4ec51 | 8295 | rtx set ATTRIBUTE_UNUSED; |
7afe21cc RK |
8296 | { |
8297 | if (cse_check_loop_start_value == 0 | |
8298 | || GET_CODE (x) == CC0 || GET_CODE (x) == PC) | |
8299 | return; | |
8300 | ||
8301 | if ((GET_CODE (x) == MEM && GET_CODE (cse_check_loop_start_value) == MEM) | |
8302 | || reg_overlap_mentioned_p (x, cse_check_loop_start_value)) | |
8303 | cse_check_loop_start_value = 0; | |
8304 | } | |
8305 | ||
8306 | /* X is a SET or CLOBBER contained in INSN that was found near the start of | |
8307 | a loop that starts with the label at LOOP_START. | |
8308 | ||
8309 | If X is a SET, we see if its SET_SRC is currently in our hash table. | |
8310 | If so, we see if it has a value equal to some register used only in the | |
8311 | loop exit code (as marked by jump.c). | |
8312 | ||
8313 | If those two conditions are true, we search backwards from the start of | |
8314 | the loop to see if that same value was loaded into a register that still | |
8315 | retains its value at the start of the loop. | |
8316 | ||
8317 | If so, we insert an insn after the load to copy the destination of that | |
8318 | load into the equivalent register and (try to) replace our SET_SRC with that | |
8319 | register. | |
8320 | ||
8321 | In any event, we invalidate whatever this SET or CLOBBER modifies. */ | |
8322 | ||
8323 | static void | |
8324 | cse_set_around_loop (x, insn, loop_start) | |
8325 | rtx x; | |
8326 | rtx insn; | |
8327 | rtx loop_start; | |
8328 | { | |
7afe21cc | 8329 | struct table_elt *src_elt; |
7afe21cc RK |
8330 | |
8331 | /* If this is a SET, see if we can replace SET_SRC, but ignore SETs that | |
8332 | are setting PC or CC0 or whose SET_SRC is already a register. */ | |
8333 | if (GET_CODE (x) == SET | |
8334 | && GET_CODE (SET_DEST (x)) != PC && GET_CODE (SET_DEST (x)) != CC0 | |
8335 | && GET_CODE (SET_SRC (x)) != REG) | |
8336 | { | |
8337 | src_elt = lookup (SET_SRC (x), | |
8338 | HASH (SET_SRC (x), GET_MODE (SET_DEST (x))), | |
8339 | GET_MODE (SET_DEST (x))); | |
8340 | ||
8341 | if (src_elt) | |
8342 | for (src_elt = src_elt->first_same_value; src_elt; | |
8343 | src_elt = src_elt->next_same_value) | |
8344 | if (GET_CODE (src_elt->exp) == REG && REG_LOOP_TEST_P (src_elt->exp) | |
8345 | && COST (src_elt->exp) < COST (SET_SRC (x))) | |
8346 | { | |
8347 | rtx p, set; | |
8348 | ||
8349 | /* Look for an insn in front of LOOP_START that sets | |
8350 | something in the desired mode to SET_SRC (x) before we hit | |
8351 | a label or CALL_INSN. */ | |
8352 | ||
8353 | for (p = prev_nonnote_insn (loop_start); | |
8354 | p && GET_CODE (p) != CALL_INSN | |
8355 | && GET_CODE (p) != CODE_LABEL; | |
8356 | p = prev_nonnote_insn (p)) | |
8357 | if ((set = single_set (p)) != 0 | |
8358 | && GET_CODE (SET_DEST (set)) == REG | |
8359 | && GET_MODE (SET_DEST (set)) == src_elt->mode | |
8360 | && rtx_equal_p (SET_SRC (set), SET_SRC (x))) | |
8361 | { | |
8362 | /* We now have to ensure that nothing between P | |
8363 | and LOOP_START modified anything referenced in | |
8364 | SET_SRC (x). We know that nothing within the loop | |
8365 | can modify it, or we would have invalidated it in | |
8366 | the hash table. */ | |
8367 | rtx q; | |
8368 | ||
8369 | cse_check_loop_start_value = SET_SRC (x); | |
8370 | for (q = p; q != loop_start; q = NEXT_INSN (q)) | |
8371 | if (GET_RTX_CLASS (GET_CODE (q)) == 'i') | |
8372 | note_stores (PATTERN (q), cse_check_loop_start); | |
8373 | ||
8374 | /* If nothing was changed and we can replace our | |
8375 | SET_SRC, add an insn after P to copy its destination | |
8376 | to what we will be replacing SET_SRC with. */ | |
8377 | if (cse_check_loop_start_value | |
8378 | && validate_change (insn, &SET_SRC (x), | |
8379 | src_elt->exp, 0)) | |
e89d3e6f R |
8380 | { |
8381 | /* If this creates new pseudos, this is unsafe, | |
8382 | because the regno of new pseudo is unsuitable | |
8383 | to index into reg_qty when cse_insn processes | |
8384 | the new insn. Therefore, if a new pseudo was | |
8385 | created, discard this optimization. */ | |
8386 | int nregs = max_reg_num (); | |
8387 | rtx move | |
8388 | = gen_move_insn (src_elt->exp, SET_DEST (set)); | |
8389 | if (nregs != max_reg_num ()) | |
8390 | { | |
8391 | if (! validate_change (insn, &SET_SRC (x), | |
8392 | SET_SRC (set), 0)) | |
8393 | abort (); | |
8394 | } | |
8395 | else | |
8396 | emit_insn_after (move, p); | |
8397 | } | |
7afe21cc RK |
8398 | break; |
8399 | } | |
8400 | } | |
8401 | } | |
8402 | ||
8403 | /* Now invalidate anything modified by X. */ | |
9ae8ffe7 | 8404 | note_mem_written (SET_DEST (x)); |
7afe21cc | 8405 | |
9ae8ffe7 | 8406 | /* See comment on similar code in cse_insn for explanation of these tests. */ |
7afe21cc | 8407 | if (GET_CODE (SET_DEST (x)) == REG || GET_CODE (SET_DEST (x)) == SUBREG |
9ae8ffe7 | 8408 | || GET_CODE (SET_DEST (x)) == MEM) |
bb4034b3 | 8409 | invalidate (SET_DEST (x), VOIDmode); |
2708da92 RS |
8410 | else if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART |
8411 | || GET_CODE (SET_DEST (x)) == ZERO_EXTRACT) | |
bb4034b3 | 8412 | invalidate (XEXP (SET_DEST (x), 0), GET_MODE (SET_DEST (x))); |
7afe21cc RK |
8413 | } |
8414 | \f | |
8415 | /* Find the end of INSN's basic block and return its range, | |
8416 | the total number of SETs in all the insns of the block, the last insn of the | |
8417 | block, and the branch path. | |
8418 | ||
8419 | The branch path indicates which branches should be followed. If a non-zero | |
8420 | path size is specified, the block should be rescanned and a different set | |
8421 | of branches will be taken. The branch path is only used if | |
8b3686ed | 8422 | FLAG_CSE_FOLLOW_JUMPS or FLAG_CSE_SKIP_BLOCKS is non-zero. |
7afe21cc RK |
8423 | |
8424 | DATA is a pointer to a struct cse_basic_block_data, defined below, that is | |
8425 | used to describe the block. It is filled in with the information about | |
8426 | the current block. The incoming structure's branch path, if any, is used | |
8427 | to construct the output branch path. */ | |
8428 | ||
7afe21cc | 8429 | void |
8b3686ed | 8430 | cse_end_of_basic_block (insn, data, follow_jumps, after_loop, skip_blocks) |
7afe21cc RK |
8431 | rtx insn; |
8432 | struct cse_basic_block_data *data; | |
8433 | int follow_jumps; | |
8434 | int after_loop; | |
8b3686ed | 8435 | int skip_blocks; |
7afe21cc RK |
8436 | { |
8437 | rtx p = insn, q; | |
8438 | int nsets = 0; | |
8439 | int low_cuid = INSN_CUID (insn), high_cuid = INSN_CUID (insn); | |
fc3ffe83 | 8440 | rtx next = GET_RTX_CLASS (GET_CODE (insn)) == 'i' ? insn : next_real_insn (insn); |
7afe21cc RK |
8441 | int path_size = data->path_size; |
8442 | int path_entry = 0; | |
8443 | int i; | |
8444 | ||
8445 | /* Update the previous branch path, if any. If the last branch was | |
8446 | previously TAKEN, mark it NOT_TAKEN. If it was previously NOT_TAKEN, | |
8447 | shorten the path by one and look at the previous branch. We know that | |
8448 | at least one branch must have been taken if PATH_SIZE is non-zero. */ | |
8449 | while (path_size > 0) | |
8450 | { | |
8b3686ed | 8451 | if (data->path[path_size - 1].status != NOT_TAKEN) |
7afe21cc RK |
8452 | { |
8453 | data->path[path_size - 1].status = NOT_TAKEN; | |
8454 | break; | |
8455 | } | |
8456 | else | |
8457 | path_size--; | |
8458 | } | |
8459 | ||
8460 | /* Scan to end of this basic block. */ | |
8461 | while (p && GET_CODE (p) != CODE_LABEL) | |
8462 | { | |
8463 | /* Don't cse out the end of a loop. This makes a difference | |
8464 | only for the unusual loops that always execute at least once; | |
8465 | all other loops have labels there so we will stop in any case. | |
8466 | Cse'ing out the end of the loop is dangerous because it | |
8467 | might cause an invariant expression inside the loop | |
8468 | to be reused after the end of the loop. This would make it | |
8469 | hard to move the expression out of the loop in loop.c, | |
8470 | especially if it is one of several equivalent expressions | |
8471 | and loop.c would like to eliminate it. | |
8472 | ||
8473 | If we are running after loop.c has finished, we can ignore | |
8474 | the NOTE_INSN_LOOP_END. */ | |
8475 | ||
8476 | if (! after_loop && GET_CODE (p) == NOTE | |
8477 | && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END) | |
8478 | break; | |
8479 | ||
8480 | /* Don't cse over a call to setjmp; on some machines (eg vax) | |
8481 | the regs restored by the longjmp come from | |
8482 | a later time than the setjmp. */ | |
8483 | if (GET_CODE (p) == NOTE | |
8484 | && NOTE_LINE_NUMBER (p) == NOTE_INSN_SETJMP) | |
8485 | break; | |
8486 | ||
8487 | /* A PARALLEL can have lots of SETs in it, | |
8488 | especially if it is really an ASM_OPERANDS. */ | |
8489 | if (GET_RTX_CLASS (GET_CODE (p)) == 'i' | |
8490 | && GET_CODE (PATTERN (p)) == PARALLEL) | |
8491 | nsets += XVECLEN (PATTERN (p), 0); | |
8492 | else if (GET_CODE (p) != NOTE) | |
8493 | nsets += 1; | |
8494 | ||
164c8956 RK |
8495 | /* Ignore insns made by CSE; they cannot affect the boundaries of |
8496 | the basic block. */ | |
8497 | ||
8498 | if (INSN_UID (p) <= max_uid && INSN_CUID (p) > high_cuid) | |
8b3686ed | 8499 | high_cuid = INSN_CUID (p); |
164c8956 RK |
8500 | if (INSN_UID (p) <= max_uid && INSN_CUID (p) < low_cuid) |
8501 | low_cuid = INSN_CUID (p); | |
7afe21cc RK |
8502 | |
8503 | /* See if this insn is in our branch path. If it is and we are to | |
8504 | take it, do so. */ | |
8505 | if (path_entry < path_size && data->path[path_entry].branch == p) | |
8506 | { | |
8b3686ed | 8507 | if (data->path[path_entry].status != NOT_TAKEN) |
7afe21cc RK |
8508 | p = JUMP_LABEL (p); |
8509 | ||
8510 | /* Point to next entry in path, if any. */ | |
8511 | path_entry++; | |
8512 | } | |
8513 | ||
8514 | /* If this is a conditional jump, we can follow it if -fcse-follow-jumps | |
8515 | was specified, we haven't reached our maximum path length, there are | |
8516 | insns following the target of the jump, this is the only use of the | |
8b3686ed RK |
8517 | jump label, and the target label is preceded by a BARRIER. |
8518 | ||
8519 | Alternatively, we can follow the jump if it branches around a | |
8520 | block of code and there are no other branches into the block. | |
8521 | In this case invalidate_skipped_block will be called to invalidate any | |
8522 | registers set in the block when following the jump. */ | |
8523 | ||
8524 | else if ((follow_jumps || skip_blocks) && path_size < PATHLENGTH - 1 | |
7afe21cc RK |
8525 | && GET_CODE (p) == JUMP_INSN |
8526 | && GET_CODE (PATTERN (p)) == SET | |
8527 | && GET_CODE (SET_SRC (PATTERN (p))) == IF_THEN_ELSE | |
85c3ba60 | 8528 | && JUMP_LABEL (p) != 0 |
7afe21cc RK |
8529 | && LABEL_NUSES (JUMP_LABEL (p)) == 1 |
8530 | && NEXT_INSN (JUMP_LABEL (p)) != 0) | |
8531 | { | |
8532 | for (q = PREV_INSN (JUMP_LABEL (p)); q; q = PREV_INSN (q)) | |
8533 | if ((GET_CODE (q) != NOTE | |
8534 | || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END | |
8535 | || NOTE_LINE_NUMBER (q) == NOTE_INSN_SETJMP) | |
8536 | && (GET_CODE (q) != CODE_LABEL || LABEL_NUSES (q) != 0)) | |
8537 | break; | |
8538 | ||
8539 | /* If we ran into a BARRIER, this code is an extension of the | |
8540 | basic block when the branch is taken. */ | |
8b3686ed | 8541 | if (follow_jumps && q != 0 && GET_CODE (q) == BARRIER) |
7afe21cc RK |
8542 | { |
8543 | /* Don't allow ourself to keep walking around an | |
8544 | always-executed loop. */ | |
fc3ffe83 RK |
8545 | if (next_real_insn (q) == next) |
8546 | { | |
8547 | p = NEXT_INSN (p); | |
8548 | continue; | |
8549 | } | |
7afe21cc RK |
8550 | |
8551 | /* Similarly, don't put a branch in our path more than once. */ | |
8552 | for (i = 0; i < path_entry; i++) | |
8553 | if (data->path[i].branch == p) | |
8554 | break; | |
8555 | ||
8556 | if (i != path_entry) | |
8557 | break; | |
8558 | ||
8559 | data->path[path_entry].branch = p; | |
8560 | data->path[path_entry++].status = TAKEN; | |
8561 | ||
8562 | /* This branch now ends our path. It was possible that we | |
8563 | didn't see this branch the last time around (when the | |
8564 | insn in front of the target was a JUMP_INSN that was | |
8565 | turned into a no-op). */ | |
8566 | path_size = path_entry; | |
8567 | ||
8568 | p = JUMP_LABEL (p); | |
8569 | /* Mark block so we won't scan it again later. */ | |
8570 | PUT_MODE (NEXT_INSN (p), QImode); | |
8571 | } | |
8b3686ed RK |
8572 | /* Detect a branch around a block of code. */ |
8573 | else if (skip_blocks && q != 0 && GET_CODE (q) != CODE_LABEL) | |
8574 | { | |
8575 | register rtx tmp; | |
8576 | ||
fc3ffe83 RK |
8577 | if (next_real_insn (q) == next) |
8578 | { | |
8579 | p = NEXT_INSN (p); | |
8580 | continue; | |
8581 | } | |
8b3686ed RK |
8582 | |
8583 | for (i = 0; i < path_entry; i++) | |
8584 | if (data->path[i].branch == p) | |
8585 | break; | |
8586 | ||
8587 | if (i != path_entry) | |
8588 | break; | |
8589 | ||
8590 | /* This is no_labels_between_p (p, q) with an added check for | |
8591 | reaching the end of a function (in case Q precedes P). */ | |
8592 | for (tmp = NEXT_INSN (p); tmp && tmp != q; tmp = NEXT_INSN (tmp)) | |
8593 | if (GET_CODE (tmp) == CODE_LABEL) | |
8594 | break; | |
8595 | ||
8596 | if (tmp == q) | |
8597 | { | |
8598 | data->path[path_entry].branch = p; | |
8599 | data->path[path_entry++].status = AROUND; | |
8600 | ||
8601 | path_size = path_entry; | |
8602 | ||
8603 | p = JUMP_LABEL (p); | |
8604 | /* Mark block so we won't scan it again later. */ | |
8605 | PUT_MODE (NEXT_INSN (p), QImode); | |
8606 | } | |
8607 | } | |
7afe21cc | 8608 | } |
7afe21cc RK |
8609 | p = NEXT_INSN (p); |
8610 | } | |
8611 | ||
8612 | data->low_cuid = low_cuid; | |
8613 | data->high_cuid = high_cuid; | |
8614 | data->nsets = nsets; | |
8615 | data->last = p; | |
8616 | ||
8617 | /* If all jumps in the path are not taken, set our path length to zero | |
8618 | so a rescan won't be done. */ | |
8619 | for (i = path_size - 1; i >= 0; i--) | |
8b3686ed | 8620 | if (data->path[i].status != NOT_TAKEN) |
7afe21cc RK |
8621 | break; |
8622 | ||
8623 | if (i == -1) | |
8624 | data->path_size = 0; | |
8625 | else | |
8626 | data->path_size = path_size; | |
8627 | ||
8628 | /* End the current branch path. */ | |
8629 | data->path[path_size].branch = 0; | |
8630 | } | |
8631 | \f | |
7afe21cc RK |
8632 | /* Perform cse on the instructions of a function. |
8633 | F is the first instruction. | |
8634 | NREGS is one plus the highest pseudo-reg number used in the instruction. | |
8635 | ||
8636 | AFTER_LOOP is 1 if this is the cse call done after loop optimization | |
8637 | (only if -frerun-cse-after-loop). | |
8638 | ||
8639 | Returns 1 if jump_optimize should be redone due to simplifications | |
8640 | in conditional jump instructions. */ | |
8641 | ||
8642 | int | |
8643 | cse_main (f, nregs, after_loop, file) | |
8644 | rtx f; | |
8645 | int nregs; | |
8646 | int after_loop; | |
8647 | FILE *file; | |
8648 | { | |
8649 | struct cse_basic_block_data val; | |
8650 | register rtx insn = f; | |
8651 | register int i; | |
8652 | ||
8653 | cse_jumps_altered = 0; | |
a5dfb4ee | 8654 | recorded_label_ref = 0; |
7afe21cc RK |
8655 | constant_pool_entries_cost = 0; |
8656 | val.path_size = 0; | |
8657 | ||
8658 | init_recog (); | |
9ae8ffe7 | 8659 | init_alias_analysis (); |
7afe21cc RK |
8660 | |
8661 | max_reg = nregs; | |
8662 | ||
556c714b JW |
8663 | max_insn_uid = get_max_uid (); |
8664 | ||
7afe21cc RK |
8665 | reg_next_eqv = (int *) alloca (nregs * sizeof (int)); |
8666 | reg_prev_eqv = (int *) alloca (nregs * sizeof (int)); | |
7afe21cc | 8667 | |
7bac1be0 RK |
8668 | #ifdef LOAD_EXTEND_OP |
8669 | ||
8670 | /* Allocate scratch rtl here. cse_insn will fill in the memory reference | |
8671 | and change the code and mode as appropriate. */ | |
38a448ca | 8672 | memory_extend_rtx = gen_rtx_ZERO_EXTEND (VOIDmode, NULL_RTX); |
7bac1be0 RK |
8673 | #endif |
8674 | ||
7afe21cc RK |
8675 | /* Discard all the free elements of the previous function |
8676 | since they are allocated in the temporarily obstack. */ | |
4c9a05bc | 8677 | bzero ((char *) table, sizeof table); |
7afe21cc RK |
8678 | free_element_chain = 0; |
8679 | n_elements_made = 0; | |
8680 | ||
8681 | /* Find the largest uid. */ | |
8682 | ||
164c8956 RK |
8683 | max_uid = get_max_uid (); |
8684 | uid_cuid = (int *) alloca ((max_uid + 1) * sizeof (int)); | |
4c9a05bc | 8685 | bzero ((char *) uid_cuid, (max_uid + 1) * sizeof (int)); |
7afe21cc RK |
8686 | |
8687 | /* Compute the mapping from uids to cuids. | |
8688 | CUIDs are numbers assigned to insns, like uids, | |
8689 | except that cuids increase monotonically through the code. | |
8690 | Don't assign cuids to line-number NOTEs, so that the distance in cuids | |
8691 | between two insns is not affected by -g. */ | |
8692 | ||
8693 | for (insn = f, i = 0; insn; insn = NEXT_INSN (insn)) | |
8694 | { | |
8695 | if (GET_CODE (insn) != NOTE | |
8696 | || NOTE_LINE_NUMBER (insn) < 0) | |
8697 | INSN_CUID (insn) = ++i; | |
8698 | else | |
8699 | /* Give a line number note the same cuid as preceding insn. */ | |
8700 | INSN_CUID (insn) = i; | |
8701 | } | |
8702 | ||
8703 | /* Initialize which registers are clobbered by calls. */ | |
8704 | ||
8705 | CLEAR_HARD_REG_SET (regs_invalidated_by_call); | |
8706 | ||
8707 | for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) | |
8708 | if ((call_used_regs[i] | |
8709 | /* Used to check !fixed_regs[i] here, but that isn't safe; | |
8710 | fixed regs are still call-clobbered, and sched can get | |
8711 | confused if they can "live across calls". | |
8712 | ||
8713 | The frame pointer is always preserved across calls. The arg | |
8714 | pointer is if it is fixed. The stack pointer usually is, unless | |
8715 | RETURN_POPS_ARGS, in which case an explicit CLOBBER | |
8716 | will be present. If we are generating PIC code, the PIC offset | |
8717 | table register is preserved across calls. */ | |
8718 | ||
8719 | && i != STACK_POINTER_REGNUM | |
8720 | && i != FRAME_POINTER_REGNUM | |
8bc169f2 DE |
8721 | #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM |
8722 | && i != HARD_FRAME_POINTER_REGNUM | |
8723 | #endif | |
7afe21cc RK |
8724 | #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM |
8725 | && ! (i == ARG_POINTER_REGNUM && fixed_regs[i]) | |
8726 | #endif | |
be8fe470 | 8727 | #if defined (PIC_OFFSET_TABLE_REGNUM) && !defined (PIC_OFFSET_TABLE_REG_CALL_CLOBBERED) |
7afe21cc RK |
8728 | && ! (i == PIC_OFFSET_TABLE_REGNUM && flag_pic) |
8729 | #endif | |
8730 | ) | |
8731 | || global_regs[i]) | |
8732 | SET_HARD_REG_BIT (regs_invalidated_by_call, i); | |
8733 | ||
1497faf6 RH |
8734 | if (ggc_p) |
8735 | ggc_push_context (); | |
8736 | ||
7afe21cc RK |
8737 | /* Loop over basic blocks. |
8738 | Compute the maximum number of qty's needed for each basic block | |
8739 | (which is 2 for each SET). */ | |
8740 | insn = f; | |
8741 | while (insn) | |
8742 | { | |
8b3686ed RK |
8743 | cse_end_of_basic_block (insn, &val, flag_cse_follow_jumps, after_loop, |
8744 | flag_cse_skip_blocks); | |
7afe21cc RK |
8745 | |
8746 | /* If this basic block was already processed or has no sets, skip it. */ | |
8747 | if (val.nsets == 0 || GET_MODE (insn) == QImode) | |
8748 | { | |
8749 | PUT_MODE (insn, VOIDmode); | |
8750 | insn = (val.last ? NEXT_INSN (val.last) : 0); | |
8751 | val.path_size = 0; | |
8752 | continue; | |
8753 | } | |
8754 | ||
8755 | cse_basic_block_start = val.low_cuid; | |
8756 | cse_basic_block_end = val.high_cuid; | |
8757 | max_qty = val.nsets * 2; | |
8758 | ||
8759 | if (file) | |
ab87f8c8 | 8760 | fnotice (file, ";; Processing block from %d to %d, %d sets.\n", |
7afe21cc RK |
8761 | INSN_UID (insn), val.last ? INSN_UID (val.last) : 0, |
8762 | val.nsets); | |
8763 | ||
8764 | /* Make MAX_QTY bigger to give us room to optimize | |
8765 | past the end of this basic block, if that should prove useful. */ | |
8766 | if (max_qty < 500) | |
8767 | max_qty = 500; | |
8768 | ||
8769 | max_qty += max_reg; | |
8770 | ||
8771 | /* If this basic block is being extended by following certain jumps, | |
8772 | (see `cse_end_of_basic_block'), we reprocess the code from the start. | |
8773 | Otherwise, we start after this basic block. */ | |
8774 | if (val.path_size > 0) | |
8775 | cse_basic_block (insn, val.last, val.path, 0); | |
8776 | else | |
8777 | { | |
8778 | int old_cse_jumps_altered = cse_jumps_altered; | |
8779 | rtx temp; | |
8780 | ||
8781 | /* When cse changes a conditional jump to an unconditional | |
8782 | jump, we want to reprocess the block, since it will give | |
8783 | us a new branch path to investigate. */ | |
8784 | cse_jumps_altered = 0; | |
8785 | temp = cse_basic_block (insn, val.last, val.path, ! after_loop); | |
8b3686ed RK |
8786 | if (cse_jumps_altered == 0 |
8787 | || (flag_cse_follow_jumps == 0 && flag_cse_skip_blocks == 0)) | |
7afe21cc RK |
8788 | insn = temp; |
8789 | ||
8790 | cse_jumps_altered |= old_cse_jumps_altered; | |
8791 | } | |
8792 | ||
1497faf6 RH |
8793 | if (ggc_p) |
8794 | ggc_collect (); | |
8795 | ||
7afe21cc RK |
8796 | #ifdef USE_C_ALLOCA |
8797 | alloca (0); | |
8798 | #endif | |
8799 | } | |
8800 | ||
1497faf6 RH |
8801 | if (ggc_p) |
8802 | ggc_pop_context (); | |
8803 | ||
7afe21cc RK |
8804 | /* Tell refers_to_mem_p that qty_const info is not available. */ |
8805 | qty_const = 0; | |
8806 | ||
8807 | if (max_elements_made < n_elements_made) | |
8808 | max_elements_made = n_elements_made; | |
8809 | ||
a5dfb4ee | 8810 | return cse_jumps_altered || recorded_label_ref; |
7afe21cc RK |
8811 | } |
8812 | ||
8813 | /* Process a single basic block. FROM and TO and the limits of the basic | |
8814 | block. NEXT_BRANCH points to the branch path when following jumps or | |
8815 | a null path when not following jumps. | |
8816 | ||
8817 | AROUND_LOOP is non-zero if we are to try to cse around to the start of a | |
8818 | loop. This is true when we are being called for the last time on a | |
8819 | block and this CSE pass is before loop.c. */ | |
8820 | ||
8821 | static rtx | |
8822 | cse_basic_block (from, to, next_branch, around_loop) | |
8823 | register rtx from, to; | |
8824 | struct branch_path *next_branch; | |
8825 | int around_loop; | |
8826 | { | |
8827 | register rtx insn; | |
8828 | int to_usage = 0; | |
7bd8b2a8 | 8829 | rtx libcall_insn = NULL_RTX; |
e9a25f70 | 8830 | int num_insns = 0; |
7afe21cc RK |
8831 | |
8832 | /* Each of these arrays is undefined before max_reg, so only allocate | |
8833 | the space actually needed and adjust the start below. */ | |
8834 | ||
8835 | qty_first_reg = (int *) alloca ((max_qty - max_reg) * sizeof (int)); | |
8836 | qty_last_reg = (int *) alloca ((max_qty - max_reg) * sizeof (int)); | |
c5c76735 JL |
8837 | qty_mode = (enum machine_mode *) alloca ((max_qty - max_reg) |
8838 | * sizeof (enum machine_mode)); | |
7afe21cc RK |
8839 | qty_const = (rtx *) alloca ((max_qty - max_reg) * sizeof (rtx)); |
8840 | qty_const_insn = (rtx *) alloca ((max_qty - max_reg) * sizeof (rtx)); | |
8841 | qty_comparison_code | |
8842 | = (enum rtx_code *) alloca ((max_qty - max_reg) * sizeof (enum rtx_code)); | |
8843 | qty_comparison_qty = (int *) alloca ((max_qty - max_reg) * sizeof (int)); | |
8844 | qty_comparison_const = (rtx *) alloca ((max_qty - max_reg) * sizeof (rtx)); | |
8845 | ||
8846 | qty_first_reg -= max_reg; | |
8847 | qty_last_reg -= max_reg; | |
8848 | qty_mode -= max_reg; | |
8849 | qty_const -= max_reg; | |
8850 | qty_const_insn -= max_reg; | |
8851 | qty_comparison_code -= max_reg; | |
8852 | qty_comparison_qty -= max_reg; | |
8853 | qty_comparison_const -= max_reg; | |
8854 | ||
8855 | new_basic_block (); | |
8856 | ||
8857 | /* TO might be a label. If so, protect it from being deleted. */ | |
8858 | if (to != 0 && GET_CODE (to) == CODE_LABEL) | |
8859 | ++LABEL_NUSES (to); | |
8860 | ||
8861 | for (insn = from; insn != to; insn = NEXT_INSN (insn)) | |
8862 | { | |
1d22a2c1 | 8863 | register enum rtx_code code = GET_CODE (insn); |
e9a25f70 | 8864 | |
1d22a2c1 MM |
8865 | /* If we have processed 1,000 insns, flush the hash table to |
8866 | avoid extreme quadratic behavior. We must not include NOTEs | |
8867 | in the count since there may be more or them when generating | |
8868 | debugging information. If we clear the table at different | |
8869 | times, code generated with -g -O might be different than code | |
8870 | generated with -O but not -g. | |
e9a25f70 JL |
8871 | |
8872 | ??? This is a real kludge and needs to be done some other way. | |
8873 | Perhaps for 2.9. */ | |
1d22a2c1 | 8874 | if (code != NOTE && num_insns++ > 1000) |
e9a25f70 | 8875 | { |
01e752d3 | 8876 | flush_hash_table (); |
e9a25f70 JL |
8877 | num_insns = 0; |
8878 | } | |
7afe21cc RK |
8879 | |
8880 | /* See if this is a branch that is part of the path. If so, and it is | |
8881 | to be taken, do so. */ | |
8882 | if (next_branch->branch == insn) | |
8883 | { | |
8b3686ed RK |
8884 | enum taken status = next_branch++->status; |
8885 | if (status != NOT_TAKEN) | |
7afe21cc | 8886 | { |
8b3686ed RK |
8887 | if (status == TAKEN) |
8888 | record_jump_equiv (insn, 1); | |
8889 | else | |
8890 | invalidate_skipped_block (NEXT_INSN (insn)); | |
8891 | ||
7afe21cc RK |
8892 | /* Set the last insn as the jump insn; it doesn't affect cc0. |
8893 | Then follow this branch. */ | |
8894 | #ifdef HAVE_cc0 | |
8895 | prev_insn_cc0 = 0; | |
8896 | #endif | |
8897 | prev_insn = insn; | |
8898 | insn = JUMP_LABEL (insn); | |
8899 | continue; | |
8900 | } | |
8901 | } | |
8902 | ||
7afe21cc RK |
8903 | if (GET_MODE (insn) == QImode) |
8904 | PUT_MODE (insn, VOIDmode); | |
8905 | ||
8906 | if (GET_RTX_CLASS (code) == 'i') | |
8907 | { | |
7bd8b2a8 JL |
8908 | rtx p; |
8909 | ||
7afe21cc RK |
8910 | /* Process notes first so we have all notes in canonical forms when |
8911 | looking for duplicate operations. */ | |
8912 | ||
8913 | if (REG_NOTES (insn)) | |
906c4e36 | 8914 | REG_NOTES (insn) = cse_process_notes (REG_NOTES (insn), NULL_RTX); |
7afe21cc RK |
8915 | |
8916 | /* Track when we are inside in LIBCALL block. Inside such a block, | |
8917 | we do not want to record destinations. The last insn of a | |
8918 | LIBCALL block is not considered to be part of the block, since | |
830a38ee | 8919 | its destination is the result of the block and hence should be |
7afe21cc RK |
8920 | recorded. */ |
8921 | ||
63be02db | 8922 | if ((p = find_reg_note (insn, REG_LIBCALL, NULL_RTX))) |
7bd8b2a8 | 8923 | libcall_insn = XEXP (p, 0); |
906c4e36 | 8924 | else if (find_reg_note (insn, REG_RETVAL, NULL_RTX)) |
7bd8b2a8 | 8925 | libcall_insn = NULL_RTX; |
7afe21cc | 8926 | |
7bd8b2a8 | 8927 | cse_insn (insn, libcall_insn); |
7afe21cc RK |
8928 | } |
8929 | ||
8930 | /* If INSN is now an unconditional jump, skip to the end of our | |
8931 | basic block by pretending that we just did the last insn in the | |
8932 | basic block. If we are jumping to the end of our block, show | |
8933 | that we can have one usage of TO. */ | |
8934 | ||
8935 | if (simplejump_p (insn)) | |
8936 | { | |
8937 | if (to == 0) | |
8938 | return 0; | |
8939 | ||
8940 | if (JUMP_LABEL (insn) == to) | |
8941 | to_usage = 1; | |
8942 | ||
6a5293dc RS |
8943 | /* Maybe TO was deleted because the jump is unconditional. |
8944 | If so, there is nothing left in this basic block. */ | |
8945 | /* ??? Perhaps it would be smarter to set TO | |
8946 | to whatever follows this insn, | |
8947 | and pretend the basic block had always ended here. */ | |
8948 | if (INSN_DELETED_P (to)) | |
8949 | break; | |
8950 | ||
7afe21cc RK |
8951 | insn = PREV_INSN (to); |
8952 | } | |
8953 | ||
8954 | /* See if it is ok to keep on going past the label | |
8955 | which used to end our basic block. Remember that we incremented | |
d45cf215 | 8956 | the count of that label, so we decrement it here. If we made |
7afe21cc RK |
8957 | a jump unconditional, TO_USAGE will be one; in that case, we don't |
8958 | want to count the use in that jump. */ | |
8959 | ||
8960 | if (to != 0 && NEXT_INSN (insn) == to | |
8961 | && GET_CODE (to) == CODE_LABEL && --LABEL_NUSES (to) == to_usage) | |
8962 | { | |
8963 | struct cse_basic_block_data val; | |
146135d6 | 8964 | rtx prev; |
7afe21cc RK |
8965 | |
8966 | insn = NEXT_INSN (to); | |
8967 | ||
8968 | if (LABEL_NUSES (to) == 0) | |
146135d6 | 8969 | insn = delete_insn (to); |
7afe21cc | 8970 | |
146135d6 RK |
8971 | /* If TO was the last insn in the function, we are done. */ |
8972 | if (insn == 0) | |
7afe21cc RK |
8973 | return 0; |
8974 | ||
146135d6 RK |
8975 | /* If TO was preceded by a BARRIER we are done with this block |
8976 | because it has no continuation. */ | |
8977 | prev = prev_nonnote_insn (to); | |
8978 | if (prev && GET_CODE (prev) == BARRIER) | |
8979 | return insn; | |
8980 | ||
8981 | /* Find the end of the following block. Note that we won't be | |
8982 | following branches in this case. */ | |
7afe21cc RK |
8983 | to_usage = 0; |
8984 | val.path_size = 0; | |
8b3686ed | 8985 | cse_end_of_basic_block (insn, &val, 0, 0, 0); |
7afe21cc RK |
8986 | |
8987 | /* If the tables we allocated have enough space left | |
8988 | to handle all the SETs in the next basic block, | |
8989 | continue through it. Otherwise, return, | |
8990 | and that block will be scanned individually. */ | |
8991 | if (val.nsets * 2 + next_qty > max_qty) | |
8992 | break; | |
8993 | ||
8994 | cse_basic_block_start = val.low_cuid; | |
8995 | cse_basic_block_end = val.high_cuid; | |
8996 | to = val.last; | |
8997 | ||
8998 | /* Prevent TO from being deleted if it is a label. */ | |
8999 | if (to != 0 && GET_CODE (to) == CODE_LABEL) | |
9000 | ++LABEL_NUSES (to); | |
9001 | ||
9002 | /* Back up so we process the first insn in the extension. */ | |
9003 | insn = PREV_INSN (insn); | |
9004 | } | |
9005 | } | |
9006 | ||
9007 | if (next_qty > max_qty) | |
9008 | abort (); | |
9009 | ||
9010 | /* If we are running before loop.c, we stopped on a NOTE_INSN_LOOP_END, and | |
9011 | the previous insn is the only insn that branches to the head of a loop, | |
9012 | we can cse into the loop. Don't do this if we changed the jump | |
9013 | structure of a loop unless we aren't going to be following jumps. */ | |
9014 | ||
8b3686ed RK |
9015 | if ((cse_jumps_altered == 0 |
9016 | || (flag_cse_follow_jumps == 0 && flag_cse_skip_blocks == 0)) | |
7afe21cc RK |
9017 | && around_loop && to != 0 |
9018 | && GET_CODE (to) == NOTE && NOTE_LINE_NUMBER (to) == NOTE_INSN_LOOP_END | |
9019 | && GET_CODE (PREV_INSN (to)) == JUMP_INSN | |
9020 | && JUMP_LABEL (PREV_INSN (to)) != 0 | |
9021 | && LABEL_NUSES (JUMP_LABEL (PREV_INSN (to))) == 1) | |
9022 | cse_around_loop (JUMP_LABEL (PREV_INSN (to))); | |
9023 | ||
9024 | return to ? NEXT_INSN (to) : 0; | |
9025 | } | |
9026 | \f | |
9027 | /* Count the number of times registers are used (not set) in X. | |
9028 | COUNTS is an array in which we accumulate the count, INCR is how much | |
79644f06 RK |
9029 | we count each register usage. |
9030 | ||
9031 | Don't count a usage of DEST, which is the SET_DEST of a SET which | |
9032 | contains X in its SET_SRC. This is because such a SET does not | |
9033 | modify the liveness of DEST. */ | |
7afe21cc RK |
9034 | |
9035 | static void | |
79644f06 | 9036 | count_reg_usage (x, counts, dest, incr) |
7afe21cc RK |
9037 | rtx x; |
9038 | int *counts; | |
79644f06 | 9039 | rtx dest; |
7afe21cc RK |
9040 | int incr; |
9041 | { | |
f1e7c95f | 9042 | enum rtx_code code; |
6f7d635c | 9043 | const char *fmt; |
7afe21cc RK |
9044 | int i, j; |
9045 | ||
f1e7c95f RK |
9046 | if (x == 0) |
9047 | return; | |
9048 | ||
9049 | switch (code = GET_CODE (x)) | |
7afe21cc RK |
9050 | { |
9051 | case REG: | |
79644f06 RK |
9052 | if (x != dest) |
9053 | counts[REGNO (x)] += incr; | |
7afe21cc RK |
9054 | return; |
9055 | ||
9056 | case PC: | |
9057 | case CC0: | |
9058 | case CONST: | |
9059 | case CONST_INT: | |
9060 | case CONST_DOUBLE: | |
9061 | case SYMBOL_REF: | |
9062 | case LABEL_REF: | |
02e39abc JL |
9063 | return; |
9064 | ||
9065 | case CLOBBER: | |
9066 | /* If we are clobbering a MEM, mark any registers inside the address | |
9067 | as being used. */ | |
9068 | if (GET_CODE (XEXP (x, 0)) == MEM) | |
9069 | count_reg_usage (XEXP (XEXP (x, 0), 0), counts, NULL_RTX, incr); | |
7afe21cc RK |
9070 | return; |
9071 | ||
9072 | case SET: | |
9073 | /* Unless we are setting a REG, count everything in SET_DEST. */ | |
9074 | if (GET_CODE (SET_DEST (x)) != REG) | |
79644f06 | 9075 | count_reg_usage (SET_DEST (x), counts, NULL_RTX, incr); |
9ff08f70 RK |
9076 | |
9077 | /* If SRC has side-effects, then we can't delete this insn, so the | |
9078 | usage of SET_DEST inside SRC counts. | |
9079 | ||
9080 | ??? Strictly-speaking, we might be preserving this insn | |
9081 | because some other SET has side-effects, but that's hard | |
9082 | to do and can't happen now. */ | |
9083 | count_reg_usage (SET_SRC (x), counts, | |
9084 | side_effects_p (SET_SRC (x)) ? NULL_RTX : SET_DEST (x), | |
9085 | incr); | |
7afe21cc RK |
9086 | return; |
9087 | ||
f1e7c95f RK |
9088 | case CALL_INSN: |
9089 | count_reg_usage (CALL_INSN_FUNCTION_USAGE (x), counts, NULL_RTX, incr); | |
9090 | ||
9091 | /* ... falls through ... */ | |
7afe21cc RK |
9092 | case INSN: |
9093 | case JUMP_INSN: | |
79644f06 | 9094 | count_reg_usage (PATTERN (x), counts, NULL_RTX, incr); |
7afe21cc RK |
9095 | |
9096 | /* Things used in a REG_EQUAL note aren't dead since loop may try to | |
9097 | use them. */ | |
9098 | ||
f1e7c95f | 9099 | count_reg_usage (REG_NOTES (x), counts, NULL_RTX, incr); |
7afe21cc RK |
9100 | return; |
9101 | ||
9102 | case EXPR_LIST: | |
9103 | case INSN_LIST: | |
f1e7c95f | 9104 | if (REG_NOTE_KIND (x) == REG_EQUAL |
c6a26dc4 | 9105 | || (REG_NOTE_KIND (x) != REG_NONNEG && GET_CODE (XEXP (x,0)) == USE)) |
79644f06 | 9106 | count_reg_usage (XEXP (x, 0), counts, NULL_RTX, incr); |
f1e7c95f | 9107 | count_reg_usage (XEXP (x, 1), counts, NULL_RTX, incr); |
7afe21cc | 9108 | return; |
e9a25f70 JL |
9109 | |
9110 | default: | |
9111 | break; | |
7afe21cc RK |
9112 | } |
9113 | ||
9114 | fmt = GET_RTX_FORMAT (code); | |
9115 | for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) | |
9116 | { | |
9117 | if (fmt[i] == 'e') | |
79644f06 | 9118 | count_reg_usage (XEXP (x, i), counts, dest, incr); |
7afe21cc RK |
9119 | else if (fmt[i] == 'E') |
9120 | for (j = XVECLEN (x, i) - 1; j >= 0; j--) | |
79644f06 | 9121 | count_reg_usage (XVECEXP (x, i, j), counts, dest, incr); |
7afe21cc RK |
9122 | } |
9123 | } | |
9124 | \f | |
9125 | /* Scan all the insns and delete any that are dead; i.e., they store a register | |
9126 | that is never used or they copy a register to itself. | |
9127 | ||
c6a26dc4 JL |
9128 | This is used to remove insns made obviously dead by cse, loop or other |
9129 | optimizations. It improves the heuristics in loop since it won't try to | |
9130 | move dead invariants out of loops or make givs for dead quantities. The | |
9131 | remaining passes of the compilation are also sped up. */ | |
7afe21cc RK |
9132 | |
9133 | void | |
c6a26dc4 | 9134 | delete_trivially_dead_insns (insns, nreg) |
7afe21cc RK |
9135 | rtx insns; |
9136 | int nreg; | |
9137 | { | |
9138 | int *counts = (int *) alloca (nreg * sizeof (int)); | |
77fa0940 | 9139 | rtx insn, prev; |
51723711 | 9140 | #ifdef HAVE_cc0 |
d45cf215 | 9141 | rtx tem; |
51723711 | 9142 | #endif |
7afe21cc | 9143 | int i; |
614bb5d4 | 9144 | int in_libcall = 0, dead_libcall = 0; |
7afe21cc RK |
9145 | |
9146 | /* First count the number of times each register is used. */ | |
4c9a05bc | 9147 | bzero ((char *) counts, sizeof (int) * nreg); |
7afe21cc | 9148 | for (insn = next_real_insn (insns); insn; insn = next_real_insn (insn)) |
79644f06 | 9149 | count_reg_usage (insn, counts, NULL_RTX, 1); |
7afe21cc RK |
9150 | |
9151 | /* Go from the last insn to the first and delete insns that only set unused | |
9152 | registers or copy a register to itself. As we delete an insn, remove | |
8d71a510 JL |
9153 | usage counts for registers it uses. |
9154 | ||
9155 | The first jump optimization pass may leave a real insn as the last | |
9156 | insn in the function. We must not skip that insn or we may end | |
9157 | up deleting code that is not really dead. */ | |
9158 | insn = get_last_insn (); | |
9159 | if (GET_RTX_CLASS (GET_CODE (insn)) != 'i') | |
9160 | insn = prev_real_insn (insn); | |
9161 | ||
9162 | for ( ; insn; insn = prev) | |
7afe21cc RK |
9163 | { |
9164 | int live_insn = 0; | |
614bb5d4 | 9165 | rtx note; |
7afe21cc | 9166 | |
77fa0940 RK |
9167 | prev = prev_real_insn (insn); |
9168 | ||
614bb5d4 JL |
9169 | /* Don't delete any insns that are part of a libcall block unless |
9170 | we can delete the whole libcall block. | |
9171 | ||
77fa0940 RK |
9172 | Flow or loop might get confused if we did that. Remember |
9173 | that we are scanning backwards. */ | |
906c4e36 | 9174 | if (find_reg_note (insn, REG_RETVAL, NULL_RTX)) |
614bb5d4 JL |
9175 | { |
9176 | in_libcall = 1; | |
9177 | live_insn = 1; | |
9178 | dead_libcall = 0; | |
e4890d45 | 9179 | |
614bb5d4 JL |
9180 | /* See if there's a REG_EQUAL note on this insn and try to |
9181 | replace the source with the REG_EQUAL expression. | |
9182 | ||
9183 | We assume that insns with REG_RETVALs can only be reg->reg | |
9184 | copies at this point. */ | |
9185 | note = find_reg_note (insn, REG_EQUAL, NULL_RTX); | |
9186 | if (note) | |
9187 | { | |
9188 | rtx set = single_set (insn); | |
9189 | if (set | |
9190 | && validate_change (insn, &SET_SRC (set), XEXP (note, 0), 0)) | |
9191 | { | |
9192 | remove_note (insn, | |
9193 | find_reg_note (insn, REG_RETVAL, NULL_RTX)); | |
9194 | dead_libcall = 1; | |
9195 | } | |
9196 | } | |
9197 | } | |
9198 | else if (in_libcall) | |
9199 | live_insn = ! dead_libcall; | |
e4890d45 | 9200 | else if (GET_CODE (PATTERN (insn)) == SET) |
7afe21cc RK |
9201 | { |
9202 | if (GET_CODE (SET_DEST (PATTERN (insn))) == REG | |
9203 | && SET_DEST (PATTERN (insn)) == SET_SRC (PATTERN (insn))) | |
9204 | ; | |
9205 | ||
d45cf215 RS |
9206 | #ifdef HAVE_cc0 |
9207 | else if (GET_CODE (SET_DEST (PATTERN (insn))) == CC0 | |
9208 | && ! side_effects_p (SET_SRC (PATTERN (insn))) | |
9209 | && ((tem = next_nonnote_insn (insn)) == 0 | |
9210 | || GET_RTX_CLASS (GET_CODE (tem)) != 'i' | |
9211 | || ! reg_referenced_p (cc0_rtx, PATTERN (tem)))) | |
9212 | ; | |
9213 | #endif | |
7afe21cc RK |
9214 | else if (GET_CODE (SET_DEST (PATTERN (insn))) != REG |
9215 | || REGNO (SET_DEST (PATTERN (insn))) < FIRST_PSEUDO_REGISTER | |
9216 | || counts[REGNO (SET_DEST (PATTERN (insn)))] != 0 | |
61c48fbf JL |
9217 | || side_effects_p (SET_SRC (PATTERN (insn))) |
9218 | /* An ADDRESSOF expression can turn into a use of the | |
9219 | internal arg pointer, so always consider the | |
9220 | internal arg pointer live. If it is truly dead, | |
9221 | flow will delete the initializing insn. */ | |
9222 | || (SET_DEST (PATTERN (insn)) | |
9223 | == current_function_internal_arg_pointer)) | |
7afe21cc RK |
9224 | live_insn = 1; |
9225 | } | |
9226 | else if (GET_CODE (PATTERN (insn)) == PARALLEL) | |
9227 | for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--) | |
9228 | { | |
9229 | rtx elt = XVECEXP (PATTERN (insn), 0, i); | |
9230 | ||
9231 | if (GET_CODE (elt) == SET) | |
9232 | { | |
9233 | if (GET_CODE (SET_DEST (elt)) == REG | |
9234 | && SET_DEST (elt) == SET_SRC (elt)) | |
9235 | ; | |
9236 | ||
d45cf215 RS |
9237 | #ifdef HAVE_cc0 |
9238 | else if (GET_CODE (SET_DEST (elt)) == CC0 | |
9239 | && ! side_effects_p (SET_SRC (elt)) | |
9240 | && ((tem = next_nonnote_insn (insn)) == 0 | |
9241 | || GET_RTX_CLASS (GET_CODE (tem)) != 'i' | |
9242 | || ! reg_referenced_p (cc0_rtx, PATTERN (tem)))) | |
9243 | ; | |
9244 | #endif | |
7afe21cc RK |
9245 | else if (GET_CODE (SET_DEST (elt)) != REG |
9246 | || REGNO (SET_DEST (elt)) < FIRST_PSEUDO_REGISTER | |
9247 | || counts[REGNO (SET_DEST (elt))] != 0 | |
af37f0dd JL |
9248 | || side_effects_p (SET_SRC (elt)) |
9249 | /* An ADDRESSOF expression can turn into a use of the | |
9250 | internal arg pointer, so always consider the | |
9251 | internal arg pointer live. If it is truly dead, | |
9252 | flow will delete the initializing insn. */ | |
9253 | || (SET_DEST (elt) | |
9254 | == current_function_internal_arg_pointer)) | |
7afe21cc RK |
9255 | live_insn = 1; |
9256 | } | |
9257 | else if (GET_CODE (elt) != CLOBBER && GET_CODE (elt) != USE) | |
9258 | live_insn = 1; | |
9259 | } | |
9260 | else | |
9261 | live_insn = 1; | |
9262 | ||
9263 | /* If this is a dead insn, delete it and show registers in it aren't | |
e4890d45 | 9264 | being used. */ |
7afe21cc | 9265 | |
e4890d45 | 9266 | if (! live_insn) |
7afe21cc | 9267 | { |
79644f06 | 9268 | count_reg_usage (insn, counts, NULL_RTX, -1); |
77fa0940 | 9269 | delete_insn (insn); |
7afe21cc | 9270 | } |
e4890d45 | 9271 | |
906c4e36 | 9272 | if (find_reg_note (insn, REG_LIBCALL, NULL_RTX)) |
614bb5d4 JL |
9273 | { |
9274 | in_libcall = 0; | |
9275 | dead_libcall = 0; | |
9276 | } | |
7afe21cc RK |
9277 | } |
9278 | } |