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