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