1 /*
2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 #include "precompiled.hpp"
26 #include "opto/regalloc.hpp"
27 
28 static const int NodeRegsOverflowSize = 200;
29 
30 void (*PhaseRegAlloc::_alloc_statistics[MAX_REG_ALLOCATORS])();
31 int PhaseRegAlloc::_num_allocators = 0;
32 #ifndef PRODUCT
33 int PhaseRegAlloc::_total_framesize = 0;
34 int PhaseRegAlloc::_max_framesize = 0;
35 #endif
36 
PhaseRegAlloc(uint unique,PhaseCFG & cfg,Matcher & matcher,void (* pr_stats)())37 PhaseRegAlloc::PhaseRegAlloc( uint unique, PhaseCFG &cfg,
38                               Matcher &matcher,
39                               void (*pr_stats)() ):
40                Phase(Register_Allocation),
41                _node_regs(0),
42                _node_regs_max_index(0),
43                _cfg(cfg),
44                _framesize(0xdeadbeef),
45                _matcher(matcher)
46 {
47     int i;
48 
49     for (i=0; i < _num_allocators; i++) {
50         if (_alloc_statistics[i] == pr_stats)
51             return;
52     }
53     assert((_num_allocators + 1) < MAX_REG_ALLOCATORS, "too many register allocators");
54     _alloc_statistics[_num_allocators++] = pr_stats;
55 }
56 
57 
58 //------------------------------reg2offset-------------------------------------
reg2offset_unchecked(OptoReg::Name reg) const59 int PhaseRegAlloc::reg2offset_unchecked( OptoReg::Name reg ) const {
60   // Slots below _max_in_arg_stack_reg are offset by the entire frame.
61   // Slots above _max_in_arg_stack_reg are frame_slots and are not offset.
62   int slot = (reg < _matcher._new_SP)
63     ? reg - OptoReg::stack0() + _framesize
64     : reg - _matcher._new_SP;
65   // Note:  We use the direct formula (reg - SharedInfo::stack0) instead of
66   // OptoReg::reg2stack(reg), in order to avoid asserts in the latter
67   // function.  This routine must remain unchecked, so that dump_frame()
68   // can do its work undisturbed.
69   // %%% not really clear why reg2stack would assert here
70 
71   return slot*VMRegImpl::stack_slot_size;
72 }
73 
reg2offset(OptoReg::Name reg) const74 int PhaseRegAlloc::reg2offset( OptoReg::Name reg ) const {
75 
76   // Not allowed in the out-preserve area.
77   // In-preserve area is allowed so Intel can fetch the return pc out.
78   assert( reg <  _matcher._old_SP ||
79           (reg >= OptoReg::add(_matcher._old_SP,C->out_preserve_stack_slots()) &&
80            reg <  _matcher._in_arg_limit) ||
81           reg >=  OptoReg::add(_matcher._new_SP, C->out_preserve_stack_slots()) ||
82           // Allow return_addr in the out-preserve area.
83           reg == _matcher.return_addr(),
84           "register allocated in a preserve area" );
85   return reg2offset_unchecked( reg );
86 }
87 
88 //------------------------------offset2reg-------------------------------------
offset2reg(int stk_offset) const89 OptoReg::Name PhaseRegAlloc::offset2reg(int stk_offset) const {
90   int slot = stk_offset / jintSize;
91   int reg = (slot < (int) _framesize)
92     ? slot + _matcher._new_SP
93     : OptoReg::stack2reg(slot) - _framesize;
94   assert(stk_offset == reg2offset((OptoReg::Name) reg),
95          "offset2reg does not invert properly");
96   return (OptoReg::Name) reg;
97 }
98 
99 //------------------------------set_oop----------------------------------------
set_oop(const Node * n,bool is_an_oop)100 void PhaseRegAlloc::set_oop( const Node *n, bool is_an_oop ) {
101   if( is_an_oop ) {
102     _node_oops.set(n->_idx);
103   }
104 }
105 
106 //------------------------------is_oop-----------------------------------------
is_oop(const Node * n) const107 bool PhaseRegAlloc::is_oop( const Node *n ) const {
108   return _node_oops.test(n->_idx) != 0;
109 }
110 
111 // Allocate _node_regs table with at least "size" elements
alloc_node_regs(int size)112 void PhaseRegAlloc::alloc_node_regs(int size) {
113   _node_regs_max_index = size + (size >> 1) + NodeRegsOverflowSize;
114   _node_regs = NEW_RESOURCE_ARRAY( OptoRegPair, _node_regs_max_index );
115   // We assume our caller will fill in all elements up to size-1, so
116   // only the extra space we allocate is initialized here.
117   for( uint i = size; i < _node_regs_max_index; ++i )
118     _node_regs[i].set_bad();
119 }
120 
121 #ifndef PRODUCT
122 void
print_statistics()123 PhaseRegAlloc::print_statistics() {
124   tty->print_cr("Total frameslots = %d, Max frameslots = %d", _total_framesize, _max_framesize);
125   int i;
126 
127   for (i=0; i < _num_allocators; i++) {
128     _alloc_statistics[i]();
129   }
130 }
131 #endif
132