Debian Codegraph @

gcc/ira.cc

raw
1 /* Integrated Register Allocator (IRA) entry point.
2 Copyright (C) 2006-2025 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <[email protected]>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 /* The integrated register allocator (IRA) is a
22 regional register allocator performing graph coloring on a top-down
23 traversal of nested regions. Graph coloring in a region is based
24 on Chaitin-Briggs algorithm. It is called integrated because
25 register coalescing, register live range splitting, and choosing a
26 better hard register are done on-the-fly during coloring. Register
27 coalescing and choosing a cheaper hard register is done by hard
28 register preferencing during hard register assigning. The live
29 range splitting is a byproduct of the regional register allocation.
30
31 Major IRA notions are:
32
33 o *Region* is a part of CFG where graph coloring based on
34 Chaitin-Briggs algorithm is done. IRA can work on any set of
35 nested CFG regions forming a tree. Currently the regions are
36 the entire function for the root region and natural loops for
37 the other regions. Therefore data structure representing a
38 region is called loop_tree_node.
39
40 o *Allocno class* is a register class used for allocation of
41 given allocno. It means that only hard register of given
42 register class can be assigned to given allocno. In reality,
43 even smaller subset of (*profitable*) hard registers can be
44 assigned. In rare cases, the subset can be even smaller
45 because our modification of Chaitin-Briggs algorithm requires
46 that sets of hard registers can be assigned to allocnos forms a
47 forest, i.e. the sets can be ordered in a way where any
48 previous set is not intersected with given set or is a superset
49 of given set.
50
51 o *Pressure class* is a register class belonging to a set of
52 register classes containing all of the hard-registers available
53 for register allocation. The set of all pressure classes for a
54 target is defined in the corresponding machine-description file
55 according some criteria. Register pressure is calculated only
56 for pressure classes and it affects some IRA decisions as
57 forming allocation regions.
58
59 o *Allocno* represents the live range of a pseudo-register in a
60 region. Besides the obvious attributes like the corresponding
61 pseudo-register number, allocno class, conflicting allocnos and
62 conflicting hard-registers, there are a few allocno attributes
63 which are important for understanding the allocation algorithm:
64
65 - *Live ranges*. This is a list of ranges of *program points*
66 where the allocno lives. Program points represent places
67 where a pseudo can be born or become dead (there are
68 approximately two times more program points than the insns)
69 and they are represented by integers starting with 0. The
70 live ranges are used to find conflicts between allocnos.
71 They also play very important role for the transformation of
72 the IRA internal representation of several regions into a one
73 region representation. The later is used during the reload
74 pass work because each allocno represents all of the
75 corresponding pseudo-registers.
76
77 - *Hard-register costs*. This is a vector of size equal to the
78 number of available hard-registers of the allocno class. The
79 cost of a callee-clobbered hard-register for an allocno is
80 increased by the cost of save/restore code around the calls
81 through the given allocno's life. If the allocno is a move
82 instruction operand and another operand is a hard-register of
83 the allocno class, the cost of the hard-register is decreased
84 by the move cost.
85
86 When an allocno is assigned, the hard-register with minimal
87 full cost is used. Initially, a hard-register's full cost is
88 the corresponding value from the hard-register's cost vector.
89 If the allocno is connected by a *copy* (see below) to
90 another allocno which has just received a hard-register, the
91 cost of the hard-register is decreased. Before choosing a
92 hard-register for an allocno, the allocno's current costs of
93 the hard-registers are modified by the conflict hard-register
94 costs of all of the conflicting allocnos which are not
95 assigned yet.
96
97 - *Conflict hard-register costs*. This is a vector of the same
98 size as the hard-register costs vector. To permit an
99 unassigned allocno to get a better hard-register, IRA uses
100 this vector to calculate the final full cost of the
101 available hard-registers. Conflict hard-register costs of an
102 unassigned allocno are also changed with a change of the
103 hard-register cost of the allocno when a copy involving the
104 allocno is processed as described above. This is done to
105 show other unassigned allocnos that a given allocno prefers
106 some hard-registers in order to remove the move instruction
107 corresponding to the copy.
108
109 o *Cap*. If a pseudo-register does not live in a region but
110 lives in a nested region, IRA creates a special allocno called
111 a cap in the outer region. A region cap is also created for a
112 subregion cap.
113
114 o *Copy*. Allocnos can be connected by copies. Copies are used
115 to modify hard-register costs for allocnos during coloring.
116 Such modifications reflects a preference to use the same
117 hard-register for the allocnos connected by copies. Usually
118 copies are created for move insns (in this case it results in
119 register coalescing). But IRA also creates copies for operands
120 of an insn which should be assigned to the same hard-register
121 due to constraints in the machine description (it usually
122 results in removing a move generated in reload to satisfy
123 the constraints) and copies referring to the allocno which is
124 the output operand of an instruction and the allocno which is
125 an input operand dying in the instruction (creation of such
126 copies results in less register shuffling). IRA *does not*
127 create copies between the same register allocnos from different
128 regions because we use another technique for propagating
129 hard-register preference on the borders of regions.
130
131 Allocnos (including caps) for the upper region in the region tree
132 *accumulate* information important for coloring from allocnos with
133 the same pseudo-register from nested regions. This includes
134 hard-register and memory costs, conflicts with hard-registers,
135 allocno conflicts, allocno copies and more. *Thus, attributes for
136 allocnos in a region have the same values as if the region had no
137 subregions*. It means that attributes for allocnos in the
138 outermost region corresponding to the function have the same values
139 as though the allocation used only one region which is the entire
140 function. It also means that we can look at IRA work as if the
141 first IRA did allocation for all function then it improved the
142 allocation for loops then their subloops and so on.
143
144 IRA major passes are:
145
146 o Building IRA internal representation which consists of the
147 following subpasses:
148
149 * First, IRA builds regions and creates allocnos (file
150 ira-build.cc) and initializes most of their attributes.
151
152 * Then IRA finds an allocno class for each allocno and
153 calculates its initial (non-accumulated) cost of memory and
154 each hard-register of its allocno class (file ira-cost.c).
155
156 * IRA creates live ranges of each allocno, calculates register
157 pressure for each pressure class in each region, sets up
158 conflict hard registers for each allocno and info about calls
159 the allocno lives through (file ira-lives.cc).
160
161 * IRA removes low register pressure loops from the regions
162 mostly to speed IRA up (file ira-build.cc).
163
164 * IRA propagates accumulated allocno info from lower region
165 allocnos to corresponding upper region allocnos (file
166 ira-build.cc).
167
168 * IRA creates all caps (file ira-build.cc).
169
170 * Having live-ranges of allocnos and their classes, IRA creates
171 conflicting allocnos for each allocno. Conflicting allocnos
172 are stored as a bit vector or array of pointers to the
173 conflicting allocnos whatever is more profitable (file
174 ira-conflicts.cc). At this point IRA creates allocno copies.
175
176 o Coloring. Now IRA has all necessary info to start graph coloring
177 process. It is done in each region on top-down traverse of the
178 region tree (file ira-color.cc). There are following subpasses:
179
180 * Finding profitable hard registers of corresponding allocno
181 class for each allocno. For example, only callee-saved hard
182 registers are frequently profitable for allocnos living
183 through colors. If the profitable hard register set of
184 allocno does not form a tree based on subset relation, we use
185 some approximation to form the tree. This approximation is
186 used to figure out trivial colorability of allocnos. The
187 approximation is a pretty rare case.
188
189 * Putting allocnos onto the coloring stack. IRA uses Briggs
190 optimistic coloring which is a major improvement over
191 Chaitin's coloring. Therefore IRA does not spill allocnos at
192 this point. There is some freedom in the order of putting
193 allocnos on the stack which can affect the final result of
194 the allocation. IRA uses some heuristics to improve the
195 order. The major one is to form *threads* from colorable
196 allocnos and push them on the stack by threads. Thread is a
197 set of non-conflicting colorable allocnos connected by
198 copies. The thread contains allocnos from the colorable
199 bucket or colorable allocnos already pushed onto the coloring
200 stack. Pushing thread allocnos one after another onto the
201 stack increases chances of removing copies when the allocnos
202 get the same hard reg.
203
204 We also use a modification of Chaitin-Briggs algorithm which
205 works for intersected register classes of allocnos. To
206 figure out trivial colorability of allocnos, the mentioned
207 above tree of hard register sets is used. To get an idea how
208 the algorithm works in i386 example, let us consider an
209 allocno to which any general hard register can be assigned.
210 If the allocno conflicts with eight allocnos to which only
211 EAX register can be assigned, given allocno is still
212 trivially colorable because all conflicting allocnos might be
213 assigned only to EAX and all other general hard registers are
214 still free.
215
216 To get an idea of the used trivial colorability criterion, it
217 is also useful to read article "Graph-Coloring Register
218 Allocation for Irregular Architectures" by Michael D. Smith
219 and Glen Holloway. Major difference between the article
220 approach and approach used in IRA is that Smith's approach
221 takes register classes only from machine description and IRA
222 calculate register classes from intermediate code too
223 (e.g. an explicit usage of hard registers in RTL code for
224 parameter passing can result in creation of additional
225 register classes which contain or exclude the hard
226 registers). That makes IRA approach useful for improving
227 coloring even for architectures with regular register files
228 and in fact some benchmarking shows the improvement for
229 regular class architectures is even bigger than for irregular
230 ones. Another difference is that Smith's approach chooses
231 intersection of classes of all insn operands in which a given
232 pseudo occurs. IRA can use bigger classes if it is still
233 more profitable than memory usage.
234
235 * Popping the allocnos from the stack and assigning them hard
236 registers. If IRA cannot assign a hard register to an
237 allocno and the allocno is coalesced, IRA undoes the
238 coalescing and puts the uncoalesced allocnos onto the stack in
239 the hope that some such allocnos will get a hard register
240 separately. If IRA fails to assign hard register or memory
241 is more profitable for it, IRA spills the allocno. IRA
242 assigns the allocno the hard-register with minimal full
243 allocation cost which reflects the cost of usage of the
244 hard-register for the allocno and cost of usage of the
245 hard-register for allocnos conflicting with given allocno.
246
247 * Chaitin-Briggs coloring assigns as many pseudos as possible
248 to hard registers. After coloring we try to improve
249 allocation with cost point of view. We improve the
250 allocation by spilling some allocnos and assigning the freed
251 hard registers to other allocnos if it decreases the overall
252 allocation cost.
253
254 * After allocno assigning in the region, IRA modifies the hard
255 register and memory costs for the corresponding allocnos in
256 the subregions to reflect the cost of possible loads, stores,
257 or moves on the border of the region and its subregions.
258 When default regional allocation algorithm is used
259 (-fira-algorithm=mixed), IRA just propagates the assignment
260 for allocnos if the register pressure in the region for the
261 corresponding pressure class is less than number of available
262 hard registers for given pressure class.
263
264 o Spill/restore code moving. When IRA performs an allocation
265 by traversing regions in top-down order, it does not know what
266 happens below in the region tree. Therefore, sometimes IRA
267 misses opportunities to perform a better allocation. A simple
268 optimization tries to improve allocation in a region having
269 subregions and containing in another region. If the
270 corresponding allocnos in the subregion are spilled, it spills
271 the region allocno if it is profitable. The optimization
272 implements a simple iterative algorithm performing profitable
273 transformations while they are still possible. It is fast in
274 practice, so there is no real need for a better time complexity
275 algorithm.
276
277 o Code change. After coloring, two allocnos representing the
278 same pseudo-register outside and inside a region respectively
279 may be assigned to different locations (hard-registers or
280 memory). In this case IRA creates and uses a new
281 pseudo-register inside the region and adds code to move allocno
282 values on the region's borders. This is done during top-down
283 traversal of the regions (file ira-emit.cc). In some
284 complicated cases IRA can create a new allocno to move allocno
285 values (e.g. when a swap of values stored in two hard-registers
286 is needed). At this stage, the new allocno is marked as
287 spilled. IRA still creates the pseudo-register and the moves
288 on the region borders even when both allocnos were assigned to
289 the same hard-register. If the reload pass spills a
290 pseudo-register for some reason, the effect will be smaller
291 because another allocno will still be in the hard-register. In
292 most cases, this is better then spilling both allocnos. If
293 reload does not change the allocation for the two
294 pseudo-registers, the trivial move will be removed by
295 post-reload optimizations. IRA does not generate moves for
296 allocnos assigned to the same hard register when the default
297 regional allocation algorithm is used and the register pressure
298 in the region for the corresponding pressure class is less than
299 number of available hard registers for given pressure class.
300 IRA also does some optimizations to remove redundant stores and
301 to reduce code duplication on the region borders.
302
303 o Flattening internal representation. After changing code, IRA
304 transforms its internal representation for several regions into
305 one region representation (file ira-build.cc). This process is
306 called IR flattening. Such process is more complicated than IR
307 rebuilding would be, but is much faster.
308
309 o After IR flattening, IRA tries to assign hard registers to all
310 spilled allocnos. This is implemented by a simple and fast
311 priority coloring algorithm (see function
312 ira_reassign_conflict_allocnos::ira-color.cc). Here new allocnos
313 created during the code change pass can be assigned to hard
314 registers.
315
316 o At the end IRA calls the reload pass. The reload pass
317 communicates with IRA through several functions in file
318 ira-color.cc to improve its decisions in
319
320 * sharing stack slots for the spilled pseudos based on IRA info
321 about pseudo-register conflicts.
322
323 * reassigning hard-registers to all spilled pseudos at the end
324 of each reload iteration.
325
326 * choosing a better hard-register to spill based on IRA info
327 about pseudo-register live ranges and the register pressure
328 in places where the pseudo-register lives.
329
330 IRA uses a lot of data representing the target processors. These
331 data are initialized in file ira.cc.
332
333 If function has no loops (or the loops are ignored when
334 -fira-algorithm=CB is used), we have classic Chaitin-Briggs
335 coloring (only instead of separate pass of coalescing, we use hard
336 register preferencing). In such case, IRA works much faster
337 because many things are not made (like IR flattening, the
338 spill/restore optimization, and the code change).
339
340 Literature is worth to read for better understanding the code:
341
342 o Preston Briggs, Keith D. Cooper, Linda Torczon. Improvements to
343 Graph Coloring Register Allocation.
344
345 o David Callahan, Brian Koblenz. Register allocation via
346 hierarchical graph coloring.
347
348 o Keith Cooper, Anshuman Dasgupta, Jason Eckhardt. Revisiting Graph
349 Coloring Register Allocation: A Study of the Chaitin-Briggs and
350 Callahan-Koblenz Algorithms.
351
352 o Guei-Yuan Lueh, Thomas Gross, and Ali-Reza Adl-Tabatabai. Global
353 Register Allocation Based on Graph Fusion.
354
355 o Michael D. Smith and Glenn Holloway. Graph-Coloring Register
356 Allocation for Irregular Architectures
357
358 o Vladimir Makarov. The Integrated Register Allocator for GCC.
359
360 o Vladimir Makarov. The top-down register allocator for irregular
361 register file architectures.
362
363 */
364
365
366 #include "config.h"
367 #include "system.h"
368 #include "coretypes.h"
369 #include "backend.h"
370 #include "target.h"
371 #include "rtl.h"
372 #include "tree.h"
373 #include "df.h"
374 #include "memmodel.h"
375 #include "tm_p.h"
376 #include "insn-config.h"
377 #include "regs.h"
378 #include "ira.h"
379 #include "ira-int.h"
380 #include "diagnostic-core.h"
381 #include "cfgrtl.h"
382 #include "cfgbuild.h"
383 #include "cfgcleanup.h"
384 #include "expr.h"
385 #include "tree-pass.h"
386 #include "output.h"
387 #include "reload.h"
388 #include "cfgloop.h"
389 #include "lra.h"
390 #include "dce.h"
391 #include "dbgcnt.h"
392 #include "rtl-iter.h"
393 #include "shrink-wrap.h"
394 #include "print-rtl.h"
395
396 struct target_ira default_target_ira;
397 class target_ira_int default_target_ira_int;
398 #if SWITCHABLE_TARGET
399 struct target_ira *this_target_ira = &default_target_ira;
400 class target_ira_int *this_target_ira_int = &default_target_ira_int;
401 #endif
402
403 /* A modified value of flag `-fira-verbose' used internally. */
404 int internal_flag_ira_verbose;
405
406 /* Dump file of the allocator if it is not NULL. */
407 FILE *ira_dump_file;
408
409 /* The number of elements in the following array. */
410 int ira_spilled_reg_stack_slots_num;
411
412 /* The following array contains info about spilled pseudo-registers
413 stack slots used in current function so far. */
414 class ira_spilled_reg_stack_slot *ira_spilled_reg_stack_slots;
415
416 /* Correspondingly overall cost of the allocation, overall cost before
417 reload, cost of the allocnos assigned to hard-registers, cost of
418 the allocnos assigned to memory, cost of loads, stores and register
419 move insns generated for pseudo-register live range splitting (see
420 ira-emit.cc). */
421 int64_t ira_overall_cost, overall_cost_before;
422 int64_t ira_reg_cost, ira_mem_cost;
423 int64_t ira_load_cost, ira_store_cost, ira_shuffle_cost;
424 int ira_move_loops_num, ira_additional_jumps_num;
425
426 /* All registers that can be eliminated. */
427
428 HARD_REG_SET eliminable_regset;
429
430 /* Value of max_reg_num () before IRA work start. This value helps
431 us to recognize a situation when new pseudos were created during
432 IRA work. */
433 static int max_regno_before_ira;
434
435 /* Temporary hard reg set used for a different calculation. */
436 static HARD_REG_SET temp_hard_regset;
437
438 #define last_mode_for_init_move_cost \
439 (this_target_ira_int->x_last_mode_for_init_move_cost)
440
441
442 /* The function sets up the map IRA_REG_MODE_HARD_REGSET. */
443 static void
444 setup_reg_mode_hard_regset (void)
445 {
446 int i, m, hard_regno;
447
448 for (m = 0; m < NUM_MACHINE_MODES; m++)
449 for (hard_regno = 0; hard_regno < FIRST_PSEUDO_REGISTER; hard_regno++)
450 {
451 CLEAR_HARD_REG_SET (ira_reg_mode_hard_regset[hard_regno][m]);
452 for (i = hard_regno_nregs (hard_regno, (machine_mode) m) - 1;
453 i >= 0; i--)
454 if (hard_regno + i < FIRST_PSEUDO_REGISTER)
455 SET_HARD_REG_BIT (ira_reg_mode_hard_regset[hard_regno][m],
456 hard_regno + i);
457 }
458 }
459
460
461 #define no_unit_alloc_regs \
462 (this_target_ira_int->x_no_unit_alloc_regs)
463
464 /* The function sets up the three arrays declared above. */
465 static void
466 setup_class_hard_regs (void)
467 {
468 int cl, i, hard_regno, n;
469 HARD_REG_SET processed_hard_reg_set;
470
471 ira_assert (SHRT_MAX >= FIRST_PSEUDO_REGISTER);
472 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
473 {
474 temp_hard_regset = reg_class_contents[cl] & ~no_unit_alloc_regs;
475 CLEAR_HARD_REG_SET (processed_hard_reg_set);
476 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
477 {
478 ira_non_ordered_class_hard_regs[cl][i] = -1;
479 ira_class_hard_reg_index[cl][i] = -1;
480 }
481 for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
482 {
483 #ifdef REG_ALLOC_ORDER
484 hard_regno = reg_alloc_order[i];
485 #else
486 hard_regno = i;
487 #endif
488 if (TEST_HARD_REG_BIT (processed_hard_reg_set, hard_regno))
489 continue;
490 SET_HARD_REG_BIT (processed_hard_reg_set, hard_regno);
491 if (! TEST_HARD_REG_BIT (temp_hard_regset, hard_regno))
492 ira_class_hard_reg_index[cl][hard_regno] = -1;
493 else
494 {
495 ira_class_hard_reg_index[cl][hard_regno] = n;
496 ira_class_hard_regs[cl][n++] = hard_regno;
497 }
498 }
499 ira_class_hard_regs_num[cl] = n;
500 for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
501 if (TEST_HARD_REG_BIT (temp_hard_regset, i))
502 ira_non_ordered_class_hard_regs[cl][n++] = i;
503 ira_assert (ira_class_hard_regs_num[cl] == n);
504 }
505 }
506
507 /* Set up global variables defining info about hard registers for the
508 allocation. These depend on USE_HARD_FRAME_P whose TRUE value means
509 that we can use the hard frame pointer for the allocation. */
510 static void
511 setup_alloc_regs (bool use_hard_frame_p)
512 {
513 #ifdef ADJUST_REG_ALLOC_ORDER
514 ADJUST_REG_ALLOC_ORDER;
515 #endif
516 no_unit_alloc_regs = fixed_nonglobal_reg_set;
517 if (! use_hard_frame_p)
518 add_to_hard_reg_set (&no_unit_alloc_regs, Pmode,
519 HARD_FRAME_POINTER_REGNUM);
520 setup_class_hard_regs ();
521 }
522
523
524
525 #define alloc_reg_class_subclasses \
526 (this_target_ira_int->x_alloc_reg_class_subclasses)
527
528 /* Initialize the table of subclasses of each reg class. */
529 static void
530 setup_reg_subclasses (void)
531 {
532 int i, j;
533 HARD_REG_SET temp_hard_regset2;
534
535 for (i = 0; i < N_REG_CLASSES; i++)
536 for (j = 0; j < N_REG_CLASSES; j++)
537 alloc_reg_class_subclasses[i][j] = LIM_REG_CLASSES;
538
539 for (i = 0; i < N_REG_CLASSES; i++)
540 {
541 if (i == (int) NO_REGS)
542 continue;
543
544 temp_hard_regset = reg_class_contents[i] & ~no_unit_alloc_regs;
545 if (hard_reg_set_empty_p (temp_hard_regset))
546 continue;
547 for (j = 0; j < N_REG_CLASSES; j++)
548 if (i != j)
549 {
550 enum reg_class *p;
551
552 temp_hard_regset2 = reg_class_contents[j] & ~no_unit_alloc_regs;
553 if (! hard_reg_set_subset_p (temp_hard_regset,
554 temp_hard_regset2))
555 continue;
556 p = &alloc_reg_class_subclasses[j][0];
557 while (*p != LIM_REG_CLASSES) p++;
558 *p = (enum reg_class) i;
559 }
560 }
561 }
562
563
564
565 /* Set up IRA_MEMORY_MOVE_COST and IRA_MAX_MEMORY_MOVE_COST. */
566 static void
567 setup_class_subset_and_memory_move_costs (void)
568 {
569 int cl, cl2, mode, cost;
570 HARD_REG_SET temp_hard_regset2;
571
572 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
573 ira_memory_move_cost[mode][NO_REGS][0]
574 = ira_memory_move_cost[mode][NO_REGS][1] = SHRT_MAX;
575 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
576 {
577 if (cl != (int) NO_REGS)
578 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
579 {
580 ira_max_memory_move_cost[mode][cl][0]
581 = ira_memory_move_cost[mode][cl][0]
582 = memory_move_cost ((machine_mode) mode,
583 (reg_class_t) cl, false);
584 ira_max_memory_move_cost[mode][cl][1]
585 = ira_memory_move_cost[mode][cl][1]
586 = memory_move_cost ((machine_mode) mode,
587 (reg_class_t) cl, true);
588 /* Costs for NO_REGS are used in cost calculation on the
589 1st pass when the preferred register classes are not
590 known yet. In this case we take the best scenario. */
591 if (!targetm.hard_regno_mode_ok (ira_class_hard_regs[cl][0],
592 (machine_mode) mode))
593 continue;
594
595 if (ira_memory_move_cost[mode][NO_REGS][0]
596 > ira_memory_move_cost[mode][cl][0])
597 ira_max_memory_move_cost[mode][NO_REGS][0]
598 = ira_memory_move_cost[mode][NO_REGS][0]
599 = ira_memory_move_cost[mode][cl][0];
600 if (ira_memory_move_cost[mode][NO_REGS][1]
601 > ira_memory_move_cost[mode][cl][1])
602 ira_max_memory_move_cost[mode][NO_REGS][1]
603 = ira_memory_move_cost[mode][NO_REGS][1]
604 = ira_memory_move_cost[mode][cl][1];
605 }
606 }
607 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
608 for (cl2 = (int) N_REG_CLASSES - 1; cl2 >= 0; cl2--)
609 {
610 temp_hard_regset = reg_class_contents[cl] & ~no_unit_alloc_regs;
611 temp_hard_regset2 = reg_class_contents[cl2] & ~no_unit_alloc_regs;
612 ira_class_subset_p[cl][cl2]
613 = hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2);
614 if (! hard_reg_set_empty_p (temp_hard_regset2)
615 && hard_reg_set_subset_p (reg_class_contents[cl2],
616 reg_class_contents[cl]))
617 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
618 {
619 cost = ira_memory_move_cost[mode][cl2][0];
620 if (cost > ira_max_memory_move_cost[mode][cl][0])
621 ira_max_memory_move_cost[mode][cl][0] = cost;
622 cost = ira_memory_move_cost[mode][cl2][1];
623 if (cost > ira_max_memory_move_cost[mode][cl][1])
624 ira_max_memory_move_cost[mode][cl][1] = cost;
625 }
626 }
627 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
628 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
629 {
630 ira_memory_move_cost[mode][cl][0]
631 = ira_max_memory_move_cost[mode][cl][0];
632 ira_memory_move_cost[mode][cl][1]
633 = ira_max_memory_move_cost[mode][cl][1];
634 }
635 setup_reg_subclasses ();
636 }
637
638
639
640 /* Define the following macro if allocation through malloc if
641 preferable. */
642 #define IRA_NO_OBSTACK
643
644 #ifndef IRA_NO_OBSTACK
645 /* Obstack used for storing all dynamic data (except bitmaps) of the
646 IRA. */
647 static struct obstack ira_obstack;
648 #endif
649
650 /* Obstack used for storing all bitmaps of the IRA. */
651 static struct bitmap_obstack ira_bitmap_obstack;
652
653 /* Allocate memory of size LEN for IRA data. */
654 void *
655 ira_allocate (size_t len)
656 {
657 void *res;
658
659 #ifndef IRA_NO_OBSTACK
660 res = obstack_alloc (&ira_obstack, len);
661 #else
662 res = xmalloc (len);
663 #endif
664 return res;
665 }
666
667 /* Free memory ADDR allocated for IRA data. */
668 void
669 ira_free (void *addr ATTRIBUTE_UNUSED)
670 {
671 #ifndef IRA_NO_OBSTACK
672 /* do nothing */
673 #else
674 free (addr);
675 #endif
676 }
677
678
679 /* Allocate and returns bitmap for IRA. */
680 bitmap
681 ira_allocate_bitmap (void)
682 {
683 return BITMAP_ALLOC (&ira_bitmap_obstack);
684 }
685
686 /* Free bitmap B allocated for IRA. */
687 void
688 ira_free_bitmap (bitmap b ATTRIBUTE_UNUSED)
689 {
690 /* do nothing */
691 }
692
693
694
695 /* Output information about allocation of all allocnos (except for
696 caps) into file F. */
697 void
698 ira_print_disposition (FILE *f)
699 {
700 int i, n, max_regno;
701 ira_allocno_t a;
702 basic_block bb;
703
704 fprintf (f, "Disposition:");
705 max_regno = max_reg_num ();
706 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
707 for (a = ira_regno_allocno_map[i];
708 a != NULL;
709 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
710 {
711 if (n % 4 == 0)
712 fprintf (f, "\n");
713 n++;
714 fprintf (f, " %4d:r%-4d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
715 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
716 fprintf (f, "b%-3d", bb->index);
717 else
718 fprintf (f, "l%-3d", ALLOCNO_LOOP_TREE_NODE (a)->loop_num);
719 if (ALLOCNO_HARD_REGNO (a) >= 0)
720 fprintf (f, " %3d", ALLOCNO_HARD_REGNO (a));
721 else
722 fprintf (f, " mem");
723 }
724 fprintf (f, "\n");
725 }
726
727 /* Outputs information about allocation of all allocnos into
728 stderr. */
729 void
730 ira_debug_disposition (void)
731 {
732 ira_print_disposition (stderr);
733 }
734
735
736
737 /* Set up ira_stack_reg_pressure_class which is the biggest pressure
738 register class containing stack registers or NO_REGS if there are
739 no stack registers. To find this class, we iterate through all
740 register pressure classes and choose the first register pressure
741 class containing all the stack registers and having the biggest
742 size. */
743 static void
744 setup_stack_reg_pressure_class (void)
745 {
746 ira_stack_reg_pressure_class = NO_REGS;
747 #ifdef STACK_REGS
748 {
749 int i, best, size;
750 enum reg_class cl;
751 HARD_REG_SET temp_hard_regset2;
752
753 CLEAR_HARD_REG_SET (temp_hard_regset);
754 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
755 SET_HARD_REG_BIT (temp_hard_regset, i);
756 best = 0;
757 for (i = 0; i < ira_pressure_classes_num; i++)
758 {
759 cl = ira_pressure_classes[i];
760 temp_hard_regset2 = temp_hard_regset & reg_class_contents[cl];
761 size = hard_reg_set_size (temp_hard_regset2);
762 if (best < size)
763 {
764 best = size;
765 ira_stack_reg_pressure_class = cl;
766 }
767 }
768 }
769 #endif
770 }
771
772 /* Find pressure classes which are register classes for which we
773 calculate register pressure in IRA, register pressure sensitive
774 insn scheduling, and register pressure sensitive loop invariant
775 motion.
776
777 To make register pressure calculation easy, we always use
778 non-intersected register pressure classes. A move of hard
779 registers from one register pressure class is not more expensive
780 than load and store of the hard registers. Most likely an allocno
781 class will be a subset of a register pressure class and in many
782 cases a register pressure class. That makes usage of register
783 pressure classes a good approximation to find a high register
784 pressure. */
785 static void
786 setup_pressure_classes (void)
787 {
788 int cost, i, n, curr;
789 int cl, cl2;
790 enum reg_class pressure_classes[N_REG_CLASSES];
791 int m;
792 HARD_REG_SET temp_hard_regset2;
793 bool insert_p;
794
795 if (targetm.compute_pressure_classes)
796 n = targetm.compute_pressure_classes (pressure_classes);
797 else
798 {
799 n = 0;
800 for (cl = 0; cl < N_REG_CLASSES; cl++)
801 {
802 if (ira_class_hard_regs_num[cl] == 0)
803 continue;
804 if (ira_class_hard_regs_num[cl] != 1
805 /* A register class without subclasses may contain a few
806 hard registers and movement between them is costly
807 (e.g. SPARC FPCC registers). We still should consider it
808 as a candidate for a pressure class. */
809 && alloc_reg_class_subclasses[cl][0] < cl)
810 {
811 /* Check that the moves between any hard registers of the
812 current class are not more expensive for a legal mode
813 than load/store of the hard registers of the current
814 class. Such class is a potential candidate to be a
815 register pressure class. */
816 for (m = 0; m < NUM_MACHINE_MODES; m++)
817 {
818 temp_hard_regset
819 = (reg_class_contents[cl]
820 & ~(no_unit_alloc_regs
821 | ira_prohibited_class_mode_regs[cl][m]));
822 if (hard_reg_set_empty_p (temp_hard_regset))
823 continue;
824 ira_init_register_move_cost_if_necessary ((machine_mode) m);
825 cost = ira_register_move_cost[m][cl][cl];
826 if (cost <= ira_max_memory_move_cost[m][cl][1]
827 || cost <= ira_max_memory_move_cost[m][cl][0])
828 break;
829 }
830 if (m >= NUM_MACHINE_MODES)
831 continue;
832 }
833 curr = 0;
834 insert_p = true;
835 temp_hard_regset = reg_class_contents[cl] & ~no_unit_alloc_regs;
836 /* Remove so far added pressure classes which are subset of the
837 current candidate class. Prefer GENERAL_REGS as a pressure
838 register class to another class containing the same
839 allocatable hard registers. We do this because machine
840 dependent cost hooks might give wrong costs for the latter
841 class but always give the right cost for the former class
842 (GENERAL_REGS). */
843 for (i = 0; i < n; i++)
844 {
845 cl2 = pressure_classes[i];
846 temp_hard_regset2 = (reg_class_contents[cl2]
847 & ~no_unit_alloc_regs);
848 if (hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2)
849 && (temp_hard_regset != temp_hard_regset2
850 || cl2 == (int) GENERAL_REGS))
851 {
852 pressure_classes[curr++] = (enum reg_class) cl2;
853 insert_p = false;
854 continue;
855 }
856 if (hard_reg_set_subset_p (temp_hard_regset2, temp_hard_regset)
857 && (temp_hard_regset2 != temp_hard_regset
858 || cl == (int) GENERAL_REGS))
859 continue;
860 if (temp_hard_regset2 == temp_hard_regset)
861 insert_p = false;
862 pressure_classes[curr++] = (enum reg_class) cl2;
863 }
864 /* If the current candidate is a subset of a so far added
865 pressure class, don't add it to the list of the pressure
866 classes. */
867 if (insert_p)
868 pressure_classes[curr++] = (enum reg_class) cl;
869 n = curr;
870 }
871 }
872 #ifdef ENABLE_IRA_CHECKING
873 {
874 HARD_REG_SET ignore_hard_regs;
875
876 /* Check pressure classes correctness: here we check that hard
877 registers from all register pressure classes contains all hard
878 registers available for the allocation. */
879 CLEAR_HARD_REG_SET (temp_hard_regset);
880 CLEAR_HARD_REG_SET (temp_hard_regset2);
881 ignore_hard_regs = no_unit_alloc_regs;
882 for (cl = 0; cl < LIM_REG_CLASSES; cl++)
883 {
884 /* For some targets (like MIPS with MD_REGS), there are some
885 classes with hard registers available for allocation but
886 not able to hold value of any mode. */
887 for (m = 0; m < NUM_MACHINE_MODES; m++)
888 if (contains_reg_of_mode[cl][m])
889 break;
890 if (m >= NUM_MACHINE_MODES)
891 {
892 ignore_hard_regs |= reg_class_contents[cl];
893 continue;
894 }
895 for (i = 0; i < n; i++)
896 if ((int) pressure_classes[i] == cl)
897 break;
898 temp_hard_regset2 |= reg_class_contents[cl];
899 if (i < n)
900 temp_hard_regset |= reg_class_contents[cl];
901 }
902 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
903 /* Some targets (like SPARC with ICC reg) have allocatable regs
904 for which no reg class is defined. */
905 if (REGNO_REG_CLASS (i) == NO_REGS)
906 SET_HARD_REG_BIT (ignore_hard_regs, i);
907 temp_hard_regset &= ~ignore_hard_regs;
908 temp_hard_regset2 &= ~ignore_hard_regs;
909 ira_assert (hard_reg_set_subset_p (temp_hard_regset2, temp_hard_regset));
910 }
911 #endif
912 ira_pressure_classes_num = 0;
913 for (i = 0; i < n; i++)
914 {
915 cl = (int) pressure_classes[i];
916 ira_reg_pressure_class_p[cl] = true;
917 ira_pressure_classes[ira_pressure_classes_num++] = (enum reg_class) cl;
918 }
919 setup_stack_reg_pressure_class ();
920 }
921
922 /* Set up IRA_UNIFORM_CLASS_P. Uniform class is a register class
923 whose register move cost between any registers of the class is the
924 same as for all its subclasses. We use the data to speed up the
925 2nd pass of calculations of allocno costs. */
926 static void
927 setup_uniform_class_p (void)
928 {
929 int i, cl, cl2, m;
930
931 for (cl = 0; cl < N_REG_CLASSES; cl++)
932 {
933 ira_uniform_class_p[cl] = false;
934 if (ira_class_hard_regs_num[cl] == 0)
935 continue;
936 /* We cannot use alloc_reg_class_subclasses here because move
937 cost hooks does not take into account that some registers are
938 unavailable for the subtarget. E.g. for i686, INT_SSE_REGS
939 is element of alloc_reg_class_subclasses for GENERAL_REGS
940 because SSE regs are unavailable. */
941 for (i = 0; (cl2 = reg_class_subclasses[cl][i]) != LIM_REG_CLASSES; i++)
942 {
943 if (ira_class_hard_regs_num[cl2] == 0)
944 continue;
945 for (m = 0; m < NUM_MACHINE_MODES; m++)
946 if (contains_reg_of_mode[cl][m] && contains_reg_of_mode[cl2][m])
947 {
948 ira_init_register_move_cost_if_necessary ((machine_mode) m);
949 if (ira_register_move_cost[m][cl][cl]
950 != ira_register_move_cost[m][cl2][cl2])
951 break;
952 }
953 if (m < NUM_MACHINE_MODES)
954 break;
955 }
956 if (cl2 == LIM_REG_CLASSES)
957 ira_uniform_class_p[cl] = true;
958 }
959 }
960
961 /* Set up IRA_ALLOCNO_CLASSES, IRA_ALLOCNO_CLASSES_NUM,
962 IRA_IMPORTANT_CLASSES, and IRA_IMPORTANT_CLASSES_NUM.
963
964 Target may have many subtargets and not all target hard registers can
965 be used for allocation, e.g. x86 port in 32-bit mode cannot use
966 hard registers introduced in x86-64 like r8-r15). Some classes
967 might have the same allocatable hard registers, e.g. INDEX_REGS
968 and GENERAL_REGS in x86 port in 32-bit mode. To decrease different
969 calculations efforts we introduce allocno classes which contain
970 unique non-empty sets of allocatable hard-registers.
971
972 Pseudo class cost calculation in ira-costs.cc is very expensive.
973 Therefore we are trying to decrease number of classes involved in
974 such calculation. Register classes used in the cost calculation
975 are called important classes. They are allocno classes and other
976 non-empty classes whose allocatable hard register sets are inside
977 of an allocno class hard register set. From the first sight, it
978 looks like that they are just allocno classes. It is not true. In
979 example of x86-port in 32-bit mode, allocno classes will contain
980 GENERAL_REGS but not LEGACY_REGS (because allocatable hard
981 registers are the same for the both classes). The important
982 classes will contain GENERAL_REGS and LEGACY_REGS. It is done
983 because a machine description insn constraint may refers for
984 LEGACY_REGS and code in ira-costs.cc is mostly base on investigation
985 of the insn constraints. */
986 static void
987 setup_allocno_and_important_classes (void)
988 {
989 int i, j, n, cl;
990 bool set_p;
991 HARD_REG_SET temp_hard_regset2;
992 static enum reg_class classes[LIM_REG_CLASSES + 1];
993
994 n = 0;
995 /* Collect classes which contain unique sets of allocatable hard
996 registers. Prefer GENERAL_REGS to other classes containing the
997 same set of hard registers. */
998 for (i = 0; i < LIM_REG_CLASSES; i++)
999 {
1000 temp_hard_regset = reg_class_contents[i] & ~no_unit_alloc_regs;
1001 for (j = 0; j < n; j++)
1002 {
1003 cl = classes[j];
1004 temp_hard_regset2 = reg_class_contents[cl] & ~no_unit_alloc_regs;
1005 if (temp_hard_regset == temp_hard_regset2)
1006 break;
1007 }
1008 if (j >= n || targetm.additional_allocno_class_p (i))
1009 classes[n++] = (enum reg_class) i;
1010 else if (i == GENERAL_REGS)
1011 /* Prefer general regs. For i386 example, it means that
1012 we prefer GENERAL_REGS over INDEX_REGS or LEGACY_REGS
1013 (all of them consists of the same available hard
1014 registers). */
1015 classes[j] = (enum reg_class) i;
1016 }
1017 classes[n] = LIM_REG_CLASSES;
1018
1019 /* Set up classes which can be used for allocnos as classes
1020 containing non-empty unique sets of allocatable hard
1021 registers. */
1022 ira_allocno_classes_num = 0;
1023 for (i = 0; (cl = classes[i]) != LIM_REG_CLASSES; i++)
1024 if (ira_class_hard_regs_num[cl] > 0)
1025 ira_allocno_classes[ira_allocno_classes_num++] = (enum reg_class) cl;
1026 ira_important_classes_num = 0;
1027 /* Add non-allocno classes containing to non-empty set of
1028 allocatable hard regs. */
1029 for (cl = 0; cl < N_REG_CLASSES; cl++)
1030 if (ira_class_hard_regs_num[cl] > 0)
1031 {
1032 temp_hard_regset = reg_class_contents[cl] & ~no_unit_alloc_regs;
1033 set_p = false;
1034 for (j = 0; j < ira_allocno_classes_num; j++)
1035 {
1036 temp_hard_regset2 = (reg_class_contents[ira_allocno_classes[j]]
1037 & ~no_unit_alloc_regs);
1038 if ((enum reg_class) cl == ira_allocno_classes[j])
1039 break;
1040 else if (hard_reg_set_subset_p (temp_hard_regset,
1041 temp_hard_regset2))
1042 set_p = true;
1043 }
1044 if (set_p && j >= ira_allocno_classes_num)
1045 ira_important_classes[ira_important_classes_num++]
1046 = (enum reg_class) cl;
1047 }
1048 /* Now add allocno classes to the important classes. */
1049 for (j = 0; j < ira_allocno_classes_num; j++)
1050 ira_important_classes[ira_important_classes_num++]
1051 = ira_allocno_classes[j];
1052 for (cl = 0; cl < N_REG_CLASSES; cl++)
1053 {
1054 ira_reg_allocno_class_p[cl] = false;
1055 ira_reg_pressure_class_p[cl] = false;
1056 }
1057 for (j = 0; j < ira_allocno_classes_num; j++)
1058 ira_reg_allocno_class_p[ira_allocno_classes[j]] = true;
1059 setup_pressure_classes ();
1060 setup_uniform_class_p ();
1061 }
1062
1063 /* Setup translation in CLASS_TRANSLATE of all classes into a class
1064 given by array CLASSES of length CLASSES_NUM. The function is used
1065 make translation any reg class to an allocno class or to an
1066 pressure class. This translation is necessary for some
1067 calculations when we can use only allocno or pressure classes and
1068 such translation represents an approximate representation of all
1069 classes.
1070
1071 The translation in case when allocatable hard register set of a
1072 given class is subset of allocatable hard register set of a class
1073 in CLASSES is pretty simple. We use smallest classes from CLASSES
1074 containing a given class. If allocatable hard register set of a
1075 given class is not a subset of any corresponding set of a class
1076 from CLASSES, we use the cheapest (with load/store point of view)
1077 class from CLASSES whose set intersects with given class set. */
1078 static void
1079 setup_class_translate_array (enum reg_class *class_translate,
1080 int classes_num, enum reg_class *classes)
1081 {
1082 int cl, mode;
1083 enum reg_class aclass, best_class, *cl_ptr;
1084 int i, cost, min_cost, best_cost;
1085
1086 for (cl = 0; cl < N_REG_CLASSES; cl++)
1087 class_translate[cl] = NO_REGS;
1088
1089 for (i = 0; i < classes_num; i++)
1090 {
1091 aclass = classes[i];
1092 for (cl_ptr = &alloc_reg_class_subclasses[aclass][0];
1093 (cl = *cl_ptr) != LIM_REG_CLASSES;
1094 cl_ptr++)
1095 if (class_translate[cl] == NO_REGS)
1096 class_translate[cl] = aclass;
1097 class_translate[aclass] = aclass;
1098 }
1099 /* For classes which are not fully covered by one of given classes
1100 (in other words covered by more one given class), use the
1101 cheapest class. */
1102 for (cl = 0; cl < N_REG_CLASSES; cl++)
1103 {
1104 if (cl == NO_REGS || class_translate[cl] != NO_REGS)
1105 continue;
1106 best_class = NO_REGS;
1107 best_cost = INT_MAX;
1108 for (i = 0; i < classes_num; i++)
1109 {
1110 aclass = classes[i];
1111 temp_hard_regset = (reg_class_contents[aclass]
1112 & reg_class_contents[cl]
1113 & ~no_unit_alloc_regs);
1114 if (! hard_reg_set_empty_p (temp_hard_regset))
1115 {
1116 min_cost = INT_MAX;
1117 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1118 {
1119 cost = (ira_memory_move_cost[mode][aclass][0]
1120 + ira_memory_move_cost[mode][aclass][1]);
1121 if (min_cost > cost)
1122 min_cost = cost;
1123 }
1124 if (best_class == NO_REGS || best_cost > min_cost)
1125 {
1126 best_class = aclass;
1127 best_cost = min_cost;
1128 }
1129 }
1130 }
1131 class_translate[cl] = best_class;
1132 }
1133 }
1134
1135 /* Set up array IRA_ALLOCNO_CLASS_TRANSLATE and
1136 IRA_PRESSURE_CLASS_TRANSLATE. */
1137 static void
1138 setup_class_translate (void)
1139 {
1140 setup_class_translate_array (ira_allocno_class_translate,
1141 ira_allocno_classes_num, ira_allocno_classes);
1142 setup_class_translate_array (ira_pressure_class_translate,
1143 ira_pressure_classes_num, ira_pressure_classes);
1144 }
1145
1146 /* Order numbers of allocno classes in original target allocno class
1147 array, -1 for non-allocno classes. */
1148 static int allocno_class_order[N_REG_CLASSES];
1149
1150 /* The function used to sort the important classes. */
1151 static int
1152 comp_reg_classes_func (const void *v1p, const void *v2p)
1153 {
1154 enum reg_class cl1 = *(const enum reg_class *) v1p;
1155 enum reg_class cl2 = *(const enum reg_class *) v2p;
1156 enum reg_class tcl1, tcl2;
1157 int diff;
1158
1159 tcl1 = ira_allocno_class_translate[cl1];
1160 tcl2 = ira_allocno_class_translate[cl2];
1161 if (tcl1 != NO_REGS && tcl2 != NO_REGS
1162 && (diff = allocno_class_order[tcl1] - allocno_class_order[tcl2]) != 0)
1163 return diff;
1164 return (int) cl1 - (int) cl2;
1165 }
1166
1167 /* For correct work of function setup_reg_class_relation we need to
1168 reorder important classes according to the order of their allocno
1169 classes. It places important classes containing the same
1170 allocatable hard register set adjacent to each other and allocno
1171 class with the allocatable hard register set right after the other
1172 important classes with the same set.
1173
1174 In example from comments of function
1175 setup_allocno_and_important_classes, it places LEGACY_REGS and
1176 GENERAL_REGS close to each other and GENERAL_REGS is after
1177 LEGACY_REGS. */
1178 static void
1179 reorder_important_classes (void)
1180 {
1181 int i;
1182
1183 for (i = 0; i < N_REG_CLASSES; i++)
1184 allocno_class_order[i] = -1;
1185 for (i = 0; i < ira_allocno_classes_num; i++)
1186 allocno_class_order[ira_allocno_classes[i]] = i;
1187 qsort (ira_important_classes, ira_important_classes_num,
1188 sizeof (enum reg_class), comp_reg_classes_func);
1189 for (i = 0; i < ira_important_classes_num; i++)
1190 ira_important_class_nums[ira_important_classes[i]] = i;
1191 }
1192
1193 /* Set up IRA_REG_CLASS_SUBUNION, IRA_REG_CLASS_SUPERUNION,
1194 IRA_REG_CLASS_SUPER_CLASSES, IRA_REG_CLASSES_INTERSECT, and
1195 IRA_REG_CLASSES_INTERSECT_P. For the meaning of the relations,
1196 please see corresponding comments in ira-int.h. */
1197 static void
1198 setup_reg_class_relations (void)
1199 {
1200 int i, cl1, cl2, cl3;
1201 HARD_REG_SET intersection_set, union_set, temp_set2;
1202 bool important_class_p[N_REG_CLASSES];
1203
1204 memset (important_class_p, 0, sizeof (important_class_p));
1205 for (i = 0; i < ira_important_classes_num; i++)
1206 important_class_p[ira_important_classes[i]] = true;
1207 for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1208 {
1209 ira_reg_class_super_classes[cl1][0] = LIM_REG_CLASSES;
1210 for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1211 {
1212 ira_reg_classes_intersect_p[cl1][cl2] = false;
1213 ira_reg_class_intersect[cl1][cl2] = NO_REGS;
1214 ira_reg_class_subset[cl1][cl2] = NO_REGS;
1215 temp_hard_regset = reg_class_contents[cl1] & ~no_unit_alloc_regs;
1216 temp_set2 = reg_class_contents[cl2] & ~no_unit_alloc_regs;
1217 if (hard_reg_set_empty_p (temp_hard_regset)
1218 && hard_reg_set_empty_p (temp_set2))
1219 {
1220 /* The both classes have no allocatable hard registers
1221 -- take all class hard registers into account and use
1222 reg_class_subunion and reg_class_superunion. */
1223 for (i = 0;; i++)
1224 {
1225 cl3 = reg_class_subclasses[cl1][i];
1226 if (cl3 == LIM_REG_CLASSES)
1227 break;
1228 if (reg_class_subset_p (ira_reg_class_intersect[cl1][cl2],
1229 (enum reg_class) cl3))
1230 ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
1231 }
1232 ira_reg_class_subunion[cl1][cl2] = reg_class_subunion[cl1][cl2];
1233 ira_reg_class_superunion[cl1][cl2] = reg_class_superunion[cl1][cl2];
1234 continue;
1235 }
1236 ira_reg_classes_intersect_p[cl1][cl2]
1237 = hard_reg_set_intersect_p (temp_hard_regset, temp_set2);
1238 if (important_class_p[cl1] && important_class_p[cl2]
1239 && hard_reg_set_subset_p (temp_hard_regset, temp_set2))
1240 {
1241 /* CL1 and CL2 are important classes and CL1 allocatable
1242 hard register set is inside of CL2 allocatable hard
1243 registers -- make CL1 a superset of CL2. */
1244 enum reg_class *p;
1245
1246 p = &ira_reg_class_super_classes[cl1][0];
1247 while (*p != LIM_REG_CLASSES)
1248 p++;
1249 *p++ = (enum reg_class) cl2;
1250 *p = LIM_REG_CLASSES;
1251 }
1252 ira_reg_class_subunion[cl1][cl2] = NO_REGS;
1253 ira_reg_class_superunion[cl1][cl2] = NO_REGS;
1254 intersection_set = (reg_class_contents[cl1]
1255 & reg_class_contents[cl2]
1256 & ~no_unit_alloc_regs);
1257 union_set = ((reg_class_contents[cl1] | reg_class_contents[cl2])
1258 & ~no_unit_alloc_regs);
1259 for (cl3 = 0; cl3 < N_REG_CLASSES; cl3++)
1260 {
1261 temp_hard_regset = reg_class_contents[cl3] & ~no_unit_alloc_regs;
1262 if (hard_reg_set_empty_p (temp_hard_regset))
1263 continue;
1264
1265 if (hard_reg_set_subset_p (temp_hard_regset, intersection_set))
1266 {
1267 /* CL3 allocatable hard register set is inside of
1268 intersection of allocatable hard register sets
1269 of CL1 and CL2. */
1270 if (important_class_p[cl3])
1271 {
1272 temp_set2
1273 = (reg_class_contents
1274 [ira_reg_class_intersect[cl1][cl2]]);
1275 temp_set2 &= ~no_unit_alloc_regs;
1276 if (! hard_reg_set_subset_p (temp_hard_regset, temp_set2)
1277 /* If the allocatable hard register sets are
1278 the same, prefer GENERAL_REGS or the
1279 smallest class for debugging
1280 purposes. */
1281 || (temp_hard_regset == temp_set2
1282 && (cl3 == GENERAL_REGS
1283 || ((ira_reg_class_intersect[cl1][cl2]
1284 != GENERAL_REGS)
1285 && hard_reg_set_subset_p
1286 (reg_class_contents[cl3],
1287 reg_class_contents
1288 [(int)
1289 ira_reg_class_intersect[cl1][cl2]])))))
1290 ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
1291 }
1292 temp_set2
1293 = (reg_class_contents[ira_reg_class_subset[cl1][cl2]]
1294 & ~no_unit_alloc_regs);
1295 if (! hard_reg_set_subset_p (temp_hard_regset, temp_set2)
1296 /* Ignore unavailable hard registers and prefer
1297 smallest class for debugging purposes. */
1298 || (temp_hard_regset == temp_set2
1299 && hard_reg_set_subset_p
1300 (reg_class_contents[cl3],
1301 reg_class_contents
1302 [(int) ira_reg_class_subset[cl1][cl2]])))
1303 ira_reg_class_subset[cl1][cl2] = (enum reg_class) cl3;
1304 }
1305 if (important_class_p[cl3]
1306 && hard_reg_set_subset_p (temp_hard_regset, union_set))
1307 {
1308 /* CL3 allocatable hard register set is inside of
1309 union of allocatable hard register sets of CL1
1310 and CL2. */
1311 temp_set2
1312 = (reg_class_contents[ira_reg_class_subunion[cl1][cl2]]
1313 & ~no_unit_alloc_regs);
1314 if (ira_reg_class_subunion[cl1][cl2] == NO_REGS
1315 || (hard_reg_set_subset_p (temp_set2, temp_hard_regset)
1316
1317 && (temp_set2 != temp_hard_regset
1318 || cl3 == GENERAL_REGS
1319 /* If the allocatable hard register sets are the
1320 same, prefer GENERAL_REGS or the smallest
1321 class for debugging purposes. */
1322 || (ira_reg_class_subunion[cl1][cl2] != GENERAL_REGS
1323 && hard_reg_set_subset_p
1324 (reg_class_contents[cl3],
1325 reg_class_contents
1326 [(int) ira_reg_class_subunion[cl1][cl2]])))))
1327 ira_reg_class_subunion[cl1][cl2] = (enum reg_class) cl3;
1328 }
1329 if (hard_reg_set_subset_p (union_set, temp_hard_regset))
1330 {
1331 /* CL3 allocatable hard register set contains union
1332 of allocatable hard register sets of CL1 and
1333 CL2. */
1334 temp_set2
1335 = (reg_class_contents[ira_reg_class_superunion[cl1][cl2]]
1336 & ~no_unit_alloc_regs);
1337 if (ira_reg_class_superunion[cl1][cl2] == NO_REGS
1338 || (hard_reg_set_subset_p (temp_hard_regset, temp_set2)
1339
1340 && (temp_set2 != temp_hard_regset
1341 || cl3 == GENERAL_REGS
1342 /* If the allocatable hard register sets are the
1343 same, prefer GENERAL_REGS or the smallest
1344 class for debugging purposes. */
1345 || (ira_reg_class_superunion[cl1][cl2] != GENERAL_REGS
1346 && hard_reg_set_subset_p
1347 (reg_class_contents[cl3],
1348 reg_class_contents
1349 [(int) ira_reg_class_superunion[cl1][cl2]])))))
1350 ira_reg_class_superunion[cl1][cl2] = (enum reg_class) cl3;
1351 }
1352 }
1353 }
1354 }
1355 }
1356
1357 /* Output all uniform and important classes into file F. */
1358 static void
1359 print_uniform_and_important_classes (FILE *f)
1360 {
1361 int i, cl;
1362
1363 fprintf (f, "Uniform classes:\n");
1364 for (cl = 0; cl < N_REG_CLASSES; cl++)
1365 if (ira_uniform_class_p[cl])
1366 fprintf (f, " %s", reg_class_names[cl]);
1367 fprintf (f, "\nImportant classes:\n");
1368 for (i = 0; i < ira_important_classes_num; i++)
1369 fprintf (f, " %s", reg_class_names[ira_important_classes[i]]);
1370 fprintf (f, "\n");
1371 }
1372
1373 /* Output all possible allocno or pressure classes and their
1374 translation map into file F. */
1375 static void
1376 print_translated_classes (FILE *f, bool pressure_p)
1377 {
1378 int classes_num = (pressure_p
1379 ? ira_pressure_classes_num : ira_allocno_classes_num);
1380 enum reg_class *classes = (pressure_p
1381 ? ira_pressure_classes : ira_allocno_classes);
1382 enum reg_class *class_translate = (pressure_p
1383 ? ira_pressure_class_translate
1384 : ira_allocno_class_translate);
1385 int i;
1386
1387 fprintf (f, "%s classes:\n", pressure_p ? "Pressure" : "Allocno");
1388 for (i = 0; i < classes_num; i++)
1389 fprintf (f, " %s", reg_class_names[classes[i]]);
1390 fprintf (f, "\nClass translation:\n");
1391 for (i = 0; i < N_REG_CLASSES; i++)
1392 fprintf (f, " %s -> %s\n", reg_class_names[i],
1393 reg_class_names[class_translate[i]]);
1394 }
1395
1396 /* Output all possible allocno and translation classes and the
1397 translation maps into stderr. */
1398 void
1399 ira_debug_allocno_classes (void)
1400 {
1401 print_uniform_and_important_classes (stderr);
1402 print_translated_classes (stderr, false);
1403 print_translated_classes (stderr, true);
1404 }
1405
1406 /* Set up different arrays concerning class subsets, allocno and
1407 important classes. */
1408 static void
1409 find_reg_classes (void)
1410 {
1411 setup_allocno_and_important_classes ();
1412 setup_class_translate ();
1413 reorder_important_classes ();
1414 setup_reg_class_relations ();
1415 }
1416
1417
1418
1419 /* Set up array ira_hard_regno_allocno_class. */
1420 static void
1421 setup_hard_regno_aclass (void)
1422 {
1423 int i;
1424
1425 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1426 {
1427 ira_hard_regno_allocno_class[i]
1428 = (TEST_HARD_REG_BIT (no_unit_alloc_regs, i)
1429 ? NO_REGS
1430 : ira_allocno_class_translate[REGNO_REG_CLASS (i)]);
1431 }
1432 }
1433
1434
1435
1436 /* Form IRA_REG_CLASS_MAX_NREGS and IRA_REG_CLASS_MIN_NREGS maps. */
1437 static void
1438 setup_reg_class_nregs (void)
1439 {
1440 int i, cl, cl2, m;
1441
1442 for (m = 0; m < MAX_MACHINE_MODE; m++)
1443 {
1444 for (cl = 0; cl < N_REG_CLASSES; cl++)
1445 ira_reg_class_max_nregs[cl][m]
1446 = ira_reg_class_min_nregs[cl][m]
1447 = targetm.class_max_nregs ((reg_class_t) cl, (machine_mode) m);
1448 for (cl = 0; cl < N_REG_CLASSES; cl++)
1449 for (i = 0;
1450 (cl2 = alloc_reg_class_subclasses[cl][i]) != LIM_REG_CLASSES;
1451 i++)
1452 if (ira_reg_class_min_nregs[cl2][m]
1453 < ira_reg_class_min_nregs[cl][m])
1454 ira_reg_class_min_nregs[cl][m] = ira_reg_class_min_nregs[cl2][m];
1455 }
1456 }
1457
1458
1459
1460 /* Set up IRA_PROHIBITED_CLASS_MODE_REGS, IRA_EXCLUDE_CLASS_MODE_REGS, and
1461 IRA_CLASS_SINGLETON. This function is called once IRA_CLASS_HARD_REGS has
1462 been initialized. */
1463 static void
1464 setup_prohibited_and_exclude_class_mode_regs (void)
1465 {
1466 int j, k, hard_regno, cl, last_hard_regno, count;
1467
1468 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
1469 {
1470 temp_hard_regset = reg_class_contents[cl] & ~no_unit_alloc_regs;
1471 for (j = 0; j < NUM_MACHINE_MODES; j++)
1472 {
1473 count = 0;
1474 last_hard_regno = -1;
1475 CLEAR_HARD_REG_SET (ira_prohibited_class_mode_regs[cl][j]);
1476 CLEAR_HARD_REG_SET (ira_exclude_class_mode_regs[cl][j]);
1477 for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
1478 {
1479 hard_regno = ira_class_hard_regs[cl][k];
1480 if (!targetm.hard_regno_mode_ok (hard_regno, (machine_mode) j))
1481 SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1482 hard_regno);
1483 else if (in_hard_reg_set_p (temp_hard_regset,
1484 (machine_mode) j, hard_regno))
1485 {
1486 last_hard_regno = hard_regno;
1487 count++;
1488 }
1489 else
1490 {
1491 SET_HARD_REG_BIT (ira_exclude_class_mode_regs[cl][j], hard_regno);
1492 }
1493 }
1494 ira_class_singleton[cl][j] = (count == 1 ? last_hard_regno : -1);
1495 }
1496 }
1497 }
1498
1499 /* Clarify IRA_PROHIBITED_CLASS_MODE_REGS by excluding hard registers
1500 spanning from one register pressure class to another one. It is
1501 called after defining the pressure classes. */
1502 static void
1503 clarify_prohibited_class_mode_regs (void)
1504 {
1505 int j, k, hard_regno, cl, pclass, nregs;
1506
1507 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
1508 for (j = 0; j < NUM_MACHINE_MODES; j++)
1509 {
1510 CLEAR_HARD_REG_SET (ira_useful_class_mode_regs[cl][j]);
1511 for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
1512 {
1513 hard_regno = ira_class_hard_regs[cl][k];
1514 if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j], hard_regno))
1515 continue;
1516 nregs = hard_regno_nregs (hard_regno, (machine_mode) j);
1517 if (hard_regno + nregs > FIRST_PSEUDO_REGISTER)
1518 {
1519 SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1520 hard_regno);
1521 continue;
1522 }
1523 pclass = ira_pressure_class_translate[REGNO_REG_CLASS (hard_regno)];
1524 for (nregs-- ;nregs >= 0; nregs--)
1525 if (((enum reg_class) pclass
1526 != ira_pressure_class_translate[REGNO_REG_CLASS
1527 (hard_regno + nregs)]))
1528 {
1529 SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1530 hard_regno);
1531 break;
1532 }
1533 if (!TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1534 hard_regno))
1535 add_to_hard_reg_set (&ira_useful_class_mode_regs[cl][j],
1536 (machine_mode) j, hard_regno);
1537 }
1538 }
1539 }
1540
1541 /* Allocate and initialize IRA_REGISTER_MOVE_COST, IRA_MAY_MOVE_IN_COST
1542 and IRA_MAY_MOVE_OUT_COST for MODE. */
1543 void
1544 ira_init_register_move_cost (machine_mode mode)
1545 {
1546 static unsigned short last_move_cost[N_REG_CLASSES][N_REG_CLASSES];
1547 bool all_match = true;
1548 unsigned int i, cl1, cl2;
1549 HARD_REG_SET ok_regs;
1550
1551 ira_assert (ira_register_move_cost[mode] == NULL
1552 && ira_may_move_in_cost[mode] == NULL
1553 && ira_may_move_out_cost[mode] == NULL);
1554 CLEAR_HARD_REG_SET (ok_regs);
1555 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1556 if (targetm.hard_regno_mode_ok (i, mode))
1557 SET_HARD_REG_BIT (ok_regs, i);
1558
1559 /* Note that we might be asked about the move costs of modes that
1560 cannot be stored in any hard register, for example if an inline
1561 asm tries to create a register operand with an impossible mode.
1562 We therefore can't assert have_regs_of_mode[mode] here. */
1563 for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1564 for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1565 {
1566 int cost;
1567 if (!hard_reg_set_intersect_p (ok_regs, reg_class_contents[cl1])
1568 || !hard_reg_set_intersect_p (ok_regs, reg_class_contents[cl2]))
1569 {
1570 if ((ira_reg_class_max_nregs[cl1][mode]
1571 > ira_class_hard_regs_num[cl1])
1572 || (ira_reg_class_max_nregs[cl2][mode]
1573 > ira_class_hard_regs_num[cl2]))
1574 cost = 65535;
1575 else
1576 cost = (ira_memory_move_cost[mode][cl1][0]
1577 + ira_memory_move_cost[mode][cl2][1]) * 2;
1578 }
1579 else
1580 {
1581 cost = register_move_cost (mode, (enum reg_class) cl1,
1582 (enum reg_class) cl2);
1583 ira_assert (cost < 65535);
1584 }
1585 all_match &= (last_move_cost[cl1][cl2] == cost);
1586 last_move_cost[cl1][cl2] = cost;
1587 }
1588 if (all_match && last_mode_for_init_move_cost != -1)
1589 {
1590 ira_register_move_cost[mode]
1591 = ira_register_move_cost[last_mode_for_init_move_cost];
1592 ira_may_move_in_cost[mode]
1593 = ira_may_move_in_cost[last_mode_for_init_move_cost];
1594 ira_may_move_out_cost[mode]
1595 = ira_may_move_out_cost[last_mode_for_init_move_cost];
1596 return;
1597 }
1598 last_mode_for_init_move_cost = mode;
1599 ira_register_move_cost[mode] = XNEWVEC (move_table, N_REG_CLASSES);
1600 ira_may_move_in_cost[mode] = XNEWVEC (move_table, N_REG_CLASSES);
1601 ira_may_move_out_cost[mode] = XNEWVEC (move_table, N_REG_CLASSES);
1602 for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1603 for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1604 {
1605 int cost;
1606 enum reg_class *p1, *p2;
1607
1608 if (last_move_cost[cl1][cl2] == 65535)
1609 {
1610 ira_register_move_cost[mode][cl1][cl2] = 65535;
1611 ira_may_move_in_cost[mode][cl1][cl2] = 65535;
1612 ira_may_move_out_cost[mode][cl1][cl2] = 65535;
1613 }
1614 else
1615 {
1616 cost = last_move_cost[cl1][cl2];
1617
1618 for (p2 = &reg_class_subclasses[cl2][0];
1619 *p2 != LIM_REG_CLASSES; p2++)
1620 if (ira_class_hard_regs_num[*p2] > 0
1621 && (ira_reg_class_max_nregs[*p2][mode]
1622 <= ira_class_hard_regs_num[*p2]))
1623 cost = MAX (cost, ira_register_move_cost[mode][cl1][*p2]);
1624
1625 for (p1 = &reg_class_subclasses[cl1][0];
1626 *p1 != LIM_REG_CLASSES; p1++)
1627 if (ira_class_hard_regs_num[*p1] > 0
1628 && (ira_reg_class_max_nregs[*p1][mode]
1629 <= ira_class_hard_regs_num[*p1]))
1630 cost = MAX (cost, ira_register_move_cost[mode][*p1][cl2]);
1631
1632 ira_assert (cost <= 65535);
1633 ira_register_move_cost[mode][cl1][cl2] = cost;
1634
1635 if (ira_class_subset_p[cl1][cl2])
1636 ira_may_move_in_cost[mode][cl1][cl2] = 0;
1637 else
1638 ira_may_move_in_cost[mode][cl1][cl2] = cost;
1639
1640 if (ira_class_subset_p[cl2][cl1])
1641 ira_may_move_out_cost[mode][cl1][cl2] = 0;
1642 else
1643 ira_may_move_out_cost[mode][cl1][cl2] = cost;
1644 }
1645 }
1646 }
1647
1648
1649
1650 /* This is called once during compiler work. It sets up
1651 different arrays whose values don't depend on the compiled
1652 function. */
1653 void
1654 ira_init_once (void)
1655 {
1656 ira_init_costs_once ();
1657 lra_init_once ();
1658
1659 ira_use_lra_p = targetm.lra_p ();
1660 }
1661
1662 /* Free ira_max_register_move_cost, ira_may_move_in_cost and
1663 ira_may_move_out_cost for each mode. */
1664 void
1665 target_ira_int::free_register_move_costs (void)
1666 {
1667 int mode, i;
1668
1669 /* Reset move_cost and friends, making sure we only free shared
1670 table entries once. */
1671 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1672 if (x_ira_register_move_cost[mode])
1673 {
1674 for (i = 0;
1675 i < mode && (x_ira_register_move_cost[i]
1676 != x_ira_register_move_cost[mode]);
1677 i++)
1678 ;
1679 if (i == mode)
1680 {
1681 free (x_ira_register_move_cost[mode]);
1682 free (x_ira_may_move_in_cost[mode]);
1683 free (x_ira_may_move_out_cost[mode]);
1684 }
1685 }
1686 memset (x_ira_register_move_cost, 0, sizeof x_ira_register_move_cost);
1687 memset (x_ira_may_move_in_cost, 0, sizeof x_ira_may_move_in_cost);
1688 memset (x_ira_may_move_out_cost, 0, sizeof x_ira_may_move_out_cost);
1689 last_mode_for_init_move_cost = -1;
1690 }
1691
1692 target_ira_int::~target_ira_int ()
1693 {
1694 free_ira_costs ();
1695 free_register_move_costs ();
1696 }
1697
1698 /* This is called every time when register related information is
1699 changed. */
1700 void
1701 ira_init (void)
1702 {
1703 this_target_ira_int->free_register_move_costs ();
1704 setup_reg_mode_hard_regset ();
1705 setup_alloc_regs (flag_omit_frame_pointer != 0);
1706 setup_class_subset_and_memory_move_costs ();
1707 setup_reg_class_nregs ();
1708 setup_prohibited_and_exclude_class_mode_regs ();
1709 find_reg_classes ();
1710 clarify_prohibited_class_mode_regs ();
1711 setup_hard_regno_aclass ();
1712 ira_init_costs ();
1713 }
1714
1715
1716 #define ira_prohibited_mode_move_regs_initialized_p \
1717 (this_target_ira_int->x_ira_prohibited_mode_move_regs_initialized_p)
1718
1719 /* Set up IRA_PROHIBITED_MODE_MOVE_REGS. */
1720 static void
1721 setup_prohibited_mode_move_regs (void)
1722 {
1723 int i, j;
1724 rtx test_reg1, test_reg2, move_pat;
1725 rtx_insn *move_insn;
1726
1727 if (ira_prohibited_mode_move_regs_initialized_p)
1728 return;
1729 ira_prohibited_mode_move_regs_initialized_p = true;
1730 test_reg1 = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
1731 test_reg2 = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 2);
1732 move_pat = gen_rtx_SET (test_reg1, test_reg2);
1733 move_insn = gen_rtx_INSN (VOIDmode, 0, 0, 0, move_pat, 0, -1, 0);
1734 for (i = 0; i < NUM_MACHINE_MODES; i++)
1735 {
1736 SET_HARD_REG_SET (ira_prohibited_mode_move_regs[i]);
1737 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1738 {
1739 if (!targetm.hard_regno_mode_ok (j, (machine_mode) i))
1740 continue;
1741 set_mode_and_regno (test_reg1, (machine_mode) i, j);
1742 set_mode_and_regno (test_reg2, (machine_mode) i, j);
1743 INSN_CODE (move_insn) = -1;
1744 recog_memoized (move_insn);
1745 if (INSN_CODE (move_insn) < 0)
1746 continue;
1747 extract_insn (move_insn);
1748 /* We don't know whether the move will be in code that is optimized
1749 for size or speed, so consider all enabled alternatives. */
1750 if (! constrain_operands (1, get_enabled_alternatives (move_insn)))
1751 continue;
1752 CLEAR_HARD_REG_BIT (ira_prohibited_mode_move_regs[i], j);
1753 }
1754 }
1755 }
1756
1757
1758
1759 /* Extract INSN and return the set of alternatives that we should consider.
1760 This excludes any alternatives whose constraints are obviously impossible
1761 to meet (e.g. because the constraint requires a constant and the operand
1762 is nonconstant). It also excludes alternatives that are bound to need
1763 a spill or reload, as long as we have other alternatives that match
1764 exactly. */
1765 alternative_mask
1766 ira_setup_alts (rtx_insn *insn)
1767 {
1768 int nop, nalt;
1769 bool curr_swapped;
1770 const char *p;
1771 int commutative = -1;
1772
1773 extract_insn (insn);
1774 preprocess_constraints (insn);
1775 alternative_mask preferred = get_preferred_alternatives (insn);
1776 alternative_mask alts = 0;
1777 alternative_mask exact_alts = 0;
1778 /* Check that the hard reg set is enough for holding all
1779 alternatives. It is hard to imagine the situation when the
1780 assertion is wrong. */
1781 ira_assert (recog_data.n_alternatives
1782 <= (int) MAX (sizeof (HARD_REG_ELT_TYPE) * CHAR_BIT,
1783 FIRST_PSEUDO_REGISTER));
1784 for (nop = 0; nop < recog_data.n_operands; nop++)
1785 if (recog_data.constraints[nop][0] == '%')
1786 {
1787 commutative = nop;
1788 break;
1789 }
1790 for (curr_swapped = false;; curr_swapped = true)
1791 {
1792 for (nalt = 0; nalt < recog_data.n_alternatives; nalt++)
1793 {
1794 if (!TEST_BIT (preferred, nalt) || TEST_BIT (exact_alts, nalt))
1795 continue;
1796
1797 const operand_alternative *op_alt
1798 = &recog_op_alt[nalt * recog_data.n_operands];
1799 int this_reject = 0;
1800 for (nop = 0; nop < recog_data.n_operands; nop++)
1801 {
1802 int c, len;
1803
1804 this_reject += op_alt[nop].reject;
1805
1806 rtx op = recog_data.operand[nop];
1807 p = op_alt[nop].constraint;
1808 if (*p == 0 || *p == ',')
1809 continue;
1810
1811 bool win_p = false;
1812 do
1813 switch (c = *p, len = CONSTRAINT_LEN (c, p), c)
1814 {
1815 case '#':
1816 case ',':
1817 c = '\0';
1818 /* FALLTHRU */
1819 case '\0':
1820 len = 0;
1821 break;
1822
1823 case '%':
1824 /* The commutative modifier is handled above. */
1825 break;
1826
1827 case '0': case '1': case '2': case '3': case '4':
1828 case '5': case '6': case '7': case '8': case '9':
1829 {
1830 char *end;
1831 unsigned long dup = strtoul (p, &end, 10);
1832 rtx other = recog_data.operand[dup];
1833 len = end - p;
1834 if (MEM_P (other)
1835 ? rtx_equal_p (other, op)
1836 : REG_P (op) || SUBREG_P (op))
1837 goto op_success;
1838 win_p = true;
1839 }
1840 break;
1841
1842 case 'g':
1843 goto op_success;
1844 break;
1845
1846 default:
1847 {
1848 enum constraint_num cn = lookup_constraint (p);
1849 rtx mem = NULL;
1850 switch (get_constraint_type (cn))
1851 {
1852 case CT_REGISTER:
1853 if (reg_class_for_constraint (cn) != NO_REGS)
1854 {
1855 if (REG_P (op) || SUBREG_P (op))
1856 goto op_success;
1857 win_p = true;
1858 }
1859 break;
1860
1861 case CT_CONST_INT:
1862 if (CONST_INT_P (op)
1863 && (insn_const_int_ok_for_constraint
1864 (INTVAL (op), cn)))
1865 goto op_success;
1866 break;
1867
1868 case CT_ADDRESS:
1869 goto op_success;
1870
1871 case CT_MEMORY:
1872 case CT_RELAXED_MEMORY:
1873 mem = op;
1874 /* Fall through. */
1875 case CT_SPECIAL_MEMORY:
1876 if (!mem)
1877 mem = extract_mem_from_operand (op);
1878 if (MEM_P (mem))
1879 goto op_success;
1880 win_p = true;
1881 break;
1882
1883 case CT_FIXED_FORM:
1884 if (constraint_satisfied_p (op, cn))
1885 goto op_success;
1886 break;
1887 }
1888 break;
1889 }
1890 }
1891 while (p += len, c);
1892 if (!win_p)
1893 break;
1894 /* We can make the alternative match by spilling a register
1895 to memory or loading something into a register. Count a
1896 cost of one reload (the equivalent of the '?' constraint). */
1897 this_reject += 6;
1898 op_success:
1899 ;
1900 }
1901
1902 if (nop >= recog_data.n_operands)
1903 {
1904 alts |= ALTERNATIVE_BIT (nalt);
1905 if (this_reject == 0)
1906 exact_alts |= ALTERNATIVE_BIT (nalt);
1907 }
1908 }
1909 if (commutative < 0)
1910 break;
1911 /* Swap forth and back to avoid changing recog_data. */
1912 std::swap (recog_data.operand[commutative],
1913 recog_data.operand[commutative + 1]);
1914 if (curr_swapped)
1915 break;
1916 }
1917 return exact_alts ? exact_alts : alts;
1918 }
1919
1920 /* Return the number of the output non-early clobber operand which
1921 should be the same in any case as operand with number OP_NUM (or
1922 negative value if there is no such operand). ALTS is the mask
1923 of alternatives that we should consider. SINGLE_INPUT_OP_HAS_CSTR_P
1924 should be set in this function, it indicates whether there is only
1925 a single input operand which has the matching constraint on the
1926 output operand at the position specified in return value. If the
1927 pattern allows any one of several input operands holds the matching
1928 constraint, it's set as false, one typical case is destructive FMA
1929 instruction on target rs6000. Note that for a non-NO_REG preferred
1930 register class with no free register move copy, if the parameter
1931 PARAM_IRA_CONSIDER_DUP_IN_ALL_ALTS is set to one, this function
1932 will check all available alternatives for matching constraints,
1933 even if it has found or will find one alternative with non-NO_REG
1934 regclass, it can respect more cases with matching constraints. If
1935 PARAM_IRA_CONSIDER_DUP_IN_ALL_ALTS is set to zero,
1936 SINGLE_INPUT_OP_HAS_CSTR_P is always true, it will stop to find
1937 matching constraint relationship once it hits some alternative with
1938 some non-NO_REG regclass. */
1939 int
1940 ira_get_dup_out_num (int op_num, alternative_mask alts,
1941 bool &single_input_op_has_cstr_p)
1942 {
1943 int curr_alt, c, original;
1944 bool ignore_p, use_commut_op_p;
1945 const char *str;
1946
1947 if (op_num < 0 || recog_data.n_alternatives == 0)
1948 return -1;
1949 /* We should find duplications only for input operands. */
1950 if (recog_data.operand_type[op_num] != OP_IN)
1951 return -1;
1952 str = recog_data.constraints[op_num];
1953 use_commut_op_p = false;
1954 single_input_op_has_cstr_p = true;
1955
1956 rtx op = recog_data.operand[op_num];
1957 int op_regno = reg_or_subregno (op);
1958 enum reg_class op_pref_cl = reg_preferred_class (op_regno);
1959 machine_mode op_mode = GET_MODE (op);
1960
1961 ira_init_register_move_cost_if_necessary (op_mode);
1962 /* If the preferred regclass isn't NO_REG, continue to find the matching
1963 constraint in all available alternatives with preferred regclass, even
1964 if we have found or will find one alternative whose constraint stands
1965 for a REG (non-NO_REG) regclass. Note that it would be fine not to
1966 respect matching constraint if the register copy is free, so exclude
1967 it. */
1968 bool respect_dup_despite_reg_cstr
1969 = param_ira_consider_dup_in_all_alts
1970 && op_pref_cl != NO_REGS
1971 && ira_register_move_cost[op_mode][op_pref_cl][op_pref_cl] > 0;
1972
1973 /* Record the alternative whose constraint uses the same regclass as the
1974 preferred regclass, later if we find one matching constraint for this
1975 operand with preferred reclass, we will visit these recorded
1976 alternatives to check whether if there is one alternative in which no
1977 any INPUT operands have one matching constraint same as our candidate.
1978 If yes, it means there is one alternative which is perfectly fine
1979 without satisfying this matching constraint. If no, it means in any
1980 alternatives there is one other INPUT operand holding this matching
1981 constraint, it's fine to respect this matching constraint and further
1982 create this constraint copy since it would become harmless once some
1983 other takes preference and it's interfered. */
1984 alternative_mask pref_cl_alts;
1985
1986 for (;;)
1987 {
1988 pref_cl_alts = 0;
1989
1990 for (curr_alt = 0, ignore_p = !TEST_BIT (alts, curr_alt),
1991 original = -1;;)
1992 {
1993 c = *str;
1994 if (c == '\0')
1995 break;
1996 if (c == '#')
1997 ignore_p = true;
1998 else if (c == ',')
1999 {
2000 curr_alt++;
2001 ignore_p = !TEST_BIT (alts, curr_alt);
2002 }
2003 else if (! ignore_p)
2004 switch (c)
2005 {
2006 case 'g':
2007 goto fail;
2008 default:
2009 {
2010 enum constraint_num cn = lookup_constraint (str);
2011 enum reg_class cl = reg_class_for_constraint (cn);
2012 if (cl != NO_REGS && !targetm.class_likely_spilled_p (cl))
2013 {
2014 if (respect_dup_despite_reg_cstr)
2015 {
2016 /* If it's free to move from one preferred class to
2017 the one without matching constraint, it doesn't
2018 have to respect this constraint with costs. */
2019 if (cl != op_pref_cl
2020 && (ira_reg_class_intersect[cl][op_pref_cl]
2021 != NO_REGS)
2022 && (ira_may_move_in_cost[op_mode][op_pref_cl][cl]
2023 == 0))
2024 goto fail;
2025 else if (cl == op_pref_cl)
2026 pref_cl_alts |= ALTERNATIVE_BIT (curr_alt);
2027 }
2028 else
2029 goto fail;
2030 }
2031 if (constraint_satisfied_p (op, cn))
2032 goto fail;
2033 break;
2034 }
2035
2036 case '0': case '1': case '2': case '3': case '4':
2037 case '5': case '6': case '7': case '8': case '9':
2038 {
2039 char *end;
2040 int n = (int) strtoul (str, &end, 10);
2041 str = end;
2042 if (original != -1 && original != n)
2043 goto fail;
2044 gcc_assert (n < recog_data.n_operands);
2045 if (respect_dup_despite_reg_cstr)
2046 {
2047 const operand_alternative *op_alt
2048 = &recog_op_alt[curr_alt * recog_data.n_operands];
2049 /* Only respect the one with preferred rclass, without
2050 respect_dup_despite_reg_cstr it's possible to get
2051 one whose regclass isn't preferred first before,
2052 but it would fail since there should be other
2053 alternatives with preferred regclass. */
2054 if (op_alt[n].cl == op_pref_cl)
2055 original = n;
2056 }
2057 else
2058 original = n;
2059 continue;
2060 }
2061 }
2062 str += CONSTRAINT_LEN (c, str);
2063 }
2064 if (original == -1)
2065 goto fail;
2066 if (recog_data.operand_type[original] == OP_OUT)
2067 {
2068 if (pref_cl_alts == 0)
2069 return original;
2070 /* Visit these recorded alternatives to check whether
2071 there is one alternative in which no any INPUT operands
2072 have one matching constraint same as our candidate.
2073 Give up this candidate if so. */
2074 int nop, nalt;
2075 for (nalt = 0; nalt < recog_data.n_alternatives; nalt++)
2076 {
2077 if (!TEST_BIT (pref_cl_alts, nalt))
2078 continue;
2079 const operand_alternative *op_alt
2080 = &recog_op_alt[nalt * recog_data.n_operands];
2081 bool dup_in_other = false;
2082 for (nop = 0; nop < recog_data.n_operands; nop++)
2083 {
2084 if (recog_data.operand_type[nop] != OP_IN)
2085 continue;
2086 if (nop == op_num)
2087 continue;
2088 if (op_alt[nop].matches == original)
2089 {
2090 dup_in_other = true;
2091 break;
2092 }
2093 }
2094 if (!dup_in_other)
2095 return -1;
2096 }
2097 single_input_op_has_cstr_p = false;
2098 return original;
2099 }
2100 fail:
2101 if (use_commut_op_p)
2102 break;
2103 use_commut_op_p = true;
2104 if (recog_data.constraints[op_num][0] == '%')
2105 str = recog_data.constraints[op_num + 1];
2106 else if (op_num > 0 && recog_data.constraints[op_num - 1][0] == '%')
2107 str = recog_data.constraints[op_num - 1];
2108 else
2109 break;
2110 }
2111 return -1;
2112 }
2113
2114
2115
2116 /* Search forward to see if the source register of a copy insn dies
2117 before either it or the destination register is modified, but don't
2118 scan past the end of the basic block. If so, we can replace the
2119 source with the destination and let the source die in the copy
2120 insn.
2121
2122 This will reduce the number of registers live in that range and may
2123 enable the destination and the source coalescing, thus often saving
2124 one register in addition to a register-register copy. */
2125
2126 static void
2127 decrease_live_ranges_number (void)
2128 {
2129 basic_block bb;
2130 rtx_insn *insn;
2131 rtx set, src, dest, dest_death, note;
2132 rtx_insn *p, *q;
2133 int sregno, dregno;
2134
2135 if (! flag_expensive_optimizations)
2136 return;
2137
2138 if (ira_dump_file)
2139 fprintf (ira_dump_file, "Starting decreasing number of live ranges...\n");
2140
2141 FOR_EACH_BB_FN (bb, cfun)
2142 FOR_BB_INSNS (bb, insn)
2143 {
2144 set = single_set (insn);
2145 if (! set)
2146 continue;
2147 src = SET_SRC (set);
2148 dest = SET_DEST (set);
2149 if (! REG_P (src) || ! REG_P (dest)
2150 || find_reg_note (insn, REG_DEAD, src))
2151 continue;
2152 sregno = REGNO (src);
2153 dregno = REGNO (dest);
2154
2155 /* We don't want to mess with hard regs if register classes
2156 are small. */
2157 if (sregno == dregno
2158 || (targetm.small_register_classes_for_mode_p (GET_MODE (src))
2159 && (sregno < FIRST_PSEUDO_REGISTER
2160 || dregno < FIRST_PSEUDO_REGISTER))
2161 /* We don't see all updates to SP if they are in an
2162 auto-inc memory reference, so we must disallow this
2163 optimization on them. */
2164 || sregno == STACK_POINTER_REGNUM
2165 || dregno == STACK_POINTER_REGNUM)
2166 continue;
2167
2168 dest_death = NULL_RTX;
2169
2170 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
2171 {
2172 if (! INSN_P (p))
2173 continue;
2174 if (BLOCK_FOR_INSN (p) != bb)
2175 break;
2176
2177 if (reg_set_p (src, p) || reg_set_p (dest, p)
2178 /* If SRC is an asm-declared register, it must not be
2179 replaced in any asm. Unfortunately, the REG_EXPR
2180 tree for the asm variable may be absent in the SRC
2181 rtx, so we can't check the actual register
2182 declaration easily (the asm operand will have it,
2183 though). To avoid complicating the test for a rare
2184 case, we just don't perform register replacement
2185 for a hard reg mentioned in an asm. */
2186 || (sregno < FIRST_PSEUDO_REGISTER
2187 && asm_noperands (PATTERN (p)) >= 0
2188 && reg_overlap_mentioned_p (src, PATTERN (p)))
2189 /* Don't change hard registers used by a call. */
2190 || (CALL_P (p) && sregno < FIRST_PSEUDO_REGISTER
2191 && find_reg_fusage (p, USE, src))
2192 /* Don't change a USE of a register. */
2193 || (GET_CODE (PATTERN (p)) == USE
2194 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
2195 break;
2196
2197 /* See if all of SRC dies in P. This test is slightly
2198 more conservative than it needs to be. */
2199 if ((note = find_regno_note (p, REG_DEAD, sregno))
2200 && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
2201 {
2202 int failed = 0;
2203
2204 /* We can do the optimization. Scan forward from INSN
2205 again, replacing regs as we go. Set FAILED if a
2206 replacement can't be done. In that case, we can't
2207 move the death note for SRC. This should be
2208 rare. */
2209
2210 /* Set to stop at next insn. */
2211 for (q = next_real_insn (insn);
2212 q != next_real_insn (p);
2213 q = next_real_insn (q))
2214 {
2215 if (reg_overlap_mentioned_p (src, PATTERN (q)))
2216 {
2217 /* If SRC is a hard register, we might miss
2218 some overlapping registers with
2219 validate_replace_rtx, so we would have to
2220 undo it. We can't if DEST is present in
2221 the insn, so fail in that combination of
2222 cases. */
2223 if (sregno < FIRST_PSEUDO_REGISTER
2224 && reg_mentioned_p (dest, PATTERN (q)))
2225 failed = 1;
2226
2227 /* Attempt to replace all uses. */
2228 else if (!validate_replace_rtx (src, dest, q))
2229 failed = 1;
2230
2231 /* If this succeeded, but some part of the
2232 register is still present, undo the
2233 replacement. */
2234 else if (sregno < FIRST_PSEUDO_REGISTER
2235 && reg_overlap_mentioned_p (src, PATTERN (q)))
2236 {
2237 validate_replace_rtx (dest, src, q);
2238 failed = 1;
2239 }
2240 }
2241
2242 /* If DEST dies here, remove the death note and
2243 save it for later. Make sure ALL of DEST dies
2244 here; again, this is overly conservative. */
2245 if (! dest_death
2246 && (dest_death = find_regno_note (q, REG_DEAD, dregno)))
2247 {
2248 if (GET_MODE (XEXP (dest_death, 0)) == GET_MODE (dest))
2249 remove_note (q, dest_death);
2250 else
2251 {
2252 failed = 1;
2253 dest_death = 0;
2254 }
2255 }
2256 }
2257
2258 if (! failed)
2259 {
2260 /* Move death note of SRC from P to INSN. */
2261 remove_note (p, note);
2262 XEXP (note, 1) = REG_NOTES (insn);
2263 REG_NOTES (insn) = note;
2264 }
2265
2266 /* DEST is also dead if INSN has a REG_UNUSED note for
2267 DEST. */
2268 if (! dest_death
2269 && (dest_death
2270 = find_regno_note (insn, REG_UNUSED, dregno)))
2271 {
2272 PUT_REG_NOTE_KIND (dest_death, REG_DEAD);
2273 remove_note (insn, dest_death);
2274 }
2275
2276 /* Put death note of DEST on P if we saw it die. */
2277 if (dest_death)
2278 {
2279 XEXP (dest_death, 1) = REG_NOTES (p);
2280 REG_NOTES (p) = dest_death;
2281 }
2282 break;
2283 }
2284
2285 /* If SRC is a hard register which is set or killed in
2286 some other way, we can't do this optimization. */
2287 else if (sregno < FIRST_PSEUDO_REGISTER && dead_or_set_p (p, src))
2288 break;
2289 }
2290 }
2291 }
2292
2293
2294
2295 /* Return nonzero if REGNO is a particularly bad choice for reloading X. */
2296 static bool
2297 ira_bad_reload_regno_1 (int regno, rtx x)
2298 {
2299 int x_regno, n, i;
2300 ira_allocno_t a;
2301 enum reg_class pref;
2302
2303 /* We only deal with pseudo regs. */
2304 if (! x || GET_CODE (x) != REG)
2305 return false;
2306
2307 x_regno = REGNO (x);
2308 if (x_regno < FIRST_PSEUDO_REGISTER)
2309 return false;
2310
2311 /* If the pseudo prefers REGNO explicitly, then do not consider
2312 REGNO a bad spill choice. */
2313 pref = reg_preferred_class (x_regno);
2314 if (reg_class_size[pref] == 1)
2315 return !TEST_HARD_REG_BIT (reg_class_contents[pref], regno);
2316
2317 /* If the pseudo conflicts with REGNO, then we consider REGNO a
2318 poor choice for a reload regno. */
2319 a = ira_regno_allocno_map[x_regno];
2320 n = ALLOCNO_NUM_OBJECTS (a);
2321 for (i = 0; i < n; i++)
2322 {
2323 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2324 if (TEST_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), regno))
2325 return true;
2326 }
2327 return false;
2328 }
2329
2330 /* Return nonzero if REGNO is a particularly bad choice for reloading
2331 IN or OUT. */
2332 bool
2333 ira_bad_reload_regno (int regno, rtx in, rtx out)
2334 {
2335 return (ira_bad_reload_regno_1 (regno, in)
2336 || ira_bad_reload_regno_1 (regno, out));
2337 }
2338
2339 /* Add register clobbers from asm statements. */
2340 static void
2341 compute_regs_asm_clobbered (void)
2342 {
2343 basic_block bb;
2344
2345 FOR_EACH_BB_FN (bb, cfun)
2346 {
2347 rtx_insn *insn;
2348 FOR_BB_INSNS_REVERSE (bb, insn)
2349 {
2350 df_ref def;
2351
2352 if (NONDEBUG_INSN_P (insn) && asm_noperands (PATTERN (insn)) >= 0)
2353 FOR_EACH_INSN_DEF (def, insn)
2354 {
2355 unsigned int dregno = DF_REF_REGNO (def);
2356 if (HARD_REGISTER_NUM_P (dregno))
2357 add_to_hard_reg_set (&crtl->asm_clobbers,
2358 GET_MODE (DF_REF_REAL_REG (def)),
2359 dregno);
2360 }
2361 }
2362 }
2363 }
2364
2365
2366 /* Set up ELIMINABLE_REGSET, IRA_NO_ALLOC_REGS, and
2367 REGS_EVER_LIVE. */
2368 void
2369 ira_setup_eliminable_regset (void)
2370 {
2371 int i;
2372 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
2373 int fp_reg_count = hard_regno_nregs (HARD_FRAME_POINTER_REGNUM, Pmode);
2374
2375 /* Setup is_leaf as frame_pointer_required may use it. This function
2376 is called by sched_init before ira if scheduling is enabled. */
2377 crtl->is_leaf = leaf_function_p ();
2378
2379 /* FIXME: If EXIT_IGNORE_STACK is set, we will not save and restore
2380 sp for alloca. So we can't eliminate the frame pointer in that
2381 case. At some point, we should improve this by emitting the
2382 sp-adjusting insns for this case. */
2383 frame_pointer_needed
2384 = (! flag_omit_frame_pointer
2385 || (cfun->calls_alloca && EXIT_IGNORE_STACK)
2386 /* We need the frame pointer to catch stack overflow exceptions if
2387 the stack pointer is moving (as for the alloca case just above). */
2388 || (STACK_CHECK_MOVING_SP
2389 && flag_stack_check
2390 && flag_exceptions
2391 && cfun->can_throw_non_call_exceptions)
2392 || crtl->accesses_prior_frames
2393 || (SUPPORTS_STACK_ALIGNMENT && crtl->stack_realign_needed)
2394 || targetm.frame_pointer_required ());
2395
2396 /* The chance that FRAME_POINTER_NEEDED is changed from inspecting
2397 RTL is very small. So if we use frame pointer for RA and RTL
2398 actually prevents this, we will spill pseudos assigned to the
2399 frame pointer in LRA. */
2400
2401 if (frame_pointer_needed)
2402 for (i = 0; i < fp_reg_count; i++)
2403 df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM + i, true);
2404
2405 ira_no_alloc_regs = no_unit_alloc_regs;
2406 CLEAR_HARD_REG_SET (eliminable_regset);
2407
2408 compute_regs_asm_clobbered ();
2409
2410 /* Build the regset of all eliminable registers and show we can't
2411 use those that we already know won't be eliminated. */
2412 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
2413 {
2414 bool cannot_elim
2415 = (! targetm.can_eliminate (eliminables[i].from, eliminables[i].to)
2416 || (eliminables[i].to == STACK_POINTER_REGNUM && frame_pointer_needed));
2417
2418 if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, eliminables[i].from))
2419 {
2420 SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
2421
2422 if (cannot_elim)
2423 SET_HARD_REG_BIT (ira_no_alloc_regs, eliminables[i].from);
2424 }
2425 else if (cannot_elim)
2426 error ("%s cannot be used in %<asm%> here",
2427 reg_names[eliminables[i].from]);
2428 else
2429 df_set_regs_ever_live (eliminables[i].from, true);
2430 }
2431 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
2432 {
2433 for (i = 0; i < fp_reg_count; i++)
2434 if (global_regs[HARD_FRAME_POINTER_REGNUM + i])
2435 /* Nothing to do: the register is already treated as live
2436 where appropriate, and cannot be eliminated. */
2437 ;
2438 else if (!TEST_HARD_REG_BIT (crtl->asm_clobbers,
2439 HARD_FRAME_POINTER_REGNUM + i))
2440 {
2441 SET_HARD_REG_BIT (eliminable_regset,
2442 HARD_FRAME_POINTER_REGNUM + i);
2443 if (frame_pointer_needed)
2444 SET_HARD_REG_BIT (ira_no_alloc_regs,
2445 HARD_FRAME_POINTER_REGNUM + i);
2446 }
2447 else if (frame_pointer_needed)
2448 error ("%s cannot be used in %<asm%> here",
2449 reg_names[HARD_FRAME_POINTER_REGNUM + i]);
2450 else
2451 df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM + i, true);
2452 }
2453 }
2454
2455
2456
2457 /* Vector of substitutions of register numbers,
2458 used to map pseudo regs into hardware regs.
2459 This is set up as a result of register allocation.
2460 Element N is the hard reg assigned to pseudo reg N,
2461 or is -1 if no hard reg was assigned.
2462 If N is a hard reg number, element N is N. */
2463 short *reg_renumber;
2464
2465 /* Set up REG_RENUMBER and CALLER_SAVE_NEEDED (used by reload) from
2466 the allocation found by IRA. */
2467 static void
2468 setup_reg_renumber (void)
2469 {
2470 int regno, hard_regno;
2471 ira_allocno_t a;
2472 ira_allocno_iterator ai;
2473
2474 caller_save_needed = 0;
2475 FOR_EACH_ALLOCNO (a, ai)
2476 {
2477 if (ira_use_lra_p && ALLOCNO_CAP_MEMBER (a) != NULL)
2478 continue;
2479 /* There are no caps at this point. */
2480 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
2481 if (! ALLOCNO_ASSIGNED_P (a))
2482 /* It can happen if A is not referenced but partially anticipated
2483 somewhere in a region. */
2484 ALLOCNO_ASSIGNED_P (a) = true;
2485 ira_free_allocno_updated_costs (a);
2486 hard_regno = ALLOCNO_HARD_REGNO (a);
2487 regno = ALLOCNO_REGNO (a);
2488 reg_renumber[regno] = (hard_regno < 0 ? -1 : hard_regno);
2489 if (hard_regno >= 0)
2490 {
2491 int i, nwords;
2492 enum reg_class pclass;
2493 ira_object_t obj;
2494
2495 pclass = ira_pressure_class_translate[REGNO_REG_CLASS (hard_regno)];
2496 nwords = ALLOCNO_NUM_OBJECTS (a);
2497 for (i = 0; i < nwords; i++)
2498 {
2499 obj = ALLOCNO_OBJECT (a, i);
2500 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj)
2501 |= ~reg_class_contents[pclass];
2502 }
2503 if (ira_need_caller_save_p (a, hard_regno))
2504 {
2505 ira_assert (!optimize || flag_caller_saves
2506 || (ALLOCNO_CALLS_CROSSED_NUM (a)
2507 == ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a))
2508 || regno >= ira_reg_equiv_len
2509 || ira_equiv_no_lvalue_p (regno));
2510 caller_save_needed = 1;
2511 }
2512 }
2513 }
2514 }
2515
2516 /* Set up allocno assignment flags for further allocation
2517 improvements. */
2518 static void
2519 setup_allocno_assignment_flags (void)
2520 {
2521 int hard_regno;
2522 ira_allocno_t a;
2523 ira_allocno_iterator ai;
2524
2525 FOR_EACH_ALLOCNO (a, ai)
2526 {
2527 if (! ALLOCNO_ASSIGNED_P (a))
2528 /* It can happen if A is not referenced but partially anticipated
2529 somewhere in a region. */
2530 ira_free_allocno_updated_costs (a);
2531 hard_regno = ALLOCNO_HARD_REGNO (a);
2532 /* Don't assign hard registers to allocnos which are destination
2533 of removed store at the end of loop. It has no sense to keep
2534 the same value in different hard registers. It is also
2535 impossible to assign hard registers correctly to such
2536 allocnos because the cost info and info about intersected
2537 calls are incorrect for them. */
2538 ALLOCNO_ASSIGNED_P (a) = (hard_regno >= 0
2539 || ALLOCNO_EMIT_DATA (a)->mem_optimized_dest_p
2540 || (ALLOCNO_MEMORY_COST (a)
2541 - ALLOCNO_CLASS_COST (a)) < 0);
2542 ira_assert
2543 (hard_regno < 0
2544 || ira_hard_reg_in_set_p (hard_regno, ALLOCNO_MODE (a),
2545 reg_class_contents[ALLOCNO_CLASS (a)]));
2546 }
2547 }
2548
2549 /* Evaluate overall allocation cost and the costs for using hard
2550 registers and memory for allocnos. */
2551 static void
2552 calculate_allocation_cost (void)
2553 {
2554 int hard_regno, cost;
2555 ira_allocno_t a;
2556 ira_allocno_iterator ai;
2557
2558 ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
2559 FOR_EACH_ALLOCNO (a, ai)
2560 {
2561 hard_regno = ALLOCNO_HARD_REGNO (a);
2562 ira_assert (hard_regno < 0
2563 || (ira_hard_reg_in_set_p
2564 (hard_regno, ALLOCNO_MODE (a),
2565 reg_class_contents[ALLOCNO_CLASS (a)])));
2566 if (hard_regno < 0)
2567 {
2568 cost = ALLOCNO_MEMORY_COST (a);
2569 ira_mem_cost += cost;
2570 }
2571 else if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
2572 {
2573 cost = (ALLOCNO_HARD_REG_COSTS (a)
2574 [ira_class_hard_reg_index
2575 [ALLOCNO_CLASS (a)][hard_regno]]);
2576 ira_reg_cost += cost;
2577 }
2578 else
2579 {
2580 cost = ALLOCNO_CLASS_COST (a);
2581 ira_reg_cost += cost;
2582 }
2583 ira_overall_cost += cost;
2584 }
2585
2586 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
2587 {
2588 fprintf (ira_dump_file,
2589 "+++Costs: overall %" PRId64
2590 ", reg %" PRId64
2591 ", mem %" PRId64
2592 ", ld %" PRId64
2593 ", st %" PRId64
2594 ", move %" PRId64,
2595 ira_overall_cost, ira_reg_cost, ira_mem_cost,
2596 ira_load_cost, ira_store_cost, ira_shuffle_cost);
2597 fprintf (ira_dump_file, "\n+++ move loops %d, new jumps %d\n",
2598 ira_move_loops_num, ira_additional_jumps_num);
2599 }
2600
2601 }
2602
2603 #ifdef ENABLE_IRA_CHECKING
2604 /* Check the correctness of the allocation. We do need this because
2605 of complicated code to transform more one region internal
2606 representation into one region representation. */
2607 static void
2608 check_allocation (void)
2609 {
2610 ira_allocno_t a;
2611 int hard_regno, nregs, conflict_nregs;
2612 ira_allocno_iterator ai;
2613
2614 FOR_EACH_ALLOCNO (a, ai)
2615 {
2616 int n = ALLOCNO_NUM_OBJECTS (a);
2617 int i;
2618
2619 if (ALLOCNO_CAP_MEMBER (a) != NULL
2620 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
2621 continue;
2622 nregs = hard_regno_nregs (hard_regno, ALLOCNO_MODE (a));
2623 if (nregs == 1)
2624 /* We allocated a single hard register. */
2625 n = 1;
2626 else if (n > 1)
2627 /* We allocated multiple hard registers, and we will test
2628 conflicts in a granularity of single hard regs. */
2629 nregs = 1;
2630
2631 for (i = 0; i < n; i++)
2632 {
2633 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2634 ira_object_t conflict_obj;
2635 ira_object_conflict_iterator oci;
2636 int this_regno = hard_regno;
2637 if (n > 1)
2638 {
2639 if (REG_WORDS_BIG_ENDIAN)
2640 this_regno += n - i - 1;
2641 else
2642 this_regno += i;
2643 }
2644 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2645 {
2646 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2647 int conflict_hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
2648 if (conflict_hard_regno < 0)
2649 continue;
2650 if (ira_soft_conflict (a, conflict_a))
2651 continue;
2652
2653 conflict_nregs = hard_regno_nregs (conflict_hard_regno,
2654 ALLOCNO_MODE (conflict_a));
2655
2656 if (ALLOCNO_NUM_OBJECTS (conflict_a) > 1
2657 && conflict_nregs == ALLOCNO_NUM_OBJECTS (conflict_a))
2658 {
2659 if (REG_WORDS_BIG_ENDIAN)
2660 conflict_hard_regno += (ALLOCNO_NUM_OBJECTS (conflict_a)
2661 - OBJECT_SUBWORD (conflict_obj) - 1);
2662 else
2663 conflict_hard_regno += OBJECT_SUBWORD (conflict_obj);
2664 conflict_nregs = 1;
2665 }
2666
2667 if ((conflict_hard_regno <= this_regno
2668 && this_regno < conflict_hard_regno + conflict_nregs)
2669 || (this_regno <= conflict_hard_regno
2670 && conflict_hard_regno < this_regno + nregs))
2671 {
2672 fprintf (stderr, "bad allocation for %d and %d\n",
2673 ALLOCNO_REGNO (a), ALLOCNO_REGNO (conflict_a));
2674 gcc_unreachable ();
2675 }
2676 }
2677 }
2678 }
2679 }
2680 #endif
2681
2682 /* Allocate REG_EQUIV_INIT. Set up it from IRA_REG_EQUIV which should
2683 be already calculated. */
2684 static void
2685 setup_reg_equiv_init (void)
2686 {
2687 int i;
2688 int max_regno = max_reg_num ();
2689
2690 for (i = 0; i < max_regno; i++)
2691 reg_equiv_init (i) = ira_reg_equiv[i].init_insns;
2692 }
2693
2694 /* Update equiv regno from movement of FROM_REGNO to TO_REGNO. INSNS
2695 are insns which were generated for such movement. It is assumed
2696 that FROM_REGNO and TO_REGNO always have the same value at the
2697 point of any move containing such registers. This function is used
2698 to update equiv info for register shuffles on the region borders
2699 and for caller save/restore insns. */
2700 void
2701 ira_update_equiv_info_by_shuffle_insn (int to_regno, int from_regno, rtx_insn *insns)
2702 {
2703 rtx_insn *insn;
2704 rtx x, note;
2705
2706 if (! ira_reg_equiv[from_regno].defined_p
2707 && (! ira_reg_equiv[to_regno].defined_p
2708 || ((x = ira_reg_equiv[to_regno].memory) != NULL_RTX
2709 && ! MEM_READONLY_P (x))))
2710 return;
2711 insn = insns;
2712 if (NEXT_INSN (insn) != NULL_RTX)
2713 {
2714 if (! ira_reg_equiv[to_regno].defined_p)
2715 {
2716 ira_assert (ira_reg_equiv[to_regno].init_insns == NULL_RTX);
2717 return;
2718 }
2719 ira_reg_equiv[to_regno].defined_p = false;
2720 ira_reg_equiv[to_regno].caller_save_p = false;
2721 ira_reg_equiv[to_regno].memory
2722 = ira_reg_equiv[to_regno].constant
2723 = ira_reg_equiv[to_regno].invariant
2724 = ira_reg_equiv[to_regno].init_insns = NULL;
2725 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2726 fprintf (ira_dump_file,
2727 " Invalidating equiv info for reg %d\n", to_regno);
2728 return;
2729 }
2730 /* It is possible that FROM_REGNO still has no equivalence because
2731 in shuffles to_regno<-from_regno and from_regno<-to_regno the 2nd
2732 insn was not processed yet. */
2733 if (ira_reg_equiv[from_regno].defined_p)
2734 {
2735 ira_reg_equiv[to_regno].defined_p = true;
2736 if ((x = ira_reg_equiv[from_regno].memory) != NULL_RTX)
2737 {
2738 ira_assert (ira_reg_equiv[from_regno].invariant == NULL_RTX
2739 && ira_reg_equiv[from_regno].constant == NULL_RTX);
2740 ira_assert (ira_reg_equiv[to_regno].memory == NULL_RTX
2741 || rtx_equal_p (ira_reg_equiv[to_regno].memory, x));
2742 ira_reg_equiv[to_regno].memory = x;
2743 if (! MEM_READONLY_P (x))
2744 /* We don't add the insn to insn init list because memory
2745 equivalence is just to say what memory is better to use
2746 when the pseudo is spilled. */
2747 return;
2748 }
2749 else if ((x = ira_reg_equiv[from_regno].constant) != NULL_RTX)
2750 {
2751 ira_assert (ira_reg_equiv[from_regno].invariant == NULL_RTX);
2752 ira_assert (ira_reg_equiv[to_regno].constant == NULL_RTX
2753 || rtx_equal_p (ira_reg_equiv[to_regno].constant, x));
2754 ira_reg_equiv[to_regno].constant = x;
2755 }
2756 else
2757 {
2758 x = ira_reg_equiv[from_regno].invariant;
2759 ira_assert (x != NULL_RTX);
2760 ira_assert (ira_reg_equiv[to_regno].invariant == NULL_RTX
2761 || rtx_equal_p (ira_reg_equiv[to_regno].invariant, x));
2762 ira_reg_equiv[to_regno].invariant = x;
2763 }
2764 if (find_reg_note (insn, REG_EQUIV, x) == NULL_RTX)
2765 {
2766 note = set_unique_reg_note (insn, REG_EQUIV, copy_rtx (x));
2767 gcc_assert (note != NULL_RTX);
2768 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2769 {
2770 fprintf (ira_dump_file,
2771 " Adding equiv note to insn %u for reg %d ",
2772 INSN_UID (insn), to_regno);
2773 dump_value_slim (ira_dump_file, x, 1);
2774 fprintf (ira_dump_file, "\n");
2775 }
2776 }
2777 }
2778 ira_reg_equiv[to_regno].init_insns
2779 = gen_rtx_INSN_LIST (VOIDmode, insn,
2780 ira_reg_equiv[to_regno].init_insns);
2781 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2782 fprintf (ira_dump_file,
2783 " Adding equiv init move insn %u to reg %d\n",
2784 INSN_UID (insn), to_regno);
2785 }
2786
2787 /* Fix values of array REG_EQUIV_INIT after live range splitting done
2788 by IRA. */
2789 static void
2790 fix_reg_equiv_init (void)
2791 {
2792 int max_regno = max_reg_num ();
2793 int i, new_regno, max;
2794 rtx set;
2795 rtx_insn_list *x, *next, *prev;
2796 rtx_insn *insn;
2797
2798 if (max_regno_before_ira < max_regno)
2799 {
2800 max = vec_safe_length (reg_equivs);
2801 grow_reg_equivs ();
2802 for (i = FIRST_PSEUDO_REGISTER; i < max; i++)
2803 for (prev = NULL, x = reg_equiv_init (i);
2804 x != NULL_RTX;
2805 x = next)
2806 {
2807 next = x->next ();
2808 insn = x->insn ();
2809 set = single_set (insn);
2810 ira_assert (set != NULL_RTX
2811 && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))));
2812 if (REG_P (SET_DEST (set))
2813 && ((int) REGNO (SET_DEST (set)) == i
2814 || (int) ORIGINAL_REGNO (SET_DEST (set)) == i))
2815 new_regno = REGNO (SET_DEST (set));
2816 else if (REG_P (SET_SRC (set))
2817 && ((int) REGNO (SET_SRC (set)) == i
2818 || (int) ORIGINAL_REGNO (SET_SRC (set)) == i))
2819 new_regno = REGNO (SET_SRC (set));
2820 else
2821 gcc_unreachable ();
2822 if (new_regno == i)
2823 prev = x;
2824 else
2825 {
2826 /* Remove the wrong list element. */
2827 if (prev == NULL_RTX)
2828 reg_equiv_init (i) = next;
2829 else
2830 XEXP (prev, 1) = next;
2831 XEXP (x, 1) = reg_equiv_init (new_regno);
2832 reg_equiv_init (new_regno) = x;
2833 }
2834 }
2835 }
2836 }
2837
2838 #ifdef ENABLE_IRA_CHECKING
2839 /* Print redundant memory-memory copies. */
2840 static void
2841 print_redundant_copies (void)
2842 {
2843 int hard_regno;
2844 ira_allocno_t a;
2845 ira_copy_t cp, next_cp;
2846 ira_allocno_iterator ai;
2847
2848 FOR_EACH_ALLOCNO (a, ai)
2849 {
2850 if (ALLOCNO_CAP_MEMBER (a) != NULL)
2851 /* It is a cap. */
2852 continue;
2853 hard_regno = ALLOCNO_HARD_REGNO (a);
2854 if (hard_regno >= 0)
2855 continue;
2856 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2857 if (cp->first == a)
2858 next_cp = cp->next_first_allocno_copy;
2859 else
2860 {
2861 next_cp = cp->next_second_allocno_copy;
2862 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL
2863 && cp->insn != NULL_RTX
2864 && ALLOCNO_HARD_REGNO (cp->first) == hard_regno)
2865 fprintf (ira_dump_file,
2866 " Redundant move from %d(freq %d):%d\n",
2867 INSN_UID (cp->insn), cp->freq, hard_regno);
2868 }
2869 }
2870 }
2871 #endif
2872
2873 /* Setup preferred and alternative classes for new pseudo-registers
2874 created by IRA starting with START. */
2875 static void
2876 setup_preferred_alternate_classes_for_new_pseudos (int start)
2877 {
2878 int i, old_regno;
2879 int max_regno = max_reg_num ();
2880
2881 for (i = start; i < max_regno; i++)
2882 {
2883 old_regno = ORIGINAL_REGNO (regno_reg_rtx[i]);
2884 ira_assert (i != old_regno);
2885 setup_reg_classes (i, reg_preferred_class (old_regno),
2886 reg_alternate_class (old_regno),
2887 reg_allocno_class (old_regno));
2888 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2889 fprintf (ira_dump_file,
2890 " New r%d: setting preferred %s, alternative %s\n",
2891 i, reg_class_names[reg_preferred_class (old_regno)],
2892 reg_class_names[reg_alternate_class (old_regno)]);
2893 }
2894 }
2895
2896
2897 /* The number of entries allocated in reg_info. */
2898 static int allocated_reg_info_size;
2899
2900 /* Regional allocation can create new pseudo-registers. This function
2901 expands some arrays for pseudo-registers. */
2902 static void
2903 expand_reg_info (void)
2904 {
2905 int i;
2906 int size = max_reg_num ();
2907
2908 resize_reg_info ();
2909 for (i = allocated_reg_info_size; i < size; i++)
2910 setup_reg_classes (i, GENERAL_REGS, ALL_REGS, GENERAL_REGS);
2911 setup_preferred_alternate_classes_for_new_pseudos (allocated_reg_info_size);
2912 allocated_reg_info_size = size;
2913 }
2914
2915 /* Return TRUE if there is too high register pressure in the function.
2916 It is used to decide when stack slot sharing is worth to do. */
2917 static bool
2918 too_high_register_pressure_p (void)
2919 {
2920 int i;
2921 enum reg_class pclass;
2922
2923 for (i = 0; i < ira_pressure_classes_num; i++)
2924 {
2925 pclass = ira_pressure_classes[i];
2926 if (ira_loop_tree_root->reg_pressure[pclass] > 10000)
2927 return true;
2928 }
2929 return false;
2930 }
2931
2932
2933
2934 /* Indicate that hard register number FROM was eliminated and replaced with
2935 an offset from hard register number TO. The status of hard registers live
2936 at the start of a basic block is updated by replacing a use of FROM with
2937 a use of TO. */
2938
2939 void
2940 mark_elimination (int from, int to)
2941 {
2942 basic_block bb;
2943 bitmap r;
2944
2945 FOR_EACH_BB_FN (bb, cfun)
2946 {
2947 r = DF_LR_IN (bb);
2948 if (bitmap_bit_p (r, from))
2949 {
2950 bitmap_clear_bit (r, from);
2951 bitmap_set_bit (r, to);
2952 }
2953 if (! df_live)
2954 continue;
2955 r = DF_LIVE_IN (bb);
2956 if (bitmap_bit_p (r, from))
2957 {
2958 bitmap_clear_bit (r, from);
2959 bitmap_set_bit (r, to);
2960 }
2961 }
2962 }
2963
2964
2965
2966 /* The length of the following array. */
2967 int ira_reg_equiv_len;
2968
2969 /* Info about equiv. info for each register. */
2970 struct ira_reg_equiv_s *ira_reg_equiv;
2971
2972 /* Expand ira_reg_equiv if necessary. */
2973 void
2974 ira_expand_reg_equiv (void)
2975 {
2976 int old = ira_reg_equiv_len;
2977
2978 if (ira_reg_equiv_len > max_reg_num ())
2979 return;
2980 ira_reg_equiv_len = max_reg_num () * 3 / 2 + 1;
2981 ira_reg_equiv
2982 = (struct ira_reg_equiv_s *) xrealloc (ira_reg_equiv,
2983 ira_reg_equiv_len
2984 * sizeof (struct ira_reg_equiv_s));
2985 gcc_assert (old < ira_reg_equiv_len);
2986 memset (ira_reg_equiv + old, 0,
2987 sizeof (struct ira_reg_equiv_s) * (ira_reg_equiv_len - old));
2988 }
2989
2990 static void
2991 init_reg_equiv (void)
2992 {
2993 ira_reg_equiv_len = 0;
2994 ira_reg_equiv = NULL;
2995 ira_expand_reg_equiv ();
2996 }
2997
2998 static void
2999 finish_reg_equiv (void)
3000 {
3001 free (ira_reg_equiv);
3002 }
3003
3004
3005
3006 struct equivalence
3007 {
3008 /* Set when a REG_EQUIV note is found or created. Use to
3009 keep track of what memory accesses might be created later,
3010 e.g. by reload. */
3011 rtx replacement;
3012 rtx *src_p;
3013
3014 /* The list of each instruction which initializes this register.
3015
3016 NULL indicates we know nothing about this register's equivalence
3017 properties.
3018
3019 An INSN_LIST with a NULL insn indicates this pseudo is already
3020 known to not have a valid equivalence. */
3021 rtx_insn_list *init_insns;
3022
3023 /* Loop depth is used to recognize equivalences which appear
3024 to be present within the same loop (or in an inner loop). */
3025 short loop_depth;
3026 /* Nonzero if this had a preexisting REG_EQUIV note. */
3027 unsigned char is_arg_equivalence : 1;
3028 /* Set when an attempt should be made to replace a register
3029 with the associated src_p entry. */
3030 unsigned char replace : 1;
3031 /* Set if this register has no known equivalence. */
3032 unsigned char no_equiv : 1;
3033 /* Set if this register is mentioned in a paradoxical subreg. */
3034 unsigned char pdx_subregs : 1;
3035 };
3036
3037 /* reg_equiv[N] (where N is a pseudo reg number) is the equivalence
3038 structure for that register. */
3039 static struct equivalence *reg_equiv;
3040
3041 /* Used for communication between the following two functions. */
3042 struct equiv_mem_data
3043 {
3044 /* A MEM that we wish to ensure remains unchanged. */
3045 rtx equiv_mem;
3046
3047 /* Set true if EQUIV_MEM is modified. */
3048 bool equiv_mem_modified;
3049 };
3050
3051 /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
3052 Called via note_stores. */
3053 static void
3054 validate_equiv_mem_from_store (rtx dest, const_rtx set ATTRIBUTE_UNUSED,
3055 void *data)
3056 {
3057 struct equiv_mem_data *info = (struct equiv_mem_data *) data;
3058
3059 if ((REG_P (dest)
3060 && reg_overlap_mentioned_p (dest, info->equiv_mem))
3061 || (MEM_P (dest)
3062 && anti_dependence (info->equiv_mem, dest)))
3063 info->equiv_mem_modified = true;
3064 }
3065
3066 static bool equiv_init_varies_p (rtx x);
3067
3068 enum valid_equiv { valid_none, valid_combine, valid_reload };
3069
3070 /* Verify that no store between START and the death of REG invalidates
3071 MEMREF. MEMREF is invalidated by modifying a register used in MEMREF,
3072 by storing into an overlapping memory location, or with a non-const
3073 CALL_INSN.
3074
3075 Return VALID_RELOAD if MEMREF remains valid for both reload and
3076 combine_and_move insns, VALID_COMBINE if only valid for
3077 combine_and_move_insns, and VALID_NONE otherwise. */
3078 static enum valid_equiv
3079 validate_equiv_mem (rtx_insn *start, rtx reg, rtx memref)
3080 {
3081 rtx_insn *insn;
3082 rtx note;
3083 struct equiv_mem_data info = { memref, false };
3084 enum valid_equiv ret = valid_reload;
3085
3086 /* If the memory reference has side effects or is volatile, it isn't a
3087 valid equivalence. */
3088 if (side_effects_p (memref))
3089 return valid_none;
3090
3091 for (insn = start; insn; insn = NEXT_INSN (insn))
3092 {
3093 if (!INSN_P (insn))
3094 continue;
3095
3096 if (find_reg_note (insn, REG_DEAD, reg))
3097 return ret;
3098
3099 if (CALL_P (insn))
3100 {
3101 /* We can combine a reg def from one insn into a reg use in
3102 another over a call if the memory is readonly or the call
3103 const/pure. However, we can't set reg_equiv notes up for
3104 reload over any call. The problem is the equivalent form
3105 may reference a pseudo which gets assigned a call
3106 clobbered hard reg. When we later replace REG with its
3107 equivalent form, the value in the call-clobbered reg has
3108 been changed and all hell breaks loose. */
3109 ret = valid_combine;
3110 if (!MEM_READONLY_P (memref)
3111 && (!RTL_CONST_OR_PURE_CALL_P (insn)
3112 || equiv_init_varies_p (XEXP (memref, 0))))
3113 return valid_none;
3114 }
3115
3116 note_stores (insn, validate_equiv_mem_from_store, &info);
3117 if (info.equiv_mem_modified)
3118 return valid_none;
3119
3120 /* If a register mentioned in MEMREF is modified via an
3121 auto-increment, we lose the equivalence. Do the same if one
3122 dies; although we could extend the life, it doesn't seem worth
3123 the trouble. */
3124
3125 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3126 if ((REG_NOTE_KIND (note) == REG_INC
3127 || REG_NOTE_KIND (note) == REG_DEAD)
3128 && REG_P (XEXP (note, 0))
3129 && reg_overlap_mentioned_p (XEXP (note, 0), memref))
3130 return valid_none;
3131 }
3132
3133 return valid_none;
3134 }
3135
3136 /* Returns false if X is known to be invariant. */
3137 static bool
3138 equiv_init_varies_p (rtx x)
3139 {
3140 RTX_CODE code = GET_CODE (x);
3141 int i;
3142 const char *fmt;
3143
3144 switch (code)
3145 {
3146 case MEM:
3147 return !MEM_READONLY_P (x) || equiv_init_varies_p (XEXP (x, 0));
3148
3149 case CONST:
3150 CASE_CONST_ANY:
3151 case SYMBOL_REF:
3152 case LABEL_REF:
3153 return false;
3154
3155 case REG:
3156 return reg_equiv[REGNO (x)].replace == 0 && rtx_varies_p (x, 0);
3157
3158 case ASM_OPERANDS:
3159 if (MEM_VOLATILE_P (x))
3160 return true;
3161
3162 /* Fall through. */
3163
3164 default:
3165 break;
3166 }
3167
3168 fmt = GET_RTX_FORMAT (code);
3169 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3170 if (fmt[i] == 'e')
3171 {
3172 if (equiv_init_varies_p (XEXP (x, i)))
3173 return true;
3174 }
3175 else if (fmt[i] == 'E')
3176 {
3177 int j;
3178 for (j = 0; j < XVECLEN (x, i); j++)
3179 if (equiv_init_varies_p (XVECEXP (x, i, j)))
3180 return true;
3181 }
3182
3183 return false;
3184 }
3185
3186 /* Returns true if X (used to initialize register REGNO) is movable.
3187 X is only movable if the registers it uses have equivalent initializations
3188 which appear to be within the same loop (or in an inner loop) and movable
3189 or if they are not candidates for local_alloc and don't vary. */
3190 static bool
3191 equiv_init_movable_p (rtx x, int regno)
3192 {
3193 int i, j;
3194 const char *fmt;
3195 enum rtx_code code = GET_CODE (x);
3196
3197 switch (code)
3198 {
3199 case SET:
3200 return equiv_init_movable_p (SET_SRC (x), regno);
3201
3202 case CLOBBER:
3203 return false;
3204
3205 case PRE_INC:
3206 case PRE_DEC:
3207 case POST_INC:
3208 case POST_DEC:
3209 case PRE_MODIFY:
3210 case POST_MODIFY:
3211 return false;
3212
3213 case REG:
3214 return ((reg_equiv[REGNO (x)].loop_depth >= reg_equiv[regno].loop_depth
3215 && reg_equiv[REGNO (x)].replace)
3216 || (REG_BASIC_BLOCK (REGNO (x)) < NUM_FIXED_BLOCKS
3217 && ! rtx_varies_p (x, 0)));
3218
3219 case UNSPEC_VOLATILE:
3220 return false;
3221
3222 case ASM_OPERANDS:
3223 if (MEM_VOLATILE_P (x))
3224 return false;
3225
3226 /* Fall through. */
3227
3228 default:
3229 break;
3230 }
3231
3232 fmt = GET_RTX_FORMAT (code);
3233 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3234 switch (fmt[i])
3235 {
3236 case 'e':
3237 if (! equiv_init_movable_p (XEXP (x, i), regno))
3238 return false;
3239 break;
3240 case 'E':
3241 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3242 if (! equiv_init_movable_p (XVECEXP (x, i, j), regno))
3243 return false;
3244 break;
3245 }
3246
3247 return true;
3248 }
3249
3250 static bool memref_referenced_p (rtx memref, rtx x, bool read_p);
3251
3252 /* Auxiliary function for memref_referenced_p. Process setting X for
3253 MEMREF store. */
3254 static bool
3255 process_set_for_memref_referenced_p (rtx memref, rtx x)
3256 {
3257 /* If we are setting a MEM, it doesn't count (its address does), but any
3258 other SET_DEST that has a MEM in it is referencing the MEM. */
3259 if (MEM_P (x))
3260 {
3261 if (memref_referenced_p (memref, XEXP (x, 0), true))
3262 return true;
3263 }
3264 else if (memref_referenced_p (memref, x, false))
3265 return true;
3266
3267 return false;
3268 }
3269
3270 /* TRUE if X references a memory location (as a read if READ_P) that
3271 would be affected by a store to MEMREF. */
3272 static bool
3273 memref_referenced_p (rtx memref, rtx x, bool read_p)
3274 {
3275 int i, j;
3276 const char *fmt;
3277 enum rtx_code code = GET_CODE (x);
3278
3279 switch (code)
3280 {
3281 case CONST:
3282 case LABEL_REF:
3283 case SYMBOL_REF:
3284 CASE_CONST_ANY:
3285 case PC:
3286 case HIGH:
3287 case LO_SUM:
3288 return false;
3289
3290 case REG:
3291 return (reg_equiv[REGNO (x)].replacement
3292 && memref_referenced_p (memref,
3293 reg_equiv[REGNO (x)].replacement, read_p));
3294
3295 case MEM:
3296 /* Memory X might have another effective type than MEMREF. */
3297 if (read_p || true_dependence (memref, VOIDmode, x))
3298 return true;
3299 break;
3300
3301 case SET:
3302 if (process_set_for_memref_referenced_p (memref, SET_DEST (x)))
3303 return true;
3304
3305 return memref_referenced_p (memref, SET_SRC (x), true);
3306
3307 case CLOBBER:
3308 if (process_set_for_memref_referenced_p (memref, XEXP (x, 0)))
3309 return true;
3310
3311 return false;
3312
3313 case PRE_DEC:
3314 case POST_DEC:
3315 case PRE_INC:
3316 case POST_INC:
3317 if (process_set_for_memref_referenced_p (memref, XEXP (x, 0)))
3318 return true;
3319
3320 return memref_referenced_p (memref, XEXP (x, 0), true);
3321
3322 case POST_MODIFY:
3323 case PRE_MODIFY:
3324 /* op0 = op0 + op1 */
3325 if (process_set_for_memref_referenced_p (memref, XEXP (x, 0)))
3326 return true;
3327
3328 if (memref_referenced_p (memref, XEXP (x, 0), true))
3329 return true;
3330
3331 return memref_referenced_p (memref, XEXP (x, 1), true);
3332
3333 default:
3334 break;
3335 }
3336
3337 fmt = GET_RTX_FORMAT (code);
3338 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3339 switch (fmt[i])
3340 {
3341 case 'e':
3342 if (memref_referenced_p (memref, XEXP (x, i), read_p))
3343 return true;
3344 break;
3345 case 'E':
3346 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3347 if (memref_referenced_p (memref, XVECEXP (x, i, j), read_p))
3348 return true;
3349 break;
3350 }
3351
3352 return false;
3353 }
3354
3355 /* TRUE if some insn in the range (START, END] references a memory location
3356 that would be affected by a store to MEMREF.
3357
3358 Callers should not call this routine if START is after END in the
3359 RTL chain. */
3360
3361 static bool
3362 memref_used_between_p (rtx memref, rtx_insn *start, rtx_insn *end)
3363 {
3364 rtx_insn *insn;
3365
3366 for (insn = NEXT_INSN (start);
3367 insn && insn != NEXT_INSN (end);
3368 insn = NEXT_INSN (insn))
3369 {
3370 if (!NONDEBUG_INSN_P (insn))
3371 continue;
3372
3373 if (memref_referenced_p (memref, PATTERN (insn), false))
3374 return true;
3375
3376 /* Nonconst functions may access memory. */
3377 if (CALL_P (insn) && (! RTL_CONST_CALL_P (insn)))
3378 return true;
3379 }
3380
3381 gcc_assert (insn == NEXT_INSN (end));
3382 return false;
3383 }
3384
3385 /* Mark REG as having no known equivalence.
3386 Some instructions might have been processed before and furnished
3387 with REG_EQUIV notes for this register; these notes will have to be
3388 removed.
3389 STORE is the piece of RTL that does the non-constant / conflicting
3390 assignment - a SET, CLOBBER or REG_INC note. It is currently not used,
3391 but needs to be there because this function is called from note_stores. */
3392 static void
3393 no_equiv (rtx reg, const_rtx store ATTRIBUTE_UNUSED,
3394 void *data ATTRIBUTE_UNUSED)
3395 {
3396 int regno;
3397 rtx_insn_list *list;
3398
3399 if (!REG_P (reg))
3400 return;
3401 regno = REGNO (reg);
3402 reg_equiv[regno].no_equiv = 1;
3403 list = reg_equiv[regno].init_insns;
3404 if (list && list->insn () == NULL)
3405 return;
3406 reg_equiv[regno].init_insns = gen_rtx_INSN_LIST (VOIDmode, NULL_RTX, NULL);
3407 reg_equiv[regno].replacement = NULL_RTX;
3408 /* This doesn't matter for equivalences made for argument registers, we
3409 should keep their initialization insns. */
3410 if (reg_equiv[regno].is_arg_equivalence)
3411 return;
3412 ira_reg_equiv[regno].defined_p = false;
3413 ira_reg_equiv[regno].caller_save_p = false;
3414 ira_reg_equiv[regno].init_insns = NULL;
3415 for (; list; list = list->next ())
3416 {
3417 rtx_insn *insn = list->insn ();
3418 remove_note (insn, find_reg_note (insn, REG_EQUIV, NULL_RTX));
3419 }
3420 }
3421
3422 /* Check whether the SUBREG is a paradoxical subreg and set the result
3423 in PDX_SUBREGS. */
3424
3425 static void
3426 set_paradoxical_subreg (rtx_insn *insn)
3427 {
3428 subrtx_iterator::array_type array;
3429 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
3430 {
3431 const_rtx subreg = *iter;
3432 if (GET_CODE (subreg) == SUBREG)
3433 {
3434 const_rtx reg = SUBREG_REG (subreg);
3435 if (REG_P (reg) && paradoxical_subreg_p (subreg))
3436 reg_equiv[REGNO (reg)].pdx_subregs = true;
3437 }
3438 }
3439 }
3440
3441 /* In DEBUG_INSN location adjust REGs from CLEARED_REGS bitmap to the
3442 equivalent replacement. */
3443
3444 static rtx
3445 adjust_cleared_regs (rtx loc, const_rtx old_rtx ATTRIBUTE_UNUSED, void *data)
3446 {
3447 if (REG_P (loc))
3448 {
3449 bitmap cleared_regs = (bitmap) data;
3450 if (bitmap_bit_p (cleared_regs, REGNO (loc)))
3451 return simplify_replace_fn_rtx (copy_rtx (*reg_equiv[REGNO (loc)].src_p),
3452 NULL_RTX, adjust_cleared_regs, data);
3453 }
3454 return NULL_RTX;
3455 }
3456
3457 /* Given register REGNO is set only once, return true if the defining
3458 insn dominates all uses. */
3459
3460 static bool
3461 def_dominates_uses (int regno)
3462 {
3463 df_ref def = DF_REG_DEF_CHAIN (regno);
3464
3465 struct df_insn_info *def_info = DF_REF_INSN_INFO (def);
3466 /* If this is an artificial def (eh handler regs, hard frame pointer
3467 for non-local goto, regs defined on function entry) then def_info
3468 is NULL and the reg is always live before any use. We might
3469 reasonably return true in that case, but since the only call
3470 of this function is currently here in ira.cc when we are looking
3471 at a defining insn we can't have an artificial def as that would
3472 bump DF_REG_DEF_COUNT. */
3473 gcc_assert (DF_REG_DEF_COUNT (regno) == 1 && def_info != NULL);
3474
3475 rtx_insn *def_insn = DF_REF_INSN (def);
3476 basic_block def_bb = BLOCK_FOR_INSN (def_insn);
3477
3478 for (df_ref use = DF_REG_USE_CHAIN (regno);
3479 use;
3480 use = DF_REF_NEXT_REG (use))
3481 {
3482 struct df_insn_info *use_info = DF_REF_INSN_INFO (use);
3483 /* Only check real uses, not artificial ones. */
3484 if (use_info)
3485 {
3486 rtx_insn *use_insn = DF_REF_INSN (use);
3487 if (!DEBUG_INSN_P (use_insn))
3488 {
3489 basic_block use_bb = BLOCK_FOR_INSN (use_insn);
3490 if (use_bb != def_bb
3491 ? !dominated_by_p (CDI_DOMINATORS, use_bb, def_bb)
3492 : DF_INSN_INFO_LUID (use_info) < DF_INSN_INFO_LUID (def_info))
3493 return false;
3494 }
3495 }
3496 }
3497 return true;
3498 }
3499
3500 /* Scan the instructions before update_equiv_regs. Record which registers
3501 are referenced as paradoxical subregs. Also check for cases in which
3502 the current function needs to save a register that one of its call
3503 instructions clobbers.
3504
3505 These things are logically unrelated, but it's more efficient to do
3506 them together. */
3507
3508 static void
3509 update_equiv_regs_prescan (void)
3510 {
3511 basic_block bb;
3512 rtx_insn *insn;
3513 function_abi_aggregator callee_abis;
3514
3515 FOR_EACH_BB_FN (bb, cfun)
3516 FOR_BB_INSNS (bb, insn)
3517 if (NONDEBUG_INSN_P (insn))
3518 {
3519 set_paradoxical_subreg (insn);
3520 if (CALL_P (insn))
3521 callee_abis.note_callee_abi (insn_callee_abi (insn));
3522 }
3523
3524 HARD_REG_SET extra_caller_saves = callee_abis.caller_save_regs (*crtl->abi);
3525 if (!hard_reg_set_empty_p (extra_caller_saves))
3526 for (unsigned int regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
3527 if (TEST_HARD_REG_BIT (extra_caller_saves, regno))
3528 df_set_regs_ever_live (regno, true);
3529 }
3530
3531 /* Find registers that are equivalent to a single value throughout the
3532 compilation (either because they can be referenced in memory or are
3533 set once from a single constant). Lower their priority for a
3534 register.
3535
3536 If such a register is only referenced once, try substituting its
3537 value into the using insn. If it succeeds, we can eliminate the
3538 register completely.
3539
3540 Initialize init_insns in ira_reg_equiv array. */
3541 static void
3542 update_equiv_regs (void)
3543 {
3544 rtx_insn *insn;
3545 basic_block bb;
3546
3547 /* Scan the insns and find which registers have equivalences. Do this
3548 in a separate scan of the insns because (due to -fcse-follow-jumps)
3549 a register can be set below its use. */
3550 bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
3551 FOR_EACH_BB_FN (bb, cfun)
3552 {
3553 int loop_depth = bb_loop_depth (bb);
3554
3555 for (insn = BB_HEAD (bb);
3556 insn != NEXT_INSN (BB_END (bb));
3557 insn = NEXT_INSN (insn))
3558 {
3559 rtx note;
3560 rtx set;
3561 rtx dest, src;
3562 int regno;
3563
3564 if (! INSN_P (insn))
3565 continue;
3566
3567 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3568 if (REG_NOTE_KIND (note) == REG_INC)
3569 no_equiv (XEXP (note, 0), note, NULL);
3570
3571 set = single_set (insn);
3572
3573 /* If this insn contains more (or less) than a single SET,
3574 only mark all destinations as having no known equivalence. */
3575 if (set == NULL_RTX
3576 || side_effects_p (SET_SRC (set)))
3577 {
3578 note_pattern_stores (PATTERN (insn), no_equiv, NULL);
3579 continue;
3580 }
3581 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3582 {
3583 int i;
3584
3585 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
3586 {
3587 rtx part = XVECEXP (PATTERN (insn), 0, i);
3588 if (part != set)
3589 note_pattern_stores (part, no_equiv, NULL);
3590 }
3591 }
3592
3593 dest = SET_DEST (set);
3594 src = SET_SRC (set);
3595
3596 /* See if this is setting up the equivalence between an argument
3597 register and its stack slot. */
3598 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
3599 if (note)
3600 {
3601 gcc_assert (REG_P (dest));
3602 regno = REGNO (dest);
3603
3604 /* Note that we don't want to clear init_insns in
3605 ira_reg_equiv even if there are multiple sets of this
3606 register. */
3607 reg_equiv[regno].is_arg_equivalence = 1;
3608
3609 /* The insn result can have equivalence memory although
3610 the equivalence is not set up by the insn. We add
3611 this insn to init insns as it is a flag for now that
3612 regno has an equivalence. We will remove the insn
3613 from init insn list later. */
3614 if (rtx_equal_p (src, XEXP (note, 0)) || MEM_P (XEXP (note, 0)))
3615 ira_reg_equiv[regno].init_insns
3616 = gen_rtx_INSN_LIST (VOIDmode, insn,
3617 ira_reg_equiv[regno].init_insns);
3618
3619 /* Continue normally in case this is a candidate for
3620 replacements. */
3621 }
3622
3623 if (!optimize)
3624 continue;
3625
3626 /* We only handle the case of a pseudo register being set
3627 once, or always to the same value. */
3628 /* ??? The mn10200 port breaks if we add equivalences for
3629 values that need an ADDRESS_REGS register and set them equivalent
3630 to a MEM of a pseudo. The actual problem is in the over-conservative
3631 handling of INPADDR_ADDRESS / INPUT_ADDRESS / INPUT triples in
3632 calculate_needs, but we traditionally work around this problem
3633 here by rejecting equivalences when the destination is in a register
3634 that's likely spilled. This is fragile, of course, since the
3635 preferred class of a pseudo depends on all instructions that set
3636 or use it. */
3637
3638 if (!REG_P (dest)
3639 || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
3640 || (reg_equiv[regno].init_insns
3641 && reg_equiv[regno].init_insns->insn () == NULL)
3642 || (targetm.class_likely_spilled_p (reg_preferred_class (regno))
3643 && MEM_P (src) && ! reg_equiv[regno].is_arg_equivalence))
3644 {
3645 /* This might be setting a SUBREG of a pseudo, a pseudo that is
3646 also set somewhere else to a constant. */
3647 note_pattern_stores (set, no_equiv, NULL);
3648 continue;
3649 }
3650
3651 /* Don't set reg mentioned in a paradoxical subreg
3652 equivalent to a mem. */
3653 if (MEM_P (src) && reg_equiv[regno].pdx_subregs)
3654 {
3655 note_pattern_stores (set, no_equiv, NULL);
3656 continue;
3657 }
3658
3659 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
3660
3661 /* cse sometimes generates function invariants, but doesn't put a
3662 REG_EQUAL note on the insn. Since this note would be redundant,
3663 there's no point creating it earlier than here. */
3664 if (! note && ! rtx_varies_p (src, 0))
3665 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
3666
3667 /* Don't bother considering a REG_EQUAL note containing an EXPR_LIST
3668 since it represents a function call. */
3669 if (note && GET_CODE (XEXP (note, 0)) == EXPR_LIST)
3670 note = NULL_RTX;
3671
3672 if (DF_REG_DEF_COUNT (regno) != 1)
3673 {
3674 bool equal_p = true;
3675 rtx_insn_list *list;
3676
3677 /* If we have already processed this pseudo and determined it
3678 cannot have an equivalence, then honor that decision. */
3679 if (reg_equiv[regno].no_equiv)
3680 continue;
3681
3682 if (! note
3683 || rtx_varies_p (XEXP (note, 0), 0)
3684 || (reg_equiv[regno].replacement
3685 && ! rtx_equal_p (XEXP (note, 0),
3686 reg_equiv[regno].replacement)))
3687 {
3688 no_equiv (dest, set, NULL);
3689 continue;
3690 }
3691
3692 list = reg_equiv[regno].init_insns;
3693 for (; list; list = list->next ())
3694 {
3695 rtx note_tmp;
3696 rtx_insn *insn_tmp;
3697
3698 insn_tmp = list->insn ();
3699 note_tmp = find_reg_note (insn_tmp, REG_EQUAL, NULL_RTX);
3700 gcc_assert (note_tmp);
3701 if (! rtx_equal_p (XEXP (note, 0), XEXP (note_tmp, 0)))
3702 {
3703 equal_p = false;
3704 break;
3705 }
3706 }
3707
3708 if (! equal_p)
3709 {
3710 no_equiv (dest, set, NULL);
3711 continue;
3712 }
3713 }
3714
3715 /* Record this insn as initializing this register. */
3716 reg_equiv[regno].init_insns
3717 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv[regno].init_insns);
3718
3719 /* If this register is known to be equal to a constant, record that
3720 it is always equivalent to the constant.
3721 Note that it is possible to have a register use before
3722 the def in loops (see gcc.c-torture/execute/pr79286.c)
3723 where the reg is undefined on first use. If the def insn
3724 won't trap we can use it as an equivalence, effectively
3725 choosing the "undefined" value for the reg to be the
3726 same as the value set by the def. */
3727 if (DF_REG_DEF_COUNT (regno) == 1
3728 && note
3729 && !rtx_varies_p (XEXP (note, 0), 0)
3730 && (!may_trap_or_fault_p (XEXP (note, 0))
3731 || def_dominates_uses (regno)))
3732 {
3733 rtx note_value = XEXP (note, 0);
3734 remove_note (insn, note);
3735 set_unique_reg_note (insn, REG_EQUIV, note_value);
3736 }
3737
3738 /* If this insn introduces a "constant" register, decrease the priority
3739 of that register. Record this insn if the register is only used once
3740 more and the equivalence value is the same as our source.
3741
3742 The latter condition is checked for two reasons: First, it is an
3743 indication that it may be more efficient to actually emit the insn
3744 as written (if no registers are available, reload will substitute
3745 the equivalence). Secondly, it avoids problems with any registers
3746 dying in this insn whose death notes would be missed.
3747
3748 If we don't have a REG_EQUIV note, see if this insn is loading
3749 a register used only in one basic block from a MEM. If so, and the
3750 MEM remains unchanged for the life of the register, add a REG_EQUIV
3751 note. */
3752 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
3753
3754 rtx replacement = NULL_RTX;
3755 if (note)
3756 replacement = XEXP (note, 0);
3757 else if (REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
3758 && MEM_P (SET_SRC (set)))
3759 {
3760 enum valid_equiv validity;
3761 validity = validate_equiv_mem (insn, dest, SET_SRC (set));
3762 if (validity != valid_none)
3763 {
3764 replacement = copy_rtx (SET_SRC (set));
3765 if (validity == valid_reload)
3766 {
3767 note = set_unique_reg_note (insn, REG_EQUIV, replacement);
3768 }
3769 else if (ira_use_lra_p)
3770 {
3771 /* We still can use this equivalence for caller save
3772 optimization in LRA. Mark this. */
3773 ira_reg_equiv[regno].caller_save_p = true;
3774 ira_reg_equiv[regno].init_insns
3775 = gen_rtx_INSN_LIST (VOIDmode, insn,
3776 ira_reg_equiv[regno].init_insns);
3777 }
3778 }
3779 }
3780
3781 /* If we haven't done so, record for reload that this is an
3782 equivalencing insn. */
3783 if (note && !reg_equiv[regno].is_arg_equivalence)
3784 ira_reg_equiv[regno].init_insns
3785 = gen_rtx_INSN_LIST (VOIDmode, insn,
3786 ira_reg_equiv[regno].init_insns);
3787
3788 if (replacement)
3789 {
3790 reg_equiv[regno].replacement = replacement;
3791 reg_equiv[regno].src_p = &SET_SRC (set);
3792 reg_equiv[regno].loop_depth = (short) loop_depth;
3793
3794 /* Don't mess with things live during setjmp. */
3795 if (optimize && !bitmap_bit_p (setjmp_crosses, regno))
3796 {
3797 /* If the register is referenced exactly twice, meaning it is
3798 set once and used once, indicate that the reference may be
3799 replaced by the equivalence we computed above. Do this
3800 even if the register is only used in one block so that
3801 dependencies can be handled where the last register is
3802 used in a different block (i.e. HIGH / LO_SUM sequences)
3803 and to reduce the number of registers alive across
3804 calls. */
3805
3806 if (REG_N_REFS (regno) == 2
3807 && (rtx_equal_p (replacement, src)
3808 || ! equiv_init_varies_p (src))
3809 && NONJUMP_INSN_P (insn)
3810 && equiv_init_movable_p (PATTERN (insn), regno))
3811 reg_equiv[regno].replace = 1;
3812 }
3813 }
3814 }
3815 }
3816 }
3817
3818 /* For insns that set a MEM to the contents of a REG that is only used
3819 in a single basic block, see if the register is always equivalent
3820 to that memory location and if moving the store from INSN to the
3821 insn that sets REG is safe. If so, put a REG_EQUIV note on the
3822 initializing insn. */
3823 static void
3824 add_store_equivs (void)
3825 {
3826 auto_sbitmap seen_insns (get_max_uid () + 1);
3827 bitmap_clear (seen_insns);
3828
3829 for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
3830 {
3831 rtx set, src, dest;
3832 unsigned regno;
3833 rtx_insn *init_insn;
3834
3835 bitmap_set_bit (seen_insns, INSN_UID (insn));
3836
3837 if (! INSN_P (insn))
3838 continue;
3839
3840 set = single_set (insn);
3841 if (! set)
3842 continue;
3843
3844 dest = SET_DEST (set);
3845 src = SET_SRC (set);
3846
3847 /* Don't add a REG_EQUIV note if the insn already has one. The existing
3848 REG_EQUIV is likely more useful than the one we are adding. */
3849 if (MEM_P (dest) && REG_P (src)
3850 && (regno = REGNO (src)) >= FIRST_PSEUDO_REGISTER
3851 && REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
3852 && DF_REG_DEF_COUNT (regno) == 1
3853 && ! reg_equiv[regno].pdx_subregs
3854 && reg_equiv[regno].init_insns != NULL
3855 && (init_insn = reg_equiv[regno].init_insns->insn ()) != 0
3856 && bitmap_bit_p (seen_insns, INSN_UID (init_insn))
3857 && ! find_reg_note (init_insn, REG_EQUIV, NULL_RTX)
3858 && validate_equiv_mem (init_insn, src, dest) == valid_reload
3859 && ! memref_used_between_p (dest, init_insn, insn)
3860 /* Attaching a REG_EQUIV note will fail if INIT_INSN has
3861 multiple sets. */
3862 && set_unique_reg_note (init_insn, REG_EQUIV, copy_rtx (dest)))
3863 {
3864 /* This insn makes the equivalence, not the one initializing
3865 the register. */
3866 ira_reg_equiv[regno].init_insns
3867 = gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
3868 df_notes_rescan (init_insn);
3869 if (dump_file)
3870 fprintf (dump_file,
3871 "Adding REG_EQUIV to insn %d for source of insn %d\n",
3872 INSN_UID (init_insn),
3873 INSN_UID (insn));
3874 }
3875 }
3876 }
3877
3878 /* Scan all regs killed in an insn to see if any of them are registers
3879 only used that once. If so, see if we can replace the reference
3880 with the equivalent form. If we can, delete the initializing
3881 reference and this register will go away. If we can't replace the
3882 reference, and the initializing reference is within the same loop
3883 (or in an inner loop), then move the register initialization just
3884 before the use, so that they are in the same basic block. */
3885 static void
3886 combine_and_move_insns (void)
3887 {
3888 auto_bitmap cleared_regs;
3889 int max = max_reg_num ();
3890
3891 for (int regno = FIRST_PSEUDO_REGISTER; regno < max; regno++)
3892 {
3893 if (!reg_equiv[regno].replace)
3894 continue;
3895
3896 rtx_insn *use_insn = 0;
3897 for (df_ref use = DF_REG_USE_CHAIN (regno);
3898 use;
3899 use = DF_REF_NEXT_REG (use))
3900 if (DF_REF_INSN_INFO (use))
3901 {
3902 if (DEBUG_INSN_P (DF_REF_INSN (use)))
3903 continue;
3904 gcc_assert (!use_insn);
3905 use_insn = DF_REF_INSN (use);
3906 }
3907 gcc_assert (use_insn);
3908
3909 /* Don't substitute into jumps. indirect_jump_optimize does
3910 this for anything we are prepared to handle. */
3911 if (JUMP_P (use_insn))
3912 continue;
3913
3914 /* Also don't substitute into a conditional trap insn -- it can become
3915 an unconditional trap, and that is a flow control insn. */
3916 if (GET_CODE (PATTERN (use_insn)) == TRAP_IF)
3917 continue;
3918
3919 df_ref def = DF_REG_DEF_CHAIN (regno);
3920 gcc_assert (DF_REG_DEF_COUNT (regno) == 1 && DF_REF_INSN_INFO (def));
3921 rtx_insn *def_insn = DF_REF_INSN (def);
3922
3923 /* We may not move instructions that can throw, since that
3924 changes basic block boundaries and we are not prepared to
3925 adjust the CFG to match. */
3926 if (can_throw_internal (def_insn))
3927 continue;
3928
3929 /* Instructions with multiple sets can only be moved if DF analysis is
3930 performed for all of the registers set. See PR91052. */
3931 if (multiple_sets (def_insn))
3932 continue;
3933
3934 basic_block use_bb = BLOCK_FOR_INSN (use_insn);
3935 basic_block def_bb = BLOCK_FOR_INSN (def_insn);
3936 if (bb_loop_depth (use_bb) > bb_loop_depth (def_bb))
3937 continue;
3938
3939 if (asm_noperands (PATTERN (def_insn)) < 0
3940 && validate_replace_rtx (regno_reg_rtx[regno],
3941 *reg_equiv[regno].src_p, use_insn))
3942 {
3943 rtx link;
3944 /* Append the REG_DEAD notes from def_insn. */
3945 for (rtx *p = &REG_NOTES (def_insn); (link = *p) != 0; )
3946 {
3947 if (REG_NOTE_KIND (XEXP (link, 0)) == REG_DEAD)
3948 {
3949 *p = XEXP (link, 1);
3950 XEXP (link, 1) = REG_NOTES (use_insn);
3951 REG_NOTES (use_insn) = link;
3952 }
3953 else
3954 p = &XEXP (link, 1);
3955 }
3956
3957 remove_death (regno, use_insn);
3958 SET_REG_N_REFS (regno, 0);
3959 REG_FREQ (regno) = 0;
3960 df_ref use;
3961 FOR_EACH_INSN_USE (use, def_insn)
3962 {
3963 unsigned int use_regno = DF_REF_REGNO (use);
3964 if (!HARD_REGISTER_NUM_P (use_regno))
3965 reg_equiv[use_regno].replace = 0;
3966 }
3967
3968 delete_insn (def_insn);
3969
3970 reg_equiv[regno].init_insns = NULL;
3971 ira_reg_equiv[regno].init_insns = NULL;
3972 bitmap_set_bit (cleared_regs, regno);
3973 }
3974
3975 /* Move the initialization of the register to just before
3976 USE_INSN. Update the flow information. */
3977 else if (prev_nondebug_insn (use_insn) != def_insn)
3978 {
3979 rtx_insn *new_insn;
3980
3981 new_insn = emit_insn_before (PATTERN (def_insn), use_insn);
3982 REG_NOTES (new_insn) = REG_NOTES (def_insn);
3983 REG_NOTES (def_insn) = 0;
3984 /* Rescan it to process the notes. */
3985 df_insn_rescan (new_insn);
3986
3987 /* Make sure this insn is recognized before reload begins,
3988 otherwise eliminate_regs_in_insn will die. */
3989 INSN_CODE (new_insn) = INSN_CODE (def_insn);
3990
3991 delete_insn (def_insn);
3992
3993 XEXP (reg_equiv[regno].init_insns, 0) = new_insn;
3994
3995 REG_BASIC_BLOCK (regno) = use_bb->index;
3996 REG_N_CALLS_CROSSED (regno) = 0;
3997
3998 if (use_insn == BB_HEAD (use_bb))
3999 BB_HEAD (use_bb) = new_insn;
4000
4001 /* We know regno dies in use_insn, but inside a loop
4002 REG_DEAD notes might be missing when def_insn was in
4003 another basic block. However, when we move def_insn into
4004 this bb we'll definitely get a REG_DEAD note and reload
4005 will see the death. It's possible that update_equiv_regs
4006 set up an equivalence referencing regno for a reg set by
4007 use_insn, when regno was seen as non-local. Now that
4008 regno is local to this block, and dies, such an
4009 equivalence is invalid. */
4010 if (find_reg_note (use_insn, REG_EQUIV, regno_reg_rtx[regno]))
4011 {
4012 rtx set = single_set (use_insn);
4013 if (set && REG_P (SET_DEST (set)))
4014 no_equiv (SET_DEST (set), set, NULL);
4015 }
4016
4017 ira_reg_equiv[regno].init_insns
4018 = gen_rtx_INSN_LIST (VOIDmode, new_insn, NULL_RTX);
4019 bitmap_set_bit (cleared_regs, regno);
4020 }
4021 }
4022
4023 if (!bitmap_empty_p (cleared_regs))
4024 {
4025 basic_block bb;
4026
4027 FOR_EACH_BB_FN (bb, cfun)
4028 {
4029 bitmap_and_compl_into (DF_LR_IN (bb), cleared_regs);
4030 bitmap_and_compl_into (DF_LR_OUT (bb), cleared_regs);
4031 if (!df_live)
4032 continue;
4033 bitmap_and_compl_into (DF_LIVE_IN (bb), cleared_regs);
4034 bitmap_and_compl_into (DF_LIVE_OUT (bb), cleared_regs);
4035 }
4036
4037 /* Last pass - adjust debug insns referencing cleared regs. */
4038 if (MAY_HAVE_DEBUG_BIND_INSNS)
4039 for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
4040 if (DEBUG_BIND_INSN_P (insn))
4041 {
4042 rtx old_loc = INSN_VAR_LOCATION_LOC (insn);
4043 INSN_VAR_LOCATION_LOC (insn)
4044 = simplify_replace_fn_rtx (old_loc, NULL_RTX,
4045 adjust_cleared_regs,
4046 (void *) cleared_regs);
4047 if (old_loc != INSN_VAR_LOCATION_LOC (insn))
4048 df_insn_rescan (insn);
4049 }
4050 }
4051 }
4052
4053 /* A pass over indirect jumps, converting simple cases to direct jumps.
4054 Combine does this optimization too, but only within a basic block. */
4055 static void
4056 indirect_jump_optimize (void)
4057 {
4058 basic_block bb;
4059 bool rebuild_p = false;
4060
4061 FOR_EACH_BB_REVERSE_FN (bb, cfun)
4062 {
4063 rtx_insn *insn = BB_END (bb);
4064 if (!JUMP_P (insn)
4065 || find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
4066 continue;
4067
4068 rtx x = pc_set (insn);
4069 if (!x || !REG_P (SET_SRC (x)))
4070 continue;
4071
4072 int regno = REGNO (SET_SRC (x));
4073 if (DF_REG_DEF_COUNT (regno) == 1)
4074 {
4075 df_ref def = DF_REG_DEF_CHAIN (regno);
4076 if (!DF_REF_IS_ARTIFICIAL (def))
4077 {
4078 rtx_insn *def_insn = DF_REF_INSN (def);
4079 rtx lab = NULL_RTX;
4080 rtx set = single_set (def_insn);
4081 if (set && GET_CODE (SET_SRC (set)) == LABEL_REF)
4082 lab = SET_SRC (set);
4083 else
4084 {
4085 rtx eqnote = find_reg_note (def_insn, REG_EQUAL, NULL_RTX);
4086 if (eqnote && GET_CODE (XEXP (eqnote, 0)) == LABEL_REF)
4087 lab = XEXP (eqnote, 0);
4088 }
4089 if (lab && validate_replace_rtx (SET_SRC (x), lab, insn))
4090 rebuild_p = true;
4091 }
4092 }
4093 }
4094
4095 if (rebuild_p)
4096 {
4097 timevar_push (TV_JUMP);
4098 rebuild_jump_labels (get_insns ());
4099 if (purge_all_dead_edges ())
4100 delete_unreachable_blocks ();
4101 timevar_pop (TV_JUMP);
4102 }
4103 }
4104
4105 /* Set up fields memory, constant, and invariant from init_insns in
4106 the structures of array ira_reg_equiv. */
4107 static void
4108 setup_reg_equiv (void)
4109 {
4110 int i;
4111 rtx_insn_list *elem, *prev_elem, *next_elem;
4112 rtx_insn *insn;
4113 rtx set, x;
4114
4115 for (i = FIRST_PSEUDO_REGISTER; i < ira_reg_equiv_len; i++)
4116 for (prev_elem = NULL, elem = ira_reg_equiv[i].init_insns;
4117 elem;
4118 prev_elem = elem, elem = next_elem)
4119 {
4120 next_elem = elem->next ();
4121 insn = elem->insn ();
4122 set = single_set (insn);
4123
4124 /* Init insns can set up equivalence when the reg is a destination or
4125 a source (in this case the destination is memory). */
4126 if (set != 0 && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))))
4127 {
4128 if ((x = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != NULL)
4129 {
4130 x = XEXP (x, 0);
4131 if (REG_P (SET_DEST (set))
4132 && REGNO (SET_DEST (set)) == (unsigned int) i
4133 && ! rtx_equal_p (SET_SRC (set), x) && MEM_P (x))
4134 {
4135 /* This insn reporting the equivalence but
4136 actually not setting it. Remove it from the
4137 list. */
4138 if (prev_elem == NULL)
4139 ira_reg_equiv[i].init_insns = next_elem;
4140 else
4141 XEXP (prev_elem, 1) = next_elem;
4142 elem = prev_elem;
4143 }
4144 }
4145 else if (REG_P (SET_DEST (set))
4146 && REGNO (SET_DEST (set)) == (unsigned int) i)
4147 x = SET_SRC (set);
4148 else
4149 {
4150 gcc_assert (REG_P (SET_SRC (set))
4151 && REGNO (SET_SRC (set)) == (unsigned int) i);
4152 x = SET_DEST (set);
4153 }
4154 if (! function_invariant_p (x)
4155 || ! flag_pic
4156 /* A function invariant is often CONSTANT_P but may
4157 include a register. We promise to only pass
4158 CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
4159 || (CONSTANT_P (x) && LEGITIMATE_PIC_OPERAND_P (x)))
4160 {
4161 /* It can happen that a REG_EQUIV note contains a MEM
4162 that is not a legitimate memory operand. As later
4163 stages of reload assume that all addresses found in
4164 the lra_regno_equiv_* arrays were originally
4165 legitimate, we ignore such REG_EQUIV notes. */
4166 if (memory_operand (x, VOIDmode))
4167 {
4168 ira_reg_equiv[i].defined_p = !ira_reg_equiv[i].caller_save_p;
4169 ira_reg_equiv[i].memory = x;
4170 continue;
4171 }
4172 else if (function_invariant_p (x))
4173 {
4174 machine_mode mode;
4175
4176 mode = GET_MODE (SET_DEST (set));
4177 if (GET_CODE (x) == PLUS
4178 || x == frame_pointer_rtx || x == arg_pointer_rtx)
4179 /* This is PLUS of frame pointer and a constant,
4180 or fp, or argp. */
4181 ira_reg_equiv[i].invariant = x;
4182 else if (targetm.legitimate_constant_p (mode, x))
4183 ira_reg_equiv[i].constant = x;
4184 else
4185 {
4186 ira_reg_equiv[i].memory = force_const_mem (mode, x);
4187 if (ira_reg_equiv[i].memory == NULL_RTX)
4188 {
4189 ira_reg_equiv[i].defined_p = false;
4190 ira_reg_equiv[i].caller_save_p = false;
4191 ira_reg_equiv[i].init_insns = NULL;
4192 break;
4193 }
4194 }
4195 ira_reg_equiv[i].defined_p = true;
4196 continue;
4197 }
4198 }
4199 }
4200 ira_reg_equiv[i].defined_p = false;
4201 ira_reg_equiv[i].caller_save_p = false;
4202 ira_reg_equiv[i].init_insns = NULL;
4203 break;
4204 }
4205 }
4206
4207
4208
4209 /* Print chain C to FILE. */
4210 static void
4211 print_insn_chain (FILE *file, class insn_chain *c)
4212 {
4213 fprintf (file, "insn=%d, ", INSN_UID (c->insn));
4214 bitmap_print (file, &c->live_throughout, "live_throughout: ", ", ");
4215 bitmap_print (file, &c->dead_or_set, "dead_or_set: ", "\n");
4216 }
4217
4218
4219 /* Print all reload_insn_chains to FILE. */
4220 static void
4221 print_insn_chains (FILE *file)
4222 {
4223 class insn_chain *c;
4224 for (c = reload_insn_chain; c ; c = c->next)
4225 print_insn_chain (file, c);
4226 }
4227
4228 /* Return true if pseudo REGNO should be added to set live_throughout
4229 or dead_or_set of the insn chains for reload consideration. */
4230 static bool
4231 pseudo_for_reload_consideration_p (int regno)
4232 {
4233 /* Consider spilled pseudos too for IRA because they still have a
4234 chance to get hard-registers in the reload when IRA is used. */
4235 return (reg_renumber[regno] >= 0 || ira_conflicts_p);
4236 }
4237
4238 /* Return true if we can track the individual bytes of subreg X.
4239 When returning true, set *OUTER_SIZE to the number of bytes in
4240 X itself, *INNER_SIZE to the number of bytes in the inner register
4241 and *START to the offset of the first byte. */
4242 static bool
4243 get_subreg_tracking_sizes (rtx x, HOST_WIDE_INT *outer_size,
4244 HOST_WIDE_INT *inner_size, HOST_WIDE_INT *start)
4245 {
4246 rtx reg = regno_reg_rtx[REGNO (SUBREG_REG (x))];
4247 return (GET_MODE_SIZE (GET_MODE (x)).is_constant (outer_size)
4248 && GET_MODE_SIZE (GET_MODE (reg)).is_constant (inner_size)
4249 && SUBREG_BYTE (x).is_constant (start));
4250 }
4251
4252 /* Init LIVE_SUBREGS[ALLOCNUM] and LIVE_SUBREGS_USED[ALLOCNUM] for
4253 a register with SIZE bytes, making the register live if INIT_VALUE. */
4254 static void
4255 init_live_subregs (bool init_value, sbitmap *live_subregs,
4256 bitmap live_subregs_used, int allocnum, int size)
4257 {
4258 gcc_assert (size > 0);
4259
4260 /* Been there, done that. */
4261 if (bitmap_bit_p (live_subregs_used, allocnum))
4262 return;
4263
4264 /* Create a new one. */
4265 if (live_subregs[allocnum] == NULL)
4266 live_subregs[allocnum] = sbitmap_alloc (size);
4267
4268 /* If the entire reg was live before blasting into subregs, we need
4269 to init all of the subregs to ones else init to 0. */
4270 if (init_value)
4271 bitmap_ones (live_subregs[allocnum]);
4272 else
4273 bitmap_clear (live_subregs[allocnum]);
4274
4275 bitmap_set_bit (live_subregs_used, allocnum);
4276 }
4277
4278 /* Walk the insns of the current function and build reload_insn_chain,
4279 and record register life information. */
4280 static void
4281 build_insn_chain (void)
4282 {
4283 unsigned int i;
4284 class insn_chain **p = &reload_insn_chain;
4285 basic_block bb;
4286 class insn_chain *c = NULL;
4287 class insn_chain *next = NULL;
4288 auto_bitmap live_relevant_regs;
4289 auto_bitmap elim_regset;
4290 /* live_subregs is a vector used to keep accurate information about
4291 which hardregs are live in multiword pseudos. live_subregs and
4292 live_subregs_used are indexed by pseudo number. The live_subreg
4293 entry for a particular pseudo is only used if the corresponding
4294 element is non zero in live_subregs_used. The sbitmap size of
4295 live_subreg[allocno] is number of bytes that the pseudo can
4296 occupy. */
4297 sbitmap *live_subregs = XCNEWVEC (sbitmap, max_regno);
4298 auto_bitmap live_subregs_used;
4299
4300 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4301 if (TEST_HARD_REG_BIT (eliminable_regset, i))
4302 bitmap_set_bit (elim_regset, i);
4303 FOR_EACH_BB_REVERSE_FN (bb, cfun)
4304 {
4305 bitmap_iterator bi;
4306 rtx_insn *insn;
4307
4308 CLEAR_REG_SET (live_relevant_regs);
4309 bitmap_clear (live_subregs_used);
4310
4311 EXECUTE_IF_SET_IN_BITMAP (df_get_live_out (bb), 0, i, bi)
4312 {
4313 if (i >= FIRST_PSEUDO_REGISTER)
4314 break;
4315 bitmap_set_bit (live_relevant_regs, i);
4316 }
4317
4318 EXECUTE_IF_SET_IN_BITMAP (df_get_live_out (bb),
4319 FIRST_PSEUDO_REGISTER, i, bi)
4320 {
4321 if (pseudo_for_reload_consideration_p (i))
4322 bitmap_set_bit (live_relevant_regs, i);
4323 }
4324
4325 FOR_BB_INSNS_REVERSE (bb, insn)
4326 {
4327 if (!NOTE_P (insn) && !BARRIER_P (insn))
4328 {
4329 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4330 df_ref def, use;
4331
4332 c = new_insn_chain ();
4333 c->next = next;
4334 next = c;
4335 *p = c;
4336 p = &c->prev;
4337
4338 c->insn = insn;
4339 c->block = bb->index;
4340
4341 if (NONDEBUG_INSN_P (insn))
4342 FOR_EACH_INSN_INFO_DEF (def, insn_info)
4343 {
4344 unsigned int regno = DF_REF_REGNO (def);
4345
4346 /* Ignore may clobbers because these are generated
4347 from calls. However, every other kind of def is
4348 added to dead_or_set. */
4349 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
4350 {
4351 if (regno < FIRST_PSEUDO_REGISTER)
4352 {
4353 if (!fixed_regs[regno])
4354 bitmap_set_bit (&c->dead_or_set, regno);
4355 }
4356 else if (pseudo_for_reload_consideration_p (regno))
4357 bitmap_set_bit (&c->dead_or_set, regno);
4358 }
4359
4360 if ((regno < FIRST_PSEUDO_REGISTER
4361 || reg_renumber[regno] >= 0
4362 || ira_conflicts_p)
4363 && (!DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL)))
4364 {
4365 rtx reg = DF_REF_REG (def);
4366 HOST_WIDE_INT outer_size, inner_size, start;
4367
4368 /* We can usually track the liveness of individual
4369 bytes within a subreg. The only exceptions are
4370 subregs wrapped in ZERO_EXTRACTs and subregs whose
4371 size is not known; in those cases we need to be
4372 conservative and treat the definition as a partial
4373 definition of the full register rather than a full
4374 definition of a specific part of the register. */
4375 if (GET_CODE (reg) == SUBREG
4376 && !DF_REF_FLAGS_IS_SET (def, DF_REF_ZERO_EXTRACT)
4377 && get_subreg_tracking_sizes (reg, &outer_size,
4378 &inner_size, &start))
4379 {
4380 HOST_WIDE_INT last = start + outer_size;
4381
4382 init_live_subregs
4383 (bitmap_bit_p (live_relevant_regs, regno),
4384 live_subregs, live_subregs_used, regno,
4385 inner_size);
4386
4387 if (!DF_REF_FLAGS_IS_SET
4388 (def, DF_REF_STRICT_LOW_PART))
4389 {
4390 /* Expand the range to cover entire words.
4391 Bytes added here are "don't care". */
4392 start
4393 = start / UNITS_PER_WORD * UNITS_PER_WORD;
4394 last = ((last + UNITS_PER_WORD - 1)
4395 / UNITS_PER_WORD * UNITS_PER_WORD);
4396 }
4397
4398 /* Ignore the paradoxical bits. */
4399 if (last > SBITMAP_SIZE (live_subregs[regno]))
4400 last = SBITMAP_SIZE (live_subregs[regno]);
4401
4402 while (start < last)
4403 {
4404 bitmap_clear_bit (live_subregs[regno], start);
4405 start++;
4406 }
4407
4408 if (bitmap_empty_p (live_subregs[regno]))
4409 {
4410 bitmap_clear_bit (live_subregs_used, regno);
4411 bitmap_clear_bit (live_relevant_regs, regno);
4412 }
4413 else
4414 /* Set live_relevant_regs here because
4415 that bit has to be true to get us to
4416 look at the live_subregs fields. */
4417 bitmap_set_bit (live_relevant_regs, regno);
4418 }
4419 else
4420 {
4421 /* DF_REF_PARTIAL is generated for
4422 subregs, STRICT_LOW_PART, and
4423 ZERO_EXTRACT. We handle the subreg
4424 case above so here we have to keep from
4425 modeling the def as a killing def. */
4426 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL))
4427 {
4428 bitmap_clear_bit (live_subregs_used, regno);
4429 bitmap_clear_bit (live_relevant_regs, regno);
4430 }
4431 }
4432 }
4433 }
4434
4435 bitmap_and_compl_into (live_relevant_regs, elim_regset);
4436 bitmap_copy (&c->live_throughout, live_relevant_regs);
4437
4438 if (NONDEBUG_INSN_P (insn))
4439 FOR_EACH_INSN_INFO_USE (use, insn_info)
4440 {
4441 unsigned int regno = DF_REF_REGNO (use);
4442 rtx reg = DF_REF_REG (use);
4443
4444 /* DF_REF_READ_WRITE on a use means that this use
4445 is fabricated from a def that is a partial set
4446 to a multiword reg. Here, we only model the
4447 subreg case that is not wrapped in ZERO_EXTRACT
4448 precisely so we do not need to look at the
4449 fabricated use. */
4450 if (DF_REF_FLAGS_IS_SET (use, DF_REF_READ_WRITE)
4451 && !DF_REF_FLAGS_IS_SET (use, DF_REF_ZERO_EXTRACT)
4452 && DF_REF_FLAGS_IS_SET (use, DF_REF_SUBREG))
4453 continue;
4454
4455 /* Add the last use of each var to dead_or_set. */
4456 if (!bitmap_bit_p (live_relevant_regs, regno))
4457 {
4458 if (regno < FIRST_PSEUDO_REGISTER)
4459 {
4460 if (!fixed_regs[regno])
4461 bitmap_set_bit (&c->dead_or_set, regno);
4462 }
4463 else if (pseudo_for_reload_consideration_p (regno))
4464 bitmap_set_bit (&c->dead_or_set, regno);
4465 }
4466
4467 if (regno < FIRST_PSEUDO_REGISTER
4468 || pseudo_for_reload_consideration_p (regno))
4469 {
4470 HOST_WIDE_INT outer_size, inner_size, start;
4471 if (GET_CODE (reg) == SUBREG
4472 && !DF_REF_FLAGS_IS_SET (use,
4473 DF_REF_SIGN_EXTRACT
4474 | DF_REF_ZERO_EXTRACT)
4475 && get_subreg_tracking_sizes (reg, &outer_size,
4476 &inner_size, &start))
4477 {
4478 HOST_WIDE_INT last = start + outer_size;
4479
4480 init_live_subregs
4481 (bitmap_bit_p (live_relevant_regs, regno),
4482 live_subregs, live_subregs_used, regno,
4483 inner_size);
4484
4485 /* Ignore the paradoxical bits. */
4486 if (last > SBITMAP_SIZE (live_subregs[regno]))
4487 last = SBITMAP_SIZE (live_subregs[regno]);
4488
4489 while (start < last)
4490 {
4491 bitmap_set_bit (live_subregs[regno], start);
4492 start++;
4493 }
4494 }
4495 else
4496 /* Resetting the live_subregs_used is
4497 effectively saying do not use the subregs
4498 because we are reading the whole
4499 pseudo. */
4500 bitmap_clear_bit (live_subregs_used, regno);
4501 bitmap_set_bit (live_relevant_regs, regno);
4502 }
4503 }
4504 }
4505 }
4506
4507 /* FIXME!! The following code is a disaster. Reload needs to see the
4508 labels and jump tables that are just hanging out in between
4509 the basic blocks. See pr33676. */
4510 insn = BB_HEAD (bb);
4511
4512 /* Skip over the barriers and cruft. */
4513 while (insn && (BARRIER_P (insn) || NOTE_P (insn)
4514 || BLOCK_FOR_INSN (insn) == bb))
4515 insn = PREV_INSN (insn);
4516
4517 /* While we add anything except barriers and notes, the focus is
4518 to get the labels and jump tables into the
4519 reload_insn_chain. */
4520 while (insn)
4521 {
4522 if (!NOTE_P (insn) && !BARRIER_P (insn))
4523 {
4524 if (BLOCK_FOR_INSN (insn))
4525 break;
4526
4527 c = new_insn_chain ();
4528 c->next = next;
4529 next = c;
4530 *p = c;
4531 p = &c->prev;
4532
4533 /* The block makes no sense here, but it is what the old
4534 code did. */
4535 c->block = bb->index;
4536 c->insn = insn;
4537 bitmap_copy (&c->live_throughout, live_relevant_regs);
4538 }
4539 insn = PREV_INSN (insn);
4540 }
4541 }
4542
4543 reload_insn_chain = c;
4544 *p = NULL;
4545
4546 for (i = 0; i < (unsigned int) max_regno; i++)
4547 if (live_subregs[i] != NULL)
4548 sbitmap_free (live_subregs[i]);
4549 free (live_subregs);
4550
4551 if (dump_file)
4552 print_insn_chains (dump_file);
4553 }
4554
4555 /* Examine the rtx found in *LOC, which is read or written to as determined
4556 by TYPE. Return false if we find a reason why an insn containing this
4557 rtx should not be moved (such as accesses to non-constant memory), true
4558 otherwise. */
4559 static bool
4560 rtx_moveable_p (rtx *loc, enum op_type type)
4561 {
4562 const char *fmt;
4563 rtx x = *loc;
4564 int i, j;
4565
4566 enum rtx_code code = GET_CODE (x);
4567 switch (code)
4568 {
4569 case CONST:
4570 CASE_CONST_ANY:
4571 case SYMBOL_REF:
4572 case LABEL_REF:
4573 return true;
4574
4575 case PC:
4576 return type == OP_IN;
4577
4578 case REG:
4579 if (x == frame_pointer_rtx)
4580 return true;
4581 if (HARD_REGISTER_P (x))
4582 return false;
4583
4584 return true;
4585
4586 case MEM:
4587 if (type == OP_IN && MEM_READONLY_P (x))
4588 return rtx_moveable_p (&XEXP (x, 0), OP_IN);
4589 return false;
4590
4591 case SET:
4592 return (rtx_moveable_p (&SET_SRC (x), OP_IN)
4593 && rtx_moveable_p (&SET_DEST (x), OP_OUT));
4594
4595 case STRICT_LOW_PART:
4596 return rtx_moveable_p (&XEXP (x, 0), OP_OUT);
4597
4598 case ZERO_EXTRACT:
4599 case SIGN_EXTRACT:
4600 return (rtx_moveable_p (&XEXP (x, 0), type)
4601 && rtx_moveable_p (&XEXP (x, 1), OP_IN)
4602 && rtx_moveable_p (&XEXP (x, 2), OP_IN));
4603
4604 case CLOBBER:
4605 return rtx_moveable_p (&SET_DEST (x), OP_OUT);
4606
4607 case UNSPEC_VOLATILE:
4608 /* It is a bad idea to consider insns with such rtl
4609 as moveable ones. The insn scheduler also considers them as barrier
4610 for a reason. */
4611 return false;
4612
4613 case ASM_OPERANDS:
4614 /* The same is true for volatile asm: it has unknown side effects, it
4615 cannot be moved at will. */
4616 if (MEM_VOLATILE_P (x))
4617 return false;
4618
4619 default:
4620 break;
4621 }
4622
4623 fmt = GET_RTX_FORMAT (code);
4624 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4625 {
4626 if (fmt[i] == 'e')
4627 {
4628 if (!rtx_moveable_p (&XEXP (x, i), type))
4629 return false;
4630 }
4631 else if (fmt[i] == 'E')
4632 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4633 {
4634 if (!rtx_moveable_p (&XVECEXP (x, i, j), type))
4635 return false;
4636 }
4637 }
4638 return true;
4639 }
4640
4641 /* A wrapper around dominated_by_p, which uses the information in UID_LUID
4642 to give dominance relationships between two insns I1 and I2. */
4643 static bool
4644 insn_dominated_by_p (rtx i1, rtx i2, int *uid_luid)
4645 {
4646 basic_block bb1 = BLOCK_FOR_INSN (i1);
4647 basic_block bb2 = BLOCK_FOR_INSN (i2);
4648
4649 if (bb1 == bb2)
4650 return uid_luid[INSN_UID (i2)] < uid_luid[INSN_UID (i1)];
4651 return dominated_by_p (CDI_DOMINATORS, bb1, bb2);
4652 }
4653
4654 /* Record the range of register numbers added by find_moveable_pseudos. */
4655 int first_moveable_pseudo, last_moveable_pseudo;
4656
4657 /* These two vectors hold data for every register added by
4658 find_movable_pseudos, with index 0 holding data for the
4659 first_moveable_pseudo. */
4660 /* The original home register. */
4661 static vec<rtx> pseudo_replaced_reg;
4662
4663 /* Look for instances where we have an instruction that is known to increase
4664 register pressure, and whose result is not used immediately. If it is
4665 possible to move the instruction downwards to just before its first use,
4666 split its lifetime into two ranges. We create a new pseudo to compute the
4667 value, and emit a move instruction just before the first use. If, after
4668 register allocation, the new pseudo remains unallocated, the function
4669 move_unallocated_pseudos then deletes the move instruction and places
4670 the computation just before the first use.
4671
4672 Such a move is safe and profitable if all the input registers remain live
4673 and unchanged between the original computation and its first use. In such
4674 a situation, the computation is known to increase register pressure, and
4675 moving it is known to at least not worsen it.
4676
4677 We restrict moves to only those cases where a register remains unallocated,
4678 in order to avoid interfering too much with the instruction schedule. As
4679 an exception, we may move insns which only modify their input register
4680 (typically induction variables), as this increases the freedom for our
4681 intended transformation, and does not limit the second instruction
4682 scheduler pass. */
4683
4684 static void
4685 find_moveable_pseudos (void)
4686 {
4687 unsigned i;
4688 int max_regs = max_reg_num ();
4689 int max_uid = get_max_uid ();
4690 basic_block bb;
4691 int *uid_luid = XNEWVEC (int, max_uid);
4692 rtx_insn **closest_uses = XNEWVEC (rtx_insn *, max_regs);
4693 /* A set of registers which are live but not modified throughout a block. */
4694 bitmap_head *bb_transp_live = XNEWVEC (bitmap_head,
4695 last_basic_block_for_fn (cfun));
4696 /* A set of registers which only exist in a given basic block. */
4697 bitmap_head *bb_local = XNEWVEC (bitmap_head,
4698 last_basic_block_for_fn (cfun));
4699 /* A set of registers which are set once, in an instruction that can be
4700 moved freely downwards, but are otherwise transparent to a block. */
4701 bitmap_head *bb_moveable_reg_sets = XNEWVEC (bitmap_head,
4702 last_basic_block_for_fn (cfun));
4703 auto_bitmap live, used, set, interesting, unusable_as_input;
4704 bitmap_iterator bi;
4705
4706 first_moveable_pseudo = max_regs;
4707 pseudo_replaced_reg.release ();
4708 pseudo_replaced_reg.safe_grow_cleared (max_regs, true);
4709
4710 df_analyze ();
4711 calculate_dominance_info (CDI_DOMINATORS);
4712
4713 i = 0;
4714 FOR_EACH_BB_FN (bb, cfun)
4715 {
4716 rtx_insn *insn;
4717 bitmap transp = bb_transp_live + bb->index;
4718 bitmap moveable = bb_moveable_reg_sets + bb->index;
4719 bitmap local = bb_local + bb->index;
4720
4721 bitmap_initialize (local, 0);
4722 bitmap_initialize (transp, 0);
4723 bitmap_initialize (moveable, 0);
4724 bitmap_copy (live, df_get_live_out (bb));
4725 bitmap_and_into (live, df_get_live_in (bb));
4726 bitmap_copy (transp, live);
4727 bitmap_clear (moveable);
4728 bitmap_clear (live);
4729 bitmap_clear (used);
4730 bitmap_clear (set);
4731 FOR_BB_INSNS (bb, insn)
4732 if (NONDEBUG_INSN_P (insn))
4733 {
4734 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4735 df_ref def, use;
4736
4737 uid_luid[INSN_UID (insn)] = i++;
4738
4739 def = df_single_def (insn_info);
4740 use = df_single_use (insn_info);
4741 if (use
4742 && def
4743 && DF_REF_REGNO (use) == DF_REF_REGNO (def)
4744 && !bitmap_bit_p (set, DF_REF_REGNO (use))
4745 && rtx_moveable_p (&PATTERN (insn), OP_IN))
4746 {
4747 unsigned regno = DF_REF_REGNO (use);
4748 bitmap_set_bit (moveable, regno);
4749 bitmap_set_bit (set, regno);
4750 bitmap_set_bit (used, regno);
4751 bitmap_clear_bit (transp, regno);
4752 continue;
4753 }
4754 FOR_EACH_INSN_INFO_USE (use, insn_info)
4755 {
4756 unsigned regno = DF_REF_REGNO (use);
4757 bitmap_set_bit (used, regno);
4758 if (bitmap_clear_bit (moveable, regno))
4759 bitmap_clear_bit (transp, regno);
4760 }
4761
4762 FOR_EACH_INSN_INFO_DEF (def, insn_info)
4763 {
4764 unsigned regno = DF_REF_REGNO (def);
4765 bitmap_set_bit (set, regno);
4766 bitmap_clear_bit (transp, regno);
4767 bitmap_clear_bit (moveable, regno);
4768 }
4769 }
4770 }
4771
4772 FOR_EACH_BB_FN (bb, cfun)
4773 {
4774 bitmap local = bb_local + bb->index;
4775 rtx_insn *insn;
4776
4777 FOR_BB_INSNS (bb, insn)
4778 if (NONDEBUG_INSN_P (insn))
4779 {
4780 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4781 rtx_insn *def_insn;
4782 rtx closest_use, note;
4783 df_ref def, use;
4784 unsigned regno;
4785 bool all_dominated, all_local;
4786 machine_mode mode;
4787
4788 def = df_single_def (insn_info);
4789 /* There must be exactly one def in this insn. */
4790 if (!def || !single_set (insn))
4791 continue;
4792 /* This must be the only definition of the reg. We also limit
4793 which modes we deal with so that we can assume we can generate
4794 move instructions. */
4795 regno = DF_REF_REGNO (def);
4796 mode = GET_MODE (DF_REF_REG (def));
4797 if (DF_REG_DEF_COUNT (regno) != 1
4798 || !DF_REF_INSN_INFO (def)
4799 || HARD_REGISTER_NUM_P (regno)
4800 || DF_REG_EQ_USE_COUNT (regno) > 0
4801 || (!INTEGRAL_MODE_P (mode)
4802 && !FLOAT_MODE_P (mode)
4803 && !OPAQUE_MODE_P (mode)))
4804 continue;
4805 def_insn = DF_REF_INSN (def);
4806
4807 for (note = REG_NOTES (def_insn); note; note = XEXP (note, 1))
4808 if (REG_NOTE_KIND (note) == REG_EQUIV && MEM_P (XEXP (note, 0)))
4809 break;
4810
4811 if (note)
4812 {
4813 if (dump_file)
4814 fprintf (dump_file, "Ignoring reg %d, has equiv memory\n",
4815 regno);
4816 bitmap_set_bit (unusable_as_input, regno);
4817 continue;
4818 }
4819
4820 use = DF_REG_USE_CHAIN (regno);
4821 all_dominated = true;
4822 all_local = true;
4823 closest_use = NULL_RTX;
4824 for (; use; use = DF_REF_NEXT_REG (use))
4825 {
4826 rtx_insn *insn;
4827 if (!DF_REF_INSN_INFO (use))
4828 {
4829 all_dominated = false;
4830 all_local = false;
4831 break;
4832 }
4833 insn = DF_REF_INSN (use);
4834 if (DEBUG_INSN_P (insn))
4835 continue;
4836 if (BLOCK_FOR_INSN (insn) != BLOCK_FOR_INSN (def_insn))
4837 all_local = false;
4838 if (!insn_dominated_by_p (insn, def_insn, uid_luid))
4839 all_dominated = false;
4840 if (closest_use != insn && closest_use != const0_rtx)
4841 {
4842 if (closest_use == NULL_RTX)
4843 closest_use = insn;
4844 else if (insn_dominated_by_p (closest_use, insn, uid_luid))
4845 closest_use = insn;
4846 else if (!insn_dominated_by_p (insn, closest_use, uid_luid))
4847 closest_use = const0_rtx;
4848 }
4849 }
4850 if (!all_dominated)
4851 {
4852 if (dump_file)
4853 fprintf (dump_file, "Reg %d not all uses dominated by set\n",
4854 regno);
4855 continue;
4856 }
4857 if (all_local)
4858 bitmap_set_bit (local, regno);
4859 if (closest_use == const0_rtx || closest_use == NULL
4860 || next_nonnote_nondebug_insn (def_insn) == closest_use)
4861 {
4862 if (dump_file)
4863 fprintf (dump_file, "Reg %d uninteresting%s\n", regno,
4864 closest_use == const0_rtx || closest_use == NULL
4865 ? " (no unique first use)" : "");
4866 continue;
4867 }
4868
4869 bitmap_set_bit (interesting, regno);
4870 /* If we get here, we know closest_use is a non-NULL insn
4871 (as opposed to const_0_rtx). */
4872 closest_uses[regno] = as_a <rtx_insn *> (closest_use);
4873
4874 if (dump_file && (all_local || all_dominated))
4875 {
4876 fprintf (dump_file, "Reg %u:", regno);
4877 if (all_local)
4878 fprintf (dump_file, " local to bb %d", bb->index);
4879 if (all_dominated)
4880 fprintf (dump_file, " def dominates all uses");
4881 if (closest_use != const0_rtx)
4882 fprintf (dump_file, " has unique first use");
4883 fputs ("\n", dump_file);
4884 }
4885 }
4886 }
4887
4888 EXECUTE_IF_SET_IN_BITMAP (interesting, 0, i, bi)
4889 {
4890 df_ref def = DF_REG_DEF_CHAIN (i);
4891 rtx_insn *def_insn = DF_REF_INSN (def);
4892 basic_block def_block = BLOCK_FOR_INSN (def_insn);
4893 bitmap def_bb_local = bb_local + def_block->index;
4894 bitmap def_bb_moveable = bb_moveable_reg_sets + def_block->index;
4895 bitmap def_bb_transp = bb_transp_live + def_block->index;
4896 bool local_to_bb_p = bitmap_bit_p (def_bb_local, i);
4897 rtx_insn *use_insn = closest_uses[i];
4898 df_ref use;
4899 bool all_ok = true;
4900 bool all_transp = true;
4901
4902 if (!REG_P (DF_REF_REG (def)))
4903 continue;
4904
4905 if (!local_to_bb_p)
4906 {
4907 if (dump_file)
4908 fprintf (dump_file, "Reg %u not local to one basic block\n",
4909 i);
4910 continue;
4911 }
4912 if (reg_equiv_init (i) != NULL_RTX)
4913 {
4914 if (dump_file)
4915 fprintf (dump_file, "Ignoring reg %u with equiv init insn\n",
4916 i);
4917 continue;
4918 }
4919 if (!rtx_moveable_p (&PATTERN (def_insn), OP_IN))
4920 {
4921 if (dump_file)
4922 fprintf (dump_file, "Found def insn %d for %d to be not moveable\n",
4923 INSN_UID (def_insn), i);
4924 continue;
4925 }
4926 if (dump_file)
4927 fprintf (dump_file, "Examining insn %d, def for %d\n",
4928 INSN_UID (def_insn), i);
4929 FOR_EACH_INSN_USE (use, def_insn)
4930 {
4931 unsigned regno = DF_REF_REGNO (use);
4932 if (bitmap_bit_p (unusable_as_input, regno))
4933 {
4934 all_ok = false;
4935 if (dump_file)
4936 fprintf (dump_file, " found unusable input reg %u.\n", regno);
4937 break;
4938 }
4939 if (!bitmap_bit_p (def_bb_transp, regno))
4940 {
4941 if (bitmap_bit_p (def_bb_moveable, regno)
4942 && !control_flow_insn_p (use_insn))
4943 {
4944 if (modified_between_p (DF_REF_REG (use), def_insn, use_insn))
4945 {
4946 rtx_insn *x = NEXT_INSN (def_insn);
4947 while (!modified_in_p (DF_REF_REG (use), x))
4948 {
4949 gcc_assert (x != use_insn);
4950 x = NEXT_INSN (x);
4951 }
4952 if (dump_file)
4953 fprintf (dump_file, " input reg %u modified but insn %d moveable\n",
4954 regno, INSN_UID (x));
4955 emit_insn_after (PATTERN (x), use_insn);
4956 set_insn_deleted (x);
4957 }
4958 else
4959 {
4960 if (dump_file)
4961 fprintf (dump_file, " input reg %u modified between def and use\n",
4962 regno);
4963 all_transp = false;
4964 }
4965 }
4966 else
4967 all_transp = false;
4968 }
4969 }
4970 if (!all_ok)
4971 continue;
4972 if (!dbg_cnt (ira_move))
4973 break;
4974 if (dump_file)
4975 fprintf (dump_file, " all ok%s\n", all_transp ? " and transp" : "");
4976
4977 if (all_transp)
4978 {
4979 rtx def_reg = DF_REF_REG (def);
4980 rtx newreg = ira_create_new_reg (def_reg);
4981 if (validate_change (def_insn, DF_REF_REAL_LOC (def), newreg, 0))
4982 {
4983 unsigned nregno = REGNO (newreg);
4984 emit_insn_before (gen_move_insn (def_reg, newreg), use_insn);
4985 nregno -= max_regs;
4986 pseudo_replaced_reg[nregno] = def_reg;
4987 }
4988 }
4989 }
4990
4991 FOR_EACH_BB_FN (bb, cfun)
4992 {
4993 bitmap_clear (bb_local + bb->index);
4994 bitmap_clear (bb_transp_live + bb->index);
4995 bitmap_clear (bb_moveable_reg_sets + bb->index);
4996 }
4997 free (uid_luid);
4998 free (closest_uses);
4999 free (bb_local);
5000 free (bb_transp_live);
5001 free (bb_moveable_reg_sets);
5002
5003 last_moveable_pseudo = max_reg_num ();
5004
5005 fix_reg_equiv_init ();
5006 expand_reg_info ();
5007 regstat_free_n_sets_and_refs ();
5008 regstat_free_ri ();
5009 regstat_init_n_sets_and_refs ();
5010 regstat_compute_ri ();
5011 free_dominance_info (CDI_DOMINATORS);
5012 }
5013
5014 /* If SET pattern SET is an assignment from a hard register to a pseudo which
5015 is live at CALL_DOM (if non-NULL, otherwise this check is omitted), return
5016 the destination. Otherwise return NULL. */
5017
5018 static rtx
5019 interesting_dest_for_shprep_1 (rtx set, basic_block call_dom)
5020 {
5021 rtx src = SET_SRC (set);
5022 rtx dest = SET_DEST (set);
5023 if (!REG_P (src) || !HARD_REGISTER_P (src)
5024 || !REG_P (dest) || HARD_REGISTER_P (dest)
5025 || (call_dom && !bitmap_bit_p (df_get_live_in (call_dom), REGNO (dest))))
5026 return NULL;
5027 return dest;
5028 }
5029
5030 /* If insn is interesting for parameter range-splitting shrink-wrapping
5031 preparation, i.e. it is a single set from a hard register to a pseudo, which
5032 is live at CALL_DOM (if non-NULL, otherwise this check is omitted), or a
5033 parallel statement with only one such statement, return the destination.
5034 Otherwise return NULL. */
5035
5036 static rtx
5037 interesting_dest_for_shprep (rtx_insn *insn, basic_block call_dom)
5038 {
5039 if (!INSN_P (insn))
5040 return NULL;
5041 rtx pat = PATTERN (insn);
5042 if (GET_CODE (pat) == SET)
5043 return interesting_dest_for_shprep_1 (pat, call_dom);
5044
5045 if (GET_CODE (pat) != PARALLEL)
5046 return NULL;
5047 rtx ret = NULL;
5048 for (int i = 0; i < XVECLEN (pat, 0); i++)
5049 {
5050 rtx sub = XVECEXP (pat, 0, i);
5051 if (GET_CODE (sub) == USE || GET_CODE (sub) == CLOBBER)
5052 continue;
5053 if (GET_CODE (sub) != SET
5054 || side_effects_p (sub))
5055 return NULL;
5056 rtx dest = interesting_dest_for_shprep_1 (sub, call_dom);
5057 if (dest && ret)
5058 return NULL;
5059 if (dest)
5060 ret = dest;
5061 }
5062 return ret;
5063 }
5064
5065 /* Split live ranges of pseudos that are loaded from hard registers in the
5066 first BB in a BB that dominates all non-sibling call if such a BB can be
5067 found and is not in a loop. Return true if the function has made any
5068 changes. */
5069
5070 static bool
5071 split_live_ranges_for_shrink_wrap (void)
5072 {
5073 basic_block bb, call_dom = NULL;
5074 basic_block first = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5075 rtx_insn *insn, *last_interesting_insn = NULL;
5076 auto_bitmap need_new, reachable;
5077 vec<basic_block> queue;
5078
5079 if (!SHRINK_WRAPPING_ENABLED)
5080 return false;
5081
5082 queue.create (n_basic_blocks_for_fn (cfun));
5083
5084 FOR_EACH_BB_FN (bb, cfun)
5085 FOR_BB_INSNS (bb, insn)
5086 if (CALL_P (insn) && !SIBLING_CALL_P (insn))
5087 {
5088 if (bb == first)
5089 {
5090 queue.release ();
5091 return false;
5092 }
5093
5094 bitmap_set_bit (need_new, bb->index);
5095 bitmap_set_bit (reachable, bb->index);
5096 queue.quick_push (bb);
5097 break;
5098 }
5099
5100 if (queue.is_empty ())
5101 {
5102 queue.release ();
5103 return false;
5104 }
5105
5106 while (!queue.is_empty ())
5107 {
5108 edge e;
5109 edge_iterator ei;
5110
5111 bb = queue.pop ();
5112 FOR_EACH_EDGE (e, ei, bb->succs)
5113 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
5114 && bitmap_set_bit (reachable, e->dest->index))
5115 queue.quick_push (e->dest);
5116 }
5117 queue.release ();
5118
5119 FOR_BB_INSNS (first, insn)
5120 {
5121 rtx dest = interesting_dest_for_shprep (insn, NULL);
5122 if (!dest)
5123 continue;
5124
5125 if (DF_REG_DEF_COUNT (REGNO (dest)) > 1)
5126 return false;
5127
5128 for (df_ref use = DF_REG_USE_CHAIN (REGNO(dest));
5129 use;
5130 use = DF_REF_NEXT_REG (use))
5131 {
5132 int ubbi = DF_REF_BB (use)->index;
5133
5134 /* Only non debug insns should be taken into account. */
5135 if (NONDEBUG_INSN_P (DF_REF_INSN (use))
5136 && bitmap_bit_p (reachable, ubbi))
5137 bitmap_set_bit (need_new, ubbi);
5138 }
5139 last_interesting_insn = insn;
5140 }
5141
5142 if (!last_interesting_insn)
5143 return false;
5144
5145 call_dom = nearest_common_dominator_for_set (CDI_DOMINATORS, need_new);
5146 if (call_dom == first)
5147 return false;
5148
5149 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
5150 while (bb_loop_depth (call_dom) > 0)
5151 call_dom = get_immediate_dominator (CDI_DOMINATORS, call_dom);
5152 loop_optimizer_finalize ();
5153
5154 if (call_dom == first)
5155 return false;
5156
5157 calculate_dominance_info (CDI_POST_DOMINATORS);
5158 if (dominated_by_p (CDI_POST_DOMINATORS, first, call_dom))
5159 {
5160 free_dominance_info (CDI_POST_DOMINATORS);
5161 return false;
5162 }
5163 free_dominance_info (CDI_POST_DOMINATORS);
5164
5165 if (dump_file)
5166 fprintf (dump_file, "Will split live ranges of parameters at BB %i\n",
5167 call_dom->index);
5168
5169 bool ret = false;
5170 FOR_BB_INSNS (first, insn)
5171 {
5172 rtx dest = interesting_dest_for_shprep (insn, call_dom);
5173 if (!dest || dest == pic_offset_table_rtx)
5174 continue;
5175
5176 bool need_newreg = false;
5177 df_ref use, next;
5178 for (use = DF_REG_USE_CHAIN (REGNO (dest)); use; use = next)
5179 {
5180 rtx_insn *uin = DF_REF_INSN (use);
5181 next = DF_REF_NEXT_REG (use);
5182
5183 if (DEBUG_INSN_P (uin))
5184 continue;
5185
5186 basic_block ubb = BLOCK_FOR_INSN (uin);
5187 if (ubb == call_dom
5188 || dominated_by_p (CDI_DOMINATORS, ubb, call_dom))
5189 {
5190 need_newreg = true;
5191 break;
5192 }
5193 }
5194
5195 if (need_newreg)
5196 {
5197 rtx newreg = ira_create_new_reg (dest);
5198
5199 for (use = DF_REG_USE_CHAIN (REGNO (dest)); use; use = next)
5200 {
5201 rtx_insn *uin = DF_REF_INSN (use);
5202 next = DF_REF_NEXT_REG (use);
5203
5204 basic_block ubb = BLOCK_FOR_INSN (uin);
5205 if (ubb == call_dom
5206 || dominated_by_p (CDI_DOMINATORS, ubb, call_dom))
5207 validate_change (uin, DF_REF_REAL_LOC (use), newreg, true);
5208 }
5209
5210 rtx_insn *new_move = gen_move_insn (newreg, dest);
5211 emit_insn_after (new_move, bb_note (call_dom));
5212 if (dump_file)
5213 {
5214 fprintf (dump_file, "Split live-range of register ");
5215 print_rtl_single (dump_file, dest);
5216 }
5217 ret = true;
5218 }
5219
5220 if (insn == last_interesting_insn)
5221 break;
5222 }
5223 apply_change_group ();
5224 return ret;
5225 }
5226
5227 /* Perform the second half of the transformation started in
5228 find_moveable_pseudos. We look for instances where the newly introduced
5229 pseudo remains unallocated, and remove it by moving the definition to
5230 just before its use, replacing the move instruction generated by
5231 find_moveable_pseudos. */
5232 static void
5233 move_unallocated_pseudos (void)
5234 {
5235 int i;
5236 for (i = first_moveable_pseudo; i < last_moveable_pseudo; i++)
5237 if (reg_renumber[i] < 0)
5238 {
5239 int idx = i - first_moveable_pseudo;
5240 rtx other_reg = pseudo_replaced_reg[idx];
5241 /* The iterating range [first_moveable_pseudo, last_moveable_pseudo)
5242 covers every new pseudo created in find_moveable_pseudos,
5243 regardless of the validation with it is successful or not.
5244 So we need to skip the pseudos which were used in those failed
5245 validations to avoid unexpected DF info and consequent ICE.
5246 We only set pseudo_replaced_reg[] when the validation is successful
5247 in find_moveable_pseudos, it's enough to check it here. */
5248 if (!other_reg)
5249 continue;
5250 rtx_insn *def_insn = DF_REF_INSN (DF_REG_DEF_CHAIN (i));
5251 /* The use must follow all definitions of OTHER_REG, so we can
5252 insert the new definition immediately after any of them. */
5253 df_ref other_def = DF_REG_DEF_CHAIN (REGNO (other_reg));
5254 rtx_insn *move_insn = DF_REF_INSN (other_def);
5255 rtx_insn *newinsn = emit_insn_after (PATTERN (def_insn), move_insn);
5256 rtx set;
5257 int success;
5258
5259 if (dump_file)
5260 fprintf (dump_file, "moving def of %d (insn %d now) ",
5261 REGNO (other_reg), INSN_UID (def_insn));
5262
5263 delete_insn (move_insn);
5264 while ((other_def = DF_REG_DEF_CHAIN (REGNO (other_reg))))
5265 delete_insn (DF_REF_INSN (other_def));
5266 delete_insn (def_insn);
5267
5268 set = single_set (newinsn);
5269 success = validate_change (newinsn, &SET_DEST (set), other_reg, 0);
5270 gcc_assert (success);
5271 if (dump_file)
5272 fprintf (dump_file, " %d) rather than keep unallocated replacement %d\n",
5273 INSN_UID (newinsn), i);
5274 SET_REG_N_REFS (i, 0);
5275 }
5276
5277 first_moveable_pseudo = last_moveable_pseudo = 0;
5278 }
5279
5280
5281
5282 /* Code dealing with scratches (changing them onto
5283 pseudos and restoring them from the pseudos).
5284
5285 We change scratches into pseudos at the beginning of IRA to
5286 simplify dealing with them (conflicts, hard register assignments).
5287
5288 If the pseudo denoting scratch was spilled it means that we do not
5289 need a hard register for it. Such pseudos are transformed back to
5290 scratches at the end of LRA. */
5291
5292 /* Description of location of a former scratch operand. */
5293 struct sloc
5294 {
5295 rtx_insn *insn; /* Insn where the scratch was. */
5296 int nop; /* Number of the operand which was a scratch. */
5297 unsigned regno; /* regno gnerated instead of scratch */
5298 int icode; /* Original icode from which scratch was removed. */
5299 };
5300
5301 typedef struct sloc *sloc_t;
5302
5303 /* Locations of the former scratches. */
5304 static vec<sloc_t> scratches;
5305
5306 /* Bitmap of scratch regnos. */
5307 static bitmap_head scratch_bitmap;
5308
5309 /* Bitmap of scratch operands. */
5310 static bitmap_head scratch_operand_bitmap;
5311
5312 /* Return true if pseudo REGNO is made of SCRATCH. */
5313 bool
5314 ira_former_scratch_p (int regno)
5315 {
5316 return bitmap_bit_p (&scratch_bitmap, regno);
5317 }
5318
5319 /* Return true if the operand NOP of INSN is a former scratch. */
5320 bool
5321 ira_former_scratch_operand_p (rtx_insn *insn, int nop)
5322 {
5323 return bitmap_bit_p (&scratch_operand_bitmap,
5324 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
5325 }
5326
5327 /* Register operand NOP in INSN as a former scratch. It will be
5328 changed to scratch back, if it is necessary, at the LRA end. */
5329 void
5330 ira_register_new_scratch_op (rtx_insn *insn, int nop, int icode)
5331 {
5332 rtx op = *recog_data.operand_loc[nop];
5333 sloc_t loc = XNEW (struct sloc);
5334 ira_assert (REG_P (op));
5335 loc->insn = insn;
5336 loc->nop = nop;
5337 loc->regno = REGNO (op);
5338 loc->icode = icode;
5339 scratches.safe_push (loc);
5340 bitmap_set_bit (&scratch_bitmap, REGNO (op));
5341 bitmap_set_bit (&scratch_operand_bitmap,
5342 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop);
5343 add_reg_note (insn, REG_UNUSED, op);
5344 }
5345
5346 /* Return true if string STR contains constraint 'X'. */
5347 static bool
5348 contains_X_constraint_p (const char *str)
5349 {
5350 int c;
5351
5352 while ((c = *str))
5353 {
5354 str += CONSTRAINT_LEN (c, str);
5355 if (c == 'X') return true;
5356 }
5357 return false;
5358 }
5359
5360 /* Change INSN's scratches into pseudos and save their location.
5361 Return true if we changed any scratch. */
5362 bool
5363 ira_remove_insn_scratches (rtx_insn *insn, bool all_p, FILE *dump_file,
5364 rtx (*get_reg) (rtx original))
5365 {
5366 int i;
5367 bool insn_changed_p;
5368 rtx reg, *loc;
5369
5370 extract_insn (insn);
5371 insn_changed_p = false;
5372 for (i = 0; i < recog_data.n_operands; i++)
5373 {
5374 loc = recog_data.operand_loc[i];
5375 if (GET_CODE (*loc) == SCRATCH && GET_MODE (*loc) != VOIDmode)
5376 {
5377 if (! all_p && contains_X_constraint_p (recog_data.constraints[i]))
5378 continue;
5379 insn_changed_p = true;
5380 *loc = reg = get_reg (*loc);
5381 ira_register_new_scratch_op (insn, i, INSN_CODE (insn));
5382 if (ira_dump_file != NULL)
5383 fprintf (dump_file,
5384 "Removing SCRATCH to p%u in insn #%u (nop %d)\n",
5385 REGNO (reg), INSN_UID (insn), i);
5386 }
5387 }
5388 return insn_changed_p;
5389 }
5390
5391 /* Return new register of the same mode as ORIGINAL. Used in
5392 remove_scratches. */
5393 static rtx
5394 get_scratch_reg (rtx original)
5395 {
5396 return gen_reg_rtx (GET_MODE (original));
5397 }
5398
5399 /* Change scratches into pseudos and save their location. Return true
5400 if we changed any scratch. */
5401 static bool
5402 remove_scratches (void)
5403 {
5404 bool change_p = false;
5405 basic_block bb;
5406 rtx_insn *insn;
5407
5408 scratches.create (get_max_uid ());
5409 bitmap_initialize (&scratch_bitmap, &reg_obstack);
5410 bitmap_initialize (&scratch_operand_bitmap, &reg_obstack);
5411 FOR_EACH_BB_FN (bb, cfun)
5412 FOR_BB_INSNS (bb, insn)
5413 if (INSN_P (insn)
5414 && ira_remove_insn_scratches (insn, false, ira_dump_file, get_scratch_reg))
5415 {
5416 /* Because we might use DF, we need to keep DF info up to date. */
5417 df_insn_rescan (insn);
5418 change_p = true;
5419 }
5420 return change_p;
5421 }
5422
5423 /* Changes pseudos created by function remove_scratches onto scratches. */
5424 void
5425 ira_restore_scratches (FILE *dump_file)
5426 {
5427 int regno, n;
5428 unsigned i;
5429 rtx *op_loc;
5430 sloc_t loc;
5431
5432 for (i = 0; scratches.iterate (i, &loc); i++)
5433 {
5434 /* Ignore already deleted insns. */
5435 if (NOTE_P (loc->insn)
5436 && NOTE_KIND (loc->insn) == NOTE_INSN_DELETED)
5437 continue;
5438 extract_insn (loc->insn);
5439 if (loc->icode != INSN_CODE (loc->insn))
5440 {
5441 /* The icode doesn't match, which means the insn has been modified
5442 (e.g. register elimination). The scratch cannot be restored. */
5443 continue;
5444 }
5445 op_loc = recog_data.operand_loc[loc->nop];
5446 if (REG_P (*op_loc)
5447 && ((regno = REGNO (*op_loc)) >= FIRST_PSEUDO_REGISTER)
5448 && reg_renumber[regno] < 0)
5449 {
5450 /* It should be only case when scratch register with chosen
5451 constraint 'X' did not get memory or hard register. */
5452 ira_assert (ira_former_scratch_p (regno));
5453 *op_loc = gen_rtx_SCRATCH (GET_MODE (*op_loc));
5454 for (n = 0; n < recog_data.n_dups; n++)
5455 *recog_data.dup_loc[n]
5456 = *recog_data.operand_loc[(int) recog_data.dup_num[n]];
5457 if (dump_file != NULL)
5458 fprintf (dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
5459 INSN_UID (loc->insn), loc->nop);
5460 }
5461 }
5462 for (i = 0; scratches.iterate (i, &loc); i++)
5463 free (loc);
5464 scratches.release ();
5465 bitmap_clear (&scratch_bitmap);
5466 bitmap_clear (&scratch_operand_bitmap);
5467 }
5468
5469
5470
5471 /* If the backend knows where to allocate pseudos for hard
5472 register initial values, register these allocations now. */
5473 static void
5474 allocate_initial_values (void)
5475 {
5476 if (targetm.allocate_initial_value)
5477 {
5478 rtx hreg, preg, x;
5479 int i, regno;
5480
5481 for (i = 0; HARD_REGISTER_NUM_P (i); i++)
5482 {
5483 if (! initial_value_entry (i, &hreg, &preg))
5484 break;
5485
5486 x = targetm.allocate_initial_value (hreg);
5487 regno = REGNO (preg);
5488 if (x && REG_N_SETS (regno) <= 1)
5489 {
5490 if (MEM_P (x))
5491 reg_equiv_memory_loc (regno) = x;
5492 else
5493 {
5494 basic_block bb;
5495 int new_regno;
5496
5497 gcc_assert (REG_P (x));
5498 new_regno = REGNO (x);
5499 reg_renumber[regno] = new_regno;
5500 /* Poke the regno right into regno_reg_rtx so that even
5501 fixed regs are accepted. */
5502 SET_REGNO (preg, new_regno);
5503 /* Update global register liveness information. */
5504 FOR_EACH_BB_FN (bb, cfun)
5505 {
5506 if (REGNO_REG_SET_P (df_get_live_in (bb), regno))
5507 SET_REGNO_REG_SET (df_get_live_in (bb), new_regno);
5508 if (REGNO_REG_SET_P (df_get_live_out (bb), regno))
5509 SET_REGNO_REG_SET (df_get_live_out (bb), new_regno);
5510 }
5511 }
5512 }
5513 }
5514
5515 gcc_checking_assert (! initial_value_entry (FIRST_PSEUDO_REGISTER,
5516 &hreg, &preg));
5517 }
5518 }
5519
5520
5521
5522
5523 /* True when we use LRA instead of reload pass for the current
5524 function. */
5525 bool ira_use_lra_p;
5526
5527 /* True if we have allocno conflicts. It is false for non-optimized
5528 mode or when the conflict table is too big. */
5529 bool ira_conflicts_p;
5530
5531 /* Saved between IRA and reload. */
5532 static int saved_flag_ira_share_spill_slots;
5533
5534 /* Set to true while in IRA. */
5535 bool ira_in_progress = false;
5536
5537 /* Set up array ira_hard_regno_nrefs. */
5538 static void
5539 setup_hard_regno_nrefs (void)
5540 {
5541 int i;
5542
5543 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5544 {
5545 ira_hard_regno_nrefs[i] = 0;
5546 for (df_ref use = DF_REG_USE_CHAIN (i);
5547 use != NULL;
5548 use = DF_REF_NEXT_REG (use))
5549 if (DF_REF_CLASS (use) != DF_REF_ARTIFICIAL
5550 && !(DF_REF_INSN_INFO (use) && DEBUG_INSN_P (DF_REF_INSN (use))))
5551 ira_hard_regno_nrefs[i]++;
5552 for (df_ref def = DF_REG_DEF_CHAIN (i);
5553 def != NULL;
5554 def = DF_REF_NEXT_REG (def))
5555 if (DF_REF_CLASS (def) != DF_REF_ARTIFICIAL
5556 && !(DF_REF_INSN_INFO (def) && DEBUG_INSN_P (DF_REF_INSN (def))))
5557 ira_hard_regno_nrefs[i]++;
5558 }
5559 }
5560
5561 /* This is the main entry of IRA. */
5562 static void
5563 ira (FILE *f)
5564 {
5565 bool loops_p;
5566 int ira_max_point_before_emit;
5567 bool saved_flag_caller_saves = flag_caller_saves;
5568 enum ira_region saved_flag_ira_region = flag_ira_region;
5569 basic_block bb;
5570 edge_iterator ei;
5571 edge e;
5572 bool output_jump_reload_p = false;
5573
5574 setup_hard_regno_nrefs ();
5575 if (ira_use_lra_p)
5576 {
5577 /* First put potential jump output reloads on the output edges
5578 as USE which will be removed at the end of LRA. The major
5579 goal is actually to create BBs for critical edges for LRA and
5580 populate them later by live info. In LRA it will be
5581 difficult to do this. */
5582 FOR_EACH_BB_FN (bb, cfun)
5583 {
5584 rtx_insn *end = BB_END (bb);
5585 if (!JUMP_P (end))
5586 continue;
5587 extract_insn (end);
5588 for (int i = 0; i < recog_data.n_operands; i++)
5589 if (recog_data.operand_type[i] != OP_IN)
5590 {
5591 bool skip_p = false;
5592 FOR_EACH_EDGE (e, ei, bb->succs)
5593 if (EDGE_CRITICAL_P (e)
5594 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
5595 && (e->flags & EDGE_ABNORMAL))
5596 {
5597 skip_p = true;
5598 break;
5599 }
5600 if (skip_p)
5601 break;
5602 output_jump_reload_p = true;
5603 FOR_EACH_EDGE (e, ei, bb->succs)
5604 if (EDGE_CRITICAL_P (e)
5605 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
5606 {
5607 start_sequence ();
5608 /* We need to put some no-op insn here. We can
5609 not put a note as commit_edges insertion will
5610 fail. */
5611 emit_insn (gen_rtx_USE (VOIDmode, const1_rtx));
5612 rtx_insn *insns = get_insns ();
5613 end_sequence ();
5614 insert_insn_on_edge (insns, e);
5615 }
5616 break;
5617 }
5618 }
5619 if (output_jump_reload_p)
5620 commit_edge_insertions ();
5621 }
5622
5623 if (flag_ira_verbose < 10)
5624 {
5625 internal_flag_ira_verbose = flag_ira_verbose;
5626 ira_dump_file = f;
5627 }
5628 else
5629 {
5630 internal_flag_ira_verbose = flag_ira_verbose - 10;
5631 ira_dump_file = stderr;
5632 }
5633
5634 clear_bb_flags ();
5635
5636 /* Determine if the current function is a leaf before running IRA
5637 since this can impact optimizations done by the prologue and
5638 epilogue thus changing register elimination offsets.
5639 Other target callbacks may use crtl->is_leaf too, including
5640 SHRINK_WRAPPING_ENABLED, so initialize as early as possible. */
5641 crtl->is_leaf = leaf_function_p ();
5642
5643 /* Perform target specific PIC register initialization. */
5644 targetm.init_pic_reg ();
5645
5646 ira_conflicts_p = optimize > 0;
5647
5648 /* Determine the number of pseudos actually requiring coloring. */
5649 unsigned int num_used_regs = 0;
5650 for (unsigned int i = FIRST_PSEUDO_REGISTER; i < DF_REG_SIZE (df); i++)
5651 if (DF_REG_DEF_COUNT (i) || DF_REG_USE_COUNT (i))
5652 num_used_regs++;
5653
5654 /* If there are too many pseudos and/or basic blocks (e.g. 10K pseudos and
5655 10K blocks or 100K pseudos and 1K blocks) or we have too many function
5656 insns, we will use simplified and faster algorithms in LRA. */
5657 lra_simple_p
5658 = (ira_use_lra_p
5659 && (num_used_regs >= (1U << 26) / last_basic_block_for_fn (cfun)
5660 /* max uid is a good evaluation of the number of insns as most
5661 optimizations are done on tree-SSA level. */
5662 || ((uint64_t) get_max_uid ()
5663 > (uint64_t) param_ira_simple_lra_insn_threshold * 1000)));
5664
5665 if (lra_simple_p)
5666 {
5667 /* It permits to skip live range splitting in LRA. */
5668 flag_caller_saves = false;
5669 /* There is no sense to do regional allocation when we use
5670 simplified LRA. */
5671 flag_ira_region = IRA_REGION_ONE;
5672 ira_conflicts_p = false;
5673 }
5674
5675 #ifndef IRA_NO_OBSTACK
5676 gcc_obstack_init (&ira_obstack);
5677 #endif
5678 bitmap_obstack_initialize (&ira_bitmap_obstack);
5679
5680 /* LRA uses its own infrastructure to handle caller save registers. */
5681 if (flag_caller_saves && !ira_use_lra_p)
5682 init_caller_save ();
5683
5684 setup_prohibited_mode_move_regs ();
5685 decrease_live_ranges_number ();
5686 df_note_add_problem ();
5687
5688 /* DF_LIVE can't be used in the register allocator, too many other
5689 parts of the compiler depend on using the "classic" liveness
5690 interpretation of the DF_LR problem. See PR38711.
5691 Remove the problem, so that we don't spend time updating it in
5692 any of the df_analyze() calls during IRA/LRA. */
5693 if (optimize > 1)
5694 df_remove_problem (df_live);
5695 gcc_checking_assert (df_live == NULL);
5696
5697 if (flag_checking)
5698 df->changeable_flags |= DF_VERIFY_SCHEDULED;
5699
5700 df_analyze ();
5701
5702 init_reg_equiv ();
5703 if (ira_conflicts_p)
5704 {
5705 calculate_dominance_info (CDI_DOMINATORS);
5706
5707 if (split_live_ranges_for_shrink_wrap ())
5708 df_analyze ();
5709
5710 free_dominance_info (CDI_DOMINATORS);
5711 }
5712
5713 df_clear_flags (DF_NO_INSN_RESCAN);
5714
5715 indirect_jump_optimize ();
5716 if (delete_trivially_dead_insns (get_insns (), max_reg_num ()))
5717 df_analyze ();
5718
5719 regstat_init_n_sets_and_refs ();
5720 regstat_compute_ri ();
5721
5722 /* If we are not optimizing, then this is the only place before
5723 register allocation where dataflow is done. And that is needed
5724 to generate these warnings. */
5725 if (warn_clobbered)
5726 generate_setjmp_warnings ();
5727
5728 /* update_equiv_regs can use reg classes of pseudos and they are set up in
5729 register pressure sensitive scheduling and loop invariant motion and in
5730 live range shrinking. This info can become obsolete if we add new pseudos
5731 since the last set up. Recalculate it again if the new pseudos were
5732 added. */
5733 if (resize_reg_info () && (flag_sched_pressure || flag_live_range_shrinkage
5734 || flag_ira_loop_pressure))
5735 ira_set_pseudo_classes (true, ira_dump_file);
5736
5737 init_alias_analysis ();
5738 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
5739 reg_equiv = XCNEWVEC (struct equivalence, max_reg_num ());
5740 update_equiv_regs_prescan ();
5741 update_equiv_regs ();
5742
5743 /* Don't move insns if live range shrinkage or register
5744 pressure-sensitive scheduling were done because it will not
5745 improve allocation but likely worsen insn scheduling. */
5746 if (optimize
5747 && !flag_live_range_shrinkage
5748 && !(flag_sched_pressure && flag_schedule_insns))
5749 combine_and_move_insns ();
5750
5751 /* Gather additional equivalences with memory. */
5752 if (optimize && flag_expensive_optimizations)
5753 add_store_equivs ();
5754
5755 loop_optimizer_finalize ();
5756 free_dominance_info (CDI_DOMINATORS);
5757 end_alias_analysis ();
5758 free (reg_equiv);
5759
5760 /* Once max_regno changes, we need to free and re-init/re-compute
5761 some data structures like regstat_n_sets_and_refs and reg_info_p. */
5762 auto regstat_recompute_for_max_regno = []() {
5763 regstat_free_n_sets_and_refs ();
5764 regstat_free_ri ();
5765 regstat_init_n_sets_and_refs ();
5766 regstat_compute_ri ();
5767 resize_reg_info ();
5768 };
5769
5770 int max_regno_before_rm = max_reg_num ();
5771 if (ira_use_lra_p && remove_scratches ())
5772 {
5773 ira_expand_reg_equiv ();
5774 /* For now remove_scatches is supposed to create pseudos when it
5775 succeeds, assert this happens all the time. Once it doesn't
5776 hold, we should guard the regstat recompute for the case
5777 max_regno changes. */
5778 gcc_assert (max_regno_before_rm != max_reg_num ());
5779 regstat_recompute_for_max_regno ();
5780 }
5781
5782 setup_reg_equiv ();
5783 grow_reg_equivs ();
5784 setup_reg_equiv_init ();
5785
5786 allocated_reg_info_size = max_reg_num ();
5787
5788 /* It is not worth to do such improvement when we use a simple
5789 allocation because of -O0 usage or because the function is too
5790 big. */
5791 if (ira_conflicts_p)
5792 find_moveable_pseudos ();
5793
5794 max_regno_before_ira = max_reg_num ();
5795 ira_setup_eliminable_regset ();
5796
5797 ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
5798 ira_load_cost = ira_store_cost = ira_shuffle_cost = 0;
5799 ira_move_loops_num = ira_additional_jumps_num = 0;
5800
5801 ira_assert (current_loops == NULL);
5802 if (flag_ira_region == IRA_REGION_ALL || flag_ira_region == IRA_REGION_MIXED)
5803 loop_optimizer_init (AVOID_CFG_MODIFICATIONS | LOOPS_HAVE_RECORDED_EXITS);
5804
5805 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
5806 fprintf (ira_dump_file, "Building IRA IR\n");
5807 loops_p = ira_build ();
5808
5809 ira_assert (ira_conflicts_p || !loops_p);
5810
5811 saved_flag_ira_share_spill_slots = flag_ira_share_spill_slots;
5812 if (too_high_register_pressure_p () || cfun->calls_setjmp)
5813 /* It is just wasting compiler's time to pack spilled pseudos into
5814 stack slots in this case -- prohibit it. We also do this if
5815 there is setjmp call because a variable not modified between
5816 setjmp and longjmp the compiler is required to preserve its
5817 value and sharing slots does not guarantee it. */
5818 flag_ira_share_spill_slots = false;
5819
5820 ira_color ();
5821
5822 ira_max_point_before_emit = ira_max_point;
5823
5824 ira_initiate_emit_data ();
5825
5826 ira_emit (loops_p);
5827
5828 max_regno = max_reg_num ();
5829 if (ira_conflicts_p)
5830 {
5831 if (! loops_p)
5832 {
5833 if (! ira_use_lra_p)
5834 ira_initiate_assign ();
5835 }
5836 else
5837 {
5838 expand_reg_info ();
5839
5840 if (ira_use_lra_p)
5841 {
5842 ira_allocno_t a;
5843 ira_allocno_iterator ai;
5844
5845 FOR_EACH_ALLOCNO (a, ai)
5846 {
5847 int old_regno = ALLOCNO_REGNO (a);
5848 int new_regno = REGNO (ALLOCNO_EMIT_DATA (a)->reg);
5849
5850 ALLOCNO_REGNO (a) = new_regno;
5851
5852 if (old_regno != new_regno)
5853 setup_reg_classes (new_regno, reg_preferred_class (old_regno),
5854 reg_alternate_class (old_regno),
5855 reg_allocno_class (old_regno));
5856 }
5857 }
5858 else
5859 {
5860 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
5861 fprintf (ira_dump_file, "Flattening IR\n");
5862 ira_flattening (max_regno_before_ira, ira_max_point_before_emit);
5863 }
5864 /* New insns were generated: add notes and recalculate live
5865 info. */
5866 df_analyze ();
5867
5868 /* ??? Rebuild the loop tree, but why? Does the loop tree
5869 change if new insns were generated? Can that be handled
5870 by updating the loop tree incrementally? */
5871 loop_optimizer_finalize ();
5872 free_dominance_info (CDI_DOMINATORS);
5873 loop_optimizer_init (AVOID_CFG_MODIFICATIONS
5874 | LOOPS_HAVE_RECORDED_EXITS);
5875
5876 if (! ira_use_lra_p)
5877 {
5878 setup_allocno_assignment_flags ();
5879 ira_initiate_assign ();
5880 ira_reassign_conflict_allocnos (max_regno);
5881 }
5882 }
5883 }
5884
5885 ira_finish_emit_data ();
5886
5887 setup_reg_renumber ();
5888
5889 calculate_allocation_cost ();
5890
5891 #ifdef ENABLE_IRA_CHECKING
5892 if (ira_conflicts_p && ! ira_use_lra_p)
5893 /* Opposite to reload pass, LRA does not use any conflict info
5894 from IRA. We don't rebuild conflict info for LRA (through
5895 ira_flattening call) and cannot use the check here. We could
5896 rebuild this info for LRA in the check mode but there is a risk
5897 that code generated with the check and without it will be a bit
5898 different. Calling ira_flattening in any mode would be a
5899 wasting CPU time. So do not check the allocation for LRA. */
5900 check_allocation ();
5901 #endif
5902
5903 if (max_regno != max_regno_before_ira)
5904 regstat_recompute_for_max_regno ();
5905
5906 overall_cost_before = ira_overall_cost;
5907 if (! ira_conflicts_p)
5908 grow_reg_equivs ();
5909 else
5910 {
5911 fix_reg_equiv_init ();
5912
5913 #ifdef ENABLE_IRA_CHECKING
5914 print_redundant_copies ();
5915 #endif
5916 if (! ira_use_lra_p)
5917 {
5918 ira_spilled_reg_stack_slots_num = 0;
5919 ira_spilled_reg_stack_slots
5920 = ((class ira_spilled_reg_stack_slot *)
5921 ira_allocate (max_regno
5922 * sizeof (class ira_spilled_reg_stack_slot)));
5923 memset ((void *)ira_spilled_reg_stack_slots, 0,
5924 max_regno * sizeof (class ira_spilled_reg_stack_slot));
5925 }
5926 }
5927 allocate_initial_values ();
5928
5929 /* See comment for find_moveable_pseudos call. */
5930 if (ira_conflicts_p)
5931 move_unallocated_pseudos ();
5932
5933 /* Restore original values. */
5934 if (lra_simple_p)
5935 {
5936 flag_caller_saves = saved_flag_caller_saves;
5937 flag_ira_region = saved_flag_ira_region;
5938 }
5939 }
5940
5941 /* Modify asm goto to avoid further trouble with this insn. We can
5942 not replace the insn by USE as in other asm insns as we still
5943 need to keep CFG consistency. */
5944 void
5945 ira_nullify_asm_goto (rtx_insn *insn)
5946 {
5947 ira_assert (JUMP_P (insn) && INSN_CODE (insn) < 0);
5948 rtx tmp = extract_asm_operands (PATTERN (insn));
5949 PATTERN (insn) = gen_rtx_ASM_OPERANDS (VOIDmode, ggc_strdup (""), "", 0,
5950 rtvec_alloc (0),
5951 rtvec_alloc (0),
5952 ASM_OPERANDS_LABEL_VEC (tmp),
5953 ASM_OPERANDS_SOURCE_LOCATION(tmp));
5954 }
5955
5956 static void
5957 do_reload (void)
5958 {
5959 basic_block bb;
5960 bool need_dce;
5961 unsigned pic_offset_table_regno = INVALID_REGNUM;
5962
5963 if (flag_ira_verbose < 10)
5964 ira_dump_file = dump_file;
5965
5966 /* If pic_offset_table_rtx is a pseudo register, then keep it so
5967 after reload to avoid possible wrong usages of hard reg assigned
5968 to it. */
5969 if (pic_offset_table_rtx
5970 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
5971 pic_offset_table_regno = REGNO (pic_offset_table_rtx);
5972
5973 timevar_push (TV_RELOAD);
5974 if (ira_use_lra_p)
5975 {
5976 if (current_loops != NULL)
5977 {
5978 loop_optimizer_finalize ();
5979 free_dominance_info (CDI_DOMINATORS);
5980 }
5981 FOR_ALL_BB_FN (bb, cfun)
5982 bb->loop_father = NULL;
5983 current_loops = NULL;
5984
5985 ira_destroy ();
5986
5987 lra (ira_dump_file, internal_flag_ira_verbose);
5988 /* ???!!! Move it before lra () when we use ira_reg_equiv in
5989 LRA. */
5990 vec_free (reg_equivs);
5991 reg_equivs = NULL;
5992 need_dce = false;
5993 }
5994 else
5995 {
5996 df_set_flags (DF_NO_INSN_RESCAN);
5997 build_insn_chain ();
5998
5999 need_dce = reload (get_insns (), ira_conflicts_p);
6000 }
6001
6002 timevar_pop (TV_RELOAD);
6003
6004 timevar_push (TV_IRA);
6005
6006 if (ira_conflicts_p && ! ira_use_lra_p)
6007 {
6008 ira_free (ira_spilled_reg_stack_slots);
6009 ira_finish_assign ();
6010 }
6011
6012 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL
6013 && overall_cost_before != ira_overall_cost)
6014 fprintf (ira_dump_file, "+++Overall after reload %" PRId64 "\n",
6015 ira_overall_cost);
6016
6017 flag_ira_share_spill_slots = saved_flag_ira_share_spill_slots;
6018
6019 if (! ira_use_lra_p)
6020 {
6021 ira_destroy ();
6022 if (current_loops != NULL)
6023 {
6024 loop_optimizer_finalize ();
6025 free_dominance_info (CDI_DOMINATORS);
6026 }
6027 FOR_ALL_BB_FN (bb, cfun)
6028 bb->loop_father = NULL;
6029 current_loops = NULL;
6030
6031 regstat_free_ri ();
6032 regstat_free_n_sets_and_refs ();
6033 }
6034
6035 if (optimize)
6036 cleanup_cfg (CLEANUP_EXPENSIVE);
6037
6038 finish_reg_equiv ();
6039
6040 bitmap_obstack_release (&ira_bitmap_obstack);
6041 #ifndef IRA_NO_OBSTACK
6042 obstack_free (&ira_obstack, NULL);
6043 #endif
6044
6045 /* The code after the reload has changed so much that at this point
6046 we might as well just rescan everything. Note that
6047 df_rescan_all_insns is not going to help here because it does not
6048 touch the artificial uses and defs. */
6049 df_finish_pass (true);
6050 df_scan_alloc (NULL);
6051 df_scan_blocks ();
6052
6053 if (optimize > 1)
6054 {
6055 df_live_add_problem ();
6056 df_live_set_all_dirty ();
6057 }
6058
6059 if (optimize)
6060 df_analyze ();
6061
6062 if (need_dce && optimize)
6063 run_fast_dce ();
6064
6065 /* Diagnose uses of the hard frame pointer when it is used as a global
6066 register. Often we can get away with letting the user appropriate
6067 the frame pointer, but we should let them know when code generation
6068 makes that impossible. */
6069 if (global_regs[HARD_FRAME_POINTER_REGNUM] && frame_pointer_needed)
6070 {
6071 tree decl = global_regs_decl[HARD_FRAME_POINTER_REGNUM];
6072 error_at (DECL_SOURCE_LOCATION (current_function_decl),
6073 "frame pointer required, but reserved");
6074 inform (DECL_SOURCE_LOCATION (decl), "for %qD", decl);
6075 }
6076
6077 /* If we are doing generic stack checking, give a warning if this
6078 function's frame size is larger than we expect. */
6079 if (flag_stack_check == GENERIC_STACK_CHECK)
6080 {
6081 poly_int64 size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
6082
6083 for (int i = 0; i < FIRST_PSEUDO_REGISTER; i++)
6084 if (df_regs_ever_live_p (i)
6085 && !fixed_regs[i]
6086 && !crtl->abi->clobbers_full_reg_p (i))
6087 size += UNITS_PER_WORD;
6088
6089 if (constant_lower_bound (size) > STACK_CHECK_MAX_FRAME_SIZE)
6090 warning (0, "frame size too large for reliable stack checking");
6091 }
6092
6093 if (pic_offset_table_regno != INVALID_REGNUM)
6094 pic_offset_table_rtx = gen_rtx_REG (Pmode, pic_offset_table_regno);
6095
6096 timevar_pop (TV_IRA);
6097 }
6098
6099 /* Run the integrated register allocator. */
6100
6101 namespace {
6102
6103 const pass_data pass_data_ira =
6104 {
6105 RTL_PASS, /* type */
6106 "ira", /* name */
6107 OPTGROUP_NONE, /* optinfo_flags */
6108 TV_IRA, /* tv_id */
6109 0, /* properties_required */
6110 0, /* properties_provided */
6111 0, /* properties_destroyed */
6112 0, /* todo_flags_start */
6113 TODO_do_not_ggc_collect, /* todo_flags_finish */
6114 };
6115
6116 class pass_ira : public rtl_opt_pass
6117 {
6118 public:
6119 pass_ira (gcc::context *ctxt)
6120 : rtl_opt_pass (pass_data_ira, ctxt)
6121 {}
6122
6123 /* opt_pass methods: */
6124 bool gate (function *) final override
6125 {
6126 return !targetm.no_register_allocation;
6127 }
6128 unsigned int execute (function *) final override
6129 {
6130 ira_in_progress = true;
6131 ira (dump_file);
6132 ira_in_progress = false;
6133 return 0;
6134 }
6135
6136 }; // class pass_ira
6137
6138 } // anon namespace
6139
6140 rtl_opt_pass *
6141 make_pass_ira (gcc::context *ctxt)
6142 {
6143 return new pass_ira (ctxt);
6144 }
6145
6146 namespace {
6147
6148 const pass_data pass_data_reload =
6149 {
6150 RTL_PASS, /* type */
6151 "reload", /* name */
6152 OPTGROUP_NONE, /* optinfo_flags */
6153 TV_RELOAD, /* tv_id */
6154 0, /* properties_required */
6155 0, /* properties_provided */
6156 0, /* properties_destroyed */
6157 0, /* todo_flags_start */
6158 0, /* todo_flags_finish */
6159 };
6160
6161 class pass_reload : public rtl_opt_pass
6162 {
6163 public:
6164 pass_reload (gcc::context *ctxt)
6165 : rtl_opt_pass (pass_data_reload, ctxt)
6166 {}
6167
6168 /* opt_pass methods: */
6169 bool gate (function *) final override
6170 {
6171 return !targetm.no_register_allocation;
6172 }
6173 unsigned int execute (function *) final override
6174 {
6175 do_reload ();
6176 return 0;
6177 }
6178
6179 }; // class pass_reload
6180
6181 } // anon namespace
6182
6183 rtl_opt_pass *
6184 make_pass_reload (gcc::context *ctxt)
6185 {
6186 return new pass_reload (ctxt);
6187 }