1 /*
   2  * Copyright (c) 2007, 2014, 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.
  33 int VectorNode::opcode(int sopc, BasicType bt) {
  34   switch (sopc) {
  35   case Op_AddI:
  36     switch (bt) {
  37     case T_BOOLEAN:
  38     case T_BYTE:      return Op_AddVB;
  39     case T_CHAR:
  40     case T_SHORT:     return Op_AddVS;
  41     case T_INT:       return Op_AddVI;
  42     }
  43     ShouldNotReachHere();
  44   case Op_AddL:
  45     assert(bt == T_LONG, "must be");
  46     return Op_AddVL;
  47   case Op_AddF:
  48     assert(bt == T_FLOAT, "must be");
  49     return Op_AddVF;
  50   case Op_AddD:
  51     assert(bt == T_DOUBLE, "must be");
  52     return Op_AddVD;
  53   case Op_SubI:
  54     switch (bt) {
  55     case T_BOOLEAN:
  56     case T_BYTE:   return Op_SubVB;
  57     case T_CHAR:
  58     case T_SHORT:  return Op_SubVS;
  59     case T_INT:    return Op_SubVI;
  60     }
  61     ShouldNotReachHere();
  62   case Op_SubL:
  63     assert(bt == T_LONG, "must be");
  64     return Op_SubVL;
  65   case Op_SubF:
  66     assert(bt == T_FLOAT, "must be");
  67     return Op_SubVF;
  68   case Op_SubD:
  69     assert(bt == T_DOUBLE, "must be");
  70     return Op_SubVD;
  71   case Op_MulI:
  72     switch (bt) {
  73     case T_BOOLEAN:
  74     case T_BYTE:   return 0;   // Unimplemented
  75     case T_CHAR:
  76     case T_SHORT:  return Op_MulVS;
  77     case T_INT:    return Op_MulVI;
  78     }
  79     ShouldNotReachHere();
  80   case Op_MulF:
  81     assert(bt == T_FLOAT, "must be");
  82     return Op_MulVF;
  83   case Op_MulD:
  84     assert(bt == T_DOUBLE, "must be");
  85     return Op_MulVD;
  86   case Op_DivF:
  87     assert(bt == T_FLOAT, "must be");
  88     return Op_DivVF;
  89   case Op_DivD:
  90     assert(bt == T_DOUBLE, "must be");
  91     return Op_DivVD;
  92   case Op_LShiftI:
  93     switch (bt) {
  94     case T_BOOLEAN:
  95     case T_BYTE:   return Op_LShiftVB;
  96     case T_CHAR:
  97     case T_SHORT:  return Op_LShiftVS;
  98     case T_INT:    return Op_LShiftVI;
  99     }
 100     ShouldNotReachHere();
 101   case Op_LShiftL:
 102     assert(bt == T_LONG, "must be");
 103     return Op_LShiftVL;
 104   case Op_RShiftI:
 105     switch (bt) {
 106     case T_BOOLEAN:return Op_URShiftVB; // boolean is unsigned value
 107     case T_CHAR:   return Op_URShiftVS; // char is unsigned value
 108     case T_BYTE:   return Op_RShiftVB;
 109     case T_SHORT:  return Op_RShiftVS;
 110     case T_INT:    return Op_RShiftVI;
 111     }
 112     ShouldNotReachHere();
 113   case Op_RShiftL:
 114     assert(bt == T_LONG, "must be");
 115     return Op_RShiftVL;
 116   case Op_URShiftI:
 117     switch (bt) {
 118     case T_BOOLEAN:return Op_URShiftVB;
 119     case T_CHAR:   return Op_URShiftVS;
 120     case T_BYTE:
 121     case T_SHORT:  return 0; // Vector logical right shift for signed short
 122                              // values produces incorrect Java result for
 123                              // negative data because java code should convert
 124                              // a short value into int value with sign
 125                              // extension before a shift.
 126     case T_INT:    return Op_URShiftVI;
 127     }
 128     ShouldNotReachHere();
 129   case Op_URShiftL:
 130     assert(bt == T_LONG, "must be");
 131     return Op_URShiftVL;
 132   case Op_AndI:
 133   case Op_AndL:
 134     return Op_AndV;
 135   case Op_OrI:
 136   case Op_OrL:
 137     return Op_OrV;
 138   case Op_XorI:
 139   case Op_XorL:
 140     return Op_XorV;
 141 
 142   case Op_LoadB:
 143   case Op_LoadUB:
 144   case Op_LoadUS:
 145   case Op_LoadS:
 146   case Op_LoadI:
 147   case Op_LoadL:
 148   case Op_LoadF:
 149   case Op_LoadD:
 150     return Op_LoadVector;
 151 
 152   case Op_StoreB:
 153   case Op_StoreC:
 154   case Op_StoreI:
 155   case Op_StoreL:
 156   case Op_StoreF:
 157   case Op_StoreD:
 158     return Op_StoreVector;
 159   }
 160   return 0; // Unimplemented
 161 }
 162 
 163 // Also used to check if the code generator
 164 // supports the vector operation.
 165 bool VectorNode::implemented(int opc, uint vlen, BasicType bt) {
 166   if (is_java_primitive(bt) &&
 167       (vlen > 1) && is_power_of_2(vlen) &&
 168       Matcher::vector_size_supported(bt, vlen)) {
 169     int vopc = VectorNode::opcode(opc, bt);
 170     return vopc > 0 && Matcher::match_rule_supported(vopc);
 171   }
 172   return false;
 173 }
 174 
 175 bool VectorNode::is_shift(Node* n) {
 176   switch (n->Opcode()) {
 177   case Op_LShiftI:
 178   case Op_LShiftL:
 179   case Op_RShiftI:
 180   case Op_RShiftL:
 181   case Op_URShiftI:
 182   case Op_URShiftL:
 183     return true;
 184   }
 185   return false;
 186 }
 187 
 188 // Check if input is loop invariant vector.
 189 bool VectorNode::is_invariant_vector(Node* n) {
 190   // Only Replicate vector nodes are loop invariant for now.
 191   switch (n->Opcode()) {
 192   case Op_ReplicateB:
 193   case Op_ReplicateS:
 194   case Op_ReplicateI:
 195   case Op_ReplicateL:
 196   case Op_ReplicateF:
 197   case Op_ReplicateD:
 198     return true;
 199   }
 200   return false;
 201 }
 202 
 203 // [Start, end) half-open range defining which operands are vectors
 204 void VectorNode::vector_operands(Node* n, uint* start, uint* end) {
 205   switch (n->Opcode()) {
 206   case Op_LoadB:   case Op_LoadUB:
 207   case Op_LoadS:   case Op_LoadUS:
 208   case Op_LoadI:   case Op_LoadL:
 209   case Op_LoadF:   case Op_LoadD:
 210   case Op_LoadP:   case Op_LoadN:
 211     *start = 0;
 212     *end   = 0; // no vector operands
 213     break;
 214   case Op_StoreB:  case Op_StoreC:
 215   case Op_StoreI:  case Op_StoreL:
 216   case Op_StoreF:  case Op_StoreD:
 217   case Op_StoreP:  case Op_StoreN:
 218     *start = MemNode::ValueIn;
 219     *end   = MemNode::ValueIn + 1; // 1 vector operand
 220     break;
 221   case Op_LShiftI:  case Op_LShiftL:
 222   case Op_RShiftI:  case Op_RShiftL:
 223   case Op_URShiftI: case Op_URShiftL:
 224     *start = 1;
 225     *end   = 2; // 1 vector operand
 226     break;
 227   case Op_AddI: case Op_AddL: case Op_AddF: case Op_AddD:
 228   case Op_SubI: case Op_SubL: case Op_SubF: case Op_SubD:
 229   case Op_MulI: case Op_MulL: case Op_MulF: case Op_MulD:
 230   case Op_DivF: case Op_DivD:
 231   case Op_AndI: case Op_AndL:
 232   case Op_OrI:  case Op_OrL:
 233   case Op_XorI: case Op_XorL:
 234     *start = 1;
 235     *end   = 3; // 2 vector operands
 236     break;
 237   case Op_CMoveI:  case Op_CMoveL:  case Op_CMoveF:  case Op_CMoveD:
 238     *start = 2;
 239     *end   = n->req();
 240     break;
 241   default:
 242     *start = 1;
 243     *end   = n->req(); // default is all operands
 244   }
 245 }
 246 
 247 // Return the vector version of a scalar operation node.
 248 VectorNode* VectorNode::make(int opc, Node* n1, Node* n2, uint vlen, BasicType bt) {
 249   const TypeVect* vt = TypeVect::make(bt, vlen);
 250   int vopc = VectorNode::opcode(opc, bt);
 251   // This method should not be called for unimplemented vectors.
 252   guarantee(vopc > 0, err_msg_res("Vector for '%s' is not implemented", NodeClassNames[opc]));
 253   switch (vopc) {
 254   case Op_AddVB: return new AddVBNode(n1, n2, vt);
 255   case Op_AddVS: return new AddVSNode(n1, n2, vt);
 256   case Op_AddVI: return new AddVINode(n1, n2, vt);
 257   case Op_AddVL: return new AddVLNode(n1, n2, vt);
 258   case Op_AddVF: return new AddVFNode(n1, n2, vt);
 259   case Op_AddVD: return new AddVDNode(n1, n2, vt);
 260 
 261   case Op_SubVB: return new SubVBNode(n1, n2, vt);
 262   case Op_SubVS: return new SubVSNode(n1, n2, vt);
 263   case Op_SubVI: return new SubVINode(n1, n2, vt);
 264   case Op_SubVL: return new SubVLNode(n1, n2, vt);
 265   case Op_SubVF: return new SubVFNode(n1, n2, vt);
 266   case Op_SubVD: return new SubVDNode(n1, n2, vt);
 267 
 268   case Op_MulVS: return new MulVSNode(n1, n2, vt);
 269   case Op_MulVI: return new MulVINode(n1, n2, vt);
 270   case Op_MulVF: return new MulVFNode(n1, n2, vt);
 271   case Op_MulVD: return new MulVDNode(n1, n2, vt);
 272 
 273   case Op_DivVF: return new DivVFNode(n1, n2, vt);
 274   case Op_DivVD: return new DivVDNode(n1, n2, vt);
 275 
 276   case Op_LShiftVB: return new LShiftVBNode(n1, n2, vt);
 277   case Op_LShiftVS: return new LShiftVSNode(n1, n2, vt);
 278   case Op_LShiftVI: return new LShiftVINode(n1, n2, vt);
 279   case Op_LShiftVL: return new LShiftVLNode(n1, n2, vt);
 280 
 281   case Op_RShiftVB: return new RShiftVBNode(n1, n2, vt);
 282   case Op_RShiftVS: return new RShiftVSNode(n1, n2, vt);
 283   case Op_RShiftVI: return new RShiftVINode(n1, n2, vt);
 284   case Op_RShiftVL: return new RShiftVLNode(n1, n2, vt);
 285 
 286   case Op_URShiftVB: return new URShiftVBNode(n1, n2, vt);
 287   case Op_URShiftVS: return new URShiftVSNode(n1, n2, vt);
 288   case Op_URShiftVI: return new URShiftVINode(n1, n2, vt);
 289   case Op_URShiftVL: return new URShiftVLNode(n1, n2, vt);
 290 
 291   case Op_AndV: return new AndVNode(n1, n2, vt);
 292   case Op_OrV:  return new OrVNode (n1, n2, vt);
 293   case Op_XorV: return new XorVNode(n1, n2, vt);
 294   }
 295   fatal(err_msg_res("Missed vector creation for '%s'", NodeClassNames[vopc]));
 296   return NULL;
 297 
 298 }
 299 
 300 // Scalar promotion
 301 VectorNode* VectorNode::scalar2vector(Node* s, uint vlen, const Type* opd_t) {
 302   BasicType bt = opd_t->array_element_basic_type();
 303   const TypeVect* vt = opd_t->singleton() ? TypeVect::make(opd_t, vlen)
 304                                           : TypeVect::make(bt, vlen);
 305   switch (bt) {
 306   case T_BOOLEAN:
 307   case T_BYTE:
 308     return new ReplicateBNode(s, vt);
 309   case T_CHAR:
 310   case T_SHORT:
 311     return new ReplicateSNode(s, vt);
 312   case T_INT:
 313     return new ReplicateINode(s, vt);
 314   case T_LONG:
 315     return new ReplicateLNode(s, vt);
 316   case T_FLOAT:
 317     return new ReplicateFNode(s, vt);
 318   case T_DOUBLE:
 319     return new ReplicateDNode(s, vt);
 320   }
 321   fatal(err_msg_res("Type '%s' is not supported for vectors", type2name(bt)));
 322   return NULL;
 323 }
 324 
 325 VectorNode* VectorNode::shift_count(Node* shift, Node* cnt, uint vlen, BasicType bt) {
 326   assert(VectorNode::is_shift(shift) && !cnt->is_Con(), "only variable shift count");
 327   // Match shift count type with shift vector type.
 328   const TypeVect* vt = TypeVect::make(bt, vlen);
 329   switch (shift->Opcode()) {
 330   case Op_LShiftI:
 331   case Op_LShiftL:
 332     return new LShiftCntVNode(cnt, vt);
 333   case Op_RShiftI:
 334   case Op_RShiftL:
 335   case Op_URShiftI:
 336   case Op_URShiftL:
 337     return new RShiftCntVNode(cnt, vt);
 338   }
 339   fatal(err_msg_res("Missed vector creation for '%s'", NodeClassNames[shift->Opcode()]));
 340   return NULL;
 341 }
 342 
 343 // Return initial Pack node. Additional operands added with add_opd() calls.
 344 PackNode* PackNode::make(Node* s, uint vlen, BasicType bt) {
 345   const TypeVect* vt = TypeVect::make(bt, vlen);
 346   switch (bt) {
 347   case T_BOOLEAN:
 348   case T_BYTE:
 349     return new PackBNode(s, vt);
 350   case T_CHAR:
 351   case T_SHORT:
 352     return new PackSNode(s, vt);
 353   case T_INT:
 354     return new PackINode(s, vt);
 355   case T_LONG:
 356     return new PackLNode(s, vt);
 357   case T_FLOAT:
 358     return new PackFNode(s, vt);
 359   case T_DOUBLE:
 360     return new PackDNode(s, vt);
 361   }
 362   fatal(err_msg_res("Type '%s' is not supported for vectors", type2name(bt)));
 363   return NULL;
 364 }
 365 
 366 // Create a binary tree form for Packs. [lo, hi) (half-open) range
 367 PackNode* PackNode::binary_tree_pack(int lo, int hi) {
 368   int ct = hi - lo;
 369   assert(is_power_of_2(ct), "power of 2");
 370   if (ct == 2) {
 371     PackNode* pk = PackNode::make(in(lo), 2, vect_type()->element_basic_type());
 372     pk->add_opd(in(lo+1));
 373     return pk;
 374 
 375   } else {
 376     int mid = lo + ct/2;
 377     PackNode* n1 = binary_tree_pack(lo,  mid);
 378     PackNode* n2 = binary_tree_pack(mid, hi );
 379 
 380     BasicType bt = n1->vect_type()->element_basic_type();
 381     assert(bt == n2->vect_type()->element_basic_type(), "should be the same");
 382     switch (bt) {
 383     case T_BOOLEAN:
 384     case T_BYTE:
 385       return new PackSNode(n1, n2, TypeVect::make(T_SHORT, 2));
 386     case T_CHAR:
 387     case T_SHORT:
 388       return new PackINode(n1, n2, TypeVect::make(T_INT, 2));
 389     case T_INT:
 390       return new PackLNode(n1, n2, TypeVect::make(T_LONG, 2));
 391     case T_LONG:
 392       return new Pack2LNode(n1, n2, TypeVect::make(T_LONG, 2));
 393     case T_FLOAT:
 394       return new PackDNode(n1, n2, TypeVect::make(T_DOUBLE, 2));
 395     case T_DOUBLE:
 396       return new Pack2DNode(n1, n2, TypeVect::make(T_DOUBLE, 2));
 397     }
 398     fatal(err_msg_res("Type '%s' is not supported for vectors", type2name(bt)));
 399   }
 400   return NULL;
 401 }
 402 
 403 // Return the vector version of a scalar load node.
 404 LoadVectorNode* LoadVectorNode::make(int opc, Node* ctl, Node* mem,
 405                                      Node* adr, const TypePtr* atyp, uint vlen, BasicType bt) {
 406   const TypeVect* vt = TypeVect::make(bt, vlen);
 407   return new LoadVectorNode(ctl, mem, adr, atyp, vt);
 408 }
 409 
 410 // Return the vector version of a scalar store node.
 411 StoreVectorNode* StoreVectorNode::make(int opc, Node* ctl, Node* mem,
 412                                        Node* adr, const TypePtr* atyp, Node* val,
 413                                        uint vlen) {
 414   return new StoreVectorNode(ctl, mem, adr, atyp, val);
 415 }
 416 
 417 // Extract a scalar element of vector.
 418 Node* ExtractNode::make(Node* v, uint position, BasicType bt) {
 419   assert((int)position < Matcher::max_vector_size(bt), "pos in range");
 420   ConINode* pos = ConINode::make((int)position);
 421   switch (bt) {
 422   case T_BOOLEAN:
 423     return new ExtractUBNode(v, pos);
 424   case T_BYTE:
 425     return new ExtractBNode(v, pos);
 426   case T_CHAR:
 427     return new ExtractCNode(v, pos);
 428   case T_SHORT:
 429     return new ExtractSNode(v, pos);
 430   case T_INT:
 431     return new ExtractINode(v, pos);
 432   case T_LONG:
 433     return new ExtractLNode(v, pos);
 434   case T_FLOAT:
 435     return new ExtractFNode(v, pos);
 436   case T_DOUBLE:
 437     return new ExtractDNode(v, pos);
 438   }
 439   fatal(err_msg_res("Type '%s' is not supported for vectors", type2name(bt)));
 440   return NULL;
 441 }
 442 
 443 int ReductionNode::opcode(int opc, BasicType bt) {
 444   int vopc = opc;
 445   switch (opc) {
 446     case Op_AddI:
 447       assert(bt == T_INT, "must be");
 448       vopc = Op_AddReductionVI;
 449       break;
 450     case Op_AddL:
 451       assert(bt == T_LONG, "must be");
 452       vopc = Op_AddReductionVL;
 453       break;
 454     case Op_AddF:
 455       assert(bt == T_FLOAT, "must be");
 456       vopc = Op_AddReductionVF;
 457       break;
 458     case Op_AddD:
 459       assert(bt == T_DOUBLE, "must be");
 460       vopc = Op_AddReductionVD;
 461       break;
 462     case Op_MulI:
 463       assert(bt == T_INT, "must be");
 464       vopc = Op_MulReductionVI;
 465       break;
 466     case Op_MulF:
 467       assert(bt == T_FLOAT, "must be");
 468       vopc = Op_MulReductionVF;
 469       break;
 470     case Op_MulD:
 471       assert(bt == T_DOUBLE, "must be");
 472       vopc = Op_MulReductionVD;
 473       break;
 474     // TODO: add MulL for targets that support it
 475     default:
 476       break;
 477   }
 478   return vopc;
 479 }
 480 
 481 // Return the appropriate reduction node.
 482 ReductionNode* ReductionNode::make(int opc, Node *ctrl, Node* n1, Node* n2, BasicType bt) {
 483 
 484   int vopc = opcode(opc, bt); 
 485   
 486   // This method should not be called for unimplemented vectors.
 487   guarantee(vopc > 0, err_msg_res("Vector for '%s' is not implemented", NodeClassNames[opc]));
 488   
 489   switch (vopc) {
 490   case Op_AddReductionVI: return new AddReductionVINode(ctrl, n1, n2);
 491   case Op_AddReductionVL: return new AddReductionVLNode(ctrl, n1, n2);
 492   case Op_AddReductionVF: return new AddReductionVFNode(ctrl, n1, n2);
 493   case Op_AddReductionVD: return new AddReductionVDNode(ctrl, n1, n2);
 494   case Op_MulReductionVI: return new MulReductionVINode(ctrl, n1, n2);
 495   case Op_MulReductionVF: return new MulReductionVFNode(ctrl, n1, n2);
 496   case Op_MulReductionVD: return new MulReductionVDNode(ctrl, n1, n2);
 497   }
 498   fatal(err_msg_res("Missed vector creation for '%s'", NodeClassNames[vopc]));
 499   return NULL;
 500 }
 501 
 502 bool ReductionNode::implemented(int opc, uint vlen, BasicType bt) {
 503   if (is_java_primitive(bt) &&
 504       (vlen > 1) && is_power_of_2(vlen) &&
 505       Matcher::vector_size_supported(bt, vlen)) {
 506     int vopc = ReductionNode::opcode(opc, bt);
 507     return vopc != opc;
 508   }
 509   return false;
 510 }
 511