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