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