OSDN Git Service

compiler: Prohibit comparisons of funcs, maps, and slices to non-nil.
[pf3gnuchains/gcc-fork.git] / gcc / ira-int.h
1 /* Integrated Register Allocator (IRA) intercommunication header file.
2    Copyright (C) 2006, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4    Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "cfgloop.h"
23 #include "ira.h"
24 #include "alloc-pool.h"
25
26 /* To provide consistency in naming, all IRA external variables,
27    functions, common typedefs start with prefix ira_.  */
28
29 #ifdef ENABLE_CHECKING
30 #define ENABLE_IRA_CHECKING
31 #endif
32
33 #ifdef ENABLE_IRA_CHECKING
34 #define ira_assert(c) gcc_assert (c)
35 #else
36 /* Always define and include C, so that warnings for empty body in an
37   ‘if’ statement and unused variable do not occur.  */
38 #define ira_assert(c) ((void)(0 && (c)))
39 #endif
40
41 /* Compute register frequency from edge frequency FREQ.  It is
42    analogous to REG_FREQ_FROM_BB.  When optimizing for size, or
43    profile driven feedback is available and the function is never
44    executed, frequency is always equivalent.  Otherwise rescale the
45    edge frequency.  */
46 #define REG_FREQ_FROM_EDGE_FREQ(freq)                                      \
47   (optimize_size || (flag_branch_probabilities && !ENTRY_BLOCK_PTR->count) \
48    ? REG_FREQ_MAX : (freq * REG_FREQ_MAX / BB_FREQ_MAX)                    \
49    ? (freq * REG_FREQ_MAX / BB_FREQ_MAX) : 1)
50
51 /* All natural loops.  */
52 extern struct loops ira_loops;
53
54 /* A modified value of flag `-fira-verbose' used internally.  */
55 extern int internal_flag_ira_verbose;
56
57 /* Dump file of the allocator if it is not NULL.  */
58 extern FILE *ira_dump_file;
59
60 /* Typedefs for pointers to allocno live range, allocno, and copy of
61    allocnos.  */
62 typedef struct live_range *live_range_t;
63 typedef struct ira_allocno *ira_allocno_t;
64 typedef struct ira_allocno_copy *ira_copy_t;
65 typedef struct ira_object *ira_object_t;
66
67 /* Definition of vector of allocnos and copies.  */
68 DEF_VEC_P(ira_allocno_t);
69 DEF_VEC_ALLOC_P(ira_allocno_t, heap);
70 DEF_VEC_P(ira_object_t);
71 DEF_VEC_ALLOC_P(ira_object_t, heap);
72 DEF_VEC_P(ira_copy_t);
73 DEF_VEC_ALLOC_P(ira_copy_t, heap);
74
75 /* Typedef for pointer to the subsequent structure.  */
76 typedef struct ira_loop_tree_node *ira_loop_tree_node_t;
77
78 /* In general case, IRA is a regional allocator.  The regions are
79    nested and form a tree.  Currently regions are natural loops.  The
80    following structure describes loop tree node (representing basic
81    block or loop).  We need such tree because the loop tree from
82    cfgloop.h is not convenient for the optimization: basic blocks are
83    not a part of the tree from cfgloop.h.  We also use the nodes for
84    storing additional information about basic blocks/loops for the
85    register allocation purposes.  */
86 struct ira_loop_tree_node
87 {
88   /* The node represents basic block if children == NULL.  */
89   basic_block bb;    /* NULL for loop.  */
90   struct loop *loop; /* NULL for BB.  */
91   /* NEXT/SUBLOOP_NEXT is the next node/loop-node of the same parent.
92      SUBLOOP_NEXT is always NULL for BBs.  */
93   ira_loop_tree_node_t subloop_next, next;
94   /* CHILDREN/SUBLOOPS is the first node/loop-node immediately inside
95      the node.  They are NULL for BBs.  */
96   ira_loop_tree_node_t subloops, children;
97   /* The node immediately containing given node.  */
98   ira_loop_tree_node_t parent;
99
100   /* Loop level in range [0, ira_loop_tree_height).  */
101   int level;
102
103   /* All the following members are defined only for nodes representing
104      loops.  */
105
106   /* True if the loop was marked for removal from the register
107      allocation.  */
108   bool to_remove_p;
109
110   /* Allocnos in the loop corresponding to their regnos.  If it is
111      NULL the loop does not form a separate register allocation region
112      (e.g. because it has abnormal enter/exit edges and we can not put
113      code for register shuffling on the edges if a different
114      allocation is used for a pseudo-register on different sides of
115      the edges).  Caps are not in the map (remember we can have more
116      one cap with the same regno in a region).  */
117   ira_allocno_t *regno_allocno_map;
118
119   /* True if there is an entry to given loop not from its parent (or
120      grandparent) basic block.  For example, it is possible for two
121      adjacent loops inside another loop.  */
122   bool entered_from_non_parent_p;
123
124   /* Maximal register pressure inside loop for given register class
125      (defined only for the pressure classes).  */
126   int reg_pressure[N_REG_CLASSES];
127
128   /* Numbers of allocnos referred or living in the loop node (except
129      for its subloops).  */
130   bitmap all_allocnos;
131
132   /* Numbers of allocnos living at the loop borders.  */
133   bitmap border_allocnos;
134
135   /* Regnos of pseudos modified in the loop node (including its
136      subloops).  */
137   bitmap modified_regnos;
138
139   /* Numbers of copies referred in the corresponding loop.  */
140   bitmap local_copies;
141 };
142
143 /* The root of the loop tree corresponding to the all function.  */
144 extern ira_loop_tree_node_t ira_loop_tree_root;
145
146 /* Height of the loop tree.  */
147 extern int ira_loop_tree_height;
148
149 /* All nodes representing basic blocks are referred through the
150    following array.  We can not use basic block member `aux' for this
151    because it is used for insertion of insns on edges.  */
152 extern ira_loop_tree_node_t ira_bb_nodes;
153
154 /* Two access macros to the nodes representing basic blocks.  */
155 #if defined ENABLE_IRA_CHECKING && (GCC_VERSION >= 2007)
156 #define IRA_BB_NODE_BY_INDEX(index) __extension__                       \
157 (({ ira_loop_tree_node_t _node = (&ira_bb_nodes[index]);        \
158      if (_node->children != NULL || _node->loop != NULL || _node->bb == NULL)\
159        {                                                                \
160          fprintf (stderr,                                               \
161                   "\n%s: %d: error in %s: it is not a block node\n",    \
162                   __FILE__, __LINE__, __FUNCTION__);                    \
163          gcc_unreachable ();                                            \
164        }                                                                \
165      _node; }))
166 #else
167 #define IRA_BB_NODE_BY_INDEX(index) (&ira_bb_nodes[index])
168 #endif
169
170 #define IRA_BB_NODE(bb) IRA_BB_NODE_BY_INDEX ((bb)->index)
171
172 /* All nodes representing loops are referred through the following
173    array.  */
174 extern ira_loop_tree_node_t ira_loop_nodes;
175
176 /* Two access macros to the nodes representing loops.  */
177 #if defined ENABLE_IRA_CHECKING && (GCC_VERSION >= 2007)
178 #define IRA_LOOP_NODE_BY_INDEX(index) __extension__                     \
179 (({ ira_loop_tree_node_t const _node = (&ira_loop_nodes[index]);\
180      if (_node->children == NULL || _node->bb != NULL || _node->loop == NULL)\
181        {                                                                \
182          fprintf (stderr,                                               \
183                   "\n%s: %d: error in %s: it is not a loop node\n",     \
184                   __FILE__, __LINE__, __FUNCTION__);                    \
185          gcc_unreachable ();                                            \
186        }                                                                \
187      _node; }))
188 #else
189 #define IRA_LOOP_NODE_BY_INDEX(index) (&ira_loop_nodes[index])
190 #endif
191
192 #define IRA_LOOP_NODE(loop) IRA_LOOP_NODE_BY_INDEX ((loop)->num)
193
194 \f
195 /* The structure describes program points where a given allocno lives.
196    If the live ranges of two allocnos are intersected, the allocnos
197    are in conflict.  */
198 struct live_range
199 {
200   /* Object whose live range is described by given structure.  */
201   ira_object_t object;
202   /* Program point range.  */
203   int start, finish;
204   /* Next structure describing program points where the allocno
205      lives.  */
206   live_range_t next;
207   /* Pointer to structures with the same start/finish.  */
208   live_range_t start_next, finish_next;
209 };
210
211 /* Program points are enumerated by numbers from range
212    0..IRA_MAX_POINT-1.  There are approximately two times more program
213    points than insns.  Program points are places in the program where
214    liveness info can be changed.  In most general case (there are more
215    complicated cases too) some program points correspond to places
216    where input operand dies and other ones correspond to places where
217    output operands are born.  */
218 extern int ira_max_point;
219
220 /* Arrays of size IRA_MAX_POINT mapping a program point to the allocno
221    live ranges with given start/finish point.  */
222 extern live_range_t *ira_start_point_ranges, *ira_finish_point_ranges;
223
224 /* A structure representing conflict information for an allocno
225    (or one of its subwords).  */
226 struct ira_object
227 {
228   /* The allocno associated with this record.  */
229   ira_allocno_t allocno;
230   /* Vector of accumulated conflicting conflict_redords with NULL end
231      marker (if OBJECT_CONFLICT_VEC_P is true) or conflict bit vector
232      otherwise.  */
233   void *conflicts_array;
234   /* Pointer to structures describing at what program point the
235      object lives.  We always maintain the list in such way that *the
236      ranges in the list are not intersected and ordered by decreasing
237      their program points*.  */
238   live_range_t live_ranges;
239   /* The subword within ALLOCNO which is represented by this object.
240      Zero means the lowest-order subword (or the entire allocno in case
241      it is not being tracked in subwords).  */
242   int subword;
243   /* Allocated size of the conflicts array.  */
244   unsigned int conflicts_array_size;
245   /* A unique number for every instance of this structure, which is used
246      to represent it in conflict bit vectors.  */
247   int id;
248   /* Before building conflicts, MIN and MAX are initialized to
249      correspondingly minimal and maximal points of the accumulated
250      live ranges.  Afterwards, they hold the minimal and maximal ids
251      of other ira_objects that this one can conflict with.  */
252   int min, max;
253   /* Initial and accumulated hard registers conflicting with this
254      object and as a consequences can not be assigned to the allocno.
255      All non-allocatable hard regs and hard regs of register classes
256      different from given allocno one are included in the sets.  */
257   HARD_REG_SET conflict_hard_regs, total_conflict_hard_regs;
258   /* Number of accumulated conflicts in the vector of conflicting
259      objects.  */
260   int num_accumulated_conflicts;
261   /* TRUE if conflicts are represented by a vector of pointers to
262      ira_object structures.  Otherwise, we use a bit vector indexed
263      by conflict ID numbers.  */
264   unsigned int conflict_vec_p : 1;
265 };
266
267 /* A structure representing an allocno (allocation entity).  Allocno
268    represents a pseudo-register in an allocation region.  If
269    pseudo-register does not live in a region but it lives in the
270    nested regions, it is represented in the region by special allocno
271    called *cap*.  There may be more one cap representing the same
272    pseudo-register in region.  It means that the corresponding
273    pseudo-register lives in more one non-intersected subregion.  */
274 struct ira_allocno
275 {
276   /* The allocno order number starting with 0.  Each allocno has an
277      unique number and the number is never changed for the
278      allocno.  */
279   int num;
280   /* Regno for allocno or cap.  */
281   int regno;
282   /* Mode of the allocno which is the mode of the corresponding
283      pseudo-register.  */
284   ENUM_BITFIELD (machine_mode) mode : 8;
285   /* Register class which should be used for allocation for given
286      allocno.  NO_REGS means that we should use memory.  */
287   ENUM_BITFIELD (reg_class) aclass : 16;
288   /* During the reload, value TRUE means that we should not reassign a
289      hard register to the allocno got memory earlier.  It is set up
290      when we removed memory-memory move insn before each iteration of
291      the reload.  */
292   unsigned int dont_reassign_p : 1;
293 #ifdef STACK_REGS
294   /* Set to TRUE if allocno can't be assigned to the stack hard
295      register correspondingly in this region and area including the
296      region and all its subregions recursively.  */
297   unsigned int no_stack_reg_p : 1, total_no_stack_reg_p : 1;
298 #endif
299   /* TRUE value means that there is no sense to spill the allocno
300      during coloring because the spill will result in additional
301      reloads in reload pass.  */
302   unsigned int bad_spill_p : 1;
303   /* TRUE if a hard register or memory has been assigned to the
304      allocno.  */
305   unsigned int assigned_p : 1;
306   /* TRUE if conflicts for given allocno are represented by vector of
307      pointers to the conflicting allocnos.  Otherwise, we use a bit
308      vector where a bit with given index represents allocno with the
309      same number.  */
310   unsigned int conflict_vec_p : 1;
311   /* Hard register assigned to given allocno.  Negative value means
312      that memory was allocated to the allocno.  During the reload,
313      spilled allocno has value equal to the corresponding stack slot
314      number (0, ...) - 2.  Value -1 is used for allocnos spilled by the
315      reload (at this point pseudo-register has only one allocno) which
316      did not get stack slot yet.  */
317   short int hard_regno;
318   /* Allocnos with the same regno are linked by the following member.
319      Allocnos corresponding to inner loops are first in the list (it
320      corresponds to depth-first traverse of the loops).  */
321   ira_allocno_t next_regno_allocno;
322   /* There may be different allocnos with the same regno in different
323      regions.  Allocnos are bound to the corresponding loop tree node.
324      Pseudo-register may have only one regular allocno with given loop
325      tree node but more than one cap (see comments above).  */
326   ira_loop_tree_node_t loop_tree_node;
327   /* Accumulated usage references of the allocno.  Here and below,
328      word 'accumulated' means info for given region and all nested
329      subregions.  In this case, 'accumulated' means sum of references
330      of the corresponding pseudo-register in this region and in all
331      nested subregions recursively. */
332   int nrefs;
333   /* Accumulated frequency of usage of the allocno.  */
334   int freq;
335   /* Minimal accumulated and updated costs of usage register of the
336      allocno class.  */
337   int class_cost, updated_class_cost;
338   /* Minimal accumulated, and updated costs of memory for the allocno.
339      At the allocation start, the original and updated costs are
340      equal.  The updated cost may be changed after finishing
341      allocation in a region and starting allocation in a subregion.
342      The change reflects the cost of spill/restore code on the
343      subregion border if we assign memory to the pseudo in the
344      subregion.  */
345   int memory_cost, updated_memory_cost;
346   /* Accumulated number of points where the allocno lives and there is
347      excess pressure for its class.  Excess pressure for a register
348      class at some point means that there are more allocnos of given
349      register class living at the point than number of hard-registers
350      of the class available for the allocation.  */
351   int excess_pressure_points_num;
352   /* Copies to other non-conflicting allocnos.  The copies can
353      represent move insn or potential move insn usually because of two
354      operand insn constraints.  */
355   ira_copy_t allocno_copies;
356   /* It is a allocno (cap) representing given allocno on upper loop tree
357      level.  */
358   ira_allocno_t cap;
359   /* It is a link to allocno (cap) on lower loop level represented by
360      given cap.  Null if given allocno is not a cap.  */
361   ira_allocno_t cap_member;
362   /* The number of objects tracked in the following array.  */
363   int num_objects;
364   /* An array of structures describing conflict information and live
365      ranges for each object associated with the allocno.  There may be
366      more than one such object in cases where the allocno represents a
367      multi-word register.  */
368   ira_object_t objects[2];
369   /* Accumulated frequency of calls which given allocno
370      intersects.  */
371   int call_freq;
372   /* Accumulated number of the intersected calls.  */
373   int calls_crossed_num;
374   /* Array of usage costs (accumulated and the one updated during
375      coloring) for each hard register of the allocno class.  The
376      member value can be NULL if all costs are the same and equal to
377      CLASS_COST.  For example, the costs of two different hard
378      registers can be different if one hard register is callee-saved
379      and another one is callee-used and the allocno lives through
380      calls.  Another example can be case when for some insn the
381      corresponding pseudo-register value should be put in specific
382      register class (e.g. AREG for x86) which is a strict subset of
383      the allocno class (GENERAL_REGS for x86).  We have updated costs
384      to reflect the situation when the usage cost of a hard register
385      is decreased because the allocno is connected to another allocno
386      by a copy and the another allocno has been assigned to the hard
387      register.  */
388   int *hard_reg_costs, *updated_hard_reg_costs;
389   /* Array of decreasing costs (accumulated and the one updated during
390      coloring) for allocnos conflicting with given allocno for hard
391      regno of the allocno class.  The member value can be NULL if all
392      costs are the same.  These costs are used to reflect preferences
393      of other allocnos not assigned yet during assigning to given
394      allocno.  */
395   int *conflict_hard_reg_costs, *updated_conflict_hard_reg_costs;
396   /* Different additional data.  It is used to decrease size of
397      allocno data footprint.  */
398   void *add_data;
399 };
400
401
402 /* All members of the allocno structures should be accessed only
403    through the following macros.  */
404 #define ALLOCNO_NUM(A) ((A)->num)
405 #define ALLOCNO_REGNO(A) ((A)->regno)
406 #define ALLOCNO_REG(A) ((A)->reg)
407 #define ALLOCNO_NEXT_REGNO_ALLOCNO(A) ((A)->next_regno_allocno)
408 #define ALLOCNO_LOOP_TREE_NODE(A) ((A)->loop_tree_node)
409 #define ALLOCNO_CAP(A) ((A)->cap)
410 #define ALLOCNO_CAP_MEMBER(A) ((A)->cap_member)
411 #define ALLOCNO_NREFS(A) ((A)->nrefs)
412 #define ALLOCNO_FREQ(A) ((A)->freq)
413 #define ALLOCNO_HARD_REGNO(A) ((A)->hard_regno)
414 #define ALLOCNO_CALL_FREQ(A) ((A)->call_freq)
415 #define ALLOCNO_CALLS_CROSSED_NUM(A) ((A)->calls_crossed_num)
416 #define ALLOCNO_MEM_OPTIMIZED_DEST(A) ((A)->mem_optimized_dest)
417 #define ALLOCNO_MEM_OPTIMIZED_DEST_P(A) ((A)->mem_optimized_dest_p)
418 #define ALLOCNO_SOMEWHERE_RENAMED_P(A) ((A)->somewhere_renamed_p)
419 #define ALLOCNO_CHILD_RENAMED_P(A) ((A)->child_renamed_p)
420 #define ALLOCNO_DONT_REASSIGN_P(A) ((A)->dont_reassign_p)
421 #ifdef STACK_REGS
422 #define ALLOCNO_NO_STACK_REG_P(A) ((A)->no_stack_reg_p)
423 #define ALLOCNO_TOTAL_NO_STACK_REG_P(A) ((A)->total_no_stack_reg_p)
424 #endif
425 #define ALLOCNO_BAD_SPILL_P(A) ((A)->bad_spill_p)
426 #define ALLOCNO_ASSIGNED_P(A) ((A)->assigned_p)
427 #define ALLOCNO_MODE(A) ((A)->mode)
428 #define ALLOCNO_COPIES(A) ((A)->allocno_copies)
429 #define ALLOCNO_HARD_REG_COSTS(A) ((A)->hard_reg_costs)
430 #define ALLOCNO_UPDATED_HARD_REG_COSTS(A) ((A)->updated_hard_reg_costs)
431 #define ALLOCNO_CONFLICT_HARD_REG_COSTS(A) \
432   ((A)->conflict_hard_reg_costs)
433 #define ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS(A) \
434   ((A)->updated_conflict_hard_reg_costs)
435 #define ALLOCNO_CLASS(A) ((A)->aclass)
436 #define ALLOCNO_CLASS_COST(A) ((A)->class_cost)
437 #define ALLOCNO_UPDATED_CLASS_COST(A) ((A)->updated_class_cost)
438 #define ALLOCNO_MEMORY_COST(A) ((A)->memory_cost)
439 #define ALLOCNO_UPDATED_MEMORY_COST(A) ((A)->updated_memory_cost)
440 #define ALLOCNO_EXCESS_PRESSURE_POINTS_NUM(A) \
441   ((A)->excess_pressure_points_num)
442 #define ALLOCNO_OBJECT(A,N) ((A)->objects[N])
443 #define ALLOCNO_NUM_OBJECTS(A) ((A)->num_objects)
444 #define ALLOCNO_ADD_DATA(A) ((A)->add_data)
445
446 /* Typedef for pointer to the subsequent structure.  */
447 typedef struct ira_emit_data *ira_emit_data_t;
448
449 /* Allocno bound data used for emit pseudo live range split insns and
450    to flattening IR.  */
451 struct ira_emit_data
452 {
453   /* TRUE if the allocno assigned to memory was a destination of
454      removed move (see ira-emit.c) at loop exit because the value of
455      the corresponding pseudo-register is not changed inside the
456      loop.  */
457   unsigned int mem_optimized_dest_p : 1;
458   /* TRUE if the corresponding pseudo-register has disjoint live
459      ranges and the other allocnos of the pseudo-register except this
460      one changed REG.  */
461   unsigned int somewhere_renamed_p : 1;
462   /* TRUE if allocno with the same REGNO in a subregion has been
463      renamed, in other words, got a new pseudo-register.  */
464   unsigned int child_renamed_p : 1;
465   /* Final rtx representation of the allocno.  */
466   rtx reg;
467   /* Non NULL if we remove restoring value from given allocno to
468      MEM_OPTIMIZED_DEST at loop exit (see ira-emit.c) because the
469      allocno value is not changed inside the loop.  */
470   ira_allocno_t mem_optimized_dest;
471 };
472
473 #define ALLOCNO_EMIT_DATA(a) ((ira_emit_data_t) ALLOCNO_ADD_DATA (a))
474
475 /* Data used to emit live range split insns and to flattening IR.  */
476 extern ira_emit_data_t ira_allocno_emit_data;
477
478 /* Abbreviation for frequent emit data access.  */
479 static inline rtx
480 allocno_emit_reg (ira_allocno_t a)
481 {
482   return ALLOCNO_EMIT_DATA (a)->reg;
483 }
484
485 #define OBJECT_ALLOCNO(O) ((O)->allocno)
486 #define OBJECT_SUBWORD(O) ((O)->subword)
487 #define OBJECT_CONFLICT_ARRAY(O) ((O)->conflicts_array)
488 #define OBJECT_CONFLICT_VEC(O) ((ira_object_t *)(O)->conflicts_array)
489 #define OBJECT_CONFLICT_BITVEC(O) ((IRA_INT_TYPE *)(O)->conflicts_array)
490 #define OBJECT_CONFLICT_ARRAY_SIZE(O) ((O)->conflicts_array_size)
491 #define OBJECT_CONFLICT_VEC_P(O) ((O)->conflict_vec_p)
492 #define OBJECT_NUM_CONFLICTS(O) ((O)->num_accumulated_conflicts)
493 #define OBJECT_CONFLICT_HARD_REGS(O) ((O)->conflict_hard_regs)
494 #define OBJECT_TOTAL_CONFLICT_HARD_REGS(O) ((O)->total_conflict_hard_regs)
495 #define OBJECT_MIN(O) ((O)->min)
496 #define OBJECT_MAX(O) ((O)->max)
497 #define OBJECT_CONFLICT_ID(O) ((O)->id)
498 #define OBJECT_LIVE_RANGES(O) ((O)->live_ranges)
499
500 /* Map regno -> allocnos with given regno (see comments for
501    allocno member `next_regno_allocno').  */
502 extern ira_allocno_t *ira_regno_allocno_map;
503
504 /* Array of references to all allocnos.  The order number of the
505    allocno corresponds to the index in the array.  Removed allocnos
506    have NULL element value.  */
507 extern ira_allocno_t *ira_allocnos;
508
509 /* The size of the previous array.  */
510 extern int ira_allocnos_num;
511
512 /* Map a conflict id to its corresponding ira_object structure.  */
513 extern ira_object_t *ira_object_id_map;
514
515 /* The size of the previous array.  */
516 extern int ira_objects_num;
517
518 /* The following structure represents a copy of two allocnos.  The
519    copies represent move insns or potential move insns usually because
520    of two operand insn constraints.  To remove register shuffle, we
521    also create copies between allocno which is output of an insn and
522    allocno becoming dead in the insn.  */
523 struct ira_allocno_copy
524 {
525   /* The unique order number of the copy node starting with 0.  */
526   int num;
527   /* Allocnos connected by the copy.  The first allocno should have
528      smaller order number than the second one.  */
529   ira_allocno_t first, second;
530   /* Execution frequency of the copy.  */
531   int freq;
532   bool constraint_p;
533   /* It is a move insn which is an origin of the copy.  The member
534      value for the copy representing two operand insn constraints or
535      for the copy created to remove register shuffle is NULL.  In last
536      case the copy frequency is smaller than the corresponding insn
537      execution frequency.  */
538   rtx insn;
539   /* All copies with the same allocno as FIRST are linked by the two
540      following members.  */
541   ira_copy_t prev_first_allocno_copy, next_first_allocno_copy;
542   /* All copies with the same allocno as SECOND are linked by the two
543      following members.  */
544   ira_copy_t prev_second_allocno_copy, next_second_allocno_copy;
545   /* Region from which given copy is originated.  */
546   ira_loop_tree_node_t loop_tree_node;
547 };
548
549 /* Array of references to all copies.  The order number of the copy
550    corresponds to the index in the array.  Removed copies have NULL
551    element value.  */
552 extern ira_copy_t *ira_copies;
553
554 /* Size of the previous array.  */
555 extern int ira_copies_num;
556
557 /* The following structure describes a stack slot used for spilled
558    pseudo-registers.  */
559 struct ira_spilled_reg_stack_slot
560 {
561   /* pseudo-registers assigned to the stack slot.  */
562   bitmap_head spilled_regs;
563   /* RTL representation of the stack slot.  */
564   rtx mem;
565   /* Size of the stack slot.  */
566   unsigned int width;
567 };
568
569 /* The number of elements in the following array.  */
570 extern int ira_spilled_reg_stack_slots_num;
571
572 /* The following array contains info about spilled pseudo-registers
573    stack slots used in current function so far.  */
574 extern struct ira_spilled_reg_stack_slot *ira_spilled_reg_stack_slots;
575
576 /* Correspondingly overall cost of the allocation, cost of the
577    allocnos assigned to hard-registers, cost of the allocnos assigned
578    to memory, cost of loads, stores and register move insns generated
579    for pseudo-register live range splitting (see ira-emit.c).  */
580 extern int ira_overall_cost;
581 extern int ira_reg_cost, ira_mem_cost;
582 extern int ira_load_cost, ira_store_cost, ira_shuffle_cost;
583 extern int ira_move_loops_num, ira_additional_jumps_num;
584
585 \f
586 /* This page contains a bitset implementation called 'min/max sets' used to
587    record conflicts in IRA.
588    They are named min/maxs set since we keep track of a minimum and a maximum
589    bit number for each set representing the bounds of valid elements.  Otherwise,
590    the implementation resembles sbitmaps in that we store an array of integers
591    whose bits directly represent the members of the set.  */
592
593 /* The type used as elements in the array, and the number of bits in
594    this type.  */
595
596 #define IRA_INT_BITS HOST_BITS_PER_WIDE_INT
597 #define IRA_INT_TYPE HOST_WIDE_INT
598
599 /* Set, clear or test bit number I in R, a bit vector of elements with
600    minimal index and maximal index equal correspondingly to MIN and
601    MAX.  */
602 #if defined ENABLE_IRA_CHECKING && (GCC_VERSION >= 2007)
603
604 #define SET_MINMAX_SET_BIT(R, I, MIN, MAX) __extension__                \
605   (({ int _min = (MIN), _max = (MAX), _i = (I);                         \
606      if (_i < _min || _i > _max)                                        \
607        {                                                                \
608          fprintf (stderr,                                               \
609                   "\n%s: %d: error in %s: %d not in range [%d,%d]\n",   \
610                   __FILE__, __LINE__, __FUNCTION__, _i, _min, _max);    \
611          gcc_unreachable ();                                            \
612        }                                                                \
613      ((R)[(unsigned) (_i - _min) / IRA_INT_BITS]                        \
614       |= ((IRA_INT_TYPE) 1 << ((unsigned) (_i - _min) % IRA_INT_BITS))); }))
615
616
617 #define CLEAR_MINMAX_SET_BIT(R, I, MIN, MAX) __extension__              \
618   (({ int _min = (MIN), _max = (MAX), _i = (I);                         \
619      if (_i < _min || _i > _max)                                        \
620        {                                                                \
621          fprintf (stderr,                                               \
622                   "\n%s: %d: error in %s: %d not in range [%d,%d]\n",   \
623                   __FILE__, __LINE__, __FUNCTION__, _i, _min, _max);    \
624          gcc_unreachable ();                                            \
625        }                                                                \
626      ((R)[(unsigned) (_i - _min) / IRA_INT_BITS]                        \
627       &= ~((IRA_INT_TYPE) 1 << ((unsigned) (_i - _min) % IRA_INT_BITS))); }))
628
629 #define TEST_MINMAX_SET_BIT(R, I, MIN, MAX) __extension__               \
630   (({ int _min = (MIN), _max = (MAX), _i = (I);                         \
631      if (_i < _min || _i > _max)                                        \
632        {                                                                \
633          fprintf (stderr,                                               \
634                   "\n%s: %d: error in %s: %d not in range [%d,%d]\n",   \
635                   __FILE__, __LINE__, __FUNCTION__, _i, _min, _max);    \
636          gcc_unreachable ();                                            \
637        }                                                                \
638      ((R)[(unsigned) (_i - _min) / IRA_INT_BITS]                        \
639       & ((IRA_INT_TYPE) 1 << ((unsigned) (_i - _min) % IRA_INT_BITS))); }))
640
641 #else
642
643 #define SET_MINMAX_SET_BIT(R, I, MIN, MAX)                      \
644   ((R)[(unsigned) ((I) - (MIN)) / IRA_INT_BITS]                 \
645    |= ((IRA_INT_TYPE) 1 << ((unsigned) ((I) - (MIN)) % IRA_INT_BITS)))
646
647 #define CLEAR_MINMAX_SET_BIT(R, I, MIN, MAX)                    \
648   ((R)[(unsigned) ((I) - (MIN)) / IRA_INT_BITS]                 \
649    &= ~((IRA_INT_TYPE) 1 << ((unsigned) ((I) - (MIN)) % IRA_INT_BITS)))
650
651 #define TEST_MINMAX_SET_BIT(R, I, MIN, MAX)                     \
652   ((R)[(unsigned) ((I) - (MIN)) / IRA_INT_BITS]                 \
653    & ((IRA_INT_TYPE) 1 << ((unsigned) ((I) - (MIN)) % IRA_INT_BITS)))
654
655 #endif
656
657 /* The iterator for min/max sets.  */
658 typedef struct {
659
660   /* Array containing the bit vector.  */
661   IRA_INT_TYPE *vec;
662
663   /* The number of the current element in the vector.  */
664   unsigned int word_num;
665
666   /* The number of bits in the bit vector.  */
667   unsigned int nel;
668
669   /* The current bit index of the bit vector.  */
670   unsigned int bit_num;
671
672   /* Index corresponding to the 1st bit of the bit vector.   */
673   int start_val;
674
675   /* The word of the bit vector currently visited.  */
676   unsigned IRA_INT_TYPE word;
677 } minmax_set_iterator;
678
679 /* Initialize the iterator I for bit vector VEC containing minimal and
680    maximal values MIN and MAX.  */
681 static inline void
682 minmax_set_iter_init (minmax_set_iterator *i, IRA_INT_TYPE *vec, int min,
683                       int max)
684 {
685   i->vec = vec;
686   i->word_num = 0;
687   i->nel = max < min ? 0 : max - min + 1;
688   i->start_val = min;
689   i->bit_num = 0;
690   i->word = i->nel == 0 ? 0 : vec[0];
691 }
692
693 /* Return TRUE if we have more allocnos to visit, in which case *N is
694    set to the number of the element to be visited.  Otherwise, return
695    FALSE.  */
696 static inline bool
697 minmax_set_iter_cond (minmax_set_iterator *i, int *n)
698 {
699   /* Skip words that are zeros.  */
700   for (; i->word == 0; i->word = i->vec[i->word_num])
701     {
702       i->word_num++;
703       i->bit_num = i->word_num * IRA_INT_BITS;
704
705       /* If we have reached the end, break.  */
706       if (i->bit_num >= i->nel)
707         return false;
708     }
709
710   /* Skip bits that are zero.  */
711   for (; (i->word & 1) == 0; i->word >>= 1)
712     i->bit_num++;
713
714   *n = (int) i->bit_num + i->start_val;
715
716   return true;
717 }
718
719 /* Advance to the next element in the set.  */
720 static inline void
721 minmax_set_iter_next (minmax_set_iterator *i)
722 {
723   i->word >>= 1;
724   i->bit_num++;
725 }
726
727 /* Loop over all elements of a min/max set given by bit vector VEC and
728    their minimal and maximal values MIN and MAX.  In each iteration, N
729    is set to the number of next allocno.  ITER is an instance of
730    minmax_set_iterator used to iterate over the set.  */
731 #define FOR_EACH_BIT_IN_MINMAX_SET(VEC, MIN, MAX, N, ITER)      \
732   for (minmax_set_iter_init (&(ITER), (VEC), (MIN), (MAX));     \
733        minmax_set_iter_cond (&(ITER), &(N));                    \
734        minmax_set_iter_next (&(ITER)))
735 \f
736 struct target_ira_int {
737   /* Initialized once.  It is a maximal possible size of the allocated
738      struct costs.  */
739   int x_max_struct_costs_size;
740
741   /* Allocated and initialized once, and used to initialize cost values
742      for each insn.  */
743   struct costs *x_init_cost;
744
745   /* Allocated once, and used for temporary purposes.  */
746   struct costs *x_temp_costs;
747
748   /* Allocated once, and used for the cost calculation.  */
749   struct costs *x_op_costs[MAX_RECOG_OPERANDS];
750   struct costs *x_this_op_costs[MAX_RECOG_OPERANDS];
751
752   /* Hard registers that can not be used for the register allocator for
753      all functions of the current compilation unit.  */
754   HARD_REG_SET x_no_unit_alloc_regs;
755
756   /* Map: hard regs X modes -> set of hard registers for storing value
757      of given mode starting with given hard register.  */
758   HARD_REG_SET (x_ira_reg_mode_hard_regset
759                 [FIRST_PSEUDO_REGISTER][NUM_MACHINE_MODES]);
760
761   /* Array based on TARGET_REGISTER_MOVE_COST.  Don't use
762      ira_register_move_cost directly.  Use function of
763      ira_get_may_move_cost instead.  */
764   move_table *x_ira_register_move_cost[MAX_MACHINE_MODE];
765
766   /* Array analogs of the macros MEMORY_MOVE_COST and
767      REGISTER_MOVE_COST but they contain maximal cost not minimal as
768      the previous two ones do.  */
769   short int x_ira_max_memory_move_cost[MAX_MACHINE_MODE][N_REG_CLASSES][2];
770   move_table *x_ira_max_register_move_cost[MAX_MACHINE_MODE];
771
772   /* Similar to may_move_in_cost but it is calculated in IRA instead of
773      regclass.  Another difference we take only available hard registers
774      into account to figure out that one register class is a subset of
775      the another one.  Don't use it directly.  Use function of
776      ira_get_may_move_cost instead.  */
777   move_table *x_ira_may_move_in_cost[MAX_MACHINE_MODE];
778
779   /* Similar to may_move_out_cost but it is calculated in IRA instead of
780      regclass.  Another difference we take only available hard registers
781      into account to figure out that one register class is a subset of
782      the another one.  Don't use it directly.  Use function of
783      ira_get_may_move_cost instead.  */
784   move_table *x_ira_may_move_out_cost[MAX_MACHINE_MODE];
785
786 /* Similar to ira_may_move_in_cost and ira_may_move_out_cost but they
787    return maximal cost.  */
788   move_table *x_ira_max_may_move_in_cost[MAX_MACHINE_MODE];
789   move_table *x_ira_max_may_move_out_cost[MAX_MACHINE_MODE];
790
791   /* Map class->true if class is a possible allocno class, false
792      otherwise. */
793   bool x_ira_reg_allocno_class_p[N_REG_CLASSES];
794
795   /* Map class->true if class is a pressure class, false otherwise. */
796   bool x_ira_reg_pressure_class_p[N_REG_CLASSES];
797
798   /* Register class subset relation: TRUE if the first class is a subset
799      of the second one considering only hard registers available for the
800      allocation.  */
801   int x_ira_class_subset_p[N_REG_CLASSES][N_REG_CLASSES];
802
803   /* Array of the number of hard registers of given class which are
804      available for allocation.  The order is defined by the hard
805      register numbers.  */
806   short x_ira_non_ordered_class_hard_regs[N_REG_CLASSES][FIRST_PSEUDO_REGISTER];
807
808   /* Index (in ira_class_hard_regs; for given register class and hard
809      register (in general case a hard register can belong to several
810      register classes;.  The index is negative for hard registers
811      unavailable for the allocation.  */
812   short x_ira_class_hard_reg_index[N_REG_CLASSES][FIRST_PSEUDO_REGISTER];
813
814   /* Array whose values are hard regset of hard registers available for
815      the allocation of given register class whose HARD_REGNO_MODE_OK
816      values for given mode are zero.  */
817   HARD_REG_SET x_ira_prohibited_class_mode_regs[N_REG_CLASSES][NUM_MACHINE_MODES];
818
819   /* The value is number of elements in the subsequent array.  */
820   int x_ira_important_classes_num;
821
822   /* The array containing all non-empty classes.  Such classes is
823      important for calculation of the hard register usage costs.  */
824   enum reg_class x_ira_important_classes[N_REG_CLASSES];
825
826   /* The array containing indexes of important classes in the previous
827      array.  The array elements are defined only for important
828      classes.  */
829   int x_ira_important_class_nums[N_REG_CLASSES];
830
831   /* The biggest important class inside of intersection of the two
832      classes (that is calculated taking only hard registers available
833      for allocation into account;.  If the both classes contain no hard
834      registers available for allocation, the value is calculated with
835      taking all hard-registers including fixed ones into account.  */
836   enum reg_class x_ira_reg_class_intersect[N_REG_CLASSES][N_REG_CLASSES];
837
838   /* True if the two classes (that is calculated taking only hard
839      registers available for allocation into account; are
840      intersected.  */
841   bool x_ira_reg_classes_intersect_p[N_REG_CLASSES][N_REG_CLASSES];
842
843   /* Classes with end marker LIM_REG_CLASSES which are intersected with
844      given class (the first index;.  That includes given class itself.
845      This is calculated taking only hard registers available for
846      allocation into account.  */
847   enum reg_class x_ira_reg_class_super_classes[N_REG_CLASSES][N_REG_CLASSES];
848
849   /* The biggest (smallest) important class inside of (covering) union
850      of the two classes (that is calculated taking only hard registers
851      available for allocation into account).  If the both classes
852      contain no hard registers available for allocation, the value is
853      calculated with taking all hard-registers including fixed ones
854      into account.  In other words, the value is the corresponding
855      reg_class_subunion (reg_class_superunion) value.  */
856   enum reg_class x_ira_reg_class_subunion[N_REG_CLASSES][N_REG_CLASSES];
857   enum reg_class x_ira_reg_class_superunion[N_REG_CLASSES][N_REG_CLASSES];
858
859   /* For each reg class, table listing all the classes contained in it
860      (excluding the class itself.  Non-allocatable registers are
861      excluded from the consideration;.  */
862   enum reg_class x_alloc_reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES];
863
864   /* Array whose values are hard regset of hard registers for which
865      move of the hard register in given mode into itself is
866      prohibited.  */
867   HARD_REG_SET x_ira_prohibited_mode_move_regs[NUM_MACHINE_MODES];
868
869   /* Flag of that the above array has been initialized.  */
870   bool x_ira_prohibited_mode_move_regs_initialized_p;
871 };
872
873 extern struct target_ira_int default_target_ira_int;
874 #if SWITCHABLE_TARGET
875 extern struct target_ira_int *this_target_ira_int;
876 #else
877 #define this_target_ira_int (&default_target_ira_int)
878 #endif
879
880 #define ira_reg_mode_hard_regset \
881   (this_target_ira_int->x_ira_reg_mode_hard_regset)
882 #define ira_register_move_cost \
883   (this_target_ira_int->x_ira_register_move_cost)
884 #define ira_max_memory_move_cost \
885   (this_target_ira_int->x_ira_max_memory_move_cost)
886 #define ira_max_register_move_cost \
887   (this_target_ira_int->x_ira_max_register_move_cost)
888 #define ira_may_move_in_cost \
889   (this_target_ira_int->x_ira_may_move_in_cost)
890 #define ira_may_move_out_cost \
891   (this_target_ira_int->x_ira_may_move_out_cost)
892 #define ira_max_may_move_in_cost \
893   (this_target_ira_int->x_ira_max_may_move_in_cost)
894 #define ira_max_may_move_out_cost \
895   (this_target_ira_int->x_ira_max_may_move_out_cost)
896 #define ira_reg_allocno_class_p \
897   (this_target_ira_int->x_ira_reg_allocno_class_p)
898 #define ira_reg_pressure_class_p \
899   (this_target_ira_int->x_ira_reg_pressure_class_p)
900 #define ira_class_subset_p \
901   (this_target_ira_int->x_ira_class_subset_p)
902 #define ira_non_ordered_class_hard_regs \
903   (this_target_ira_int->x_ira_non_ordered_class_hard_regs)
904 #define ira_class_hard_reg_index \
905   (this_target_ira_int->x_ira_class_hard_reg_index)
906 #define ira_prohibited_class_mode_regs \
907   (this_target_ira_int->x_ira_prohibited_class_mode_regs)
908 #define ira_important_classes_num \
909   (this_target_ira_int->x_ira_important_classes_num)
910 #define ira_important_classes \
911   (this_target_ira_int->x_ira_important_classes)
912 #define ira_important_class_nums \
913   (this_target_ira_int->x_ira_important_class_nums)
914 #define ira_reg_class_intersect \
915   (this_target_ira_int->x_ira_reg_class_intersect)
916 #define ira_reg_classes_intersect_p \
917   (this_target_ira_int->x_ira_reg_classes_intersect_p)
918 #define ira_reg_class_super_classes \
919   (this_target_ira_int->x_ira_reg_class_super_classes)
920 #define ira_reg_class_subunion \
921   (this_target_ira_int->x_ira_reg_class_subunion)
922 #define ira_reg_class_superunion \
923   (this_target_ira_int->x_ira_reg_class_superunion)
924 #define ira_prohibited_mode_move_regs \
925   (this_target_ira_int->x_ira_prohibited_mode_move_regs)
926 \f
927 /* ira.c: */
928
929 extern void *ira_allocate (size_t);
930 extern void ira_free (void *addr);
931 extern bitmap ira_allocate_bitmap (void);
932 extern void ira_free_bitmap (bitmap);
933 extern void ira_print_disposition (FILE *);
934 extern void ira_debug_disposition (void);
935 extern void ira_debug_allocno_classes (void);
936 extern void ira_init_register_move_cost (enum machine_mode);
937
938 /* The length of the two following arrays.  */
939 extern int ira_reg_equiv_len;
940
941 /* The element value is TRUE if the corresponding regno value is
942    invariant.  */
943 extern bool *ira_reg_equiv_invariant_p;
944
945 /* The element value is equiv constant of given pseudo-register or
946    NULL_RTX.  */
947 extern rtx *ira_reg_equiv_const;
948
949 /* ira-build.c */
950
951 /* The current loop tree node and its regno allocno map.  */
952 extern ira_loop_tree_node_t ira_curr_loop_tree_node;
953 extern ira_allocno_t *ira_curr_regno_allocno_map;
954
955 extern void ira_debug_copy (ira_copy_t);
956 extern void ira_debug_copies (void);
957 extern void ira_debug_allocno_copies (ira_allocno_t);
958
959 extern void ira_traverse_loop_tree (bool, ira_loop_tree_node_t,
960                                     void (*) (ira_loop_tree_node_t),
961                                     void (*) (ira_loop_tree_node_t));
962 extern ira_allocno_t ira_parent_allocno (ira_allocno_t);
963 extern ira_allocno_t ira_parent_or_cap_allocno (ira_allocno_t);
964 extern ira_allocno_t ira_create_allocno (int, bool, ira_loop_tree_node_t);
965 extern void ira_create_allocno_objects (ira_allocno_t);
966 extern void ira_set_allocno_class (ira_allocno_t, enum reg_class);
967 extern bool ira_conflict_vector_profitable_p (ira_object_t, int);
968 extern void ira_allocate_conflict_vec (ira_object_t, int);
969 extern void ira_allocate_object_conflicts (ira_object_t, int);
970 extern void ior_hard_reg_conflicts (ira_allocno_t, HARD_REG_SET *);
971 extern void ira_print_expanded_allocno (ira_allocno_t);
972 extern void ira_add_live_range_to_object (ira_object_t, int, int);
973 extern live_range_t ira_create_live_range (ira_object_t, int, int,
974                                            live_range_t);
975 extern live_range_t ira_copy_live_range_list (live_range_t);
976 extern live_range_t ira_merge_live_ranges (live_range_t, live_range_t);
977 extern bool ira_live_ranges_intersect_p (live_range_t, live_range_t);
978 extern void ira_finish_live_range (live_range_t);
979 extern void ira_finish_live_range_list (live_range_t);
980 extern void ira_free_allocno_updated_costs (ira_allocno_t);
981 extern ira_copy_t ira_create_copy (ira_allocno_t, ira_allocno_t,
982                                    int, bool, rtx, ira_loop_tree_node_t);
983 extern void ira_add_allocno_copy_to_list (ira_copy_t);
984 extern void ira_swap_allocno_copy_ends_if_necessary (ira_copy_t);
985 extern ira_copy_t ira_add_allocno_copy (ira_allocno_t, ira_allocno_t, int,
986                                         bool, rtx, ira_loop_tree_node_t);
987
988 extern int *ira_allocate_cost_vector (reg_class_t);
989 extern void ira_free_cost_vector (int *, reg_class_t);
990
991 extern void ira_flattening (int, int);
992 extern bool ira_build (bool);
993 extern void ira_destroy (void);
994
995 /* ira-costs.c */
996 extern void ira_init_costs_once (void);
997 extern void ira_init_costs (void);
998 extern void ira_finish_costs_once (void);
999 extern void ira_costs (void);
1000 extern void ira_tune_allocno_costs (void);
1001
1002 /* ira-lives.c */
1003
1004 extern void ira_rebuild_start_finish_chains (void);
1005 extern void ira_print_live_range_list (FILE *, live_range_t);
1006 extern void ira_debug_live_range_list (live_range_t);
1007 extern void ira_debug_allocno_live_ranges (ira_allocno_t);
1008 extern void ira_debug_live_ranges (void);
1009 extern void ira_create_allocno_live_ranges (void);
1010 extern void ira_compress_allocno_live_ranges (void);
1011 extern void ira_finish_allocno_live_ranges (void);
1012
1013 /* ira-conflicts.c */
1014 extern void ira_debug_conflicts (bool);
1015 extern void ira_build_conflicts (void);
1016
1017 /* ira-color.c */
1018 extern void ira_debug_hard_regs_forest (void);
1019 extern int ira_loop_edge_freq (ira_loop_tree_node_t, int, bool);
1020 extern void ira_reassign_conflict_allocnos (int);
1021 extern void ira_initiate_assign (void);
1022 extern void ira_finish_assign (void);
1023 extern void ira_color (void);
1024
1025 /* ira-emit.c */
1026 extern void ira_initiate_emit_data (void);
1027 extern void ira_finish_emit_data (void);
1028 extern void ira_emit (bool);
1029
1030 \f
1031
1032 /* Initialize register costs for MODE if necessary.  */
1033 static inline void
1034 ira_init_register_move_cost_if_necessary (enum machine_mode mode)
1035 {
1036   if (ira_register_move_cost[mode] == NULL)
1037     ira_init_register_move_cost (mode);
1038 }
1039
1040 \f
1041
1042 /* The iterator for all allocnos.  */
1043 typedef struct {
1044   /* The number of the current element in IRA_ALLOCNOS.  */
1045   int n;
1046 } ira_allocno_iterator;
1047
1048 /* Initialize the iterator I.  */
1049 static inline void
1050 ira_allocno_iter_init (ira_allocno_iterator *i)
1051 {
1052   i->n = 0;
1053 }
1054
1055 /* Return TRUE if we have more allocnos to visit, in which case *A is
1056    set to the allocno to be visited.  Otherwise, return FALSE.  */
1057 static inline bool
1058 ira_allocno_iter_cond (ira_allocno_iterator *i, ira_allocno_t *a)
1059 {
1060   int n;
1061
1062   for (n = i->n; n < ira_allocnos_num; n++)
1063     if (ira_allocnos[n] != NULL)
1064       {
1065         *a = ira_allocnos[n];
1066         i->n = n + 1;
1067         return true;
1068       }
1069   return false;
1070 }
1071
1072 /* Loop over all allocnos.  In each iteration, A is set to the next
1073    allocno.  ITER is an instance of ira_allocno_iterator used to iterate
1074    the allocnos.  */
1075 #define FOR_EACH_ALLOCNO(A, ITER)                       \
1076   for (ira_allocno_iter_init (&(ITER));                 \
1077        ira_allocno_iter_cond (&(ITER), &(A));)
1078 \f
1079 /* The iterator for all objects.  */
1080 typedef struct {
1081   /* The number of the current element in ira_object_id_map.  */
1082   int n;
1083 } ira_object_iterator;
1084
1085 /* Initialize the iterator I.  */
1086 static inline void
1087 ira_object_iter_init (ira_object_iterator *i)
1088 {
1089   i->n = 0;
1090 }
1091
1092 /* Return TRUE if we have more objects to visit, in which case *OBJ is
1093    set to the object to be visited.  Otherwise, return FALSE.  */
1094 static inline bool
1095 ira_object_iter_cond (ira_object_iterator *i, ira_object_t *obj)
1096 {
1097   int n;
1098
1099   for (n = i->n; n < ira_objects_num; n++)
1100     if (ira_object_id_map[n] != NULL)
1101       {
1102         *obj = ira_object_id_map[n];
1103         i->n = n + 1;
1104         return true;
1105       }
1106   return false;
1107 }
1108
1109 /* Loop over all objects.  In each iteration, OBJ is set to the next
1110    object.  ITER is an instance of ira_object_iterator used to iterate
1111    the objects.  */
1112 #define FOR_EACH_OBJECT(OBJ, ITER)                      \
1113   for (ira_object_iter_init (&(ITER));                  \
1114        ira_object_iter_cond (&(ITER), &(OBJ));)
1115 \f
1116 /* The iterator for objects associated with an allocno.  */
1117 typedef struct {
1118   /* The number of the element the allocno's object array.  */
1119   int n;
1120 } ira_allocno_object_iterator;
1121
1122 /* Initialize the iterator I.  */
1123 static inline void
1124 ira_allocno_object_iter_init (ira_allocno_object_iterator *i)
1125 {
1126   i->n = 0;
1127 }
1128
1129 /* Return TRUE if we have more objects to visit in allocno A, in which
1130    case *O is set to the object to be visited.  Otherwise, return
1131    FALSE.  */
1132 static inline bool
1133 ira_allocno_object_iter_cond (ira_allocno_object_iterator *i, ira_allocno_t a,
1134                               ira_object_t *o)
1135 {
1136   *o = ALLOCNO_OBJECT (a, i->n);
1137   return i->n++ < ALLOCNO_NUM_OBJECTS (a);
1138 }
1139
1140 /* Loop over all objects associated with allocno A.  In each
1141    iteration, O is set to the next object.  ITER is an instance of
1142    ira_allocno_object_iterator used to iterate the conflicts.  */
1143 #define FOR_EACH_ALLOCNO_OBJECT(A, O, ITER)                     \
1144   for (ira_allocno_object_iter_init (&(ITER));                  \
1145        ira_allocno_object_iter_cond (&(ITER), (A), &(O));)
1146 \f
1147
1148 /* The iterator for copies.  */
1149 typedef struct {
1150   /* The number of the current element in IRA_COPIES.  */
1151   int n;
1152 } ira_copy_iterator;
1153
1154 /* Initialize the iterator I.  */
1155 static inline void
1156 ira_copy_iter_init (ira_copy_iterator *i)
1157 {
1158   i->n = 0;
1159 }
1160
1161 /* Return TRUE if we have more copies to visit, in which case *CP is
1162    set to the copy to be visited.  Otherwise, return FALSE.  */
1163 static inline bool
1164 ira_copy_iter_cond (ira_copy_iterator *i, ira_copy_t *cp)
1165 {
1166   int n;
1167
1168   for (n = i->n; n < ira_copies_num; n++)
1169     if (ira_copies[n] != NULL)
1170       {
1171         *cp = ira_copies[n];
1172         i->n = n + 1;
1173         return true;
1174       }
1175   return false;
1176 }
1177
1178 /* Loop over all copies.  In each iteration, C is set to the next
1179    copy.  ITER is an instance of ira_copy_iterator used to iterate
1180    the copies.  */
1181 #define FOR_EACH_COPY(C, ITER)                          \
1182   for (ira_copy_iter_init (&(ITER));                    \
1183        ira_copy_iter_cond (&(ITER), &(C));)
1184 \f
1185 /* The iterator for object conflicts.  */
1186 typedef struct {
1187
1188   /* TRUE if the conflicts are represented by vector of allocnos.  */
1189   bool conflict_vec_p;
1190
1191   /* The conflict vector or conflict bit vector.  */
1192   void *vec;
1193
1194   /* The number of the current element in the vector (of type
1195      ira_object_t or IRA_INT_TYPE).  */
1196   unsigned int word_num;
1197
1198   /* The bit vector size.  It is defined only if
1199      OBJECT_CONFLICT_VEC_P is FALSE.  */
1200   unsigned int size;
1201
1202   /* The current bit index of bit vector.  It is defined only if
1203      OBJECT_CONFLICT_VEC_P is FALSE.  */
1204   unsigned int bit_num;
1205
1206   /* The object id corresponding to the 1st bit of the bit vector.  It
1207      is defined only if OBJECT_CONFLICT_VEC_P is FALSE.  */
1208   int base_conflict_id;
1209
1210   /* The word of bit vector currently visited.  It is defined only if
1211      OBJECT_CONFLICT_VEC_P is FALSE.  */
1212   unsigned IRA_INT_TYPE word;
1213 } ira_object_conflict_iterator;
1214
1215 /* Initialize the iterator I with ALLOCNO conflicts.  */
1216 static inline void
1217 ira_object_conflict_iter_init (ira_object_conflict_iterator *i,
1218                                ira_object_t obj)
1219 {
1220   i->conflict_vec_p = OBJECT_CONFLICT_VEC_P (obj);
1221   i->vec = OBJECT_CONFLICT_ARRAY (obj);
1222   i->word_num = 0;
1223   if (i->conflict_vec_p)
1224     i->size = i->bit_num = i->base_conflict_id = i->word = 0;
1225   else
1226     {
1227       if (OBJECT_MIN (obj) > OBJECT_MAX (obj))
1228         i->size = 0;
1229       else
1230         i->size = ((OBJECT_MAX (obj) - OBJECT_MIN (obj)
1231                     + IRA_INT_BITS)
1232                    / IRA_INT_BITS) * sizeof (IRA_INT_TYPE);
1233       i->bit_num = 0;
1234       i->base_conflict_id = OBJECT_MIN (obj);
1235       i->word = (i->size == 0 ? 0 : ((IRA_INT_TYPE *) i->vec)[0]);
1236     }
1237 }
1238
1239 /* Return TRUE if we have more conflicting allocnos to visit, in which
1240    case *A is set to the allocno to be visited.  Otherwise, return
1241    FALSE.  */
1242 static inline bool
1243 ira_object_conflict_iter_cond (ira_object_conflict_iterator *i,
1244                                ira_object_t *pobj)
1245 {
1246   ira_object_t obj;
1247
1248   if (i->conflict_vec_p)
1249     {
1250       obj = ((ira_object_t *) i->vec)[i->word_num++];
1251       if (obj == NULL)
1252         return false;
1253     }
1254   else
1255     {
1256       unsigned IRA_INT_TYPE word = i->word;
1257       unsigned int bit_num = i->bit_num;
1258
1259       /* Skip words that are zeros.  */
1260       for (; word == 0; word = ((IRA_INT_TYPE *) i->vec)[i->word_num])
1261         {
1262           i->word_num++;
1263
1264           /* If we have reached the end, break.  */
1265           if (i->word_num * sizeof (IRA_INT_TYPE) >= i->size)
1266             return false;
1267
1268           bit_num = i->word_num * IRA_INT_BITS;
1269         }
1270
1271       /* Skip bits that are zero.  */
1272       for (; (word & 1) == 0; word >>= 1)
1273         bit_num++;
1274
1275       obj = ira_object_id_map[bit_num + i->base_conflict_id];
1276       i->bit_num = bit_num + 1;
1277       i->word = word >> 1;
1278     }
1279
1280   *pobj = obj;
1281   return true;
1282 }
1283
1284 /* Loop over all objects conflicting with OBJ.  In each iteration,
1285    CONF is set to the next conflicting object.  ITER is an instance
1286    of ira_object_conflict_iterator used to iterate the conflicts.  */
1287 #define FOR_EACH_OBJECT_CONFLICT(OBJ, CONF, ITER)                       \
1288   for (ira_object_conflict_iter_init (&(ITER), (OBJ));                  \
1289        ira_object_conflict_iter_cond (&(ITER), &(CONF));)
1290
1291 \f
1292
1293 /* The function returns TRUE if at least one hard register from ones
1294    starting with HARD_REGNO and containing value of MODE are in set
1295    HARD_REGSET.  */
1296 static inline bool
1297 ira_hard_reg_set_intersection_p (int hard_regno, enum machine_mode mode,
1298                                  HARD_REG_SET hard_regset)
1299 {
1300   int i;
1301
1302   gcc_assert (hard_regno >= 0);
1303   for (i = hard_regno_nregs[hard_regno][mode] - 1; i >= 0; i--)
1304     if (TEST_HARD_REG_BIT (hard_regset, hard_regno + i))
1305       return true;
1306   return false;
1307 }
1308
1309 /* Return number of hard registers in hard register SET.  */
1310 static inline int
1311 hard_reg_set_size (HARD_REG_SET set)
1312 {
1313   int i, size;
1314
1315   for (size = i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1316     if (TEST_HARD_REG_BIT (set, i))
1317       size++;
1318   return size;
1319 }
1320
1321 /* The function returns TRUE if hard registers starting with
1322    HARD_REGNO and containing value of MODE are fully in set
1323    HARD_REGSET.  */
1324 static inline bool
1325 ira_hard_reg_in_set_p (int hard_regno, enum machine_mode mode,
1326                        HARD_REG_SET hard_regset)
1327 {
1328   int i;
1329
1330   ira_assert (hard_regno >= 0);
1331   for (i = hard_regno_nregs[hard_regno][mode] - 1; i >= 0; i--)
1332     if (!TEST_HARD_REG_BIT (hard_regset, hard_regno + i))
1333       return false;
1334   return true;
1335 }
1336
1337 \f
1338
1339 /* To save memory we use a lazy approach for allocation and
1340    initialization of the cost vectors.  We do this only when it is
1341    really necessary.  */
1342
1343 /* Allocate cost vector *VEC for hard registers of ACLASS and
1344    initialize the elements by VAL if it is necessary */
1345 static inline void
1346 ira_allocate_and_set_costs (int **vec, reg_class_t aclass, int val)
1347 {
1348   int i, *reg_costs;
1349   int len;
1350
1351   if (*vec != NULL)
1352     return;
1353   *vec = reg_costs = ira_allocate_cost_vector (aclass);
1354   len = ira_class_hard_regs_num[(int) aclass];
1355   for (i = 0; i < len; i++)
1356     reg_costs[i] = val;
1357 }
1358
1359 /* Allocate cost vector *VEC for hard registers of ACLASS and copy
1360    values of vector SRC into the vector if it is necessary */
1361 static inline void
1362 ira_allocate_and_copy_costs (int **vec, enum reg_class aclass, int *src)
1363 {
1364   int len;
1365
1366   if (*vec != NULL || src == NULL)
1367     return;
1368   *vec = ira_allocate_cost_vector (aclass);
1369   len = ira_class_hard_regs_num[aclass];
1370   memcpy (*vec, src, sizeof (int) * len);
1371 }
1372
1373 /* Allocate cost vector *VEC for hard registers of ACLASS and add
1374    values of vector SRC into the vector if it is necessary */
1375 static inline void
1376 ira_allocate_and_accumulate_costs (int **vec, enum reg_class aclass, int *src)
1377 {
1378   int i, len;
1379
1380   if (src == NULL)
1381     return;
1382   len = ira_class_hard_regs_num[aclass];
1383   if (*vec == NULL)
1384     {
1385       *vec = ira_allocate_cost_vector (aclass);
1386       memset (*vec, 0, sizeof (int) * len);
1387     }
1388   for (i = 0; i < len; i++)
1389     (*vec)[i] += src[i];
1390 }
1391
1392 /* Allocate cost vector *VEC for hard registers of ACLASS and copy
1393    values of vector SRC into the vector or initialize it by VAL (if
1394    SRC is null).  */
1395 static inline void
1396 ira_allocate_and_set_or_copy_costs (int **vec, enum reg_class aclass,
1397                                     int val, int *src)
1398 {
1399   int i, *reg_costs;
1400   int len;
1401
1402   if (*vec != NULL)
1403     return;
1404   *vec = reg_costs = ira_allocate_cost_vector (aclass);
1405   len = ira_class_hard_regs_num[aclass];
1406   if (src != NULL)
1407     memcpy (reg_costs, src, sizeof (int) * len);
1408   else
1409     {
1410       for (i = 0; i < len; i++)
1411         reg_costs[i] = val;
1412     }
1413 }