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