optoreg.hpp revision 1879:f95d63e2154a
1/*
2 * Copyright (c) 2006, 2010, 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#ifndef SHARE_VM_OPTO_OPTOREG_HPP
26#define SHARE_VM_OPTO_OPTOREG_HPP
27
28//------------------------------OptoReg----------------------------------------
29// We eventually need Registers for the Real World.  Registers are essentially
30// non-SSA names.  A Register is represented as a number.  Non-regular values
31// (e.g., Control, Memory, I/O) use the Special register.  The actual machine
32// registers (as described in the ADL file for a machine) start at zero.
33// Stack-slots (spill locations) start at the nest Chunk past the last machine
34// register.
35//
36// Note that stack spill-slots are treated as a very large register set.
37// They have all the correct properties for a Register: not aliased (unique
38// named).  There is some simple mapping from a stack-slot register number
39// to the actual location on the stack; this mapping depends on the calling
40// conventions and is described in the ADL.
41//
42// Note that Name is not enum. C++ standard defines that the range of enum
43// is the range of smallest bit-field that can represent all enumerators
44// declared in the enum. The result of assigning a value to enum is undefined
45// if the value is outside the enumeration's valid range. OptoReg::Name is
46// typedef'ed as int, because it needs to be able to represent spill-slots.
47//
48class OptoReg VALUE_OBJ_CLASS_SPEC {
49
50 friend class C2Compiler;
51 public:
52  typedef int Name;
53  enum {
54    // Chunk 0
55    Physical = AdlcVMDeps::Physical, // Start of physical regs
56    // A few oddballs at the edge of the world
57    Special = -2,               // All special (not allocated) values
58    Bad = -1                    // Not a register
59  };
60
61 private:
62
63 static const VMReg opto2vm[REG_COUNT];
64 static Name vm2opto[ConcreteRegisterImpl::number_of_registers];
65
66 public:
67
68  // Stack pointer register
69  static OptoReg::Name c_frame_pointer;
70
71
72
73  // Increment a register number.  As in:
74  //    "for ( OptoReg::Name i; i=Control; i = add(i,1) ) ..."
75  static Name add( Name x, int y ) { return Name(x+y); }
76
77  // (We would like to have an operator+ for RegName, but it is not
78  // a class, so this would be illegal in C++.)
79
80  static void dump( int );
81
82  // Get the stack slot number of an OptoReg::Name
83  static unsigned int reg2stack( OptoReg::Name r) {
84    assert( r >= stack0(), " must be");
85    return r - stack0();
86  }
87
88  // convert a stack slot number into an OptoReg::Name
89  static OptoReg::Name stack2reg( int idx) {
90    return Name(stack0() + idx);
91  }
92
93  static bool is_stack(Name n) {
94    return n >= stack0();
95  }
96
97  static bool is_valid(Name n) {
98    return (n != Bad);
99  }
100
101  static bool is_reg(Name n) {
102    return  is_valid(n) && !is_stack(n);
103  }
104
105  static VMReg as_VMReg(OptoReg::Name n) {
106    if (is_reg(n)) {
107      // Must use table, it'd be nice if Bad was indexable...
108      return opto2vm[n];
109    } else {
110      assert(!is_stack(n), "must un warp");
111      return VMRegImpl::Bad();
112    }
113  }
114
115  // Can un-warp a stack slot or convert a register or Bad
116  static VMReg as_VMReg(OptoReg::Name n, int frame_size, int arg_count) {
117    if (is_reg(n)) {
118      // Must use table, it'd be nice if Bad was indexable...
119      return opto2vm[n];
120    } else if (is_stack(n)) {
121      int stack_slot = reg2stack(n);
122      if (stack_slot < arg_count) {
123        return VMRegImpl::stack2reg(stack_slot + frame_size);
124      }
125      return VMRegImpl::stack2reg(stack_slot - arg_count);
126      // return return VMRegImpl::stack2reg(reg2stack(OptoReg::add(n, -arg_count)));
127    } else {
128      return VMRegImpl::Bad();
129    }
130  }
131
132  static OptoReg::Name as_OptoReg(VMReg r) {
133    if (r->is_stack()) {
134      assert(false, "must warp");
135      return stack2reg(r->reg2stack());
136    } else if (r->is_valid()) {
137      // Must use table, it'd be nice if Bad was indexable...
138      return vm2opto[r->value()];
139    } else {
140      return Bad;
141    }
142  }
143
144  static OptoReg::Name stack0() {
145    return VMRegImpl::stack0->value();
146  }
147
148  static const char* regname(OptoReg::Name n) {
149    return as_VMReg(n)->name();
150  }
151
152};
153
154//---------------------------OptoRegPair-------------------------------------------
155// Pairs of 32-bit registers for the allocator.
156// This is a very similar class to VMRegPair. C2 only interfaces with VMRegPair
157// via the calling convention code which is shared between the compilers.
158// Since C2 uses OptoRegs for register allocation it is more efficient to use
159// VMRegPair internally for nodes that can contain a pair of OptoRegs rather
160// than use VMRegPair and continually be converting back and forth. So normally
161// C2 will take in a VMRegPair from the calling convention code and immediately
162// convert them to an OptoRegPair and stay in the OptoReg world. The only over
163// conversion between OptoRegs and VMRegs is for debug info and oopMaps. This
164// is not a high bandwidth spot and so it is not an issue.
165// Note that onde other consequence of staying in the OptoReg world with OptoRegPairs
166// is that there are "physical" OptoRegs that are not representable in the VMReg
167// world, notably flags. [ But by design there is "space" in the VMReg world
168// for such registers they just may not be concrete ]. So if we were to use VMRegPair
169// then the VMReg world would have to have a representation for these registers
170// so that a OptoReg->VMReg->OptoReg would reproduce ther original OptoReg. As it
171// stands if you convert a flag (condition code) to a VMReg you will get VMRegImpl::Bad
172// and converting that will return OptoReg::Bad losing the identity of the OptoReg.
173
174class OptoRegPair {
175private:
176  short _second;
177  short _first;
178public:
179  void set_bad (                   ) { _second = OptoReg::Bad; _first = OptoReg::Bad; }
180  void set1    ( OptoReg::Name n  ) { _second = OptoReg::Bad; _first = n; }
181  void set2    ( OptoReg::Name n  ) { _second = n + 1;       _first = n; }
182  void set_pair( OptoReg::Name second, OptoReg::Name first    ) { _second= second;    _first= first; }
183  void set_ptr ( OptoReg::Name ptr ) {
184#ifdef _LP64
185    _second = ptr+1;
186#else
187    _second = OptoReg::Bad;
188#endif
189    _first = ptr;
190  }
191
192  OptoReg::Name second() const { return _second; }
193  OptoReg::Name first() const { return _first; }
194  OptoRegPair(OptoReg::Name second, OptoReg::Name first) {  _second = second; _first = first; }
195  OptoRegPair(OptoReg::Name f) { _second = OptoReg::Bad; _first = f; }
196  OptoRegPair() { _second = OptoReg::Bad; _first = OptoReg::Bad; }
197};
198
199#endif // SHARE_VM_OPTO_OPTOREG_HPP
200