1 /*
   2  * Copyright (c) 2007, 2012, 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 #include "precompiled.hpp"
  25 #include "memory/allocation.inline.hpp"
  26 #include "opto/connode.hpp"
  27 #include "opto/vectornode.hpp"
  28 
  29 //------------------------------VectorNode--------------------------------------
  30 
  31 // Return the vector operator for the specified scalar operation
  32 // and vector length.  Also used to check if the code generator
  33 // supports the vector operation.
  34 int VectorNode::opcode(int sopc, uint vlen, BasicType bt) {
  35   switch (sopc) {
  36   case Op_AddI:
  37     switch (bt) {
  38     case T_BOOLEAN:
  39     case T_BYTE:      return Op_AddVB;
  40     case T_CHAR:      return Op_AddVC;
  41     case T_SHORT:     return Op_AddVS;
  42     case T_INT:       return Op_AddVI;
  43     }
  44     ShouldNotReachHere();
  45   case Op_AddL:
  46     assert(bt == T_LONG, "must be");
  47     return Op_AddVL;
  48   case Op_AddF:
  49     assert(bt == T_FLOAT, "must be");
  50     return Op_AddVF;
  51   case Op_AddD:
  52     assert(bt == T_DOUBLE, "must be");
  53     return Op_AddVD;
  54   case Op_SubI:
  55     switch (bt) {
  56     case T_BOOLEAN:
  57     case T_BYTE:   return Op_SubVB;
  58     case T_CHAR:   return Op_SubVC;
  59     case T_SHORT:  return Op_SubVS;
  60     case T_INT:    return Op_SubVI;
  61     }
  62     ShouldNotReachHere();
  63   case Op_SubL:
  64     assert(bt == T_LONG, "must be");
  65     return Op_SubVL;
  66   case Op_SubF:
  67     assert(bt == T_FLOAT, "must be");
  68     return Op_SubVF;
  69   case Op_SubD:
  70     assert(bt == T_DOUBLE, "must be");
  71     return Op_SubVD;
  72   case Op_MulF:
  73     assert(bt == T_FLOAT, "must be");
  74     return Op_MulVF;
  75   case Op_MulD:
  76     assert(bt == T_DOUBLE, "must be");
  77     return Op_MulVD;
  78   case Op_DivF:
  79     assert(bt == T_FLOAT, "must be");
  80     return Op_DivVF;
  81   case Op_DivD:
  82     assert(bt == T_DOUBLE, "must be");
  83     return Op_DivVD;
  84   case Op_LShiftI:
  85     switch (bt) {
  86     case T_BOOLEAN:
  87     case T_BYTE:   return Op_LShiftVB;
  88     case T_CHAR:   return Op_LShiftVC;
  89     case T_SHORT:  return Op_LShiftVS;
  90     case T_INT:    return Op_LShiftVI;
  91     }
  92     ShouldNotReachHere();
  93   case Op_RShiftI:
  94     switch (bt) {
  95     case T_BOOLEAN:
  96     case T_BYTE:   return Op_RShiftVB;
  97     case T_CHAR:   return Op_RShiftVC;
  98     case T_SHORT:  return Op_RShiftVS;
  99     case T_INT:    return Op_RShiftVI;
 100     }
 101     ShouldNotReachHere();
 102   case Op_AndI:
 103   case Op_AndL:
 104     return Op_AndV;
 105   case Op_OrI:
 106   case Op_OrL:
 107     return Op_OrV;
 108   case Op_XorI:
 109   case Op_XorL:
 110     return Op_XorV;
 111 
 112   case Op_LoadB:
 113   case Op_LoadUS:
 114   case Op_LoadS:
 115   case Op_LoadI:
 116   case Op_LoadL:
 117   case Op_LoadF:
 118   case Op_LoadD:
 119     return Op_LoadVector;
 120 
 121   case Op_StoreB:
 122   case Op_StoreC:
 123   case Op_StoreI:
 124   case Op_StoreL:
 125   case Op_StoreF:
 126   case Op_StoreD:
 127     return Op_StoreVector;
 128   }
 129   return 0; // Unimplemented
 130 }
 131 
 132 bool VectorNode::implemented(int opc, uint vlen, BasicType bt) {
 133   if (is_java_primitive(bt) &&
 134       (vlen > 1) && is_power_of_2(vlen) &&
 135       Matcher::vector_size_supported(bt, vlen)) {
 136     int vopc = VectorNode::opcode(opc, vlen, bt);
 137     return vopc > 0 && Matcher::has_match_rule(vopc);
 138   }
 139   return false;
 140 }
 141 
 142 // Return the vector version of a scalar operation node.
 143 VectorNode* VectorNode::make(Compile* C, int opc, Node* n1, Node* n2, uint vlen, BasicType bt) {
 144   const TypeVect* vt = TypeVect::make(bt, vlen);
 145   int vopc = VectorNode::opcode(opc, vlen, bt);
 146 
 147   switch (vopc) {
 148   case Op_AddVB: return new (C, 3) AddVBNode(n1, n2, vt);
 149   case Op_AddVC: return new (C, 3) AddVCNode(n1, n2, vt);
 150   case Op_AddVS: return new (C, 3) AddVSNode(n1, n2, vt);
 151   case Op_AddVI: return new (C, 3) AddVINode(n1, n2, vt);
 152   case Op_AddVL: return new (C, 3) AddVLNode(n1, n2, vt);
 153   case Op_AddVF: return new (C, 3) AddVFNode(n1, n2, vt);
 154   case Op_AddVD: return new (C, 3) AddVDNode(n1, n2, vt);
 155 
 156   case Op_SubVB: return new (C, 3) SubVBNode(n1, n2, vt);
 157   case Op_SubVC: return new (C, 3) SubVCNode(n1, n2, vt);
 158   case Op_SubVS: return new (C, 3) SubVSNode(n1, n2, vt);
 159   case Op_SubVI: return new (C, 3) SubVINode(n1, n2, vt);
 160   case Op_SubVL: return new (C, 3) SubVLNode(n1, n2, vt);
 161   case Op_SubVF: return new (C, 3) SubVFNode(n1, n2, vt);
 162   case Op_SubVD: return new (C, 3) SubVDNode(n1, n2, vt);
 163 
 164   case Op_MulVF: return new (C, 3) MulVFNode(n1, n2, vt);
 165   case Op_MulVD: return new (C, 3) MulVDNode(n1, n2, vt);
 166 
 167   case Op_DivVF: return new (C, 3) DivVFNode(n1, n2, vt);
 168   case Op_DivVD: return new (C, 3) DivVDNode(n1, n2, vt);
 169 
 170   case Op_LShiftVB: return new (C, 3) LShiftVBNode(n1, n2, vt);
 171   case Op_LShiftVC: return new (C, 3) LShiftVCNode(n1, n2, vt);
 172   case Op_LShiftVS: return new (C, 3) LShiftVSNode(n1, n2, vt);
 173   case Op_LShiftVI: return new (C, 3) LShiftVINode(n1, n2, vt);
 174 
 175   case Op_RShiftVB: return new (C, 3) RShiftVBNode(n1, n2, vt);
 176   case Op_RShiftVC: return new (C, 3) RShiftVCNode(n1, n2, vt);
 177   case Op_RShiftVS: return new (C, 3) RShiftVSNode(n1, n2, vt);
 178   case Op_RShiftVI: return new (C, 3) RShiftVINode(n1, n2, vt);
 179 
 180   case Op_AndV: return new (C, 3) AndVNode(n1, n2, vt);
 181   case Op_OrV:  return new (C, 3) OrVNode (n1, n2, vt);
 182   case Op_XorV: return new (C, 3) XorVNode(n1, n2, vt);
 183   }
 184   ShouldNotReachHere();
 185   return NULL;
 186 
 187 }
 188 
 189 // Scalar promotion
 190 VectorNode* VectorNode::scalar2vector(Compile* C, Node* s, uint vlen, const Type* opd_t) {
 191   BasicType bt = opd_t->array_element_basic_type();
 192   const TypeVect* vt = opd_t->singleton() ? TypeVect::make(opd_t, vlen)
 193                                           : TypeVect::make(bt, vlen);
 194   switch (bt) {
 195   case T_BOOLEAN:
 196   case T_BYTE:
 197     return new (C, 2) ReplicateBNode(s, vt);
 198   case T_CHAR:
 199     return new (C, 2) ReplicateCNode(s, vt);
 200   case T_SHORT:
 201     return new (C, 2) ReplicateSNode(s, vt);
 202   case T_INT:
 203     return new (C, 2) ReplicateINode(s, vt);
 204   case T_LONG:
 205     return new (C, 2) ReplicateLNode(s, vt);
 206   case T_FLOAT:
 207     return new (C, 2) ReplicateFNode(s, vt);
 208   case T_DOUBLE:
 209     return new (C, 2) ReplicateDNode(s, vt);
 210   }
 211   ShouldNotReachHere();
 212   return NULL;
 213 }
 214 
 215 // Return initial Pack node. Additional operands added with add_opd() calls.
 216 PackNode* PackNode::make(Compile* C, Node* s, uint vlen, BasicType bt) {
 217   const TypeVect* vt = TypeVect::make(bt, vlen);
 218   switch (bt) {
 219   case T_BOOLEAN:
 220   case T_BYTE:
 221     return new (C, vlen+1) PackBNode(s, vt);
 222   case T_CHAR:
 223     return new (C, vlen+1) PackCNode(s, vt);
 224   case T_SHORT:
 225     return new (C, vlen+1) PackSNode(s, vt);
 226   case T_INT:
 227     return new (C, vlen+1) PackINode(s, vt);
 228   case T_LONG:
 229     return new (C, vlen+1) PackLNode(s, vt);
 230   case T_FLOAT:
 231     return new (C, vlen+1) PackFNode(s, vt);
 232   case T_DOUBLE:
 233     return new (C, vlen+1) PackDNode(s, vt);
 234   }
 235   ShouldNotReachHere();
 236   return NULL;
 237 }
 238 
 239 // Create a binary tree form for Packs. [lo, hi) (half-open) range
 240 Node* PackNode::binaryTreePack(Compile* C, int lo, int hi) {
 241   int ct = hi - lo;
 242   assert(is_power_of_2(ct), "power of 2");
 243   if (ct == 2) {
 244     PackNode* pk = PackNode::make(C, in(lo), 2, vect_type()->element_basic_type());
 245     pk->add_opd(1, in(lo+1));
 246     return pk;
 247 
 248   } else {
 249     int mid = lo + ct/2;
 250     Node* n1 = binaryTreePack(C, lo,  mid);
 251     Node* n2 = binaryTreePack(C, mid, hi );
 252 
 253     BasicType bt = vect_type()->element_basic_type();
 254     switch (bt) {
 255     case T_BOOLEAN:
 256     case T_BYTE:
 257       return new (C, 3) PackSNode(n1, n2, TypeVect::make(T_SHORT, 2));
 258     case T_CHAR:
 259     case T_SHORT:
 260       return new (C, 3) PackINode(n1, n2, TypeVect::make(T_INT, 2));
 261     case T_INT:
 262       return new (C, 3) PackLNode(n1, n2, TypeVect::make(T_LONG, 2));
 263     case T_LONG:
 264       return new (C, 3) Pack2LNode(n1, n2, TypeVect::make(T_LONG, 2));
 265     case T_FLOAT:
 266       return new (C, 3) PackDNode(n1, n2, TypeVect::make(T_DOUBLE, 2));
 267     case T_DOUBLE:
 268       return new (C, 3) Pack2DNode(n1, n2, TypeVect::make(T_DOUBLE, 2));
 269     }
 270     ShouldNotReachHere();
 271   }
 272   return NULL;
 273 }
 274 
 275 // Return the vector version of a scalar load node.
 276 LoadVectorNode* LoadVectorNode::make(Compile* C, int opc, Node* ctl, Node* mem,
 277                                      Node* adr, const TypePtr* atyp, uint vlen, BasicType bt) {
 278   const TypeVect* vt = TypeVect::make(bt, vlen);
 279   return new (C, 3) LoadVectorNode(ctl, mem, adr, atyp, vt);
 280   return NULL;
 281 }
 282 
 283 // Return the vector version of a scalar store node.
 284 StoreVectorNode* StoreVectorNode::make(Compile* C, int opc, Node* ctl, Node* mem,
 285                                        Node* adr, const TypePtr* atyp, Node* val,
 286                                        uint vlen) {
 287   return new (C, 4) StoreVectorNode(ctl, mem, adr, atyp, val);
 288 }
 289 
 290 // Extract a scalar element of vector.
 291 Node* ExtractNode::make(Compile* C, Node* v, uint position, BasicType bt) {
 292   assert((int)position < Matcher::max_vector_size(bt), "pos in range");
 293   ConINode* pos = ConINode::make(C, (int)position);
 294   switch (bt) {
 295   case T_BOOLEAN:
 296   case T_BYTE:
 297     return new (C, 3) ExtractBNode(v, pos);
 298   case T_CHAR:
 299     return new (C, 3) ExtractCNode(v, pos);
 300   case T_SHORT:
 301     return new (C, 3) ExtractSNode(v, pos);
 302   case T_INT:
 303     return new (C, 3) ExtractINode(v, pos);
 304   case T_LONG:
 305     return new (C, 3) ExtractLNode(v, pos);
 306   case T_FLOAT:
 307     return new (C, 3) ExtractFNode(v, pos);
 308   case T_DOUBLE:
 309     return new (C, 3) ExtractDNode(v, pos);
 310   }
 311   ShouldNotReachHere();
 312   return NULL;
 313 }
 314