]> gcc.gnu.org Git - gcc.git/blob - gcc/cfgexpand.c
cfgexpand.c, [...]: Fix comment typos.
[gcc.git] / gcc / cfgexpand.c
1 /* A pass for lowering trees to RTL.
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "expr.h"
31 #include "langhooks.h"
32 #include "tree-flow.h"
33 #include "timevar.h"
34 #include "tree-dump.h"
35 #include "tree-pass.h"
36 #include "except.h"
37 #include "flags.h"
38 #include "diagnostic.h"
39 #include "toplev.h"
40
41 /* Verify that there is exactly single jump instruction since last and attach
42 REG_BR_PROB note specifying probability.
43 ??? We really ought to pass the probability down to RTL expanders and let it
44 re-distribute it when the conditional expands into multiple conditionals.
45 This is however difficult to do. */
46 static void
47 add_reg_br_prob_note (FILE *dump_file, rtx last, int probability)
48 {
49 if (profile_status == PROFILE_ABSENT)
50 return;
51 for (last = NEXT_INSN (last); last && NEXT_INSN (last); last = NEXT_INSN (last))
52 if (JUMP_P (last))
53 {
54 /* It is common to emit condjump-around-jump sequence when we don't know
55 how to reverse the conditional. Special case this. */
56 if (!any_condjump_p (last)
57 || !JUMP_P (NEXT_INSN (last))
58 || !simplejump_p (NEXT_INSN (last))
59 || !BARRIER_P (NEXT_INSN (NEXT_INSN (last)))
60 || !LABEL_P (NEXT_INSN (NEXT_INSN (NEXT_INSN (last))))
61 || NEXT_INSN (NEXT_INSN (NEXT_INSN (NEXT_INSN (last)))))
62 goto failed;
63 gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
64 REG_NOTES (last)
65 = gen_rtx_EXPR_LIST (REG_BR_PROB,
66 GEN_INT (REG_BR_PROB_BASE - probability),
67 REG_NOTES (last));
68 return;
69 }
70 if (!last || !JUMP_P (last) || !any_condjump_p (last))
71 goto failed;
72 gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
73 REG_NOTES (last)
74 = gen_rtx_EXPR_LIST (REG_BR_PROB,
75 GEN_INT (probability), REG_NOTES (last));
76 return;
77 failed:
78 if (dump_file)
79 fprintf (dump_file, "Failed to add probability note\n");
80 }
81
82
83 #ifndef LOCAL_ALIGNMENT
84 #define LOCAL_ALIGNMENT(TYPE, ALIGNMENT) ALIGNMENT
85 #endif
86
87 #ifndef STACK_ALIGNMENT_NEEDED
88 #define STACK_ALIGNMENT_NEEDED 1
89 #endif
90
91 #ifdef FRAME_GROWS_DOWNWARD
92 # undef FRAME_GROWS_DOWNWARD
93 # define FRAME_GROWS_DOWNWARD 1
94 #else
95 # define FRAME_GROWS_DOWNWARD 0
96 #endif
97
98
99 /* This structure holds data relevant to one variable that will be
100 placed in a stack slot. */
101 struct stack_var
102 {
103 /* The Variable. */
104 tree decl;
105
106 /* The offset of the variable. During partitioning, this is the
107 offset relative to the partition. After partitioning, this
108 is relative to the stack frame. */
109 HOST_WIDE_INT offset;
110
111 /* Initially, the size of the variable. Later, the size of the partition,
112 if this variable becomes it's partition's representative. */
113 HOST_WIDE_INT size;
114
115 /* The *byte* alignment required for this variable. Or as, with the
116 size, the alignment for this partition. */
117 unsigned int alignb;
118
119 /* The partition representative. */
120 size_t representative;
121
122 /* The next stack variable in the partition, or EOC. */
123 size_t next;
124 };
125
126 #define EOC ((size_t)-1)
127
128 /* We have an array of such objects while deciding allocation. */
129 static struct stack_var *stack_vars;
130 static size_t stack_vars_alloc;
131 static size_t stack_vars_num;
132
133 /* An array of indicies such that stack_vars[stack_vars_sorted[i]].size
134 is non-decreasing. */
135 static size_t *stack_vars_sorted;
136
137 /* We have an interference graph between such objects. This graph
138 is lower triangular. */
139 static bool *stack_vars_conflict;
140 static size_t stack_vars_conflict_alloc;
141
142 /* The phase of the stack frame. This is the known misalignment of
143 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
144 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
145 static int frame_phase;
146
147
148 /* Discover the byte alignment to use for DECL. Ignore alignment
149 we can't do with expected alignment of the stack boundary. */
150
151 static unsigned int
152 get_decl_align_unit (tree decl)
153 {
154 unsigned int align;
155
156 align = DECL_ALIGN (decl);
157 align = LOCAL_ALIGNMENT (TREE_TYPE (decl), align);
158 if (align > PREFERRED_STACK_BOUNDARY)
159 align = PREFERRED_STACK_BOUNDARY;
160 if (cfun->stack_alignment_needed < align)
161 cfun->stack_alignment_needed = align;
162
163 return align / BITS_PER_UNIT;
164 }
165
166 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
167 Return the frame offset. */
168
169 static HOST_WIDE_INT
170 alloc_stack_frame_space (HOST_WIDE_INT size, HOST_WIDE_INT align)
171 {
172 HOST_WIDE_INT offset, new_frame_offset;
173
174 new_frame_offset = frame_offset;
175 if (FRAME_GROWS_DOWNWARD)
176 {
177 new_frame_offset -= size + frame_phase;
178 new_frame_offset &= -align;
179 new_frame_offset += frame_phase;
180 offset = new_frame_offset;
181 }
182 else
183 {
184 new_frame_offset -= frame_phase;
185 new_frame_offset += align - 1;
186 new_frame_offset &= -align;
187 new_frame_offset += frame_phase;
188 offset = new_frame_offset;
189 new_frame_offset += size;
190 }
191 frame_offset = new_frame_offset;
192
193 return offset;
194 }
195
196 /* Accumulate DECL into STACK_VARS. */
197
198 static void
199 add_stack_var (tree decl)
200 {
201 if (stack_vars_num >= stack_vars_alloc)
202 {
203 if (stack_vars_alloc)
204 stack_vars_alloc = stack_vars_alloc * 3 / 2;
205 else
206 stack_vars_alloc = 32;
207 stack_vars
208 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
209 }
210 stack_vars[stack_vars_num].decl = decl;
211 stack_vars[stack_vars_num].offset = 0;
212 stack_vars[stack_vars_num].size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
213 stack_vars[stack_vars_num].alignb = get_decl_align_unit (decl);
214
215 /* All variables are initially in their own partition. */
216 stack_vars[stack_vars_num].representative = stack_vars_num;
217 stack_vars[stack_vars_num].next = EOC;
218
219 /* Ensure that this decl doesn't get put onto the list twice. */
220 SET_DECL_RTL (decl, pc_rtx);
221
222 stack_vars_num++;
223 }
224
225 /* Compute the linear index of a lower-triangular coordinate (I, J). */
226
227 static size_t
228 triangular_index (size_t i, size_t j)
229 {
230 if (i < j)
231 {
232 size_t t;
233 t = i, i = j, j = t;
234 }
235 return (i * (i + 1)) / 2 + j;
236 }
237
238 /* Ensure that STACK_VARS_CONFLICT is large enough for N objects. */
239
240 static void
241 resize_stack_vars_conflict (size_t n)
242 {
243 size_t size = triangular_index (n-1, n-1) + 1;
244
245 if (size <= stack_vars_conflict_alloc)
246 return;
247
248 stack_vars_conflict = XRESIZEVEC (bool, stack_vars_conflict, size);
249 memset (stack_vars_conflict + stack_vars_conflict_alloc, 0,
250 (size - stack_vars_conflict_alloc) * sizeof (bool));
251 stack_vars_conflict_alloc = size;
252 }
253
254 /* Make the decls associated with luid's X and Y conflict. */
255
256 static void
257 add_stack_var_conflict (size_t x, size_t y)
258 {
259 size_t index = triangular_index (x, y);
260 gcc_assert (index < stack_vars_conflict_alloc);
261 stack_vars_conflict[index] = true;
262 }
263
264 /* Check whether the decls associated with luid's X and Y conflict. */
265
266 static bool
267 stack_var_conflict_p (size_t x, size_t y)
268 {
269 size_t index = triangular_index (x, y);
270 gcc_assert (index < stack_vars_conflict_alloc);
271 return stack_vars_conflict[index];
272 }
273
274 /* A subroutine of expand_used_vars. If two variables X and Y have alias
275 sets that do not conflict, then do add a conflict for these variables
276 in the interference graph. We also have to mind MEM_IN_STRUCT_P and
277 MEM_SCALAR_P. */
278
279 static void
280 add_alias_set_conflicts (void)
281 {
282 size_t i, j, n = stack_vars_num;
283
284 for (i = 0; i < n; ++i)
285 {
286 bool aggr_i = AGGREGATE_TYPE_P (TREE_TYPE (stack_vars[i].decl));
287 HOST_WIDE_INT set_i = get_alias_set (stack_vars[i].decl);
288
289 for (j = 0; j < i; ++j)
290 {
291 bool aggr_j = AGGREGATE_TYPE_P (TREE_TYPE (stack_vars[j].decl));
292 HOST_WIDE_INT set_j = get_alias_set (stack_vars[j].decl);
293 if (aggr_i != aggr_j || !alias_sets_conflict_p (set_i, set_j))
294 add_stack_var_conflict (i, j);
295 }
296 }
297 }
298
299 /* A subroutine of partition_stack_vars. A comparison function for qsort,
300 sorting an array of indicies by the size of the object. */
301
302 static int
303 stack_var_size_cmp (const void *a, const void *b)
304 {
305 HOST_WIDE_INT sa = stack_vars[*(const size_t *)a].size;
306 HOST_WIDE_INT sb = stack_vars[*(const size_t *)b].size;
307
308 if (sa < sb)
309 return -1;
310 if (sa > sb)
311 return 1;
312 return 0;
313 }
314
315 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
316 partitioning algorithm. Partitions A and B are known to be non-conflicting.
317 Merge them into a single partition A.
318
319 At the same time, add OFFSET to all variables in partition B. At the end
320 of the partitioning process we've have a nice block easy to lay out within
321 the stack frame. */
322
323 static void
324 union_stack_vars (size_t a, size_t b, HOST_WIDE_INT offset)
325 {
326 size_t i, last;
327
328 /* Update each element of partition B with the given offset,
329 and merge them into partition A. */
330 for (last = i = b; i != EOC; last = i, i = stack_vars[i].next)
331 {
332 stack_vars[i].offset += offset;
333 stack_vars[i].representative = a;
334 }
335 stack_vars[last].next = stack_vars[a].next;
336 stack_vars[a].next = b;
337
338 /* Update the required alignment of partition A to account for B. */
339 if (stack_vars[a].alignb < stack_vars[b].alignb)
340 stack_vars[a].alignb = stack_vars[b].alignb;
341
342 /* Update the interference graph and merge the conflicts. */
343 for (last = stack_vars_num, i = 0; i < last; ++i)
344 if (stack_var_conflict_p (b, i))
345 add_stack_var_conflict (a, i);
346 }
347
348 /* A subroutine of expand_used_vars. Binpack the variables into
349 partitions constrained by the interference graph. The overall
350 algorithm used is as follows:
351
352 Sort the objects by size.
353 For each object A {
354 S = size(A)
355 O = 0
356 loop {
357 Look for the largest non-conflicting object B with size <= S.
358 UNION (A, B)
359 offset(B) = O
360 O += size(B)
361 S -= size(B)
362 }
363 }
364 */
365
366 static void
367 partition_stack_vars (void)
368 {
369 size_t si, sj, n = stack_vars_num;
370
371 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
372 for (si = 0; si < n; ++si)
373 stack_vars_sorted[si] = si;
374
375 if (n == 1)
376 return;
377
378 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_size_cmp);
379
380 /* Special case: detect when all variables conflict, and thus we can't
381 do anything during the partitioning loop. It isn't uncommon (with
382 C code at least) to declare all variables at the top of the function,
383 and if we're not inlining, then all variables will be in the same scope.
384 Take advantage of very fast libc routines for this scan. */
385 gcc_assert (sizeof(bool) == sizeof(char));
386 if (memchr (stack_vars_conflict, false, stack_vars_conflict_alloc) == NULL)
387 return;
388
389 for (si = 0; si < n; ++si)
390 {
391 size_t i = stack_vars_sorted[si];
392 HOST_WIDE_INT isize = stack_vars[i].size;
393 HOST_WIDE_INT offset = 0;
394
395 for (sj = si; sj-- > 0; )
396 {
397 size_t j = stack_vars_sorted[sj];
398 HOST_WIDE_INT jsize = stack_vars[j].size;
399 unsigned int jalign = stack_vars[j].alignb;
400
401 /* Ignore objects that aren't partition representatives. */
402 if (stack_vars[j].representative != j)
403 continue;
404
405 /* Ignore objects too large for the remaining space. */
406 if (isize < jsize)
407 continue;
408
409 /* Ignore conflicting objects. */
410 if (stack_var_conflict_p (i, j))
411 continue;
412
413 /* Refine the remaining space check to include alignment. */
414 if (offset & (jalign - 1))
415 {
416 HOST_WIDE_INT toff = offset;
417 toff += jalign - 1;
418 toff &= -(HOST_WIDE_INT)jalign;
419 if (isize - (toff - offset) < jsize)
420 continue;
421
422 isize -= toff - offset;
423 offset = toff;
424 }
425
426 /* UNION the objects, placing J at OFFSET. */
427 union_stack_vars (i, j, offset);
428
429 isize -= jsize;
430 if (isize == 0)
431 break;
432 }
433 }
434 }
435
436 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
437
438 static void
439 dump_stack_var_partition (void)
440 {
441 size_t si, i, j, n = stack_vars_num;
442
443 for (si = 0; si < n; ++si)
444 {
445 i = stack_vars_sorted[si];
446
447 /* Skip variables that aren't partition representatives, for now. */
448 if (stack_vars[i].representative != i)
449 continue;
450
451 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
452 " align %u\n", (unsigned long) i, stack_vars[i].size,
453 stack_vars[i].alignb);
454
455 for (j = i; j != EOC; j = stack_vars[j].next)
456 {
457 fputc ('\t', dump_file);
458 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
459 fprintf (dump_file, ", offset " HOST_WIDE_INT_PRINT_DEC "\n",
460 stack_vars[i].offset);
461 }
462 }
463 }
464
465 /* Assign rtl to DECL at frame offset OFFSET. */
466
467 static void
468 expand_one_stack_var_at (tree decl, HOST_WIDE_INT offset)
469 {
470 HOST_WIDE_INT align;
471 rtx x;
472
473 /* If this fails, we've overflowed the stack frame. Error nicely? */
474 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
475
476 x = plus_constant (virtual_stack_vars_rtx, offset);
477 x = gen_rtx_MEM (DECL_MODE (decl), x);
478
479 /* Set alignment we actually gave this decl. */
480 offset -= frame_phase;
481 align = offset & -offset;
482 align *= BITS_PER_UNIT;
483 if (align > STACK_BOUNDARY || align == 0)
484 align = STACK_BOUNDARY;
485 DECL_ALIGN (decl) = align;
486 DECL_USER_ALIGN (decl) = 0;
487
488 set_mem_attributes (x, decl, true);
489 SET_DECL_RTL (decl, x);
490 }
491
492 /* A subroutine of expand_used_vars. Give each partition representative
493 a unique location within the stack frame. Update each partition member
494 with that location. */
495
496 static void
497 expand_stack_vars (void)
498 {
499 size_t si, i, j, n = stack_vars_num;
500
501 for (si = 0; si < n; ++si)
502 {
503 HOST_WIDE_INT offset;
504
505 i = stack_vars_sorted[si];
506
507 /* Skip variables that aren't partition representatives, for now. */
508 if (stack_vars[i].representative != i)
509 continue;
510
511 offset = alloc_stack_frame_space (stack_vars[i].size,
512 stack_vars[i].alignb);
513
514 /* Create rtl for each variable based on their location within the
515 partition. */
516 for (j = i; j != EOC; j = stack_vars[j].next)
517 expand_one_stack_var_at (stack_vars[j].decl,
518 stack_vars[j].offset + offset);
519 }
520 }
521
522 /* A subroutine of expand_one_var. Called to immediately assign rtl
523 to a variable to be allocated in the stack frame. */
524
525 static void
526 expand_one_stack_var (tree var)
527 {
528 HOST_WIDE_INT size, offset, align;
529
530 size = tree_low_cst (DECL_SIZE_UNIT (var), 1);
531 align = get_decl_align_unit (var);
532 offset = alloc_stack_frame_space (size, align);
533
534 expand_one_stack_var_at (var, offset);
535 }
536
537 /* A subroutine of expand_one_var. Called to assign rtl
538 to a TREE_STATIC VAR_DECL. */
539
540 static void
541 expand_one_static_var (tree var)
542 {
543 /* If this is an inlined copy of a static local variable,
544 look up the original. */
545 var = DECL_ORIGIN (var);
546
547 /* If we've already processed this variable because of that, do nothing. */
548 if (TREE_ASM_WRITTEN (var))
549 return;
550
551 /* Give the front end a chance to do whatever. In practice, this is
552 resolving duplicate names for IMA in C. */
553 if (lang_hooks.expand_decl (var))
554 return;
555
556 /* Otherwise, just emit the variable. */
557 rest_of_decl_compilation (var, 0, 0);
558 }
559
560 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
561 that will reside in a hard register. */
562
563 static void
564 expand_one_hard_reg_var (tree var)
565 {
566 rest_of_decl_compilation (var, 0, 0);
567 }
568
569 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
570 that will reside in a pseudo register. */
571
572 static void
573 expand_one_register_var (tree var)
574 {
575 tree type = TREE_TYPE (var);
576 int unsignedp = TYPE_UNSIGNED (type);
577 enum machine_mode reg_mode
578 = promote_mode (type, DECL_MODE (var), &unsignedp, 0);
579 rtx x = gen_reg_rtx (reg_mode);
580
581 SET_DECL_RTL (var, x);
582
583 /* Note if the object is a user variable. */
584 if (!DECL_ARTIFICIAL (var))
585 {
586 mark_user_reg (x);
587
588 /* Trust user variables which have a pointer type to really
589 be pointers. Do not trust compiler generated temporaries
590 as our type system is totally busted as it relates to
591 pointer arithmetic which translates into lots of compiler
592 generated objects with pointer types, but which are not really
593 pointers. */
594 if (POINTER_TYPE_P (type))
595 mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var))));
596 }
597 }
598
599 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
600 has some associated error, e.g. its type is error-mark. We just need
601 to pick something that won't crash the rest of the compiler. */
602
603 static void
604 expand_one_error_var (tree var)
605 {
606 enum machine_mode mode = DECL_MODE (var);
607 rtx x;
608
609 if (mode == BLKmode)
610 x = gen_rtx_MEM (BLKmode, const0_rtx);
611 else if (mode == VOIDmode)
612 x = const0_rtx;
613 else
614 x = gen_reg_rtx (mode);
615
616 SET_DECL_RTL (var, x);
617 }
618
619 /* A subroutine of expand_one_var. VAR is a variable that will be
620 allocated to the local stack frame. Return true if we wish to
621 add VAR to STACK_VARS so that it will be coalesced with other
622 variables. Return false to allocate VAR immediately.
623
624 This function is used to reduce the number of variables considered
625 for coalescing, which reduces the size of the quadratic problem. */
626
627 static bool
628 defer_stack_allocation (tree var, bool toplevel)
629 {
630 /* Variables in the outermost scope automatically conflict with
631 every other variable. The only reason to want to defer them
632 at all is that, after sorting, we can more efficiently pack
633 small variables in the stack frame. Continue to defer at -O2. */
634 if (toplevel && optimize < 2)
635 return false;
636
637 /* Without optimization, *most* variables are allocated from the
638 stack, which makes the quadratic problem large exactly when we
639 want compilation to proceed as quickly as possible. On the
640 other hand, we don't want the function's stack frame size to
641 get completely out of hand. So we avoid adding scalars and
642 "small" aggregates to the list at all. */
643 if (optimize == 0 && tree_low_cst (DECL_SIZE_UNIT (var), 1) < 32)
644 return false;
645
646 return true;
647 }
648
649 /* A subroutine of expand_used_vars. Expand one variable according to
650 its flavor. Variables to be placed on the stack are not actually
651 expanded yet, merely recorded. */
652
653 static void
654 expand_one_var (tree var, bool toplevel)
655 {
656 if (TREE_CODE (var) != VAR_DECL)
657 lang_hooks.expand_decl (var);
658 else if (DECL_EXTERNAL (var))
659 ;
660 else if (DECL_VALUE_EXPR (var))
661 ;
662 else if (TREE_STATIC (var))
663 expand_one_static_var (var);
664 else if (DECL_RTL_SET_P (var))
665 ;
666 else if (TREE_TYPE (var) == error_mark_node)
667 expand_one_error_var (var);
668 else if (DECL_HARD_REGISTER (var))
669 expand_one_hard_reg_var (var);
670 else if (use_register_for_decl (var))
671 expand_one_register_var (var);
672 else if (defer_stack_allocation (var, toplevel))
673 add_stack_var (var);
674 else
675 expand_one_stack_var (var);
676 }
677
678 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
679 expanding variables. Those variables that can be put into registers
680 are allocated pseudos; those that can't are put on the stack.
681
682 TOPLEVEL is true if this is the outermost BLOCK. */
683
684 static void
685 expand_used_vars_for_block (tree block, bool toplevel)
686 {
687 size_t i, j, old_sv_num, this_sv_num, new_sv_num;
688 tree t;
689
690 old_sv_num = toplevel ? 0 : stack_vars_num;
691
692 /* Expand all variables at this level. */
693 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
694 if (TREE_USED (t))
695 expand_one_var (t, toplevel);
696
697 this_sv_num = stack_vars_num;
698
699 /* Expand all variables at containing levels. */
700 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
701 expand_used_vars_for_block (t, false);
702
703 /* Since we do not track exact variable lifetimes (which is not even
704 possible for varibles whose address escapes), we mirror the block
705 tree in the interference graph. Here we cause all variables at this
706 level, and all sublevels, to conflict. Do make certain that a
707 variable conflicts with itself. */
708 if (old_sv_num < this_sv_num)
709 {
710 new_sv_num = stack_vars_num;
711 resize_stack_vars_conflict (new_sv_num);
712
713 for (i = old_sv_num; i < new_sv_num; ++i)
714 for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;)
715 add_stack_var_conflict (i, j);
716 }
717 }
718
719 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
720 and clear TREE_USED on all local variables. */
721
722 static void
723 clear_tree_used (tree block)
724 {
725 tree t;
726
727 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
728 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
729 TREE_USED (t) = 0;
730
731 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
732 clear_tree_used (t);
733 }
734
735 /* Expand all variables used in the function. */
736
737 static void
738 expand_used_vars (void)
739 {
740 tree t, outer_block = DECL_INITIAL (current_function_decl);
741
742 /* Compute the phase of the stack frame for this function. */
743 {
744 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
745 int off = STARTING_FRAME_OFFSET % align;
746 frame_phase = off ? align - off : 0;
747 }
748
749 /* Set TREE_USED on all variables in the unexpanded_var_list. */
750 for (t = cfun->unexpanded_var_list; t; t = TREE_CHAIN (t))
751 TREE_USED (TREE_VALUE (t)) = 1;
752
753 /* Clear TREE_USED on all variables associated with a block scope. */
754 clear_tree_used (outer_block);
755
756 /* At this point all variables on the unexpanded_var_list with TREE_USED
757 set are not associated with any block scope. Lay them out. */
758 for (t = cfun->unexpanded_var_list; t; t = TREE_CHAIN (t))
759 {
760 tree var = TREE_VALUE (t);
761 bool expand_now = false;
762
763 /* We didn't set a block for static or extern because it's hard
764 to tell the difference between a global variable (re)declared
765 in a local scope, and one that's really declared there to
766 begin with. And it doesn't really matter much, since we're
767 not giving them stack space. Expand them now. */
768 if (TREE_STATIC (var) || DECL_EXTERNAL (var))
769 expand_now = true;
770
771 /* Any variable that could have been hoisted into an SSA_NAME
772 will have been propagated anywhere the optimizers chose,
773 i.e. not confined to their original block. Allocate them
774 as if they were defined in the outermost scope. */
775 else if (is_gimple_reg (var))
776 expand_now = true;
777
778 /* If the variable is not associated with any block, then it
779 was created by the optimizers, and could be live anywhere
780 in the function. */
781 else if (TREE_USED (var))
782 expand_now = true;
783
784 /* Finally, mark all variables on the list as used. We'll use
785 this in a moment when we expand those associated with scopes. */
786 TREE_USED (var) = 1;
787
788 if (expand_now)
789 expand_one_var (var, true);
790 }
791 cfun->unexpanded_var_list = NULL_TREE;
792
793 /* At this point, all variables within the block tree with TREE_USED
794 set are actually used by the optimized function. Lay them out. */
795 expand_used_vars_for_block (outer_block, true);
796
797 if (stack_vars_num > 0)
798 {
799 /* Due to the way alias sets work, no variables with non-conflicting
800 alias sets may be assigned the same address. Add conflicts to
801 reflect this. */
802 add_alias_set_conflicts ();
803
804 /* Now that we have collected all stack variables, and have computed a
805 minimal interference graph, attempt to save some stack space. */
806 partition_stack_vars ();
807 if (dump_file)
808 dump_stack_var_partition ();
809
810 /* Assign rtl to each variable based on these partitions. */
811 expand_stack_vars ();
812
813 /* Free up stack variable graph data. */
814 XDELETEVEC (stack_vars);
815 XDELETEVEC (stack_vars_sorted);
816 XDELETEVEC (stack_vars_conflict);
817 stack_vars = NULL;
818 stack_vars_alloc = stack_vars_num = 0;
819 stack_vars_conflict = NULL;
820 stack_vars_conflict_alloc = 0;
821 }
822
823 /* If the target requires that FRAME_OFFSET be aligned, do it. */
824 if (STACK_ALIGNMENT_NEEDED)
825 {
826 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
827 if (!FRAME_GROWS_DOWNWARD)
828 frame_offset += align - 1;
829 frame_offset &= -align;
830 }
831 }
832
833
834 /* If we need to produce a detailed dump, print the tree representation
835 for STMT to the dump file. SINCE is the last RTX after which the RTL
836 generated for STMT should have been appended. */
837
838 static void
839 maybe_dump_rtl_for_tree_stmt (tree stmt, rtx since)
840 {
841 if (dump_file && (dump_flags & TDF_DETAILS))
842 {
843 fprintf (dump_file, "\n;; ");
844 print_generic_expr (dump_file, stmt, TDF_SLIM);
845 fprintf (dump_file, "\n");
846
847 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
848 }
849 }
850
851 /* A subroutine of expand_gimple_basic_block. Expand one COND_EXPR.
852 Returns a new basic block if we've terminated the current basic
853 block and created a new one. */
854
855 static basic_block
856 expand_gimple_cond_expr (basic_block bb, tree stmt)
857 {
858 basic_block new_bb, dest;
859 edge new_edge;
860 edge true_edge;
861 edge false_edge;
862 tree pred = COND_EXPR_COND (stmt);
863 tree then_exp = COND_EXPR_THEN (stmt);
864 tree else_exp = COND_EXPR_ELSE (stmt);
865 rtx last2, last;
866
867 last2 = last = get_last_insn ();
868
869 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
870 if (EXPR_LOCUS (stmt))
871 {
872 emit_line_note (*(EXPR_LOCUS (stmt)));
873 record_block_change (TREE_BLOCK (stmt));
874 }
875
876 /* These flags have no purpose in RTL land. */
877 true_edge->flags &= ~EDGE_TRUE_VALUE;
878 false_edge->flags &= ~EDGE_FALSE_VALUE;
879
880 /* We can either have a pure conditional jump with one fallthru edge or
881 two-way jump that needs to be decomposed into two basic blocks. */
882 if (TREE_CODE (then_exp) == GOTO_EXPR && IS_EMPTY_STMT (else_exp))
883 {
884 jumpif (pred, label_rtx (GOTO_DESTINATION (then_exp)));
885 add_reg_br_prob_note (dump_file, last, true_edge->probability);
886 maybe_dump_rtl_for_tree_stmt (stmt, last);
887 if (EXPR_LOCUS (then_exp))
888 emit_line_note (*(EXPR_LOCUS (then_exp)));
889 return NULL;
890 }
891 if (TREE_CODE (else_exp) == GOTO_EXPR && IS_EMPTY_STMT (then_exp))
892 {
893 jumpifnot (pred, label_rtx (GOTO_DESTINATION (else_exp)));
894 add_reg_br_prob_note (dump_file, last, false_edge->probability);
895 maybe_dump_rtl_for_tree_stmt (stmt, last);
896 if (EXPR_LOCUS (else_exp))
897 emit_line_note (*(EXPR_LOCUS (else_exp)));
898 return NULL;
899 }
900 gcc_assert (TREE_CODE (then_exp) == GOTO_EXPR
901 && TREE_CODE (else_exp) == GOTO_EXPR);
902
903 jumpif (pred, label_rtx (GOTO_DESTINATION (then_exp)));
904 add_reg_br_prob_note (dump_file, last, true_edge->probability);
905 last = get_last_insn ();
906 expand_expr (else_exp, const0_rtx, VOIDmode, 0);
907
908 BB_END (bb) = last;
909 if (BARRIER_P (BB_END (bb)))
910 BB_END (bb) = PREV_INSN (BB_END (bb));
911 update_bb_for_insn (bb);
912
913 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
914 dest = false_edge->dest;
915 redirect_edge_succ (false_edge, new_bb);
916 false_edge->flags |= EDGE_FALLTHRU;
917 new_bb->count = false_edge->count;
918 new_bb->frequency = EDGE_FREQUENCY (false_edge);
919 new_edge = make_edge (new_bb, dest, 0);
920 new_edge->probability = REG_BR_PROB_BASE;
921 new_edge->count = new_bb->count;
922 if (BARRIER_P (BB_END (new_bb)))
923 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
924 update_bb_for_insn (new_bb);
925
926 maybe_dump_rtl_for_tree_stmt (stmt, last2);
927
928 if (EXPR_LOCUS (else_exp))
929 emit_line_note (*(EXPR_LOCUS (else_exp)));
930
931 return new_bb;
932 }
933
934 /* A subroutine of expand_gimple_basic_block. Expand one CALL_EXPR
935 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
936 generated a tail call (something that might be denied by the ABI
937 rules governing the call; see calls.c).
938
939 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
940 can still reach the rest of BB. The case here is __builtin_sqrt,
941 where the NaN result goes through the external function (with a
942 tailcall) and the normal result happens via a sqrt instruction. */
943
944 static basic_block
945 expand_gimple_tailcall (basic_block bb, tree stmt, bool *can_fallthru)
946 {
947 rtx last2, last;
948 edge e;
949 edge_iterator ei;
950 int probability;
951 gcov_type count;
952
953 last2 = last = get_last_insn ();
954
955 expand_expr_stmt (stmt);
956
957 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
958 if (CALL_P (last) && SIBLING_CALL_P (last))
959 goto found;
960
961 maybe_dump_rtl_for_tree_stmt (stmt, last2);
962
963 *can_fallthru = true;
964 return NULL;
965
966 found:
967 /* ??? Wouldn't it be better to just reset any pending stack adjust?
968 Any instructions emitted here are about to be deleted. */
969 do_pending_stack_adjust ();
970
971 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
972 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
973 EH or abnormal edges, we shouldn't have created a tail call in
974 the first place. So it seems to me we should just be removing
975 all edges here, or redirecting the existing fallthru edge to
976 the exit block. */
977
978 probability = 0;
979 count = 0;
980
981 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
982 {
983 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
984 {
985 if (e->dest != EXIT_BLOCK_PTR)
986 {
987 e->dest->count -= e->count;
988 e->dest->frequency -= EDGE_FREQUENCY (e);
989 if (e->dest->count < 0)
990 e->dest->count = 0;
991 if (e->dest->frequency < 0)
992 e->dest->frequency = 0;
993 }
994 count += e->count;
995 probability += e->probability;
996 remove_edge (e);
997 }
998 else
999 ei_next (&ei);
1000 }
1001
1002 /* This is somewhat ugly: the call_expr expander often emits instructions
1003 after the sibcall (to perform the function return). These confuse the
1004 find_many_sub_basic_blocks code, so we need to get rid of these. */
1005 last = NEXT_INSN (last);
1006 gcc_assert (BARRIER_P (last));
1007
1008 *can_fallthru = false;
1009 while (NEXT_INSN (last))
1010 {
1011 /* For instance an sqrt builtin expander expands if with
1012 sibcall in the then and label for `else`. */
1013 if (LABEL_P (NEXT_INSN (last)))
1014 {
1015 *can_fallthru = true;
1016 break;
1017 }
1018 delete_insn (NEXT_INSN (last));
1019 }
1020
1021 e = make_edge (bb, EXIT_BLOCK_PTR, EDGE_ABNORMAL | EDGE_SIBCALL);
1022 e->probability += probability;
1023 e->count += count;
1024 BB_END (bb) = last;
1025 update_bb_for_insn (bb);
1026
1027 if (NEXT_INSN (last))
1028 {
1029 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1030
1031 last = BB_END (bb);
1032 if (BARRIER_P (last))
1033 BB_END (bb) = PREV_INSN (last);
1034 }
1035
1036 maybe_dump_rtl_for_tree_stmt (stmt, last2);
1037
1038 return bb;
1039 }
1040
1041 /* Expand basic block BB from GIMPLE trees to RTL. */
1042
1043 static basic_block
1044 expand_gimple_basic_block (basic_block bb, FILE * dump_file)
1045 {
1046 block_stmt_iterator bsi = bsi_start (bb);
1047 tree stmt = NULL;
1048 rtx note, last;
1049 edge e;
1050 edge_iterator ei;
1051
1052 if (dump_file)
1053 {
1054 fprintf (dump_file,
1055 "\n;; Generating RTL for tree basic block %d\n",
1056 bb->index);
1057 }
1058
1059 if (!bsi_end_p (bsi))
1060 stmt = bsi_stmt (bsi);
1061
1062 if (stmt && TREE_CODE (stmt) == LABEL_EXPR)
1063 {
1064 last = get_last_insn ();
1065
1066 expand_expr_stmt (stmt);
1067
1068 /* Java emits line number notes in the top of labels.
1069 ??? Make this go away once line number notes are obsoleted. */
1070 BB_HEAD (bb) = NEXT_INSN (last);
1071 if (NOTE_P (BB_HEAD (bb)))
1072 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
1073 bsi_next (&bsi);
1074 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
1075
1076 maybe_dump_rtl_for_tree_stmt (stmt, last);
1077 }
1078 else
1079 note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK);
1080
1081 NOTE_BASIC_BLOCK (note) = bb;
1082
1083 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
1084 {
1085 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
1086 e->flags &= ~EDGE_EXECUTABLE;
1087
1088 /* At the moment not all abnormal edges match the RTL representation.
1089 It is safe to remove them here as find_many_sub_basic_blocks will
1090 rediscover them. In the future we should get this fixed properly. */
1091 if (e->flags & EDGE_ABNORMAL)
1092 remove_edge (e);
1093 else
1094 ei_next (&ei);
1095 }
1096
1097 for (; !bsi_end_p (bsi); bsi_next (&bsi))
1098 {
1099 tree stmt = bsi_stmt (bsi);
1100 basic_block new_bb;
1101
1102 if (!stmt)
1103 continue;
1104
1105 /* Expand this statement, then evaluate the resulting RTL and
1106 fixup the CFG accordingly. */
1107 if (TREE_CODE (stmt) == COND_EXPR)
1108 {
1109 new_bb = expand_gimple_cond_expr (bb, stmt);
1110 if (new_bb)
1111 return new_bb;
1112 }
1113 else
1114 {
1115 tree call = get_call_expr_in (stmt);
1116 if (call && CALL_EXPR_TAILCALL (call))
1117 {
1118 bool can_fallthru;
1119 new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru);
1120 if (new_bb)
1121 {
1122 if (can_fallthru)
1123 bb = new_bb;
1124 else
1125 return new_bb;
1126 }
1127 }
1128 else
1129 {
1130 last = get_last_insn ();
1131 expand_expr_stmt (stmt);
1132 maybe_dump_rtl_for_tree_stmt (stmt, last);
1133 }
1134 }
1135 }
1136
1137 do_pending_stack_adjust ();
1138
1139 /* Find the block tail. The last insn in the block is the insn
1140 before a barrier and/or table jump insn. */
1141 last = get_last_insn ();
1142 if (BARRIER_P (last))
1143 last = PREV_INSN (last);
1144 if (JUMP_TABLE_DATA_P (last))
1145 last = PREV_INSN (PREV_INSN (last));
1146 BB_END (bb) = last;
1147
1148 update_bb_for_insn (bb);
1149
1150 return bb;
1151 }
1152
1153
1154 /* Create a basic block for initialization code. */
1155
1156 static basic_block
1157 construct_init_block (void)
1158 {
1159 basic_block init_block, first_block;
1160 edge e = NULL, e2;
1161 edge_iterator ei;
1162
1163 FOR_EACH_EDGE (e2, ei, ENTRY_BLOCK_PTR->succs)
1164 {
1165 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend.
1166
1167 For all other blocks this edge flag is cleared while expanding
1168 a basic block in expand_gimple_basic_block, but there we never
1169 looked at the successors of the entry block.
1170 This caused PR17513. */
1171 e2->flags &= ~EDGE_EXECUTABLE;
1172
1173 if (e2->dest == ENTRY_BLOCK_PTR->next_bb)
1174 e = e2;
1175 }
1176
1177 init_block = create_basic_block (NEXT_INSN (get_insns ()),
1178 get_last_insn (),
1179 ENTRY_BLOCK_PTR);
1180 init_block->frequency = ENTRY_BLOCK_PTR->frequency;
1181 init_block->count = ENTRY_BLOCK_PTR->count;
1182 if (e)
1183 {
1184 first_block = e->dest;
1185 redirect_edge_succ (e, init_block);
1186 e = make_edge (init_block, first_block, EDGE_FALLTHRU);
1187 }
1188 else
1189 e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1190 e->probability = REG_BR_PROB_BASE;
1191 e->count = ENTRY_BLOCK_PTR->count;
1192
1193 update_bb_for_insn (init_block);
1194 return init_block;
1195 }
1196
1197
1198 /* Create a block containing landing pads and similar stuff. */
1199
1200 static void
1201 construct_exit_block (void)
1202 {
1203 rtx head = get_last_insn ();
1204 rtx end;
1205 basic_block exit_block;
1206 edge e, e2;
1207 unsigned ix;
1208 edge_iterator ei;
1209
1210 /* Make sure the locus is set to the end of the function, so that
1211 epilogue line numbers and warnings are set properly. */
1212 #ifdef USE_MAPPED_LOCATION
1213 if (cfun->function_end_locus != UNKNOWN_LOCATION)
1214 #else
1215 if (cfun->function_end_locus.file)
1216 #endif
1217 input_location = cfun->function_end_locus;
1218
1219 /* The following insns belong to the top scope. */
1220 record_block_change (DECL_INITIAL (current_function_decl));
1221
1222 /* Generate rtl for function exit. */
1223 expand_function_end ();
1224
1225 end = get_last_insn ();
1226 if (head == end)
1227 return;
1228 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
1229 head = NEXT_INSN (head);
1230 exit_block = create_basic_block (NEXT_INSN (head), end,
1231 EXIT_BLOCK_PTR->prev_bb);
1232 exit_block->frequency = EXIT_BLOCK_PTR->frequency;
1233 exit_block->count = EXIT_BLOCK_PTR->count;
1234
1235 ix = 0;
1236 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds))
1237 {
1238 e = EDGE_PRED (EXIT_BLOCK_PTR, ix);
1239 if (!(e->flags & EDGE_ABNORMAL))
1240 redirect_edge_succ (e, exit_block);
1241 else
1242 ix++;
1243 }
1244
1245 e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1246 e->probability = REG_BR_PROB_BASE;
1247 e->count = EXIT_BLOCK_PTR->count;
1248 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds)
1249 if (e2 != e)
1250 {
1251 e->count -= e2->count;
1252 exit_block->count -= e2->count;
1253 exit_block->frequency -= EDGE_FREQUENCY (e2);
1254 }
1255 if (e->count < 0)
1256 e->count = 0;
1257 if (exit_block->count < 0)
1258 exit_block->count = 0;
1259 if (exit_block->frequency < 0)
1260 exit_block->frequency = 0;
1261 update_bb_for_insn (exit_block);
1262 }
1263
1264 /* Translate the intermediate representation contained in the CFG
1265 from GIMPLE trees to RTL.
1266
1267 We do conversion per basic block and preserve/update the tree CFG.
1268 This implies we have to do some magic as the CFG can simultaneously
1269 consist of basic blocks containing RTL and GIMPLE trees. This can
1270 confuse the CFG hooks, so be careful to not manipulate CFG during
1271 the expansion. */
1272
1273 static void
1274 tree_expand_cfg (void)
1275 {
1276 basic_block bb, init_block;
1277 sbitmap blocks;
1278
1279 /* Some backends want to know that we are expanding to RTL. */
1280 currently_expanding_to_rtl = 1;
1281
1282 /* Prepare the rtl middle end to start recording block changes. */
1283 reset_block_changes ();
1284
1285 /* Expand the variables recorded during gimple lowering. */
1286 expand_used_vars ();
1287
1288 /* Set up parameters and prepare for return, for the function. */
1289 expand_function_start (current_function_decl);
1290
1291 /* If this function is `main', emit a call to `__main'
1292 to run global initializers, etc. */
1293 if (DECL_NAME (current_function_decl)
1294 && MAIN_NAME_P (DECL_NAME (current_function_decl))
1295 && DECL_FILE_SCOPE_P (current_function_decl))
1296 expand_main_function ();
1297
1298 /* Register rtl specific functions for cfg. */
1299 rtl_register_cfg_hooks ();
1300
1301 init_block = construct_init_block ();
1302
1303 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb)
1304 bb = expand_gimple_basic_block (bb, dump_file);
1305
1306 construct_exit_block ();
1307
1308 /* We're done expanding trees to RTL. */
1309 currently_expanding_to_rtl = 0;
1310
1311 /* Convert tree EH labels to RTL EH labels, and clean out any unreachable
1312 EH regions. */
1313 convert_from_eh_region_ranges ();
1314
1315 rebuild_jump_labels (get_insns ());
1316 find_exception_handler_labels ();
1317
1318 blocks = sbitmap_alloc (last_basic_block);
1319 sbitmap_ones (blocks);
1320 find_many_sub_basic_blocks (blocks);
1321 purge_all_dead_edges ();
1322 sbitmap_free (blocks);
1323
1324 compact_blocks ();
1325 #ifdef ENABLE_CHECKING
1326 verify_flow_info();
1327 #endif
1328
1329 /* There's no need to defer outputting this function any more; we
1330 know we want to output it. */
1331 DECL_DEFER_OUTPUT (current_function_decl) = 0;
1332
1333 /* Now that we're done expanding trees to RTL, we shouldn't have any
1334 more CONCATs anywhere. */
1335 generating_concat_p = 0;
1336
1337 finalize_block_changes ();
1338
1339 if (dump_file)
1340 {
1341 fprintf (dump_file,
1342 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
1343 /* And the pass manager will dump RTL for us. */
1344 }
1345 }
1346
1347 struct tree_opt_pass pass_expand =
1348 {
1349 "expand", /* name */
1350 NULL, /* gate */
1351 tree_expand_cfg, /* execute */
1352 NULL, /* sub */
1353 NULL, /* next */
1354 0, /* static_pass_number */
1355 TV_EXPAND, /* tv_id */
1356 /* ??? If TER is enabled, we actually receive GENERIC. */
1357 PROP_gimple_leh | PROP_cfg, /* properties_required */
1358 PROP_rtl, /* properties_provided */
1359 PROP_gimple_leh, /* properties_destroyed */
1360 0, /* todo_flags_start */
1361 0, /* todo_flags_finish */
1362 'r' /* letter */
1363 };
This page took 0.096039 seconds and 6 git commands to generate.