Branch data Line data Source code
1 : : /* Integrated Register Allocator (IRA) entry point.
2 : : Copyright (C) 2006-2025 Free Software Foundation, Inc.
3 : : Contributed by Vladimir Makarov <vmakarov@redhat.com>.
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 : 214550 : setup_reg_mode_hard_regset (void)
445 : : {
446 : 214550 : int i, m, hard_regno;
447 : :
448 : 26818750 : for (m = 0; m < NUM_MACHINE_MODES; m++)
449 : 2474190600 : for (hard_regno = 0; hard_regno < FIRST_PSEUDO_REGISTER; hard_regno++)
450 : : {
451 : 2447586400 : CLEAR_HARD_REG_SET (ira_reg_mode_hard_regset[hard_regno][m]);
452 : 7408650768 : for (i = hard_regno_nregs (hard_regno, (machine_mode) m) - 1;
453 : 7408650768 : i >= 0; i--)
454 : 4961064368 : if (hard_regno + i < FIRST_PSEUDO_REGISTER)
455 : 4550639210 : SET_HARD_REG_BIT (ira_reg_mode_hard_regset[hard_regno][m],
456 : : hard_regno + i);
457 : : }
458 : 214550 : }
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 : 214550 : setup_class_hard_regs (void)
467 : : {
468 : 214550 : int cl, i, hard_regno, n;
469 : 214550 : HARD_REG_SET processed_hard_reg_set;
470 : :
471 : 214550 : ira_assert (SHRT_MAX >= FIRST_PSEUDO_REGISTER);
472 : 7509250 : for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
473 : : {
474 : 7294700 : temp_hard_regset = reg_class_contents[cl] & ~no_unit_alloc_regs;
475 : 7294700 : CLEAR_HARD_REG_SET (processed_hard_reg_set);
476 : 678407100 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
477 : : {
478 : 671112400 : ira_non_ordered_class_hard_regs[cl][i] = -1;
479 : 671112400 : ira_class_hard_reg_index[cl][i] = -1;
480 : : }
481 : 678407100 : for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
482 : : {
483 : : #ifdef REG_ALLOC_ORDER
484 : 671112400 : hard_regno = reg_alloc_order[i];
485 : : #else
486 : : hard_regno = i;
487 : : #endif
488 : 671112400 : if (TEST_HARD_REG_BIT (processed_hard_reg_set, hard_regno))
489 : 29178800 : continue;
490 : 641933600 : SET_HARD_REG_BIT (processed_hard_reg_set, hard_regno);
491 : 641933600 : if (! TEST_HARD_REG_BIT (temp_hard_regset, hard_regno))
492 : 568054191 : ira_class_hard_reg_index[cl][hard_regno] = -1;
493 : : else
494 : : {
495 : 73879409 : ira_class_hard_reg_index[cl][hard_regno] = n;
496 : 73879409 : ira_class_hard_regs[cl][n++] = hard_regno;
497 : : }
498 : : }
499 : 7294700 : ira_class_hard_regs_num[cl] = n;
500 : 678407100 : for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
501 : 671112400 : if (TEST_HARD_REG_BIT (temp_hard_regset, i))
502 : 73879409 : ira_non_ordered_class_hard_regs[cl][n++] = i;
503 : 7294700 : ira_assert (ira_class_hard_regs_num[cl] == n);
504 : : }
505 : 214550 : }
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 : 214550 : setup_alloc_regs (bool use_hard_frame_p)
512 : : {
513 : : #ifdef ADJUST_REG_ALLOC_ORDER
514 : 214550 : ADJUST_REG_ALLOC_ORDER;
515 : : #endif
516 : 214550 : no_unit_alloc_regs = fixed_nonglobal_reg_set;
517 : 214550 : if (! use_hard_frame_p)
518 : 68024 : add_to_hard_reg_set (&no_unit_alloc_regs, Pmode,
519 : : HARD_FRAME_POINTER_REGNUM);
520 : 214550 : setup_class_hard_regs ();
521 : 214550 : }
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 : 214550 : setup_reg_subclasses (void)
531 : : {
532 : 214550 : int i, j;
533 : 214550 : HARD_REG_SET temp_hard_regset2;
534 : :
535 : 7509250 : for (i = 0; i < N_REG_CLASSES; i++)
536 : 255314500 : for (j = 0; j < N_REG_CLASSES; j++)
537 : 248019800 : alloc_reg_class_subclasses[i][j] = LIM_REG_CLASSES;
538 : :
539 : 7509250 : for (i = 0; i < N_REG_CLASSES; i++)
540 : : {
541 : 7294700 : if (i == (int) NO_REGS)
542 : 214550 : continue;
543 : :
544 : 7080150 : temp_hard_regset = reg_class_contents[i] & ~no_unit_alloc_regs;
545 : 14160300 : if (hard_reg_set_empty_p (temp_hard_regset))
546 : 426918 : continue;
547 : 232863120 : for (j = 0; j < N_REG_CLASSES; j++)
548 : 226209888 : if (i != j)
549 : : {
550 : 219556656 : enum reg_class *p;
551 : :
552 : 219556656 : temp_hard_regset2 = reg_class_contents[j] & ~no_unit_alloc_regs;
553 : 439113312 : if (! hard_reg_set_subset_p (temp_hard_regset,
554 : : temp_hard_regset2))
555 : 167295981 : continue;
556 : 52260675 : p = &alloc_reg_class_subclasses[j][0];
557 : 502678707 : while (*p != LIM_REG_CLASSES) p++;
558 : 52260675 : *p = (enum reg_class) i;
559 : : }
560 : : }
561 : 214550 : }
562 : :
563 : :
564 : :
565 : : /* Set up IRA_MEMORY_MOVE_COST and IRA_MAX_MEMORY_MOVE_COST. */
566 : : static void
567 : 214550 : setup_class_subset_and_memory_move_costs (void)
568 : : {
569 : 214550 : int cl, cl2, mode, cost;
570 : 214550 : HARD_REG_SET temp_hard_regset2;
571 : :
572 : 26818750 : for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
573 : 26604200 : ira_memory_move_cost[mode][NO_REGS][0]
574 : 26604200 : = ira_memory_move_cost[mode][NO_REGS][1] = SHRT_MAX;
575 : 7509250 : for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
576 : : {
577 : 7294700 : if (cl != (int) NO_REGS)
578 : 885018750 : for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
579 : : {
580 : 1755877200 : ira_max_memory_move_cost[mode][cl][0]
581 : 877938600 : = ira_memory_move_cost[mode][cl][0]
582 : 877938600 : = memory_move_cost ((machine_mode) mode,
583 : : (reg_class_t) cl, false);
584 : 1755877200 : ira_max_memory_move_cost[mode][cl][1]
585 : 877938600 : = ira_memory_move_cost[mode][cl][1]
586 : 877938600 : = 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 : 877938600 : if (!targetm.hard_regno_mode_ok (ira_class_hard_regs[cl][0],
592 : : (machine_mode) mode))
593 : 647394605 : continue;
594 : :
595 : 230543995 : if (ira_memory_move_cost[mode][NO_REGS][0]
596 : 230543995 : > ira_memory_move_cost[mode][cl][0])
597 : 12949804 : ira_max_memory_move_cost[mode][NO_REGS][0]
598 : 12949804 : = ira_memory_move_cost[mode][NO_REGS][0]
599 : 12949804 : = ira_memory_move_cost[mode][cl][0];
600 : 230543995 : if (ira_memory_move_cost[mode][NO_REGS][1]
601 : 230543995 : > ira_memory_move_cost[mode][cl][1])
602 : 12940925 : ira_max_memory_move_cost[mode][NO_REGS][1]
603 : 12940925 : = ira_memory_move_cost[mode][NO_REGS][1]
604 : 12940925 : = ira_memory_move_cost[mode][cl][1];
605 : : }
606 : : }
607 : 7509250 : for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
608 : 255314500 : for (cl2 = (int) N_REG_CLASSES - 1; cl2 >= 0; cl2--)
609 : : {
610 : 248019800 : temp_hard_regset = reg_class_contents[cl] & ~no_unit_alloc_regs;
611 : 248019800 : temp_hard_regset2 = reg_class_contents[cl2] & ~no_unit_alloc_regs;
612 : 248019800 : ira_class_subset_p[cl][cl2]
613 : 248019800 : = hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2);
614 : 248019800 : if (! hard_reg_set_empty_p (temp_hard_regset2)
615 : 474229688 : && hard_reg_set_subset_p (reg_class_contents[cl2],
616 : : reg_class_contents[cl]))
617 : 6676097250 : for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
618 : : {
619 : 6622688472 : cost = ira_memory_move_cost[mode][cl2][0];
620 : 6622688472 : if (cost > ira_max_memory_move_cost[mode][cl][0])
621 : 112959154 : ira_max_memory_move_cost[mode][cl][0] = cost;
622 : 6622688472 : cost = ira_memory_move_cost[mode][cl2][1];
623 : 6622688472 : if (cost > ira_max_memory_move_cost[mode][cl][1])
624 : 113008215 : ira_max_memory_move_cost[mode][cl][1] = cost;
625 : : }
626 : : }
627 : 7509250 : for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
628 : 911837500 : for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
629 : : {
630 : 904542800 : ira_memory_move_cost[mode][cl][0]
631 : 904542800 : = ira_max_memory_move_cost[mode][cl][0];
632 : 904542800 : ira_memory_move_cost[mode][cl][1]
633 : 904542800 : = ira_max_memory_move_cost[mode][cl][1];
634 : : }
635 : 214550 : setup_reg_subclasses ();
636 : 214550 : }
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 : 206571894 : ira_allocate (size_t len)
656 : : {
657 : 206571894 : void *res;
658 : :
659 : : #ifndef IRA_NO_OBSTACK
660 : : res = obstack_alloc (&ira_obstack, len);
661 : : #else
662 : 206571894 : res = xmalloc (len);
663 : : #endif
664 : 206571894 : return res;
665 : : }
666 : :
667 : : /* Free memory ADDR allocated for IRA data. */
668 : : void
669 : 206571894 : ira_free (void *addr ATTRIBUTE_UNUSED)
670 : : {
671 : : #ifndef IRA_NO_OBSTACK
672 : : /* do nothing */
673 : : #else
674 : 206571894 : free (addr);
675 : : #endif
676 : 206571894 : }
677 : :
678 : :
679 : : /* Allocate and returns bitmap for IRA. */
680 : : bitmap
681 : 10620328 : ira_allocate_bitmap (void)
682 : : {
683 : 10620328 : return BITMAP_ALLOC (&ira_bitmap_obstack);
684 : : }
685 : :
686 : : /* Free bitmap B allocated for IRA. */
687 : : void
688 : 10620328 : ira_free_bitmap (bitmap b ATTRIBUTE_UNUSED)
689 : : {
690 : : /* do nothing */
691 : 10620328 : }
692 : :
693 : :
694 : :
695 : : /* Output information about allocation of all allocnos (except for
696 : : caps) into file F. */
697 : : void
698 : 95 : ira_print_disposition (FILE *f)
699 : : {
700 : 95 : int i, n, max_regno;
701 : 95 : ira_allocno_t a;
702 : 95 : basic_block bb;
703 : :
704 : 95 : fprintf (f, "Disposition:");
705 : 95 : max_regno = max_reg_num ();
706 : 1967 : for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
707 : 1777 : for (a = ira_regno_allocno_map[i];
708 : 2370 : a != NULL;
709 : 593 : a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
710 : : {
711 : 593 : if (n % 4 == 0)
712 : 178 : fprintf (f, "\n");
713 : 593 : n++;
714 : 593 : fprintf (f, " %4d:r%-4d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
715 : 593 : if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
716 : 0 : fprintf (f, "b%-3d", bb->index);
717 : : else
718 : 593 : fprintf (f, "l%-3d", ALLOCNO_LOOP_TREE_NODE (a)->loop_num);
719 : 593 : if (ALLOCNO_HARD_REGNO (a) >= 0)
720 : 592 : fprintf (f, " %3d", ALLOCNO_HARD_REGNO (a));
721 : : else
722 : 1 : fprintf (f, " mem");
723 : : }
724 : 95 : fprintf (f, "\n");
725 : 95 : }
726 : :
727 : : /* Outputs information about allocation of all allocnos into
728 : : stderr. */
729 : : void
730 : 0 : ira_debug_disposition (void)
731 : : {
732 : 0 : ira_print_disposition (stderr);
733 : 0 : }
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 : 214550 : setup_stack_reg_pressure_class (void)
745 : : {
746 : 214550 : ira_stack_reg_pressure_class = NO_REGS;
747 : : #ifdef STACK_REGS
748 : 214550 : {
749 : 214550 : int i, best, size;
750 : 214550 : enum reg_class cl;
751 : 214550 : HARD_REG_SET temp_hard_regset2;
752 : :
753 : 214550 : CLEAR_HARD_REG_SET (temp_hard_regset);
754 : 1930950 : for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
755 : 1716400 : SET_HARD_REG_BIT (temp_hard_regset, i);
756 : : best = 0;
757 : 1074145 : for (i = 0; i < ira_pressure_classes_num; i++)
758 : : {
759 : 859595 : cl = ira_pressure_classes[i];
760 : 859595 : temp_hard_regset2 = temp_hard_regset & reg_class_contents[cl];
761 : 859595 : size = hard_reg_set_size (temp_hard_regset2);
762 : 859595 : if (best < size)
763 : : {
764 : 213882 : best = size;
765 : 213882 : ira_stack_reg_pressure_class = cl;
766 : : }
767 : : }
768 : : }
769 : : #endif
770 : 214550 : }
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 : 214550 : setup_pressure_classes (void)
787 : : {
788 : 214550 : int cost, i, n, curr;
789 : 214550 : int cl, cl2;
790 : 214550 : enum reg_class pressure_classes[N_REG_CLASSES];
791 : 214550 : int m;
792 : 214550 : HARD_REG_SET temp_hard_regset2;
793 : 214550 : bool insert_p;
794 : :
795 : 214550 : if (targetm.compute_pressure_classes)
796 : 0 : n = targetm.compute_pressure_classes (pressure_classes);
797 : : else
798 : : {
799 : : n = 0;
800 : 7509250 : for (cl = 0; cl < N_REG_CLASSES; cl++)
801 : : {
802 : 7294700 : if (ira_class_hard_regs_num[cl] == 0)
803 : 641468 : continue;
804 : 6653232 : 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 : 4724346 : && 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 : 219414860 : for (m = 0; m < NUM_MACHINE_MODES; m++)
817 : : {
818 : 218128210 : temp_hard_regset
819 : 218128210 : = (reg_class_contents[cl]
820 : 218128210 : & ~(no_unit_alloc_regs
821 : 218128210 : | ira_prohibited_class_mode_regs[cl][m]));
822 : 436256420 : if (hard_reg_set_empty_p (temp_hard_regset))
823 : 161254723 : continue;
824 : 56873487 : ira_init_register_move_cost_if_necessary ((machine_mode) m);
825 : 56873487 : cost = ira_register_move_cost[m][cl][cl];
826 : 56873487 : if (cost <= ira_max_memory_move_cost[m][cl][1]
827 : 53653132 : || cost <= ira_max_memory_move_cost[m][cl][0])
828 : : break;
829 : : }
830 : 4507005 : if (m >= NUM_MACHINE_MODES)
831 : 1286650 : continue;
832 : : }
833 : 5366582 : curr = 0;
834 : 5366582 : insert_p = true;
835 : 5366582 : 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 : 19256002 : for (i = 0; i < n; i++)
844 : : {
845 : 13889420 : cl2 = pressure_classes[i];
846 : 13889420 : temp_hard_regset2 = (reg_class_contents[cl2]
847 : 13889420 : & ~no_unit_alloc_regs);
848 : 13889420 : if (hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2)
849 : 15035143 : && (temp_hard_regset != temp_hard_regset2
850 : 1078116 : || cl2 == (int) GENERAL_REGS))
851 : : {
852 : 711116 : pressure_classes[curr++] = (enum reg_class) cl2;
853 : 711116 : insert_p = false;
854 : 711116 : continue;
855 : : }
856 : 16545163 : if (hard_reg_set_subset_p (temp_hard_regset2, temp_hard_regset)
857 : 16974175 : && (temp_hard_regset2 != temp_hard_regset
858 : 434607 : || cl == (int) GENERAL_REGS))
859 : 3366859 : continue;
860 : 9811445 : if (temp_hard_regset2 == temp_hard_regset)
861 : 429012 : insert_p = false;
862 : 9811445 : 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 : 5366582 : if (insert_p)
868 : 4226454 : pressure_classes[curr++] = (enum reg_class) cl;
869 : : n = curr;
870 : : }
871 : : }
872 : : #ifdef ENABLE_IRA_CHECKING
873 : 214550 : {
874 : 214550 : 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 : 858200 : CLEAR_HARD_REG_SET (temp_hard_regset);
880 : 214550 : CLEAR_HARD_REG_SET (temp_hard_regset2);
881 : 214550 : ignore_hard_regs = no_unit_alloc_regs;
882 : 7509250 : 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 : 207380686 : for (m = 0; m < NUM_MACHINE_MODES; m++)
888 : 206739218 : if (contains_reg_of_mode[cl][m])
889 : : break;
890 : 7294700 : if (m >= NUM_MACHINE_MODES)
891 : : {
892 : 641468 : ignore_hard_regs |= reg_class_contents[cl];
893 : 641468 : continue;
894 : : }
895 : 31180868 : for (i = 0; i < n; i++)
896 : 25387231 : if ((int) pressure_classes[i] == cl)
897 : : break;
898 : 6653232 : temp_hard_regset2 |= reg_class_contents[cl];
899 : 6653232 : if (i < n)
900 : 7294700 : temp_hard_regset |= reg_class_contents[cl];
901 : : }
902 : 19953150 : 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 : 19738600 : if (REGNO_REG_CLASS (i) == NO_REGS)
906 : 429100 : SET_HARD_REG_BIT (ignore_hard_regs, i);
907 : 214550 : temp_hard_regset &= ~ignore_hard_regs;
908 : 214550 : temp_hard_regset2 &= ~ignore_hard_regs;
909 : 429100 : ira_assert (hard_reg_set_subset_p (temp_hard_regset2, temp_hard_regset));
910 : : }
911 : : #endif
912 : 214550 : ira_pressure_classes_num = 0;
913 : 1074145 : for (i = 0; i < n; i++)
914 : : {
915 : 859595 : cl = (int) pressure_classes[i];
916 : 859595 : ira_reg_pressure_class_p[cl] = true;
917 : 859595 : ira_pressure_classes[ira_pressure_classes_num++] = (enum reg_class) cl;
918 : : }
919 : 214550 : setup_stack_reg_pressure_class ();
920 : 214550 : }
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 : 214550 : setup_uniform_class_p (void)
928 : : {
929 : 214550 : int i, cl, cl2, m;
930 : :
931 : 7509250 : for (cl = 0; cl < N_REG_CLASSES; cl++)
932 : : {
933 : 7294700 : ira_uniform_class_p[cl] = false;
934 : 7294700 : if (ira_class_hard_regs_num[cl] == 0)
935 : 641468 : 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 : 25950089 : for (i = 0; (cl2 = reg_class_subclasses[cl][i]) != LIM_REG_CLASSES; i++)
942 : : {
943 : 20583508 : if (ira_class_hard_regs_num[cl2] == 0)
944 : 376 : continue;
945 : 2439063020 : for (m = 0; m < NUM_MACHINE_MODES; m++)
946 : 2419766539 : if (contains_reg_of_mode[cl][m] && contains_reg_of_mode[cl2][m])
947 : : {
948 : 581318986 : ira_init_register_move_cost_if_necessary ((machine_mode) m);
949 : 581318986 : if (ira_register_move_cost[m][cl][cl]
950 : 581318986 : != ira_register_move_cost[m][cl2][cl2])
951 : : break;
952 : : }
953 : 20583132 : if (m < NUM_MACHINE_MODES)
954 : : break;
955 : : }
956 : 6653232 : if (cl2 == LIM_REG_CLASSES)
957 : 5366581 : ira_uniform_class_p[cl] = true;
958 : : }
959 : 214550 : }
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 : 214550 : setup_allocno_and_important_classes (void)
988 : : {
989 : 214550 : int i, j, n, cl;
990 : 214550 : bool set_p;
991 : 214550 : HARD_REG_SET temp_hard_regset2;
992 : 214550 : static enum reg_class classes[LIM_REG_CLASSES + 1];
993 : :
994 : 214550 : 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 : 7509250 : for (i = 0; i < LIM_REG_CLASSES; i++)
999 : : {
1000 : 7294700 : temp_hard_regset = reg_class_contents[i] & ~no_unit_alloc_regs;
1001 : 94893389 : for (j = 0; j < n; j++)
1002 : : {
1003 : 89317788 : cl = classes[j];
1004 : 89317788 : temp_hard_regset2 = reg_class_contents[cl] & ~no_unit_alloc_regs;
1005 : 178635576 : if (temp_hard_regset == temp_hard_regset2)
1006 : : break;
1007 : : }
1008 : 7294700 : if (j >= n || targetm.additional_allocno_class_p (i))
1009 : 5575601 : classes[n++] = (enum reg_class) i;
1010 : 1719099 : 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 : 5595 : classes[j] = (enum reg_class) i;
1016 : : }
1017 : 214550 : 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 : 214550 : ira_allocno_classes_num = 0;
1023 : 5790151 : for (i = 0; (cl = classes[i]) != LIM_REG_CLASSES; i++)
1024 : 5575601 : if (ira_class_hard_regs_num[cl] > 0)
1025 : 5361051 : ira_allocno_classes[ira_allocno_classes_num++] = (enum reg_class) cl;
1026 : 214550 : ira_important_classes_num = 0;
1027 : : /* Add non-allocno classes containing to non-empty set of
1028 : : allocatable hard regs. */
1029 : 7509250 : for (cl = 0; cl < N_REG_CLASSES; cl++)
1030 : 7294700 : if (ira_class_hard_regs_num[cl] > 0)
1031 : : {
1032 : 6653232 : temp_hard_regset = reg_class_contents[cl] & ~no_unit_alloc_regs;
1033 : 6653232 : set_p = false;
1034 : 103253255 : for (j = 0; j < ira_allocno_classes_num; j++)
1035 : : {
1036 : 101961074 : temp_hard_regset2 = (reg_class_contents[ira_allocno_classes[j]]
1037 : 101961074 : & ~no_unit_alloc_regs);
1038 : 101961074 : if ((enum reg_class) cl == ira_allocno_classes[j])
1039 : : break;
1040 : 96600023 : else if (hard_reg_set_subset_p (temp_hard_regset,
1041 : : temp_hard_regset2))
1042 : 6717033 : set_p = true;
1043 : : }
1044 : 6653232 : if (set_p && j >= ira_allocno_classes_num)
1045 : 1292181 : ira_important_classes[ira_important_classes_num++]
1046 : 1292181 : = (enum reg_class) cl;
1047 : : }
1048 : : /* Now add allocno classes to the important classes. */
1049 : 5575601 : for (j = 0; j < ira_allocno_classes_num; j++)
1050 : 5361051 : ira_important_classes[ira_important_classes_num++]
1051 : 5361051 : = ira_allocno_classes[j];
1052 : 7509250 : for (cl = 0; cl < N_REG_CLASSES; cl++)
1053 : : {
1054 : 7294700 : ira_reg_allocno_class_p[cl] = false;
1055 : 7294700 : ira_reg_pressure_class_p[cl] = false;
1056 : : }
1057 : 5575601 : for (j = 0; j < ira_allocno_classes_num; j++)
1058 : 5361051 : ira_reg_allocno_class_p[ira_allocno_classes[j]] = true;
1059 : 214550 : setup_pressure_classes ();
1060 : 214550 : setup_uniform_class_p ();
1061 : 214550 : }
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 : 429100 : setup_class_translate_array (enum reg_class *class_translate,
1080 : : int classes_num, enum reg_class *classes)
1081 : : {
1082 : 429100 : int cl, mode;
1083 : 429100 : enum reg_class aclass, best_class, *cl_ptr;
1084 : 429100 : int i, cost, min_cost, best_cost;
1085 : :
1086 : 15018500 : for (cl = 0; cl < N_REG_CLASSES; cl++)
1087 : 14589400 : class_translate[cl] = NO_REGS;
1088 : :
1089 : 6649746 : for (i = 0; i < classes_num; i++)
1090 : : {
1091 : 6220646 : aclass = classes[i];
1092 : 45675100 : for (cl_ptr = &alloc_reg_class_subclasses[aclass][0];
1093 : 45675100 : (cl = *cl_ptr) != LIM_REG_CLASSES;
1094 : : cl_ptr++)
1095 : 39454454 : if (class_translate[cl] == NO_REGS)
1096 : 6079054 : class_translate[cl] = aclass;
1097 : 6220646 : 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 : 15018500 : for (cl = 0; cl < N_REG_CLASSES; cl++)
1103 : : {
1104 : 14589400 : if (cl == NO_REGS || class_translate[cl] != NO_REGS)
1105 : 12661270 : continue;
1106 : : best_class = NO_REGS;
1107 : : best_cost = INT_MAX;
1108 : 18480176 : for (i = 0; i < classes_num; i++)
1109 : : {
1110 : 16552046 : aclass = classes[i];
1111 : 16552046 : temp_hard_regset = (reg_class_contents[aclass]
1112 : 16552046 : & reg_class_contents[cl]
1113 : 16552046 : & ~no_unit_alloc_regs);
1114 : 33104092 : if (! hard_reg_set_empty_p (temp_hard_regset))
1115 : : {
1116 : : min_cost = INT_MAX;
1117 : 349192375 : for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1118 : : {
1119 : 346398836 : cost = (ira_memory_move_cost[mode][aclass][0]
1120 : 346398836 : + ira_memory_move_cost[mode][aclass][1]);
1121 : 346398836 : if (min_cost > cost)
1122 : : min_cost = cost;
1123 : : }
1124 : 2793539 : if (best_class == NO_REGS || best_cost > min_cost)
1125 : : {
1126 : 16552046 : best_class = aclass;
1127 : 16552046 : best_cost = min_cost;
1128 : : }
1129 : : }
1130 : : }
1131 : 1928130 : class_translate[cl] = best_class;
1132 : : }
1133 : 429100 : }
1134 : :
1135 : : /* Set up array IRA_ALLOCNO_CLASS_TRANSLATE and
1136 : : IRA_PRESSURE_CLASS_TRANSLATE. */
1137 : : static void
1138 : 214550 : setup_class_translate (void)
1139 : : {
1140 : 214550 : setup_class_translate_array (ira_allocno_class_translate,
1141 : 214550 : ira_allocno_classes_num, ira_allocno_classes);
1142 : 214550 : setup_class_translate_array (ira_pressure_class_translate,
1143 : 214550 : ira_pressure_classes_num, ira_pressure_classes);
1144 : 214550 : }
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 : 178826928 : comp_reg_classes_func (const void *v1p, const void *v2p)
1153 : : {
1154 : 178826928 : enum reg_class cl1 = *(const enum reg_class *) v1p;
1155 : 178826928 : enum reg_class cl2 = *(const enum reg_class *) v2p;
1156 : 178826928 : enum reg_class tcl1, tcl2;
1157 : 178826928 : int diff;
1158 : :
1159 : 178826928 : tcl1 = ira_allocno_class_translate[cl1];
1160 : 178826928 : tcl2 = ira_allocno_class_translate[cl2];
1161 : 178826928 : if (tcl1 != NO_REGS && tcl2 != NO_REGS
1162 : 178826928 : && (diff = allocno_class_order[tcl1] - allocno_class_order[tcl2]) != 0)
1163 : : return diff;
1164 : 8111143 : 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 : 214550 : reorder_important_classes (void)
1180 : : {
1181 : 214550 : int i;
1182 : :
1183 : 7509250 : for (i = 0; i < N_REG_CLASSES; i++)
1184 : 7294700 : allocno_class_order[i] = -1;
1185 : 5575601 : for (i = 0; i < ira_allocno_classes_num; i++)
1186 : 5361051 : allocno_class_order[ira_allocno_classes[i]] = i;
1187 : 214550 : qsort (ira_important_classes, ira_important_classes_num,
1188 : : sizeof (enum reg_class), comp_reg_classes_func);
1189 : 7082332 : for (i = 0; i < ira_important_classes_num; i++)
1190 : 6653232 : ira_important_class_nums[ira_important_classes[i]] = i;
1191 : 214550 : }
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 : 214550 : setup_reg_class_relations (void)
1199 : : {
1200 : 214550 : int i, cl1, cl2, cl3;
1201 : 214550 : HARD_REG_SET intersection_set, union_set, temp_set2;
1202 : 214550 : bool important_class_p[N_REG_CLASSES];
1203 : :
1204 : 214550 : memset (important_class_p, 0, sizeof (important_class_p));
1205 : 6867782 : for (i = 0; i < ira_important_classes_num; i++)
1206 : 6653232 : important_class_p[ira_important_classes[i]] = true;
1207 : 7509250 : for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1208 : : {
1209 : 7294700 : ira_reg_class_super_classes[cl1][0] = LIM_REG_CLASSES;
1210 : 255314500 : for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1211 : : {
1212 : 248019800 : ira_reg_classes_intersect_p[cl1][cl2] = false;
1213 : 248019800 : ira_reg_class_intersect[cl1][cl2] = NO_REGS;
1214 : 248019800 : ira_reg_class_subset[cl1][cl2] = NO_REGS;
1215 : 248019800 : temp_hard_regset = reg_class_contents[cl1] & ~no_unit_alloc_regs;
1216 : 248019800 : temp_set2 = reg_class_contents[cl2] & ~no_unit_alloc_regs;
1217 : 248019800 : if (hard_reg_set_empty_p (temp_hard_regset)
1218 : 269829712 : && 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 : 757152 : for (i = 0;; i++)
1224 : : {
1225 : 2750800 : cl3 = reg_class_subclasses[cl1][i];
1226 : 2750800 : if (cl3 == LIM_REG_CLASSES)
1227 : : break;
1228 : 757152 : if (reg_class_subset_p (ira_reg_class_intersect[cl1][cl2],
1229 : : (enum reg_class) cl3))
1230 : 710102 : ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
1231 : : }
1232 : 1993648 : ira_reg_class_subunion[cl1][cl2] = reg_class_subunion[cl1][cl2];
1233 : 1993648 : ira_reg_class_superunion[cl1][cl2] = reg_class_superunion[cl1][cl2];
1234 : 1993648 : continue;
1235 : : }
1236 : : ira_reg_classes_intersect_p[cl1][cl2]
1237 : 246026152 : = hard_reg_set_intersect_p (temp_hard_regset, temp_set2);
1238 : 226209888 : if (important_class_p[cl1] && important_class_p[cl2]
1239 : 452419776 : && 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 : 58913907 : enum reg_class *p;
1245 : :
1246 : 58913907 : p = &ira_reg_class_super_classes[cl1][0];
1247 : 355587030 : while (*p != LIM_REG_CLASSES)
1248 : 296673123 : p++;
1249 : 58913907 : *p++ = (enum reg_class) cl2;
1250 : 58913907 : *p = LIM_REG_CLASSES;
1251 : : }
1252 : 246026152 : ira_reg_class_subunion[cl1][cl2] = NO_REGS;
1253 : 246026152 : ira_reg_class_superunion[cl1][cl2] = NO_REGS;
1254 : 246026152 : intersection_set = (reg_class_contents[cl1]
1255 : 246026152 : & reg_class_contents[cl2]
1256 : 246026152 : & ~no_unit_alloc_regs);
1257 : 246026152 : union_set = ((reg_class_contents[cl1] | reg_class_contents[cl2])
1258 : 246026152 : & ~no_unit_alloc_regs);
1259 : 8610915320 : for (cl3 = 0; cl3 < N_REG_CLASSES; cl3++)
1260 : : {
1261 : 8364889168 : temp_hard_regset = reg_class_contents[cl3] & ~no_unit_alloc_regs;
1262 : 16729778336 : if (hard_reg_set_empty_p (temp_hard_regset))
1263 : 734677560 : continue;
1264 : :
1265 : 7630211608 : 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 : 652260153 : if (important_class_p[cl3])
1271 : : {
1272 : 652260153 : temp_set2
1273 : 652260153 : = (reg_class_contents
1274 : 652260153 : [ira_reg_class_intersect[cl1][cl2]]);
1275 : 652260153 : temp_set2 &= ~no_unit_alloc_regs;
1276 : 652260153 : 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 : 758272211 : || (temp_hard_regset == temp_set2
1282 : 101797618 : && (cl3 == GENERAL_REGS
1283 : 101120617 : || ((ira_reg_class_intersect[cl1][cl2]
1284 : : != GENERAL_REGS)
1285 : 33838950 : && hard_reg_set_subset_p
1286 : 33838950 : (reg_class_contents[cl3],
1287 : : reg_class_contents
1288 : : [(int)
1289 : : ira_reg_class_intersect[cl1][cl2]])))))
1290 : 572887559 : ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
1291 : : }
1292 : 652260153 : temp_set2
1293 : 652260153 : = (reg_class_contents[ira_reg_class_subset[cl1][cl2]]
1294 : 652260153 : & ~no_unit_alloc_regs);
1295 : 652260153 : 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 : 758272211 : || (temp_hard_regset == temp_set2
1299 : 101797618 : && hard_reg_set_subset_p
1300 : 101797618 : (reg_class_contents[cl3],
1301 : : reg_class_contents
1302 : : [(int) ira_reg_class_subset[cl1][cl2]])))
1303 : 606053245 : ira_reg_class_subset[cl1][cl2] = (enum reg_class) cl3;
1304 : : }
1305 : 7630211608 : if (important_class_p[cl3]
1306 : 15260423216 : && 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 : 3397135515 : temp_set2
1312 : 3397135515 : = (reg_class_contents[ira_reg_class_subunion[cl1][cl2]]
1313 : 3397135515 : & ~no_unit_alloc_regs);
1314 : 3397135515 : if (ira_reg_class_subunion[cl1][cl2] == NO_REGS
1315 : 6548244878 : || (hard_reg_set_subset_p (temp_set2, temp_hard_regset)
1316 : :
1317 : 1049296835 : && (temp_set2 != temp_hard_regset
1318 : 437175137 : || 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 : 433611158 : || (ira_reg_class_subunion[cl1][cl2] != GENERAL_REGS
1323 : 28859698 : && hard_reg_set_subset_p
1324 : 28859698 : (reg_class_contents[cl3],
1325 : : reg_class_contents
1326 : : [(int) ira_reg_class_subunion[cl1][cl2]])))))
1327 : 884175967 : ira_reg_class_subunion[cl1][cl2] = (enum reg_class) cl3;
1328 : : }
1329 : 15260423216 : 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 : 1416305259 : temp_set2
1335 : 1416305259 : = (reg_class_contents[ira_reg_class_superunion[cl1][cl2]]
1336 : 1416305259 : & ~no_unit_alloc_regs);
1337 : 1416305259 : if (ira_reg_class_superunion[cl1][cl2] == NO_REGS
1338 : 2586584366 : || (hard_reg_set_subset_p (temp_hard_regset, temp_set2)
1339 : :
1340 : 201458498 : && (temp_set2 != temp_hard_regset
1341 : 200573000 : || 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 : 199024333 : || (ira_reg_class_superunion[cl1][cl2] != GENERAL_REGS
1346 : 19818375 : && hard_reg_set_subset_p
1347 : 19818375 : (reg_class_contents[cl3],
1348 : : reg_class_contents
1349 : : [(int) ira_reg_class_superunion[cl1][cl2]])))))
1350 : 262696269 : ira_reg_class_superunion[cl1][cl2] = (enum reg_class) cl3;
1351 : : }
1352 : : }
1353 : : }
1354 : : }
1355 : 214550 : }
1356 : :
1357 : : /* Output all uniform and important classes into file F. */
1358 : : static void
1359 : 0 : print_uniform_and_important_classes (FILE *f)
1360 : : {
1361 : 0 : int i, cl;
1362 : :
1363 : 0 : fprintf (f, "Uniform classes:\n");
1364 : 0 : for (cl = 0; cl < N_REG_CLASSES; cl++)
1365 : 0 : if (ira_uniform_class_p[cl])
1366 : 0 : fprintf (f, " %s", reg_class_names[cl]);
1367 : 0 : fprintf (f, "\nImportant classes:\n");
1368 : 0 : for (i = 0; i < ira_important_classes_num; i++)
1369 : 0 : fprintf (f, " %s", reg_class_names[ira_important_classes[i]]);
1370 : 0 : fprintf (f, "\n");
1371 : 0 : }
1372 : :
1373 : : /* Output all possible allocno or pressure classes and their
1374 : : translation map into file F. */
1375 : : static void
1376 : 0 : print_translated_classes (FILE *f, bool pressure_p)
1377 : : {
1378 : 0 : int classes_num = (pressure_p
1379 : 0 : ? ira_pressure_classes_num : ira_allocno_classes_num);
1380 : 0 : enum reg_class *classes = (pressure_p
1381 : 0 : ? ira_pressure_classes : ira_allocno_classes);
1382 : 0 : enum reg_class *class_translate = (pressure_p
1383 : : ? ira_pressure_class_translate
1384 : : : ira_allocno_class_translate);
1385 : 0 : int i;
1386 : :
1387 : 0 : fprintf (f, "%s classes:\n", pressure_p ? "Pressure" : "Allocno");
1388 : 0 : for (i = 0; i < classes_num; i++)
1389 : 0 : fprintf (f, " %s", reg_class_names[classes[i]]);
1390 : 0 : fprintf (f, "\nClass translation:\n");
1391 : 0 : for (i = 0; i < N_REG_CLASSES; i++)
1392 : 0 : fprintf (f, " %s -> %s\n", reg_class_names[i],
1393 : 0 : reg_class_names[class_translate[i]]);
1394 : 0 : }
1395 : :
1396 : : /* Output all possible allocno and translation classes and the
1397 : : translation maps into stderr. */
1398 : : void
1399 : 0 : ira_debug_allocno_classes (void)
1400 : : {
1401 : 0 : print_uniform_and_important_classes (stderr);
1402 : 0 : print_translated_classes (stderr, false);
1403 : 0 : print_translated_classes (stderr, true);
1404 : 0 : }
1405 : :
1406 : : /* Set up different arrays concerning class subsets, allocno and
1407 : : important classes. */
1408 : : static void
1409 : 214550 : find_reg_classes (void)
1410 : : {
1411 : 214550 : setup_allocno_and_important_classes ();
1412 : 214550 : setup_class_translate ();
1413 : 214550 : reorder_important_classes ();
1414 : 214550 : setup_reg_class_relations ();
1415 : 214550 : }
1416 : :
1417 : :
1418 : :
1419 : : /* Set up array ira_hard_regno_allocno_class. */
1420 : : static void
1421 : 214550 : setup_hard_regno_aclass (void)
1422 : : {
1423 : 214550 : int i;
1424 : :
1425 : 19953150 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1426 : : {
1427 : 39477200 : ira_hard_regno_allocno_class[i]
1428 : 29750840 : = (TEST_HARD_REG_BIT (no_unit_alloc_regs, i)
1429 : 19738600 : ? NO_REGS
1430 : 10012240 : : ira_allocno_class_translate[REGNO_REG_CLASS (i)]);
1431 : : }
1432 : 214550 : }
1433 : :
1434 : :
1435 : :
1436 : : /* Form IRA_REG_CLASS_MAX_NREGS and IRA_REG_CLASS_MIN_NREGS maps. */
1437 : : static void
1438 : 214550 : setup_reg_class_nregs (void)
1439 : : {
1440 : 214550 : int i, cl, cl2, m;
1441 : :
1442 : 26818750 : for (m = 0; m < MAX_MACHINE_MODE; m++)
1443 : : {
1444 : 931147000 : for (cl = 0; cl < N_REG_CLASSES; cl++)
1445 : 1809085600 : ira_reg_class_max_nregs[cl][m]
1446 : 1809085600 : = ira_reg_class_min_nregs[cl][m]
1447 : 904542800 : = targetm.class_max_nregs ((reg_class_t) cl, (machine_mode) m);
1448 : 931147000 : for (cl = 0; cl < N_REG_CLASSES; cl++)
1449 : 6480323700 : for (i = 0;
1450 : 7384866500 : (cl2 = alloc_reg_class_subclasses[cl][i]) != LIM_REG_CLASSES;
1451 : : i++)
1452 : 6480323700 : if (ira_reg_class_min_nregs[cl2][m]
1453 : 6480323700 : < ira_reg_class_min_nregs[cl][m])
1454 : 51987428 : ira_reg_class_min_nregs[cl][m] = ira_reg_class_min_nregs[cl2][m];
1455 : : }
1456 : 214550 : }
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 : 214550 : setup_prohibited_and_exclude_class_mode_regs (void)
1465 : : {
1466 : 214550 : int j, k, hard_regno, cl, last_hard_regno, count;
1467 : :
1468 : 7509250 : for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
1469 : : {
1470 : 7294700 : temp_hard_regset = reg_class_contents[cl] & ~no_unit_alloc_regs;
1471 : 911837500 : for (j = 0; j < NUM_MACHINE_MODES; j++)
1472 : : {
1473 : 904542800 : count = 0;
1474 : 904542800 : last_hard_regno = -1;
1475 : 3618171200 : CLEAR_HARD_REG_SET (ira_prohibited_class_mode_regs[cl][j]);
1476 : 904542800 : CLEAR_HARD_REG_SET (ira_exclude_class_mode_regs[cl][j]);
1477 : 10065589516 : for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
1478 : : {
1479 : 9161046716 : hard_regno = ira_class_hard_regs[cl][k];
1480 : 9161046716 : if (!targetm.hard_regno_mode_ok (hard_regno, (machine_mode) j))
1481 : 6906584161 : SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1482 : : hard_regno);
1483 : 2254462555 : else if (in_hard_reg_set_p (temp_hard_regset,
1484 : : (machine_mode) j, hard_regno))
1485 : : {
1486 : 2159035996 : last_hard_regno = hard_regno;
1487 : 2159035996 : count++;
1488 : : }
1489 : : else
1490 : : {
1491 : 95426559 : SET_HARD_REG_BIT (ira_exclude_class_mode_regs[cl][j], hard_regno);
1492 : : }
1493 : : }
1494 : 951461181 : ira_class_singleton[cl][j] = (count == 1 ? last_hard_regno : -1);
1495 : : }
1496 : : }
1497 : 214550 : }
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 : 214550 : clarify_prohibited_class_mode_regs (void)
1504 : : {
1505 : 214550 : int j, k, hard_regno, cl, pclass, nregs;
1506 : :
1507 : 7509250 : for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
1508 : 911837500 : for (j = 0; j < NUM_MACHINE_MODES; j++)
1509 : : {
1510 : 904542800 : CLEAR_HARD_REG_SET (ira_useful_class_mode_regs[cl][j]);
1511 : 10065589516 : for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
1512 : : {
1513 : 9161046716 : hard_regno = ira_class_hard_regs[cl][k];
1514 : 9161046716 : if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j], hard_regno))
1515 : 6906584161 : continue;
1516 : 2254462555 : nregs = hard_regno_nregs (hard_regno, (machine_mode) j);
1517 : 2254462555 : if (hard_regno + nregs > FIRST_PSEUDO_REGISTER)
1518 : : {
1519 : 8445 : SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1520 : : hard_regno);
1521 : 8445 : continue;
1522 : : }
1523 : 2254454110 : pclass = ira_pressure_class_translate[REGNO_REG_CLASS (hard_regno)];
1524 : 4953490125 : for (nregs-- ;nregs >= 0; nregs--)
1525 : 2749173538 : if (((enum reg_class) pclass
1526 : 2749173538 : != ira_pressure_class_translate[REGNO_REG_CLASS
1527 : 2749173538 : (hard_regno + nregs)]))
1528 : : {
1529 : 50137523 : SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1530 : : hard_regno);
1531 : 50137523 : break;
1532 : : }
1533 : 2254454110 : if (!TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1534 : : hard_regno))
1535 : 2204316587 : add_to_hard_reg_set (&ira_useful_class_mode_regs[cl][j],
1536 : : (machine_mode) j, hard_regno);
1537 : : }
1538 : : }
1539 : 214550 : }
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 : 10002748 : ira_init_register_move_cost (machine_mode mode)
1545 : : {
1546 : 10002748 : static unsigned short last_move_cost[N_REG_CLASSES][N_REG_CLASSES];
1547 : 10002748 : bool all_match = true;
1548 : 10002748 : unsigned int i, cl1, cl2;
1549 : 10002748 : HARD_REG_SET ok_regs;
1550 : :
1551 : 10002748 : 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 : 930255564 : CLEAR_HARD_REG_SET (ok_regs);
1555 : 930255564 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1556 : 920252816 : if (targetm.hard_regno_mode_ok (i, mode))
1557 : 418476859 : 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 : 350096180 : for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1564 : 11903270120 : for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1565 : : {
1566 : 11563176688 : int cost;
1567 : 11563176688 : if (!hard_reg_set_intersect_p (ok_regs, reg_class_contents[cl1])
1568 : 19118025614 : || !hard_reg_set_intersect_p (ok_regs, reg_class_contents[cl2]))
1569 : : {
1570 : 6037397085 : if ((ira_reg_class_max_nregs[cl1][mode]
1571 : 6037397085 : > ira_class_hard_regs_num[cl1])
1572 : 4283518377 : || (ira_reg_class_max_nregs[cl2][mode]
1573 : 4283518377 : > ira_class_hard_regs_num[cl2]))
1574 : : cost = 65535;
1575 : : else
1576 : 2946243185 : cost = (ira_memory_move_cost[mode][cl1][0]
1577 : 2946243185 : + ira_memory_move_cost[mode][cl2][1]) * 2;
1578 : : }
1579 : : else
1580 : : {
1581 : 5525779603 : cost = register_move_cost (mode, (enum reg_class) cl1,
1582 : : (enum reg_class) cl2);
1583 : 5525779603 : ira_assert (cost < 65535);
1584 : : }
1585 : 11563176688 : all_match &= (last_move_cost[cl1][cl2] == cost);
1586 : 11563176688 : last_move_cost[cl1][cl2] = cost;
1587 : : }
1588 : 10002748 : if (all_match && last_mode_for_init_move_cost != -1)
1589 : : {
1590 : 4151417 : ira_register_move_cost[mode]
1591 : 4151417 : = ira_register_move_cost[last_mode_for_init_move_cost];
1592 : 4151417 : ira_may_move_in_cost[mode]
1593 : 4151417 : = ira_may_move_in_cost[last_mode_for_init_move_cost];
1594 : 4151417 : ira_may_move_out_cost[mode]
1595 : 4151417 : = ira_may_move_out_cost[last_mode_for_init_move_cost];
1596 : 4151417 : return;
1597 : : }
1598 : 5851331 : last_mode_for_init_move_cost = mode;
1599 : 5851331 : ira_register_move_cost[mode] = XNEWVEC (move_table, N_REG_CLASSES);
1600 : 5851331 : ira_may_move_in_cost[mode] = XNEWVEC (move_table, N_REG_CLASSES);
1601 : 5851331 : ira_may_move_out_cost[mode] = XNEWVEC (move_table, N_REG_CLASSES);
1602 : 204796585 : for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1603 : 6963083890 : for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1604 : : {
1605 : 6764138636 : int cost;
1606 : 6764138636 : enum reg_class *p1, *p2;
1607 : :
1608 : 6764138636 : if (last_move_cost[cl1][cl2] == 65535)
1609 : : {
1610 : 1689647408 : ira_register_move_cost[mode][cl1][cl2] = 65535;
1611 : 1689647408 : ira_may_move_in_cost[mode][cl1][cl2] = 65535;
1612 : 1689647408 : ira_may_move_out_cost[mode][cl1][cl2] = 65535;
1613 : : }
1614 : : else
1615 : : {
1616 : 5074491228 : cost = last_move_cost[cl1][cl2];
1617 : :
1618 : 42523226176 : for (p2 = ®_class_subclasses[cl2][0];
1619 : 42523226176 : *p2 != LIM_REG_CLASSES; p2++)
1620 : 37448734948 : if (ira_class_hard_regs_num[*p2] > 0
1621 : 36756861221 : && (ira_reg_class_max_nregs[*p2][mode]
1622 : : <= ira_class_hard_regs_num[*p2]))
1623 : 30147064559 : cost = MAX (cost, ira_register_move_cost[mode][cl1][*p2]);
1624 : :
1625 : 42523226176 : for (p1 = ®_class_subclasses[cl1][0];
1626 : 42523226176 : *p1 != LIM_REG_CLASSES; p1++)
1627 : 37448734948 : if (ira_class_hard_regs_num[*p1] > 0
1628 : 36756861221 : && (ira_reg_class_max_nregs[*p1][mode]
1629 : : <= ira_class_hard_regs_num[*p1]))
1630 : 30147064559 : cost = MAX (cost, ira_register_move_cost[mode][*p1][cl2]);
1631 : :
1632 : 5074491228 : ira_assert (cost <= 65535);
1633 : 5074491228 : ira_register_move_cost[mode][cl1][cl2] = cost;
1634 : :
1635 : 5074491228 : if (ira_class_subset_p[cl1][cl2])
1636 : 1532323203 : ira_may_move_in_cost[mode][cl1][cl2] = 0;
1637 : : else
1638 : 3542168025 : ira_may_move_in_cost[mode][cl1][cl2] = cost;
1639 : :
1640 : 5074491228 : if (ira_class_subset_p[cl2][cl1])
1641 : 1532323203 : ira_may_move_out_cost[mode][cl1][cl2] = 0;
1642 : : else
1643 : 3542168025 : 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 : 209255 : ira_init_once (void)
1655 : : {
1656 : 209255 : ira_init_costs_once ();
1657 : 209255 : lra_init_once ();
1658 : :
1659 : 209255 : ira_use_lra_p = targetm.lra_p ();
1660 : 209255 : }
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 : 545258 : target_ira_int::free_register_move_costs (void)
1666 : : {
1667 : 545258 : int mode, i;
1668 : :
1669 : : /* Reset move_cost and friends, making sure we only free shared
1670 : : table entries once. */
1671 : 68157250 : for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1672 : 67611992 : if (x_ira_register_move_cost[mode])
1673 : : {
1674 : 600489151 : for (i = 0;
1675 : 609716333 : i < mode && (x_ira_register_move_cost[i]
1676 : : != x_ira_register_move_cost[mode]);
1677 : : i++)
1678 : : ;
1679 : 9227182 : if (i == mode)
1680 : : {
1681 : 5400561 : free (x_ira_register_move_cost[mode]);
1682 : 5400561 : free (x_ira_may_move_in_cost[mode]);
1683 : 5400561 : free (x_ira_may_move_out_cost[mode]);
1684 : : }
1685 : : }
1686 : 545258 : memset (x_ira_register_move_cost, 0, sizeof x_ira_register_move_cost);
1687 : 545258 : memset (x_ira_may_move_in_cost, 0, sizeof x_ira_may_move_in_cost);
1688 : 545258 : memset (x_ira_may_move_out_cost, 0, sizeof x_ira_may_move_out_cost);
1689 : 545258 : last_mode_for_init_move_cost = -1;
1690 : 545258 : }
1691 : :
1692 : 330708 : target_ira_int::~target_ira_int ()
1693 : : {
1694 : 330708 : free_ira_costs ();
1695 : 330708 : free_register_move_costs ();
1696 : 330708 : }
1697 : :
1698 : : /* This is called every time when register related information is
1699 : : changed. */
1700 : : void
1701 : 214550 : ira_init (void)
1702 : : {
1703 : 214550 : this_target_ira_int->free_register_move_costs ();
1704 : 214550 : setup_reg_mode_hard_regset ();
1705 : 214550 : setup_alloc_regs (flag_omit_frame_pointer != 0);
1706 : 214550 : setup_class_subset_and_memory_move_costs ();
1707 : 214550 : setup_reg_class_nregs ();
1708 : 214550 : setup_prohibited_and_exclude_class_mode_regs ();
1709 : 214550 : find_reg_classes ();
1710 : 214550 : clarify_prohibited_class_mode_regs ();
1711 : 214550 : setup_hard_regno_aclass ();
1712 : 214550 : ira_init_costs ();
1713 : 214550 : }
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 : 1481332 : setup_prohibited_mode_move_regs (void)
1722 : : {
1723 : 1481332 : int i, j;
1724 : 1481332 : rtx test_reg1, test_reg2, move_pat;
1725 : 1481332 : rtx_insn *move_insn;
1726 : :
1727 : 1481332 : if (ira_prohibited_mode_move_regs_initialized_p)
1728 : : return;
1729 : 212226 : ira_prohibited_mode_move_regs_initialized_p = true;
1730 : 212226 : test_reg1 = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
1731 : 212226 : test_reg2 = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 2);
1732 : 212226 : move_pat = gen_rtx_SET (test_reg1, test_reg2);
1733 : 212226 : move_insn = gen_rtx_INSN (VOIDmode, 0, 0, 0, move_pat, 0, -1, 0);
1734 : 26740476 : for (i = 0; i < NUM_MACHINE_MODES; i++)
1735 : : {
1736 : 26316024 : SET_HARD_REG_SET (ira_prohibited_mode_move_regs[i]);
1737 : 2447390232 : for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1738 : : {
1739 : 2421074208 : if (!targetm.hard_regno_mode_ok (j, (machine_mode) i))
1740 : 2002792127 : continue;
1741 : 418282081 : set_mode_and_regno (test_reg1, (machine_mode) i, j);
1742 : 418282081 : set_mode_and_regno (test_reg2, (machine_mode) i, j);
1743 : 418282081 : INSN_CODE (move_insn) = -1;
1744 : 418282081 : recog_memoized (move_insn);
1745 : 418282081 : if (INSN_CODE (move_insn) < 0)
1746 : 197991617 : continue;
1747 : 220290464 : 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 : 220290464 : if (! constrain_operands (1, get_enabled_alternatives (move_insn)))
1751 : 1303112 : continue;
1752 : 218987352 : 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 : 104164923 : ira_setup_alts (rtx_insn *insn)
1767 : : {
1768 : 104164923 : int nop, nalt;
1769 : 104164923 : bool curr_swapped;
1770 : 104164923 : const char *p;
1771 : 104164923 : int commutative = -1;
1772 : :
1773 : 104164923 : extract_insn (insn);
1774 : 104164923 : preprocess_constraints (insn);
1775 : 104164923 : alternative_mask preferred = get_preferred_alternatives (insn);
1776 : 104164923 : alternative_mask alts = 0;
1777 : 104164923 : 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 : 104164923 : ira_assert (recog_data.n_alternatives
1782 : : <= (int) MAX (sizeof (HARD_REG_ELT_TYPE) * CHAR_BIT,
1783 : : FIRST_PSEUDO_REGISTER));
1784 : 305542136 : for (nop = 0; nop < recog_data.n_operands; nop++)
1785 : 213568640 : if (recog_data.constraints[nop][0] == '%')
1786 : : {
1787 : : commutative = nop;
1788 : : break;
1789 : : }
1790 : 104164923 : for (curr_swapped = false;; curr_swapped = true)
1791 : : {
1792 : 1377119645 : for (nalt = 0; nalt < recog_data.n_alternatives; nalt++)
1793 : : {
1794 : 1260763295 : if (!TEST_BIT (preferred, nalt) || TEST_BIT (exact_alts, nalt))
1795 : 420211382 : continue;
1796 : :
1797 : 840551913 : const operand_alternative *op_alt
1798 : 840551913 : = &recog_op_alt[nalt * recog_data.n_operands];
1799 : 840551913 : int this_reject = 0;
1800 : 2399768063 : for (nop = 0; nop < recog_data.n_operands; nop++)
1801 : : {
1802 : 1751470263 : int c, len;
1803 : :
1804 : 1751470263 : this_reject += op_alt[nop].reject;
1805 : :
1806 : 1751470263 : rtx op = recog_data.operand[nop];
1807 : 1751470263 : p = op_alt[nop].constraint;
1808 : 1751470263 : if (*p == 0 || *p == ',')
1809 : 24275760 : continue;
1810 : :
1811 : : bool win_p = false;
1812 : 3503769035 : do
1813 : 3503769035 : switch (c = *p, len = CONSTRAINT_LEN (c, p), c)
1814 : : {
1815 : : case '#':
1816 : : case ',':
1817 : : c = '\0';
1818 : : /* FALLTHRU */
1819 : 735214834 : case '\0':
1820 : 735214834 : len = 0;
1821 : 735214834 : break;
1822 : :
1823 : : case '%':
1824 : : /* The commutative modifier is handled above. */
1825 : : break;
1826 : :
1827 : 74490529 : case '0': case '1': case '2': case '3': case '4':
1828 : 74490529 : case '5': case '6': case '7': case '8': case '9':
1829 : 74490529 : {
1830 : 74490529 : char *end;
1831 : 74490529 : unsigned long dup = strtoul (p, &end, 10);
1832 : 74490529 : rtx other = recog_data.operand[dup];
1833 : 74490529 : len = end - p;
1834 : 1798185 : if (MEM_P (other)
1835 : 74490529 : ? rtx_equal_p (other, op)
1836 : 72692344 : : REG_P (op) || SUBREG_P (op))
1837 : 51713111 : goto op_success;
1838 : 22777418 : win_p = true;
1839 : : }
1840 : 22777418 : break;
1841 : :
1842 : 10735047 : case 'g':
1843 : 10735047 : goto op_success;
1844 : 2664116815 : break;
1845 : :
1846 : 2664116815 : default:
1847 : 2664116815 : {
1848 : 2664116815 : enum constraint_num cn = lookup_constraint (p);
1849 : 2664116815 : rtx mem = NULL;
1850 : 2664116815 : switch (get_constraint_type (cn))
1851 : : {
1852 : 2172664210 : case CT_REGISTER:
1853 : 3421323165 : if (reg_class_for_constraint (cn) != NO_REGS)
1854 : : {
1855 : 1201110437 : if (REG_P (op) || SUBREG_P (op))
1856 : 786090483 : goto op_success;
1857 : : win_p = true;
1858 : : }
1859 : : break;
1860 : :
1861 : 4078232 : case CT_CONST_INT:
1862 : 4078232 : if (CONST_INT_P (op)
1863 : 6716772 : && (insn_const_int_ok_for_constraint
1864 : 2638540 : (INTVAL (op), cn)))
1865 : 1865684 : goto op_success;
1866 : : break;
1867 : :
1868 : 825854 : case CT_ADDRESS:
1869 : 825854 : goto op_success;
1870 : :
1871 : 164647394 : case CT_MEMORY:
1872 : 164647394 : case CT_RELAXED_MEMORY:
1873 : 164647394 : mem = op;
1874 : : /* Fall through. */
1875 : 164647394 : case CT_SPECIAL_MEMORY:
1876 : 164647394 : if (!mem)
1877 : 67533042 : mem = extract_mem_from_operand (op);
1878 : 232180436 : if (MEM_P (mem))
1879 : 69915834 : goto op_success;
1880 : : win_p = true;
1881 : : break;
1882 : :
1883 : 254368083 : case CT_FIXED_FORM:
1884 : 254368083 : if (constraint_satisfied_p (op, cn))
1885 : 70833656 : goto op_success;
1886 : : break;
1887 : : }
1888 : : break;
1889 : : }
1890 : : }
1891 : 2511789366 : while (p += len, c);
1892 : 735214834 : 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 : 542960721 : this_reject += 6;
1898 : 1559216150 : op_success:
1899 : 1559216150 : ;
1900 : : }
1901 : :
1902 : 840551913 : if (nop >= recog_data.n_operands)
1903 : : {
1904 : 648297800 : alts |= ALTERNATIVE_BIT (nalt);
1905 : 648297800 : if (this_reject == 0)
1906 : 137049722 : exact_alts |= ALTERNATIVE_BIT (nalt);
1907 : : }
1908 : : }
1909 : 116356350 : if (commutative < 0)
1910 : : break;
1911 : : /* Swap forth and back to avoid changing recog_data. */
1912 : 24382854 : std::swap (recog_data.operand[commutative],
1913 : 24382854 : recog_data.operand[commutative + 1]);
1914 : 24382854 : if (curr_swapped)
1915 : : break;
1916 : : }
1917 : 104164923 : 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 : 20619056 : ira_get_dup_out_num (int op_num, alternative_mask alts,
1941 : : bool &single_input_op_has_cstr_p)
1942 : : {
1943 : 20619056 : int curr_alt, c, original;
1944 : 20619056 : bool ignore_p, use_commut_op_p;
1945 : 20619056 : const char *str;
1946 : :
1947 : 20619056 : if (op_num < 0 || recog_data.n_alternatives == 0)
1948 : : return -1;
1949 : : /* We should find duplications only for input operands. */
1950 : 20619056 : if (recog_data.operand_type[op_num] != OP_IN)
1951 : : return -1;
1952 : 14867657 : str = recog_data.constraints[op_num];
1953 : 14867657 : use_commut_op_p = false;
1954 : 14867657 : single_input_op_has_cstr_p = true;
1955 : :
1956 : 14867657 : rtx op = recog_data.operand[op_num];
1957 : 14867657 : int op_regno = reg_or_subregno (op);
1958 : 14867657 : enum reg_class op_pref_cl = reg_preferred_class (op_regno);
1959 : 14867657 : machine_mode op_mode = GET_MODE (op);
1960 : :
1961 : 14867657 : 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 : 14867657 : bool respect_dup_despite_reg_cstr
1969 : 14867657 : = param_ira_consider_dup_in_all_alts
1970 : 471 : && op_pref_cl != NO_REGS
1971 : 14868124 : && 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 : 17311474 : alternative_mask pref_cl_alts;
1985 : :
1986 : 17311474 : for (;;)
1987 : : {
1988 : 17311474 : pref_cl_alts = 0;
1989 : :
1990 : 17311474 : for (curr_alt = 0, ignore_p = !TEST_BIT (alts, curr_alt),
1991 : 17311474 : original = -1;;)
1992 : : {
1993 : 101764921 : c = *str;
1994 : 101764921 : if (c == '\0')
1995 : : break;
1996 : 96853374 : if (c == '#')
1997 : : ignore_p = true;
1998 : 96853374 : else if (c == ',')
1999 : : {
2000 : 31515823 : curr_alt++;
2001 : 31515823 : ignore_p = !TEST_BIT (alts, curr_alt);
2002 : : }
2003 : 65337551 : else if (! ignore_p)
2004 : 20750363 : switch (c)
2005 : : {
2006 : 927 : case 'g':
2007 : 927 : goto fail;
2008 : 15618842 : default:
2009 : 15618842 : {
2010 : 15618842 : enum constraint_num cn = lookup_constraint (str);
2011 : 15618842 : enum reg_class cl = reg_class_for_constraint (cn);
2012 : 12671353 : if (cl != NO_REGS && !targetm.class_likely_spilled_p (cl))
2013 : : {
2014 : 12397531 : 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 : 654 : if (cl != op_pref_cl
2020 : 102 : && (ira_reg_class_intersect[cl][op_pref_cl]
2021 : : != NO_REGS)
2022 : 90 : && (ira_may_move_in_cost[op_mode][op_pref_cl][cl]
2023 : : == 0))
2024 : 74 : goto fail;
2025 : 580 : else if (cl == op_pref_cl)
2026 : 552 : pref_cl_alts |= ALTERNATIVE_BIT (curr_alt);
2027 : : }
2028 : : else
2029 : 12396877 : goto fail;
2030 : : }
2031 : 3221891 : if (constraint_satisfied_p (op, cn))
2032 : 2049 : goto fail;
2033 : : break;
2034 : : }
2035 : :
2036 : 5130594 : case '0': case '1': case '2': case '3': case '4':
2037 : 5130594 : case '5': case '6': case '7': case '8': case '9':
2038 : 5130594 : {
2039 : 5130594 : char *end;
2040 : 5130594 : int n = (int) strtoul (str, &end, 10);
2041 : 5130594 : str = end;
2042 : 5130594 : if (original != -1 && original != n)
2043 : 0 : goto fail;
2044 : 5130594 : gcc_assert (n < recog_data.n_operands);
2045 : 5130594 : if (respect_dup_despite_reg_cstr)
2046 : : {
2047 : 218 : const operand_alternative *op_alt
2048 : 218 : = &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 : 218 : if (op_alt[n].cl == op_pref_cl)
2055 : 5130536 : original = n;
2056 : : }
2057 : : else
2058 : : original = n;
2059 : 5130594 : continue;
2060 : 5130594 : }
2061 : : }
2062 : 79322853 : str += CONSTRAINT_LEN (c, str);
2063 : : }
2064 : 4911547 : if (original == -1)
2065 : 1841431 : goto fail;
2066 : 3070116 : if (recog_data.operand_type[original] == OP_OUT)
2067 : : {
2068 : 3069740 : 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 : 357 : for (nalt = 0; nalt < recog_data.n_alternatives; nalt++)
2076 : : {
2077 : 333 : if (!TEST_BIT (pref_cl_alts, nalt))
2078 : 236 : continue;
2079 : 97 : const operand_alternative *op_alt
2080 : 97 : = &recog_op_alt[nalt * recog_data.n_operands];
2081 : 97 : bool dup_in_other = false;
2082 : 358 : for (nop = 0; nop < recog_data.n_operands; nop++)
2083 : : {
2084 : 304 : if (recog_data.operand_type[nop] != OP_IN)
2085 : 97 : continue;
2086 : 207 : if (nop == op_num)
2087 : 86 : continue;
2088 : 121 : if (op_alt[nop].matches == original)
2089 : : {
2090 : : dup_in_other = true;
2091 : : break;
2092 : : }
2093 : : }
2094 : 97 : if (!dup_in_other)
2095 : : return -1;
2096 : : }
2097 : 24 : single_input_op_has_cstr_p = false;
2098 : 24 : return original;
2099 : : }
2100 : 376 : fail:
2101 : 14241734 : if (use_commut_op_p)
2102 : : break;
2103 : 12598172 : use_commut_op_p = true;
2104 : 12598172 : if (recog_data.constraints[op_num][0] == '%')
2105 : 1067235 : str = recog_data.constraints[op_num + 1];
2106 : 11530937 : 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 : : /* Return true if a replacement of SRC by DEST does not lead to unsatisfiable
2117 : : asm. A replacement is valid if SRC or DEST are not constrained in asm
2118 : : inputs of a single asm statement. See match_asm_constraints_2() for more
2119 : : details. TODO: As in match_asm_constraints_2() consider alternatives more
2120 : : precisely. */
2121 : :
2122 : : static bool
2123 : 7799 : valid_replacement_for_asm_input_p_1 (const_rtx asmops, const_rtx src, const_rtx dest)
2124 : : {
2125 : 7799 : int ninputs = ASM_OPERANDS_INPUT_LENGTH (asmops);
2126 : 7799 : rtvec inputs = ASM_OPERANDS_INPUT_VEC (asmops);
2127 : 39869 : for (int i = 0; i < ninputs; ++i)
2128 : : {
2129 : 32070 : rtx input_src = RTVEC_ELT (inputs, i);
2130 : 32070 : const char *constraint_src
2131 : 32070 : = ASM_OPERANDS_INPUT_CONSTRAINT (asmops, i);
2132 : 32070 : if (rtx_equal_p (input_src, src)
2133 : 32070 : && strchr (constraint_src, '{') != nullptr)
2134 : 0 : for (int j = 0; j < ninputs; ++j)
2135 : : {
2136 : 0 : rtx input_dest = RTVEC_ELT (inputs, j);
2137 : 0 : const char *constraint_dest
2138 : 0 : = ASM_OPERANDS_INPUT_CONSTRAINT (asmops, j);
2139 : 0 : if (rtx_equal_p (input_dest, dest)
2140 : 0 : && strchr (constraint_dest, '{') != nullptr)
2141 : : return false;
2142 : : }
2143 : : }
2144 : : return true;
2145 : : }
2146 : :
2147 : : /* Return true if a replacement of SRC by DEST does not lead to unsatisfiable
2148 : : asm. A replacement is valid if SRC or DEST are not constrained in asm
2149 : : inputs of a single asm statement. The final check is done in function
2150 : : valid_replacement_for_asm_input_p_1. */
2151 : :
2152 : : static bool
2153 : 515624 : valid_replacement_for_asm_input_p (const_rtx src, const_rtx dest)
2154 : : {
2155 : : /* Bail out early if there is no asm statement. */
2156 : 515624 : if (!crtl->has_asm_statement)
2157 : : return true;
2158 : 24299 : for (df_ref use = DF_REG_USE_CHAIN (REGNO (src));
2159 : 772832 : use;
2160 : 748533 : use = DF_REF_NEXT_REG (use))
2161 : : {
2162 : 748533 : struct df_insn_info *use_info = DF_REF_INSN_INFO (use);
2163 : : /* Only check real uses, not artificial ones. */
2164 : 748533 : if (use_info)
2165 : : {
2166 : 748533 : rtx_insn *insn = DF_REF_INSN (use);
2167 : 748533 : rtx pat = PATTERN (insn);
2168 : 748533 : if (asm_noperands (pat) <= 0)
2169 : 745181 : continue;
2170 : 3352 : if (GET_CODE (pat) == SET)
2171 : : {
2172 : 0 : if (!valid_replacement_for_asm_input_p_1 (SET_SRC (pat), src, dest))
2173 : : return false;
2174 : : }
2175 : 3352 : else if (GET_CODE (pat) == PARALLEL)
2176 : 14551 : for (int i = 0, len = XVECLEN (pat, 0); i < len; ++i)
2177 : : {
2178 : 11199 : rtx asmops = XVECEXP (pat, 0, i);
2179 : 11199 : if (GET_CODE (asmops) == SET)
2180 : 7765 : asmops = SET_SRC (asmops);
2181 : 11199 : if (GET_CODE (asmops) == ASM_OPERANDS
2182 : 11199 : && !valid_replacement_for_asm_input_p_1 (asmops, src, dest))
2183 : : return false;
2184 : : }
2185 : 0 : else if (GET_CODE (pat) == ASM_OPERANDS)
2186 : : {
2187 : 0 : if (!valid_replacement_for_asm_input_p_1 (pat, src, dest))
2188 : : return false;
2189 : : }
2190 : : else
2191 : 0 : gcc_unreachable ();
2192 : : }
2193 : : }
2194 : : return true;
2195 : : }
2196 : :
2197 : : /* Search forward to see if the source register of a copy insn dies
2198 : : before either it or the destination register is modified, but don't
2199 : : scan past the end of the basic block. If so, we can replace the
2200 : : source with the destination and let the source die in the copy
2201 : : insn.
2202 : :
2203 : : This will reduce the number of registers live in that range and may
2204 : : enable the destination and the source coalescing, thus often saving
2205 : : one register in addition to a register-register copy. */
2206 : :
2207 : : static void
2208 : 1481332 : decrease_live_ranges_number (void)
2209 : : {
2210 : 1481332 : basic_block bb;
2211 : 1481332 : rtx_insn *insn;
2212 : 1481332 : rtx set, src, dest, dest_death, note;
2213 : 1481332 : rtx_insn *p, *q;
2214 : 1481332 : int sregno, dregno;
2215 : :
2216 : 1481332 : if (! flag_expensive_optimizations)
2217 : : return;
2218 : :
2219 : 966056 : if (ira_dump_file)
2220 : 32 : fprintf (ira_dump_file, "Starting decreasing number of live ranges...\n");
2221 : :
2222 : 11548398 : FOR_EACH_BB_FN (bb, cfun)
2223 : 136127568 : FOR_BB_INSNS (bb, insn)
2224 : : {
2225 : 125545226 : set = single_set (insn);
2226 : 125545226 : if (! set)
2227 : 73342313 : continue;
2228 : 52202913 : src = SET_SRC (set);
2229 : 52202913 : dest = SET_DEST (set);
2230 : 14312267 : if (! REG_P (src) || ! REG_P (dest)
2231 : 61292636 : || find_reg_note (insn, REG_DEAD, src))
2232 : 49319546 : continue;
2233 : 2883367 : sregno = REGNO (src);
2234 : 2883367 : dregno = REGNO (dest);
2235 : :
2236 : : /* We don't want to mess with hard regs if register classes
2237 : : are small. */
2238 : 5251110 : if (sregno == dregno
2239 : 2883333 : || (targetm.small_register_classes_for_mode_p (GET_MODE (src))
2240 : 2883333 : && (sregno < FIRST_PSEUDO_REGISTER
2241 : 2883333 : || dregno < FIRST_PSEUDO_REGISTER))
2242 : : /* We don't see all updates to SP if they are in an
2243 : : auto-inc memory reference, so we must disallow this
2244 : : optimization on them. */
2245 : 515624 : || sregno == STACK_POINTER_REGNUM
2246 : 515624 : || dregno == STACK_POINTER_REGNUM
2247 : 3398991 : || !valid_replacement_for_asm_input_p (src, dest))
2248 : 2367743 : continue;
2249 : :
2250 : 515624 : dest_death = NULL_RTX;
2251 : :
2252 : 5808876 : for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
2253 : : {
2254 : 5804691 : if (! INSN_P (p))
2255 : 1073767 : continue;
2256 : 4730924 : if (BLOCK_FOR_INSN (p) != bb)
2257 : : break;
2258 : :
2259 : 8528091 : if (reg_set_p (src, p) || reg_set_p (dest, p)
2260 : : /* If SRC is an asm-declared register, it must not be
2261 : : replaced in any asm. Unfortunately, the REG_EXPR
2262 : : tree for the asm variable may be absent in the SRC
2263 : : rtx, so we can't check the actual register
2264 : : declaration easily (the asm operand will have it,
2265 : : though). To avoid complicating the test for a rare
2266 : : case, we just don't perform register replacement
2267 : : for a hard reg mentioned in an asm. */
2268 : 4225846 : || (sregno < FIRST_PSEUDO_REGISTER
2269 : 0 : && asm_noperands (PATTERN (p)) >= 0
2270 : 0 : && reg_overlap_mentioned_p (src, PATTERN (p)))
2271 : : /* Don't change hard registers used by a call. */
2272 : 4225846 : || (CALL_P (p) && sregno < FIRST_PSEUDO_REGISTER
2273 : 0 : && find_reg_fusage (p, USE, src))
2274 : : /* Don't change a USE of a register. */
2275 : 8502778 : || (GET_CODE (PATTERN (p)) == USE
2276 : 906 : && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
2277 : : break;
2278 : :
2279 : : /* See if all of SRC dies in P. This test is slightly
2280 : : more conservative than it needs to be. */
2281 : 4225846 : if ((note = find_regno_note (p, REG_DEAD, sregno))
2282 : 4225846 : && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
2283 : : {
2284 : 6361 : int failed = 0;
2285 : :
2286 : : /* We can do the optimization. Scan forward from INSN
2287 : : again, replacing regs as we go. Set FAILED if a
2288 : : replacement can't be done. In that case, we can't
2289 : : move the death note for SRC. This should be
2290 : : rare. */
2291 : :
2292 : : /* Set to stop at next insn. */
2293 : 6361 : for (q = next_real_insn (insn);
2294 : 32584 : q != next_real_insn (p);
2295 : 26223 : q = next_real_insn (q))
2296 : : {
2297 : 26223 : if (reg_overlap_mentioned_p (src, PATTERN (q)))
2298 : : {
2299 : : /* If SRC is a hard register, we might miss
2300 : : some overlapping registers with
2301 : : validate_replace_rtx, so we would have to
2302 : : undo it. We can't if DEST is present in
2303 : : the insn, so fail in that combination of
2304 : : cases. */
2305 : 7498 : if (sregno < FIRST_PSEUDO_REGISTER
2306 : 7498 : && reg_mentioned_p (dest, PATTERN (q)))
2307 : : failed = 1;
2308 : :
2309 : : /* Attempt to replace all uses. */
2310 : 7498 : else if (!validate_replace_rtx (src, dest, q))
2311 : : failed = 1;
2312 : :
2313 : : /* If this succeeded, but some part of the
2314 : : register is still present, undo the
2315 : : replacement. */
2316 : 7498 : else if (sregno < FIRST_PSEUDO_REGISTER
2317 : 7498 : && reg_overlap_mentioned_p (src, PATTERN (q)))
2318 : : {
2319 : 0 : validate_replace_rtx (dest, src, q);
2320 : 0 : failed = 1;
2321 : : }
2322 : : }
2323 : :
2324 : : /* If DEST dies here, remove the death note and
2325 : : save it for later. Make sure ALL of DEST dies
2326 : : here; again, this is overly conservative. */
2327 : 26223 : if (! dest_death
2328 : 26223 : && (dest_death = find_regno_note (q, REG_DEAD, dregno)))
2329 : : {
2330 : 9 : if (GET_MODE (XEXP (dest_death, 0)) == GET_MODE (dest))
2331 : 9 : remove_note (q, dest_death);
2332 : : else
2333 : : {
2334 : : failed = 1;
2335 : : dest_death = 0;
2336 : : }
2337 : : }
2338 : : }
2339 : :
2340 : 6361 : if (! failed)
2341 : : {
2342 : : /* Move death note of SRC from P to INSN. */
2343 : 6361 : remove_note (p, note);
2344 : 6361 : XEXP (note, 1) = REG_NOTES (insn);
2345 : 6361 : REG_NOTES (insn) = note;
2346 : : }
2347 : :
2348 : : /* DEST is also dead if INSN has a REG_UNUSED note for
2349 : : DEST. */
2350 : 6361 : if (! dest_death
2351 : 6361 : && (dest_death
2352 : 6352 : = find_regno_note (insn, REG_UNUSED, dregno)))
2353 : : {
2354 : 0 : PUT_REG_NOTE_KIND (dest_death, REG_DEAD);
2355 : 0 : remove_note (insn, dest_death);
2356 : : }
2357 : :
2358 : : /* Put death note of DEST on P if we saw it die. */
2359 : 6361 : if (dest_death)
2360 : : {
2361 : 9 : XEXP (dest_death, 1) = REG_NOTES (p);
2362 : 9 : REG_NOTES (p) = dest_death;
2363 : : }
2364 : : break;
2365 : : }
2366 : :
2367 : : /* If SRC is a hard register which is set or killed in
2368 : : some other way, we can't do this optimization. */
2369 : 4219485 : else if (sregno < FIRST_PSEUDO_REGISTER && dead_or_set_p (p, src))
2370 : : break;
2371 : : }
2372 : : }
2373 : : }
2374 : :
2375 : :
2376 : :
2377 : : /* Return nonzero if REGNO is a particularly bad choice for reloading X. */
2378 : : static bool
2379 : 0 : ira_bad_reload_regno_1 (int regno, rtx x)
2380 : : {
2381 : 0 : int x_regno, n, i;
2382 : 0 : ira_allocno_t a;
2383 : 0 : enum reg_class pref;
2384 : :
2385 : : /* We only deal with pseudo regs. */
2386 : 0 : if (! x || GET_CODE (x) != REG)
2387 : : return false;
2388 : :
2389 : 0 : x_regno = REGNO (x);
2390 : 0 : if (x_regno < FIRST_PSEUDO_REGISTER)
2391 : : return false;
2392 : :
2393 : : /* If the pseudo prefers REGNO explicitly, then do not consider
2394 : : REGNO a bad spill choice. */
2395 : 0 : pref = reg_preferred_class (x_regno);
2396 : 0 : if (reg_class_size[pref] == 1)
2397 : 0 : return !TEST_HARD_REG_BIT (reg_class_contents[pref], regno);
2398 : :
2399 : : /* If the pseudo conflicts with REGNO, then we consider REGNO a
2400 : : poor choice for a reload regno. */
2401 : 0 : a = ira_regno_allocno_map[x_regno];
2402 : 0 : n = ALLOCNO_NUM_OBJECTS (a);
2403 : 0 : for (i = 0; i < n; i++)
2404 : : {
2405 : 0 : ira_object_t obj = ALLOCNO_OBJECT (a, i);
2406 : 0 : if (TEST_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), regno))
2407 : : return true;
2408 : : }
2409 : : return false;
2410 : : }
2411 : :
2412 : : /* Return nonzero if REGNO is a particularly bad choice for reloading
2413 : : IN or OUT. */
2414 : : bool
2415 : 0 : ira_bad_reload_regno (int regno, rtx in, rtx out)
2416 : : {
2417 : 0 : return (ira_bad_reload_regno_1 (regno, in)
2418 : 0 : || ira_bad_reload_regno_1 (regno, out));
2419 : : }
2420 : :
2421 : : /* Add register clobbers from asm statements. */
2422 : : static void
2423 : 1510544 : compute_regs_asm_clobbered (void)
2424 : : {
2425 : 1510544 : basic_block bb;
2426 : :
2427 : 16443648 : FOR_EACH_BB_FN (bb, cfun)
2428 : : {
2429 : 14933104 : rtx_insn *insn;
2430 : 177882318 : FOR_BB_INSNS_REVERSE (bb, insn)
2431 : : {
2432 : 162949214 : df_ref def;
2433 : :
2434 : 162949214 : if (NONDEBUG_INSN_P (insn) && asm_noperands (PATTERN (insn)) >= 0)
2435 : 322169 : FOR_EACH_INSN_DEF (def, insn)
2436 : : {
2437 : 215356 : unsigned int dregno = DF_REF_REGNO (def);
2438 : 215356 : if (HARD_REGISTER_NUM_P (dregno))
2439 : 299424 : add_to_hard_reg_set (&crtl->asm_clobbers,
2440 : 149712 : GET_MODE (DF_REF_REAL_REG (def)),
2441 : : dregno);
2442 : : }
2443 : : }
2444 : : }
2445 : 1510544 : }
2446 : :
2447 : :
2448 : : /* Set up ELIMINABLE_REGSET, IRA_NO_ALLOC_REGS, and
2449 : : REGS_EVER_LIVE. */
2450 : : void
2451 : 1510544 : ira_setup_eliminable_regset (void)
2452 : : {
2453 : 1510544 : int i;
2454 : 1510544 : static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
2455 : 1640430 : int fp_reg_count = hard_regno_nregs (HARD_FRAME_POINTER_REGNUM, Pmode);
2456 : :
2457 : : /* Setup is_leaf as frame_pointer_required may use it. This function
2458 : : is called by sched_init before ira if scheduling is enabled. */
2459 : 1510544 : crtl->is_leaf = leaf_function_p ();
2460 : :
2461 : : /* FIXME: If EXIT_IGNORE_STACK is set, we will not save and restore
2462 : : sp for alloca. So we can't eliminate the frame pointer in that
2463 : : case. At some point, we should improve this by emitting the
2464 : : sp-adjusting insns for this case. */
2465 : 1510544 : frame_pointer_needed
2466 : 3021088 : = (! flag_omit_frame_pointer
2467 : 1059988 : || (cfun->calls_alloca && EXIT_IGNORE_STACK)
2468 : : /* We need the frame pointer to catch stack overflow exceptions if
2469 : : the stack pointer is moving (as for the alloca case just above). */
2470 : 1050298 : || (STACK_CHECK_MOVING_SP
2471 : 1050298 : && flag_stack_check
2472 : 57 : && flag_exceptions
2473 : 20 : && cfun->can_throw_non_call_exceptions)
2474 : 1050294 : || crtl->accesses_prior_frames
2475 : 1047438 : || (SUPPORTS_STACK_ALIGNMENT && crtl->stack_realign_needed)
2476 : 2513729 : || targetm.frame_pointer_required ());
2477 : :
2478 : : /* The chance that FRAME_POINTER_NEEDED is changed from inspecting
2479 : : RTL is very small. So if we use frame pointer for RA and RTL
2480 : : actually prevents this, we will spill pseudos assigned to the
2481 : : frame pointer in LRA. */
2482 : :
2483 : 1510544 : if (frame_pointer_needed)
2484 : 1014808 : for (i = 0; i < fp_reg_count; i++)
2485 : 507404 : df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM + i, true);
2486 : :
2487 : 1510544 : ira_no_alloc_regs = no_unit_alloc_regs;
2488 : 1510544 : CLEAR_HARD_REG_SET (eliminable_regset);
2489 : :
2490 : 1510544 : compute_regs_asm_clobbered ();
2491 : :
2492 : : /* Build the regset of all eliminable registers and show we can't
2493 : : use those that we already know won't be eliminated. */
2494 : 7552720 : for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
2495 : : {
2496 : 6042176 : bool cannot_elim
2497 : 6042176 : = (! targetm.can_eliminate (eliminables[i].from, eliminables[i].to)
2498 : 6042176 : || (eliminables[i].to == STACK_POINTER_REGNUM && frame_pointer_needed));
2499 : :
2500 : 6042176 : if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, eliminables[i].from))
2501 : : {
2502 : 6042176 : SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
2503 : :
2504 : 6042176 : if (cannot_elim)
2505 : 1073244 : SET_HARD_REG_BIT (ira_no_alloc_regs, eliminables[i].from);
2506 : : }
2507 : 0 : else if (cannot_elim)
2508 : 0 : error ("%s cannot be used in %<asm%> here",
2509 : : reg_names[eliminables[i].from]);
2510 : : else
2511 : 0 : df_set_regs_ever_live (eliminables[i].from, true);
2512 : : }
2513 : : if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
2514 : : {
2515 : 3021088 : for (i = 0; i < fp_reg_count; i++)
2516 : 1510544 : if (global_regs[HARD_FRAME_POINTER_REGNUM + i])
2517 : : /* Nothing to do: the register is already treated as live
2518 : : where appropriate, and cannot be eliminated. */
2519 : : ;
2520 : 1510523 : else if (!TEST_HARD_REG_BIT (crtl->asm_clobbers,
2521 : : HARD_FRAME_POINTER_REGNUM + i))
2522 : : {
2523 : 1509232 : SET_HARD_REG_BIT (eliminable_regset,
2524 : : HARD_FRAME_POINTER_REGNUM + i);
2525 : 1509232 : if (frame_pointer_needed)
2526 : 507402 : SET_HARD_REG_BIT (ira_no_alloc_regs,
2527 : : HARD_FRAME_POINTER_REGNUM + i);
2528 : : }
2529 : 1291 : else if (frame_pointer_needed)
2530 : 0 : error ("%s cannot be used in %<asm%> here",
2531 : : reg_names[HARD_FRAME_POINTER_REGNUM + i]);
2532 : : else
2533 : 1291 : df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM + i, true);
2534 : : }
2535 : 1510544 : }
2536 : :
2537 : :
2538 : :
2539 : : /* Vector of substitutions of register numbers,
2540 : : used to map pseudo regs into hardware regs.
2541 : : This is set up as a result of register allocation.
2542 : : Element N is the hard reg assigned to pseudo reg N,
2543 : : or is -1 if no hard reg was assigned.
2544 : : If N is a hard reg number, element N is N. */
2545 : : short *reg_renumber;
2546 : :
2547 : : /* Set up REG_RENUMBER and CALLER_SAVE_NEEDED (used by reload) from
2548 : : the allocation found by IRA. */
2549 : : static void
2550 : 1481332 : setup_reg_renumber (void)
2551 : : {
2552 : 1481332 : int regno, hard_regno;
2553 : 1481332 : ira_allocno_t a;
2554 : 1481332 : ira_allocno_iterator ai;
2555 : :
2556 : 1481332 : caller_save_needed = 0;
2557 : 38121000 : FOR_EACH_ALLOCNO (a, ai)
2558 : : {
2559 : 36639668 : if (ira_use_lra_p && ALLOCNO_CAP_MEMBER (a) != NULL)
2560 : 3623925 : continue;
2561 : : /* There are no caps at this point. */
2562 : 33015743 : ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
2563 : 33015743 : if (! ALLOCNO_ASSIGNED_P (a))
2564 : : /* It can happen if A is not referenced but partially anticipated
2565 : : somewhere in a region. */
2566 : 0 : ALLOCNO_ASSIGNED_P (a) = true;
2567 : 33015743 : ira_free_allocno_updated_costs (a);
2568 : 33015743 : hard_regno = ALLOCNO_HARD_REGNO (a);
2569 : 33015743 : regno = ALLOCNO_REGNO (a);
2570 : 33015743 : reg_renumber[regno] = (hard_regno < 0 ? -1 : hard_regno);
2571 : 33015743 : if (hard_regno >= 0)
2572 : : {
2573 : 29706786 : int i, nwords;
2574 : 29706786 : enum reg_class pclass;
2575 : 29706786 : ira_object_t obj;
2576 : :
2577 : 29706786 : pclass = ira_pressure_class_translate[REGNO_REG_CLASS (hard_regno)];
2578 : 29706786 : nwords = ALLOCNO_NUM_OBJECTS (a);
2579 : 60531389 : for (i = 0; i < nwords; i++)
2580 : : {
2581 : 30824603 : obj = ALLOCNO_OBJECT (a, i);
2582 : 30824603 : OBJECT_TOTAL_CONFLICT_HARD_REGS (obj)
2583 : 61649206 : |= ~reg_class_contents[pclass];
2584 : : }
2585 : 29706786 : if (ira_need_caller_save_p (a, hard_regno))
2586 : : {
2587 : 422398 : ira_assert (!optimize || flag_caller_saves
2588 : : || (ALLOCNO_CALLS_CROSSED_NUM (a)
2589 : : == ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a))
2590 : : || regno >= ira_reg_equiv_len
2591 : : || ira_equiv_no_lvalue_p (regno));
2592 : 422398 : caller_save_needed = 1;
2593 : : }
2594 : : }
2595 : : }
2596 : 1481332 : }
2597 : :
2598 : : /* Set up allocno assignment flags for further allocation
2599 : : improvements. */
2600 : : static void
2601 : 0 : setup_allocno_assignment_flags (void)
2602 : : {
2603 : 0 : int hard_regno;
2604 : 0 : ira_allocno_t a;
2605 : 0 : ira_allocno_iterator ai;
2606 : :
2607 : 0 : FOR_EACH_ALLOCNO (a, ai)
2608 : : {
2609 : 0 : if (! ALLOCNO_ASSIGNED_P (a))
2610 : : /* It can happen if A is not referenced but partially anticipated
2611 : : somewhere in a region. */
2612 : 0 : ira_free_allocno_updated_costs (a);
2613 : 0 : hard_regno = ALLOCNO_HARD_REGNO (a);
2614 : : /* Don't assign hard registers to allocnos which are destination
2615 : : of removed store at the end of loop. It has no sense to keep
2616 : : the same value in different hard registers. It is also
2617 : : impossible to assign hard registers correctly to such
2618 : : allocnos because the cost info and info about intersected
2619 : : calls are incorrect for them. */
2620 : 0 : ALLOCNO_ASSIGNED_P (a) = (hard_regno >= 0
2621 : 0 : || ALLOCNO_EMIT_DATA (a)->mem_optimized_dest_p
2622 : 0 : || (ALLOCNO_MEMORY_COST (a)
2623 : 0 : - ALLOCNO_CLASS_COST (a)) < 0);
2624 : 0 : ira_assert
2625 : : (hard_regno < 0
2626 : : || ira_hard_reg_in_set_p (hard_regno, ALLOCNO_MODE (a),
2627 : : reg_class_contents[ALLOCNO_CLASS (a)]));
2628 : : }
2629 : 0 : }
2630 : :
2631 : : /* Evaluate overall allocation cost and the costs for using hard
2632 : : registers and memory for allocnos. */
2633 : : static void
2634 : 1481332 : calculate_allocation_cost (void)
2635 : : {
2636 : 1481332 : int hard_regno, cost;
2637 : 1481332 : ira_allocno_t a;
2638 : 1481332 : ira_allocno_iterator ai;
2639 : :
2640 : 1481332 : ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
2641 : 38121000 : FOR_EACH_ALLOCNO (a, ai)
2642 : : {
2643 : 36639668 : hard_regno = ALLOCNO_HARD_REGNO (a);
2644 : 36639668 : ira_assert (hard_regno < 0
2645 : : || (ira_hard_reg_in_set_p
2646 : : (hard_regno, ALLOCNO_MODE (a),
2647 : : reg_class_contents[ALLOCNO_CLASS (a)])));
2648 : 36639668 : if (hard_regno < 0)
2649 : : {
2650 : 3707953 : cost = ALLOCNO_MEMORY_COST (a);
2651 : 3707953 : ira_mem_cost += cost;
2652 : : }
2653 : 32931715 : else if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
2654 : : {
2655 : 8686839 : cost = (ALLOCNO_HARD_REG_COSTS (a)
2656 : : [ira_class_hard_reg_index
2657 : 8686839 : [ALLOCNO_CLASS (a)][hard_regno]]);
2658 : 8686839 : ira_reg_cost += cost;
2659 : : }
2660 : : else
2661 : : {
2662 : 24244876 : cost = ALLOCNO_CLASS_COST (a);
2663 : 24244876 : ira_reg_cost += cost;
2664 : : }
2665 : 36639668 : ira_overall_cost += cost;
2666 : : }
2667 : :
2668 : 1481332 : if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
2669 : : {
2670 : 95 : fprintf (ira_dump_file,
2671 : : "+++Costs: overall %" PRId64
2672 : : ", reg %" PRId64
2673 : : ", mem %" PRId64
2674 : : ", ld %" PRId64
2675 : : ", st %" PRId64
2676 : : ", move %" PRId64,
2677 : : ira_overall_cost, ira_reg_cost, ira_mem_cost,
2678 : : ira_load_cost, ira_store_cost, ira_shuffle_cost);
2679 : 95 : fprintf (ira_dump_file, "\n+++ move loops %d, new jumps %d\n",
2680 : : ira_move_loops_num, ira_additional_jumps_num);
2681 : : }
2682 : :
2683 : 1481332 : }
2684 : :
2685 : : #ifdef ENABLE_IRA_CHECKING
2686 : : /* Check the correctness of the allocation. We do need this because
2687 : : of complicated code to transform more one region internal
2688 : : representation into one region representation. */
2689 : : static void
2690 : 0 : check_allocation (void)
2691 : : {
2692 : 0 : ira_allocno_t a;
2693 : 0 : int hard_regno, nregs, conflict_nregs;
2694 : 0 : ira_allocno_iterator ai;
2695 : :
2696 : 0 : FOR_EACH_ALLOCNO (a, ai)
2697 : : {
2698 : 0 : int n = ALLOCNO_NUM_OBJECTS (a);
2699 : 0 : int i;
2700 : :
2701 : 0 : if (ALLOCNO_CAP_MEMBER (a) != NULL
2702 : 0 : || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
2703 : 0 : continue;
2704 : 0 : nregs = hard_regno_nregs (hard_regno, ALLOCNO_MODE (a));
2705 : 0 : if (nregs == 1)
2706 : : /* We allocated a single hard register. */
2707 : : n = 1;
2708 : 0 : else if (n > 1)
2709 : : /* We allocated multiple hard registers, and we will test
2710 : : conflicts in a granularity of single hard regs. */
2711 : 0 : nregs = 1;
2712 : :
2713 : 0 : for (i = 0; i < n; i++)
2714 : : {
2715 : 0 : ira_object_t obj = ALLOCNO_OBJECT (a, i);
2716 : 0 : ira_object_t conflict_obj;
2717 : 0 : ira_object_conflict_iterator oci;
2718 : 0 : int this_regno = hard_regno;
2719 : 0 : if (n > 1)
2720 : : {
2721 : 0 : if (REG_WORDS_BIG_ENDIAN)
2722 : : this_regno += n - i - 1;
2723 : : else
2724 : 0 : this_regno += i;
2725 : : }
2726 : 0 : FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2727 : : {
2728 : 0 : ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2729 : 0 : int conflict_hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
2730 : 0 : if (conflict_hard_regno < 0)
2731 : 0 : continue;
2732 : 0 : if (ira_soft_conflict (a, conflict_a))
2733 : 0 : continue;
2734 : :
2735 : 0 : conflict_nregs = hard_regno_nregs (conflict_hard_regno,
2736 : 0 : ALLOCNO_MODE (conflict_a));
2737 : :
2738 : 0 : if (ALLOCNO_NUM_OBJECTS (conflict_a) > 1
2739 : 0 : && conflict_nregs == ALLOCNO_NUM_OBJECTS (conflict_a))
2740 : : {
2741 : 0 : if (REG_WORDS_BIG_ENDIAN)
2742 : : conflict_hard_regno += (ALLOCNO_NUM_OBJECTS (conflict_a)
2743 : : - OBJECT_SUBWORD (conflict_obj) - 1);
2744 : : else
2745 : 0 : conflict_hard_regno += OBJECT_SUBWORD (conflict_obj);
2746 : 0 : conflict_nregs = 1;
2747 : : }
2748 : :
2749 : 0 : if ((conflict_hard_regno <= this_regno
2750 : 0 : && this_regno < conflict_hard_regno + conflict_nregs)
2751 : 0 : || (this_regno <= conflict_hard_regno
2752 : 0 : && conflict_hard_regno < this_regno + nregs))
2753 : : {
2754 : 0 : fprintf (stderr, "bad allocation for %d and %d\n",
2755 : : ALLOCNO_REGNO (a), ALLOCNO_REGNO (conflict_a));
2756 : 0 : gcc_unreachable ();
2757 : : }
2758 : : }
2759 : : }
2760 : : }
2761 : 0 : }
2762 : : #endif
2763 : :
2764 : : /* Allocate REG_EQUIV_INIT. Set up it from IRA_REG_EQUIV which should
2765 : : be already calculated. */
2766 : : static void
2767 : 1481332 : setup_reg_equiv_init (void)
2768 : : {
2769 : 1481332 : int i;
2770 : 1481332 : int max_regno = max_reg_num ();
2771 : :
2772 : 205011007 : for (i = 0; i < max_regno; i++)
2773 : 202048343 : reg_equiv_init (i) = ira_reg_equiv[i].init_insns;
2774 : 1481332 : }
2775 : :
2776 : : /* Update equiv regno from movement of FROM_REGNO to TO_REGNO. INSNS
2777 : : are insns which were generated for such movement. It is assumed
2778 : : that FROM_REGNO and TO_REGNO always have the same value at the
2779 : : point of any move containing such registers. This function is used
2780 : : to update equiv info for register shuffles on the region borders
2781 : : and for caller save/restore insns. */
2782 : : void
2783 : 2170075 : ira_update_equiv_info_by_shuffle_insn (int to_regno, int from_regno, rtx_insn *insns)
2784 : : {
2785 : 2170075 : rtx_insn *insn;
2786 : 2170075 : rtx x, note;
2787 : :
2788 : 2170075 : if (! ira_reg_equiv[from_regno].defined_p
2789 : 2170075 : && (! ira_reg_equiv[to_regno].defined_p
2790 : 1118 : || ((x = ira_reg_equiv[to_regno].memory) != NULL_RTX
2791 : 1118 : && ! MEM_READONLY_P (x))))
2792 : : return;
2793 : 36949 : insn = insns;
2794 : 36949 : if (NEXT_INSN (insn) != NULL_RTX)
2795 : : {
2796 : 0 : if (! ira_reg_equiv[to_regno].defined_p)
2797 : : {
2798 : 0 : ira_assert (ira_reg_equiv[to_regno].init_insns == NULL_RTX);
2799 : : return;
2800 : : }
2801 : 0 : ira_reg_equiv[to_regno].defined_p = false;
2802 : 0 : ira_reg_equiv[to_regno].caller_save_p = false;
2803 : 0 : ira_reg_equiv[to_regno].memory
2804 : 0 : = ira_reg_equiv[to_regno].constant
2805 : 0 : = ira_reg_equiv[to_regno].invariant
2806 : 0 : = ira_reg_equiv[to_regno].init_insns = NULL;
2807 : 0 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2808 : 0 : fprintf (ira_dump_file,
2809 : : " Invalidating equiv info for reg %d\n", to_regno);
2810 : 0 : return;
2811 : : }
2812 : : /* It is possible that FROM_REGNO still has no equivalence because
2813 : : in shuffles to_regno<-from_regno and from_regno<-to_regno the 2nd
2814 : : insn was not processed yet. */
2815 : 36949 : if (ira_reg_equiv[from_regno].defined_p)
2816 : : {
2817 : 36949 : ira_reg_equiv[to_regno].defined_p = true;
2818 : 36949 : if ((x = ira_reg_equiv[from_regno].memory) != NULL_RTX)
2819 : : {
2820 : 36814 : ira_assert (ira_reg_equiv[from_regno].invariant == NULL_RTX
2821 : : && ira_reg_equiv[from_regno].constant == NULL_RTX);
2822 : 36814 : ira_assert (ira_reg_equiv[to_regno].memory == NULL_RTX
2823 : : || rtx_equal_p (ira_reg_equiv[to_regno].memory, x));
2824 : 36814 : ira_reg_equiv[to_regno].memory = x;
2825 : 36814 : if (! MEM_READONLY_P (x))
2826 : : /* We don't add the insn to insn init list because memory
2827 : : equivalence is just to say what memory is better to use
2828 : : when the pseudo is spilled. */
2829 : : return;
2830 : : }
2831 : 135 : else if ((x = ira_reg_equiv[from_regno].constant) != NULL_RTX)
2832 : : {
2833 : 37 : ira_assert (ira_reg_equiv[from_regno].invariant == NULL_RTX);
2834 : 37 : ira_assert (ira_reg_equiv[to_regno].constant == NULL_RTX
2835 : : || rtx_equal_p (ira_reg_equiv[to_regno].constant, x));
2836 : 37 : ira_reg_equiv[to_regno].constant = x;
2837 : : }
2838 : : else
2839 : : {
2840 : 98 : x = ira_reg_equiv[from_regno].invariant;
2841 : 98 : ira_assert (x != NULL_RTX);
2842 : 98 : ira_assert (ira_reg_equiv[to_regno].invariant == NULL_RTX
2843 : : || rtx_equal_p (ira_reg_equiv[to_regno].invariant, x));
2844 : 98 : ira_reg_equiv[to_regno].invariant = x;
2845 : : }
2846 : 153 : if (find_reg_note (insn, REG_EQUIV, x) == NULL_RTX)
2847 : : {
2848 : 153 : note = set_unique_reg_note (insn, REG_EQUIV, copy_rtx (x));
2849 : 153 : gcc_assert (note != NULL_RTX);
2850 : 153 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2851 : : {
2852 : 0 : fprintf (ira_dump_file,
2853 : : " Adding equiv note to insn %u for reg %d ",
2854 : 0 : INSN_UID (insn), to_regno);
2855 : 0 : dump_value_slim (ira_dump_file, x, 1);
2856 : 0 : fprintf (ira_dump_file, "\n");
2857 : : }
2858 : : }
2859 : : }
2860 : 153 : ira_reg_equiv[to_regno].init_insns
2861 : 306 : = gen_rtx_INSN_LIST (VOIDmode, insn,
2862 : 153 : ira_reg_equiv[to_regno].init_insns);
2863 : 153 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2864 : 0 : fprintf (ira_dump_file,
2865 : : " Adding equiv init move insn %u to reg %d\n",
2866 : 0 : INSN_UID (insn), to_regno);
2867 : : }
2868 : :
2869 : : /* Fix values of array REG_EQUIV_INIT after live range splitting done
2870 : : by IRA. */
2871 : : static void
2872 : 2089952 : fix_reg_equiv_init (void)
2873 : : {
2874 : 2089952 : int max_regno = max_reg_num ();
2875 : 2089952 : int i, new_regno, max;
2876 : 2089952 : rtx set;
2877 : 2089952 : rtx_insn_list *x, *next, *prev;
2878 : 2089952 : rtx_insn *insn;
2879 : :
2880 : 2089952 : if (max_regno_before_ira < max_regno)
2881 : : {
2882 : 508602 : max = vec_safe_length (reg_equivs);
2883 : 508602 : grow_reg_equivs ();
2884 : 47996891 : for (i = FIRST_PSEUDO_REGISTER; i < max; i++)
2885 : 47488289 : for (prev = NULL, x = reg_equiv_init (i);
2886 : 52118765 : x != NULL_RTX;
2887 : : x = next)
2888 : : {
2889 : 4630476 : next = x->next ();
2890 : 4630476 : insn = x->insn ();
2891 : 4630476 : set = single_set (insn);
2892 : 4630476 : ira_assert (set != NULL_RTX
2893 : : && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))));
2894 : 4630476 : if (REG_P (SET_DEST (set))
2895 : 4630476 : && ((int) REGNO (SET_DEST (set)) == i
2896 : 0 : || (int) ORIGINAL_REGNO (SET_DEST (set)) == i))
2897 : : new_regno = REGNO (SET_DEST (set));
2898 : 517478 : else if (REG_P (SET_SRC (set))
2899 : 517478 : && ((int) REGNO (SET_SRC (set)) == i
2900 : 0 : || (int) ORIGINAL_REGNO (SET_SRC (set)) == i))
2901 : : new_regno = REGNO (SET_SRC (set));
2902 : : else
2903 : 0 : gcc_unreachable ();
2904 : 4630476 : if (new_regno == i)
2905 : : prev = x;
2906 : : else
2907 : : {
2908 : : /* Remove the wrong list element. */
2909 : 0 : if (prev == NULL_RTX)
2910 : 0 : reg_equiv_init (i) = next;
2911 : : else
2912 : 0 : XEXP (prev, 1) = next;
2913 : 0 : XEXP (x, 1) = reg_equiv_init (new_regno);
2914 : 0 : reg_equiv_init (new_regno) = x;
2915 : : }
2916 : : }
2917 : : }
2918 : 2089952 : }
2919 : :
2920 : : #ifdef ENABLE_IRA_CHECKING
2921 : : /* Print redundant memory-memory copies. */
2922 : : static void
2923 : 1044976 : print_redundant_copies (void)
2924 : : {
2925 : 1044976 : int hard_regno;
2926 : 1044976 : ira_allocno_t a;
2927 : 1044976 : ira_copy_t cp, next_cp;
2928 : 1044976 : ira_allocno_iterator ai;
2929 : :
2930 : 26281314 : FOR_EACH_ALLOCNO (a, ai)
2931 : : {
2932 : 25236338 : if (ALLOCNO_CAP_MEMBER (a) != NULL)
2933 : : /* It is a cap. */
2934 : 3623925 : continue;
2935 : 21612413 : hard_regno = ALLOCNO_HARD_REGNO (a);
2936 : 21612413 : if (hard_regno >= 0)
2937 : 18392942 : continue;
2938 : 4182148 : for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2939 : 962677 : if (cp->first == a)
2940 : 365002 : next_cp = cp->next_first_allocno_copy;
2941 : : else
2942 : : {
2943 : 597675 : next_cp = cp->next_second_allocno_copy;
2944 : 597675 : if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL
2945 : 1 : && cp->insn != NULL_RTX
2946 : 0 : && ALLOCNO_HARD_REGNO (cp->first) == hard_regno)
2947 : 0 : fprintf (ira_dump_file,
2948 : : " Redundant move from %d(freq %d):%d\n",
2949 : 0 : INSN_UID (cp->insn), cp->freq, hard_regno);
2950 : : }
2951 : : }
2952 : 1044976 : }
2953 : : #endif
2954 : :
2955 : : /* Setup preferred and alternative classes for new pseudo-registers
2956 : : created by IRA starting with START. */
2957 : : static void
2958 : 1080646 : setup_preferred_alternate_classes_for_new_pseudos (int start)
2959 : : {
2960 : 1080646 : int i, old_regno;
2961 : 1080646 : int max_regno = max_reg_num ();
2962 : :
2963 : 2242817 : for (i = start; i < max_regno; i++)
2964 : : {
2965 : 1162171 : old_regno = ORIGINAL_REGNO (regno_reg_rtx[i]);
2966 : 1162171 : ira_assert (i != old_regno);
2967 : 1162171 : setup_reg_classes (i, reg_preferred_class (old_regno),
2968 : : reg_alternate_class (old_regno),
2969 : : reg_allocno_class (old_regno));
2970 : 1162171 : if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2971 : 0 : fprintf (ira_dump_file,
2972 : : " New r%d: setting preferred %s, alternative %s\n",
2973 : 0 : i, reg_class_names[reg_preferred_class (old_regno)],
2974 : 0 : reg_class_names[reg_alternate_class (old_regno)]);
2975 : : }
2976 : 1080646 : }
2977 : :
2978 : :
2979 : : /* The number of entries allocated in reg_info. */
2980 : : static int allocated_reg_info_size;
2981 : :
2982 : : /* Regional allocation can create new pseudo-registers. This function
2983 : : expands some arrays for pseudo-registers. */
2984 : : static void
2985 : 1080646 : expand_reg_info (void)
2986 : : {
2987 : 1080646 : int i;
2988 : 1080646 : int size = max_reg_num ();
2989 : :
2990 : 1080646 : resize_reg_info ();
2991 : 2242817 : for (i = allocated_reg_info_size; i < size; i++)
2992 : 1162171 : setup_reg_classes (i, GENERAL_REGS, ALL_REGS, GENERAL_REGS);
2993 : 1080646 : setup_preferred_alternate_classes_for_new_pseudos (allocated_reg_info_size);
2994 : 1080646 : allocated_reg_info_size = size;
2995 : 1080646 : }
2996 : :
2997 : : /* Return TRUE if there is too high register pressure in the function.
2998 : : It is used to decide when stack slot sharing is worth to do. */
2999 : : static bool
3000 : 1481332 : too_high_register_pressure_p (void)
3001 : : {
3002 : 1481332 : int i;
3003 : 1481332 : enum reg_class pclass;
3004 : :
3005 : 7441831 : for (i = 0; i < ira_pressure_classes_num; i++)
3006 : : {
3007 : 5960501 : pclass = ira_pressure_classes[i];
3008 : 5960501 : if (ira_loop_tree_root->reg_pressure[pclass] > 10000)
3009 : : return true;
3010 : : }
3011 : : return false;
3012 : : }
3013 : :
3014 : :
3015 : :
3016 : : /* Indicate that hard register number FROM was eliminated and replaced with
3017 : : an offset from hard register number TO. The status of hard registers live
3018 : : at the start of a basic block is updated by replacing a use of FROM with
3019 : : a use of TO. */
3020 : :
3021 : : void
3022 : 0 : mark_elimination (int from, int to)
3023 : : {
3024 : 0 : basic_block bb;
3025 : 0 : bitmap r;
3026 : :
3027 : 0 : FOR_EACH_BB_FN (bb, cfun)
3028 : : {
3029 : 0 : r = DF_LR_IN (bb);
3030 : 0 : if (bitmap_bit_p (r, from))
3031 : : {
3032 : 0 : bitmap_clear_bit (r, from);
3033 : 0 : bitmap_set_bit (r, to);
3034 : : }
3035 : 0 : if (! df_live)
3036 : 0 : continue;
3037 : 0 : r = DF_LIVE_IN (bb);
3038 : 0 : if (bitmap_bit_p (r, from))
3039 : : {
3040 : 0 : bitmap_clear_bit (r, from);
3041 : 0 : bitmap_set_bit (r, to);
3042 : : }
3043 : : }
3044 : 0 : }
3045 : :
3046 : :
3047 : :
3048 : : /* The length of the following array. */
3049 : : int ira_reg_equiv_len;
3050 : :
3051 : : /* Info about equiv. info for each register. */
3052 : : struct ira_reg_equiv_s *ira_reg_equiv;
3053 : :
3054 : : /* Expand ira_reg_equiv if necessary. */
3055 : : void
3056 : 11656673 : ira_expand_reg_equiv (void)
3057 : : {
3058 : 11656673 : int old = ira_reg_equiv_len;
3059 : :
3060 : 11656673 : if (ira_reg_equiv_len > max_reg_num ())
3061 : : return;
3062 : 1484432 : ira_reg_equiv_len = max_reg_num () * 3 / 2 + 1;
3063 : 1484432 : ira_reg_equiv
3064 : 2968864 : = (struct ira_reg_equiv_s *) xrealloc (ira_reg_equiv,
3065 : 1484432 : ira_reg_equiv_len
3066 : : * sizeof (struct ira_reg_equiv_s));
3067 : 1484432 : gcc_assert (old < ira_reg_equiv_len);
3068 : 1484432 : memset (ira_reg_equiv + old, 0,
3069 : 1484432 : sizeof (struct ira_reg_equiv_s) * (ira_reg_equiv_len - old));
3070 : : }
3071 : :
3072 : : static void
3073 : 1481332 : init_reg_equiv (void)
3074 : : {
3075 : 1481332 : ira_reg_equiv_len = 0;
3076 : 1481332 : ira_reg_equiv = NULL;
3077 : 0 : ira_expand_reg_equiv ();
3078 : 0 : }
3079 : :
3080 : : static void
3081 : 1481332 : finish_reg_equiv (void)
3082 : : {
3083 : 1481332 : free (ira_reg_equiv);
3084 : 0 : }
3085 : :
3086 : :
3087 : :
3088 : : struct equivalence
3089 : : {
3090 : : /* Set when a REG_EQUIV note is found or created. Use to
3091 : : keep track of what memory accesses might be created later,
3092 : : e.g. by reload. */
3093 : : rtx replacement;
3094 : : rtx *src_p;
3095 : :
3096 : : /* The list of each instruction which initializes this register.
3097 : :
3098 : : NULL indicates we know nothing about this register's equivalence
3099 : : properties.
3100 : :
3101 : : An INSN_LIST with a NULL insn indicates this pseudo is already
3102 : : known to not have a valid equivalence. */
3103 : : rtx_insn_list *init_insns;
3104 : :
3105 : : /* Loop depth is used to recognize equivalences which appear
3106 : : to be present within the same loop (or in an inner loop). */
3107 : : short loop_depth;
3108 : : /* Nonzero if this had a preexisting REG_EQUIV note. */
3109 : : unsigned char is_arg_equivalence : 1;
3110 : : /* Set when an attempt should be made to replace a register
3111 : : with the associated src_p entry. */
3112 : : unsigned char replace : 1;
3113 : : /* Set if this register has no known equivalence. */
3114 : : unsigned char no_equiv : 1;
3115 : : /* Set if this register is mentioned in a paradoxical subreg. */
3116 : : unsigned char pdx_subregs : 1;
3117 : : };
3118 : :
3119 : : /* reg_equiv[N] (where N is a pseudo reg number) is the equivalence
3120 : : structure for that register. */
3121 : : static struct equivalence *reg_equiv;
3122 : :
3123 : : /* Used for communication between the following two functions. */
3124 : : struct equiv_mem_data
3125 : : {
3126 : : /* A MEM that we wish to ensure remains unchanged. */
3127 : : rtx equiv_mem;
3128 : :
3129 : : /* Set true if EQUIV_MEM is modified. */
3130 : : bool equiv_mem_modified;
3131 : : };
3132 : :
3133 : : /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
3134 : : Called via note_stores. */
3135 : : static void
3136 : 14049253 : validate_equiv_mem_from_store (rtx dest, const_rtx set ATTRIBUTE_UNUSED,
3137 : : void *data)
3138 : : {
3139 : 14049253 : struct equiv_mem_data *info = (struct equiv_mem_data *) data;
3140 : :
3141 : 14049253 : if ((REG_P (dest)
3142 : 10462361 : && reg_overlap_mentioned_p (dest, info->equiv_mem))
3143 : 24496468 : || (MEM_P (dest)
3144 : 3554779 : && anti_dependence (info->equiv_mem, dest)))
3145 : 292670 : info->equiv_mem_modified = true;
3146 : 14049253 : }
3147 : :
3148 : : static bool equiv_init_varies_p (rtx x);
3149 : :
3150 : : enum valid_equiv { valid_none, valid_combine, valid_reload };
3151 : :
3152 : : /* Verify that no store between START and the death of REG invalidates
3153 : : MEMREF. MEMREF is invalidated by modifying a register used in MEMREF,
3154 : : by storing into an overlapping memory location, or with a non-const
3155 : : CALL_INSN.
3156 : :
3157 : : Return VALID_RELOAD if MEMREF remains valid for both reload and
3158 : : combine_and_move insns, VALID_COMBINE if only valid for
3159 : : combine_and_move_insns, and VALID_NONE otherwise. */
3160 : : static enum valid_equiv
3161 : 4038059 : validate_equiv_mem (rtx_insn *start, rtx reg, rtx memref)
3162 : : {
3163 : 4038059 : rtx_insn *insn;
3164 : 4038059 : rtx note;
3165 : 4038059 : struct equiv_mem_data info = { memref, false };
3166 : 4038059 : enum valid_equiv ret = valid_reload;
3167 : :
3168 : : /* If the memory reference has side effects or is volatile, it isn't a
3169 : : valid equivalence. */
3170 : 4038059 : if (side_effects_p (memref))
3171 : : return valid_none;
3172 : :
3173 : 21100668 : for (insn = start; insn; insn = NEXT_INSN (insn))
3174 : : {
3175 : 21100467 : if (!INSN_P (insn))
3176 : 1412933 : continue;
3177 : :
3178 : 19687534 : if (find_reg_note (insn, REG_DEAD, reg))
3179 : : return ret;
3180 : :
3181 : 16833150 : if (CALL_P (insn))
3182 : : {
3183 : : /* We can combine a reg def from one insn into a reg use in
3184 : : another over a call if the memory is readonly or the call
3185 : : const/pure. However, we can't set reg_equiv notes up for
3186 : : reload over any call. The problem is the equivalent form
3187 : : may reference a pseudo which gets assigned a call
3188 : : clobbered hard reg. When we later replace REG with its
3189 : : equivalent form, the value in the call-clobbered reg has
3190 : : been changed and all hell breaks loose. */
3191 : 94602 : ret = valid_combine;
3192 : 94602 : if (!MEM_READONLY_P (memref)
3193 : 94602 : && (!RTL_CONST_OR_PURE_CALL_P (insn)
3194 : 8371 : || equiv_init_varies_p (XEXP (memref, 0))))
3195 : 88534 : return valid_none;
3196 : : }
3197 : :
3198 : 16744616 : note_stores (insn, validate_equiv_mem_from_store, &info);
3199 : 16744616 : if (info.equiv_mem_modified)
3200 : : return valid_none;
3201 : :
3202 : : /* If a register mentioned in MEMREF is modified via an
3203 : : auto-increment, we lose the equivalence. Do the same if one
3204 : : dies; although we could extend the life, it doesn't seem worth
3205 : : the trouble. */
3206 : :
3207 : 22931606 : for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3208 : 7092439 : if ((REG_NOTE_KIND (note) == REG_INC
3209 : 7092439 : || REG_NOTE_KIND (note) == REG_DEAD)
3210 : 5336825 : && REG_P (XEXP (note, 0))
3211 : 12429264 : && reg_overlap_mentioned_p (XEXP (note, 0), memref))
3212 : : return valid_none;
3213 : : }
3214 : :
3215 : : return valid_none;
3216 : : }
3217 : :
3218 : : /* Returns false if X is known to be invariant. */
3219 : : static bool
3220 : 888314 : equiv_init_varies_p (rtx x)
3221 : : {
3222 : 888314 : RTX_CODE code = GET_CODE (x);
3223 : 888314 : int i;
3224 : 888314 : const char *fmt;
3225 : :
3226 : 888314 : switch (code)
3227 : : {
3228 : 252167 : case MEM:
3229 : 252167 : return !MEM_READONLY_P (x) || equiv_init_varies_p (XEXP (x, 0));
3230 : :
3231 : : case CONST:
3232 : : CASE_CONST_ANY:
3233 : : case SYMBOL_REF:
3234 : : case LABEL_REF:
3235 : : return false;
3236 : :
3237 : 190177 : case REG:
3238 : 190177 : return reg_equiv[REGNO (x)].replace == 0 && rtx_varies_p (x, 0);
3239 : :
3240 : 0 : case ASM_OPERANDS:
3241 : 0 : if (MEM_VOLATILE_P (x))
3242 : : return true;
3243 : :
3244 : : /* Fall through. */
3245 : :
3246 : 133279 : default:
3247 : 133279 : break;
3248 : : }
3249 : :
3250 : 133279 : fmt = GET_RTX_FORMAT (code);
3251 : 327886 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3252 : 221073 : if (fmt[i] == 'e')
3253 : : {
3254 : 219196 : if (equiv_init_varies_p (XEXP (x, i)))
3255 : : return true;
3256 : : }
3257 : 1877 : else if (fmt[i] == 'E')
3258 : : {
3259 : : int j;
3260 : 3809 : for (j = 0; j < XVECLEN (x, i); j++)
3261 : 3453 : if (equiv_init_varies_p (XVECEXP (x, i, j)))
3262 : : return true;
3263 : : }
3264 : :
3265 : : return false;
3266 : : }
3267 : :
3268 : : /* Returns true if X (used to initialize register REGNO) is movable.
3269 : : X is only movable if the registers it uses have equivalent initializations
3270 : : which appear to be within the same loop (or in an inner loop) and movable
3271 : : or if they are not candidates for local_alloc and don't vary. */
3272 : : static bool
3273 : 10650452 : equiv_init_movable_p (rtx x, int regno)
3274 : : {
3275 : 13646419 : int i, j;
3276 : 13646419 : const char *fmt;
3277 : 13646419 : enum rtx_code code = GET_CODE (x);
3278 : :
3279 : 13646419 : switch (code)
3280 : : {
3281 : 2995967 : case SET:
3282 : 2995967 : return equiv_init_movable_p (SET_SRC (x), regno);
3283 : :
3284 : : case CLOBBER:
3285 : : return false;
3286 : :
3287 : : case PRE_INC:
3288 : : case PRE_DEC:
3289 : : case POST_INC:
3290 : : case POST_DEC:
3291 : : case PRE_MODIFY:
3292 : : case POST_MODIFY:
3293 : : return false;
3294 : :
3295 : 1828139 : case REG:
3296 : 1828139 : return ((reg_equiv[REGNO (x)].loop_depth >= reg_equiv[regno].loop_depth
3297 : 1341988 : && reg_equiv[REGNO (x)].replace)
3298 : 3098074 : || (REG_BASIC_BLOCK (REGNO (x)) < NUM_FIXED_BLOCKS
3299 : 1608676 : && ! rtx_varies_p (x, 0)));
3300 : :
3301 : : case UNSPEC_VOLATILE:
3302 : : return false;
3303 : :
3304 : 0 : case ASM_OPERANDS:
3305 : 0 : if (MEM_VOLATILE_P (x))
3306 : : return false;
3307 : :
3308 : : /* Fall through. */
3309 : :
3310 : 8177879 : default:
3311 : 8177879 : break;
3312 : : }
3313 : :
3314 : 8177879 : fmt = GET_RTX_FORMAT (code);
3315 : 19396009 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3316 : 13285992 : switch (fmt[i])
3317 : : {
3318 : 6156323 : case 'e':
3319 : 6156323 : if (! equiv_init_movable_p (XEXP (x, i), regno))
3320 : : return false;
3321 : : break;
3322 : 758468 : case 'E':
3323 : 967762 : for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3324 : 853728 : if (! equiv_init_movable_p (XVECEXP (x, i, j), regno))
3325 : : return false;
3326 : : break;
3327 : : }
3328 : :
3329 : : return true;
3330 : : }
3331 : :
3332 : : static bool memref_referenced_p (rtx memref, rtx x, bool read_p);
3333 : :
3334 : : /* Auxiliary function for memref_referenced_p. Process setting X for
3335 : : MEMREF store. */
3336 : : static bool
3337 : 805618 : process_set_for_memref_referenced_p (rtx memref, rtx x)
3338 : : {
3339 : : /* If we are setting a MEM, it doesn't count (its address does), but any
3340 : : other SET_DEST that has a MEM in it is referencing the MEM. */
3341 : 805618 : if (MEM_P (x))
3342 : : {
3343 : 674910 : if (memref_referenced_p (memref, XEXP (x, 0), true))
3344 : : return true;
3345 : : }
3346 : 130708 : else if (memref_referenced_p (memref, x, false))
3347 : : return true;
3348 : :
3349 : : return false;
3350 : : }
3351 : :
3352 : : /* TRUE if X references a memory location (as a read if READ_P) that
3353 : : would be affected by a store to MEMREF. */
3354 : : static bool
3355 : 3872874 : memref_referenced_p (rtx memref, rtx x, bool read_p)
3356 : : {
3357 : 3872874 : int i, j;
3358 : 3872874 : const char *fmt;
3359 : 3872874 : enum rtx_code code = GET_CODE (x);
3360 : :
3361 : 3872874 : switch (code)
3362 : : {
3363 : : case CONST:
3364 : : case LABEL_REF:
3365 : : case SYMBOL_REF:
3366 : : CASE_CONST_ANY:
3367 : : case PC:
3368 : : case HIGH:
3369 : : case LO_SUM:
3370 : : return false;
3371 : :
3372 : 1559688 : case REG:
3373 : 1559688 : return (reg_equiv[REGNO (x)].replacement
3374 : 1623635 : && memref_referenced_p (memref,
3375 : 63947 : reg_equiv[REGNO (x)].replacement, read_p));
3376 : :
3377 : 105418 : case MEM:
3378 : : /* Memory X might have another effective type than MEMREF. */
3379 : 105418 : if (read_p || true_dependence (memref, VOIDmode, x))
3380 : 93754 : return true;
3381 : : break;
3382 : :
3383 : 791926 : case SET:
3384 : 791926 : if (process_set_for_memref_referenced_p (memref, SET_DEST (x)))
3385 : : return true;
3386 : :
3387 : 777122 : return memref_referenced_p (memref, SET_SRC (x), true);
3388 : :
3389 : 13692 : case CLOBBER:
3390 : 13692 : if (process_set_for_memref_referenced_p (memref, XEXP (x, 0)))
3391 : : return true;
3392 : :
3393 : : return false;
3394 : :
3395 : 0 : case PRE_DEC:
3396 : 0 : case POST_DEC:
3397 : 0 : case PRE_INC:
3398 : 0 : case POST_INC:
3399 : 0 : if (process_set_for_memref_referenced_p (memref, XEXP (x, 0)))
3400 : : return true;
3401 : :
3402 : 0 : return memref_referenced_p (memref, XEXP (x, 0), true);
3403 : :
3404 : 0 : case POST_MODIFY:
3405 : 0 : case PRE_MODIFY:
3406 : : /* op0 = op0 + op1 */
3407 : 0 : if (process_set_for_memref_referenced_p (memref, XEXP (x, 0)))
3408 : : return true;
3409 : :
3410 : 0 : if (memref_referenced_p (memref, XEXP (x, 0), true))
3411 : : return true;
3412 : :
3413 : 0 : return memref_referenced_p (memref, XEXP (x, 1), true);
3414 : :
3415 : : default:
3416 : : break;
3417 : : }
3418 : :
3419 : 735800 : fmt = GET_RTX_FORMAT (code);
3420 : 2141450 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3421 : 1436601 : switch (fmt[i])
3422 : : {
3423 : 1399925 : case 'e':
3424 : 1399925 : if (memref_referenced_p (memref, XEXP (x, i), read_p))
3425 : : return true;
3426 : : break;
3427 : 17317 : case 'E':
3428 : 48906 : for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3429 : 34362 : if (memref_referenced_p (memref, XVECEXP (x, i, j), read_p))
3430 : : return true;
3431 : : break;
3432 : : }
3433 : :
3434 : : return false;
3435 : : }
3436 : :
3437 : : /* TRUE if some insn in the range (START, END] references a memory location
3438 : : that would be affected by a store to MEMREF.
3439 : :
3440 : : Callers should not call this routine if START is after END in the
3441 : : RTL chain. */
3442 : :
3443 : : static bool
3444 : 616581 : memref_used_between_p (rtx memref, rtx_insn *start, rtx_insn *end)
3445 : : {
3446 : 616581 : rtx_insn *insn;
3447 : :
3448 : 2024813 : for (insn = NEXT_INSN (start);
3449 : 4033753 : insn && insn != NEXT_INSN (end);
3450 : 1408232 : insn = NEXT_INSN (insn))
3451 : : {
3452 : 1501986 : if (!NONDEBUG_INSN_P (insn))
3453 : 710086 : continue;
3454 : :
3455 : 791900 : if (memref_referenced_p (memref, PATTERN (insn), false))
3456 : : return true;
3457 : :
3458 : : /* Nonconst functions may access memory. */
3459 : 698146 : if (CALL_P (insn) && (! RTL_CONST_CALL_P (insn)))
3460 : : return true;
3461 : : }
3462 : :
3463 : 522827 : gcc_assert (insn == NEXT_INSN (end));
3464 : : return false;
3465 : : }
3466 : :
3467 : : /* Mark REG as having no known equivalence.
3468 : : Some instructions might have been processed before and furnished
3469 : : with REG_EQUIV notes for this register; these notes will have to be
3470 : : removed.
3471 : : STORE is the piece of RTL that does the non-constant / conflicting
3472 : : assignment - a SET, CLOBBER or REG_INC note. It is currently not used,
3473 : : but needs to be there because this function is called from note_stores. */
3474 : : static void
3475 : 51506353 : no_equiv (rtx reg, const_rtx store ATTRIBUTE_UNUSED,
3476 : : void *data ATTRIBUTE_UNUSED)
3477 : : {
3478 : 51506353 : int regno;
3479 : 51506353 : rtx_insn_list *list;
3480 : :
3481 : 51506353 : if (!REG_P (reg))
3482 : : return;
3483 : 35655702 : regno = REGNO (reg);
3484 : 35655702 : reg_equiv[regno].no_equiv = 1;
3485 : 35655702 : list = reg_equiv[regno].init_insns;
3486 : 64872838 : if (list && list->insn () == NULL)
3487 : : return;
3488 : 7231486 : reg_equiv[regno].init_insns = gen_rtx_INSN_LIST (VOIDmode, NULL_RTX, NULL);
3489 : 7231486 : reg_equiv[regno].replacement = NULL_RTX;
3490 : : /* This doesn't matter for equivalences made for argument registers, we
3491 : : should keep their initialization insns. */
3492 : 7231486 : if (reg_equiv[regno].is_arg_equivalence)
3493 : : return;
3494 : 7226269 : ira_reg_equiv[regno].defined_p = false;
3495 : 7226269 : ira_reg_equiv[regno].caller_save_p = false;
3496 : 7226269 : ira_reg_equiv[regno].init_insns = NULL;
3497 : 8086230 : for (; list; list = list->next ())
3498 : : {
3499 : 859961 : rtx_insn *insn = list->insn ();
3500 : 859961 : remove_note (insn, find_reg_note (insn, REG_EQUIV, NULL_RTX));
3501 : : }
3502 : : }
3503 : :
3504 : : /* Check whether the SUBREG is a paradoxical subreg and set the result
3505 : : in PDX_SUBREGS. */
3506 : :
3507 : : static void
3508 : 84446158 : set_paradoxical_subreg (rtx_insn *insn)
3509 : : {
3510 : 84446158 : subrtx_iterator::array_type array;
3511 : 533450112 : FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
3512 : : {
3513 : 449003954 : const_rtx subreg = *iter;
3514 : 449003954 : if (GET_CODE (subreg) == SUBREG)
3515 : : {
3516 : 2892826 : const_rtx reg = SUBREG_REG (subreg);
3517 : 2892826 : if (REG_P (reg) && paradoxical_subreg_p (subreg))
3518 : 819504 : reg_equiv[REGNO (reg)].pdx_subregs = true;
3519 : : }
3520 : : }
3521 : 84446158 : }
3522 : :
3523 : : /* In DEBUG_INSN location adjust REGs from CLEARED_REGS bitmap to the
3524 : : equivalent replacement. */
3525 : :
3526 : : static rtx
3527 : 41642296 : adjust_cleared_regs (rtx loc, const_rtx old_rtx ATTRIBUTE_UNUSED, void *data)
3528 : : {
3529 : 41642296 : if (REG_P (loc))
3530 : : {
3531 : 6323682 : bitmap cleared_regs = (bitmap) data;
3532 : 6323682 : if (bitmap_bit_p (cleared_regs, REGNO (loc)))
3533 : 17824 : return simplify_replace_fn_rtx (copy_rtx (*reg_equiv[REGNO (loc)].src_p),
3534 : 17824 : NULL_RTX, adjust_cleared_regs, data);
3535 : : }
3536 : : return NULL_RTX;
3537 : : }
3538 : :
3539 : : /* Given register REGNO is set only once, return true if the defining
3540 : : insn dominates all uses. */
3541 : :
3542 : : static bool
3543 : 48721 : def_dominates_uses (int regno)
3544 : : {
3545 : 48721 : df_ref def = DF_REG_DEF_CHAIN (regno);
3546 : :
3547 : 48721 : struct df_insn_info *def_info = DF_REF_INSN_INFO (def);
3548 : : /* If this is an artificial def (eh handler regs, hard frame pointer
3549 : : for non-local goto, regs defined on function entry) then def_info
3550 : : is NULL and the reg is always live before any use. We might
3551 : : reasonably return true in that case, but since the only call
3552 : : of this function is currently here in ira.cc when we are looking
3553 : : at a defining insn we can't have an artificial def as that would
3554 : : bump DF_REG_DEF_COUNT. */
3555 : 48721 : gcc_assert (DF_REG_DEF_COUNT (regno) == 1 && def_info != NULL);
3556 : :
3557 : 48721 : rtx_insn *def_insn = DF_REF_INSN (def);
3558 : 48721 : basic_block def_bb = BLOCK_FOR_INSN (def_insn);
3559 : :
3560 : 48721 : for (df_ref use = DF_REG_USE_CHAIN (regno);
3561 : 139357 : use;
3562 : 90636 : use = DF_REF_NEXT_REG (use))
3563 : : {
3564 : 90637 : struct df_insn_info *use_info = DF_REF_INSN_INFO (use);
3565 : : /* Only check real uses, not artificial ones. */
3566 : 90637 : if (use_info)
3567 : : {
3568 : 90637 : rtx_insn *use_insn = DF_REF_INSN (use);
3569 : 90637 : if (!DEBUG_INSN_P (use_insn))
3570 : : {
3571 : 90380 : basic_block use_bb = BLOCK_FOR_INSN (use_insn);
3572 : 90380 : if (use_bb != def_bb
3573 : 90380 : ? !dominated_by_p (CDI_DOMINATORS, use_bb, def_bb)
3574 : 53515 : : DF_INSN_INFO_LUID (use_info) < DF_INSN_INFO_LUID (def_info))
3575 : : return false;
3576 : : }
3577 : : }
3578 : : }
3579 : : return true;
3580 : : }
3581 : :
3582 : : /* Scan the instructions before update_equiv_regs. Record which registers
3583 : : are referenced as paradoxical subregs. Also check for cases in which
3584 : : the current function needs to save a register that one of its call
3585 : : instructions clobbers.
3586 : :
3587 : : These things are logically unrelated, but it's more efficient to do
3588 : : them together. */
3589 : :
3590 : : static void
3591 : 1481332 : update_equiv_regs_prescan (void)
3592 : : {
3593 : 1481332 : basic_block bb;
3594 : 1481332 : rtx_insn *insn;
3595 : 1481332 : function_abi_aggregator callee_abis;
3596 : :
3597 : 16080290 : FOR_EACH_BB_FN (bb, cfun)
3598 : 175426123 : FOR_BB_INSNS (bb, insn)
3599 : 160827165 : if (NONDEBUG_INSN_P (insn))
3600 : : {
3601 : 84446158 : set_paradoxical_subreg (insn);
3602 : 84446158 : if (CALL_P (insn))
3603 : 6035724 : callee_abis.note_callee_abi (insn_callee_abi (insn));
3604 : : }
3605 : :
3606 : 1481332 : HARD_REG_SET extra_caller_saves = callee_abis.caller_save_regs (*crtl->abi);
3607 : 2962664 : if (!hard_reg_set_empty_p (extra_caller_saves))
3608 : 0 : for (unsigned int regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
3609 : 0 : if (TEST_HARD_REG_BIT (extra_caller_saves, regno))
3610 : 0 : df_set_regs_ever_live (regno, true);
3611 : 1481332 : }
3612 : :
3613 : : /* Find registers that are equivalent to a single value throughout the
3614 : : compilation (either because they can be referenced in memory or are
3615 : : set once from a single constant). Lower their priority for a
3616 : : register.
3617 : :
3618 : : If such a register is only referenced once, try substituting its
3619 : : value into the using insn. If it succeeds, we can eliminate the
3620 : : register completely.
3621 : :
3622 : : Initialize init_insns in ira_reg_equiv array. */
3623 : : static void
3624 : 1481332 : update_equiv_regs (void)
3625 : : {
3626 : 1481332 : rtx_insn *insn;
3627 : 1481332 : basic_block bb;
3628 : :
3629 : : /* Scan the insns and find which registers have equivalences. Do this
3630 : : in a separate scan of the insns because (due to -fcse-follow-jumps)
3631 : : a register can be set below its use. */
3632 : 1481332 : bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
3633 : 16080290 : FOR_EACH_BB_FN (bb, cfun)
3634 : : {
3635 : 14598958 : int loop_depth = bb_loop_depth (bb);
3636 : :
3637 : 175426123 : for (insn = BB_HEAD (bb);
3638 : 175426123 : insn != NEXT_INSN (BB_END (bb));
3639 : 160827165 : insn = NEXT_INSN (insn))
3640 : : {
3641 : 160827165 : rtx note;
3642 : 160827165 : rtx set;
3643 : 160827165 : rtx dest, src;
3644 : 160827165 : int regno;
3645 : :
3646 : 160827165 : if (! INSN_P (insn))
3647 : 26833762 : continue;
3648 : :
3649 : 220009253 : for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3650 : 86015850 : if (REG_NOTE_KIND (note) == REG_INC)
3651 : 0 : no_equiv (XEXP (note, 0), note, NULL);
3652 : :
3653 : 133993403 : set = single_set (insn);
3654 : :
3655 : : /* If this insn contains more (or less) than a single SET,
3656 : : only mark all destinations as having no known equivalence. */
3657 : 191904291 : if (set == NULL_RTX
3658 : 133993403 : || side_effects_p (SET_SRC (set)))
3659 : : {
3660 : 57910888 : note_pattern_stores (PATTERN (insn), no_equiv, NULL);
3661 : 57910888 : continue;
3662 : : }
3663 : 76082515 : else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3664 : : {
3665 : 10598913 : int i;
3666 : :
3667 : 31982662 : for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
3668 : : {
3669 : 21383749 : rtx part = XVECEXP (PATTERN (insn), 0, i);
3670 : 21383749 : if (part != set)
3671 : 10784836 : note_pattern_stores (part, no_equiv, NULL);
3672 : : }
3673 : : }
3674 : :
3675 : 76082515 : dest = SET_DEST (set);
3676 : 76082515 : src = SET_SRC (set);
3677 : :
3678 : : /* See if this is setting up the equivalence between an argument
3679 : : register and its stack slot. */
3680 : 76082515 : note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
3681 : 76082515 : if (note)
3682 : : {
3683 : 229419 : gcc_assert (REG_P (dest));
3684 : 229419 : regno = REGNO (dest);
3685 : :
3686 : : /* Note that we don't want to clear init_insns in
3687 : : ira_reg_equiv even if there are multiple sets of this
3688 : : register. */
3689 : 229419 : reg_equiv[regno].is_arg_equivalence = 1;
3690 : :
3691 : : /* The insn result can have equivalence memory although
3692 : : the equivalence is not set up by the insn. We add
3693 : : this insn to init insns as it is a flag for now that
3694 : : regno has an equivalence. We will remove the insn
3695 : : from init insn list later. */
3696 : 229419 : if (rtx_equal_p (src, XEXP (note, 0)) || MEM_P (XEXP (note, 0)))
3697 : 229419 : ira_reg_equiv[regno].init_insns
3698 : 229419 : = gen_rtx_INSN_LIST (VOIDmode, insn,
3699 : 229419 : ira_reg_equiv[regno].init_insns);
3700 : :
3701 : : /* Continue normally in case this is a candidate for
3702 : : replacements. */
3703 : : }
3704 : :
3705 : 76082515 : if (!optimize)
3706 : 22800970 : continue;
3707 : :
3708 : : /* We only handle the case of a pseudo register being set
3709 : : once, or always to the same value. */
3710 : : /* ??? The mn10200 port breaks if we add equivalences for
3711 : : values that need an ADDRESS_REGS register and set them equivalent
3712 : : to a MEM of a pseudo. The actual problem is in the over-conservative
3713 : : handling of INPADDR_ADDRESS / INPUT_ADDRESS / INPUT triples in
3714 : : calculate_needs, but we traditionally work around this problem
3715 : : here by rejecting equivalences when the destination is in a register
3716 : : that's likely spilled. This is fragile, of course, since the
3717 : : preferred class of a pseudo depends on all instructions that set
3718 : : or use it. */
3719 : :
3720 : 87991696 : if (!REG_P (dest)
3721 : 37096918 : || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
3722 : 21278230 : || (reg_equiv[regno].init_insns
3723 : 3613155 : && reg_equiv[regno].init_insns->insn () == NULL)
3724 : 71853002 : || (targetm.class_likely_spilled_p (reg_preferred_class (regno))
3725 : 370 : && MEM_P (src) && ! reg_equiv[regno].is_arg_equivalence))
3726 : : {
3727 : : /* This might be setting a SUBREG of a pseudo, a pseudo that is
3728 : : also set somewhere else to a constant. */
3729 : 34710151 : note_pattern_stores (set, no_equiv, NULL);
3730 : 34710151 : continue;
3731 : : }
3732 : :
3733 : : /* Don't set reg mentioned in a paradoxical subreg
3734 : : equivalent to a mem. */
3735 : 18571394 : if (MEM_P (src) && reg_equiv[regno].pdx_subregs)
3736 : : {
3737 : 16665 : note_pattern_stores (set, no_equiv, NULL);
3738 : 16665 : continue;
3739 : : }
3740 : :
3741 : 18554729 : note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
3742 : :
3743 : : /* cse sometimes generates function invariants, but doesn't put a
3744 : : REG_EQUAL note on the insn. Since this note would be redundant,
3745 : : there's no point creating it earlier than here. */
3746 : 18554729 : if (! note && ! rtx_varies_p (src, 0))
3747 : 2777537 : note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
3748 : :
3749 : : /* Don't bother considering a REG_EQUAL note containing an EXPR_LIST
3750 : : since it represents a function call. */
3751 : 18554729 : if (note && GET_CODE (XEXP (note, 0)) == EXPR_LIST)
3752 : 14730521 : note = NULL_RTX;
3753 : :
3754 : 18554729 : if (DF_REG_DEF_COUNT (regno) != 1)
3755 : : {
3756 : 3000508 : bool equal_p = true;
3757 : 3000508 : rtx_insn_list *list;
3758 : :
3759 : : /* If we have already processed this pseudo and determined it
3760 : : cannot have an equivalence, then honor that decision. */
3761 : 3000508 : if (reg_equiv[regno].no_equiv)
3762 : 0 : continue;
3763 : :
3764 : 4835487 : if (! note
3765 : 1197656 : || rtx_varies_p (XEXP (note, 0), 0)
3766 : 4166041 : || (reg_equiv[regno].replacement
3767 : 4 : && ! rtx_equal_p (XEXP (note, 0),
3768 : : reg_equiv[regno].replacement)))
3769 : : {
3770 : 1834979 : no_equiv (dest, set, NULL);
3771 : 1834979 : continue;
3772 : : }
3773 : :
3774 : 1165529 : list = reg_equiv[regno].init_insns;
3775 : 3014043 : for (; list; list = list->next ())
3776 : : {
3777 : 1943381 : rtx note_tmp;
3778 : 1943381 : rtx_insn *insn_tmp;
3779 : :
3780 : 1943381 : insn_tmp = list->insn ();
3781 : 1943381 : note_tmp = find_reg_note (insn_tmp, REG_EQUAL, NULL_RTX);
3782 : 1943381 : gcc_assert (note_tmp);
3783 : 1943381 : if (! rtx_equal_p (XEXP (note, 0), XEXP (note_tmp, 0)))
3784 : : {
3785 : : equal_p = false;
3786 : : break;
3787 : : }
3788 : : }
3789 : :
3790 : 1165529 : if (! equal_p)
3791 : : {
3792 : 94867 : no_equiv (dest, set, NULL);
3793 : 94867 : continue;
3794 : : }
3795 : : }
3796 : :
3797 : : /* Record this insn as initializing this register. */
3798 : 16624883 : reg_equiv[regno].init_insns
3799 : 16624883 : = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv[regno].init_insns);
3800 : :
3801 : : /* If this register is known to be equal to a constant, record that
3802 : : it is always equivalent to the constant.
3803 : : Note that it is possible to have a register use before
3804 : : the def in loops (see gcc.c-torture/execute/pr79286.c)
3805 : : where the reg is undefined on first use. If the def insn
3806 : : won't trap we can use it as an equivalence, effectively
3807 : : choosing the "undefined" value for the reg to be the
3808 : : same as the value set by the def. */
3809 : 16624883 : if (DF_REG_DEF_COUNT (regno) == 1
3810 : 15554221 : && note
3811 : 2626552 : && !rtx_varies_p (XEXP (note, 0), 0)
3812 : 18833347 : && (!may_trap_or_fault_p (XEXP (note, 0))
3813 : 48721 : || def_dominates_uses (regno)))
3814 : : {
3815 : 2208463 : rtx note_value = XEXP (note, 0);
3816 : 2208463 : remove_note (insn, note);
3817 : 2208463 : set_unique_reg_note (insn, REG_EQUIV, note_value);
3818 : : }
3819 : :
3820 : : /* If this insn introduces a "constant" register, decrease the priority
3821 : : of that register. Record this insn if the register is only used once
3822 : : more and the equivalence value is the same as our source.
3823 : :
3824 : : The latter condition is checked for two reasons: First, it is an
3825 : : indication that it may be more efficient to actually emit the insn
3826 : : as written (if no registers are available, reload will substitute
3827 : : the equivalence). Secondly, it avoids problems with any registers
3828 : : dying in this insn whose death notes would be missed.
3829 : :
3830 : : If we don't have a REG_EQUIV note, see if this insn is loading
3831 : : a register used only in one basic block from a MEM. If so, and the
3832 : : MEM remains unchanged for the life of the register, add a REG_EQUIV
3833 : : note. */
3834 : 16624883 : note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
3835 : :
3836 : 16624883 : rtx replacement = NULL_RTX;
3837 : 16624883 : if (note)
3838 : 2430515 : replacement = XEXP (note, 0);
3839 : 14194368 : else if (REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
3840 : 10178935 : && MEM_P (SET_SRC (set)))
3841 : : {
3842 : 2990487 : enum valid_equiv validity;
3843 : 2990487 : validity = validate_equiv_mem (insn, dest, SET_SRC (set));
3844 : 2990487 : if (validity != valid_none)
3845 : : {
3846 : 2237545 : replacement = copy_rtx (SET_SRC (set));
3847 : 2237545 : if (validity == valid_reload)
3848 : : {
3849 : 2236697 : note = set_unique_reg_note (insn, REG_EQUIV, replacement);
3850 : : }
3851 : 848 : else if (ira_use_lra_p)
3852 : : {
3853 : : /* We still can use this equivalence for caller save
3854 : : optimization in LRA. Mark this. */
3855 : 848 : ira_reg_equiv[regno].caller_save_p = true;
3856 : 848 : ira_reg_equiv[regno].init_insns
3857 : 848 : = gen_rtx_INSN_LIST (VOIDmode, insn,
3858 : 848 : ira_reg_equiv[regno].init_insns);
3859 : : }
3860 : : }
3861 : : }
3862 : :
3863 : : /* If we haven't done so, record for reload that this is an
3864 : : equivalencing insn. */
3865 : 16624883 : if (note && !reg_equiv[regno].is_arg_equivalence)
3866 : 4445160 : ira_reg_equiv[regno].init_insns
3867 : 4445160 : = gen_rtx_INSN_LIST (VOIDmode, insn,
3868 : 4445160 : ira_reg_equiv[regno].init_insns);
3869 : :
3870 : 16624883 : if (replacement)
3871 : : {
3872 : 4668060 : reg_equiv[regno].replacement = replacement;
3873 : 4668060 : reg_equiv[regno].src_p = &SET_SRC (set);
3874 : 4668060 : reg_equiv[regno].loop_depth = (short) loop_depth;
3875 : :
3876 : : /* Don't mess with things live during setjmp. */
3877 : 4668060 : if (optimize && !bitmap_bit_p (setjmp_crosses, regno))
3878 : : {
3879 : : /* If the register is referenced exactly twice, meaning it is
3880 : : set once and used once, indicate that the reference may be
3881 : : replaced by the equivalence we computed above. Do this
3882 : : even if the register is only used in one block so that
3883 : : dependencies can be handled where the last register is
3884 : : used in a different block (i.e. HIGH / LO_SUM sequences)
3885 : : and to reduce the number of registers alive across
3886 : : calls. */
3887 : :
3888 : 4668019 : if (REG_N_REFS (regno) == 2
3889 : 3704670 : && (rtx_equal_p (replacement, src)
3890 : 405131 : || ! equiv_init_varies_p (src))
3891 : 3640401 : && NONJUMP_INSN_P (insn)
3892 : 8308420 : && equiv_init_movable_p (PATTERN (insn), regno))
3893 : 2203828 : reg_equiv[regno].replace = 1;
3894 : : }
3895 : : }
3896 : : }
3897 : : }
3898 : 1481332 : }
3899 : :
3900 : : /* For insns that set a MEM to the contents of a REG that is only used
3901 : : in a single basic block, see if the register is always equivalent
3902 : : to that memory location and if moving the store from INSN to the
3903 : : insn that sets REG is safe. If so, put a REG_EQUIV note on the
3904 : : initializing insn. */
3905 : : static void
3906 : 966055 : add_store_equivs (void)
3907 : : {
3908 : 966055 : auto_sbitmap seen_insns (get_max_uid () + 1);
3909 : 966055 : bitmap_clear (seen_insns);
3910 : :
3911 : 130102425 : for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
3912 : : {
3913 : 129136370 : rtx set, src, dest;
3914 : 129136370 : unsigned regno;
3915 : 129136370 : rtx_insn *init_insn;
3916 : :
3917 : 129136370 : bitmap_set_bit (seen_insns, INSN_UID (insn));
3918 : :
3919 : 129136370 : if (! INSN_P (insn))
3920 : 24161655 : continue;
3921 : :
3922 : 104974715 : set = single_set (insn);
3923 : 104974715 : if (! set)
3924 : 53039103 : continue;
3925 : :
3926 : 51935612 : dest = SET_DEST (set);
3927 : 51935612 : src = SET_SRC (set);
3928 : :
3929 : : /* Don't add a REG_EQUIV note if the insn already has one. The existing
3930 : : REG_EQUIV is likely more useful than the one we are adding. */
3931 : 7738695 : if (MEM_P (dest) && REG_P (src)
3932 : 5170456 : && (regno = REGNO (src)) >= FIRST_PSEUDO_REGISTER
3933 : 5105213 : && REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
3934 : 2922611 : && DF_REG_DEF_COUNT (regno) == 1
3935 : 2856647 : && ! reg_equiv[regno].pdx_subregs
3936 : 2712865 : && reg_equiv[regno].init_insns != NULL
3937 : 2712865 : && (init_insn = reg_equiv[regno].init_insns->insn ()) != 0
3938 : 2690206 : && bitmap_bit_p (seen_insns, INSN_UID (init_insn))
3939 : 2690206 : && ! find_reg_note (init_insn, REG_EQUIV, NULL_RTX)
3940 : 1047572 : && validate_equiv_mem (init_insn, src, dest) == valid_reload
3941 : 616581 : && ! memref_used_between_p (dest, init_insn, insn)
3942 : : /* Attaching a REG_EQUIV note will fail if INIT_INSN has
3943 : : multiple sets. */
3944 : 52458439 : && set_unique_reg_note (init_insn, REG_EQUIV, copy_rtx (dest)))
3945 : : {
3946 : : /* This insn makes the equivalence, not the one initializing
3947 : : the register. */
3948 : 522341 : ira_reg_equiv[regno].init_insns
3949 : 522341 : = gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
3950 : 522341 : df_notes_rescan (init_insn);
3951 : 522341 : if (dump_file)
3952 : 88 : fprintf (dump_file,
3953 : : "Adding REG_EQUIV to insn %d for source of insn %d\n",
3954 : 88 : INSN_UID (init_insn),
3955 : 88 : INSN_UID (insn));
3956 : : }
3957 : : }
3958 : 966055 : }
3959 : :
3960 : : /* Scan all regs killed in an insn to see if any of them are registers
3961 : : only used that once. If so, see if we can replace the reference
3962 : : with the equivalent form. If we can, delete the initializing
3963 : : reference and this register will go away. If we can't replace the
3964 : : reference, and the initializing reference is within the same loop
3965 : : (or in an inner loop), then move the register initialization just
3966 : : before the use, so that they are in the same basic block. */
3967 : : static void
3968 : 1044923 : combine_and_move_insns (void)
3969 : : {
3970 : 1044923 : auto_bitmap cleared_regs;
3971 : 1044923 : int max = max_reg_num ();
3972 : :
3973 : 51914401 : for (int regno = FIRST_PSEUDO_REGISTER; regno < max; regno++)
3974 : : {
3975 : 50869478 : if (!reg_equiv[regno].replace)
3976 : 48665849 : continue;
3977 : :
3978 : 2203629 : rtx_insn *use_insn = 0;
3979 : 2203629 : for (df_ref use = DF_REG_USE_CHAIN (regno);
3980 : 4428161 : use;
3981 : 2224532 : use = DF_REF_NEXT_REG (use))
3982 : 2224532 : if (DF_REF_INSN_INFO (use))
3983 : : {
3984 : 2224532 : if (DEBUG_INSN_P (DF_REF_INSN (use)))
3985 : 20903 : continue;
3986 : 2203629 : gcc_assert (!use_insn);
3987 : : use_insn = DF_REF_INSN (use);
3988 : : }
3989 : 2203629 : gcc_assert (use_insn);
3990 : :
3991 : : /* Don't substitute into jumps. indirect_jump_optimize does
3992 : : this for anything we are prepared to handle. */
3993 : 2203629 : if (JUMP_P (use_insn))
3994 : 394 : continue;
3995 : :
3996 : : /* Also don't substitute into a conditional trap insn -- it can become
3997 : : an unconditional trap, and that is a flow control insn. */
3998 : 2203235 : if (GET_CODE (PATTERN (use_insn)) == TRAP_IF)
3999 : 0 : continue;
4000 : :
4001 : 2203235 : df_ref def = DF_REG_DEF_CHAIN (regno);
4002 : 2203235 : gcc_assert (DF_REG_DEF_COUNT (regno) == 1 && DF_REF_INSN_INFO (def));
4003 : 2203235 : rtx_insn *def_insn = DF_REF_INSN (def);
4004 : :
4005 : : /* We may not move instructions that can throw, since that
4006 : : changes basic block boundaries and we are not prepared to
4007 : : adjust the CFG to match. */
4008 : 2203235 : if (can_throw_internal (def_insn))
4009 : 0 : continue;
4010 : :
4011 : : /* Instructions with multiple sets can only be moved if DF analysis is
4012 : : performed for all of the registers set. See PR91052. */
4013 : 2203235 : if (multiple_sets (def_insn))
4014 : 0 : continue;
4015 : :
4016 : 2203235 : basic_block use_bb = BLOCK_FOR_INSN (use_insn);
4017 : 2203235 : basic_block def_bb = BLOCK_FOR_INSN (def_insn);
4018 : 2203235 : if (bb_loop_depth (use_bb) > bb_loop_depth (def_bb))
4019 : 135944 : continue;
4020 : :
4021 : 2067291 : if (asm_noperands (PATTERN (def_insn)) < 0
4022 : 4134582 : && validate_replace_rtx (regno_reg_rtx[regno],
4023 : 2067291 : *reg_equiv[regno].src_p, use_insn))
4024 : : {
4025 : 390417 : rtx link;
4026 : : /* Append the REG_DEAD notes from def_insn. */
4027 : 781711 : for (rtx *p = ®_NOTES (def_insn); (link = *p) != 0; )
4028 : : {
4029 : 391294 : if (REG_NOTE_KIND (XEXP (link, 0)) == REG_DEAD)
4030 : : {
4031 : 0 : *p = XEXP (link, 1);
4032 : 0 : XEXP (link, 1) = REG_NOTES (use_insn);
4033 : 0 : REG_NOTES (use_insn) = link;
4034 : : }
4035 : : else
4036 : 391294 : p = &XEXP (link, 1);
4037 : : }
4038 : :
4039 : 390417 : remove_death (regno, use_insn);
4040 : 390417 : SET_REG_N_REFS (regno, 0);
4041 : 390417 : REG_FREQ (regno) = 0;
4042 : 390417 : df_ref use;
4043 : 480985 : FOR_EACH_INSN_USE (use, def_insn)
4044 : : {
4045 : 90568 : unsigned int use_regno = DF_REF_REGNO (use);
4046 : 90568 : if (!HARD_REGISTER_NUM_P (use_regno))
4047 : 1282 : reg_equiv[use_regno].replace = 0;
4048 : : }
4049 : :
4050 : 390417 : delete_insn (def_insn);
4051 : :
4052 : 390417 : reg_equiv[regno].init_insns = NULL;
4053 : 390417 : ira_reg_equiv[regno].init_insns = NULL;
4054 : 390417 : bitmap_set_bit (cleared_regs, regno);
4055 : : }
4056 : :
4057 : : /* Move the initialization of the register to just before
4058 : : USE_INSN. Update the flow information. */
4059 : 1676874 : else if (prev_nondebug_insn (use_insn) != def_insn)
4060 : : {
4061 : 319142 : rtx_insn *new_insn;
4062 : :
4063 : 319142 : new_insn = emit_insn_before (PATTERN (def_insn), use_insn);
4064 : 319142 : REG_NOTES (new_insn) = REG_NOTES (def_insn);
4065 : 319142 : REG_NOTES (def_insn) = 0;
4066 : : /* Rescan it to process the notes. */
4067 : 319142 : df_insn_rescan (new_insn);
4068 : :
4069 : : /* Make sure this insn is recognized before reload begins,
4070 : : otherwise eliminate_regs_in_insn will die. */
4071 : 319142 : INSN_CODE (new_insn) = INSN_CODE (def_insn);
4072 : :
4073 : 319142 : delete_insn (def_insn);
4074 : :
4075 : 319142 : XEXP (reg_equiv[regno].init_insns, 0) = new_insn;
4076 : :
4077 : 319142 : REG_BASIC_BLOCK (regno) = use_bb->index;
4078 : 319142 : REG_N_CALLS_CROSSED (regno) = 0;
4079 : :
4080 : 319142 : if (use_insn == BB_HEAD (use_bb))
4081 : 0 : BB_HEAD (use_bb) = new_insn;
4082 : :
4083 : : /* We know regno dies in use_insn, but inside a loop
4084 : : REG_DEAD notes might be missing when def_insn was in
4085 : : another basic block. However, when we move def_insn into
4086 : : this bb we'll definitely get a REG_DEAD note and reload
4087 : : will see the death. It's possible that update_equiv_regs
4088 : : set up an equivalence referencing regno for a reg set by
4089 : : use_insn, when regno was seen as non-local. Now that
4090 : : regno is local to this block, and dies, such an
4091 : : equivalence is invalid. */
4092 : 319142 : if (find_reg_note (use_insn, REG_EQUIV, regno_reg_rtx[regno]))
4093 : : {
4094 : 0 : rtx set = single_set (use_insn);
4095 : 0 : if (set && REG_P (SET_DEST (set)))
4096 : 0 : no_equiv (SET_DEST (set), set, NULL);
4097 : : }
4098 : :
4099 : 319142 : ira_reg_equiv[regno].init_insns
4100 : 319142 : = gen_rtx_INSN_LIST (VOIDmode, new_insn, NULL_RTX);
4101 : 319142 : bitmap_set_bit (cleared_regs, regno);
4102 : : }
4103 : : }
4104 : :
4105 : 1044923 : if (!bitmap_empty_p (cleared_regs))
4106 : : {
4107 : 220775 : basic_block bb;
4108 : :
4109 : 5904116 : FOR_EACH_BB_FN (bb, cfun)
4110 : : {
4111 : 11366682 : bitmap_and_compl_into (DF_LR_IN (bb), cleared_regs);
4112 : 11366682 : bitmap_and_compl_into (DF_LR_OUT (bb), cleared_regs);
4113 : 5683341 : if (!df_live)
4114 : 5683341 : continue;
4115 : 0 : bitmap_and_compl_into (DF_LIVE_IN (bb), cleared_regs);
4116 : 0 : bitmap_and_compl_into (DF_LIVE_OUT (bb), cleared_regs);
4117 : : }
4118 : :
4119 : : /* Last pass - adjust debug insns referencing cleared regs. */
4120 : 220775 : if (MAY_HAVE_DEBUG_BIND_INSNS)
4121 : 62572183 : for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
4122 : 62448770 : if (DEBUG_BIND_INSN_P (insn))
4123 : : {
4124 : 22724606 : rtx old_loc = INSN_VAR_LOCATION_LOC (insn);
4125 : 22724606 : INSN_VAR_LOCATION_LOC (insn)
4126 : 45449212 : = simplify_replace_fn_rtx (old_loc, NULL_RTX,
4127 : : adjust_cleared_regs,
4128 : 22724606 : (void *) cleared_regs);
4129 : 22724606 : if (old_loc != INSN_VAR_LOCATION_LOC (insn))
4130 : 17498 : df_insn_rescan (insn);
4131 : : }
4132 : : }
4133 : 1044923 : }
4134 : :
4135 : : /* A pass over indirect jumps, converting simple cases to direct jumps.
4136 : : Combine does this optimization too, but only within a basic block. */
4137 : : static void
4138 : 1481332 : indirect_jump_optimize (void)
4139 : : {
4140 : 1481332 : basic_block bb;
4141 : 1481332 : bool rebuild_p = false;
4142 : :
4143 : 16080292 : FOR_EACH_BB_REVERSE_FN (bb, cfun)
4144 : : {
4145 : 14598960 : rtx_insn *insn = BB_END (bb);
4146 : 20307319 : if (!JUMP_P (insn)
4147 : 14598960 : || find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
4148 : 5708359 : continue;
4149 : :
4150 : 8890601 : rtx x = pc_set (insn);
4151 : 8890601 : if (!x || !REG_P (SET_SRC (x)))
4152 : 8889401 : continue;
4153 : :
4154 : 1200 : int regno = REGNO (SET_SRC (x));
4155 : 1200 : if (DF_REG_DEF_COUNT (regno) == 1)
4156 : : {
4157 : 1089 : df_ref def = DF_REG_DEF_CHAIN (regno);
4158 : 1089 : if (!DF_REF_IS_ARTIFICIAL (def))
4159 : : {
4160 : 1089 : rtx_insn *def_insn = DF_REF_INSN (def);
4161 : 1089 : rtx lab = NULL_RTX;
4162 : 1089 : rtx set = single_set (def_insn);
4163 : 1089 : if (set && GET_CODE (SET_SRC (set)) == LABEL_REF)
4164 : : lab = SET_SRC (set);
4165 : : else
4166 : : {
4167 : 1088 : rtx eqnote = find_reg_note (def_insn, REG_EQUAL, NULL_RTX);
4168 : 1088 : if (eqnote && GET_CODE (XEXP (eqnote, 0)) == LABEL_REF)
4169 : : lab = XEXP (eqnote, 0);
4170 : : }
4171 : 1 : if (lab && validate_replace_rtx (SET_SRC (x), lab, insn))
4172 : : rebuild_p = true;
4173 : : }
4174 : : }
4175 : : }
4176 : :
4177 : 1481332 : if (rebuild_p)
4178 : : {
4179 : 1 : timevar_push (TV_JUMP);
4180 : 1 : rebuild_jump_labels (get_insns ());
4181 : 1 : if (purge_all_dead_edges ())
4182 : 1 : delete_unreachable_blocks ();
4183 : 1 : timevar_pop (TV_JUMP);
4184 : : }
4185 : 1481332 : }
4186 : :
4187 : : /* Set up fields memory, constant, and invariant from init_insns in
4188 : : the structures of array ira_reg_equiv. */
4189 : : static void
4190 : 1481332 : setup_reg_equiv (void)
4191 : : {
4192 : 1481332 : int i;
4193 : 1481332 : rtx_insn_list *elem, *prev_elem, *next_elem;
4194 : 1481332 : rtx_insn *insn;
4195 : 1481332 : rtx set, x;
4196 : :
4197 : 169181836 : for (i = FIRST_PSEUDO_REGISTER; i < ira_reg_equiv_len; i++)
4198 : 167700504 : for (prev_elem = NULL, elem = ira_reg_equiv[i].init_insns;
4199 : 172382948 : elem;
4200 : : prev_elem = elem, elem = next_elem)
4201 : : {
4202 : 4805081 : next_elem = elem->next ();
4203 : 4805081 : insn = elem->insn ();
4204 : 4805081 : set = single_set (insn);
4205 : :
4206 : : /* Init insns can set up equivalence when the reg is a destination or
4207 : : a source (in this case the destination is memory). */
4208 : 4805081 : if (set != 0 && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))))
4209 : : {
4210 : 4805081 : if ((x = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != NULL)
4211 : : {
4212 : 4281983 : x = XEXP (x, 0);
4213 : 4281983 : if (REG_P (SET_DEST (set))
4214 : 4281983 : && REGNO (SET_DEST (set)) == (unsigned int) i
4215 : 8563966 : && ! rtx_equal_p (SET_SRC (set), x) && MEM_P (x))
4216 : : {
4217 : : /* This insn reporting the equivalence but
4218 : : actually not setting it. Remove it from the
4219 : : list. */
4220 : 30376 : if (prev_elem == NULL)
4221 : 30376 : ira_reg_equiv[i].init_insns = next_elem;
4222 : : else
4223 : 0 : XEXP (prev_elem, 1) = next_elem;
4224 : : elem = prev_elem;
4225 : : }
4226 : : }
4227 : 523098 : else if (REG_P (SET_DEST (set))
4228 : 523098 : && REGNO (SET_DEST (set)) == (unsigned int) i)
4229 : 757 : x = SET_SRC (set);
4230 : : else
4231 : : {
4232 : 522341 : gcc_assert (REG_P (SET_SRC (set))
4233 : : && REGNO (SET_SRC (set)) == (unsigned int) i);
4234 : : x = SET_DEST (set);
4235 : : }
4236 : : /* If PIC is enabled and the equiv is not a LEGITIMATE_PIC_OPERAND,
4237 : : we can't use it. */
4238 : 4805081 : if (! CONSTANT_P (x)
4239 : 999400 : || ! flag_pic
4240 : : /* A function invariant is often CONSTANT_P but may
4241 : : include a register. We promise to only pass
4242 : : CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
4243 : 4924623 : || LEGITIMATE_PIC_OPERAND_P (x))
4244 : : {
4245 : : /* It can happen that a REG_EQUIV note contains a MEM
4246 : : that is not a legitimate memory operand. As later
4247 : : stages of reload assume that all addresses found in
4248 : : the lra_regno_equiv_* arrays were originally
4249 : : legitimate, we ignore such REG_EQUIV notes. */
4250 : 4761451 : if (memory_operand (x, VOIDmode))
4251 : : {
4252 : 2832143 : ira_reg_equiv[i].defined_p = !ira_reg_equiv[i].caller_save_p;
4253 : 2832143 : ira_reg_equiv[i].memory = x;
4254 : 2832143 : continue;
4255 : : }
4256 : 1929308 : else if (function_invariant_p (x))
4257 : : {
4258 : 1850797 : machine_mode mode;
4259 : :
4260 : 1850797 : mode = GET_MODE (SET_DEST (set));
4261 : 1850797 : if (GET_CODE (x) == PLUS
4262 : 956885 : || x == frame_pointer_rtx || x == arg_pointer_rtx)
4263 : : /* This is PLUS of frame pointer and a constant,
4264 : : or fp, or argp. */
4265 : 895027 : ira_reg_equiv[i].invariant = x;
4266 : 955770 : else if (targetm.legitimate_constant_p (mode, x))
4267 : 688005 : ira_reg_equiv[i].constant = x;
4268 : : else
4269 : : {
4270 : 267765 : ira_reg_equiv[i].memory = force_const_mem (mode, x);
4271 : 267765 : if (ira_reg_equiv[i].memory == NULL_RTX)
4272 : : {
4273 : 496 : ira_reg_equiv[i].defined_p = false;
4274 : 496 : ira_reg_equiv[i].caller_save_p = false;
4275 : 496 : ira_reg_equiv[i].init_insns = NULL;
4276 : 496 : break;
4277 : : }
4278 : : }
4279 : 1850301 : ira_reg_equiv[i].defined_p = true;
4280 : 1850301 : continue;
4281 : 1850301 : }
4282 : : }
4283 : : }
4284 : 122141 : ira_reg_equiv[i].defined_p = false;
4285 : 122141 : ira_reg_equiv[i].caller_save_p = false;
4286 : 122141 : ira_reg_equiv[i].init_insns = NULL;
4287 : 122141 : break;
4288 : : }
4289 : 1481332 : }
4290 : :
4291 : :
4292 : :
4293 : : /* Print chain C to FILE. */
4294 : : static void
4295 : 0 : print_insn_chain (FILE *file, class insn_chain *c)
4296 : : {
4297 : 0 : fprintf (file, "insn=%d, ", INSN_UID (c->insn));
4298 : 0 : bitmap_print (file, &c->live_throughout, "live_throughout: ", ", ");
4299 : 0 : bitmap_print (file, &c->dead_or_set, "dead_or_set: ", "\n");
4300 : 0 : }
4301 : :
4302 : :
4303 : : /* Print all reload_insn_chains to FILE. */
4304 : : static void
4305 : 0 : print_insn_chains (FILE *file)
4306 : : {
4307 : 0 : class insn_chain *c;
4308 : 0 : for (c = reload_insn_chain; c ; c = c->next)
4309 : 0 : print_insn_chain (file, c);
4310 : 0 : }
4311 : :
4312 : : /* Return true if pseudo REGNO should be added to set live_throughout
4313 : : or dead_or_set of the insn chains for reload consideration. */
4314 : : static bool
4315 : 0 : pseudo_for_reload_consideration_p (int regno)
4316 : : {
4317 : : /* Consider spilled pseudos too for IRA because they still have a
4318 : : chance to get hard-registers in the reload when IRA is used. */
4319 : 0 : return (reg_renumber[regno] >= 0 || ira_conflicts_p);
4320 : : }
4321 : :
4322 : : /* Return true if we can track the individual bytes of subreg X.
4323 : : When returning true, set *OUTER_SIZE to the number of bytes in
4324 : : X itself, *INNER_SIZE to the number of bytes in the inner register
4325 : : and *START to the offset of the first byte. */
4326 : : static bool
4327 : 0 : get_subreg_tracking_sizes (rtx x, HOST_WIDE_INT *outer_size,
4328 : : HOST_WIDE_INT *inner_size, HOST_WIDE_INT *start)
4329 : : {
4330 : 0 : rtx reg = regno_reg_rtx[REGNO (SUBREG_REG (x))];
4331 : 0 : return (GET_MODE_SIZE (GET_MODE (x)).is_constant (outer_size)
4332 : 0 : && GET_MODE_SIZE (GET_MODE (reg)).is_constant (inner_size)
4333 : 0 : && SUBREG_BYTE (x).is_constant (start));
4334 : : }
4335 : :
4336 : : /* Init LIVE_SUBREGS[ALLOCNUM] and LIVE_SUBREGS_USED[ALLOCNUM] for
4337 : : a register with SIZE bytes, making the register live if INIT_VALUE. */
4338 : : static void
4339 : 0 : init_live_subregs (bool init_value, sbitmap *live_subregs,
4340 : : bitmap live_subregs_used, int allocnum, int size)
4341 : : {
4342 : 0 : gcc_assert (size > 0);
4343 : :
4344 : : /* Been there, done that. */
4345 : 0 : if (bitmap_bit_p (live_subregs_used, allocnum))
4346 : : return;
4347 : :
4348 : : /* Create a new one. */
4349 : 0 : if (live_subregs[allocnum] == NULL)
4350 : 0 : live_subregs[allocnum] = sbitmap_alloc (size);
4351 : :
4352 : : /* If the entire reg was live before blasting into subregs, we need
4353 : : to init all of the subregs to ones else init to 0. */
4354 : 0 : if (init_value)
4355 : 0 : bitmap_ones (live_subregs[allocnum]);
4356 : : else
4357 : 0 : bitmap_clear (live_subregs[allocnum]);
4358 : :
4359 : 0 : bitmap_set_bit (live_subregs_used, allocnum);
4360 : : }
4361 : :
4362 : : /* Walk the insns of the current function and build reload_insn_chain,
4363 : : and record register life information. */
4364 : : static void
4365 : 0 : build_insn_chain (void)
4366 : : {
4367 : 0 : unsigned int i;
4368 : 0 : class insn_chain **p = &reload_insn_chain;
4369 : 0 : basic_block bb;
4370 : 0 : class insn_chain *c = NULL;
4371 : 0 : class insn_chain *next = NULL;
4372 : 0 : auto_bitmap live_relevant_regs;
4373 : 0 : auto_bitmap elim_regset;
4374 : : /* live_subregs is a vector used to keep accurate information about
4375 : : which hardregs are live in multiword pseudos. live_subregs and
4376 : : live_subregs_used are indexed by pseudo number. The live_subreg
4377 : : entry for a particular pseudo is only used if the corresponding
4378 : : element is non zero in live_subregs_used. The sbitmap size of
4379 : : live_subreg[allocno] is number of bytes that the pseudo can
4380 : : occupy. */
4381 : 0 : sbitmap *live_subregs = XCNEWVEC (sbitmap, max_regno);
4382 : 0 : auto_bitmap live_subregs_used;
4383 : :
4384 : 0 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4385 : 0 : if (TEST_HARD_REG_BIT (eliminable_regset, i))
4386 : 0 : bitmap_set_bit (elim_regset, i);
4387 : 0 : FOR_EACH_BB_REVERSE_FN (bb, cfun)
4388 : : {
4389 : 0 : bitmap_iterator bi;
4390 : 0 : rtx_insn *insn;
4391 : :
4392 : 0 : CLEAR_REG_SET (live_relevant_regs);
4393 : 0 : bitmap_clear (live_subregs_used);
4394 : :
4395 : 0 : EXECUTE_IF_SET_IN_BITMAP (df_get_live_out (bb), 0, i, bi)
4396 : : {
4397 : 0 : if (i >= FIRST_PSEUDO_REGISTER)
4398 : : break;
4399 : 0 : bitmap_set_bit (live_relevant_regs, i);
4400 : : }
4401 : :
4402 : 0 : EXECUTE_IF_SET_IN_BITMAP (df_get_live_out (bb),
4403 : : FIRST_PSEUDO_REGISTER, i, bi)
4404 : : {
4405 : 0 : if (pseudo_for_reload_consideration_p (i))
4406 : 0 : bitmap_set_bit (live_relevant_regs, i);
4407 : : }
4408 : :
4409 : 0 : FOR_BB_INSNS_REVERSE (bb, insn)
4410 : : {
4411 : 0 : if (!NOTE_P (insn) && !BARRIER_P (insn))
4412 : : {
4413 : 0 : struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4414 : 0 : df_ref def, use;
4415 : :
4416 : 0 : c = new_insn_chain ();
4417 : 0 : c->next = next;
4418 : 0 : next = c;
4419 : 0 : *p = c;
4420 : 0 : p = &c->prev;
4421 : :
4422 : 0 : c->insn = insn;
4423 : 0 : c->block = bb->index;
4424 : :
4425 : 0 : if (NONDEBUG_INSN_P (insn))
4426 : 0 : FOR_EACH_INSN_INFO_DEF (def, insn_info)
4427 : : {
4428 : 0 : unsigned int regno = DF_REF_REGNO (def);
4429 : :
4430 : : /* Ignore may clobbers because these are generated
4431 : : from calls. However, every other kind of def is
4432 : : added to dead_or_set. */
4433 : 0 : if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
4434 : : {
4435 : 0 : if (regno < FIRST_PSEUDO_REGISTER)
4436 : : {
4437 : 0 : if (!fixed_regs[regno])
4438 : 0 : bitmap_set_bit (&c->dead_or_set, regno);
4439 : : }
4440 : 0 : else if (pseudo_for_reload_consideration_p (regno))
4441 : 0 : bitmap_set_bit (&c->dead_or_set, regno);
4442 : : }
4443 : :
4444 : 0 : if ((regno < FIRST_PSEUDO_REGISTER
4445 : 0 : || reg_renumber[regno] >= 0
4446 : 0 : || ira_conflicts_p)
4447 : 0 : && (!DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL)))
4448 : : {
4449 : 0 : rtx reg = DF_REF_REG (def);
4450 : 0 : HOST_WIDE_INT outer_size, inner_size, start;
4451 : :
4452 : : /* We can usually track the liveness of individual
4453 : : bytes within a subreg. The only exceptions are
4454 : : subregs wrapped in ZERO_EXTRACTs and subregs whose
4455 : : size is not known; in those cases we need to be
4456 : : conservative and treat the definition as a partial
4457 : : definition of the full register rather than a full
4458 : : definition of a specific part of the register. */
4459 : 0 : if (GET_CODE (reg) == SUBREG
4460 : 0 : && !DF_REF_FLAGS_IS_SET (def, DF_REF_ZERO_EXTRACT)
4461 : 0 : && get_subreg_tracking_sizes (reg, &outer_size,
4462 : : &inner_size, &start))
4463 : : {
4464 : 0 : HOST_WIDE_INT last = start + outer_size;
4465 : :
4466 : 0 : init_live_subregs
4467 : 0 : (bitmap_bit_p (live_relevant_regs, regno),
4468 : : live_subregs, live_subregs_used, regno,
4469 : : inner_size);
4470 : :
4471 : 0 : if (!DF_REF_FLAGS_IS_SET
4472 : : (def, DF_REF_STRICT_LOW_PART))
4473 : : {
4474 : : /* Expand the range to cover entire words.
4475 : : Bytes added here are "don't care". */
4476 : 0 : start
4477 : 0 : = start / UNITS_PER_WORD * UNITS_PER_WORD;
4478 : 0 : last = ((last + UNITS_PER_WORD - 1)
4479 : 0 : / UNITS_PER_WORD * UNITS_PER_WORD);
4480 : : }
4481 : :
4482 : : /* Ignore the paradoxical bits. */
4483 : 0 : if (last > SBITMAP_SIZE (live_subregs[regno]))
4484 : : last = SBITMAP_SIZE (live_subregs[regno]);
4485 : :
4486 : 0 : while (start < last)
4487 : : {
4488 : 0 : bitmap_clear_bit (live_subregs[regno], start);
4489 : 0 : start++;
4490 : : }
4491 : :
4492 : 0 : if (bitmap_empty_p (live_subregs[regno]))
4493 : : {
4494 : 0 : bitmap_clear_bit (live_subregs_used, regno);
4495 : 0 : bitmap_clear_bit (live_relevant_regs, regno);
4496 : : }
4497 : : else
4498 : : /* Set live_relevant_regs here because
4499 : : that bit has to be true to get us to
4500 : : look at the live_subregs fields. */
4501 : 0 : bitmap_set_bit (live_relevant_regs, regno);
4502 : : }
4503 : : else
4504 : : {
4505 : : /* DF_REF_PARTIAL is generated for
4506 : : subregs, STRICT_LOW_PART, and
4507 : : ZERO_EXTRACT. We handle the subreg
4508 : : case above so here we have to keep from
4509 : : modeling the def as a killing def. */
4510 : 0 : if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL))
4511 : : {
4512 : 0 : bitmap_clear_bit (live_subregs_used, regno);
4513 : 0 : bitmap_clear_bit (live_relevant_regs, regno);
4514 : : }
4515 : : }
4516 : : }
4517 : : }
4518 : :
4519 : 0 : bitmap_and_compl_into (live_relevant_regs, elim_regset);
4520 : 0 : bitmap_copy (&c->live_throughout, live_relevant_regs);
4521 : :
4522 : 0 : if (NONDEBUG_INSN_P (insn))
4523 : 0 : FOR_EACH_INSN_INFO_USE (use, insn_info)
4524 : : {
4525 : 0 : unsigned int regno = DF_REF_REGNO (use);
4526 : 0 : rtx reg = DF_REF_REG (use);
4527 : :
4528 : : /* DF_REF_READ_WRITE on a use means that this use
4529 : : is fabricated from a def that is a partial set
4530 : : to a multiword reg. Here, we only model the
4531 : : subreg case that is not wrapped in ZERO_EXTRACT
4532 : : precisely so we do not need to look at the
4533 : : fabricated use. */
4534 : 0 : if (DF_REF_FLAGS_IS_SET (use, DF_REF_READ_WRITE)
4535 : 0 : && !DF_REF_FLAGS_IS_SET (use, DF_REF_ZERO_EXTRACT)
4536 : 0 : && DF_REF_FLAGS_IS_SET (use, DF_REF_SUBREG))
4537 : 0 : continue;
4538 : :
4539 : : /* Add the last use of each var to dead_or_set. */
4540 : 0 : if (!bitmap_bit_p (live_relevant_regs, regno))
4541 : : {
4542 : 0 : if (regno < FIRST_PSEUDO_REGISTER)
4543 : : {
4544 : 0 : if (!fixed_regs[regno])
4545 : 0 : bitmap_set_bit (&c->dead_or_set, regno);
4546 : : }
4547 : 0 : else if (pseudo_for_reload_consideration_p (regno))
4548 : 0 : bitmap_set_bit (&c->dead_or_set, regno);
4549 : : }
4550 : :
4551 : 0 : if (regno < FIRST_PSEUDO_REGISTER
4552 : 0 : || pseudo_for_reload_consideration_p (regno))
4553 : : {
4554 : 0 : HOST_WIDE_INT outer_size, inner_size, start;
4555 : 0 : if (GET_CODE (reg) == SUBREG
4556 : 0 : && !DF_REF_FLAGS_IS_SET (use,
4557 : : DF_REF_SIGN_EXTRACT
4558 : : | DF_REF_ZERO_EXTRACT)
4559 : 0 : && get_subreg_tracking_sizes (reg, &outer_size,
4560 : : &inner_size, &start))
4561 : : {
4562 : 0 : HOST_WIDE_INT last = start + outer_size;
4563 : :
4564 : 0 : init_live_subregs
4565 : 0 : (bitmap_bit_p (live_relevant_regs, regno),
4566 : : live_subregs, live_subregs_used, regno,
4567 : : inner_size);
4568 : :
4569 : : /* Ignore the paradoxical bits. */
4570 : 0 : if (last > SBITMAP_SIZE (live_subregs[regno]))
4571 : : last = SBITMAP_SIZE (live_subregs[regno]);
4572 : :
4573 : 0 : while (start < last)
4574 : : {
4575 : 0 : bitmap_set_bit (live_subregs[regno], start);
4576 : 0 : start++;
4577 : : }
4578 : : }
4579 : : else
4580 : : /* Resetting the live_subregs_used is
4581 : : effectively saying do not use the subregs
4582 : : because we are reading the whole
4583 : : pseudo. */
4584 : 0 : bitmap_clear_bit (live_subregs_used, regno);
4585 : 0 : bitmap_set_bit (live_relevant_regs, regno);
4586 : : }
4587 : : }
4588 : : }
4589 : : }
4590 : :
4591 : : /* FIXME!! The following code is a disaster. Reload needs to see the
4592 : : labels and jump tables that are just hanging out in between
4593 : : the basic blocks. See pr33676. */
4594 : 0 : insn = BB_HEAD (bb);
4595 : :
4596 : : /* Skip over the barriers and cruft. */
4597 : 0 : while (insn && (BARRIER_P (insn) || NOTE_P (insn)
4598 : 0 : || BLOCK_FOR_INSN (insn) == bb))
4599 : 0 : insn = PREV_INSN (insn);
4600 : :
4601 : : /* While we add anything except barriers and notes, the focus is
4602 : : to get the labels and jump tables into the
4603 : : reload_insn_chain. */
4604 : 0 : while (insn)
4605 : : {
4606 : 0 : if (!NOTE_P (insn) && !BARRIER_P (insn))
4607 : : {
4608 : 0 : if (BLOCK_FOR_INSN (insn))
4609 : : break;
4610 : :
4611 : 0 : c = new_insn_chain ();
4612 : 0 : c->next = next;
4613 : 0 : next = c;
4614 : 0 : *p = c;
4615 : 0 : p = &c->prev;
4616 : :
4617 : : /* The block makes no sense here, but it is what the old
4618 : : code did. */
4619 : 0 : c->block = bb->index;
4620 : 0 : c->insn = insn;
4621 : 0 : bitmap_copy (&c->live_throughout, live_relevant_regs);
4622 : : }
4623 : 0 : insn = PREV_INSN (insn);
4624 : : }
4625 : : }
4626 : :
4627 : 0 : reload_insn_chain = c;
4628 : 0 : *p = NULL;
4629 : :
4630 : 0 : for (i = 0; i < (unsigned int) max_regno; i++)
4631 : 0 : if (live_subregs[i] != NULL)
4632 : 0 : sbitmap_free (live_subregs[i]);
4633 : 0 : free (live_subregs);
4634 : :
4635 : 0 : if (dump_file)
4636 : 0 : print_insn_chains (dump_file);
4637 : 0 : }
4638 : :
4639 : : /* Examine the rtx found in *LOC, which is read or written to as determined
4640 : : by TYPE. Return false if we find a reason why an insn containing this
4641 : : rtx should not be moved (such as accesses to non-constant memory), true
4642 : : otherwise. */
4643 : : static bool
4644 : 6751015 : rtx_moveable_p (rtx *loc, enum op_type type)
4645 : : {
4646 : 6757856 : const char *fmt;
4647 : 6757856 : rtx x = *loc;
4648 : 6757856 : int i, j;
4649 : :
4650 : 6757856 : enum rtx_code code = GET_CODE (x);
4651 : 6757856 : switch (code)
4652 : : {
4653 : : case CONST:
4654 : : CASE_CONST_ANY:
4655 : : case SYMBOL_REF:
4656 : : case LABEL_REF:
4657 : : return true;
4658 : :
4659 : 0 : case PC:
4660 : 0 : return type == OP_IN;
4661 : :
4662 : 2394014 : case REG:
4663 : 2394014 : if (x == frame_pointer_rtx)
4664 : : return true;
4665 : 2392133 : if (HARD_REGISTER_P (x))
4666 : : return false;
4667 : :
4668 : : return true;
4669 : :
4670 : 622309 : case MEM:
4671 : 622309 : if (type == OP_IN && MEM_READONLY_P (x))
4672 : 6771 : return rtx_moveable_p (&XEXP (x, 0), OP_IN);
4673 : : return false;
4674 : :
4675 : 2085961 : case SET:
4676 : 2085961 : return (rtx_moveable_p (&SET_SRC (x), OP_IN)
4677 : 2085961 : && rtx_moveable_p (&SET_DEST (x), OP_OUT));
4678 : :
4679 : 5 : case STRICT_LOW_PART:
4680 : 5 : return rtx_moveable_p (&XEXP (x, 0), OP_OUT);
4681 : :
4682 : 534 : case ZERO_EXTRACT:
4683 : 534 : case SIGN_EXTRACT:
4684 : 534 : return (rtx_moveable_p (&XEXP (x, 0), type)
4685 : 534 : && rtx_moveable_p (&XEXP (x, 1), OP_IN)
4686 : 1068 : && rtx_moveable_p (&XEXP (x, 2), OP_IN));
4687 : :
4688 : 65 : case CLOBBER:
4689 : 65 : return rtx_moveable_p (&SET_DEST (x), OP_OUT);
4690 : :
4691 : : case UNSPEC_VOLATILE:
4692 : : /* It is a bad idea to consider insns with such rtl
4693 : : as moveable ones. The insn scheduler also considers them as barrier
4694 : : for a reason. */
4695 : : return false;
4696 : :
4697 : 0 : case ASM_OPERANDS:
4698 : : /* The same is true for volatile asm: it has unknown side effects, it
4699 : : cannot be moved at will. */
4700 : 0 : if (MEM_VOLATILE_P (x))
4701 : : return false;
4702 : :
4703 : 1099811 : default:
4704 : 1099811 : break;
4705 : : }
4706 : :
4707 : 1099811 : fmt = GET_RTX_FORMAT (code);
4708 : 2830526 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4709 : : {
4710 : 1898338 : if (fmt[i] == 'e')
4711 : : {
4712 : 1491410 : if (!rtx_moveable_p (&XEXP (x, i), type))
4713 : : return false;
4714 : : }
4715 : 406928 : else if (fmt[i] == 'E')
4716 : 540255 : for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4717 : : {
4718 : 417417 : if (!rtx_moveable_p (&XVECEXP (x, i, j), type))
4719 : : return false;
4720 : : }
4721 : : }
4722 : : return true;
4723 : : }
4724 : :
4725 : : /* A wrapper around dominated_by_p, which uses the information in UID_LUID
4726 : : to give dominance relationships between two insns I1 and I2. */
4727 : : static bool
4728 : 21057497 : insn_dominated_by_p (rtx i1, rtx i2, int *uid_luid)
4729 : : {
4730 : 21057497 : basic_block bb1 = BLOCK_FOR_INSN (i1);
4731 : 21057497 : basic_block bb2 = BLOCK_FOR_INSN (i2);
4732 : :
4733 : 21057497 : if (bb1 == bb2)
4734 : 11181384 : return uid_luid[INSN_UID (i2)] < uid_luid[INSN_UID (i1)];
4735 : 9876113 : return dominated_by_p (CDI_DOMINATORS, bb1, bb2);
4736 : : }
4737 : :
4738 : : /* Record the range of register numbers added by find_moveable_pseudos. */
4739 : : int first_moveable_pseudo, last_moveable_pseudo;
4740 : :
4741 : : /* These two vectors hold data for every register added by
4742 : : find_movable_pseudos, with index 0 holding data for the
4743 : : first_moveable_pseudo. */
4744 : : /* The original home register. */
4745 : : static vec<rtx> pseudo_replaced_reg;
4746 : :
4747 : : /* Look for instances where we have an instruction that is known to increase
4748 : : register pressure, and whose result is not used immediately. If it is
4749 : : possible to move the instruction downwards to just before its first use,
4750 : : split its lifetime into two ranges. We create a new pseudo to compute the
4751 : : value, and emit a move instruction just before the first use. If, after
4752 : : register allocation, the new pseudo remains unallocated, the function
4753 : : move_unallocated_pseudos then deletes the move instruction and places
4754 : : the computation just before the first use.
4755 : :
4756 : : Such a move is safe and profitable if all the input registers remain live
4757 : : and unchanged between the original computation and its first use. In such
4758 : : a situation, the computation is known to increase register pressure, and
4759 : : moving it is known to at least not worsen it.
4760 : :
4761 : : We restrict moves to only those cases where a register remains unallocated,
4762 : : in order to avoid interfering too much with the instruction schedule. As
4763 : : an exception, we may move insns which only modify their input register
4764 : : (typically induction variables), as this increases the freedom for our
4765 : : intended transformation, and does not limit the second instruction
4766 : : scheduler pass. */
4767 : :
4768 : : static void
4769 : 1044976 : find_moveable_pseudos (void)
4770 : : {
4771 : 1044976 : unsigned i;
4772 : 1044976 : int max_regs = max_reg_num ();
4773 : 1044976 : int max_uid = get_max_uid ();
4774 : 1044976 : basic_block bb;
4775 : 1044976 : int *uid_luid = XNEWVEC (int, max_uid);
4776 : 1044976 : rtx_insn **closest_uses = XNEWVEC (rtx_insn *, max_regs);
4777 : : /* A set of registers which are live but not modified throughout a block. */
4778 : 1044976 : bitmap_head *bb_transp_live = XNEWVEC (bitmap_head,
4779 : : last_basic_block_for_fn (cfun));
4780 : : /* A set of registers which only exist in a given basic block. */
4781 : 1044976 : bitmap_head *bb_local = XNEWVEC (bitmap_head,
4782 : : last_basic_block_for_fn (cfun));
4783 : : /* A set of registers which are set once, in an instruction that can be
4784 : : moved freely downwards, but are otherwise transparent to a block. */
4785 : 1044976 : bitmap_head *bb_moveable_reg_sets = XNEWVEC (bitmap_head,
4786 : : last_basic_block_for_fn (cfun));
4787 : 1044976 : auto_bitmap live, used, set, interesting, unusable_as_input;
4788 : 1044976 : bitmap_iterator bi;
4789 : :
4790 : 1044976 : first_moveable_pseudo = max_regs;
4791 : 1044976 : pseudo_replaced_reg.release ();
4792 : 1044976 : pseudo_replaced_reg.safe_grow_cleared (max_regs, true);
4793 : :
4794 : 1044976 : df_analyze ();
4795 : 1044976 : calculate_dominance_info (CDI_DOMINATORS);
4796 : :
4797 : 1044976 : i = 0;
4798 : 12211762 : FOR_EACH_BB_FN (bb, cfun)
4799 : : {
4800 : 11166786 : rtx_insn *insn;
4801 : 11166786 : bitmap transp = bb_transp_live + bb->index;
4802 : 11166786 : bitmap moveable = bb_moveable_reg_sets + bb->index;
4803 : 11166786 : bitmap local = bb_local + bb->index;
4804 : :
4805 : 11166786 : bitmap_initialize (local, 0);
4806 : 11166786 : bitmap_initialize (transp, 0);
4807 : 11166786 : bitmap_initialize (moveable, 0);
4808 : 11166786 : bitmap_copy (live, df_get_live_out (bb));
4809 : 11166786 : bitmap_and_into (live, df_get_live_in (bb));
4810 : 11166786 : bitmap_copy (transp, live);
4811 : 11166786 : bitmap_clear (moveable);
4812 : 11166786 : bitmap_clear (live);
4813 : 11166786 : bitmap_clear (used);
4814 : 11166786 : bitmap_clear (set);
4815 : 141434596 : FOR_BB_INSNS (bb, insn)
4816 : 130267810 : if (NONDEBUG_INSN_P (insn))
4817 : : {
4818 : 59263656 : df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4819 : 59263656 : df_ref def, use;
4820 : :
4821 : 59263656 : uid_luid[INSN_UID (insn)] = i++;
4822 : :
4823 : 59263656 : def = df_single_def (insn_info);
4824 : 59263656 : use = df_single_use (insn_info);
4825 : 59263656 : if (use
4826 : 59263656 : && def
4827 : 19712647 : && DF_REF_REGNO (use) == DF_REF_REGNO (def)
4828 : 613511 : && !bitmap_bit_p (set, DF_REF_REGNO (use))
4829 : 59342531 : && rtx_moveable_p (&PATTERN (insn), OP_IN))
4830 : : {
4831 : 32435 : unsigned regno = DF_REF_REGNO (use);
4832 : 32435 : bitmap_set_bit (moveable, regno);
4833 : 32435 : bitmap_set_bit (set, regno);
4834 : 32435 : bitmap_set_bit (used, regno);
4835 : 32435 : bitmap_clear_bit (transp, regno);
4836 : 32435 : continue;
4837 : 32435 : }
4838 : 131502578 : FOR_EACH_INSN_INFO_USE (use, insn_info)
4839 : : {
4840 : 72271357 : unsigned regno = DF_REF_REGNO (use);
4841 : 72271357 : bitmap_set_bit (used, regno);
4842 : 72271357 : if (bitmap_clear_bit (moveable, regno))
4843 : 16152 : bitmap_clear_bit (transp, regno);
4844 : : }
4845 : :
4846 : 490375931 : FOR_EACH_INSN_INFO_DEF (def, insn_info)
4847 : : {
4848 : 431144710 : unsigned regno = DF_REF_REGNO (def);
4849 : 431144710 : bitmap_set_bit (set, regno);
4850 : 431144710 : bitmap_clear_bit (transp, regno);
4851 : 431144710 : bitmap_clear_bit (moveable, regno);
4852 : : }
4853 : : }
4854 : : }
4855 : :
4856 : 12211762 : FOR_EACH_BB_FN (bb, cfun)
4857 : : {
4858 : 11166786 : bitmap local = bb_local + bb->index;
4859 : 11166786 : rtx_insn *insn;
4860 : :
4861 : 141434596 : FOR_BB_INSNS (bb, insn)
4862 : 130267810 : if (NONDEBUG_INSN_P (insn))
4863 : : {
4864 : 59263656 : df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4865 : 59263656 : rtx_insn *def_insn;
4866 : 59263656 : rtx closest_use, note;
4867 : 59263656 : df_ref def, use;
4868 : 59263656 : unsigned regno;
4869 : 59263656 : bool all_dominated, all_local;
4870 : 59263656 : machine_mode mode;
4871 : :
4872 : 59263656 : def = df_single_def (insn_info);
4873 : : /* There must be exactly one def in this insn. */
4874 : 32461456 : if (!def || !single_set (insn))
4875 : 26881778 : continue;
4876 : : /* This must be the only definition of the reg. We also limit
4877 : : which modes we deal with so that we can assume we can generate
4878 : : move instructions. */
4879 : 32381878 : regno = DF_REF_REGNO (def);
4880 : 32381878 : mode = GET_MODE (DF_REF_REG (def));
4881 : 32381878 : if (DF_REG_DEF_COUNT (regno) != 1
4882 : 12290887 : || !DF_REF_INSN_INFO (def)
4883 : 12290887 : || HARD_REGISTER_NUM_P (regno)
4884 : 12259050 : || DF_REG_EQ_USE_COUNT (regno) > 0
4885 : 11714680 : || (!INTEGRAL_MODE_P (mode)
4886 : : && !FLOAT_MODE_P (mode)
4887 : : && !OPAQUE_MODE_P (mode)))
4888 : 20667198 : continue;
4889 : 11714680 : def_insn = DF_REF_INSN (def);
4890 : :
4891 : 19696458 : for (note = REG_NOTES (def_insn); note; note = XEXP (note, 1))
4892 : 10620696 : if (REG_NOTE_KIND (note) == REG_EQUIV && MEM_P (XEXP (note, 0)))
4893 : : break;
4894 : :
4895 : 11714680 : if (note)
4896 : : {
4897 : 2638918 : if (dump_file)
4898 : 68 : fprintf (dump_file, "Ignoring reg %d, has equiv memory\n",
4899 : : regno);
4900 : 2638918 : bitmap_set_bit (unusable_as_input, regno);
4901 : 2638918 : continue;
4902 : : }
4903 : :
4904 : 9075762 : use = DF_REG_USE_CHAIN (regno);
4905 : 9075762 : all_dominated = true;
4906 : 9075762 : all_local = true;
4907 : 9075762 : closest_use = NULL_RTX;
4908 : 26434052 : for (; use; use = DF_REF_NEXT_REG (use))
4909 : : {
4910 : 17358290 : rtx_insn *insn;
4911 : 17358290 : if (!DF_REF_INSN_INFO (use))
4912 : : {
4913 : : all_dominated = false;
4914 : : all_local = false;
4915 : : break;
4916 : : }
4917 : 17358290 : insn = DF_REF_INSN (use);
4918 : 17358290 : if (DEBUG_INSN_P (insn))
4919 : 2099615 : continue;
4920 : 15258675 : if (BLOCK_FOR_INSN (insn) != BLOCK_FOR_INSN (def_insn))
4921 : 5994891 : all_local = false;
4922 : 15258675 : if (!insn_dominated_by_p (insn, def_insn, uid_luid))
4923 : 9940 : all_dominated = false;
4924 : 15258675 : if (closest_use != insn && closest_use != const0_rtx)
4925 : : {
4926 : 13491769 : if (closest_use == NULL_RTX)
4927 : : closest_use = insn;
4928 : 4476914 : else if (insn_dominated_by_p (closest_use, insn, uid_luid))
4929 : : closest_use = insn;
4930 : 1321908 : else if (!insn_dominated_by_p (insn, closest_use, uid_luid))
4931 : 619335 : closest_use = const0_rtx;
4932 : : }
4933 : : }
4934 : 9075762 : if (!all_dominated)
4935 : : {
4936 : 4943 : if (dump_file)
4937 : 0 : fprintf (dump_file, "Reg %d not all uses dominated by set\n",
4938 : : regno);
4939 : 4943 : continue;
4940 : : }
4941 : 9070819 : if (all_local)
4942 : 6493687 : bitmap_set_bit (local, regno);
4943 : 8453343 : if (closest_use == const0_rtx || closest_use == NULL
4944 : 17463255 : || next_nonnote_nondebug_insn (def_insn) == closest_use)
4945 : : {
4946 : 5724244 : if (dump_file)
4947 : 97 : fprintf (dump_file, "Reg %d uninteresting%s\n", regno,
4948 : 97 : closest_use == const0_rtx || closest_use == NULL
4949 : : ? " (no unique first use)" : "");
4950 : 5724244 : continue;
4951 : : }
4952 : :
4953 : 3346575 : bitmap_set_bit (interesting, regno);
4954 : : /* If we get here, we know closest_use is a non-NULL insn
4955 : : (as opposed to const_0_rtx). */
4956 : 3346575 : closest_uses[regno] = as_a <rtx_insn *> (closest_use);
4957 : :
4958 : 3346575 : if (dump_file && (all_local || all_dominated))
4959 : : {
4960 : 78 : fprintf (dump_file, "Reg %u:", regno);
4961 : 78 : if (all_local)
4962 : 14 : fprintf (dump_file, " local to bb %d", bb->index);
4963 : 78 : if (all_dominated)
4964 : 78 : fprintf (dump_file, " def dominates all uses");
4965 : 78 : if (closest_use != const0_rtx)
4966 : 78 : fprintf (dump_file, " has unique first use");
4967 : 78 : fputs ("\n", dump_file);
4968 : : }
4969 : : }
4970 : : }
4971 : :
4972 : 4391551 : EXECUTE_IF_SET_IN_BITMAP (interesting, 0, i, bi)
4973 : : {
4974 : 3346575 : df_ref def = DF_REG_DEF_CHAIN (i);
4975 : 3346575 : rtx_insn *def_insn = DF_REF_INSN (def);
4976 : 3346575 : basic_block def_block = BLOCK_FOR_INSN (def_insn);
4977 : 3346575 : bitmap def_bb_local = bb_local + def_block->index;
4978 : 3346575 : bitmap def_bb_moveable = bb_moveable_reg_sets + def_block->index;
4979 : 3346575 : bitmap def_bb_transp = bb_transp_live + def_block->index;
4980 : 3346575 : bool local_to_bb_p = bitmap_bit_p (def_bb_local, i);
4981 : 3346575 : rtx_insn *use_insn = closest_uses[i];
4982 : 3346575 : df_ref use;
4983 : 3346575 : bool all_ok = true;
4984 : 3346575 : bool all_transp = true;
4985 : :
4986 : 3346575 : if (!REG_P (DF_REF_REG (def)))
4987 : 40619 : continue;
4988 : :
4989 : 3305956 : if (!local_to_bb_p)
4990 : : {
4991 : 1250580 : if (dump_file)
4992 : 64 : fprintf (dump_file, "Reg %u not local to one basic block\n",
4993 : : i);
4994 : 1250580 : continue;
4995 : : }
4996 : 2055376 : if (reg_equiv_init (i) != NULL_RTX)
4997 : : {
4998 : 48258 : if (dump_file)
4999 : 0 : fprintf (dump_file, "Ignoring reg %u with equiv init insn\n",
5000 : : i);
5001 : 48258 : continue;
5002 : : }
5003 : 2007118 : if (!rtx_moveable_p (&PATTERN (def_insn), OP_IN))
5004 : : {
5005 : 1385105 : if (dump_file)
5006 : 14 : fprintf (dump_file, "Found def insn %d for %d to be not moveable\n",
5007 : 14 : INSN_UID (def_insn), i);
5008 : 1385105 : continue;
5009 : : }
5010 : 622013 : if (dump_file)
5011 : 0 : fprintf (dump_file, "Examining insn %d, def for %d\n",
5012 : 0 : INSN_UID (def_insn), i);
5013 : 1390180 : FOR_EACH_INSN_USE (use, def_insn)
5014 : : {
5015 : 827661 : unsigned regno = DF_REF_REGNO (use);
5016 : 827661 : if (bitmap_bit_p (unusable_as_input, regno))
5017 : : {
5018 : 59494 : all_ok = false;
5019 : 59494 : if (dump_file)
5020 : 0 : fprintf (dump_file, " found unusable input reg %u.\n", regno);
5021 : : break;
5022 : : }
5023 : 768167 : if (!bitmap_bit_p (def_bb_transp, regno))
5024 : : {
5025 : 716490 : if (bitmap_bit_p (def_bb_moveable, regno)
5026 : 716490 : && !control_flow_insn_p (use_insn))
5027 : : {
5028 : 32 : if (modified_between_p (DF_REF_REG (use), def_insn, use_insn))
5029 : : {
5030 : 0 : rtx_insn *x = NEXT_INSN (def_insn);
5031 : 0 : while (!modified_in_p (DF_REF_REG (use), x))
5032 : : {
5033 : 0 : gcc_assert (x != use_insn);
5034 : 0 : x = NEXT_INSN (x);
5035 : : }
5036 : 0 : if (dump_file)
5037 : 0 : fprintf (dump_file, " input reg %u modified but insn %d moveable\n",
5038 : 0 : regno, INSN_UID (x));
5039 : 0 : emit_insn_after (PATTERN (x), use_insn);
5040 : 0 : set_insn_deleted (x);
5041 : : }
5042 : : else
5043 : : {
5044 : 32 : if (dump_file)
5045 : 0 : fprintf (dump_file, " input reg %u modified between def and use\n",
5046 : : regno);
5047 : : all_transp = false;
5048 : : }
5049 : : }
5050 : : else
5051 : : all_transp = false;
5052 : : }
5053 : : }
5054 : 0 : if (!all_ok)
5055 : 59494 : continue;
5056 : 562519 : if (!dbg_cnt (ira_move))
5057 : : break;
5058 : 562519 : if (dump_file)
5059 : 0 : fprintf (dump_file, " all ok%s\n", all_transp ? " and transp" : "");
5060 : :
5061 : 562519 : if (all_transp)
5062 : : {
5063 : 14904 : rtx def_reg = DF_REF_REG (def);
5064 : 14904 : rtx newreg = ira_create_new_reg (def_reg);
5065 : 14904 : if (validate_change (def_insn, DF_REF_REAL_LOC (def), newreg, 0))
5066 : : {
5067 : 14904 : unsigned nregno = REGNO (newreg);
5068 : 14904 : emit_insn_before (gen_move_insn (def_reg, newreg), use_insn);
5069 : 14904 : nregno -= max_regs;
5070 : 14904 : pseudo_replaced_reg[nregno] = def_reg;
5071 : : }
5072 : : }
5073 : : }
5074 : :
5075 : 12211762 : FOR_EACH_BB_FN (bb, cfun)
5076 : : {
5077 : 11166786 : bitmap_clear (bb_local + bb->index);
5078 : 11166786 : bitmap_clear (bb_transp_live + bb->index);
5079 : 11166786 : bitmap_clear (bb_moveable_reg_sets + bb->index);
5080 : : }
5081 : 1044976 : free (uid_luid);
5082 : 1044976 : free (closest_uses);
5083 : 1044976 : free (bb_local);
5084 : 1044976 : free (bb_transp_live);
5085 : 1044976 : free (bb_moveable_reg_sets);
5086 : :
5087 : 1044976 : last_moveable_pseudo = max_reg_num ();
5088 : :
5089 : 1044976 : fix_reg_equiv_init ();
5090 : 1044976 : expand_reg_info ();
5091 : 1044976 : regstat_free_n_sets_and_refs ();
5092 : 1044976 : regstat_free_ri ();
5093 : 1044976 : regstat_init_n_sets_and_refs ();
5094 : 1044976 : regstat_compute_ri ();
5095 : 1044976 : free_dominance_info (CDI_DOMINATORS);
5096 : 1044976 : }
5097 : :
5098 : : /* If SET pattern SET is an assignment from a hard register to a pseudo which
5099 : : is live at CALL_DOM (if non-NULL, otherwise this check is omitted), return
5100 : : the destination. Otherwise return NULL. */
5101 : :
5102 : : static rtx
5103 : 2127610 : interesting_dest_for_shprep_1 (rtx set, basic_block call_dom)
5104 : : {
5105 : 2127610 : rtx src = SET_SRC (set);
5106 : 2127610 : rtx dest = SET_DEST (set);
5107 : 718365 : if (!REG_P (src) || !HARD_REGISTER_P (src)
5108 : 570422 : || !REG_P (dest) || HARD_REGISTER_P (dest)
5109 : 2677959 : || (call_dom && !bitmap_bit_p (df_get_live_in (call_dom), REGNO (dest))))
5110 : 1667389 : return NULL;
5111 : : return dest;
5112 : : }
5113 : :
5114 : : /* If insn is interesting for parameter range-splitting shrink-wrapping
5115 : : preparation, i.e. it is a single set from a hard register to a pseudo, which
5116 : : is live at CALL_DOM (if non-NULL, otherwise this check is omitted), or a
5117 : : parallel statement with only one such statement, return the destination.
5118 : : Otherwise return NULL. */
5119 : :
5120 : : static rtx
5121 : 4475201 : interesting_dest_for_shprep (rtx_insn *insn, basic_block call_dom)
5122 : : {
5123 : 4475201 : if (!INSN_P (insn))
5124 : : return NULL;
5125 : 3596551 : rtx pat = PATTERN (insn);
5126 : 3596551 : if (GET_CODE (pat) == SET)
5127 : 1888390 : return interesting_dest_for_shprep_1 (pat, call_dom);
5128 : :
5129 : 1708161 : if (GET_CODE (pat) != PARALLEL)
5130 : : return NULL;
5131 : : rtx ret = NULL;
5132 : 626567 : for (int i = 0; i < XVECLEN (pat, 0); i++)
5133 : : {
5134 : 422566 : rtx sub = XVECEXP (pat, 0, i);
5135 : 422566 : if (GET_CODE (sub) == USE || GET_CODE (sub) == CLOBBER)
5136 : 176601 : continue;
5137 : 245965 : if (GET_CODE (sub) != SET
5138 : 245965 : || side_effects_p (sub))
5139 : 6745 : return NULL;
5140 : 239220 : rtx dest = interesting_dest_for_shprep_1 (sub, call_dom);
5141 : 239220 : if (dest && ret)
5142 : : return NULL;
5143 : 239220 : if (dest)
5144 : 415821 : ret = dest;
5145 : : }
5146 : : return ret;
5147 : : }
5148 : :
5149 : : /* Split live ranges of pseudos that are loaded from hard registers in the
5150 : : first BB in a BB that dominates all non-sibling call if such a BB can be
5151 : : found and is not in a loop. Return true if the function has made any
5152 : : changes. */
5153 : :
5154 : : static bool
5155 : 1044976 : split_live_ranges_for_shrink_wrap (void)
5156 : : {
5157 : 1044976 : basic_block bb, call_dom = NULL;
5158 : 1044976 : basic_block first = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5159 : 1044976 : rtx_insn *insn, *last_interesting_insn = NULL;
5160 : 1044976 : auto_bitmap need_new, reachable;
5161 : 1044976 : vec<basic_block> queue;
5162 : :
5163 : 1044976 : if (!SHRINK_WRAPPING_ENABLED)
5164 : 246 : return false;
5165 : :
5166 : 1044730 : queue.create (n_basic_blocks_for_fn (cfun));
5167 : :
5168 : 7648546 : FOR_EACH_BB_FN (bb, cfun)
5169 : 76584843 : FOR_BB_INSNS (bb, insn)
5170 : 71362425 : if (CALL_P (insn) && !SIBLING_CALL_P (insn))
5171 : : {
5172 : 1791357 : if (bb == first)
5173 : : {
5174 : 409959 : queue.release ();
5175 : 409959 : return false;
5176 : : }
5177 : :
5178 : 1381398 : bitmap_set_bit (need_new, bb->index);
5179 : 1381398 : bitmap_set_bit (reachable, bb->index);
5180 : 1381398 : queue.quick_push (bb);
5181 : 1381398 : break;
5182 : : }
5183 : :
5184 : 634771 : if (queue.is_empty ())
5185 : : {
5186 : 376384 : queue.release ();
5187 : 376384 : return false;
5188 : : }
5189 : :
5190 : 4278929 : while (!queue.is_empty ())
5191 : : {
5192 : 4020542 : edge e;
5193 : 4020542 : edge_iterator ei;
5194 : :
5195 : 4020542 : bb = queue.pop ();
5196 : 9640739 : FOR_EACH_EDGE (e, ei, bb->succs)
5197 : 5620197 : if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
5198 : 5620197 : && bitmap_set_bit (reachable, e->dest->index))
5199 : 2639144 : queue.quick_push (e->dest);
5200 : : }
5201 : 258387 : queue.release ();
5202 : :
5203 : 4021187 : FOR_BB_INSNS (first, insn)
5204 : : {
5205 : 3763078 : rtx dest = interesting_dest_for_shprep (insn, NULL);
5206 : 3763078 : if (!dest)
5207 : 3358025 : continue;
5208 : :
5209 : 405053 : if (DF_REG_DEF_COUNT (REGNO (dest)) > 1)
5210 : : return false;
5211 : :
5212 : 404775 : for (df_ref use = DF_REG_USE_CHAIN (REGNO(dest));
5213 : 2827119 : use;
5214 : 2422344 : use = DF_REF_NEXT_REG (use))
5215 : : {
5216 : 2422344 : int ubbi = DF_REF_BB (use)->index;
5217 : :
5218 : : /* Only non debug insns should be taken into account. */
5219 : 2422344 : if (NONDEBUG_INSN_P (DF_REF_INSN (use))
5220 : 2422344 : && bitmap_bit_p (reachable, ubbi))
5221 : 1138491 : bitmap_set_bit (need_new, ubbi);
5222 : : }
5223 : : last_interesting_insn = insn;
5224 : : }
5225 : :
5226 : 258109 : if (!last_interesting_insn)
5227 : : return false;
5228 : :
5229 : 186332 : call_dom = nearest_common_dominator_for_set (CDI_DOMINATORS, need_new);
5230 : 186332 : if (call_dom == first)
5231 : : return false;
5232 : :
5233 : 96103 : loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
5234 : 215123 : while (bb_loop_depth (call_dom) > 0)
5235 : 22917 : call_dom = get_immediate_dominator (CDI_DOMINATORS, call_dom);
5236 : 96103 : loop_optimizer_finalize ();
5237 : :
5238 : 96103 : if (call_dom == first)
5239 : : return false;
5240 : :
5241 : 83894 : calculate_dominance_info (CDI_POST_DOMINATORS);
5242 : 83894 : if (dominated_by_p (CDI_POST_DOMINATORS, first, call_dom))
5243 : : {
5244 : 7807 : free_dominance_info (CDI_POST_DOMINATORS);
5245 : 7807 : return false;
5246 : : }
5247 : 76087 : free_dominance_info (CDI_POST_DOMINATORS);
5248 : :
5249 : 76087 : if (dump_file)
5250 : 2 : fprintf (dump_file, "Will split live ranges of parameters at BB %i\n",
5251 : : call_dom->index);
5252 : :
5253 : 76087 : bool ret = false;
5254 : 764607 : FOR_BB_INSNS (first, insn)
5255 : : {
5256 : 712123 : rtx dest = interesting_dest_for_shprep (insn, call_dom);
5257 : 712123 : if (!dest || dest == pic_offset_table_rtx)
5258 : 656955 : continue;
5259 : :
5260 : 55168 : bool need_newreg = false;
5261 : 55168 : df_ref use, next;
5262 : 72118 : for (use = DF_REG_USE_CHAIN (REGNO (dest)); use; use = next)
5263 : : {
5264 : 72043 : rtx_insn *uin = DF_REF_INSN (use);
5265 : 72043 : next = DF_REF_NEXT_REG (use);
5266 : :
5267 : 72043 : if (DEBUG_INSN_P (uin))
5268 : 436 : continue;
5269 : :
5270 : 71607 : basic_block ubb = BLOCK_FOR_INSN (uin);
5271 : 71607 : if (ubb == call_dom
5272 : 71607 : || dominated_by_p (CDI_DOMINATORS, ubb, call_dom))
5273 : : {
5274 : : need_newreg = true;
5275 : : break;
5276 : : }
5277 : : }
5278 : :
5279 : 55168 : if (need_newreg)
5280 : : {
5281 : 55093 : rtx newreg = ira_create_new_reg (dest);
5282 : :
5283 : 421472 : for (use = DF_REG_USE_CHAIN (REGNO (dest)); use; use = next)
5284 : : {
5285 : 366379 : rtx_insn *uin = DF_REF_INSN (use);
5286 : 366379 : next = DF_REF_NEXT_REG (use);
5287 : :
5288 : 366379 : basic_block ubb = BLOCK_FOR_INSN (uin);
5289 : 366379 : if (ubb == call_dom
5290 : 366379 : || dominated_by_p (CDI_DOMINATORS, ubb, call_dom))
5291 : 264501 : validate_change (uin, DF_REF_REAL_LOC (use), newreg, true);
5292 : : }
5293 : :
5294 : 55093 : rtx_insn *new_move = gen_move_insn (newreg, dest);
5295 : 55093 : emit_insn_after (new_move, bb_note (call_dom));
5296 : 55093 : if (dump_file)
5297 : : {
5298 : 2 : fprintf (dump_file, "Split live-range of register ");
5299 : 2 : print_rtl_single (dump_file, dest);
5300 : : }
5301 : : ret = true;
5302 : : }
5303 : :
5304 : 55168 : if (insn == last_interesting_insn)
5305 : : break;
5306 : : }
5307 : 76087 : apply_change_group ();
5308 : 76087 : return ret;
5309 : 1044976 : }
5310 : :
5311 : : /* Perform the second half of the transformation started in
5312 : : find_moveable_pseudos. We look for instances where the newly introduced
5313 : : pseudo remains unallocated, and remove it by moving the definition to
5314 : : just before its use, replacing the move instruction generated by
5315 : : find_moveable_pseudos. */
5316 : : static void
5317 : 1044976 : move_unallocated_pseudos (void)
5318 : : {
5319 : 1044976 : int i;
5320 : 1059880 : for (i = first_moveable_pseudo; i < last_moveable_pseudo; i++)
5321 : 14904 : if (reg_renumber[i] < 0)
5322 : : {
5323 : 3152 : int idx = i - first_moveable_pseudo;
5324 : 3152 : rtx other_reg = pseudo_replaced_reg[idx];
5325 : : /* The iterating range [first_moveable_pseudo, last_moveable_pseudo)
5326 : : covers every new pseudo created in find_moveable_pseudos,
5327 : : regardless of the validation with it is successful or not.
5328 : : So we need to skip the pseudos which were used in those failed
5329 : : validations to avoid unexpected DF info and consequent ICE.
5330 : : We only set pseudo_replaced_reg[] when the validation is successful
5331 : : in find_moveable_pseudos, it's enough to check it here. */
5332 : 3152 : if (!other_reg)
5333 : 0 : continue;
5334 : 3152 : rtx_insn *def_insn = DF_REF_INSN (DF_REG_DEF_CHAIN (i));
5335 : : /* The use must follow all definitions of OTHER_REG, so we can
5336 : : insert the new definition immediately after any of them. */
5337 : 3152 : df_ref other_def = DF_REG_DEF_CHAIN (REGNO (other_reg));
5338 : 3152 : rtx_insn *move_insn = DF_REF_INSN (other_def);
5339 : 3152 : rtx_insn *newinsn = emit_insn_after (PATTERN (def_insn), move_insn);
5340 : 3152 : rtx set;
5341 : 3152 : int success;
5342 : :
5343 : 3152 : if (dump_file)
5344 : 0 : fprintf (dump_file, "moving def of %d (insn %d now) ",
5345 : 0 : REGNO (other_reg), INSN_UID (def_insn));
5346 : :
5347 : 3152 : delete_insn (move_insn);
5348 : 6304 : while ((other_def = DF_REG_DEF_CHAIN (REGNO (other_reg))))
5349 : 0 : delete_insn (DF_REF_INSN (other_def));
5350 : 3152 : delete_insn (def_insn);
5351 : :
5352 : 3152 : set = single_set (newinsn);
5353 : 3152 : success = validate_change (newinsn, &SET_DEST (set), other_reg, 0);
5354 : 3152 : gcc_assert (success);
5355 : 3152 : if (dump_file)
5356 : 0 : fprintf (dump_file, " %d) rather than keep unallocated replacement %d\n",
5357 : 0 : INSN_UID (newinsn), i);
5358 : 3152 : SET_REG_N_REFS (i, 0);
5359 : : }
5360 : :
5361 : 1044976 : first_moveable_pseudo = last_moveable_pseudo = 0;
5362 : 1044976 : }
5363 : :
5364 : :
5365 : :
5366 : : /* Code dealing with scratches (changing them onto
5367 : : pseudos and restoring them from the pseudos).
5368 : :
5369 : : We change scratches into pseudos at the beginning of IRA to
5370 : : simplify dealing with them (conflicts, hard register assignments).
5371 : :
5372 : : If the pseudo denoting scratch was spilled it means that we do not
5373 : : need a hard register for it. Such pseudos are transformed back to
5374 : : scratches at the end of LRA. */
5375 : :
5376 : : /* Description of location of a former scratch operand. */
5377 : : struct sloc
5378 : : {
5379 : : rtx_insn *insn; /* Insn where the scratch was. */
5380 : : int nop; /* Number of the operand which was a scratch. */
5381 : : unsigned regno; /* regno gnerated instead of scratch */
5382 : : int icode; /* Original icode from which scratch was removed. */
5383 : : };
5384 : :
5385 : : typedef struct sloc *sloc_t;
5386 : :
5387 : : /* Locations of the former scratches. */
5388 : : static vec<sloc_t> scratches;
5389 : :
5390 : : /* Bitmap of scratch regnos. */
5391 : : static bitmap_head scratch_bitmap;
5392 : :
5393 : : /* Bitmap of scratch operands. */
5394 : : static bitmap_head scratch_operand_bitmap;
5395 : :
5396 : : /* Return true if pseudo REGNO is made of SCRATCH. */
5397 : : bool
5398 : 369556286 : ira_former_scratch_p (int regno)
5399 : : {
5400 : 369556286 : return bitmap_bit_p (&scratch_bitmap, regno);
5401 : : }
5402 : :
5403 : : /* Return true if the operand NOP of INSN is a former scratch. */
5404 : : bool
5405 : 0 : ira_former_scratch_operand_p (rtx_insn *insn, int nop)
5406 : : {
5407 : 0 : return bitmap_bit_p (&scratch_operand_bitmap,
5408 : 0 : INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
5409 : : }
5410 : :
5411 : : /* Register operand NOP in INSN as a former scratch. It will be
5412 : : changed to scratch back, if it is necessary, at the LRA end. */
5413 : : void
5414 : 95669 : ira_register_new_scratch_op (rtx_insn *insn, int nop, int icode)
5415 : : {
5416 : 95669 : rtx op = *recog_data.operand_loc[nop];
5417 : 95669 : sloc_t loc = XNEW (struct sloc);
5418 : 95669 : ira_assert (REG_P (op));
5419 : 95669 : loc->insn = insn;
5420 : 95669 : loc->nop = nop;
5421 : 95669 : loc->regno = REGNO (op);
5422 : 95669 : loc->icode = icode;
5423 : 95669 : scratches.safe_push (loc);
5424 : 95669 : bitmap_set_bit (&scratch_bitmap, REGNO (op));
5425 : 191338 : bitmap_set_bit (&scratch_operand_bitmap,
5426 : 95669 : INSN_UID (insn) * MAX_RECOG_OPERANDS + nop);
5427 : 95669 : add_reg_note (insn, REG_UNUSED, op);
5428 : 95669 : }
5429 : :
5430 : : /* Return true if string STR contains constraint 'X'. */
5431 : : static bool
5432 : 95669 : contains_X_constraint_p (const char *str)
5433 : : {
5434 : 95669 : int c;
5435 : :
5436 : 348376 : while ((c = *str))
5437 : : {
5438 : 261571 : str += CONSTRAINT_LEN (c, str);
5439 : 261571 : if (c == 'X') return true;
5440 : : }
5441 : : return false;
5442 : : }
5443 : :
5444 : : /* Change INSN's scratches into pseudos and save their location.
5445 : : Return true if we changed any scratch. */
5446 : : bool
5447 : 277128513 : ira_remove_insn_scratches (rtx_insn *insn, bool all_p, FILE *dump_file,
5448 : : rtx (*get_reg) (rtx original))
5449 : : {
5450 : 277128513 : int i;
5451 : 277128513 : bool insn_changed_p;
5452 : 277128513 : rtx reg, *loc;
5453 : :
5454 : 277128513 : extract_insn (insn);
5455 : 277128513 : insn_changed_p = false;
5456 : 933509740 : for (i = 0; i < recog_data.n_operands; i++)
5457 : : {
5458 : 379252714 : loc = recog_data.operand_loc[i];
5459 : 379252714 : if (GET_CODE (*loc) == SCRATCH && GET_MODE (*loc) != VOIDmode)
5460 : : {
5461 : 104533 : if (! all_p && contains_X_constraint_p (recog_data.constraints[i]))
5462 : 8864 : continue;
5463 : 95669 : insn_changed_p = true;
5464 : 95669 : *loc = reg = get_reg (*loc);
5465 : 95669 : ira_register_new_scratch_op (insn, i, INSN_CODE (insn));
5466 : 95669 : if (ira_dump_file != NULL)
5467 : 0 : fprintf (dump_file,
5468 : : "Removing SCRATCH to p%u in insn #%u (nop %d)\n",
5469 : 0 : REGNO (reg), INSN_UID (insn), i);
5470 : : }
5471 : : }
5472 : 277128513 : return insn_changed_p;
5473 : : }
5474 : :
5475 : : /* Return new register of the same mode as ORIGINAL. Used in
5476 : : remove_scratches. */
5477 : : static rtx
5478 : 86805 : get_scratch_reg (rtx original)
5479 : : {
5480 : 86805 : return gen_reg_rtx (GET_MODE (original));
5481 : : }
5482 : :
5483 : : /* Change scratches into pseudos and save their location. Return true
5484 : : if we changed any scratch. */
5485 : : static bool
5486 : 1481332 : remove_scratches (void)
5487 : : {
5488 : 1481332 : bool change_p = false;
5489 : 1481332 : basic_block bb;
5490 : 1481332 : rtx_insn *insn;
5491 : :
5492 : 1481332 : scratches.create (get_max_uid ());
5493 : 1481332 : bitmap_initialize (&scratch_bitmap, ®_obstack);
5494 : 1481332 : bitmap_initialize (&scratch_operand_bitmap, ®_obstack);
5495 : 16080290 : FOR_EACH_BB_FN (bb, cfun)
5496 : 175035706 : FOR_BB_INSNS (bb, insn)
5497 : 160436748 : if (INSN_P (insn)
5498 : 160436748 : && ira_remove_insn_scratches (insn, false, ira_dump_file, get_scratch_reg))
5499 : : {
5500 : : /* Because we might use DF, we need to keep DF info up to date. */
5501 : 85624 : df_insn_rescan (insn);
5502 : 85624 : change_p = true;
5503 : : }
5504 : 1481332 : return change_p;
5505 : : }
5506 : :
5507 : : /* Changes pseudos created by function remove_scratches onto scratches. */
5508 : : void
5509 : 1481332 : ira_restore_scratches (FILE *dump_file)
5510 : : {
5511 : 1481332 : int regno, n;
5512 : 1481332 : unsigned i;
5513 : 1481332 : rtx *op_loc;
5514 : 1481332 : sloc_t loc;
5515 : :
5516 : 1577001 : for (i = 0; scratches.iterate (i, &loc); i++)
5517 : : {
5518 : : /* Ignore already deleted insns. */
5519 : 95669 : if (NOTE_P (loc->insn)
5520 : 0 : && NOTE_KIND (loc->insn) == NOTE_INSN_DELETED)
5521 : 0 : continue;
5522 : 95669 : extract_insn (loc->insn);
5523 : 95669 : if (loc->icode != INSN_CODE (loc->insn))
5524 : : {
5525 : : /* The icode doesn't match, which means the insn has been modified
5526 : : (e.g. register elimination). The scratch cannot be restored. */
5527 : 0 : continue;
5528 : : }
5529 : 95669 : op_loc = recog_data.operand_loc[loc->nop];
5530 : 95669 : if (REG_P (*op_loc)
5531 : 95669 : && ((regno = REGNO (*op_loc)) >= FIRST_PSEUDO_REGISTER)
5532 : 191338 : && reg_renumber[regno] < 0)
5533 : : {
5534 : : /* It should be only case when scratch register with chosen
5535 : : constraint 'X' did not get memory or hard register. */
5536 : 5243 : ira_assert (ira_former_scratch_p (regno));
5537 : 5243 : *op_loc = gen_rtx_SCRATCH (GET_MODE (*op_loc));
5538 : 5243 : for (n = 0; n < recog_data.n_dups; n++)
5539 : 0 : *recog_data.dup_loc[n]
5540 : 0 : = *recog_data.operand_loc[(int) recog_data.dup_num[n]];
5541 : 5243 : if (dump_file != NULL)
5542 : 0 : fprintf (dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
5543 : 0 : INSN_UID (loc->insn), loc->nop);
5544 : : }
5545 : : }
5546 : 1577001 : for (i = 0; scratches.iterate (i, &loc); i++)
5547 : 95669 : free (loc);
5548 : 1481332 : scratches.release ();
5549 : 1481332 : bitmap_clear (&scratch_bitmap);
5550 : 1481332 : bitmap_clear (&scratch_operand_bitmap);
5551 : 1481332 : }
5552 : :
5553 : :
5554 : :
5555 : : /* If the backend knows where to allocate pseudos for hard
5556 : : register initial values, register these allocations now. */
5557 : : static void
5558 : 1481332 : allocate_initial_values (void)
5559 : : {
5560 : 1481332 : if (targetm.allocate_initial_value)
5561 : : {
5562 : : rtx hreg, preg, x;
5563 : : int i, regno;
5564 : :
5565 : 0 : for (i = 0; HARD_REGISTER_NUM_P (i); i++)
5566 : : {
5567 : 0 : if (! initial_value_entry (i, &hreg, &preg))
5568 : : break;
5569 : :
5570 : 0 : x = targetm.allocate_initial_value (hreg);
5571 : 0 : regno = REGNO (preg);
5572 : 0 : if (x && REG_N_SETS (regno) <= 1)
5573 : : {
5574 : 0 : if (MEM_P (x))
5575 : 0 : reg_equiv_memory_loc (regno) = x;
5576 : : else
5577 : : {
5578 : 0 : basic_block bb;
5579 : 0 : int new_regno;
5580 : :
5581 : 0 : gcc_assert (REG_P (x));
5582 : 0 : new_regno = REGNO (x);
5583 : 0 : reg_renumber[regno] = new_regno;
5584 : : /* Poke the regno right into regno_reg_rtx so that even
5585 : : fixed regs are accepted. */
5586 : 0 : SET_REGNO (preg, new_regno);
5587 : : /* Update global register liveness information. */
5588 : 0 : FOR_EACH_BB_FN (bb, cfun)
5589 : : {
5590 : 0 : if (REGNO_REG_SET_P (df_get_live_in (bb), regno))
5591 : 0 : SET_REGNO_REG_SET (df_get_live_in (bb), new_regno);
5592 : 0 : if (REGNO_REG_SET_P (df_get_live_out (bb), regno))
5593 : 0 : SET_REGNO_REG_SET (df_get_live_out (bb), new_regno);
5594 : : }
5595 : : }
5596 : : }
5597 : : }
5598 : :
5599 : 0 : gcc_checking_assert (! initial_value_entry (FIRST_PSEUDO_REGISTER,
5600 : : &hreg, &preg));
5601 : : }
5602 : 1481332 : }
5603 : :
5604 : :
5605 : :
5606 : :
5607 : : /* True when we use LRA instead of reload pass for the current
5608 : : function. */
5609 : : bool ira_use_lra_p;
5610 : :
5611 : : /* True if we have allocno conflicts. It is false for non-optimized
5612 : : mode or when the conflict table is too big. */
5613 : : bool ira_conflicts_p;
5614 : :
5615 : : /* Saved between IRA and reload. */
5616 : : static int saved_flag_ira_share_spill_slots;
5617 : :
5618 : : /* Set to true while in IRA. */
5619 : : bool ira_in_progress = false;
5620 : :
5621 : : /* Set up array ira_hard_regno_nrefs. */
5622 : : static void
5623 : 1481332 : setup_hard_regno_nrefs (void)
5624 : : {
5625 : 1481332 : int i;
5626 : :
5627 : 137763876 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5628 : : {
5629 : 136282544 : ira_hard_regno_nrefs[i] = 0;
5630 : 136282544 : for (df_ref use = DF_REG_USE_CHAIN (i);
5631 : 252982300 : use != NULL;
5632 : 116699756 : use = DF_REF_NEXT_REG (use))
5633 : 116699756 : if (DF_REF_CLASS (use) != DF_REF_ARTIFICIAL
5634 : 53042558 : && !(DF_REF_INSN_INFO (use) && DEBUG_INSN_P (DF_REF_INSN (use))))
5635 : 49584639 : ira_hard_regno_nrefs[i]++;
5636 : 136282544 : for (df_ref def = DF_REG_DEF_CHAIN (i);
5637 : 691984487 : def != NULL;
5638 : 555701943 : def = DF_REF_NEXT_REG (def))
5639 : 555701943 : if (DF_REF_CLASS (def) != DF_REF_ARTIFICIAL
5640 : 528133154 : && !(DF_REF_INSN_INFO (def) && DEBUG_INSN_P (DF_REF_INSN (def))))
5641 : 528133154 : ira_hard_regno_nrefs[i]++;
5642 : : }
5643 : 1481332 : }
5644 : :
5645 : : /* This is the main entry of IRA. */
5646 : : static void
5647 : 1481332 : ira (FILE *f)
5648 : : {
5649 : 1481332 : bool loops_p;
5650 : 1481332 : int ira_max_point_before_emit;
5651 : 1481332 : bool saved_flag_caller_saves = flag_caller_saves;
5652 : 1481332 : enum ira_region saved_flag_ira_region = flag_ira_region;
5653 : 1481332 : basic_block bb;
5654 : 1481332 : edge_iterator ei;
5655 : 1481332 : edge e;
5656 : 1481332 : bool output_jump_reload_p = false;
5657 : :
5658 : 1481332 : setup_hard_regno_nrefs ();
5659 : 1481332 : if (ira_use_lra_p)
5660 : : {
5661 : : /* First put potential jump output reloads on the output edges
5662 : : as USE which will be removed at the end of LRA. The major
5663 : : goal is actually to create BBs for critical edges for LRA and
5664 : : populate them later by live info. In LRA it will be
5665 : : difficult to do this. */
5666 : 16080281 : FOR_EACH_BB_FN (bb, cfun)
5667 : : {
5668 : 14598949 : rtx_insn *end = BB_END (bb);
5669 : 14598949 : if (!JUMP_P (end))
5670 : 5707472 : continue;
5671 : 8891477 : extract_insn (end);
5672 : 24013183 : for (int i = 0; i < recog_data.n_operands; i++)
5673 : 15121850 : if (recog_data.operand_type[i] != OP_IN)
5674 : : {
5675 : 144 : bool skip_p = false;
5676 : 494 : FOR_EACH_EDGE (e, ei, bb->succs)
5677 : 711 : if (EDGE_CRITICAL_P (e)
5678 : 11 : && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
5679 : 361 : && (e->flags & EDGE_ABNORMAL))
5680 : : {
5681 : : skip_p = true;
5682 : : break;
5683 : : }
5684 : 144 : if (skip_p)
5685 : : break;
5686 : 144 : output_jump_reload_p = true;
5687 : 494 : FOR_EACH_EDGE (e, ei, bb->succs)
5688 : 711 : if (EDGE_CRITICAL_P (e)
5689 : 361 : && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
5690 : : {
5691 : 11 : start_sequence ();
5692 : : /* We need to put some no-op insn here. We can
5693 : : not put a note as commit_edges insertion will
5694 : : fail. */
5695 : 11 : emit_insn (gen_rtx_USE (VOIDmode, const1_rtx));
5696 : 11 : rtx_insn *insns = end_sequence ();
5697 : 11 : insert_insn_on_edge (insns, e);
5698 : : }
5699 : : break;
5700 : : }
5701 : : }
5702 : 1481332 : if (output_jump_reload_p)
5703 : 138 : commit_edge_insertions ();
5704 : : }
5705 : :
5706 : 1481332 : if (flag_ira_verbose < 10)
5707 : : {
5708 : 1481332 : internal_flag_ira_verbose = flag_ira_verbose;
5709 : 1481332 : ira_dump_file = f;
5710 : : }
5711 : : else
5712 : : {
5713 : 0 : internal_flag_ira_verbose = flag_ira_verbose - 10;
5714 : 0 : ira_dump_file = stderr;
5715 : : }
5716 : :
5717 : 1481332 : clear_bb_flags ();
5718 : :
5719 : : /* Determine if the current function is a leaf before running IRA
5720 : : since this can impact optimizations done by the prologue and
5721 : : epilogue thus changing register elimination offsets.
5722 : : Other target callbacks may use crtl->is_leaf too, including
5723 : : SHRINK_WRAPPING_ENABLED, so initialize as early as possible. */
5724 : 1481332 : crtl->is_leaf = leaf_function_p ();
5725 : :
5726 : : /* Perform target specific PIC register initialization. */
5727 : 1481332 : targetm.init_pic_reg ();
5728 : :
5729 : 1481332 : ira_conflicts_p = optimize > 0;
5730 : :
5731 : : /* Determine the number of pseudos actually requiring coloring. */
5732 : 1481332 : unsigned int num_used_regs = 0;
5733 : 67103502 : for (unsigned int i = FIRST_PSEUDO_REGISTER; i < DF_REG_SIZE (df); i++)
5734 : 65622170 : if (DF_REG_DEF_COUNT (i) || DF_REG_USE_COUNT (i))
5735 : 30167463 : num_used_regs++;
5736 : :
5737 : : /* If there are too many pseudos and/or basic blocks (e.g. 10K pseudos and
5738 : : 10K blocks or 100K pseudos and 1K blocks) or we have too many function
5739 : : insns, we will use simplified and faster algorithms in LRA. */
5740 : 1481332 : lra_simple_p
5741 : 1481332 : = (ira_use_lra_p
5742 : 1481332 : && (num_used_regs >= (1U << 26) / last_basic_block_for_fn (cfun)
5743 : : /* max uid is a good evaluation of the number of insns as most
5744 : : optimizations are done on tree-SSA level. */
5745 : 1481327 : || ((uint64_t) get_max_uid ()
5746 : 1481327 : > (uint64_t) param_ira_simple_lra_insn_threshold * 1000)));
5747 : :
5748 : 1481332 : if (lra_simple_p)
5749 : : {
5750 : : /* It permits to skip live range splitting in LRA. */
5751 : 5 : flag_caller_saves = false;
5752 : : /* There is no sense to do regional allocation when we use
5753 : : simplified LRA. */
5754 : 5 : flag_ira_region = IRA_REGION_ONE;
5755 : 5 : ira_conflicts_p = false;
5756 : : }
5757 : :
5758 : : #ifndef IRA_NO_OBSTACK
5759 : : gcc_obstack_init (&ira_obstack);
5760 : : #endif
5761 : 1481332 : bitmap_obstack_initialize (&ira_bitmap_obstack);
5762 : :
5763 : : /* LRA uses its own infrastructure to handle caller save registers. */
5764 : 1481332 : if (flag_caller_saves && !ira_use_lra_p)
5765 : 0 : init_caller_save ();
5766 : :
5767 : 1481332 : setup_prohibited_mode_move_regs ();
5768 : 1481332 : decrease_live_ranges_number ();
5769 : 1481332 : df_note_add_problem ();
5770 : :
5771 : : /* DF_LIVE can't be used in the register allocator, too many other
5772 : : parts of the compiler depend on using the "classic" liveness
5773 : : interpretation of the DF_LR problem. See PR38711.
5774 : : Remove the problem, so that we don't spend time updating it in
5775 : : any of the df_analyze() calls during IRA/LRA. */
5776 : 1481332 : if (optimize > 1)
5777 : 966092 : df_remove_problem (df_live);
5778 : 1481332 : gcc_checking_assert (df_live == NULL);
5779 : :
5780 : 1481332 : if (flag_checking)
5781 : 1481312 : df->changeable_flags |= DF_VERIFY_SCHEDULED;
5782 : :
5783 : 1481332 : df_analyze ();
5784 : :
5785 : 1481332 : init_reg_equiv ();
5786 : 1481332 : if (ira_conflicts_p)
5787 : : {
5788 : 1044976 : calculate_dominance_info (CDI_DOMINATORS);
5789 : :
5790 : 1044976 : if (split_live_ranges_for_shrink_wrap ())
5791 : 27082 : df_analyze ();
5792 : :
5793 : 1044976 : free_dominance_info (CDI_DOMINATORS);
5794 : : }
5795 : :
5796 : 1481332 : df_clear_flags (DF_NO_INSN_RESCAN);
5797 : :
5798 : 1481332 : indirect_jump_optimize ();
5799 : 1481332 : if (delete_trivially_dead_insns (get_insns (), max_reg_num ()))
5800 : 5388 : df_analyze ();
5801 : :
5802 : 1481332 : regstat_init_n_sets_and_refs ();
5803 : 1481332 : regstat_compute_ri ();
5804 : :
5805 : : /* If we are not optimizing, then this is the only place before
5806 : : register allocation where dataflow is done. And that is needed
5807 : : to generate these warnings. */
5808 : 1481332 : if (warn_clobbered)
5809 : 134458 : generate_setjmp_warnings ();
5810 : :
5811 : : /* update_equiv_regs can use reg classes of pseudos and they are set up in
5812 : : register pressure sensitive scheduling and loop invariant motion and in
5813 : : live range shrinking. This info can become obsolete if we add new pseudos
5814 : : since the last set up. Recalculate it again if the new pseudos were
5815 : : added. */
5816 : 1481332 : if (resize_reg_info () && (flag_sched_pressure || flag_live_range_shrinkage
5817 : 1481237 : || flag_ira_loop_pressure))
5818 : 43 : ira_set_pseudo_classes (true, ira_dump_file);
5819 : :
5820 : 1481332 : init_alias_analysis ();
5821 : 1481332 : loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
5822 : 1481332 : reg_equiv = XCNEWVEC (struct equivalence, max_reg_num ());
5823 : 1481332 : update_equiv_regs_prescan ();
5824 : 1481332 : update_equiv_regs ();
5825 : :
5826 : : /* Don't move insns if live range shrinkage or register
5827 : : pressure-sensitive scheduling were done because it will not
5828 : : improve allocation but likely worsen insn scheduling. */
5829 : 1481332 : if (optimize
5830 : 1044976 : && !flag_live_range_shrinkage
5831 : 1044945 : && !(flag_sched_pressure && flag_schedule_insns))
5832 : 1044923 : combine_and_move_insns ();
5833 : :
5834 : : /* Gather additional equivalences with memory. */
5835 : 1481332 : if (optimize && flag_expensive_optimizations)
5836 : 966055 : add_store_equivs ();
5837 : :
5838 : 1481332 : loop_optimizer_finalize ();
5839 : 1481332 : free_dominance_info (CDI_DOMINATORS);
5840 : 1481332 : end_alias_analysis ();
5841 : 1481332 : free (reg_equiv);
5842 : :
5843 : : /* Once max_regno changes, we need to free and re-init/re-compute
5844 : : some data structures like regstat_n_sets_and_refs and reg_info_p. */
5845 : 1554572 : auto regstat_recompute_for_max_regno = []() {
5846 : 73240 : regstat_free_n_sets_and_refs ();
5847 : 73240 : regstat_free_ri ();
5848 : 73240 : regstat_init_n_sets_and_refs ();
5849 : 73240 : regstat_compute_ri ();
5850 : 73240 : resize_reg_info ();
5851 : 73240 : };
5852 : :
5853 : 1481332 : int max_regno_before_rm = max_reg_num ();
5854 : 1481332 : if (ira_use_lra_p && remove_scratches ())
5855 : : {
5856 : 37972 : ira_expand_reg_equiv ();
5857 : : /* For now remove_scatches is supposed to create pseudos when it
5858 : : succeeds, assert this happens all the time. Once it doesn't
5859 : : hold, we should guard the regstat recompute for the case
5860 : : max_regno changes. */
5861 : 37972 : gcc_assert (max_regno_before_rm != max_reg_num ());
5862 : 37972 : regstat_recompute_for_max_regno ();
5863 : : }
5864 : :
5865 : 1481332 : setup_reg_equiv ();
5866 : 1481332 : grow_reg_equivs ();
5867 : 1481332 : setup_reg_equiv_init ();
5868 : :
5869 : 1481332 : allocated_reg_info_size = max_reg_num ();
5870 : :
5871 : : /* It is not worth to do such improvement when we use a simple
5872 : : allocation because of -O0 usage or because the function is too
5873 : : big. */
5874 : 1481332 : if (ira_conflicts_p)
5875 : 1044976 : find_moveable_pseudos ();
5876 : :
5877 : 1481332 : max_regno_before_ira = max_reg_num ();
5878 : 1481332 : ira_setup_eliminable_regset ();
5879 : :
5880 : 1481332 : ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
5881 : 1481332 : ira_load_cost = ira_store_cost = ira_shuffle_cost = 0;
5882 : 1481332 : ira_move_loops_num = ira_additional_jumps_num = 0;
5883 : :
5884 : 1481332 : ira_assert (current_loops == NULL);
5885 : 1481332 : if (flag_ira_region == IRA_REGION_ALL || flag_ira_region == IRA_REGION_MIXED)
5886 : 999848 : loop_optimizer_init (AVOID_CFG_MODIFICATIONS | LOOPS_HAVE_RECORDED_EXITS);
5887 : :
5888 : 1481332 : if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
5889 : 95 : fprintf (ira_dump_file, "Building IRA IR\n");
5890 : 1481332 : loops_p = ira_build ();
5891 : :
5892 : 1481332 : ira_assert (ira_conflicts_p || !loops_p);
5893 : :
5894 : 1481332 : saved_flag_ira_share_spill_slots = flag_ira_share_spill_slots;
5895 : 1481332 : if (too_high_register_pressure_p () || cfun->calls_setjmp)
5896 : : /* It is just wasting compiler's time to pack spilled pseudos into
5897 : : stack slots in this case -- prohibit it. We also do this if
5898 : : there is setjmp call because a variable not modified between
5899 : : setjmp and longjmp the compiler is required to preserve its
5900 : : value and sharing slots does not guarantee it. */
5901 : 1300 : flag_ira_share_spill_slots = false;
5902 : :
5903 : 1481332 : ira_color ();
5904 : :
5905 : 1481332 : ira_max_point_before_emit = ira_max_point;
5906 : :
5907 : 1481332 : ira_initiate_emit_data ();
5908 : :
5909 : 1481332 : ira_emit (loops_p);
5910 : :
5911 : 1481332 : max_regno = max_reg_num ();
5912 : 1481332 : if (ira_conflicts_p)
5913 : : {
5914 : 1044976 : if (! loops_p)
5915 : : {
5916 : 1009306 : if (! ira_use_lra_p)
5917 : 0 : ira_initiate_assign ();
5918 : : }
5919 : : else
5920 : : {
5921 : 35670 : expand_reg_info ();
5922 : :
5923 : 35670 : if (ira_use_lra_p)
5924 : : {
5925 : 35670 : ira_allocno_t a;
5926 : 35670 : ira_allocno_iterator ai;
5927 : :
5928 : 11658799 : FOR_EACH_ALLOCNO (a, ai)
5929 : : {
5930 : 11587459 : int old_regno = ALLOCNO_REGNO (a);
5931 : 11587459 : int new_regno = REGNO (ALLOCNO_EMIT_DATA (a)->reg);
5932 : :
5933 : 11587459 : ALLOCNO_REGNO (a) = new_regno;
5934 : :
5935 : 11587459 : if (old_regno != new_regno)
5936 : 1253947 : setup_reg_classes (new_regno, reg_preferred_class (old_regno),
5937 : : reg_alternate_class (old_regno),
5938 : : reg_allocno_class (old_regno));
5939 : : }
5940 : : }
5941 : : else
5942 : : {
5943 : 0 : if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
5944 : 0 : fprintf (ira_dump_file, "Flattening IR\n");
5945 : 0 : ira_flattening (max_regno_before_ira, ira_max_point_before_emit);
5946 : : }
5947 : : /* New insns were generated: add notes and recalculate live
5948 : : info. */
5949 : 35670 : df_analyze ();
5950 : :
5951 : : /* ??? Rebuild the loop tree, but why? Does the loop tree
5952 : : change if new insns were generated? Can that be handled
5953 : : by updating the loop tree incrementally? */
5954 : 35670 : loop_optimizer_finalize ();
5955 : 35670 : free_dominance_info (CDI_DOMINATORS);
5956 : 35670 : loop_optimizer_init (AVOID_CFG_MODIFICATIONS
5957 : : | LOOPS_HAVE_RECORDED_EXITS);
5958 : :
5959 : 35670 : if (! ira_use_lra_p)
5960 : : {
5961 : 0 : setup_allocno_assignment_flags ();
5962 : 0 : ira_initiate_assign ();
5963 : 0 : ira_reassign_conflict_allocnos (max_regno);
5964 : : }
5965 : : }
5966 : : }
5967 : :
5968 : 1481332 : ira_finish_emit_data ();
5969 : :
5970 : 1481332 : setup_reg_renumber ();
5971 : :
5972 : 1481332 : calculate_allocation_cost ();
5973 : :
5974 : : #ifdef ENABLE_IRA_CHECKING
5975 : 1481332 : if (ira_conflicts_p && ! ira_use_lra_p)
5976 : : /* Opposite to reload pass, LRA does not use any conflict info
5977 : : from IRA. We don't rebuild conflict info for LRA (through
5978 : : ira_flattening call) and cannot use the check here. We could
5979 : : rebuild this info for LRA in the check mode but there is a risk
5980 : : that code generated with the check and without it will be a bit
5981 : : different. Calling ira_flattening in any mode would be a
5982 : : wasting CPU time. So do not check the allocation for LRA. */
5983 : 0 : check_allocation ();
5984 : : #endif
5985 : :
5986 : 1481332 : if (max_regno != max_regno_before_ira)
5987 : 35268 : regstat_recompute_for_max_regno ();
5988 : :
5989 : 1481332 : overall_cost_before = ira_overall_cost;
5990 : 1481332 : if (! ira_conflicts_p)
5991 : 436356 : grow_reg_equivs ();
5992 : : else
5993 : : {
5994 : 1044976 : fix_reg_equiv_init ();
5995 : :
5996 : : #ifdef ENABLE_IRA_CHECKING
5997 : 1044976 : print_redundant_copies ();
5998 : : #endif
5999 : 1044976 : if (! ira_use_lra_p)
6000 : : {
6001 : 0 : ira_spilled_reg_stack_slots_num = 0;
6002 : 0 : ira_spilled_reg_stack_slots
6003 : 0 : = ((class ira_spilled_reg_stack_slot *)
6004 : 0 : ira_allocate (max_regno
6005 : : * sizeof (class ira_spilled_reg_stack_slot)));
6006 : 0 : memset ((void *)ira_spilled_reg_stack_slots, 0,
6007 : 0 : max_regno * sizeof (class ira_spilled_reg_stack_slot));
6008 : : }
6009 : : }
6010 : 1481332 : allocate_initial_values ();
6011 : :
6012 : : /* See comment for find_moveable_pseudos call. */
6013 : 1481332 : if (ira_conflicts_p)
6014 : 1044976 : move_unallocated_pseudos ();
6015 : :
6016 : : /* Restore original values. */
6017 : 1481332 : if (lra_simple_p)
6018 : : {
6019 : 5 : flag_caller_saves = saved_flag_caller_saves;
6020 : 5 : flag_ira_region = saved_flag_ira_region;
6021 : : }
6022 : 1481332 : }
6023 : :
6024 : : /* Modify asm goto to avoid further trouble with this insn. We can
6025 : : not replace the insn by USE as in other asm insns as we still
6026 : : need to keep CFG consistency. */
6027 : : void
6028 : 6 : ira_nullify_asm_goto (rtx_insn *insn)
6029 : : {
6030 : 6 : ira_assert (JUMP_P (insn) && INSN_CODE (insn) < 0);
6031 : 6 : rtx tmp = extract_asm_operands (PATTERN (insn));
6032 : 6 : PATTERN (insn) = gen_rtx_ASM_OPERANDS (VOIDmode, ggc_strdup (""), "", 0,
6033 : : rtvec_alloc (0),
6034 : : rtvec_alloc (0),
6035 : : ASM_OPERANDS_LABEL_VEC (tmp),
6036 : : ASM_OPERANDS_SOURCE_LOCATION(tmp));
6037 : 6 : }
6038 : :
6039 : : static void
6040 : 1481332 : do_reload (void)
6041 : : {
6042 : 1481332 : basic_block bb;
6043 : 1481332 : bool need_dce;
6044 : 1481332 : unsigned pic_offset_table_regno = INVALID_REGNUM;
6045 : :
6046 : 1481332 : if (flag_ira_verbose < 10)
6047 : 1481332 : ira_dump_file = dump_file;
6048 : :
6049 : : /* If pic_offset_table_rtx is a pseudo register, then keep it so
6050 : : after reload to avoid possible wrong usages of hard reg assigned
6051 : : to it. */
6052 : 1481332 : if (pic_offset_table_rtx
6053 : 1481332 : && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
6054 : : pic_offset_table_regno = REGNO (pic_offset_table_rtx);
6055 : :
6056 : 1481332 : timevar_push (TV_RELOAD);
6057 : 1481332 : if (ira_use_lra_p)
6058 : : {
6059 : 1481332 : if (current_loops != NULL)
6060 : : {
6061 : 999848 : loop_optimizer_finalize ();
6062 : 999848 : free_dominance_info (CDI_DOMINATORS);
6063 : : }
6064 : 19138442 : FOR_ALL_BB_FN (bb, cfun)
6065 : 17657110 : bb->loop_father = NULL;
6066 : 1481332 : current_loops = NULL;
6067 : :
6068 : 1481332 : ira_destroy ();
6069 : :
6070 : 1481332 : lra (ira_dump_file, internal_flag_ira_verbose);
6071 : : /* ???!!! Move it before lra () when we use ira_reg_equiv in
6072 : : LRA. */
6073 : 1481332 : vec_free (reg_equivs);
6074 : 1481332 : reg_equivs = NULL;
6075 : 1481332 : need_dce = false;
6076 : : }
6077 : : else
6078 : : {
6079 : 0 : df_set_flags (DF_NO_INSN_RESCAN);
6080 : 0 : build_insn_chain ();
6081 : :
6082 : 0 : need_dce = reload (get_insns (), ira_conflicts_p);
6083 : : }
6084 : :
6085 : 1481332 : timevar_pop (TV_RELOAD);
6086 : :
6087 : 1481332 : timevar_push (TV_IRA);
6088 : :
6089 : 1481332 : if (ira_conflicts_p && ! ira_use_lra_p)
6090 : : {
6091 : 0 : ira_free (ira_spilled_reg_stack_slots);
6092 : 0 : ira_finish_assign ();
6093 : : }
6094 : :
6095 : 1481332 : if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL
6096 : 96 : && overall_cost_before != ira_overall_cost)
6097 : 0 : fprintf (ira_dump_file, "+++Overall after reload %" PRId64 "\n",
6098 : : ira_overall_cost);
6099 : :
6100 : 1481332 : flag_ira_share_spill_slots = saved_flag_ira_share_spill_slots;
6101 : :
6102 : 1481332 : if (! ira_use_lra_p)
6103 : : {
6104 : 0 : ira_destroy ();
6105 : 0 : if (current_loops != NULL)
6106 : : {
6107 : 0 : loop_optimizer_finalize ();
6108 : 0 : free_dominance_info (CDI_DOMINATORS);
6109 : : }
6110 : 0 : FOR_ALL_BB_FN (bb, cfun)
6111 : 0 : bb->loop_father = NULL;
6112 : 0 : current_loops = NULL;
6113 : :
6114 : 0 : regstat_free_ri ();
6115 : 0 : regstat_free_n_sets_and_refs ();
6116 : : }
6117 : :
6118 : 1481332 : if (optimize)
6119 : 1044976 : cleanup_cfg (CLEANUP_EXPENSIVE);
6120 : :
6121 : 1481332 : finish_reg_equiv ();
6122 : :
6123 : 1481332 : bitmap_obstack_release (&ira_bitmap_obstack);
6124 : : #ifndef IRA_NO_OBSTACK
6125 : : obstack_free (&ira_obstack, NULL);
6126 : : #endif
6127 : :
6128 : : /* The code after the reload has changed so much that at this point
6129 : : we might as well just rescan everything. Note that
6130 : : df_rescan_all_insns is not going to help here because it does not
6131 : : touch the artificial uses and defs. */
6132 : 1481332 : df_finish_pass (true);
6133 : 1481332 : df_scan_alloc (NULL);
6134 : 1481332 : df_scan_blocks ();
6135 : :
6136 : 1481332 : if (optimize > 1)
6137 : : {
6138 : 966092 : df_live_add_problem ();
6139 : 966092 : df_live_set_all_dirty ();
6140 : : }
6141 : :
6142 : 1481332 : if (optimize)
6143 : 1044976 : df_analyze ();
6144 : :
6145 : 1481332 : if (need_dce && optimize)
6146 : 0 : run_fast_dce ();
6147 : :
6148 : : /* Diagnose uses of the hard frame pointer when it is used as a global
6149 : : register. Often we can get away with letting the user appropriate
6150 : : the frame pointer, but we should let them know when code generation
6151 : : makes that impossible. */
6152 : 1481332 : if (global_regs[HARD_FRAME_POINTER_REGNUM] && frame_pointer_needed)
6153 : : {
6154 : 2 : tree decl = global_regs_decl[HARD_FRAME_POINTER_REGNUM];
6155 : 2 : error_at (DECL_SOURCE_LOCATION (current_function_decl),
6156 : : "frame pointer required, but reserved");
6157 : 2 : inform (DECL_SOURCE_LOCATION (decl), "for %qD", decl);
6158 : : }
6159 : :
6160 : : /* If we are doing generic stack checking, give a warning if this
6161 : : function's frame size is larger than we expect. */
6162 : 1481332 : if (flag_stack_check == GENERIC_STACK_CHECK)
6163 : : {
6164 : 41 : poly_int64 size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
6165 : :
6166 : 3813 : for (int i = 0; i < FIRST_PSEUDO_REGISTER; i++)
6167 : 3772 : if (df_regs_ever_live_p (i)
6168 : 231 : && !fixed_regs[i]
6169 : 3939 : && !crtl->abi->clobbers_full_reg_p (i))
6170 : 82 : size += UNITS_PER_WORD;
6171 : :
6172 : 41 : if (constant_lower_bound (size) > STACK_CHECK_MAX_FRAME_SIZE)
6173 : 1 : warning (0, "frame size too large for reliable stack checking");
6174 : : }
6175 : :
6176 : 1481332 : if (pic_offset_table_regno != INVALID_REGNUM)
6177 : 79715 : pic_offset_table_rtx = gen_rtx_REG (Pmode, pic_offset_table_regno);
6178 : :
6179 : 1481332 : timevar_pop (TV_IRA);
6180 : 1481332 : }
6181 : :
6182 : : /* Run the integrated register allocator. */
6183 : :
6184 : : namespace {
6185 : :
6186 : : const pass_data pass_data_ira =
6187 : : {
6188 : : RTL_PASS, /* type */
6189 : : "ira", /* name */
6190 : : OPTGROUP_NONE, /* optinfo_flags */
6191 : : TV_IRA, /* tv_id */
6192 : : 0, /* properties_required */
6193 : : 0, /* properties_provided */
6194 : : 0, /* properties_destroyed */
6195 : : 0, /* todo_flags_start */
6196 : : TODO_do_not_ggc_collect, /* todo_flags_finish */
6197 : : };
6198 : :
6199 : : class pass_ira : public rtl_opt_pass
6200 : : {
6201 : : public:
6202 : 285689 : pass_ira (gcc::context *ctxt)
6203 : 571378 : : rtl_opt_pass (pass_data_ira, ctxt)
6204 : : {}
6205 : :
6206 : : /* opt_pass methods: */
6207 : 1481340 : bool gate (function *) final override
6208 : : {
6209 : 1481340 : return !targetm.no_register_allocation;
6210 : : }
6211 : 1481332 : unsigned int execute (function *) final override
6212 : : {
6213 : 1481332 : ira_in_progress = true;
6214 : 1481332 : ira (dump_file);
6215 : 1481332 : ira_in_progress = false;
6216 : 1481332 : return 0;
6217 : : }
6218 : :
6219 : : }; // class pass_ira
6220 : :
6221 : : } // anon namespace
6222 : :
6223 : : rtl_opt_pass *
6224 : 285689 : make_pass_ira (gcc::context *ctxt)
6225 : : {
6226 : 285689 : return new pass_ira (ctxt);
6227 : : }
6228 : :
6229 : : namespace {
6230 : :
6231 : : const pass_data pass_data_reload =
6232 : : {
6233 : : RTL_PASS, /* type */
6234 : : "reload", /* name */
6235 : : OPTGROUP_NONE, /* optinfo_flags */
6236 : : TV_RELOAD, /* tv_id */
6237 : : 0, /* properties_required */
6238 : : 0, /* properties_provided */
6239 : : 0, /* properties_destroyed */
6240 : : 0, /* todo_flags_start */
6241 : : 0, /* todo_flags_finish */
6242 : : };
6243 : :
6244 : : class pass_reload : public rtl_opt_pass
6245 : : {
6246 : : public:
6247 : 285689 : pass_reload (gcc::context *ctxt)
6248 : 571378 : : rtl_opt_pass (pass_data_reload, ctxt)
6249 : : {}
6250 : :
6251 : : /* opt_pass methods: */
6252 : 1481340 : bool gate (function *) final override
6253 : : {
6254 : 1481340 : return !targetm.no_register_allocation;
6255 : : }
6256 : 1481332 : unsigned int execute (function *) final override
6257 : : {
6258 : 1481332 : do_reload ();
6259 : 1481332 : return 0;
6260 : : }
6261 : :
6262 : : }; // class pass_reload
6263 : :
6264 : : } // anon namespace
6265 : :
6266 : : rtl_opt_pass *
6267 : 285689 : make_pass_reload (gcc::context *ctxt)
6268 : : {
6269 : 285689 : return new pass_reload (ctxt);
6270 : : }
|