1 /*
   2  * Copyright (c) 2007, 2017, 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/subnode.hpp"
  28 #include "opto/vectornode.hpp"
  29 #include "utilities/powerOfTwo.hpp"
  30 
  31 //------------------------------VectorNode--------------------------------------
  32 
  33 // Return the vector operator for the specified scalar operation
  34 // and vector length.
  35 int VectorNode::opcode(int sopc, BasicType bt) {
  36   switch (sopc) {
  37   case Op_AddI:
  38     switch (bt) {
  39     case T_BOOLEAN:
  40     case T_BYTE:      return Op_AddVB;
  41     case T_CHAR:
  42     case T_SHORT:     return Op_AddVS;
  43     case T_INT:       return Op_AddVI;
  44     default:          ShouldNotReachHere(); return 0;
  45     }
  46   case Op_AddL:
  47     assert(bt == T_LONG, "must be");
  48     return Op_AddVL;
  49   case Op_AddF:
  50     assert(bt == T_FLOAT, "must be");
  51     return Op_AddVF;
  52   case Op_AddD:
  53     assert(bt == T_DOUBLE, "must be");
  54     return Op_AddVD;
  55   case Op_SubI:
  56     switch (bt) {
  57     case T_BOOLEAN:
  58     case T_BYTE:   return Op_SubVB;
  59     case T_CHAR:
  60     case T_SHORT:  return Op_SubVS;
  61     case T_INT:    return Op_SubVI;
  62     default:       ShouldNotReachHere(); return 0;
  63     }
  64   case Op_SubL:
  65     assert(bt == T_LONG, "must be");
  66     return Op_SubVL;
  67   case Op_SubF:
  68     assert(bt == T_FLOAT, "must be");
  69     return Op_SubVF;
  70   case Op_SubD:
  71     assert(bt == T_DOUBLE, "must be");
  72     return Op_SubVD;
  73   case Op_MulI:
  74     switch (bt) {
  75     case T_BOOLEAN:return 0;
  76     case T_BYTE:   return Op_MulVB;
  77     case T_CHAR:
  78     case T_SHORT:  return Op_MulVS;
  79     case T_INT:    return Op_MulVI;
  80     default:       ShouldNotReachHere(); return 0;
  81     }
  82   case Op_MulL:
  83     assert(bt == T_LONG, "must be");
  84     return Op_MulVL;
  85   case Op_MulF:
  86     assert(bt == T_FLOAT, "must be");
  87     return Op_MulVF;
  88   case Op_MulD:
  89     assert(bt == T_DOUBLE, "must be");
  90     return Op_MulVD;
  91   case Op_FmaD:
  92     assert(bt == T_DOUBLE, "must be");
  93     return Op_FmaVD;
  94   case Op_FmaF:
  95     assert(bt == T_FLOAT, "must be");
  96     return Op_FmaVF;
  97   case Op_CMoveF:
  98     assert(bt == T_FLOAT, "must be");
  99     return Op_CMoveVF;
 100   case Op_CMoveD:
 101     assert(bt == T_DOUBLE, "must be");
 102     return Op_CMoveVD;
 103   case Op_DivF:
 104     assert(bt == T_FLOAT, "must be");
 105     return Op_DivVF;
 106   case Op_DivD:
 107     assert(bt == T_DOUBLE, "must be");
 108     return Op_DivVD;
 109   case Op_AbsI:
 110     switch (bt) {
 111     case T_BOOLEAN:
 112     case T_CHAR:  return 0; // abs does not make sense for unsigned
 113     case T_BYTE:  return Op_AbsVB;
 114     case T_SHORT: return Op_AbsVS;
 115     case T_INT:   return Op_AbsVI;
 116     default: ShouldNotReachHere(); return 0;
 117     }
 118   case Op_AbsL:
 119     assert(bt == T_LONG, "must be");
 120     return Op_AbsVL;
 121   case Op_MinI:
 122     switch (bt) {
 123     case T_BOOLEAN:
 124     case T_CHAR:   return 0;
 125     case T_BYTE:
 126     case T_SHORT:
 127     case T_INT:    return Op_MinV;
 128     default:       ShouldNotReachHere(); return 0;
 129     }
 130   case Op_MinL:
 131     assert(bt == T_LONG, "must be");
 132     return Op_MinV;
 133   case Op_MinF:
 134     assert(bt == T_FLOAT, "must be");
 135     return Op_MinV;
 136   case Op_MinD:
 137     assert(bt == T_DOUBLE, "must be");
 138     return Op_MinV;
 139   case Op_MaxI:
 140     switch (bt) {
 141     case T_BOOLEAN:
 142     case T_CHAR:   return 0;
 143     case T_BYTE:
 144     case T_SHORT:
 145     case T_INT:    return Op_MaxV;
 146     default:       ShouldNotReachHere(); return 0;
 147     }
 148   case Op_MaxL:
 149     assert(bt == T_LONG, "must be");
 150     return Op_MaxV;
 151   case Op_MaxF:
 152     assert(bt == T_FLOAT, "must be");
 153     return Op_MaxV;
 154   case Op_MaxD:
 155     assert(bt == T_DOUBLE, "must be");
 156     return Op_MaxV;
 157   case Op_AbsF:
 158     assert(bt == T_FLOAT, "must be");
 159     return Op_AbsVF;
 160   case Op_AbsD:
 161     assert(bt == T_DOUBLE, "must be");
 162     return Op_AbsVD;
 163   case Op_NegI:
 164     assert(bt == T_INT, "must be");
 165     return Op_NegVI;
 166   case Op_NegF:
 167     assert(bt == T_FLOAT, "must be");
 168     return Op_NegVF;
 169   case Op_NegD:
 170     assert(bt == T_DOUBLE, "must be");
 171     return Op_NegVD;
 172   case Op_RoundDoubleMode:
 173     assert(bt == T_DOUBLE, "must be");
 174     return Op_RoundDoubleModeV;
 175   case Op_SqrtF:
 176     assert(bt == T_FLOAT, "must be");
 177     return Op_SqrtVF;
 178   case Op_SqrtD:
 179     assert(bt == T_DOUBLE, "must be");
 180     return Op_SqrtVD;
 181   case Op_Not:
 182     return Op_NotV;
 183   case Op_PopCountI:
 184     if (bt == T_INT) {
 185       return Op_PopCountVI;
 186     }
 187     // Unimplemented for subword types since bit count changes
 188     // depending on size of lane (and sign bit).
 189     return 0;
 190   case Op_LShiftI:
 191     switch (bt) {
 192     case T_BOOLEAN:
 193     case T_BYTE:   return Op_LShiftVB;
 194     case T_CHAR:
 195     case T_SHORT:  return Op_LShiftVS;
 196     case T_INT:    return Op_LShiftVI;
 197       default:       ShouldNotReachHere(); return 0;
 198     }
 199   case Op_LShiftL:
 200     assert(bt == T_LONG, "must be");
 201     return Op_LShiftVL;
 202   case Op_RShiftI:
 203     switch (bt) {
 204     case T_BOOLEAN:return Op_URShiftVB; // boolean is unsigned value
 205     case T_CHAR:   return Op_URShiftVS; // char is unsigned value
 206     case T_BYTE:   return Op_RShiftVB;
 207     case T_SHORT:  return Op_RShiftVS;
 208     case T_INT:    return Op_RShiftVI;
 209     default:       ShouldNotReachHere(); return 0;
 210     }
 211   case Op_RShiftL:
 212     assert(bt == T_LONG, "must be");
 213     return Op_RShiftVL;
 214   case Op_URShiftB:
 215     assert(bt == T_BYTE, "must be");
 216     return Op_URShiftVB;
 217   case Op_URShiftS:
 218     assert(bt == T_SHORT, "must be");
 219     return Op_URShiftVS;
 220   case Op_URShiftI:
 221     switch (bt) {
 222     case T_BOOLEAN:return Op_URShiftVB;
 223     case T_CHAR:   return Op_URShiftVS;
 224     case T_BYTE:
 225     case T_SHORT:  return 0; // Vector logical right shift for signed short
 226                              // values produces incorrect Java result for
 227                              // negative data because java code should convert
 228                              // a short value into int value with sign
 229                              // extension before a shift.
 230     case T_INT:    return Op_URShiftVI;
 231     default:       ShouldNotReachHere(); return 0;
 232     }
 233   case Op_URShiftL:
 234     assert(bt == T_LONG, "must be");
 235     return Op_URShiftVL;
 236   case Op_AndI:
 237   case Op_AndL:
 238     return Op_AndV;
 239   case Op_OrI:
 240   case Op_OrL:
 241     return Op_OrV;
 242   case Op_XorI:
 243   case Op_XorL:
 244     return Op_XorV;
 245 
 246   case Op_LoadB:
 247   case Op_LoadUB:
 248   case Op_LoadUS:
 249   case Op_LoadS:
 250   case Op_LoadI:
 251   case Op_LoadL:
 252   case Op_LoadF:
 253   case Op_LoadD:
 254     return Op_LoadVector;
 255 
 256   case Op_StoreB:
 257   case Op_StoreC:
 258   case Op_StoreI:
 259   case Op_StoreL:
 260   case Op_StoreF:
 261   case Op_StoreD:
 262     return Op_StoreVector;
 263   case Op_MulAddS2I:
 264     return Op_MulAddVS2VI;
 265 
 266   default:
 267     return 0; // Unimplemented
 268   }
 269 }
 270 
 271 int VectorNode::replicate_opcode(BasicType bt) {
 272   switch(bt) {
 273     case T_BOOLEAN:
 274     case T_BYTE:
 275       return Op_ReplicateB;
 276     case T_SHORT:
 277     case T_CHAR:
 278       return Op_ReplicateS;
 279     case T_INT:
 280       return Op_ReplicateI;
 281     case T_LONG:
 282       return Op_ReplicateL;
 283     case T_FLOAT:
 284       return Op_ReplicateF;
 285     case T_DOUBLE:
 286       return Op_ReplicateD;
 287     default:
 288       break;
 289   }
 290 
 291   return 0;
 292 }
 293 
 294 // Also used to check if the code generator
 295 // supports the vector operation.
 296 bool VectorNode::implemented(int opc, uint vlen, BasicType bt) {
 297   if (is_java_primitive(bt) &&
 298       (vlen > 1) && is_power_of_2(vlen) &&
 299       Matcher::vector_size_supported(bt, vlen)) {
 300     int vopc = VectorNode::opcode(opc, bt);
 301     return vopc > 0 && Matcher::match_rule_supported_vector(vopc, vlen, bt);
 302   }
 303   return false;
 304 }
 305 
 306 bool VectorNode::is_type_transition_short_to_int(Node* n) {
 307   switch (n->Opcode()) {
 308   case Op_MulAddS2I:
 309     return true;
 310   }
 311   return false;
 312 }
 313 
 314 bool VectorNode::is_type_transition_to_int(Node* n) {
 315   return is_type_transition_short_to_int(n);
 316 }
 317 
 318 bool VectorNode::is_muladds2i(Node* n) {
 319   if (n->Opcode() == Op_MulAddS2I) {
 320     return true;
 321   }
 322   return false;
 323 }
 324 
 325 bool VectorNode::is_roundopD(Node *n) {
 326   if (n->Opcode() == Op_RoundDoubleMode) {
 327     return true;
 328   }
 329   return false;
 330 }
 331 
 332 bool VectorNode::is_shift(Node* n) {
 333   switch (n->Opcode()) {
 334   case Op_LShiftI:
 335   case Op_LShiftL:
 336   case Op_RShiftI:
 337   case Op_RShiftL:
 338   case Op_URShiftI:
 339   case Op_URShiftL:
 340     return true;
 341   default:
 342     return false;
 343   }
 344 }
 345 
 346 bool VectorNode::is_vshift(Node* n) {
 347   switch (n->Opcode()) {
 348   case Op_LShiftVB:
 349   case Op_LShiftVS:
 350   case Op_LShiftVI:
 351   case Op_LShiftVL:
 352 
 353   case Op_RShiftVB:
 354   case Op_RShiftVS:
 355   case Op_RShiftVI:
 356   case Op_RShiftVL:
 357 
 358   case Op_URShiftVB:
 359   case Op_URShiftVS:
 360   case Op_URShiftVI:
 361   case Op_URShiftVL:
 362     return true;
 363   default:
 364     return false;
 365   }
 366 }
 367 
 368 bool VectorNode::is_vshift_cnt(Node* n) {
 369   switch (n->Opcode()) {
 370   case Op_LShiftCntV:
 371   case Op_RShiftCntV:
 372     return true;
 373   default:
 374     return false;
 375   }
 376 }
 377 
 378 // Check if input is loop invariant vector.
 379 bool VectorNode::is_invariant_vector(Node* n) {
 380   // Only Replicate vector nodes are loop invariant for now.
 381   switch (n->Opcode()) {
 382   case Op_ReplicateB:
 383   case Op_ReplicateS:
 384   case Op_ReplicateI:
 385   case Op_ReplicateL:
 386   case Op_ReplicateF:
 387   case Op_ReplicateD:
 388     return true;
 389   default:
 390     return false;
 391   }
 392 }
 393 
 394 // [Start, end) half-open range defining which operands are vectors
 395 void VectorNode::vector_operands(Node* n, uint* start, uint* end) {
 396   switch (n->Opcode()) {
 397   case Op_LoadB:   case Op_LoadUB:
 398   case Op_LoadS:   case Op_LoadUS:
 399   case Op_LoadI:   case Op_LoadL:
 400   case Op_LoadF:   case Op_LoadD:
 401   case Op_LoadP:   case Op_LoadN:
 402     *start = 0;
 403     *end   = 0; // no vector operands
 404     break;
 405   case Op_StoreB:  case Op_StoreC:
 406   case Op_StoreI:  case Op_StoreL:
 407   case Op_StoreF:  case Op_StoreD:
 408   case Op_StoreP:  case Op_StoreN:
 409     *start = MemNode::ValueIn;
 410     *end   = MemNode::ValueIn + 1; // 1 vector operand
 411     break;
 412   case Op_LShiftI:  case Op_LShiftL:
 413   case Op_RShiftI:  case Op_RShiftL:
 414   case Op_URShiftI: case Op_URShiftL:
 415     *start = 1;
 416     *end   = 2; // 1 vector operand
 417     break;
 418   case Op_AddI: case Op_AddL: case Op_AddF: case Op_AddD:
 419   case Op_SubI: case Op_SubL: case Op_SubF: case Op_SubD:
 420   case Op_MulI: case Op_MulL: case Op_MulF: case Op_MulD:
 421   case Op_DivF: case Op_DivD:
 422   case Op_AndI: case Op_AndL:
 423   case Op_OrI:  case Op_OrL:
 424   case Op_XorI: case Op_XorL:
 425   case Op_MulAddS2I:
 426     *start = 1;
 427     *end   = 3; // 2 vector operands
 428     break;
 429   case Op_CMoveI:  case Op_CMoveL:  case Op_CMoveF:  case Op_CMoveD:
 430     *start = 2;
 431     *end   = n->req();
 432     break;
 433   case Op_FmaD:
 434   case Op_FmaF:
 435     *start = 1;
 436     *end   = 4; // 3 vector operands
 437     break;
 438   default:
 439     *start = 1;
 440     *end   = n->req(); // default is all operands
 441   }
 442 }
 443 
 444 // Make a vector node for binary operation
 445 VectorNode* VectorNode::make(int vopc, Node* n1, Node* n2, const TypeVect* vt) {
 446   // This method should not be called for unimplemented vectors.
 447   guarantee(vopc > 0, "vopc must be > 0");
 448   switch (vopc) {
 449   case Op_AddVB: return new AddVBNode(n1, n2, vt);
 450   case Op_AddVS: return new AddVSNode(n1, n2, vt);
 451   case Op_AddVI: return new AddVINode(n1, n2, vt);
 452   case Op_AddVL: return new AddVLNode(n1, n2, vt);
 453   case Op_AddVF: return new AddVFNode(n1, n2, vt);
 454   case Op_AddVD: return new AddVDNode(n1, n2, vt);
 455 
 456   case Op_SubVB: return new SubVBNode(n1, n2, vt);
 457   case Op_SubVS: return new SubVSNode(n1, n2, vt);
 458   case Op_SubVI: return new SubVINode(n1, n2, vt);
 459   case Op_SubVL: return new SubVLNode(n1, n2, vt);
 460   case Op_SubVF: return new SubVFNode(n1, n2, vt);
 461   case Op_SubVD: return new SubVDNode(n1, n2, vt);
 462 
 463   case Op_MulVB: return new MulVBNode(n1, n2, vt);
 464   case Op_MulVS: return new MulVSNode(n1, n2, vt);
 465   case Op_MulVI: return new MulVINode(n1, n2, vt);
 466   case Op_MulVL: return new MulVLNode(n1, n2, vt);
 467   case Op_MulVF: return new MulVFNode(n1, n2, vt);
 468   case Op_MulVD: return new MulVDNode(n1, n2, vt);
 469 
 470   case Op_DivVF: return new DivVFNode(n1, n2, vt);
 471   case Op_DivVD: return new DivVDNode(n1, n2, vt);
 472 
 473   case Op_MinV: return new MinVNode(n1, n2, vt);
 474   case Op_MaxV: return new MaxVNode(n1, n2, vt);
 475 
 476   case Op_AbsV: return new AbsVNode(n1, vt);
 477   case Op_AbsVF: return new AbsVFNode(n1, vt);
 478   case Op_AbsVD: return new AbsVDNode(n1, vt);
 479   case Op_AbsVB: return new AbsVBNode(n1, vt);
 480   case Op_AbsVS: return new AbsVSNode(n1, vt);
 481   case Op_AbsVI: return new AbsVINode(n1, vt);
 482   case Op_AbsVL: return new AbsVLNode(n1, vt);
 483 
 484   case Op_NegVI: return new NegVINode(n1, vt);
 485   case Op_NegVF: return new NegVFNode(n1, vt);
 486   case Op_NegVD: return new NegVDNode(n1, vt);
 487 
 488   case Op_SqrtVF: return new SqrtVFNode(n1, vt);
 489   case Op_SqrtVD: return new SqrtVDNode(n1, vt);
 490 
 491   case Op_PopCountVI: return new PopCountVINode(n1, vt);
 492   case Op_NotV: return new NotVNode(n1, vt);
 493 
 494   case Op_LShiftVB: return new LShiftVBNode(n1, n2, vt);
 495   case Op_LShiftVS: return new LShiftVSNode(n1, n2, vt);
 496   case Op_LShiftVI: return new LShiftVINode(n1, n2, vt);
 497   case Op_LShiftVL: return new LShiftVLNode(n1, n2, vt);
 498 
 499   case Op_RShiftVB: return new RShiftVBNode(n1, n2, vt);
 500   case Op_RShiftVS: return new RShiftVSNode(n1, n2, vt);
 501   case Op_RShiftVI: return new RShiftVINode(n1, n2, vt);
 502   case Op_RShiftVL: return new RShiftVLNode(n1, n2, vt);
 503 
 504   case Op_URShiftVB: return new URShiftVBNode(n1, n2, vt);
 505   case Op_URShiftVS: return new URShiftVSNode(n1, n2, vt);
 506   case Op_URShiftVI: return new URShiftVINode(n1, n2, vt);
 507   case Op_URShiftVL: return new URShiftVLNode(n1, n2, vt);
 508 
 509   // Variable shift left
 510   case Op_VLShiftV: return new VLShiftVNode(n1, n2, vt);
 511   case Op_VRShiftV: return new VRShiftVNode(n1, n2, vt);
 512   case Op_VURShiftV: return new VURShiftVNode(n1, n2, vt);
 513 
 514   case Op_AndV: return new AndVNode(n1, n2, vt);
 515   case Op_OrV:  return new OrVNode (n1, n2, vt);
 516   case Op_XorV: return new XorVNode(n1, n2, vt);
 517 
 518   case Op_RoundDoubleModeV: return new RoundDoubleModeVNode(n1, n2, vt);
 519 
 520   case Op_MulAddVS2VI: return new MulAddVS2VINode(n1, n2, vt);
 521   default:
 522     fatal("Missed vector creation for '%s'", NodeClassNames[vopc]);
 523     return NULL;
 524   }
 525 }
 526 
 527 // Return the vector version of a scalar binary operation node.
 528 VectorNode* VectorNode::make(int opc, Node* n1, Node* n2, uint vlen, BasicType bt) {
 529   const TypeVect* vt = TypeVect::make(bt, vlen);
 530   int vopc = VectorNode::opcode(opc, bt);
 531   // This method should not be called for unimplemented vectors.
 532   guarantee(vopc > 0, "Vector for '%s' is not implemented", NodeClassNames[opc]);
 533   return make(vopc, n1, n2, vt);
 534 }
 535 
 536 // Make a vector node for ternary operation
 537 VectorNode* VectorNode::make(int vopc, Node* n1, Node* n2, Node* n3, const TypeVect* vt) {
 538   // This method should not be called for unimplemented vectors.
 539   guarantee(vopc > 0, "vopc must be > 0");
 540   switch (vopc) {
 541   case Op_FmaVD: return new FmaVDNode(n1, n2, n3, vt);
 542   case Op_FmaVF: return new FmaVFNode(n1, n2, n3, vt);
 543   default:
 544     fatal("Missed vector creation for '%s'", NodeClassNames[vopc]);
 545     return NULL;
 546   }
 547 }
 548 
 549 // Return the vector version of a scalar ternary operation node.
 550 VectorNode* VectorNode::make(int opc, Node* n1, Node* n2, Node* n3, uint vlen, BasicType bt) {
 551   const TypeVect* vt = TypeVect::make(bt, vlen);
 552   int vopc = VectorNode::opcode(opc, bt);
 553   // This method should not be called for unimplemented vectors.
 554   guarantee(vopc > 0, "Vector for '%s' is not implemented", NodeClassNames[opc]);
 555   return make(vopc, n1, n2, n3, vt);
 556 }
 557 
 558 // Scalar promotion
 559 VectorNode* VectorNode::scalar2vector(Node* s, uint vlen, const Type* opd_t) {
 560   BasicType bt = opd_t->array_element_basic_type();
 561   const TypeVect* vt = opd_t->singleton() ? TypeVect::make(opd_t, vlen)
 562                                           : TypeVect::make(bt, vlen);
 563   switch (bt) {
 564   case T_BOOLEAN:
 565   case T_BYTE:
 566     return new ReplicateBNode(s, vt);
 567   case T_CHAR:
 568   case T_SHORT:
 569     return new ReplicateSNode(s, vt);
 570   case T_INT:
 571     return new ReplicateINode(s, vt);
 572   case T_LONG:
 573     return new ReplicateLNode(s, vt);
 574   case T_FLOAT:
 575     return new ReplicateFNode(s, vt);
 576   case T_DOUBLE:
 577     return new ReplicateDNode(s, vt);
 578   default:
 579     fatal("Type '%s' is not supported for vectors", type2name(bt));
 580     return NULL;
 581   }
 582 }
 583 
 584 VectorNode* VectorNode::shift_count(int opc, Node* cnt, uint vlen, BasicType bt) {
 585   // Match shift count type with shift vector type.
 586   const TypeVect* vt = TypeVect::make(bt, vlen);
 587   switch (opc) {
 588   case Op_LShiftI:
 589   case Op_LShiftL:
 590     return new LShiftCntVNode(cnt, vt);
 591   case Op_RShiftI:
 592   case Op_RShiftL:
 593   case Op_URShiftB:
 594   case Op_URShiftS:
 595   case Op_URShiftI:
 596   case Op_URShiftL:
 597     return new RShiftCntVNode(cnt, vt);
 598   default:
 599     fatal("Missed vector creation for '%s'", NodeClassNames[opc]);
 600     return NULL;
 601   }
 602 }
 603 
 604 bool VectorNode::is_vector_shift(int opc) {
 605   assert(opc > _last_machine_leaf && opc < _last_opcode, "invalid opcode");
 606   switch (opc) {
 607   case Op_LShiftVB:
 608   case Op_LShiftVS:
 609   case Op_LShiftVI:
 610   case Op_LShiftVL:
 611   case Op_RShiftVB:
 612   case Op_RShiftVS:
 613   case Op_RShiftVI:
 614   case Op_RShiftVL:
 615   case Op_URShiftVB:
 616   case Op_URShiftVS:
 617   case Op_URShiftVI:
 618   case Op_URShiftVL:
 619     return true;
 620   default:
 621     return false;
 622   }
 623 }
 624 
 625 bool VectorNode::is_vector_shift_count(int opc) {
 626   assert(opc > _last_machine_leaf && opc < _last_opcode, "invalid opcode");
 627   switch (opc) {
 628   case Op_RShiftCntV:
 629   case Op_LShiftCntV:
 630     return true;
 631   default:
 632     return false;
 633   }
 634 }
 635 
 636 // Return initial Pack node. Additional operands added with add_opd() calls.
 637 PackNode* PackNode::make(Node* s, uint vlen, BasicType bt) {
 638   const TypeVect* vt = TypeVect::make(bt, vlen);
 639   switch (bt) {
 640   case T_BOOLEAN:
 641   case T_BYTE:
 642     return new PackBNode(s, vt);
 643   case T_CHAR:
 644   case T_SHORT:
 645     return new PackSNode(s, vt);
 646   case T_INT:
 647     return new PackINode(s, vt);
 648   case T_LONG:
 649     return new PackLNode(s, vt);
 650   case T_FLOAT:
 651     return new PackFNode(s, vt);
 652   case T_DOUBLE:
 653     return new PackDNode(s, vt);
 654   default:
 655     fatal("Type '%s' is not supported for vectors", type2name(bt));
 656     return NULL;
 657   }
 658 }
 659 
 660 // Create a binary tree form for Packs. [lo, hi) (half-open) range
 661 PackNode* PackNode::binary_tree_pack(int lo, int hi) {
 662   int ct = hi - lo;
 663   assert(is_power_of_2(ct), "power of 2");
 664   if (ct == 2) {
 665     PackNode* pk = PackNode::make(in(lo), 2, vect_type()->element_basic_type());
 666     pk->add_opd(in(lo+1));
 667     return pk;
 668   } else {
 669     int mid = lo + ct/2;
 670     PackNode* n1 = binary_tree_pack(lo,  mid);
 671     PackNode* n2 = binary_tree_pack(mid, hi );
 672 
 673     BasicType bt = n1->vect_type()->element_basic_type();
 674     assert(bt == n2->vect_type()->element_basic_type(), "should be the same");
 675     switch (bt) {
 676     case T_BOOLEAN:
 677     case T_BYTE:
 678       return new PackSNode(n1, n2, TypeVect::make(T_SHORT, 2));
 679     case T_CHAR:
 680     case T_SHORT:
 681       return new PackINode(n1, n2, TypeVect::make(T_INT, 2));
 682     case T_INT:
 683       return new PackLNode(n1, n2, TypeVect::make(T_LONG, 2));
 684     case T_LONG:
 685       return new Pack2LNode(n1, n2, TypeVect::make(T_LONG, 2));
 686     case T_FLOAT:
 687       return new PackDNode(n1, n2, TypeVect::make(T_DOUBLE, 2));
 688     case T_DOUBLE:
 689       return new Pack2DNode(n1, n2, TypeVect::make(T_DOUBLE, 2));
 690     default:
 691       fatal("Type '%s' is not supported for vectors", type2name(bt));
 692       return NULL;
 693     }
 694   }
 695 }
 696 
 697 // Return the vector version of a scalar load node.
 698 LoadVectorNode* LoadVectorNode::make(int opc, Node* ctl, Node* mem,
 699                                      Node* adr, const TypePtr* atyp,
 700                                      uint vlen, BasicType bt,
 701                                      ControlDependency control_dependency) {
 702   const TypeVect* vt = TypeVect::make(bt, vlen);
 703   return new LoadVectorNode(ctl, mem, adr, atyp, vt, control_dependency);
 704 }
 705 
 706 // Return the vector version of a scalar store node.
 707 StoreVectorNode* StoreVectorNode::make(int opc, Node* ctl, Node* mem,
 708                                        Node* adr, const TypePtr* atyp, Node* val,
 709                                        uint vlen) {
 710   return new StoreVectorNode(ctl, mem, adr, atyp, val);
 711 }
 712 
 713 int ExtractNode::opcode(BasicType bt) {
 714   switch (bt) {
 715     case T_BOOLEAN:
 716       return Op_ExtractUB;
 717     case T_BYTE:
 718       return Op_ExtractB;
 719     case T_CHAR:
 720       return Op_ExtractC;
 721     case T_SHORT:
 722       return Op_ExtractS;
 723     case T_INT:
 724       return Op_ExtractI;
 725     case T_LONG:
 726       return Op_ExtractL;
 727     case T_FLOAT:
 728       return Op_ExtractF;
 729     case T_DOUBLE:
 730       return Op_ExtractD;
 731     default:
 732       fatal("Type '%s' is not supported for vectors", type2name(bt));
 733       return 0;
 734   }
 735 }
 736 
 737 // Extract a scalar element of vector.
 738 Node* ExtractNode::make(Node* v, uint position, BasicType bt) {
 739   assert((int)position < Matcher::max_vector_size(bt), "pos in range");
 740   ConINode* pos = ConINode::make((int)position);
 741   switch (bt) {
 742   case T_BOOLEAN:
 743     return new ExtractUBNode(v, pos);
 744   case T_BYTE:
 745     return new ExtractBNode(v, pos);
 746   case T_CHAR:
 747     return new ExtractCNode(v, pos);
 748   case T_SHORT:
 749     return new ExtractSNode(v, pos);
 750   case T_INT:
 751     return new ExtractINode(v, pos);
 752   case T_LONG:
 753     return new ExtractLNode(v, pos);
 754   case T_FLOAT:
 755     return new ExtractFNode(v, pos);
 756   case T_DOUBLE:
 757     return new ExtractDNode(v, pos);
 758   default:
 759     fatal("Type '%s' is not supported for vectors", type2name(bt));
 760     return NULL;
 761   }
 762 }
 763 
 764 int ReductionNode::opcode(int opc, BasicType bt) {
 765   int vopc = opc;
 766   switch (opc) {
 767     case Op_AddI:
 768       switch (bt) {
 769         case T_BOOLEAN:
 770         case T_CHAR: return 0;
 771         case T_BYTE:
 772         case T_SHORT:
 773         case T_INT:
 774           vopc = Op_AddReductionVI;
 775           break;
 776         default:          ShouldNotReachHere(); return 0;
 777       }
 778       break;
 779     case Op_AddL:
 780       assert(bt == T_LONG, "must be");
 781       vopc = Op_AddReductionVL;
 782       break;
 783     case Op_AddF:
 784       assert(bt == T_FLOAT, "must be");
 785       vopc = Op_AddReductionVF;
 786       break;
 787     case Op_AddD:
 788       assert(bt == T_DOUBLE, "must be");
 789       vopc = Op_AddReductionVD;
 790       break;
 791     case Op_MulI:
 792       switch (bt) {
 793         case T_BOOLEAN:
 794         case T_CHAR: return 0;
 795         case T_BYTE:
 796         case T_SHORT:
 797         case T_INT:
 798           vopc = Op_MulReductionVI;
 799           break;
 800         default:          ShouldNotReachHere(); return 0;
 801       }
 802       break;
 803     case Op_MulL:
 804       assert(bt == T_LONG, "must be");
 805       vopc = Op_MulReductionVL;
 806       break;
 807     case Op_MulF:
 808       assert(bt == T_FLOAT, "must be");
 809       vopc = Op_MulReductionVF;
 810       break;
 811     case Op_MulD:
 812       assert(bt == T_DOUBLE, "must be");
 813       vopc = Op_MulReductionVD;
 814       break;
 815     case Op_MinI:
 816       switch (bt) {
 817         case T_BOOLEAN:
 818         case T_CHAR: return 0;
 819         case T_BYTE:
 820         case T_SHORT:
 821         case T_INT:
 822           vopc = Op_MinReductionV;
 823           break;
 824         default: ShouldNotReachHere(); return 0;
 825       }
 826       break;
 827     case Op_MinL:
 828       assert(bt == T_LONG, "must be");
 829       vopc = Op_MinReductionV;
 830       break;
 831     case Op_MinF:
 832       assert(bt == T_FLOAT, "must be");
 833       vopc = Op_MinReductionV;
 834       break;
 835     case Op_MinD:
 836       assert(bt == T_DOUBLE, "must be");
 837       vopc = Op_MinReductionV;
 838       break;
 839     case Op_MaxI:
 840       switch (bt) {
 841         case T_BOOLEAN:
 842         case T_CHAR: return 0;
 843         case T_BYTE:
 844         case T_SHORT:
 845         case T_INT:
 846           vopc = Op_MaxReductionV;
 847           break;
 848         default: ShouldNotReachHere(); return 0;
 849       }
 850       break;
 851     case Op_MaxL:
 852       assert(bt == T_LONG, "must be");
 853       vopc = Op_MaxReductionV;
 854       break;
 855     case Op_MaxF:
 856       assert(bt == T_FLOAT, "must be");
 857       vopc = Op_MaxReductionV;
 858       break;
 859     case Op_MaxD:
 860       assert(bt == T_DOUBLE, "must be");
 861       vopc = Op_MaxReductionV;
 862       break;
 863     case Op_AndI:
 864       switch (bt) {
 865       case T_BOOLEAN:
 866       case T_CHAR: return 0;
 867       case T_BYTE:
 868       case T_SHORT:
 869       case T_INT:
 870         vopc = Op_AndReductionV;
 871         break;
 872       default: ShouldNotReachHere(); return 0;
 873       }
 874       break;
 875     case Op_AndL:
 876       assert(bt == T_LONG, "must be");
 877       vopc = Op_AndReductionV;
 878       break;
 879     case Op_OrI:
 880       switch(bt) {
 881       case T_BOOLEAN:
 882       case T_CHAR: return 0;
 883       case T_BYTE:
 884       case T_SHORT:
 885       case T_INT:
 886         vopc = Op_OrReductionV;
 887         break;
 888       default: ShouldNotReachHere(); return 0;
 889       }
 890       break;
 891     case Op_OrL:
 892       assert(bt == T_LONG, "must be");
 893       vopc = Op_OrReductionV;
 894       break;
 895     case Op_XorI:
 896       switch(bt) {
 897       case T_BOOLEAN:
 898       case T_CHAR: return 0;
 899       case T_BYTE:
 900       case T_SHORT:
 901       case T_INT:
 902         vopc = Op_XorReductionV;
 903         break;
 904       default: ShouldNotReachHere(); return 0;
 905       }
 906       break;
 907     case Op_XorL:
 908       assert(bt == T_LONG, "must be");
 909       vopc = Op_XorReductionV;
 910       break;
 911     default:
 912       break;
 913   }
 914   return vopc;
 915 }
 916 
 917 // Return the appropriate reduction node.
 918 ReductionNode* ReductionNode::make(int opc, Node *ctrl, Node* n1, Node* n2, BasicType bt) {
 919 
 920   int vopc = opcode(opc, bt);
 921 
 922   // This method should not be called for unimplemented vectors.
 923   guarantee(vopc != opc, "Vector for '%s' is not implemented", NodeClassNames[opc]);
 924 
 925   switch (vopc) {
 926   case Op_AddReductionVI: return new AddReductionVINode(ctrl, n1, n2);
 927   case Op_AddReductionVL: return new AddReductionVLNode(ctrl, n1, n2);
 928   case Op_AddReductionVF: return new AddReductionVFNode(ctrl, n1, n2);
 929   case Op_AddReductionVD: return new AddReductionVDNode(ctrl, n1, n2);
 930   case Op_MulReductionVI: return new MulReductionVINode(ctrl, n1, n2);
 931   case Op_MulReductionVL: return new MulReductionVLNode(ctrl, n1, n2);
 932   case Op_MulReductionVF: return new MulReductionVFNode(ctrl, n1, n2);
 933   case Op_MulReductionVD: return new MulReductionVDNode(ctrl, n1, n2);
 934   case Op_MinReductionV:  return new MinReductionVNode(ctrl, n1, n2);
 935   case Op_MaxReductionV:  return new MaxReductionVNode(ctrl, n1, n2);
 936   case Op_AndReductionV:  return new AndReductionVNode(ctrl, n1, n2);
 937   case Op_OrReductionV:   return new OrReductionVNode(ctrl, n1, n2);
 938   case Op_XorReductionV:  return new XorReductionVNode(ctrl, n1, n2);
 939   default:
 940     fatal("Missed vector creation for '%s'", NodeClassNames[vopc]);
 941     return NULL;
 942   }
 943 }
 944 
 945 VectorCastNode* VectorCastNode::make(int vopc, Node* n1, BasicType bt, uint vlen) {
 946   const TypeVect* vt = TypeVect::make(bt, vlen);
 947   switch (vopc) {
 948     case Op_VectorCastB2X: return new VectorCastB2XNode(n1, vt);
 949     case Op_VectorCastS2X: return new VectorCastS2XNode(n1, vt);
 950     case Op_VectorCastI2X: return new VectorCastI2XNode(n1, vt);
 951     case Op_VectorCastL2X: return new VectorCastL2XNode(n1, vt);
 952     case Op_VectorCastF2X: return new VectorCastF2XNode(n1, vt);
 953     case Op_VectorCastD2X: return new VectorCastD2XNode(n1, vt);
 954     default: fatal("unknown node: %s", NodeClassNames[vopc]);
 955   }
 956   return NULL;
 957 }
 958 
 959 int VectorCastNode::opcode(BasicType bt) {
 960   switch (bt) {
 961     case T_BYTE:   return Op_VectorCastB2X;
 962     case T_SHORT:  return Op_VectorCastS2X;
 963     case T_INT:    return Op_VectorCastI2X;
 964     case T_LONG:   return Op_VectorCastL2X;
 965     case T_FLOAT:  return Op_VectorCastF2X;
 966     case T_DOUBLE: return Op_VectorCastD2X;
 967     default: Unimplemented();
 968   }
 969   return 0;
 970 }
 971 
 972 Node* ReductionNode::make_reduction_input(PhaseGVN& gvn, int opc, BasicType bt) {
 973   int vopc = opcode(opc, bt);
 974   guarantee(vopc != opc, "Vector reduction for '%s' is not implemented", NodeClassNames[opc]);
 975 
 976   switch (vopc) {
 977     case Op_AndReductionV:
 978       switch (bt) {
 979         case T_BYTE:
 980         case T_SHORT:
 981         case T_INT:
 982           return gvn.makecon(TypeInt::MINUS_1);
 983         case T_LONG:
 984           return gvn.makecon(TypeLong::MINUS_1);
 985         default:
 986           fatal("Missed vector creation for '%s' as the basic type is not correct.", NodeClassNames[vopc]);
 987           return NULL;
 988       }
 989       break;
 990     case Op_AddReductionVI: // fallthrough
 991     case Op_AddReductionVL: // fallthrough
 992     case Op_AddReductionVF: // fallthrough
 993     case Op_AddReductionVD:
 994     case Op_OrReductionV:
 995     case Op_XorReductionV:
 996       return gvn.zerocon(bt);
 997     case Op_MulReductionVI:
 998       return gvn.makecon(TypeInt::ONE);
 999     case Op_MulReductionVL:
1000       return gvn.makecon(TypeLong::ONE);
1001     case Op_MulReductionVF:
1002       return gvn.makecon(TypeF::ONE);
1003     case Op_MulReductionVD:
1004       return gvn.makecon(TypeD::ONE);
1005     case Op_MinReductionV:
1006       switch (bt) {
1007         case T_BYTE:
1008         case T_SHORT:
1009         case T_INT:
1010           return gvn.makecon(TypeInt::MAX);
1011         case T_LONG:
1012           return gvn.makecon(TypeLong::MAX);
1013         case T_FLOAT:
1014           return gvn.makecon(TypeF::POS_INF);
1015         case T_DOUBLE:
1016           return gvn.makecon(TypeD::POS_INF);
1017           default: Unimplemented(); return NULL;
1018       }
1019       break;
1020     case Op_MaxReductionV:
1021       switch (bt) {
1022         case T_BYTE:
1023         case T_SHORT:
1024         case T_INT:
1025           return gvn.makecon(TypeInt::MIN);
1026         case T_LONG:
1027           return gvn.makecon(TypeLong::MIN);
1028         case T_FLOAT:
1029           return gvn.makecon(TypeF::NEG_INF);
1030         case T_DOUBLE:
1031           return gvn.makecon(TypeD::NEG_INF);
1032           default: Unimplemented(); return NULL;
1033       }
1034       break;
1035     default:
1036       fatal("Missed vector creation for '%s'", NodeClassNames[vopc]);
1037       return NULL;
1038   }
1039 }
1040 
1041 bool ReductionNode::implemented(int opc, uint vlen, BasicType bt) {
1042   if (is_java_primitive(bt) &&
1043       (vlen > 1) && is_power_of_2(vlen) &&
1044       Matcher::vector_size_supported(bt, vlen)) {
1045     int vopc = ReductionNode::opcode(opc, bt);
1046     return vopc != opc && Matcher::match_rule_supported(vopc);
1047   }
1048   return false;
1049 }
1050 
1051 #ifndef PRODUCT
1052 void VectorMaskCmpNode::dump_spec(outputStream *st) const {
1053   st->print(" %d #", _predicate); _type->dump_on(st);
1054 }
1055 #endif // PRODUCT
1056 
1057 Node* VectorReinterpretNode::Identity(PhaseGVN *phase) {
1058   Node* n = in(1);
1059   if (n->Opcode() == Op_VectorReinterpret) {
1060     if (Type::cmp(bottom_type(), n->in(1)->bottom_type()) == 0) {
1061       return n->in(1);
1062     }
1063   }
1064   return this;
1065 }
1066 
1067 Node* VectorInsertNode::make(Node* vec, Node* new_val, int position) {
1068   assert(position < (int)vec->bottom_type()->is_vect()->length(), "pos in range");
1069   ConINode* pos = ConINode::make(position);
1070   return new VectorInsertNode(vec, new_val, pos, vec->bottom_type()->is_vect());
1071 }
1072 
1073 Node* VectorUnboxNode::Identity(PhaseGVN *phase) {
1074   Node* n = obj()->uncast();
1075   if (n->Opcode() == Op_VectorBox) {
1076     if (Type::cmp(bottom_type(), n->in(VectorBoxNode::Value)->bottom_type()) == 0) {
1077       return n->in(VectorBoxNode::Value);
1078     }
1079   }
1080   return this;
1081 }
1082 
1083 const TypeFunc* VectorBoxNode::vec_box_type(const TypeInstPtr* box_type) {
1084   const Type** fields = TypeTuple::fields(0);
1085   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms, fields);
1086 
1087   fields = TypeTuple::fields(1);
1088   fields[TypeFunc::Parms+0] = box_type;
1089   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
1090 
1091   return TypeFunc::make(domain, range);
1092 }
1093 
1094 #ifndef PRODUCT
1095 void VectorBoxAllocateNode::dump_spec(outputStream *st) const {
1096   CallStaticJavaNode::dump_spec(st);
1097 }
1098 #endif // !PRODUCT