📄 global-alloc.c
字号:
/* Allocate registers for pseudo-registers that span basic blocks. Copyright (C) 1987, 1988 Free Software Foundation, Inc.This file is part of GNU CC.GNU CC is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 1, or (at your option)any later version.GNU CC is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU CC; see the file COPYING. If not, write tothe Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */#include <stdio.h>#include "config.h"#include "rtl.h"#include "flags.h"#include "basic-block.h"#include "hard-reg-set.h"#include "regs.h"#include "insn-config.h"/* This pass of the compiler performs global register allocation. It assigns hard register numbers to all the pseudo registers that were not handled in local_alloc. Assignments are recorded in the vector reg_renumber, not by changing the rtl code. (Such changes are made by final). The entry point is the function global_alloc. After allocation is complete, the reload pass is run as a subroutine of this pass, so that when a pseudo reg loses its hard reg due to spilling it is possible to make a second attempt to find a hard reg for it. The reload pass is independent in other respects and it is run even when stupid register allocation is in use. 1. count the pseudo-registers still needing allocation and assign allocation-numbers (allocnos) to them. Set up tables reg_allocno and allocno_reg to map reg numbers to allocnos and vice versa. max_allocno gets the number of allocnos in use. 2. Allocate a max_allocno by max_allocno conflict bit matrix and clear it. Allocate a max_allocno by FIRST_PSEUDO_REGISTER conflict matrix for conflicts between allocnos and explicit hard register use (which includes use of pseudo-registers allocated by local_alloc). 3. for each basic block walk forward through the block, recording which unallocated registers and which hardware registers are live. Build the conflict matrix between the unallocated registers and another of unallocated registers versus hardware registers. Also record the preferred hardware registers for each unallocated one. 4. Sort a table of the allocnos into order of desirability of the variables. 5. Allocate the variables in that order; each if possible into a preferred register, else into another register. *//* Number of pseudo-registers still requiring allocation (not allocated by local_allocate). */static int max_allocno;/* Indexed by (pseudo) reg number, gives the allocno, or -1 for pseudo registers already allocated by local_allocate. */static int *reg_allocno;/* Indexed by allocno, gives the reg number. */static int *allocno_reg;/* A vector of the integers from 0 to max_allocno-1, sorted in the order of first-to-be-allocated first. */static int *allocno_order;/* Indexed by an allocno, gives the number of consecutive hard registers needed by that pseudo reg. */static int *allocno_size;/* max_allocno by max_allocno array of bits, recording whether two allocno's conflict (can't go in the same hardware register). `conflicts' is not symmetric; a conflict between allocno's i and j is recorded either in element i,j or in element j,i. */static int *conflicts;/* Number of ints require to hold max_allocno bits. This is the length of a row in `conflicts'. */static int allocno_row_words;/* Two macros to test or store 1 in an element of `conflicts'. */#define CONFLICTP(I, J) \ (conflicts[(I) * allocno_row_words + (J) / INT_BITS] \ & (1 << ((J) % INT_BITS)))#define SET_CONFLICT(I, J) \ (conflicts[(I) * allocno_row_words + (J) / INT_BITS] \ |= (1 << ((J) % INT_BITS)))/* Set of hard regs currently live (during scan of all insns). */static HARD_REG_SET hard_regs_live;/* Indexed by N, set of hard regs conflicting with allocno N. */static HARD_REG_SET *hard_reg_conflicts;/* Indexed by N, set of hard regs preferred by allocno N. This is used to make allocnos go into regs that are copied to or from them, when possible, to reduce register shuffling. */static HARD_REG_SET *hard_reg_preferences;/* Set of registers that some allocno has a preference for. */static HARD_REG_SET regs_someone_prefers;/* Set of registers that global-alloc isn't supposed to use. */static HARD_REG_SET no_global_alloc_regs;/* Test a bit in TABLE, a vector of HARD_REG_SETs, for vector element I, and hard register number J. */#define REGBITP(TABLE, I, J) TEST_HARD_REG_BIT (TABLE[I], J)/* Set to 1 a bit in a vector of HARD_REG_SETs. Works like REGBITP. */#define SET_REGBIT(TABLE, I, J) SET_HARD_REG_BIT (TABLE[I], J)/* Bit mask for allocnos live at current point in the scan. */static int *allocnos_live;#define INT_BITS HOST_BITS_PER_INT/* Test, set or clear bit number I in allocnos_live, a bit vector indexed by allocno. */#define ALLOCNO_LIVE_P(I) \ (allocnos_live[(I) / INT_BITS] & (1 << ((I) % INT_BITS)))#define SET_ALLOCNO_LIVE(I) \ (allocnos_live[(I) / INT_BITS] |= (1 << ((I) % INT_BITS)))#define CLEAR_ALLOCNO_LIVE(I) \ (allocnos_live[(I) / INT_BITS] &= ~(1 << ((I) % INT_BITS)))/* Record all regs that are set in any one insn. Communication from mark_reg_{store,clobber} and global_conflicts. */static rtx *regs_set;static int n_regs_set;static int allocno_compare ();static void mark_reg_store ();static void mark_reg_clobber ();static void mark_reg_live_nc ();static void mark_reg_death ();static void dump_conflicts ();static void find_reg ();static void global_conflicts ();static void record_conflicts ();static void set_preference ();/* Perform allocation of pseudo-registers not allocated by local_alloc. FILE is a file to output debugging information on, or zero if such output is not desired. */voidglobal_alloc (file) FILE *file;{ register int i; max_allocno = 0; CLEAR_HARD_REG_SET (regs_someone_prefers); /* A machine may have certain hard registers that are safe to use only within a basic block. */ CLEAR_HARD_REG_SET (no_global_alloc_regs);#ifdef OVERLAPPING_REGNO_P for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) if (OVERLAPPING_REGNO_P (i)) SET_HARD_REG_BIT (no_global_alloc_regs, i);#endif /* Establish mappings from register number to allocation number and vice versa. In the process, count the allocnos. */ reg_allocno = (int *) alloca (max_regno * sizeof (int)); for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) reg_allocno[i] = -1; for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++) /* Note that reg_live_length[i] < 0 indicates a "constant" reg that we are supposed to refrain from putting in a hard reg. -2 means do make an allocno but don't allocate it. */ if (reg_n_refs[i] != 0 && reg_renumber[i] < 0 && reg_live_length[i] != -1) { reg_allocno[i] = max_allocno++; if (reg_live_length[i] == 0) abort (); } else reg_allocno[i] = -1; allocno_reg = (int *) alloca (max_allocno * sizeof (int)); allocno_size = (int *) alloca (max_allocno * sizeof (int)); bzero (allocno_size, max_allocno * sizeof (int)); for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++) if (reg_allocno[i] >= 0) { allocno_reg[reg_allocno[i]] = i; allocno_size[reg_allocno[i]] = PSEUDO_REGNO_SIZE (i); } /* Allocate the space for the conflict tables. */ hard_reg_conflicts = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET)); bzero (hard_reg_conflicts, max_allocno * sizeof (HARD_REG_SET)); hard_reg_preferences = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET)); bzero (hard_reg_preferences, max_allocno * sizeof (HARD_REG_SET)); allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS; conflicts = (int *) alloca (max_allocno * allocno_row_words * sizeof (int)); bzero (conflicts, max_allocno * allocno_row_words * sizeof (int)); allocnos_live = (int *) alloca (allocno_row_words * sizeof (int)); /* If there is work to be done (at least one reg to allocate), perform global conflict analysis and allocate the regs. */ if (max_allocno > 0) { /* Scan all the insns and compute the conflicts among allocnos and between allocnos and hard regs. */ global_conflicts (); /* Determine the order to allocate the remaining pseudo registers. */ allocno_order = (int *) alloca (max_allocno * sizeof (int)); for (i = 0; i < max_allocno; i++) allocno_order[i] = i; /* Default the size to 1, since allocno_compare uses it to divide by. */ for (i = 0; i < max_allocno; i++) if (allocno_size[i] == 0) allocno_size[i] = 1; qsort (allocno_order, max_allocno, sizeof (int), allocno_compare); if (file) dump_conflicts (file); /* Try allocating them, one by one, in that order, except for parameters marked with reg_live_length[regno] == -2. */ for (i = 0; i < max_allocno; i++) if (reg_live_length[allocno_reg[allocno_order[i]]] >= 0) { /* If we have more than one register class, first try allocating in the class that is cheapest for this pseudo-reg. If that fails, try any reg. */ if (N_REG_CLASSES > 1) { find_reg (allocno_order[i], 0, 0, 0, hard_reg_preferences[allocno_order[i]]); if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0) continue; } if (!reg_preferred_or_nothing (allocno_reg[allocno_order[i]])) find_reg (allocno_order[i], 0, 1, 0, hard_reg_preferences[allocno_order[i]]); } } /* Do the reloads now while the allocno data still exist, so that we can try to assign new hard regs to any pseudo regs that are spilled. */ if (n_basic_blocks > 0) reload (basic_block_head[0], 1, file);}/* Sort predicate for ordering the allocnos. Returns -1 (1) if *v1 should be allocated before (after) *v2. */static intallocno_compare (v1, v2) int *v1, *v2;{ register int r1 = allocno_reg[*v1]; register int r2 = allocno_reg[*v2]; /* Note that the quotient will never be bigger than the value of floor_log2 times the maximum number of times a register can occur in one insn (surely less than 100). Multiplying this by 10000 can't overflow. */ register int pri1 = (((double) (floor_log2 (reg_n_refs[r1]) * reg_n_refs[r1]) / (reg_live_length[r1] * allocno_size[*v1])) * 10000); register int pri2 = (((double) (floor_log2 (reg_n_refs[r2]) * reg_n_refs[r2]) / (reg_live_length[r2] * allocno_size[*v2])) * 10000); if (pri2 - pri1) return pri2 - pri1; /* If regs are equally good, sort by allocno, so that the results of qsort leave nothing to chance. */ return *v1 - *v2;}/* Scan the rtl code and record all conflicts in the conflict matrices. */static voidglobal_conflicts (){ register int b, i; register rtx insn; short *block_start_allocnos; /* Make a vector that mark_reg_{store,clobber} will store in. */ regs_set = (rtx *) alloca (max_parallel * sizeof (rtx) * 2); block_start_allocnos = (short *) alloca (max_allocno * sizeof (short)); for (b = 0; b < n_basic_blocks; b++) { bzero (allocnos_live, allocno_row_words * sizeof (int)); /* Initialize table of registers currently live to the state at the beginning of this basic block. This also marks the conflicts among them. For pseudo-regs, there is only one bit for each one no matter how many hard regs it occupies.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -