1 /*
   2  * Copyright (c) 2020, 2024, 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 
  25 #include "precompiled.hpp"
  26 #include "ci/ciSymbols.hpp"
  27 #include "classfile/vmSymbols.hpp"
  28 #include "opto/library_call.hpp"
  29 #include "opto/runtime.hpp"
  30 #include "opto/vectornode.hpp"
  31 #include "prims/vectorSupport.hpp"
  32 #include "runtime/stubRoutines.hpp"
  33 
  34 #ifdef ASSERT
  35 static bool is_vector(ciKlass* klass) {
  36   return klass->is_subclass_of(ciEnv::current()->vector_VectorPayload_klass());
  37 }
  38 
  39 static bool check_vbox(const TypeInstPtr* vbox_type) {
  40   assert(vbox_type->klass_is_exact(), "");
  41 
  42   ciInstanceKlass* ik = vbox_type->instance_klass();
  43   assert(is_vector(ik), "not a vector");
  44 
  45   ciField* fd1 = ik->get_field_by_name(ciSymbols::ETYPE_name(), ciSymbols::class_signature(), /* is_static */ true);
  46   assert(fd1 != nullptr, "element type info is missing");
  47 
  48   ciConstant val1 = fd1->constant_value();
  49   BasicType elem_bt = val1.as_object()->as_instance()->java_mirror_type()->basic_type();
  50   assert(is_java_primitive(elem_bt), "element type info is missing");
  51 
  52   ciField* fd2 = ik->get_field_by_name(ciSymbols::VLENGTH_name(), ciSymbols::int_signature(), /* is_static */ true);
  53   assert(fd2 != nullptr, "vector length info is missing");
  54 
  55   ciConstant val2 = fd2->constant_value();
  56   assert(val2.as_int() > 0, "vector length info is missing");
  57 
  58   return true;
  59 }
  60 #endif
  61 
  62 #define log_if_needed(...)        \
  63   if (C->print_intrinsics()) {    \
  64     tty->print_cr(__VA_ARGS__);   \
  65   }
  66 
  67 #ifndef PRODUCT
  68 #define non_product_log_if_needed(...) log_if_needed(__VA_ARGS__)
  69 #else
  70 #define non_product_log_if_needed(...)
  71 #endif
  72 
  73 static bool is_vector_mask(ciKlass* klass) {
  74   return klass->is_subclass_of(ciEnv::current()->vector_VectorMask_klass());
  75 }
  76 
  77 static bool is_vector_shuffle(ciKlass* klass) {
  78   return klass->is_subclass_of(ciEnv::current()->vector_VectorShuffle_klass());
  79 }
  80 
  81 bool LibraryCallKit::arch_supports_vector_rotate(int opc, int num_elem, BasicType elem_bt,
  82                                                  VectorMaskUseType mask_use_type, bool has_scalar_args) {
  83   bool is_supported = true;
  84 
  85   // has_scalar_args flag is true only for non-constant scalar shift count,
  86   // since in this case shift needs to be broadcasted.
  87   if (!Matcher::match_rule_supported_vector(opc, num_elem, elem_bt) ||
  88        (has_scalar_args && !arch_supports_vector(Op_Replicate, num_elem, elem_bt, VecMaskNotUsed))) {
  89     is_supported = false;
  90   }
  91 
  92   if (is_supported) {
  93     // Check if mask unboxing is supported, this is a two step process which first loads the contents
  94     // of boolean array into vector followed by either lane expansion to match the lane size of masked
  95     // vector operation or populate the predicate register.
  96     if ((mask_use_type & VecMaskUseLoad) != 0) {
  97       if (!Matcher::match_rule_supported_vector(Op_VectorLoadMask, num_elem, elem_bt) ||
  98           !Matcher::match_rule_supported_vector(Op_LoadVector, num_elem, T_BOOLEAN)) {
  99         non_product_log_if_needed("  ** Rejected vector mask loading (%s,%s,%d) because architecture does not support it",
 100                                   NodeClassNames[Op_VectorLoadMask], type2name(elem_bt), num_elem);
 101         return false;
 102       }
 103     }
 104 
 105     if ((mask_use_type & VecMaskUsePred) != 0) {
 106       if (!Matcher::has_predicated_vectors() ||
 107           !Matcher::match_rule_supported_vector_masked(opc, num_elem, elem_bt)) {
 108         non_product_log_if_needed("Rejected vector mask predicate using (%s,%s,%d) because architecture does not support it",
 109                                   NodeClassNames[opc], type2name(elem_bt), num_elem);
 110         return false;
 111       }
 112     }
 113   }
 114 
 115   int lshiftopc, rshiftopc;
 116   switch(elem_bt) {
 117     case T_BYTE:
 118       lshiftopc = Op_LShiftI;
 119       rshiftopc = Op_URShiftB;
 120       break;
 121     case T_SHORT:
 122       lshiftopc = Op_LShiftI;
 123       rshiftopc = Op_URShiftS;
 124       break;
 125     case T_INT:
 126       lshiftopc = Op_LShiftI;
 127       rshiftopc = Op_URShiftI;
 128       break;
 129     case T_LONG:
 130       lshiftopc = Op_LShiftL;
 131       rshiftopc = Op_URShiftL;
 132       break;
 133     default: fatal("Unexpected type: %s", type2name(elem_bt));
 134   }
 135   int lshiftvopc = VectorNode::opcode(lshiftopc, elem_bt);
 136   int rshiftvopc = VectorNode::opcode(rshiftopc, elem_bt);
 137   if (!is_supported &&
 138       arch_supports_vector(lshiftvopc, num_elem, elem_bt, VecMaskNotUsed, has_scalar_args) &&
 139       arch_supports_vector(rshiftvopc, num_elem, elem_bt, VecMaskNotUsed, has_scalar_args) &&
 140       arch_supports_vector(Op_OrV, num_elem, elem_bt, VecMaskNotUsed)) {
 141     is_supported = true;
 142   }
 143   return is_supported;
 144 }
 145 
 146 Node* GraphKit::box_vector(Node* vector, const TypeInstPtr* vbox_type, BasicType elem_bt, int num_elem, bool deoptimize_on_exception) {
 147   assert(EnableVectorSupport, "");
 148 
 149   PreserveReexecuteState preexecs(this);
 150   jvms()->set_should_reexecute(true);
 151 
 152   VectorBoxAllocateNode* alloc = new VectorBoxAllocateNode(C, vbox_type);
 153   set_edges_for_java_call(alloc, /*must_throw=*/false, /*separate_io_proj=*/true);
 154   make_slow_call_ex(alloc, env()->Throwable_klass(), /*separate_io_proj=*/true, deoptimize_on_exception);
 155   set_i_o(gvn().transform( new ProjNode(alloc, TypeFunc::I_O) ));
 156   set_all_memory(gvn().transform( new ProjNode(alloc, TypeFunc::Memory) ));
 157   Node* ret = gvn().transform(new ProjNode(alloc, TypeFunc::Parms));
 158 
 159   assert(check_vbox(vbox_type), "");
 160   const TypeVect* vt = TypeVect::make(elem_bt, num_elem, is_vector_mask(vbox_type->instance_klass()));
 161   VectorBoxNode* vbox = new VectorBoxNode(C, ret, vector, vbox_type, vt);
 162   return gvn().transform(vbox);
 163 }
 164 
 165 Node* GraphKit::unbox_vector(Node* v, const TypeInstPtr* vbox_type, BasicType elem_bt, int num_elem, bool shuffle_to_vector) {
 166   assert(EnableVectorSupport, "");
 167   const TypeInstPtr* vbox_type_v = gvn().type(v)->isa_instptr();
 168   if (vbox_type_v == nullptr || vbox_type->instance_klass() != vbox_type_v->instance_klass()) {
 169     return nullptr; // arguments don't agree on vector shapes
 170   }
 171   if (vbox_type_v->maybe_null()) {
 172     return nullptr; // no nulls are allowed
 173   }
 174   assert(check_vbox(vbox_type), "");
 175   const TypeVect* vt = TypeVect::make(elem_bt, num_elem, is_vector_mask(vbox_type->instance_klass()));
 176   Node* unbox = gvn().transform(new VectorUnboxNode(C, vt, v, merged_memory(), shuffle_to_vector));
 177   return unbox;
 178 }
 179 
 180 Node* GraphKit::vector_shift_count(Node* cnt, int shift_op, BasicType bt, int num_elem) {
 181   assert(bt == T_INT || bt == T_LONG || bt == T_SHORT || bt == T_BYTE, "byte, short, long and int are supported");
 182   juint mask = (type2aelembytes(bt) * BitsPerByte - 1);
 183   Node* nmask = gvn().transform(ConNode::make(TypeInt::make(mask)));
 184   Node* mcnt = gvn().transform(new AndINode(cnt, nmask));
 185   return gvn().transform(VectorNode::shift_count(shift_op, mcnt, num_elem, bt));
 186 }
 187 
 188 bool LibraryCallKit::arch_supports_vector(int sopc, int num_elem, BasicType type, VectorMaskUseType mask_use_type, bool has_scalar_args) {
 189   // Check that the operation is valid.
 190   if (sopc <= 0) {
 191     non_product_log_if_needed("  ** Rejected intrinsification because no valid vector op could be extracted");
 192     return false;
 193   }
 194 
 195   if (VectorNode::is_vector_rotate(sopc)) {
 196     if(!arch_supports_vector_rotate(sopc, num_elem, type, mask_use_type, has_scalar_args)) {
 197       non_product_log_if_needed("  ** Rejected vector op (%s,%s,%d) because architecture does not support variable vector shifts",
 198                                 NodeClassNames[sopc], type2name(type), num_elem);
 199       return false;
 200     }
 201   } else if (VectorNode::is_vector_integral_negate(sopc)) {
 202     if (!VectorNode::is_vector_integral_negate_supported(sopc, num_elem, type, false)) {
 203       non_product_log_if_needed("  ** Rejected vector op (%s,%s,%d) because architecture does not support integral vector negate",
 204                                 NodeClassNames[sopc], type2name(type), num_elem);
 205       return false;
 206     }
 207   } else {
 208     // Check that architecture supports this op-size-type combination.
 209     if (!Matcher::match_rule_supported_vector(sopc, num_elem, type)) {
 210       non_product_log_if_needed("  ** Rejected vector op (%s,%s,%d) because architecture does not support it",
 211                                 NodeClassNames[sopc], type2name(type), num_elem);
 212       return false;
 213     } else {
 214       assert(Matcher::match_rule_supported(sopc), "must be supported");
 215     }
 216   }
 217 
 218   if (num_elem == 1) {
 219     if (mask_use_type != VecMaskNotUsed) {
 220       non_product_log_if_needed("  ** Rejected vector mask op (%s,%s,%d) because architecture does not support it",
 221                                 NodeClassNames[sopc], type2name(type), num_elem);
 222       return false;
 223     }
 224 
 225     if (sopc != 0) {
 226       if (sopc != Op_LoadVector && sopc != Op_StoreVector) {
 227         non_product_log_if_needed("  ** Not a svml call or load/store vector op (%s,%s,%d)",
 228                                   NodeClassNames[sopc], type2name(type), num_elem);
 229         return false;
 230       }
 231     }
 232   }
 233 
 234   if (!has_scalar_args && VectorNode::is_vector_shift(sopc) &&
 235       Matcher::supports_vector_variable_shifts() == false) {
 236     log_if_needed("  ** Rejected vector op (%s,%s,%d) because architecture does not support variable vector shifts",
 237                   NodeClassNames[sopc], type2name(type), num_elem);
 238     return false;
 239   }
 240 
 241   // Check if mask unboxing is supported, this is a two step process which first loads the contents
 242   // of boolean array into vector followed by either lane expansion to match the lane size of masked
 243   // vector operation or populate the predicate register.
 244   if ((mask_use_type & VecMaskUseLoad) != 0) {
 245     if (!Matcher::match_rule_supported_vector(Op_VectorLoadMask, num_elem, type) ||
 246         !Matcher::match_rule_supported_vector(Op_LoadVector, num_elem, T_BOOLEAN)) {
 247       non_product_log_if_needed("  ** Rejected vector mask loading (%s,%s,%d) because architecture does not support it",
 248                                 NodeClassNames[Op_VectorLoadMask], type2name(type), num_elem);
 249       return false;
 250     }
 251   }
 252 
 253   // Check if mask boxing is supported, this is a two step process which first stores the contents
 254   // of mask vector / predicate register into a boolean vector followed by vector store operation to
 255   // transfer the contents to underlined storage of mask boxes which is a boolean array.
 256   if ((mask_use_type & VecMaskUseStore) != 0) {
 257     if (!Matcher::match_rule_supported_vector(Op_VectorStoreMask, num_elem, type) ||
 258         !Matcher::match_rule_supported_vector(Op_StoreVector, num_elem, T_BOOLEAN)) {
 259       non_product_log_if_needed("Rejected vector mask storing (%s,%s,%d) because architecture does not support it",
 260                                 NodeClassNames[Op_VectorStoreMask], type2name(type), num_elem);
 261       return false;
 262     }
 263   }
 264 
 265   if ((mask_use_type & VecMaskUsePred) != 0) {
 266     bool is_supported = false;
 267     if (Matcher::has_predicated_vectors()) {
 268       if (VectorNode::is_vector_integral_negate(sopc)) {
 269         is_supported = VectorNode::is_vector_integral_negate_supported(sopc, num_elem, type, true);
 270       } else {
 271         is_supported = Matcher::match_rule_supported_vector_masked(sopc, num_elem, type);
 272       }
 273     }
 274     is_supported |= Matcher::supports_vector_predicate_op_emulation(sopc, num_elem, type);
 275 
 276     if (!is_supported) {
 277       non_product_log_if_needed("Rejected vector mask predicate using (%s,%s,%d) because architecture does not support it",
 278                                 NodeClassNames[sopc], type2name(type), num_elem);
 279       return false;
 280     }
 281   }
 282 
 283   return true;
 284 }
 285 
 286 static bool is_klass_initialized(const TypeInstPtr* vec_klass) {
 287   if (vec_klass->const_oop() == nullptr) {
 288     return false; // uninitialized or some kind of unsafe access
 289   }
 290   assert(vec_klass->const_oop()->as_instance()->java_lang_Class_klass() != nullptr, "klass instance expected");
 291   ciInstanceKlass* klass =  vec_klass->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
 292   return klass->is_initialized();
 293 }
 294 
 295 // public static
 296 // <V extends Vector<E>,
 297 //  M extends VectorMask<E>,
 298 //  E>
 299 // V unaryOp(int oprId, Class<? extends V> vmClass, Class<? extends M> maskClass, Class<E> elementType,
 300 //           int length, V v, M m,
 301 //           UnaryOperation<V, M> defaultImpl)
 302 //
 303 // public static
 304 // <V,
 305 //  M extends VectorMask<E>,
 306 //  E>
 307 // V binaryOp(int oprId, Class<? extends V> vmClass, Class<? extends M> maskClass, Class<E> elementType,
 308 //            int length, V v1, V v2, M m,
 309 //            BinaryOperation<V, M> defaultImpl)
 310 //
 311 // public static
 312 // <V extends Vector<E>,
 313 //  M extends VectorMask<E>,
 314 //  E>
 315 // V ternaryOp(int oprId, Class<? extends V> vmClass, Class<? extends M> maskClass, Class<E> elementType,
 316 //             int length, V v1, V v2, V v3, M m,
 317 //             TernaryOperation<V, M> defaultImpl)
 318 //
 319 bool LibraryCallKit::inline_vector_nary_operation(int n) {
 320   const TypeInt*     opr          = gvn().type(argument(0))->isa_int();
 321   const TypeInstPtr* vector_klass = gvn().type(argument(1))->isa_instptr();
 322   const TypeInstPtr* mask_klass   = gvn().type(argument(2))->isa_instptr();
 323   const TypeInstPtr* elem_klass   = gvn().type(argument(3))->isa_instptr();
 324   const TypeInt*     vlen         = gvn().type(argument(4))->isa_int();
 325 
 326   if (opr          == nullptr || !opr->is_con() ||
 327       vector_klass == nullptr || vector_klass->const_oop() == nullptr ||
 328       elem_klass   == nullptr || elem_klass->const_oop()   == nullptr ||
 329       vlen         == nullptr || !vlen->is_con()) {
 330     log_if_needed("  ** missing constant: opr=%s vclass=%s etype=%s vlen=%s",
 331                     NodeClassNames[argument(0)->Opcode()],
 332                     NodeClassNames[argument(1)->Opcode()],
 333                     NodeClassNames[argument(3)->Opcode()],
 334                     NodeClassNames[argument(4)->Opcode()]);
 335     return false; // not enough info for intrinsification
 336   }
 337 
 338   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
 339   if (!elem_type->is_primitive_type()) {
 340     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
 341     return false; // should be primitive type
 342   }
 343   if (!is_klass_initialized(vector_klass)) {
 344     log_if_needed("  ** klass argument not initialized");
 345     return false;
 346   }
 347 
 348   // "argument(n + 5)" should be the mask object. We assume it is "null" when no mask
 349   // is used to control this operation.
 350   const Type* vmask_type = gvn().type(argument(n + 5));
 351   bool is_masked_op = vmask_type != TypePtr::NULL_PTR;
 352   if (is_masked_op) {
 353     if (mask_klass == nullptr || mask_klass->const_oop() == nullptr) {
 354       log_if_needed("  ** missing constant: maskclass=%s", NodeClassNames[argument(2)->Opcode()]);
 355       return false; // not enough info for intrinsification
 356     }
 357 
 358     if (!is_klass_initialized(mask_klass)) {
 359       log_if_needed("  ** mask klass argument not initialized");
 360       return false;
 361     }
 362 
 363     if (vmask_type->maybe_null()) {
 364       log_if_needed("  ** null mask values are not allowed for masked op");
 365       return false;
 366     }
 367   }
 368 
 369   BasicType elem_bt = elem_type->basic_type();
 370   int num_elem = vlen->get_con();
 371   int opc = VectorSupport::vop2ideal(opr->get_con(), elem_bt);
 372   int sopc = VectorNode::opcode(opc, elem_bt);
 373   if ((opc != Op_CallLeafVector) && (sopc == 0)) {
 374     log_if_needed("  ** operation not supported: opc=%s bt=%s", NodeClassNames[opc], type2name(elem_bt));
 375     return false; // operation not supported
 376   }
 377   if (num_elem == 1) {
 378     if (opc != Op_CallLeafVector || elem_bt != T_DOUBLE) {
 379       log_if_needed("  ** not a svml call: arity=%d opc=%d vlen=%d etype=%s",
 380                       n, opc, num_elem, type2name(elem_bt));
 381       return false;
 382     }
 383   }
 384   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
 385   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
 386 
 387   if (is_vector_mask(vbox_klass)) {
 388     assert(!is_masked_op, "mask operations do not need mask to control");
 389   }
 390 
 391   if (opc == Op_CallLeafVector) {
 392     if (!UseVectorStubs) {
 393       log_if_needed("  ** vector stubs support is disabled");
 394       return false;
 395     }
 396     if (!Matcher::supports_vector_calling_convention()) {
 397       log_if_needed("  ** no vector calling conventions supported");
 398       return false;
 399     }
 400     if (!Matcher::vector_size_supported(elem_bt, num_elem)) {
 401       log_if_needed("  ** vector size (vlen=%d, etype=%s) is not supported",
 402                       num_elem, type2name(elem_bt));
 403       return false;
 404     }
 405   }
 406 
 407   // When using mask, mask use type needs to be VecMaskUseLoad.
 408   VectorMaskUseType mask_use_type = is_vector_mask(vbox_klass) ? VecMaskUseAll
 409                                       : is_masked_op ? VecMaskUseLoad : VecMaskNotUsed;
 410   if ((sopc != 0) && !arch_supports_vector(sopc, num_elem, elem_bt, mask_use_type)) {
 411     log_if_needed("  ** not supported: arity=%d opc=%d vlen=%d etype=%s ismask=%d is_masked_op=%d",
 412                     n, sopc, num_elem, type2name(elem_bt),
 413                     is_vector_mask(vbox_klass) ? 1 : 0, is_masked_op ? 1 : 0);
 414     return false; // not supported
 415   }
 416 
 417   // Return true if current platform has implemented the masked operation with predicate feature.
 418   bool use_predicate = is_masked_op && sopc != 0 && arch_supports_vector(sopc, num_elem, elem_bt, VecMaskUsePred);
 419   if (is_masked_op && !use_predicate && !arch_supports_vector(Op_VectorBlend, num_elem, elem_bt, VecMaskUseLoad)) {
 420     log_if_needed("  ** not supported: arity=%d opc=%d vlen=%d etype=%s ismask=0 is_masked_op=1",
 421                     n, sopc, num_elem, type2name(elem_bt));
 422     return false;
 423   }
 424 
 425   Node* opd1 = nullptr; Node* opd2 = nullptr; Node* opd3 = nullptr;
 426   switch (n) {
 427     case 3: {
 428       opd3 = unbox_vector(argument(7), vbox_type, elem_bt, num_elem);
 429       if (opd3 == nullptr) {
 430         log_if_needed("  ** unbox failed v3=%s",
 431                         NodeClassNames[argument(7)->Opcode()]);
 432         return false;
 433       }
 434       // fall-through
 435     }
 436     case 2: {
 437       opd2 = unbox_vector(argument(6), vbox_type, elem_bt, num_elem);
 438       if (opd2 == nullptr) {
 439         log_if_needed("  ** unbox failed v2=%s",
 440                         NodeClassNames[argument(6)->Opcode()]);
 441         return false;
 442       }
 443       // fall-through
 444     }
 445     case 1: {
 446       opd1 = unbox_vector(argument(5), vbox_type, elem_bt, num_elem);
 447       if (opd1 == nullptr) {
 448         log_if_needed("  ** unbox failed v1=%s",
 449                         NodeClassNames[argument(5)->Opcode()]);
 450         return false;
 451       }
 452       break;
 453     }
 454     default: fatal("unsupported arity: %d", n);
 455   }
 456 
 457   Node* mask = nullptr;
 458   if (is_masked_op) {
 459     ciKlass* mbox_klass = mask_klass->const_oop()->as_instance()->java_lang_Class_klass();
 460     assert(is_vector_mask(mbox_klass), "argument(2) should be a mask class");
 461     const TypeInstPtr* mbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, mbox_klass);
 462     mask = unbox_vector(argument(n + 5), mbox_type, elem_bt, num_elem);
 463     if (mask == nullptr) {
 464       log_if_needed("  ** unbox failed mask=%s",
 465                       NodeClassNames[argument(n + 5)->Opcode()]);
 466       return false;
 467     }
 468   }
 469 
 470   Node* operation = nullptr;
 471   if (opc == Op_CallLeafVector) {
 472     assert(UseVectorStubs, "sanity");
 473     operation = gen_call_to_vector_math(opr->get_con(), elem_bt, num_elem, opd1, opd2);
 474     if (operation == nullptr) {
 475       log_if_needed("  ** Vector math call failed for %s_%s_%d",
 476                          (elem_bt == T_FLOAT) ? "float" : "double",
 477                          VectorSupport::mathname[opr->get_con() - VectorSupport::VECTOR_OP_MATH_START],
 478                          num_elem * type2aelembytes(elem_bt));
 479       return false;
 480      }
 481   } else {
 482     const TypeVect* vt = TypeVect::make(elem_bt, num_elem, is_vector_mask(vbox_klass));
 483     switch (n) {
 484       case 1:
 485       case 2: {
 486         operation = VectorNode::make(sopc, opd1, opd2, vt, is_vector_mask(vbox_klass), VectorNode::is_shift_opcode(opc));
 487         break;
 488       }
 489       case 3: {
 490         operation = VectorNode::make(sopc, opd1, opd2, opd3, vt);
 491         break;
 492       }
 493       default: fatal("unsupported arity: %d", n);
 494     }
 495   }
 496 
 497   if (is_masked_op && mask != nullptr) {
 498     if (use_predicate) {
 499       operation->add_req(mask);
 500       operation->add_flag(Node::Flag_is_predicated_vector);
 501     } else {
 502       operation->add_flag(Node::Flag_is_predicated_using_blend);
 503       operation = gvn().transform(operation);
 504       operation = new VectorBlendNode(opd1, operation, mask);
 505     }
 506   }
 507   operation = gvn().transform(operation);
 508 
 509   // Wrap it up in VectorBox to keep object type information.
 510   Node* vbox = box_vector(operation, vbox_type, elem_bt, num_elem);
 511   set_result(vbox);
 512   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
 513   return true;
 514 }
 515 
 516 // Following routine generates IR corresponding to AbstractShuffle::partiallyWrapIndex method,
 517 // which partially wraps index by modulo VEC_LENGTH and generates a negative index value if original
 518 // index is out of valid index range [0, VEC_LENGTH)
 519 //
 520 //   wrapped_index = (VEC_LENGTH - 1) & index
 521 //   if (index u> VEC_LENGTH) {
 522 //     wrapped_index -= VEC_LENGTH;
 523 //
 524 // Note: Unsigned greater than comparison treat both <0 and >VEC_LENGTH indices as out-of-bound
 525 // indexes.
 526 Node* LibraryCallKit::partially_wrap_indexes(Node* index_vec, int num_elem, BasicType elem_bt) {
 527   assert(elem_bt == T_BYTE, "Shuffles use byte array based backing storage.");
 528   const TypeVect* vt  = TypeVect::make(elem_bt, num_elem);
 529   const Type* type_bt = Type::get_const_basic_type(elem_bt);
 530 
 531   Node* mod_mask = gvn().makecon(TypeInt::make(num_elem-1));
 532   Node* bcast_mod_mask  = gvn().transform(VectorNode::scalar2vector(mod_mask, num_elem, type_bt));
 533 
 534   BoolTest::mask pred = BoolTest::ugt;
 535   ConINode* pred_node = (ConINode*)gvn().makecon(TypeInt::make(pred));
 536   Node* lane_cnt  = gvn().makecon(TypeInt::make(num_elem));
 537   Node* bcast_lane_cnt = gvn().transform(VectorNode::scalar2vector(lane_cnt, num_elem, type_bt));
 538   const TypeVect* vmask_type = TypeVect::makemask(type_bt, num_elem);
 539   Node*  mask = gvn().transform(new VectorMaskCmpNode(pred, bcast_lane_cnt, index_vec, pred_node, vmask_type));
 540 
 541   // Make the indices greater than lane count as -ve values to match the java side implementation.
 542   index_vec = gvn().transform(VectorNode::make(Op_AndV, index_vec, bcast_mod_mask, vt));
 543   Node* biased_val = gvn().transform(VectorNode::make(Op_SubVB, index_vec, bcast_lane_cnt, vt));
 544   return gvn().transform(new VectorBlendNode(biased_val, index_vec, mask));
 545 }
 546 
 547 // <Sh extends VectorShuffle<E>,  E>
 548 //  Sh ShuffleIota(Class<?> E, Class<?> shuffleClass, Vector.Species<E> s, int length,
 549 //                  int start, int step, int wrap, ShuffleIotaOperation<Sh, E> defaultImpl)
 550 bool LibraryCallKit::inline_vector_shuffle_iota() {
 551   const TypeInstPtr* shuffle_klass = gvn().type(argument(1))->isa_instptr();
 552   const TypeInt*     vlen          = gvn().type(argument(3))->isa_int();
 553   const TypeInt*     start_val     = gvn().type(argument(4))->isa_int();
 554   const TypeInt*     step_val      = gvn().type(argument(5))->isa_int();
 555   const TypeInt*     wrap          = gvn().type(argument(6))->isa_int();
 556 
 557   if (shuffle_klass == nullptr || shuffle_klass->const_oop() == nullptr ||
 558       vlen          == nullptr || !vlen->is_con() ||
 559       start_val     == nullptr ||
 560       step_val      == nullptr ||
 561       wrap          == nullptr || !wrap->is_con()) {
 562     return false; // not enough info for intrinsification
 563   }
 564 
 565   if (!is_klass_initialized(shuffle_klass)) {
 566     log_if_needed("  ** klass argument not initialized");
 567     return false;
 568   }
 569 
 570   int do_wrap = wrap->get_con();
 571   int num_elem = vlen->get_con();
 572   BasicType elem_bt = T_BYTE;
 573 
 574   bool effective_indices_in_range = false;
 575   if (start_val->is_con() && step_val->is_con()) {
 576     int effective_min_index = start_val->get_con();
 577     int effective_max_index = start_val->get_con() + step_val->get_con() * (num_elem - 1);
 578     effective_indices_in_range = effective_max_index >= effective_min_index && effective_min_index >= -128 && effective_max_index <= 127;
 579   }
 580 
 581   if (!do_wrap && !effective_indices_in_range) {
 582     // Disable instrinsification for unwrapped shuffle iota if start/step
 583     // values are non-constant OR if intermediate result overflows byte value range.
 584     return false;
 585   }
 586 
 587   if (!arch_supports_vector(Op_AddVB, num_elem, elem_bt, VecMaskNotUsed)           ||
 588       !arch_supports_vector(Op_AndV, num_elem, elem_bt, VecMaskNotUsed)            ||
 589       !arch_supports_vector(Op_VectorLoadConst, num_elem, elem_bt, VecMaskNotUsed) ||
 590       !arch_supports_vector(Op_Replicate, num_elem, elem_bt, VecMaskNotUsed)) {
 591     return false;
 592   }
 593 
 594   if (!do_wrap &&
 595       (!arch_supports_vector(Op_SubVB, num_elem, elem_bt, VecMaskNotUsed)       ||
 596       !arch_supports_vector(Op_VectorBlend, num_elem, elem_bt, VecMaskNotUsed)  ||
 597       !arch_supports_vector(Op_VectorMaskCmp, num_elem, elem_bt, VecMaskNotUsed))) {
 598     return false;
 599   }
 600 
 601   bool step_multiply = !step_val->is_con() || !is_power_of_2(step_val->get_con());
 602   if ((step_multiply && !arch_supports_vector(Op_MulVB, num_elem, elem_bt, VecMaskNotUsed)) ||
 603       (!step_multiply && !arch_supports_vector(Op_LShiftVB, num_elem, elem_bt, VecMaskNotUsed))) {
 604     return false;
 605   }
 606 
 607   const Type * type_bt = Type::get_const_basic_type(elem_bt);
 608   const TypeVect * vt  = TypeVect::make(type_bt, num_elem);
 609 
 610   Node* res = gvn().transform(new VectorLoadConstNode(gvn().makecon(TypeInt::ZERO), vt));
 611 
 612   Node* start = argument(4);
 613   Node* step  = argument(5);
 614 
 615   if (step_multiply) {
 616     Node* bcast_step     = gvn().transform(VectorNode::scalar2vector(step, num_elem, type_bt));
 617     res = gvn().transform(VectorNode::make(Op_MulVB, res, bcast_step, vt));
 618   } else if (step_val->get_con() > 1) {
 619     Node* cnt = gvn().makecon(TypeInt::make(log2i_exact(step_val->get_con())));
 620     Node* shift_cnt = vector_shift_count(cnt, Op_LShiftI, elem_bt, num_elem);
 621     res = gvn().transform(VectorNode::make(Op_LShiftVB, res, shift_cnt, vt));
 622   }
 623 
 624   if (!start_val->is_con() || start_val->get_con() != 0) {
 625     Node* bcast_start    = gvn().transform(VectorNode::scalar2vector(start, num_elem, type_bt));
 626     res = gvn().transform(VectorNode::make(Op_AddVB, res, bcast_start, vt));
 627   }
 628 
 629   Node * mod_val = gvn().makecon(TypeInt::make(num_elem-1));
 630   Node * bcast_mod  = gvn().transform(VectorNode::scalar2vector(mod_val, num_elem, type_bt));
 631 
 632   if (do_wrap)  {
 633     // Wrap the indices greater than lane count.
 634     res = gvn().transform(VectorNode::make(Op_AndV, res, bcast_mod, vt));
 635   } else {
 636     res = partially_wrap_indexes(res, num_elem, elem_bt);
 637   }
 638 
 639   ciKlass* sbox_klass = shuffle_klass->const_oop()->as_instance()->java_lang_Class_klass();
 640   const TypeInstPtr* shuffle_box_type = TypeInstPtr::make_exact(TypePtr::NotNull, sbox_klass);
 641 
 642   // Wrap it up in VectorBox to keep object type information.
 643   res = box_vector(res, shuffle_box_type, elem_bt, num_elem);
 644   set_result(res);
 645   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
 646   return true;
 647 }
 648 
 649 // <E, M>
 650 // long maskReductionCoerced(int oper, Class<? extends M> maskClass, Class<?> elemClass,
 651 //                          int length, M m, VectorMaskOp<M> defaultImpl)
 652 bool LibraryCallKit::inline_vector_mask_operation() {
 653   const TypeInt*     oper       = gvn().type(argument(0))->isa_int();
 654   const TypeInstPtr* mask_klass = gvn().type(argument(1))->isa_instptr();
 655   const TypeInstPtr* elem_klass = gvn().type(argument(2))->isa_instptr();
 656   const TypeInt*     vlen       = gvn().type(argument(3))->isa_int();
 657   Node*              mask       = argument(4);
 658 
 659   if (mask_klass == nullptr || mask_klass->const_oop() == nullptr ||
 660       elem_klass == nullptr || elem_klass->const_oop() == nullptr ||
 661       vlen       == nullptr || !vlen->is_con() ||
 662       oper       == nullptr || !oper->is_con() ||
 663       mask->is_top()) {
 664     return false; // dead code
 665   }
 666 
 667   if (!is_klass_initialized(mask_klass)) {
 668     log_if_needed("  ** klass argument not initialized");
 669     return false;
 670   }
 671 
 672   int num_elem = vlen->get_con();
 673   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
 674   BasicType elem_bt = elem_type->basic_type();
 675 
 676   int mopc = VectorSupport::vop2ideal(oper->get_con(), elem_bt);
 677   if (!arch_supports_vector(mopc, num_elem, elem_bt, VecMaskUseLoad)) {
 678     log_if_needed("  ** not supported: arity=1 op=cast#%d/3 vlen2=%d etype2=%s",
 679                     mopc, num_elem, type2name(elem_bt));
 680     return false; // not supported
 681   }
 682 
 683   const Type* elem_ty = Type::get_const_basic_type(elem_bt);
 684   ciKlass* mbox_klass = mask_klass->const_oop()->as_instance()->java_lang_Class_klass();
 685   const TypeInstPtr* mask_box_type = TypeInstPtr::make_exact(TypePtr::NotNull, mbox_klass);
 686   Node* mask_vec = unbox_vector(mask, mask_box_type, elem_bt, num_elem, true);
 687   if (mask_vec == nullptr) {
 688     log_if_needed("  ** unbox failed mask=%s",
 689                       NodeClassNames[argument(4)->Opcode()]);
 690     return false;
 691   }
 692 
 693   if (mask_vec->bottom_type()->isa_vectmask() == nullptr) {
 694     mask_vec = gvn().transform(VectorStoreMaskNode::make(gvn(), mask_vec, elem_bt, num_elem));
 695   }
 696   const Type* maskoper_ty = mopc == Op_VectorMaskToLong ? (const Type*)TypeLong::LONG : (const Type*)TypeInt::INT;
 697   Node* maskoper = gvn().transform(VectorMaskOpNode::make(mask_vec, maskoper_ty, mopc));
 698   if (mopc != Op_VectorMaskToLong) {
 699     maskoper = ConvI2L(maskoper);
 700   }
 701   set_result(maskoper);
 702 
 703   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
 704   return true;
 705 }
 706 
 707 // public static
 708 // <V,
 709 //  Sh extends VectorShuffle<E>,
 710 //  E>
 711 // V shuffleToVector(Class<? extends Vector<E>> vclass, Class<E> elementType,
 712 //                   Class<? extends Sh> shuffleClass, Sh s, int length,
 713 //                   ShuffleToVectorOperation<V, Sh, E> defaultImpl)
 714 bool LibraryCallKit::inline_vector_shuffle_to_vector() {
 715   const TypeInstPtr* vector_klass  = gvn().type(argument(0))->isa_instptr();
 716   const TypeInstPtr* elem_klass    = gvn().type(argument(1))->isa_instptr();
 717   const TypeInstPtr* shuffle_klass = gvn().type(argument(2))->isa_instptr();
 718   Node*              shuffle       = argument(3);
 719   const TypeInt*     vlen          = gvn().type(argument(4))->isa_int();
 720 
 721   if (vector_klass == nullptr || elem_klass == nullptr || shuffle_klass == nullptr || shuffle->is_top() || vlen == nullptr) {
 722     return false; // dead code
 723   }
 724   if (vector_klass->const_oop() == nullptr || elem_klass->const_oop() == nullptr || shuffle_klass->const_oop() == nullptr || !vlen->is_con()) {
 725     return false; // not enough info for intrinsification
 726   }
 727   if (!is_klass_initialized(shuffle_klass) || !is_klass_initialized(vector_klass) ) {
 728     log_if_needed("  ** klass argument not initialized");
 729     return false;
 730   }
 731 
 732   int num_elem = vlen->get_con();
 733   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
 734   BasicType elem_bt = elem_type->basic_type();
 735 
 736   if (num_elem < 4) {
 737     return false;
 738   }
 739 
 740   int cast_vopc = VectorCastNode::opcode(-1, T_BYTE); // from shuffle of type T_BYTE
 741   // Make sure that cast is implemented to particular type/size combination.
 742   if (!arch_supports_vector(cast_vopc, num_elem, elem_bt, VecMaskNotUsed)) {
 743     log_if_needed("  ** not supported: arity=1 op=cast#%d/3 vlen2=%d etype2=%s",
 744         cast_vopc, num_elem, type2name(elem_bt));
 745     return false;
 746   }
 747 
 748   ciKlass* sbox_klass = shuffle_klass->const_oop()->as_instance()->java_lang_Class_klass();
 749   const TypeInstPtr* shuffle_box_type = TypeInstPtr::make_exact(TypePtr::NotNull, sbox_klass);
 750 
 751   // Unbox shuffle with true flag to indicate its load shuffle to vector
 752   // shuffle is a byte array
 753   Node* shuffle_vec = unbox_vector(shuffle, shuffle_box_type, T_BYTE, num_elem, true);
 754 
 755   // cast byte to target element type
 756   shuffle_vec = gvn().transform(VectorCastNode::make(cast_vopc, shuffle_vec, elem_bt, num_elem));
 757 
 758   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
 759   const TypeInstPtr* vec_box_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
 760 
 761   // Box vector
 762   Node* res = box_vector(shuffle_vec, vec_box_type, elem_bt, num_elem);
 763   set_result(res);
 764   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
 765   return true;
 766 }
 767 
 768 // public static
 769 // <E,
 770 //  SH extends VectorShuffle<E>>
 771 // SH wrapShuffleIndexes(Class<E> eClass, Class<? extends SH> shClass, SH sh, int length,
 772 //                       ShuffleWrapIndexesOperation<SH> defaultImpl)
 773 bool LibraryCallKit::inline_vector_wrap_shuffle_indexes() {
 774   const TypeInstPtr* elem_klass    = gvn().type(argument(0))->isa_instptr();
 775   const TypeInstPtr* shuffle_klass = gvn().type(argument(1))->isa_instptr();
 776   Node*              shuffle       = argument(2);
 777   const TypeInt*     vlen          = gvn().type(argument(3))->isa_int();
 778 
 779   if (elem_klass == nullptr || shuffle_klass == nullptr || shuffle->is_top() || vlen == nullptr ||
 780       !vlen->is_con() || shuffle_klass->const_oop() == nullptr) {
 781     // not enough info for intrinsification
 782     return false;
 783   }
 784 
 785   if (!is_klass_initialized(shuffle_klass)) {
 786     log_if_needed("  ** klass argument not initialized");
 787     return false;
 788   }
 789 
 790   int num_elem = vlen->get_con();
 791   if ((num_elem < 4) || !is_power_of_2(num_elem)) {
 792     log_if_needed("  ** vlen < 4 or not power of two=%d", num_elem);
 793     return false;
 794   }
 795 
 796   // Shuffles use byte array based backing storage
 797   BasicType shuffle_bt = T_BYTE;
 798   if (!arch_supports_vector(Op_AndV, num_elem, shuffle_bt, VecMaskNotUsed) ||
 799       !arch_supports_vector(Op_Replicate, num_elem, shuffle_bt, VecMaskNotUsed)) {
 800     log_if_needed("  ** not supported: op=wrapShuffleIndexes vlen=%d etype=%s",
 801                   num_elem, type2name(shuffle_bt));
 802     return false;
 803   }
 804 
 805   ciKlass* sbox_klass = shuffle_klass->const_oop()->as_instance()->java_lang_Class_klass();
 806   const TypeInstPtr* shuffle_box_type = TypeInstPtr::make_exact(TypePtr::NotNull, sbox_klass);
 807 
 808   // Unbox shuffle with true flag to indicate its load shuffle to vector
 809   // shuffle is a byte array
 810   Node* shuffle_vec = unbox_vector(shuffle, shuffle_box_type, shuffle_bt, num_elem, true);
 811 
 812   const TypeVect* vt  = TypeVect::make(shuffle_bt, num_elem);
 813   const Type* shuffle_type_bt = Type::get_const_basic_type(shuffle_bt);
 814   Node* mod_mask = gvn().makecon(TypeInt::make(num_elem-1));
 815   Node* bcast_mod_mask  = gvn().transform(VectorNode::scalar2vector(mod_mask, num_elem, shuffle_type_bt));
 816   // Wrap the indices greater than lane count.
 817   Node* res = gvn().transform(VectorNode::make(Op_AndV, shuffle_vec, bcast_mod_mask, vt));
 818 
 819   // Wrap it up in VectorBox to keep object type information.
 820   res = box_vector(res, shuffle_box_type, shuffle_bt, num_elem);
 821   set_result(res);
 822   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(shuffle_bt))));
 823   return true;
 824 }
 825 
 826 // public static
 827 // <M,
 828 //  S extends VectorSpecies<E>,
 829 //  E>
 830 // M fromBitsCoerced(Class<? extends M> vmClass, Class<E> elementType, int length,
 831 //                    long bits, int mode, S s,
 832 //                    BroadcastOperation<M, E, S> defaultImpl)
 833 bool LibraryCallKit::inline_vector_frombits_coerced() {
 834   const TypeInstPtr* vector_klass = gvn().type(argument(0))->isa_instptr();
 835   const TypeInstPtr* elem_klass   = gvn().type(argument(1))->isa_instptr();
 836   const TypeInt*     vlen         = gvn().type(argument(2))->isa_int();
 837   const TypeLong*    bits_type    = gvn().type(argument(3))->isa_long();
 838   // Mode argument determines the mode of operation it can take following values:-
 839   // MODE_BROADCAST for vector Vector.broadcast and VectorMask.maskAll operations.
 840   // MODE_BITS_COERCED_LONG_TO_MASK for VectorMask.fromLong operation.
 841   const TypeInt*     mode         = gvn().type(argument(5))->isa_int();
 842 
 843   if (vector_klass == nullptr || vector_klass->const_oop() == nullptr ||
 844       elem_klass   == nullptr || elem_klass->const_oop()   == nullptr ||
 845       vlen         == nullptr || !vlen->is_con() ||
 846       bits_type    == nullptr ||
 847       mode         == nullptr || !mode->is_con()) {
 848     log_if_needed("  ** missing constant: vclass=%s etype=%s vlen=%s bitwise=%s",
 849                     NodeClassNames[argument(0)->Opcode()],
 850                     NodeClassNames[argument(1)->Opcode()],
 851                     NodeClassNames[argument(2)->Opcode()],
 852                     NodeClassNames[argument(5)->Opcode()]);
 853     return false; // not enough info for intrinsification
 854   }
 855 
 856   if (!is_klass_initialized(vector_klass)) {
 857     log_if_needed("  ** klass argument not initialized");
 858     return false;
 859   }
 860   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
 861   if (!elem_type->is_primitive_type()) {
 862     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
 863     return false; // should be primitive type
 864   }
 865   BasicType elem_bt = elem_type->basic_type();
 866   int num_elem = vlen->get_con();
 867   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
 868   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
 869 
 870   bool is_mask = is_vector_mask(vbox_klass);
 871   int  bcast_mode = mode->get_con();
 872   VectorMaskUseType checkFlags = (VectorMaskUseType)(is_mask ? VecMaskUseAll : VecMaskNotUsed);
 873   int opc = bcast_mode == VectorSupport::MODE_BITS_COERCED_LONG_TO_MASK ? Op_VectorLongToMask : Op_Replicate;
 874 
 875   if (!arch_supports_vector(opc, num_elem, elem_bt, checkFlags, true /*has_scalar_args*/)) {
 876     log_if_needed("  ** not supported: arity=0 op=broadcast vlen=%d etype=%s ismask=%d bcast_mode=%d",
 877                     num_elem, type2name(elem_bt),
 878                     is_mask ? 1 : 0,
 879                     bcast_mode);
 880     return false; // not supported
 881   }
 882 
 883   Node* broadcast = nullptr;
 884   Node* bits = argument(3);
 885   Node* elem = bits;
 886 
 887   if (opc == Op_VectorLongToMask) {
 888     const TypeVect* vt = TypeVect::makemask(elem_bt, num_elem);
 889     if (vt->isa_vectmask()) {
 890       broadcast = gvn().transform(new VectorLongToMaskNode(elem, vt));
 891     } else {
 892       const TypeVect* mvt = TypeVect::make(T_BOOLEAN, num_elem);
 893       broadcast = gvn().transform(new VectorLongToMaskNode(elem, mvt));
 894       broadcast = gvn().transform(new VectorLoadMaskNode(broadcast, vt));
 895     }
 896   } else {
 897     switch (elem_bt) {
 898       case T_BOOLEAN: // fall-through
 899       case T_BYTE:    // fall-through
 900       case T_SHORT:   // fall-through
 901       case T_CHAR:    // fall-through
 902       case T_INT: {
 903         elem = gvn().transform(new ConvL2INode(bits));
 904         break;
 905       }
 906       case T_DOUBLE: {
 907         elem = gvn().transform(new MoveL2DNode(bits));
 908         break;
 909       }
 910       case T_FLOAT: {
 911         bits = gvn().transform(new ConvL2INode(bits));
 912         elem = gvn().transform(new MoveI2FNode(bits));
 913         break;
 914       }
 915       case T_LONG: {
 916         // no conversion needed
 917         break;
 918       }
 919       default: fatal("%s", type2name(elem_bt));
 920     }
 921     broadcast = VectorNode::scalar2vector(elem, num_elem, Type::get_const_basic_type(elem_bt), is_mask);
 922     broadcast = gvn().transform(broadcast);
 923   }
 924 
 925   Node* box = box_vector(broadcast, vbox_type, elem_bt, num_elem);
 926   set_result(box);
 927   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
 928   return true;
 929 }
 930 
 931 static bool elem_consistent_with_arr(BasicType elem_bt, const TypeAryPtr* arr_type, bool mismatched_ms) {
 932   assert(arr_type != nullptr, "unexpected");
 933   BasicType arr_elem_bt = arr_type->elem()->array_element_basic_type();
 934   if (elem_bt == arr_elem_bt) {
 935     return true;
 936   } else if (elem_bt == T_SHORT && arr_elem_bt == T_CHAR) {
 937     // Load/store of short vector from/to char[] is supported
 938     return true;
 939   } else if (elem_bt == T_BYTE && arr_elem_bt == T_BOOLEAN) {
 940     // Load/store of byte vector from/to boolean[] is supported
 941     return true;
 942   } else {
 943     return mismatched_ms;
 944   }
 945 }
 946 
 947 //  public static
 948 //  <C,
 949 //   VM extends VectorPayload,
 950 //   E,
 951 //   S extends VectorSpecies<E>>
 952 //  VM load(Class<? extends VM> vmClass, Class<E> eClass,
 953 //          int length,
 954 //          Object base, long offset,            // Unsafe addressing
 955 //          boolean fromSegment,
 956 //          C container, long index, S s,        // Arguments for default implementation
 957 //          LoadOperation<C, VM, S> defaultImpl) {
 958 //  public static
 959 //  <C,
 960 //   V extends VectorPayload>
 961 //  void store(Class<?> vClass, Class<?> eClass,
 962 //             int length,
 963 //             Object base, long offset,        // Unsafe addressing
 964 //             boolean fromSegment,
 965 //             V v, C container, long index,    // Arguments for default implementation
 966 //             StoreVectorOperation<C, V> defaultImpl) {
 967 bool LibraryCallKit::inline_vector_mem_operation(bool is_store) {
 968   const TypeInstPtr* vector_klass = gvn().type(argument(0))->isa_instptr();
 969   const TypeInstPtr* elem_klass   = gvn().type(argument(1))->isa_instptr();
 970   const TypeInt*     vlen         = gvn().type(argument(2))->isa_int();
 971   const TypeInt*     from_ms      = gvn().type(argument(6))->isa_int();
 972 
 973   if (vector_klass == nullptr || vector_klass->const_oop() == nullptr ||
 974       elem_klass   == nullptr || elem_klass->const_oop()   == nullptr ||
 975       vlen         == nullptr || !vlen->is_con() ||
 976       from_ms      == nullptr || !from_ms->is_con()) {
 977     log_if_needed("  ** missing constant: vclass=%s etype=%s vlen=%s from_ms=%s",
 978                     NodeClassNames[argument(0)->Opcode()],
 979                     NodeClassNames[argument(1)->Opcode()],
 980                     NodeClassNames[argument(2)->Opcode()],
 981                     NodeClassNames[argument(6)->Opcode()]);
 982     return false; // not enough info for intrinsification
 983   }
 984   if (!is_klass_initialized(vector_klass)) {
 985     log_if_needed("  ** klass argument not initialized");
 986     return false;
 987   }
 988 
 989   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
 990   if (!elem_type->is_primitive_type()) {
 991     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
 992     return false; // should be primitive type
 993   }
 994   BasicType elem_bt = elem_type->basic_type();
 995   int num_elem = vlen->get_con();
 996 
 997   // TODO When mask usage is supported, VecMaskNotUsed needs to be VecMaskUseLoad.
 998   if (!arch_supports_vector(is_store ? Op_StoreVector : Op_LoadVector, num_elem, elem_bt, VecMaskNotUsed)) {
 999     log_if_needed("  ** not supported: arity=%d op=%s vlen=%d etype=%s ismask=no",
1000                     is_store, is_store ? "store" : "load",
1001                     num_elem, type2name(elem_bt));
1002     return false; // not supported
1003   }
1004 
1005   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
1006   bool is_mask = is_vector_mask(vbox_klass);
1007 
1008   Node* base = argument(3);
1009   Node* offset = ConvL2X(argument(4));
1010 
1011   // Save state and restore on bailout
1012   uint old_sp = sp();
1013   SafePointNode* old_map = clone_map();
1014 
1015   Node* addr = make_unsafe_address(base, offset, (is_mask ? T_BOOLEAN : elem_bt), true);
1016 
1017   // The memory barrier checks are based on ones for unsafe access.
1018   // This is not 1-1 implementation.
1019   const Type *const base_type = gvn().type(base);
1020 
1021   const TypePtr *addr_type = gvn().type(addr)->isa_ptr();
1022   const TypeAryPtr* arr_type = addr_type->isa_aryptr();
1023 
1024   const bool in_native = TypePtr::NULL_PTR == base_type; // base always null
1025   const bool in_heap   = !TypePtr::NULL_PTR->higher_equal(base_type); // base never null
1026 
1027   const bool is_mixed_access = !in_heap && !in_native;
1028 
1029   const bool is_mismatched_access = in_heap && (addr_type->isa_aryptr() == nullptr);
1030 
1031   const bool needs_cpu_membar = is_mixed_access || is_mismatched_access;
1032 
1033   // For non-masked mismatched memory segment vector read/write accesses, intrinsification can continue
1034   // with unknown backing storage type and compiler can skip inserting explicit reinterpretation IR after
1035   // loading from or before storing to backing storage which is mandatory for semantic correctness of
1036   // big-endian memory layout.
1037   bool mismatched_ms = LITTLE_ENDIAN_ONLY(false)
1038       BIG_ENDIAN_ONLY(from_ms->get_con() && !is_mask && arr_type != nullptr &&
1039                       arr_type->elem()->array_element_basic_type() != elem_bt);
1040   BasicType mem_elem_bt = mismatched_ms ? arr_type->elem()->array_element_basic_type() : elem_bt;
1041   if (!is_java_primitive(mem_elem_bt)) {
1042     log_if_needed("  ** non-primitive array element type");
1043     return false;
1044   }
1045   int mem_num_elem = mismatched_ms ? (num_elem * type2aelembytes(elem_bt)) / type2aelembytes(mem_elem_bt) : num_elem;
1046   if (arr_type != nullptr && !is_mask && !elem_consistent_with_arr(elem_bt, arr_type, mismatched_ms)) {
1047     log_if_needed("  ** not supported: arity=%d op=%s vlen=%d etype=%s atype=%s ismask=no",
1048                     is_store, is_store ? "store" : "load",
1049                     num_elem, type2name(elem_bt), type2name(arr_type->elem()->array_element_basic_type()));
1050     set_map(old_map);
1051     set_sp(old_sp);
1052     return false;
1053   }
1054 
1055   // In case of mismatched memory segment accesses, we need to double check that the source type memory operations are supported by backend.
1056   if (mismatched_ms) {
1057     if (is_store) {
1058       if (!arch_supports_vector(Op_StoreVector, num_elem, elem_bt, VecMaskNotUsed)
1059           || !arch_supports_vector(Op_VectorReinterpret, mem_num_elem, mem_elem_bt, VecMaskNotUsed)) {
1060         log_if_needed("  ** not supported: arity=%d op=%s vlen=%d*8 etype=%s/8 ismask=no",
1061                         is_store, "store",
1062                         num_elem, type2name(elem_bt));
1063         set_map(old_map);
1064         set_sp(old_sp);
1065         return false; // not supported
1066       }
1067     } else {
1068       if (!arch_supports_vector(Op_LoadVector, mem_num_elem, mem_elem_bt, VecMaskNotUsed)
1069           || !arch_supports_vector(Op_VectorReinterpret, num_elem, elem_bt, VecMaskNotUsed)) {
1070         log_if_needed("  ** not supported: arity=%d op=%s vlen=%d*8 etype=%s/8 ismask=no",
1071                         is_store, "load",
1072                         mem_num_elem, type2name(mem_elem_bt));
1073         set_map(old_map);
1074         set_sp(old_sp);
1075         return false; // not supported
1076       }
1077     }
1078   }
1079   if (is_mask) {
1080     if (!is_store) {
1081       if (!arch_supports_vector(Op_LoadVector, num_elem, elem_bt, VecMaskUseLoad)) {
1082         set_map(old_map);
1083         set_sp(old_sp);
1084         return false; // not supported
1085       }
1086     } else {
1087       if (!arch_supports_vector(Op_StoreVector, num_elem, elem_bt, VecMaskUseStore)) {
1088         set_map(old_map);
1089         set_sp(old_sp);
1090         return false; // not supported
1091       }
1092     }
1093   }
1094 
1095   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
1096 
1097   if (needs_cpu_membar) {
1098     insert_mem_bar(Op_MemBarCPUOrder);
1099   }
1100 
1101   if (is_store) {
1102     Node* val = unbox_vector(argument(7), vbox_type, elem_bt, num_elem);
1103     if (val == nullptr) {
1104       set_map(old_map);
1105       set_sp(old_sp);
1106       return false; // operand unboxing failed
1107     }
1108     set_all_memory(reset_memory());
1109 
1110     // In case the store needs to happen to byte array, reinterpret the incoming vector to byte vector.
1111     int store_num_elem = num_elem;
1112     if (mismatched_ms) {
1113       store_num_elem = mem_num_elem;
1114       const TypeVect* to_vect_type = TypeVect::make(mem_elem_bt, store_num_elem);
1115       val = gvn().transform(new VectorReinterpretNode(val, val->bottom_type()->is_vect(), to_vect_type));
1116     }
1117     if (is_mask) {
1118       val = gvn().transform(VectorStoreMaskNode::make(gvn(), val, elem_bt, num_elem));
1119     }
1120     Node* vstore = gvn().transform(StoreVectorNode::make(0, control(), memory(addr), addr, addr_type, val, store_num_elem));
1121     set_memory(vstore, addr_type);
1122   } else {
1123     // When using byte array, we need to load as byte then reinterpret the value. Otherwise, do a simple vector load.
1124     Node* vload = nullptr;
1125     if (mismatched_ms) {
1126       vload = gvn().transform(LoadVectorNode::make(0, control(), memory(addr), addr, addr_type, mem_num_elem, mem_elem_bt));
1127       const TypeVect* to_vect_type = TypeVect::make(elem_bt, num_elem);
1128       vload = gvn().transform(new VectorReinterpretNode(vload, vload->bottom_type()->is_vect(), to_vect_type));
1129     } else {
1130       // Special handle for masks
1131       if (is_mask) {
1132         vload = gvn().transform(LoadVectorNode::make(0, control(), memory(addr), addr, addr_type, num_elem, T_BOOLEAN));
1133         vload = gvn().transform(new VectorLoadMaskNode(vload, TypeVect::makemask(elem_bt, num_elem)));
1134       } else {
1135         vload = gvn().transform(LoadVectorNode::make(0, control(), memory(addr), addr, addr_type, num_elem, elem_bt));
1136       }
1137     }
1138     Node* box = box_vector(vload, vbox_type, elem_bt, num_elem);
1139     set_result(box);
1140   }
1141 
1142   destruct_map_clone(old_map);
1143 
1144   if (needs_cpu_membar) {
1145     insert_mem_bar(Op_MemBarCPUOrder);
1146   }
1147 
1148   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
1149   return true;
1150 }
1151 
1152 //  public static
1153 //  <C,
1154 //   V extends Vector<?>,
1155 //   E,
1156 //   S extends VectorSpecies<E>,
1157 //   M extends VectorMask<E>>
1158 //  V loadMasked(Class<? extends V> vClass, Class<M> mClass, Class<E> eClass,
1159 //               int length, Object base, long offset,          // Unsafe addressing
1160 //               boolean fromSegment,
1161 //               M m, int offsetInRange,
1162 //               C container, long index, S s,                  // Arguments for default implementation
1163 //               LoadVectorMaskedOperation<C, V, S, M> defaultImpl) {
1164 //  public static
1165 //  <C,
1166 //   V extends Vector<E>,
1167 //   M extends VectorMask<E>,
1168 //   E>
1169 //  void storeMasked(Class<? extends V> vClass, Class<M> mClass, Class<E> eClass,
1170 //                   int length,
1171 //                   Object base, long offset,                  // Unsafe addressing
1172 //                   boolean fromSegment,
1173 //                   V v, M m, C container, long index,         // Arguments for default implementation
1174 //                   StoreVectorMaskedOperation<C, V, M> defaultImpl) {
1175 
1176 bool LibraryCallKit::inline_vector_mem_masked_operation(bool is_store) {
1177   const TypeInstPtr* vector_klass = gvn().type(argument(0))->isa_instptr();
1178   const TypeInstPtr* mask_klass   = gvn().type(argument(1))->isa_instptr();
1179   const TypeInstPtr* elem_klass   = gvn().type(argument(2))->isa_instptr();
1180   const TypeInt*     vlen         = gvn().type(argument(3))->isa_int();
1181   const TypeInt*     from_ms      = gvn().type(argument(7))->isa_int();
1182 
1183   if (vector_klass == nullptr || vector_klass->const_oop() == nullptr ||
1184       mask_klass   == nullptr || mask_klass->const_oop()   == nullptr ||
1185       elem_klass   == nullptr || elem_klass->const_oop()   == nullptr ||
1186       vlen         == nullptr || !vlen->is_con() ||
1187       from_ms      == nullptr || !from_ms->is_con()) {
1188     log_if_needed("  ** missing constant: vclass=%s mclass=%s etype=%s vlen=%s from_ms=%s",
1189                     NodeClassNames[argument(0)->Opcode()],
1190                     NodeClassNames[argument(1)->Opcode()],
1191                     NodeClassNames[argument(2)->Opcode()],
1192                     NodeClassNames[argument(3)->Opcode()],
1193                     NodeClassNames[argument(7)->Opcode()]);
1194     return false; // not enough info for intrinsification
1195   }
1196   if (!is_klass_initialized(vector_klass)) {
1197     log_if_needed("  ** klass argument not initialized");
1198     return false;
1199   }
1200 
1201   if (!is_klass_initialized(mask_klass)) {
1202     log_if_needed("  ** mask klass argument not initialized");
1203     return false;
1204   }
1205 
1206   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
1207   if (!elem_type->is_primitive_type()) {
1208     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
1209     return false; // should be primitive type
1210   }
1211 
1212   BasicType elem_bt = elem_type->basic_type();
1213   int num_elem = vlen->get_con();
1214 
1215   Node* base = argument(4);
1216   Node* offset = ConvL2X(argument(5));
1217 
1218   // Save state and restore on bailout
1219   uint old_sp = sp();
1220   SafePointNode* old_map = clone_map();
1221 
1222   Node* addr = make_unsafe_address(base, offset, elem_bt, true);
1223   const TypePtr *addr_type = gvn().type(addr)->isa_ptr();
1224   const TypeAryPtr* arr_type = addr_type->isa_aryptr();
1225 
1226   bool mismatched_ms = from_ms->get_con() && arr_type != nullptr && arr_type->elem()->array_element_basic_type() != elem_bt;
1227   BIG_ENDIAN_ONLY(if (mismatched_ms) return false;)
1228   // If there is no consistency between array and vector element types, it must be special byte array case
1229   if (arr_type != nullptr && !elem_consistent_with_arr(elem_bt, arr_type, mismatched_ms)) {
1230     log_if_needed("  ** not supported: arity=%d op=%s vlen=%d etype=%s atype=%s",
1231                     is_store, is_store ? "storeMasked" : "loadMasked",
1232                     num_elem, type2name(elem_bt), type2name(arr_type->elem()->array_element_basic_type()));
1233     set_map(old_map);
1234     set_sp(old_sp);
1235     return false;
1236   }
1237 
1238   int mem_num_elem = mismatched_ms ? num_elem * type2aelembytes(elem_bt) : num_elem;
1239   BasicType mem_elem_bt = mismatched_ms ? T_BYTE : elem_bt;
1240   bool supports_predicate = arch_supports_vector(is_store ? Op_StoreVectorMasked : Op_LoadVectorMasked,
1241                                                 mem_num_elem, mem_elem_bt, VecMaskUseLoad);
1242 
1243   // If current arch does not support the predicated operations, we have to bail
1244   // out when current case uses the predicate feature.
1245   if (!supports_predicate) {
1246     bool needs_predicate = false;
1247     if (is_store) {
1248       // Masked vector store always uses the predicated store.
1249       needs_predicate = true;
1250     } else {
1251       // Masked vector load with IOOBE always uses the predicated load.
1252       const TypeInt* offset_in_range = gvn().type(argument(9))->isa_int();
1253       if (!offset_in_range->is_con()) {
1254         log_if_needed("  ** missing constant: offsetInRange=%s",
1255                         NodeClassNames[argument(8)->Opcode()]);
1256         set_map(old_map);
1257         set_sp(old_sp);
1258         return false;
1259       }
1260       needs_predicate = (offset_in_range->get_con() == 0);
1261     }
1262 
1263     if (needs_predicate) {
1264       log_if_needed("  ** not supported: op=%s vlen=%d etype=%s mismatched_ms=%d",
1265                       is_store ? "storeMasked" : "loadMasked",
1266                       num_elem, type2name(elem_bt), mismatched_ms ? 1 : 0);
1267       set_map(old_map);
1268       set_sp(old_sp);
1269       return false;
1270     }
1271   }
1272 
1273   // This only happens for masked vector load. If predicate is not supported, then check whether
1274   // the normal vector load and blend operations are supported by backend.
1275   if (!supports_predicate && (!arch_supports_vector(Op_LoadVector, mem_num_elem, mem_elem_bt, VecMaskNotUsed) ||
1276       !arch_supports_vector(Op_VectorBlend, mem_num_elem, mem_elem_bt, VecMaskUseLoad))) {
1277     log_if_needed("  ** not supported: op=loadMasked vlen=%d etype=%s mismatched_ms=%d",
1278                     num_elem, type2name(elem_bt), mismatched_ms ? 1 : 0);
1279     set_map(old_map);
1280     set_sp(old_sp);
1281     return false;
1282   }
1283 
1284   // Since we are using byte array, we need to double check that the vector reinterpret operation
1285   // with byte type is supported by backend.
1286   if (mismatched_ms) {
1287     if (!arch_supports_vector(Op_VectorReinterpret, mem_num_elem, T_BYTE, VecMaskNotUsed)) {
1288       log_if_needed("  ** not supported: arity=%d op=%s vlen=%d etype=%s mismatched_ms=1",
1289                       is_store, is_store ? "storeMasked" : "loadMasked",
1290                       num_elem, type2name(elem_bt));
1291       set_map(old_map);
1292       set_sp(old_sp);
1293       return false;
1294     }
1295   }
1296 
1297   // Since it needs to unbox the mask, we need to double check that the related load operations
1298   // for mask are supported by backend.
1299   if (!arch_supports_vector(Op_LoadVector, num_elem, elem_bt, VecMaskUseLoad)) {
1300     log_if_needed("  ** not supported: arity=%d op=%s vlen=%d etype=%s",
1301                       is_store, is_store ? "storeMasked" : "loadMasked",
1302                       num_elem, type2name(elem_bt));
1303     set_map(old_map);
1304     set_sp(old_sp);
1305     return false;
1306   }
1307 
1308   // Can base be null? Otherwise, always on-heap access.
1309   bool can_access_non_heap = TypePtr::NULL_PTR->higher_equal(gvn().type(base));
1310   if (can_access_non_heap) {
1311     insert_mem_bar(Op_MemBarCPUOrder);
1312   }
1313 
1314   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
1315   ciKlass* mbox_klass = mask_klass->const_oop()->as_instance()->java_lang_Class_klass();
1316   assert(!is_vector_mask(vbox_klass) && is_vector_mask(mbox_klass), "Invalid class type");
1317   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
1318   const TypeInstPtr* mbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, mbox_klass);
1319 
1320   Node* mask = unbox_vector(is_store ? argument(9) : argument(8), mbox_type, elem_bt, num_elem);
1321   if (mask == nullptr) {
1322     log_if_needed("  ** unbox failed mask=%s",
1323                     is_store ? NodeClassNames[argument(9)->Opcode()]
1324                              : NodeClassNames[argument(8)->Opcode()]);
1325     set_map(old_map);
1326     set_sp(old_sp);
1327     return false;
1328   }
1329 
1330   if (is_store) {
1331     Node* val = unbox_vector(argument(8), vbox_type, elem_bt, num_elem);
1332     if (val == nullptr) {
1333       log_if_needed("  ** unbox failed vector=%s",
1334                       NodeClassNames[argument(8)->Opcode()]);
1335       set_map(old_map);
1336       set_sp(old_sp);
1337       return false; // operand unboxing failed
1338     }
1339     set_all_memory(reset_memory());
1340 
1341     if (mismatched_ms) {
1342       // Reinterpret the incoming vector to byte vector.
1343       const TypeVect* to_vect_type = TypeVect::make(mem_elem_bt, mem_num_elem);
1344       val = gvn().transform(new VectorReinterpretNode(val, val->bottom_type()->is_vect(), to_vect_type));
1345       // Reinterpret the vector mask to byte type.
1346       const TypeVect* from_mask_type = TypeVect::makemask(elem_bt, num_elem);
1347       const TypeVect* to_mask_type = TypeVect::makemask(mem_elem_bt, mem_num_elem);
1348       mask = gvn().transform(new VectorReinterpretNode(mask, from_mask_type, to_mask_type));
1349     }
1350     Node* vstore = gvn().transform(new StoreVectorMaskedNode(control(), memory(addr), addr, val, addr_type, mask));
1351     set_memory(vstore, addr_type);
1352   } else {
1353     Node* vload = nullptr;
1354 
1355     if (mismatched_ms) {
1356       // Reinterpret the vector mask to byte type.
1357       const TypeVect* from_mask_type = TypeVect::makemask(elem_bt, num_elem);
1358       const TypeVect* to_mask_type = TypeVect::makemask(mem_elem_bt, mem_num_elem);
1359       mask = gvn().transform(new VectorReinterpretNode(mask, from_mask_type, to_mask_type));
1360     }
1361 
1362     if (supports_predicate) {
1363       // Generate masked load vector node if predicate feature is supported.
1364       const TypeVect* vt = TypeVect::make(mem_elem_bt, mem_num_elem);
1365       vload = gvn().transform(new LoadVectorMaskedNode(control(), memory(addr), addr, addr_type, vt, mask));
1366     } else {
1367       // Use the vector blend to implement the masked load vector. The biased elements are zeros.
1368       Node* zero = gvn().transform(gvn().zerocon(mem_elem_bt));
1369       zero = gvn().transform(VectorNode::scalar2vector(zero, mem_num_elem, Type::get_const_basic_type(mem_elem_bt)));
1370       vload = gvn().transform(LoadVectorNode::make(0, control(), memory(addr), addr, addr_type, mem_num_elem, mem_elem_bt));
1371       vload = gvn().transform(new VectorBlendNode(zero, vload, mask));
1372     }
1373 
1374     if (mismatched_ms) {
1375       const TypeVect* to_vect_type = TypeVect::make(elem_bt, num_elem);
1376       vload = gvn().transform(new VectorReinterpretNode(vload, vload->bottom_type()->is_vect(), to_vect_type));
1377     }
1378 
1379     Node* box = box_vector(vload, vbox_type, elem_bt, num_elem);
1380     set_result(box);
1381   }
1382 
1383   destruct_map_clone(old_map);
1384 
1385   if (can_access_non_heap) {
1386     insert_mem_bar(Op_MemBarCPUOrder);
1387   }
1388 
1389   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
1390   return true;
1391 }
1392 
1393 // <C,
1394 //  V extends Vector<?>,
1395 //  W extends Vector<Integer>,
1396 //  S extends VectorSpecies<E>,
1397 //  M extends VectorMask<E>,
1398 //  E>
1399 // V loadWithMap(Class<? extends V> vectorClass, Class<M> maskClass, Class<E> elementType, int length,
1400 //               Class<? extends Vector<Integer>> vectorIndexClass,
1401 //               Object base, long offset, // Unsafe addressing
1402 //               W index_vector, M m,
1403 //               C container, int index, int[] indexMap, int indexM, S s, // Arguments for default implementation
1404 //               LoadVectorOperationWithMap<C, V, E, S, M> defaultImpl)
1405 //
1406 //  <C,
1407 //   V extends Vector<E>,
1408 //   W extends Vector<Integer>,
1409 //   M extends VectorMask<E>,
1410 //   E>
1411 //  void storeWithMap(Class<? extends V> vectorClass, Class<M> maskClass, Class<E> elementType,
1412 //                    int length, Class<? extends Vector<Integer>> vectorIndexClass, Object base, long offset,    // Unsafe addressing
1413 //                    W index_vector, V v, M m,
1414 //                    C container, int index, int[] indexMap, int indexM, // Arguments for default implementation
1415 //                    StoreVectorOperationWithMap<C, V, M, E> defaultImpl)
1416 //
1417 bool LibraryCallKit::inline_vector_gather_scatter(bool is_scatter) {
1418   const TypeInstPtr* vector_klass     = gvn().type(argument(0))->isa_instptr();
1419   const TypeInstPtr* mask_klass       = gvn().type(argument(1))->isa_instptr();
1420   const TypeInstPtr* elem_klass       = gvn().type(argument(2))->isa_instptr();
1421   const TypeInt*     vlen             = gvn().type(argument(3))->isa_int();
1422   const TypeInstPtr* vector_idx_klass = gvn().type(argument(4))->isa_instptr();
1423 
1424   if (vector_klass     == nullptr || vector_klass->const_oop()     == nullptr ||
1425 //      mask_klass       == nullptr || mask_klass->const_oop()       == nullptr ||
1426       elem_klass       == nullptr || elem_klass->const_oop()       == nullptr ||
1427       vlen             == nullptr || !vlen->is_con() ||
1428       vector_idx_klass == nullptr || vector_idx_klass->const_oop() == nullptr) {
1429     log_if_needed("  ** missing constant: vclass=%s etype=%s vlen=%s viclass=%s",
1430                     NodeClassNames[argument(0)->Opcode()],
1431                     NodeClassNames[argument(2)->Opcode()],
1432                     NodeClassNames[argument(3)->Opcode()],
1433                     NodeClassNames[argument(4)->Opcode()]);
1434     return false; // not enough info for intrinsification
1435   }
1436 
1437   if (!is_klass_initialized(vector_klass) || !is_klass_initialized(vector_idx_klass)) {
1438     log_if_needed("  ** klass argument not initialized");
1439     return false;
1440   }
1441 
1442   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
1443   if (!elem_type->is_primitive_type()) {
1444     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
1445     return false; // should be primitive type
1446   }
1447 
1448   BasicType elem_bt = elem_type->basic_type();
1449   int num_elem = vlen->get_con();
1450 
1451   const Type* vmask_type = gvn().type(is_scatter ? argument(10) : argument(9));
1452   bool is_masked_op = vmask_type != TypePtr::NULL_PTR;
1453   if (is_masked_op) {
1454     if (mask_klass == nullptr || mask_klass->const_oop() == nullptr) {
1455       log_if_needed("  ** missing constant: maskclass=%s", NodeClassNames[argument(1)->Opcode()]);
1456       return false; // not enough info for intrinsification
1457     }
1458 
1459     if (!is_klass_initialized(mask_klass)) {
1460       log_if_needed("  ** mask klass argument not initialized");
1461       return false;
1462     }
1463 
1464     if (vmask_type->maybe_null()) {
1465       log_if_needed("  ** null mask values are not allowed for masked op");
1466       return false;
1467     }
1468 
1469     // Check whether the predicated gather/scatter node is supported by architecture.
1470     VectorMaskUseType mask = (VectorMaskUseType) (VecMaskUseLoad | VecMaskUsePred);
1471     if (!arch_supports_vector(is_scatter ? Op_StoreVectorScatterMasked : Op_LoadVectorGatherMasked, num_elem, elem_bt, mask)) {
1472       log_if_needed("  ** not supported: arity=%d op=%s vlen=%d etype=%s is_masked_op=1",
1473                       is_scatter, is_scatter ? "scatterMasked" : "gatherMasked",
1474                       num_elem, type2name(elem_bt));
1475       return false; // not supported
1476     }
1477   } else {
1478     // Check whether the normal gather/scatter node is supported for non-masked operation.
1479     if (!arch_supports_vector(is_scatter ? Op_StoreVectorScatter : Op_LoadVectorGather, num_elem, elem_bt, VecMaskNotUsed)) {
1480       log_if_needed("  ** not supported: arity=%d op=%s vlen=%d etype=%s is_masked_op=0",
1481                       is_scatter, is_scatter ? "scatter" : "gather",
1482                       num_elem, type2name(elem_bt));
1483       return false; // not supported
1484     }
1485   }
1486 
1487   // Check that the vector holding indices is supported by architecture
1488   // For sub-word gathers expander receive index array.
1489   if (!is_subword_type(elem_bt) && !arch_supports_vector(Op_LoadVector, num_elem, T_INT, VecMaskNotUsed)) {
1490       log_if_needed("  ** not supported: arity=%d op=%s/loadindex vlen=%d etype=int is_masked_op=%d",
1491                       is_scatter, is_scatter ? "scatter" : "gather",
1492                       num_elem, is_masked_op ? 1 : 0);
1493       return false; // not supported
1494   }
1495 
1496   Node* base = argument(5);
1497   Node* offset = ConvL2X(argument(6));
1498 
1499   // Save state and restore on bailout
1500   uint old_sp = sp();
1501   SafePointNode* old_map = clone_map();
1502 
1503   Node* addr = make_unsafe_address(base, offset, elem_bt, true);
1504 
1505   const TypePtr *addr_type = gvn().type(addr)->isa_ptr();
1506   const TypeAryPtr* arr_type = addr_type->isa_aryptr();
1507 
1508   // The array must be consistent with vector type
1509   if (arr_type == nullptr || (arr_type != nullptr && !elem_consistent_with_arr(elem_bt, arr_type, false))) {
1510     log_if_needed("  ** not supported: arity=%d op=%s vlen=%d etype=%s atype=%s ismask=no",
1511                     is_scatter, is_scatter ? "scatter" : "gather",
1512                     num_elem, type2name(elem_bt), type2name(arr_type->elem()->array_element_basic_type()));
1513     set_map(old_map);
1514     set_sp(old_sp);
1515     return false;
1516   }
1517 
1518   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
1519   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
1520   ciKlass* vbox_idx_klass = vector_idx_klass->const_oop()->as_instance()->java_lang_Class_klass();
1521   if (vbox_idx_klass == nullptr) {
1522     set_map(old_map);
1523     set_sp(old_sp);
1524     return false;
1525   }
1526 
1527   Node* index_vect = nullptr;
1528   const TypeInstPtr* vbox_idx_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_idx_klass);
1529   if (!is_subword_type(elem_bt)) {
1530     index_vect = unbox_vector(argument(8), vbox_idx_type, T_INT, num_elem);
1531     if (index_vect == nullptr) {
1532       set_map(old_map);
1533       set_sp(old_sp);
1534       return false;
1535     }
1536   }
1537 
1538   Node* mask = nullptr;
1539   if (is_masked_op) {
1540     ciKlass* mbox_klass = mask_klass->const_oop()->as_instance()->java_lang_Class_klass();
1541     const TypeInstPtr* mbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, mbox_klass);
1542     mask = unbox_vector(is_scatter ? argument(10) : argument(9), mbox_type, elem_bt, num_elem);
1543     if (mask == nullptr) {
1544       log_if_needed("  ** unbox failed mask=%s",
1545                     is_scatter ? NodeClassNames[argument(10)->Opcode()]
1546                                : NodeClassNames[argument(9)->Opcode()]);
1547       set_map(old_map);
1548       set_sp(old_sp);
1549       return false;
1550     }
1551   }
1552 
1553   const TypeVect* vector_type = TypeVect::make(elem_bt, num_elem);
1554   if (is_scatter) {
1555     Node* val = unbox_vector(argument(9), vbox_type, elem_bt, num_elem);
1556     if (val == nullptr) {
1557       set_map(old_map);
1558       set_sp(old_sp);
1559       return false; // operand unboxing failed
1560     }
1561     set_all_memory(reset_memory());
1562 
1563     Node* vstore = nullptr;
1564     if (mask != nullptr) {
1565       vstore = gvn().transform(new StoreVectorScatterMaskedNode(control(), memory(addr), addr, addr_type, val, index_vect, mask));
1566     } else {
1567       vstore = gvn().transform(new StoreVectorScatterNode(control(), memory(addr), addr, addr_type, val, index_vect));
1568     }
1569     set_memory(vstore, addr_type);
1570   } else {
1571     Node* vload = nullptr;
1572     Node* index    = argument(11);
1573     Node* indexMap = argument(12);
1574     Node* indexM   = argument(13);
1575     if (mask != nullptr) {
1576       if (is_subword_type(elem_bt)) {
1577         Node* index_arr_base = array_element_address(indexMap, indexM, T_INT);
1578         vload = gvn().transform(new LoadVectorGatherMaskedNode(control(), memory(addr), addr, addr_type, vector_type, index_arr_base, mask, index));
1579       } else {
1580         vload = gvn().transform(new LoadVectorGatherMaskedNode(control(), memory(addr), addr, addr_type, vector_type, index_vect, mask));
1581       }
1582     } else {
1583       if (is_subword_type(elem_bt)) {
1584         Node* index_arr_base = array_element_address(indexMap, indexM, T_INT);
1585         vload = gvn().transform(new LoadVectorGatherNode(control(), memory(addr), addr, addr_type, vector_type, index_arr_base, index));
1586       } else {
1587         vload = gvn().transform(new LoadVectorGatherNode(control(), memory(addr), addr, addr_type, vector_type, index_vect));
1588       }
1589     }
1590     Node* box = box_vector(vload, vbox_type, elem_bt, num_elem);
1591     set_result(box);
1592   }
1593 
1594   destruct_map_clone(old_map);
1595 
1596   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
1597   return true;
1598 }
1599 
1600 // public static
1601 // <V extends Vector<E>,
1602 //  M extends VectorMask<E>,
1603 //  E>
1604 // long reductionCoerced(int oprId, Class<? extends V> vectorClass, Class<? extends M> maskClass,
1605 //                       Class<E> elementType, int length, V v, M m,
1606 //                       ReductionOperation<V, M> defaultImpl)
1607 bool LibraryCallKit::inline_vector_reduction() {
1608   const TypeInt*     opr          = gvn().type(argument(0))->isa_int();
1609   const TypeInstPtr* vector_klass = gvn().type(argument(1))->isa_instptr();
1610   const TypeInstPtr* mask_klass   = gvn().type(argument(2))->isa_instptr();
1611   const TypeInstPtr* elem_klass   = gvn().type(argument(3))->isa_instptr();
1612   const TypeInt*     vlen         = gvn().type(argument(4))->isa_int();
1613 
1614   if (opr          == nullptr || !opr->is_con() ||
1615       vector_klass == nullptr || vector_klass->const_oop() == nullptr ||
1616 //      mask_klass   == nullptr || mask_klass->const_oop()   == nullptr ||
1617       elem_klass   == nullptr || elem_klass->const_oop()   == nullptr ||
1618       vlen         == nullptr || !vlen->is_con()) {
1619     log_if_needed("  ** missing constant: opr=%s vclass=%s etype=%s vlen=%s",
1620                     NodeClassNames[argument(0)->Opcode()],
1621                     NodeClassNames[argument(1)->Opcode()],
1622                     NodeClassNames[argument(3)->Opcode()],
1623                     NodeClassNames[argument(4)->Opcode()]);
1624     return false; // not enough info for intrinsification
1625   }
1626   if (!is_klass_initialized(vector_klass)) {
1627     log_if_needed("  ** klass argument not initialized");
1628     return false;
1629   }
1630   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
1631   if (!elem_type->is_primitive_type()) {
1632     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
1633     return false; // should be primitive type
1634   }
1635 
1636   const Type* vmask_type = gvn().type(argument(6));
1637   bool is_masked_op = vmask_type != TypePtr::NULL_PTR;
1638   if (is_masked_op) {
1639     if (mask_klass == nullptr || mask_klass->const_oop() == nullptr) {
1640       log_if_needed("  ** missing constant: maskclass=%s", NodeClassNames[argument(2)->Opcode()]);
1641       return false; // not enough info for intrinsification
1642     }
1643 
1644     if (!is_klass_initialized(mask_klass)) {
1645       log_if_needed("  ** mask klass argument not initialized");
1646       return false;
1647     }
1648 
1649     if (vmask_type->maybe_null()) {
1650       log_if_needed("  ** null mask values are not allowed for masked op");
1651       return false;
1652     }
1653   }
1654 
1655   BasicType elem_bt = elem_type->basic_type();
1656   int num_elem = vlen->get_con();
1657   int opc  = VectorSupport::vop2ideal(opr->get_con(), elem_bt);
1658   int sopc = ReductionNode::opcode(opc, elem_bt);
1659 
1660   // When using mask, mask use type needs to be VecMaskUseLoad.
1661   if (!arch_supports_vector(sopc, num_elem, elem_bt, is_masked_op ? VecMaskUseLoad : VecMaskNotUsed)) {
1662     log_if_needed("  ** not supported: arity=1 op=%d/reduce vlen=%d etype=%s is_masked_op=%d",
1663                     sopc, num_elem, type2name(elem_bt), is_masked_op ? 1 : 0);
1664     return false;
1665   }
1666 
1667   // Return true if current platform has implemented the masked operation with predicate feature.
1668   bool use_predicate = is_masked_op && arch_supports_vector(sopc, num_elem, elem_bt, VecMaskUsePred);
1669   if (is_masked_op && !use_predicate && !arch_supports_vector(Op_VectorBlend, num_elem, elem_bt, VecMaskUseLoad)) {
1670     log_if_needed("  ** not supported: arity=1 op=%d/reduce vlen=%d etype=%s is_masked_op=1",
1671                     sopc, num_elem, type2name(elem_bt));
1672     return false;
1673   }
1674 
1675   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
1676   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
1677 
1678   Node* opd = unbox_vector(argument(5), vbox_type, elem_bt, num_elem);
1679   if (opd == nullptr) {
1680     return false; // operand unboxing failed
1681   }
1682 
1683   Node* mask = nullptr;
1684   if (is_masked_op) {
1685     ciKlass* mbox_klass = mask_klass->const_oop()->as_instance()->java_lang_Class_klass();
1686     assert(is_vector_mask(mbox_klass), "argument(2) should be a mask class");
1687     const TypeInstPtr* mbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, mbox_klass);
1688     mask = unbox_vector(argument(6), mbox_type, elem_bt, num_elem);
1689     if (mask == nullptr) {
1690       log_if_needed("  ** unbox failed mask=%s",
1691                       NodeClassNames[argument(6)->Opcode()]);
1692       return false;
1693     }
1694   }
1695 
1696   Node* init = ReductionNode::make_identity_con_scalar(gvn(), opc, elem_bt);
1697   Node* value = opd;
1698 
1699   assert(mask != nullptr || !is_masked_op, "Masked op needs the mask value never null");
1700   if (mask != nullptr && !use_predicate) {
1701     Node* reduce_identity = gvn().transform(VectorNode::scalar2vector(init, num_elem, Type::get_const_basic_type(elem_bt)));
1702     value = gvn().transform(new VectorBlendNode(reduce_identity, value, mask));
1703   }
1704 
1705   // Make an unordered Reduction node. This affects only AddReductionVF/VD and MulReductionVF/VD,
1706   // as these operations are allowed to be associative (not requiring strict order) in VectorAPI.
1707   value = ReductionNode::make(opc, nullptr, init, value, elem_bt, /* requires_strict_order */ false);
1708 
1709   if (mask != nullptr && use_predicate) {
1710     value->add_req(mask);
1711     value->add_flag(Node::Flag_is_predicated_vector);
1712   }
1713 
1714   value = gvn().transform(value);
1715 
1716   Node* bits = nullptr;
1717   switch (elem_bt) {
1718     case T_BYTE:
1719     case T_SHORT:
1720     case T_INT: {
1721       bits = gvn().transform(new ConvI2LNode(value));
1722       break;
1723     }
1724     case T_FLOAT: {
1725       value = gvn().transform(new MoveF2INode(value));
1726       bits  = gvn().transform(new ConvI2LNode(value));
1727       break;
1728     }
1729     case T_DOUBLE: {
1730       bits = gvn().transform(new MoveD2LNode(value));
1731       break;
1732     }
1733     case T_LONG: {
1734       bits = value; // no conversion needed
1735       break;
1736     }
1737     default: fatal("%s", type2name(elem_bt));
1738   }
1739   set_result(bits);
1740   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
1741   return true;
1742 }
1743 
1744 // public static <V> boolean test(int cond, Class<?> vectorClass, Class<?> elementType, int vlen,
1745 //                                V v1, V v2,
1746 //                                BiFunction<V, V, Boolean> defaultImpl)
1747 //
1748 bool LibraryCallKit::inline_vector_test() {
1749   const TypeInt*     cond         = gvn().type(argument(0))->isa_int();
1750   const TypeInstPtr* vector_klass = gvn().type(argument(1))->isa_instptr();
1751   const TypeInstPtr* elem_klass   = gvn().type(argument(2))->isa_instptr();
1752   const TypeInt*     vlen         = gvn().type(argument(3))->isa_int();
1753 
1754   if (cond         == nullptr || !cond->is_con() ||
1755       vector_klass == nullptr || vector_klass->const_oop() == nullptr ||
1756       elem_klass   == nullptr || elem_klass->const_oop()   == nullptr ||
1757       vlen         == nullptr || !vlen->is_con()) {
1758     log_if_needed("  ** missing constant: cond=%s vclass=%s etype=%s vlen=%s",
1759                     NodeClassNames[argument(0)->Opcode()],
1760                     NodeClassNames[argument(1)->Opcode()],
1761                     NodeClassNames[argument(2)->Opcode()],
1762                     NodeClassNames[argument(3)->Opcode()]);
1763     return false; // not enough info for intrinsification
1764   }
1765   if (!is_klass_initialized(vector_klass)) {
1766     log_if_needed("  ** klass argument not initialized");
1767     return false;
1768   }
1769   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
1770   if (!elem_type->is_primitive_type()) {
1771     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
1772     return false; // should be primitive type
1773   }
1774   BasicType elem_bt = elem_type->basic_type();
1775   int num_elem = vlen->get_con();
1776   BoolTest::mask booltest = (BoolTest::mask)cond->get_con();
1777   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
1778   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
1779 
1780   if (!arch_supports_vector(Op_VectorTest, num_elem, elem_bt, is_vector_mask(vbox_klass) ? VecMaskUseLoad : VecMaskNotUsed)) {
1781     log_if_needed("  ** not supported: arity=2 op=test/%d vlen=%d etype=%s ismask=%d",
1782                     cond->get_con(), num_elem, type2name(elem_bt),
1783                     is_vector_mask(vbox_klass));
1784     return false;
1785   }
1786 
1787   Node* opd1 = unbox_vector(argument(4), vbox_type, elem_bt, num_elem);
1788   Node* opd2;
1789   if (Matcher::vectortest_needs_second_argument(booltest == BoolTest::overflow,
1790                                                 opd1->bottom_type()->isa_vectmask())) {
1791     opd2 = unbox_vector(argument(5), vbox_type, elem_bt, num_elem);
1792   } else {
1793     opd2 = opd1;
1794   }
1795   if (opd1 == nullptr || opd2 == nullptr) {
1796     return false; // operand unboxing failed
1797   }
1798 
1799   Node* cmp = gvn().transform(new VectorTestNode(opd1, opd2, booltest));
1800   BoolTest::mask test = Matcher::vectortest_mask(booltest == BoolTest::overflow,
1801                                                  opd1->bottom_type()->isa_vectmask(), num_elem);
1802   Node* bol = gvn().transform(new BoolNode(cmp, test));
1803   Node* res = gvn().transform(new CMoveINode(bol, gvn().intcon(0), gvn().intcon(1), TypeInt::BOOL));
1804 
1805   set_result(res);
1806   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
1807   return true;
1808 }
1809 
1810 // public static
1811 // <V extends Vector<E>,
1812 //  M extends VectorMask<E>,
1813 //  E>
1814 // V blend(Class<? extends V> vectorClass, Class<M> maskClass, Class<E> elementType, int vlen,
1815 //         V v1, V v2, M m,
1816 //         VectorBlendOp<V, M, E> defaultImpl)
1817 bool LibraryCallKit::inline_vector_blend() {
1818   const TypeInstPtr* vector_klass = gvn().type(argument(0))->isa_instptr();
1819   const TypeInstPtr* mask_klass   = gvn().type(argument(1))->isa_instptr();
1820   const TypeInstPtr* elem_klass   = gvn().type(argument(2))->isa_instptr();
1821   const TypeInt*     vlen         = gvn().type(argument(3))->isa_int();
1822 
1823   if (mask_klass == nullptr || vector_klass == nullptr || elem_klass == nullptr || vlen == nullptr) {
1824     return false; // dead code
1825   }
1826   if (mask_klass->const_oop() == nullptr || vector_klass->const_oop() == nullptr ||
1827       elem_klass->const_oop() == nullptr || !vlen->is_con()) {
1828     log_if_needed("  ** missing constant: vclass=%s mclass=%s etype=%s vlen=%s",
1829                     NodeClassNames[argument(0)->Opcode()],
1830                     NodeClassNames[argument(1)->Opcode()],
1831                     NodeClassNames[argument(2)->Opcode()],
1832                     NodeClassNames[argument(3)->Opcode()]);
1833     return false; // not enough info for intrinsification
1834   }
1835   if (!is_klass_initialized(vector_klass) || !is_klass_initialized(mask_klass)) {
1836     log_if_needed("  ** klass argument not initialized");
1837     return false;
1838   }
1839   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
1840   if (!elem_type->is_primitive_type()) {
1841     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
1842     return false; // should be primitive type
1843   }
1844   BasicType elem_bt = elem_type->basic_type();
1845   BasicType mask_bt = elem_bt;
1846   int num_elem = vlen->get_con();
1847 
1848   if (!arch_supports_vector(Op_VectorBlend, num_elem, elem_bt, VecMaskUseLoad)) {
1849     log_if_needed("  ** not supported: arity=2 op=blend vlen=%d etype=%s ismask=useload",
1850                     num_elem, type2name(elem_bt));
1851     return false; // not supported
1852   }
1853   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
1854   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
1855 
1856   ciKlass* mbox_klass = mask_klass->const_oop()->as_instance()->java_lang_Class_klass();
1857   const TypeInstPtr* mbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, mbox_klass);
1858 
1859   Node* v1   = unbox_vector(argument(4), vbox_type, elem_bt, num_elem);
1860   Node* v2   = unbox_vector(argument(5), vbox_type, elem_bt, num_elem);
1861   Node* mask = unbox_vector(argument(6), mbox_type, mask_bt, num_elem);
1862 
1863   if (v1 == nullptr || v2 == nullptr || mask == nullptr) {
1864     return false; // operand unboxing failed
1865   }
1866 
1867   Node* blend = gvn().transform(new VectorBlendNode(v1, v2, mask));
1868 
1869   Node* box = box_vector(blend, vbox_type, elem_bt, num_elem);
1870   set_result(box);
1871   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
1872   return true;
1873 }
1874 
1875 //  public static
1876 //  <V extends Vector<E>,
1877 //   M extends VectorMask<E>,
1878 //   E>
1879 //  M compare(int cond, Class<? extends V> vectorClass, Class<M> maskClass, Class<E> elementType, int vlen,
1880 //            V v1, V v2, M m,
1881 //            VectorCompareOp<V,M> defaultImpl)
1882 bool LibraryCallKit::inline_vector_compare() {
1883   const TypeInt*     cond         = gvn().type(argument(0))->isa_int();
1884   const TypeInstPtr* vector_klass = gvn().type(argument(1))->isa_instptr();
1885   const TypeInstPtr* mask_klass   = gvn().type(argument(2))->isa_instptr();
1886   const TypeInstPtr* elem_klass   = gvn().type(argument(3))->isa_instptr();
1887   const TypeInt*     vlen         = gvn().type(argument(4))->isa_int();
1888 
1889   if (cond == nullptr || vector_klass == nullptr || mask_klass == nullptr || elem_klass == nullptr || vlen == nullptr) {
1890     return false; // dead code
1891   }
1892   if (!cond->is_con() || vector_klass->const_oop() == nullptr || mask_klass->const_oop() == nullptr ||
1893       elem_klass->const_oop() == nullptr || !vlen->is_con()) {
1894     log_if_needed("  ** missing constant: cond=%s vclass=%s mclass=%s etype=%s vlen=%s",
1895                     NodeClassNames[argument(0)->Opcode()],
1896                     NodeClassNames[argument(1)->Opcode()],
1897                     NodeClassNames[argument(2)->Opcode()],
1898                     NodeClassNames[argument(3)->Opcode()],
1899                     NodeClassNames[argument(4)->Opcode()]);
1900     return false; // not enough info for intrinsification
1901   }
1902   if (!is_klass_initialized(vector_klass) || !is_klass_initialized(mask_klass)) {
1903     log_if_needed("  ** klass argument not initialized");
1904     return false;
1905   }
1906   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
1907   if (!elem_type->is_primitive_type()) {
1908     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
1909     return false; // should be primitive type
1910   }
1911 
1912   int num_elem = vlen->get_con();
1913   BasicType elem_bt = elem_type->basic_type();
1914   BasicType mask_bt = elem_bt;
1915 
1916   if ((cond->get_con() & BoolTest::unsigned_compare) != 0) {
1917     if (!Matcher::supports_vector_comparison_unsigned(num_elem, elem_bt)) {
1918       log_if_needed("  ** not supported: unsigned comparison op=comp/%d vlen=%d etype=%s ismask=usestore",
1919                       cond->get_con() & (BoolTest::unsigned_compare - 1), num_elem, type2name(elem_bt));
1920       return false;
1921     }
1922   }
1923 
1924   if (!arch_supports_vector(Op_VectorMaskCmp, num_elem, elem_bt, VecMaskUseStore)) {
1925     log_if_needed("  ** not supported: arity=2 op=comp/%d vlen=%d etype=%s ismask=usestore",
1926                     cond->get_con(), num_elem, type2name(elem_bt));
1927     return false;
1928   }
1929 
1930   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
1931   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
1932 
1933   ciKlass* mbox_klass = mask_klass->const_oop()->as_instance()->java_lang_Class_klass();
1934   const TypeInstPtr* mbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, mbox_klass);
1935 
1936   Node* v1 = unbox_vector(argument(5), vbox_type, elem_bt, num_elem);
1937   Node* v2 = unbox_vector(argument(6), vbox_type, elem_bt, num_elem);
1938 
1939   bool is_masked_op = argument(7)->bottom_type() != TypePtr::NULL_PTR;
1940   Node* mask = is_masked_op ? unbox_vector(argument(7), mbox_type, elem_bt, num_elem) : nullptr;
1941   if (is_masked_op && mask == nullptr) {
1942     log_if_needed("  ** not supported: mask = null arity=2 op=comp/%d vlen=%d etype=%s ismask=usestore is_masked_op=1",
1943                     cond->get_con(), num_elem, type2name(elem_bt));
1944     return false;
1945   }
1946 
1947   bool use_predicate = is_masked_op && arch_supports_vector(Op_VectorMaskCmp, num_elem, elem_bt, VecMaskUsePred);
1948   if (is_masked_op && !use_predicate && !arch_supports_vector(Op_AndV, num_elem, elem_bt, VecMaskUseLoad)) {
1949     log_if_needed("  ** not supported: arity=2 op=comp/%d vlen=%d etype=%s ismask=usestore is_masked_op=1",
1950                     cond->get_con(), num_elem, type2name(elem_bt));
1951     return false;
1952   }
1953 
1954   if (v1 == nullptr || v2 == nullptr) {
1955     return false; // operand unboxing failed
1956   }
1957   BoolTest::mask pred = (BoolTest::mask)cond->get_con();
1958   ConINode* pred_node = (ConINode*)gvn().makecon(cond);
1959 
1960   const TypeVect* vmask_type = TypeVect::makemask(mask_bt, num_elem);
1961   Node* operation = new VectorMaskCmpNode(pred, v1, v2, pred_node, vmask_type);
1962 
1963   if (is_masked_op) {
1964     if (use_predicate) {
1965       operation->add_req(mask);
1966       operation->add_flag(Node::Flag_is_predicated_vector);
1967     } else {
1968       operation = gvn().transform(operation);
1969       operation = VectorNode::make(Op_AndV, operation, mask, vmask_type);
1970     }
1971   }
1972 
1973   operation = gvn().transform(operation);
1974 
1975   Node* box = box_vector(operation, mbox_type, mask_bt, num_elem);
1976   set_result(box);
1977   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
1978   return true;
1979 }
1980 
1981 // public static
1982 // <V extends Vector<E>,
1983 //  Sh extends VectorShuffle<E>,
1984 //  M extends VectorMask<E>,
1985 //  E>
1986 // V rearrangeOp(Class<? extends V> vectorClass, Class<Sh> shuffleClass, Class<M> maskClass, Class<E> elementType, int vlen,
1987 //               V v1, Sh sh, M m,
1988 //               VectorRearrangeOp<V, Sh, M, E> defaultImpl)
1989 bool LibraryCallKit::inline_vector_rearrange() {
1990   const TypeInstPtr* vector_klass  = gvn().type(argument(0))->isa_instptr();
1991   const TypeInstPtr* shuffle_klass = gvn().type(argument(1))->isa_instptr();
1992   const TypeInstPtr* mask_klass    = gvn().type(argument(2))->isa_instptr();
1993   const TypeInstPtr* elem_klass    = gvn().type(argument(3))->isa_instptr();
1994   const TypeInt*     vlen          = gvn().type(argument(4))->isa_int();
1995 
1996   if (vector_klass == nullptr  || shuffle_klass == nullptr ||  elem_klass == nullptr || vlen == nullptr) {
1997     return false; // dead code
1998   }
1999   if (shuffle_klass->const_oop() == nullptr ||
2000       vector_klass->const_oop()  == nullptr ||
2001       elem_klass->const_oop()    == nullptr ||
2002       !vlen->is_con()) {
2003     log_if_needed("  ** missing constant: vclass=%s sclass=%s etype=%s vlen=%s",
2004                     NodeClassNames[argument(0)->Opcode()],
2005                     NodeClassNames[argument(1)->Opcode()],
2006                     NodeClassNames[argument(3)->Opcode()],
2007                     NodeClassNames[argument(4)->Opcode()]);
2008     return false; // not enough info for intrinsification
2009   }
2010   if (!is_klass_initialized(vector_klass)  ||
2011       !is_klass_initialized(shuffle_klass)) {
2012     log_if_needed("  ** klass argument not initialized");
2013     return false;
2014   }
2015   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
2016   if (!elem_type->is_primitive_type()) {
2017     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
2018     return false; // should be primitive type
2019   }
2020   BasicType elem_bt = elem_type->basic_type();
2021   BasicType shuffle_bt = elem_bt;
2022   int num_elem = vlen->get_con();
2023 
2024   if (!arch_supports_vector(Op_VectorLoadShuffle, num_elem, elem_bt, VecMaskNotUsed)) {
2025     log_if_needed("  ** not supported: arity=0 op=load/shuffle vlen=%d etype=%s ismask=no",
2026                     num_elem, type2name(elem_bt));
2027     return false; // not supported
2028   }
2029 
2030   bool is_masked_op = argument(7)->bottom_type() != TypePtr::NULL_PTR;
2031   bool use_predicate = is_masked_op;
2032   if (is_masked_op &&
2033       (mask_klass == nullptr ||
2034        mask_klass->const_oop() == nullptr ||
2035        !is_klass_initialized(mask_klass))) {
2036     log_if_needed("  ** mask_klass argument not initialized");
2037   }
2038   VectorMaskUseType checkFlags = (VectorMaskUseType)(is_masked_op ? (VecMaskUseLoad | VecMaskUsePred) : VecMaskNotUsed);
2039   if (!arch_supports_vector(Op_VectorRearrange, num_elem, elem_bt, checkFlags)) {
2040     use_predicate = false;
2041     if(!is_masked_op ||
2042        (!arch_supports_vector(Op_VectorRearrange, num_elem, elem_bt, VecMaskNotUsed) ||
2043         !arch_supports_vector(Op_VectorBlend, num_elem, elem_bt, VecMaskUseLoad)     ||
2044         !arch_supports_vector(Op_Replicate, num_elem, elem_bt, VecMaskNotUsed))) {
2045       log_if_needed("  ** not supported: arity=2 op=shuffle/rearrange vlen=%d etype=%s ismask=no",
2046                       num_elem, type2name(elem_bt));
2047       return false; // not supported
2048     }
2049   }
2050   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
2051   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
2052 
2053   ciKlass* shbox_klass = shuffle_klass->const_oop()->as_instance()->java_lang_Class_klass();
2054   const TypeInstPtr* shbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, shbox_klass);
2055 
2056   Node* v1 = unbox_vector(argument(5), vbox_type, elem_bt, num_elem);
2057   Node* shuffle = unbox_vector(argument(6), shbox_type, shuffle_bt, num_elem);
2058 
2059   if (v1 == nullptr || shuffle == nullptr) {
2060     return false; // operand unboxing failed
2061   }
2062 
2063   Node* mask = nullptr;
2064   if (is_masked_op) {
2065     ciKlass* mbox_klass = mask_klass->const_oop()->as_instance()->java_lang_Class_klass();
2066     const TypeInstPtr* mbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, mbox_klass);
2067     mask = unbox_vector(argument(7), mbox_type, elem_bt, num_elem);
2068     if (mask == nullptr) {
2069       log_if_needed("  ** not supported: arity=3 op=shuffle/rearrange vlen=%d etype=%s ismask=useload is_masked_op=1",
2070                       num_elem, type2name(elem_bt));
2071       return false;
2072     }
2073   }
2074 
2075   Node* rearrange = new VectorRearrangeNode(v1, shuffle);
2076   if (is_masked_op) {
2077     if (use_predicate) {
2078       rearrange->add_req(mask);
2079       rearrange->add_flag(Node::Flag_is_predicated_vector);
2080     } else {
2081       const TypeVect* vt = v1->bottom_type()->is_vect();
2082       rearrange = gvn().transform(rearrange);
2083       Node* zero = gvn().makecon(Type::get_zero_type(elem_bt));
2084       Node* zerovec = gvn().transform(VectorNode::scalar2vector(zero, num_elem, Type::get_const_basic_type(elem_bt)));
2085       rearrange = new VectorBlendNode(zerovec, rearrange, mask);
2086     }
2087   }
2088   rearrange = gvn().transform(rearrange);
2089 
2090   Node* box = box_vector(rearrange, vbox_type, elem_bt, num_elem);
2091   set_result(box);
2092   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
2093   return true;
2094 }
2095 
2096 static address get_vector_math_address(int vop, int bits, BasicType bt, char* name_ptr, int name_len) {
2097   address addr = nullptr;
2098   assert(UseVectorStubs, "sanity");
2099   assert(name_ptr != nullptr, "unexpected");
2100   assert((vop >= VectorSupport::VECTOR_OP_MATH_START) && (vop <= VectorSupport::VECTOR_OP_MATH_END), "unexpected");
2101   int op = vop - VectorSupport::VECTOR_OP_MATH_START;
2102 
2103   switch(bits) {
2104     case 64:  //fallthough
2105     case 128: //fallthough
2106     case 256: //fallthough
2107     case 512:
2108       if (bt == T_FLOAT) {
2109         snprintf(name_ptr, name_len, "vector_%s_float_%dbits_fixed", VectorSupport::mathname[op], bits);
2110         addr = StubRoutines::_vector_f_math[exact_log2(bits/64)][op];
2111       } else {
2112         assert(bt == T_DOUBLE, "must be FP type only");
2113         snprintf(name_ptr, name_len, "vector_%s_double_%dbits_fixed", VectorSupport::mathname[op], bits);
2114         addr = StubRoutines::_vector_d_math[exact_log2(bits/64)][op];
2115       }
2116       break;
2117     default:
2118       if (!Matcher::supports_scalable_vector() || !Matcher::vector_size_supported(bt, bits/type2aelembytes(bt)) ) {
2119         snprintf(name_ptr, name_len, "invalid");
2120         addr = nullptr;
2121         Unimplemented();
2122       }
2123       break;
2124   }
2125 
2126   if (addr == nullptr && Matcher::supports_scalable_vector()) {
2127     if (bt == T_FLOAT) {
2128       snprintf(name_ptr, name_len, "vector_%s_float_%dbits_scalable", VectorSupport::mathname[op], bits);
2129       addr = StubRoutines::_vector_f_math[VectorSupport::VEC_SIZE_SCALABLE][op];
2130     } else {
2131       assert(bt == T_DOUBLE, "must be FP type only");
2132       snprintf(name_ptr, name_len, "vector_%s_double_%dbits_scalable", VectorSupport::mathname[op], bits);
2133       addr = StubRoutines::_vector_d_math[VectorSupport::VEC_SIZE_SCALABLE][op];
2134     }
2135   }
2136 
2137   return addr;
2138 }
2139 
2140 //    public static
2141 //    <V extends Vector<E>,
2142 //     M  extends VectorMask<E>,
2143 //     E>
2144 //    V selectFromOp(Class<? extends V> vClass, Class<M> mClass, Class<E> eClass,
2145 //                   int length, V v1, V v2, M m,
2146 //                   VectorSelectFromOp<V, M> defaultImpl)
2147 bool LibraryCallKit::inline_vector_select_from() {
2148   const TypeInstPtr* vector_klass  = gvn().type(argument(0))->isa_instptr();
2149   const TypeInstPtr* mask_klass    = gvn().type(argument(1))->isa_instptr();
2150   const TypeInstPtr* elem_klass    = gvn().type(argument(2))->isa_instptr();
2151   const TypeInt*     vlen          = gvn().type(argument(3))->isa_int();
2152 
2153   if (vector_klass == nullptr  || elem_klass == nullptr || vlen == nullptr ||
2154       vector_klass->const_oop()  == nullptr ||
2155       elem_klass->const_oop()    == nullptr ||
2156       !vlen->is_con()) {
2157     log_if_needed("  ** missing constant: vclass=%s etype=%s vlen=%s",
2158                     NodeClassNames[argument(0)->Opcode()],
2159                     NodeClassNames[argument(2)->Opcode()],
2160                     NodeClassNames[argument(3)->Opcode()]);
2161     return false; // not enough info for intrinsification
2162   }
2163   if (!is_klass_initialized(vector_klass)) {
2164     log_if_needed("  ** klass argument not initialized");
2165     return false;
2166   }
2167   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
2168   if (!elem_type->is_primitive_type()) {
2169     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
2170     return false; // should be primitive type
2171   }
2172   BasicType elem_bt = elem_type->basic_type();
2173   int num_elem = vlen->get_con();
2174   if (!is_power_of_2(num_elem)) {
2175     log_if_needed("  ** vlen not power of two=%d", num_elem);
2176     return false;
2177   }
2178 
2179   int cast_vopc = VectorCastNode::opcode(-1, elem_bt); // from vector of type elem_bt
2180   if (!arch_supports_vector(Op_VectorLoadShuffle, num_elem, elem_bt, VecMaskNotUsed)||
2181       !arch_supports_vector(Op_AndV, num_elem, T_BYTE, VecMaskNotUsed)              ||
2182       !arch_supports_vector(Op_Replicate, num_elem, T_BYTE, VecMaskNotUsed)         ||
2183       !arch_supports_vector(cast_vopc, num_elem, T_BYTE, VecMaskNotUsed)) {
2184     log_if_needed("  ** not supported: arity=0 op=selectFrom vlen=%d etype=%s ismask=no",
2185                     num_elem, type2name(elem_bt));
2186     return false; // not supported
2187   }
2188 
2189   bool is_masked_op = argument(6)->bottom_type() != TypePtr::NULL_PTR;
2190   bool use_predicate = is_masked_op;
2191   if (is_masked_op &&
2192       (mask_klass == nullptr ||
2193        mask_klass->const_oop() == nullptr ||
2194        !is_klass_initialized(mask_klass))) {
2195     log_if_needed("  ** mask_klass argument not initialized");
2196     return false; // not supported
2197   }
2198   VectorMaskUseType checkFlags = (VectorMaskUseType)(is_masked_op ? (VecMaskUseLoad | VecMaskUsePred) : VecMaskNotUsed);
2199   if (!arch_supports_vector(Op_VectorRearrange, num_elem, elem_bt, checkFlags)) {
2200     use_predicate = false;
2201     if(!is_masked_op ||
2202        (!arch_supports_vector(Op_VectorRearrange, num_elem, elem_bt, VecMaskNotUsed) ||
2203         !arch_supports_vector(Op_VectorBlend, num_elem, elem_bt, VecMaskUseLoad)     ||
2204         !arch_supports_vector(Op_Replicate, num_elem, elem_bt, VecMaskNotUsed))) {
2205       log_if_needed("  ** not supported: op=selectFrom vlen=%d etype=%s is_masked_op=%d",
2206                       num_elem, type2name(elem_bt), is_masked_op);
2207       return false; // not supported
2208     }
2209   }
2210   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
2211   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
2212 
2213   // v1 is the index vector
2214   Node* v1 = unbox_vector(argument(4), vbox_type, elem_bt, num_elem);
2215   // v2 is the vector being rearranged
2216   Node* v2 = unbox_vector(argument(5), vbox_type, elem_bt, num_elem);
2217 
2218   if (v1 == nullptr) {
2219     log_if_needed("  ** unbox failed v1=%s", NodeClassNames[argument(4)->Opcode()]);
2220     return false; // operand unboxing failed
2221   }
2222 
2223   if (v2 == nullptr) {
2224     log_if_needed("  ** unbox failed v2=%s", NodeClassNames[argument(5)->Opcode()]);
2225     return false; // operand unboxing failed
2226   }
2227 
2228   Node* mask = nullptr;
2229   if (is_masked_op) {
2230     ciKlass* mbox_klass = mask_klass->const_oop()->as_instance()->java_lang_Class_klass();
2231     const TypeInstPtr* mbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, mbox_klass);
2232     mask = unbox_vector(argument(6), mbox_type, elem_bt, num_elem);
2233     if (mask == nullptr) {
2234       log_if_needed("  ** unbox failed mask=%s", NodeClassNames[argument(6)->Opcode()]);
2235       return false;
2236     }
2237   }
2238 
2239   // cast index vector from elem_bt vector to byte vector
2240   const Type * byte_bt = Type::get_const_basic_type(T_BYTE);
2241   const TypeVect * byte_vt  = TypeVect::make(byte_bt, num_elem);
2242   Node* byte_shuffle = gvn().transform(VectorCastNode::make(cast_vopc, v1, T_BYTE, num_elem));
2243 
2244   // wrap the byte vector lanes to (num_elem - 1) to form the shuffle vector where num_elem is vector length
2245   // this is a simple AND operation as we come here only for power of two vector length
2246   Node* mod_val = gvn().makecon(TypeInt::make(num_elem-1));
2247   Node* bcast_mod  = gvn().transform(VectorNode::scalar2vector(mod_val, num_elem, byte_bt));
2248   byte_shuffle = gvn().transform(VectorNode::make(Op_AndV, byte_shuffle, bcast_mod, byte_vt));
2249 
2250   // load the shuffle to use in rearrange
2251   const TypeVect * shuffle_vt  = TypeVect::make(elem_bt, num_elem);
2252   Node* load_shuffle = gvn().transform(new VectorLoadShuffleNode(byte_shuffle, shuffle_vt));
2253 
2254   // and finally rearrange
2255   Node* rearrange = new VectorRearrangeNode(v2, load_shuffle);
2256   if (is_masked_op) {
2257     if (use_predicate) {
2258       // masked rearrange is supported so use that directly
2259       rearrange->add_req(mask);
2260       rearrange->add_flag(Node::Flag_is_predicated_vector);
2261     } else {
2262       // masked rearrange is not supported so emulate usig blend
2263       const TypeVect* vt = v1->bottom_type()->is_vect();
2264       rearrange = gvn().transform(rearrange);
2265 
2266       // create a zero vector with each lane element set as zero
2267       Node* zero = gvn().makecon(Type::get_zero_type(elem_bt));
2268       Node* zerovec = gvn().transform(VectorNode::scalar2vector(zero, num_elem, Type::get_const_basic_type(elem_bt)));
2269 
2270       // For each lane for which mask is set, blend in the rearranged lane into zero vector
2271       rearrange = new VectorBlendNode(zerovec, rearrange, mask);
2272     }
2273   }
2274   rearrange = gvn().transform(rearrange);
2275 
2276   // box the result
2277   Node* box = box_vector(rearrange, vbox_type, elem_bt, num_elem);
2278   set_result(box);
2279 
2280   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
2281   return true;
2282 }
2283 
2284 Node* LibraryCallKit::gen_call_to_vector_math(int vector_api_op_id, BasicType bt, int num_elem, Node* opd1, Node* opd2) {
2285   assert(UseVectorStubs, "sanity");
2286   assert(vector_api_op_id >= VectorSupport::VECTOR_OP_MATH_START && vector_api_op_id <= VectorSupport::VECTOR_OP_MATH_END, "need valid op id");
2287   assert(opd1 != nullptr, "must not be null");
2288   const TypeVect* vt = TypeVect::make(bt, num_elem);
2289   const TypeFunc* call_type = OptoRuntime::Math_Vector_Vector_Type(opd2 != nullptr ? 2 : 1, vt, vt);
2290   char name[100] = "";
2291 
2292   // Get address for vector math method.
2293   address addr = get_vector_math_address(vector_api_op_id, vt->length_in_bytes() * BitsPerByte, bt, name, 100);
2294 
2295   if (addr == nullptr) {
2296     return nullptr;
2297   }
2298 
2299   assert(name[0] != '\0', "name must not be null");
2300   Node* operation = make_runtime_call(RC_VECTOR,
2301                                       call_type,
2302                                       addr,
2303                                       name,
2304                                       TypePtr::BOTTOM,
2305                                       opd1,
2306                                       opd2);
2307   return gvn().transform(new ProjNode(gvn().transform(operation), TypeFunc::Parms));
2308 }
2309 
2310 //  public static
2311 //  <V extends Vector<E>,
2312 //   M extends VectorMask<E>,
2313 //   E>
2314 //  V broadcastInt(int opr, Class<? extends V> vectorClass, Class<? extends M> maskClass,
2315 //                 Class<E> elementType, int length,
2316 //                 V v, int n, M m,
2317 //                 VectorBroadcastIntOp<V, M> defaultImpl)
2318 bool LibraryCallKit::inline_vector_broadcast_int() {
2319   const TypeInt*     opr          = gvn().type(argument(0))->isa_int();
2320   const TypeInstPtr* vector_klass = gvn().type(argument(1))->isa_instptr();
2321   const TypeInstPtr* mask_klass   = gvn().type(argument(2))->isa_instptr();
2322   const TypeInstPtr* elem_klass   = gvn().type(argument(3))->isa_instptr();
2323   const TypeInt*     vlen         = gvn().type(argument(4))->isa_int();
2324 
2325   if (opr == nullptr || vector_klass == nullptr || elem_klass == nullptr || vlen == nullptr) {
2326     return false; // dead code
2327   }
2328   if (!opr->is_con() || vector_klass->const_oop() == nullptr || elem_klass->const_oop() == nullptr || !vlen->is_con()) {
2329     log_if_needed("  ** missing constant: opr=%s vclass=%s etype=%s vlen=%s",
2330                     NodeClassNames[argument(0)->Opcode()],
2331                     NodeClassNames[argument(1)->Opcode()],
2332                     NodeClassNames[argument(3)->Opcode()],
2333                     NodeClassNames[argument(4)->Opcode()]);
2334     return false; // not enough info for intrinsification
2335   }
2336   if (!is_klass_initialized(vector_klass)) {
2337     log_if_needed("  ** klass argument not initialized");
2338     return false;
2339   }
2340 
2341   const Type* vmask_type = gvn().type(argument(7));
2342   bool is_masked_op = vmask_type != TypePtr::NULL_PTR;
2343   if (is_masked_op) {
2344     if (mask_klass == nullptr || mask_klass->const_oop() == nullptr) {
2345       log_if_needed("  ** missing constant: maskclass=%s", NodeClassNames[argument(2)->Opcode()]);
2346       return false; // not enough info for intrinsification
2347     }
2348 
2349     if (!is_klass_initialized(mask_klass)) {
2350       log_if_needed("  ** mask klass argument not initialized");
2351       return false;
2352     }
2353 
2354     if (vmask_type->maybe_null()) {
2355       log_if_needed("  ** null mask values are not allowed for masked op");
2356       return false;
2357     }
2358   }
2359 
2360   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
2361   if (!elem_type->is_primitive_type()) {
2362     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
2363     return false; // should be primitive type
2364   }
2365 
2366   int num_elem = vlen->get_con();
2367   BasicType elem_bt = elem_type->basic_type();
2368   int opc = VectorSupport::vop2ideal(opr->get_con(), elem_bt);
2369 
2370   bool is_shift  = VectorNode::is_shift_opcode(opc);
2371   bool is_rotate = VectorNode::is_rotate_opcode(opc);
2372 
2373   if (opc == 0 || (!is_shift && !is_rotate)) {
2374     log_if_needed("  ** operation not supported: op=%d bt=%s", opr->get_con(), type2name(elem_bt));
2375     return false; // operation not supported
2376   }
2377 
2378   int sopc = VectorNode::opcode(opc, elem_bt);
2379   if (sopc == 0) {
2380     log_if_needed("  ** operation not supported: opc=%s bt=%s", NodeClassNames[opc], type2name(elem_bt));
2381     return false; // operation not supported
2382   }
2383 
2384   Node* cnt  = argument(6);
2385   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
2386   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
2387   const TypeInt* cnt_type = cnt->bottom_type()->isa_int();
2388 
2389   // If CPU supports vector constant rotate instructions pass it directly
2390   bool is_const_rotate = is_rotate && cnt_type && cnt_type->is_con() &&
2391                          Matcher::supports_vector_constant_rotates(cnt_type->get_con());
2392   bool has_scalar_args = is_rotate ? !is_const_rotate : true;
2393 
2394   VectorMaskUseType checkFlags = (VectorMaskUseType)(is_masked_op ? (VecMaskUseLoad | VecMaskUsePred) : VecMaskNotUsed);
2395   bool use_predicate = is_masked_op;
2396 
2397   if (!arch_supports_vector(sopc, num_elem, elem_bt, checkFlags, has_scalar_args)) {
2398     use_predicate = false;
2399     if (!is_masked_op ||
2400         (!arch_supports_vector(sopc, num_elem, elem_bt, VecMaskNotUsed, has_scalar_args) ||
2401          !arch_supports_vector(Op_VectorBlend, num_elem, elem_bt, VecMaskUseLoad))) {
2402 
2403       log_if_needed("  ** not supported: arity=0 op=int/%d vlen=%d etype=%s is_masked_op=%d",
2404                       sopc, num_elem, type2name(elem_bt), is_masked_op ? 1 : 0);
2405       return false; // not supported
2406     }
2407   }
2408 
2409   Node* opd1 = unbox_vector(argument(5), vbox_type, elem_bt, num_elem);
2410   Node* opd2 = nullptr;
2411   if (is_shift) {
2412     opd2 = vector_shift_count(cnt, opc, elem_bt, num_elem);
2413   } else {
2414     assert(is_rotate, "unexpected operation");
2415     if (!is_const_rotate) {
2416       const Type * type_bt = Type::get_const_basic_type(elem_bt);
2417       cnt = elem_bt == T_LONG ? gvn().transform(new ConvI2LNode(cnt)) : cnt;
2418       opd2 = gvn().transform(VectorNode::scalar2vector(cnt, num_elem, type_bt));
2419     } else {
2420       // Constant shift value.
2421       opd2 = cnt;
2422     }
2423   }
2424 
2425   if (opd1 == nullptr || opd2 == nullptr) {
2426     return false;
2427   }
2428 
2429   Node* mask = nullptr;
2430   if (is_masked_op) {
2431     ciKlass* mbox_klass = mask_klass->const_oop()->as_instance()->java_lang_Class_klass();
2432     const TypeInstPtr* mbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, mbox_klass);
2433     mask = unbox_vector(argument(7), mbox_type, elem_bt, num_elem);
2434     if (mask == nullptr) {
2435       log_if_needed("  ** unbox failed mask=%s", NodeClassNames[argument(7)->Opcode()]);
2436       return false;
2437     }
2438   }
2439 
2440   Node* operation = VectorNode::make(opc, opd1, opd2, num_elem, elem_bt);
2441   if (is_masked_op && mask != nullptr) {
2442     if (use_predicate) {
2443       operation->add_req(mask);
2444       operation->add_flag(Node::Flag_is_predicated_vector);
2445     } else {
2446       operation = gvn().transform(operation);
2447       operation = new VectorBlendNode(opd1, operation, mask);
2448     }
2449   }
2450   operation = gvn().transform(operation);
2451   Node* vbox = box_vector(operation, vbox_type, elem_bt, num_elem);
2452   set_result(vbox);
2453   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
2454   return true;
2455 }
2456 
2457 // public static <VOUT extends VectorPayload,
2458 //                 VIN extends VectorPayload,
2459 //                   S extends VectorSpecies>
2460 // VOUT convert(int oprId,
2461 //           Class<?> fromVectorClass, Class<?> fromElementType, int fromVLen,
2462 //           Class<?>   toVectorClass, Class<?>   toElementType, int   toVLen,
2463 //           VIN v, S s,
2464 //           VectorConvertOp<VOUT, VIN, S> defaultImpl)
2465 //
2466 bool LibraryCallKit::inline_vector_convert() {
2467   const TypeInt*     opr               = gvn().type(argument(0))->isa_int();
2468 
2469   const TypeInstPtr* vector_klass_from = gvn().type(argument(1))->isa_instptr();
2470   const TypeInstPtr* elem_klass_from   = gvn().type(argument(2))->isa_instptr();
2471   const TypeInt*     vlen_from         = gvn().type(argument(3))->isa_int();
2472 
2473   const TypeInstPtr* vector_klass_to   = gvn().type(argument(4))->isa_instptr();
2474   const TypeInstPtr* elem_klass_to     = gvn().type(argument(5))->isa_instptr();
2475   const TypeInt*     vlen_to           = gvn().type(argument(6))->isa_int();
2476 
2477   if (opr == nullptr ||
2478       vector_klass_from == nullptr || elem_klass_from == nullptr || vlen_from == nullptr ||
2479       vector_klass_to   == nullptr || elem_klass_to   == nullptr || vlen_to   == nullptr) {
2480     return false; // dead code
2481   }
2482   if (!opr->is_con() ||
2483       vector_klass_from->const_oop() == nullptr || elem_klass_from->const_oop() == nullptr || !vlen_from->is_con() ||
2484       vector_klass_to->const_oop() == nullptr || elem_klass_to->const_oop() == nullptr || !vlen_to->is_con()) {
2485     log_if_needed("  ** missing constant: opr=%s vclass_from=%s etype_from=%s vlen_from=%s vclass_to=%s etype_to=%s vlen_to=%s",
2486                     NodeClassNames[argument(0)->Opcode()],
2487                     NodeClassNames[argument(1)->Opcode()],
2488                     NodeClassNames[argument(2)->Opcode()],
2489                     NodeClassNames[argument(3)->Opcode()],
2490                     NodeClassNames[argument(4)->Opcode()],
2491                     NodeClassNames[argument(5)->Opcode()],
2492                     NodeClassNames[argument(6)->Opcode()]);
2493     return false; // not enough info for intrinsification
2494   }
2495   if (!is_klass_initialized(vector_klass_from) || !is_klass_initialized(vector_klass_to)) {
2496     log_if_needed("  ** klass argument not initialized");
2497     return false;
2498   }
2499 
2500   assert(opr->get_con() == VectorSupport::VECTOR_OP_CAST  ||
2501          opr->get_con() == VectorSupport::VECTOR_OP_UCAST ||
2502          opr->get_con() == VectorSupport::VECTOR_OP_REINTERPRET, "wrong opcode");
2503   bool is_cast = (opr->get_con() == VectorSupport::VECTOR_OP_CAST || opr->get_con() == VectorSupport::VECTOR_OP_UCAST);
2504   bool is_ucast = (opr->get_con() == VectorSupport::VECTOR_OP_UCAST);
2505 
2506   ciKlass* vbox_klass_from = vector_klass_from->const_oop()->as_instance()->java_lang_Class_klass();
2507   ciKlass* vbox_klass_to = vector_klass_to->const_oop()->as_instance()->java_lang_Class_klass();
2508   if (is_vector_shuffle(vbox_klass_from)) {
2509     return false; // vector shuffles aren't supported
2510   }
2511   bool is_mask = is_vector_mask(vbox_klass_from);
2512 
2513   ciType* elem_type_from = elem_klass_from->const_oop()->as_instance()->java_mirror_type();
2514   if (!elem_type_from->is_primitive_type()) {
2515     return false; // should be primitive type
2516   }
2517   BasicType elem_bt_from = elem_type_from->basic_type();
2518   ciType* elem_type_to = elem_klass_to->const_oop()->as_instance()->java_mirror_type();
2519   if (!elem_type_to->is_primitive_type()) {
2520     return false; // should be primitive type
2521   }
2522   BasicType elem_bt_to = elem_type_to->basic_type();
2523 
2524   int num_elem_from = vlen_from->get_con();
2525   int num_elem_to = vlen_to->get_con();
2526 
2527   // Check whether we can unbox to appropriate size. Even with casting, checking for reinterpret is needed
2528   // since we may need to change size.
2529   if (!arch_supports_vector(Op_VectorReinterpret,
2530                             num_elem_from,
2531                             elem_bt_from,
2532                             is_mask ? VecMaskUseAll : VecMaskNotUsed)) {
2533     log_if_needed("  ** not supported: arity=1 op=%s/1 vlen1=%d etype1=%s ismask=%d",
2534                     is_cast ? "cast" : "reinterpret",
2535                     num_elem_from, type2name(elem_bt_from), is_mask);
2536     return false;
2537   }
2538 
2539   // Check whether we can support resizing/reinterpreting to the new size.
2540   if (!arch_supports_vector(Op_VectorReinterpret,
2541                             num_elem_to,
2542                             elem_bt_to,
2543                             is_mask ? VecMaskUseAll : VecMaskNotUsed)) {
2544     log_if_needed("  ** not supported: arity=1 op=%s/2 vlen2=%d etype2=%s ismask=%d",
2545                     is_cast ? "cast" : "reinterpret",
2546                     num_elem_to, type2name(elem_bt_to), is_mask);
2547     return false;
2548   }
2549 
2550 
2551   if (is_vector_shuffle(vbox_klass_to) &&
2552       (!arch_supports_vector(Op_SubVB, num_elem_to, elem_bt_to, VecMaskNotUsed)           ||
2553        !arch_supports_vector(Op_VectorBlend, num_elem_to, elem_bt_to, VecMaskNotUsed)     ||
2554        !arch_supports_vector(Op_VectorMaskCmp, num_elem_to, elem_bt_to, VecMaskNotUsed)   ||
2555        !arch_supports_vector(Op_AndV, num_elem_to, elem_bt_to, VecMaskNotUsed)            ||
2556        !arch_supports_vector(Op_Replicate, num_elem_to, elem_bt_to, VecMaskNotUsed))) {
2557     log_if_needed("  ** not supported: arity=1 op=shuffle_index_wrap vlen2=%d etype2=%s",
2558                     num_elem_to, type2name(elem_bt_to));
2559     return false;
2560   }
2561 
2562   // At this point, we know that both input and output vector registers are supported
2563   // by the architecture. Next check if the casted type is simply to same type - which means
2564   // that it is actually a resize and not a cast.
2565   if (is_cast && elem_bt_from == elem_bt_to) {
2566     is_cast = false;
2567   }
2568 
2569   const TypeInstPtr* vbox_type_from = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass_from);
2570 
2571   Node* opd1 = unbox_vector(argument(7), vbox_type_from, elem_bt_from, num_elem_from);
2572   if (opd1 == nullptr) {
2573     return false;
2574   }
2575 
2576   const TypeVect* src_type = TypeVect::make(elem_bt_from, num_elem_from, is_mask);
2577   const TypeVect* dst_type = TypeVect::make(elem_bt_to, num_elem_to, is_mask);
2578 
2579   // Safety check to prevent casting if source mask is of type vector
2580   // and destination mask of type predicate vector and vice-versa.
2581   // From X86 standpoint, this case will only arise over KNL target,
2582   // where certain masks (depending on the species) are either propagated
2583   // through a vector or predicate register.
2584   if (is_mask &&
2585       ((src_type->isa_vectmask() == nullptr && dst_type->isa_vectmask()) ||
2586        (dst_type->isa_vectmask() == nullptr && src_type->isa_vectmask()))) {
2587     return false;
2588   }
2589 
2590   Node* op = opd1;
2591   if (is_cast) {
2592     assert(!is_mask || num_elem_from == num_elem_to, "vector mask cast needs the same elem num");
2593     int cast_vopc = VectorCastNode::opcode(-1, elem_bt_from, !is_ucast);
2594 
2595     // Make sure that vector cast is implemented to particular type/size combination if it is
2596     // not a mask casting.
2597     if (!is_mask && !arch_supports_vector(cast_vopc, num_elem_to, elem_bt_to, VecMaskNotUsed)) {
2598       log_if_needed("  ** not supported: arity=1 op=cast#%d/3 vlen2=%d etype2=%s ismask=%d",
2599                       cast_vopc, num_elem_to, type2name(elem_bt_to), is_mask);
2600       return false;
2601     }
2602 
2603     if (num_elem_from < num_elem_to) {
2604       // Since input and output number of elements are not consistent, we need to make sure we
2605       // properly size. Thus, first make a cast that retains the number of elements from source.
2606       int num_elem_for_cast = num_elem_from;
2607 
2608       // It is possible that arch does not support this intermediate vector size
2609       // TODO More complex logic required here to handle this corner case for the sizes.
2610       if (!arch_supports_vector(cast_vopc, num_elem_for_cast, elem_bt_to, VecMaskNotUsed)) {
2611         log_if_needed("  ** not supported: arity=1 op=cast#%d/4 vlen1=%d etype2=%s ismask=%d",
2612                         cast_vopc,
2613                         num_elem_for_cast, type2name(elem_bt_to), is_mask);
2614         return false;
2615       }
2616 
2617       op = gvn().transform(VectorCastNode::make(cast_vopc, op, elem_bt_to, num_elem_for_cast));
2618       // Now ensure that the destination gets properly resized to needed size.
2619       op = gvn().transform(new VectorReinterpretNode(op, op->bottom_type()->is_vect(), dst_type));
2620     } else if (num_elem_from > num_elem_to) {
2621       // Since number of elements from input is larger than output, simply reduce size of input
2622       // (we are supposed to drop top elements anyway).
2623       int num_elem_for_resize = num_elem_to;
2624 
2625       // It is possible that arch does not support this intermediate vector size
2626       // TODO More complex logic required here to handle this corner case for the sizes.
2627       if (!arch_supports_vector(Op_VectorReinterpret,
2628                                 num_elem_for_resize,
2629                                 elem_bt_from,
2630                                 VecMaskNotUsed)) {
2631         log_if_needed("  ** not supported: arity=1 op=cast/5 vlen2=%d etype1=%s ismask=%d",
2632                         num_elem_for_resize, type2name(elem_bt_from), is_mask);
2633         return false;
2634       }
2635 
2636       const TypeVect* resize_type = TypeVect::make(elem_bt_from, num_elem_for_resize);
2637       op = gvn().transform(new VectorReinterpretNode(op, src_type, resize_type));
2638       op = gvn().transform(VectorCastNode::make(cast_vopc, op, elem_bt_to, num_elem_to));
2639     } else { // num_elem_from == num_elem_to
2640       if (is_mask) {
2641         // Make sure that cast for vector mask is implemented to particular type/size combination.
2642         if (!arch_supports_vector(Op_VectorMaskCast, num_elem_to, elem_bt_to, VecMaskNotUsed)) {
2643           log_if_needed("  ** not supported: arity=1 op=maskcast vlen2=%d etype2=%s ismask=%d",
2644                           num_elem_to, type2name(elem_bt_to), is_mask);
2645           return false;
2646         }
2647         op = gvn().transform(new VectorMaskCastNode(op, dst_type));
2648       } else {
2649         // Since input and output number of elements match, and since we know this vector size is
2650         // supported, simply do a cast with no resize needed.
2651         op = gvn().transform(VectorCastNode::make(cast_vopc, op, elem_bt_to, num_elem_to));
2652       }
2653     }
2654   } else if (!Type::equals(src_type, dst_type)) {
2655     assert(!is_cast, "must be reinterpret");
2656     op = gvn().transform(new VectorReinterpretNode(op, src_type, dst_type));
2657   }
2658 
2659   if (is_vector_shuffle(vbox_klass_to)) {
2660      op = partially_wrap_indexes(op, num_elem_to, elem_bt_to);
2661   }
2662 
2663   const TypeInstPtr* vbox_type_to = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass_to);
2664   Node* vbox = box_vector(op, vbox_type_to, elem_bt_to, num_elem_to);
2665   set_result(vbox);
2666   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem_to * type2aelembytes(elem_bt_to))));
2667   return true;
2668 }
2669 
2670 //  public static
2671 //  <V extends Vector<E>,
2672 //   E>
2673 //  V insert(Class<? extends V> vectorClass, Class<E> elementType, int vlen,
2674 //           V vec, int ix, long val,
2675 //           VecInsertOp<V> defaultImpl)
2676 bool LibraryCallKit::inline_vector_insert() {
2677   const TypeInstPtr* vector_klass = gvn().type(argument(0))->isa_instptr();
2678   const TypeInstPtr* elem_klass   = gvn().type(argument(1))->isa_instptr();
2679   const TypeInt*     vlen         = gvn().type(argument(2))->isa_int();
2680   const TypeInt*     idx          = gvn().type(argument(4))->isa_int();
2681 
2682   if (vector_klass == nullptr || elem_klass == nullptr || vlen == nullptr || idx == nullptr) {
2683     return false; // dead code
2684   }
2685   if (vector_klass->const_oop() == nullptr || elem_klass->const_oop() == nullptr || !vlen->is_con() || !idx->is_con()) {
2686     log_if_needed("  ** missing constant: vclass=%s etype=%s vlen=%s idx=%s",
2687                     NodeClassNames[argument(0)->Opcode()],
2688                     NodeClassNames[argument(1)->Opcode()],
2689                     NodeClassNames[argument(2)->Opcode()],
2690                     NodeClassNames[argument(4)->Opcode()]);
2691     return false; // not enough info for intrinsification
2692   }
2693   if (!is_klass_initialized(vector_klass)) {
2694     log_if_needed("  ** klass argument not initialized");
2695     return false;
2696   }
2697   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
2698   if (!elem_type->is_primitive_type()) {
2699     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
2700     return false; // should be primitive type
2701   }
2702   BasicType elem_bt = elem_type->basic_type();
2703   int num_elem = vlen->get_con();
2704   if (!arch_supports_vector(Op_VectorInsert, num_elem, elem_bt, VecMaskNotUsed)) {
2705     log_if_needed("  ** not supported: arity=1 op=insert vlen=%d etype=%s ismask=no",
2706                     num_elem, type2name(elem_bt));
2707     return false; // not supported
2708   }
2709 
2710   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
2711   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
2712 
2713   Node* opd = unbox_vector(argument(3), vbox_type, elem_bt, num_elem);
2714   if (opd == nullptr) {
2715     return false;
2716   }
2717 
2718   Node* insert_val = argument(5);
2719   assert(gvn().type(insert_val)->isa_long() != nullptr, "expected to be long");
2720 
2721   // Convert insert value back to its appropriate type.
2722   switch (elem_bt) {
2723     case T_BYTE:
2724       insert_val = gvn().transform(new ConvL2INode(insert_val, TypeInt::BYTE));
2725       break;
2726     case T_SHORT:
2727       insert_val = gvn().transform(new ConvL2INode(insert_val, TypeInt::SHORT));
2728       break;
2729     case T_INT:
2730       insert_val = gvn().transform(new ConvL2INode(insert_val));
2731       break;
2732     case T_FLOAT:
2733       insert_val = gvn().transform(new ConvL2INode(insert_val));
2734       insert_val = gvn().transform(new MoveI2FNode(insert_val));
2735       break;
2736     case T_DOUBLE:
2737       insert_val = gvn().transform(new MoveL2DNode(insert_val));
2738       break;
2739     case T_LONG:
2740       // no conversion needed
2741       break;
2742     default: fatal("%s", type2name(elem_bt)); break;
2743   }
2744 
2745   Node* operation = gvn().transform(VectorInsertNode::make(opd, insert_val, idx->get_con(), gvn()));
2746 
2747   Node* vbox = box_vector(operation, vbox_type, elem_bt, num_elem);
2748   set_result(vbox);
2749   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
2750   return true;
2751 }
2752 
2753 //  public static
2754 //  <VM extends VectorPayload,
2755 //   E>
2756 //  long extract(Class<? extends VM> vClass, Class<E> eClass,
2757 //               int length,
2758 //               VM vm, int i,
2759 //               VecExtractOp<VM> defaultImpl)
2760 bool LibraryCallKit::inline_vector_extract() {
2761   const TypeInstPtr* vector_klass = gvn().type(argument(0))->isa_instptr();
2762   const TypeInstPtr* elem_klass   = gvn().type(argument(1))->isa_instptr();
2763   const TypeInt*     vlen         = gvn().type(argument(2))->isa_int();
2764   const TypeInt*     idx          = gvn().type(argument(4))->isa_int();
2765 
2766   if (vector_klass == nullptr || elem_klass == nullptr || vlen == nullptr || idx == nullptr) {
2767     return false; // dead code
2768   }
2769   if (vector_klass->const_oop() == nullptr || elem_klass->const_oop() == nullptr || !vlen->is_con() || !idx->is_con()) {
2770     log_if_needed("  ** missing constant: vclass=%s etype=%s vlen=%s",
2771                     NodeClassNames[argument(0)->Opcode()],
2772                     NodeClassNames[argument(1)->Opcode()],
2773                     NodeClassNames[argument(2)->Opcode()]);
2774     return false; // not enough info for intrinsification
2775   }
2776   if (!is_klass_initialized(vector_klass)) {
2777     log_if_needed("  ** klass argument not initialized");
2778     return false;
2779   }
2780   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
2781   if (!elem_type->is_primitive_type()) {
2782     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
2783     return false; // should be primitive type
2784   }
2785   BasicType elem_bt = elem_type->basic_type();
2786   int num_elem = vlen->get_con();
2787 
2788   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
2789   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
2790 
2791   Node* opd = nullptr;
2792 
2793   if (is_vector_mask(vbox_klass)) {
2794     // vbox_klass is mask. This is used for VectorMask.laneIsSet(int).
2795 
2796     Node* pos = argument(4); // can be variable
2797     if (arch_supports_vector(Op_ExtractUB, num_elem, elem_bt, VecMaskUseAll)) {
2798       // Transform mask to vector with type of boolean and utilize ExtractUB node.
2799       opd = unbox_vector(argument(3), vbox_type, elem_bt, num_elem);
2800       if (opd == nullptr) {
2801         return false;
2802       }
2803       opd = gvn().transform(VectorStoreMaskNode::make(gvn(), opd, elem_bt, num_elem));
2804       opd = gvn().transform(new ExtractUBNode(opd, pos));
2805       opd = gvn().transform(new ConvI2LNode(opd));
2806     } else if (arch_supports_vector(Op_VectorMaskToLong, num_elem, elem_bt, VecMaskUseLoad)) {
2807       opd = unbox_vector(argument(3), vbox_type, elem_bt, num_elem);
2808       if (opd == nullptr) {
2809         return false;
2810       }
2811       // VectorMaskToLongNode requires the input is either a mask or a vector with BOOLEAN type.
2812       if (opd->bottom_type()->isa_vectmask() == nullptr) {
2813         opd = gvn().transform(VectorStoreMaskNode::make(gvn(), opd, elem_bt, num_elem));
2814       }
2815       // ((toLong() >>> pos) & 1L
2816       opd = gvn().transform(new VectorMaskToLongNode(opd, TypeLong::LONG));
2817       opd = gvn().transform(new URShiftLNode(opd, pos));
2818       opd = gvn().transform(new AndLNode(opd, gvn().makecon(TypeLong::ONE)));
2819     } else {
2820       log_if_needed("  ** Rejected mask extraction because architecture does not support it");
2821       return false; // not supported
2822     }
2823   } else {
2824     // vbox_klass is vector. This is used for Vector.lane(int).
2825     if (!idx->is_con()) {
2826       log_if_needed("  ** missing constant: idx=%s", NodeClassNames[argument(4)->Opcode()]);
2827       return false; // not enough info for intrinsification
2828     }
2829 
2830     int vopc = ExtractNode::opcode(elem_bt);
2831     if (!arch_supports_vector(vopc, num_elem, elem_bt, VecMaskNotUsed)) {
2832       log_if_needed("  ** not supported: arity=1 op=extract vlen=%d etype=%s ismask=no",
2833                       num_elem, type2name(elem_bt));
2834       return false; // not supported
2835     }
2836 
2837     opd = unbox_vector(argument(3), vbox_type, elem_bt, num_elem);
2838     if (opd == nullptr) {
2839       return false;
2840     }
2841     ConINode* idx_con = gvn().intcon(idx->get_con())->as_ConI();
2842 
2843     opd = gvn().transform(ExtractNode::make(opd, idx_con, elem_bt));
2844     switch (elem_bt) {
2845       case T_BYTE:
2846       case T_SHORT:
2847       case T_INT: {
2848         opd = gvn().transform(new ConvI2LNode(opd));
2849         break;
2850       }
2851       case T_FLOAT: {
2852         opd = gvn().transform(new MoveF2INode(opd));
2853         opd = gvn().transform(new ConvI2LNode(opd));
2854         break;
2855       }
2856       case T_DOUBLE: {
2857         opd = gvn().transform(new MoveD2LNode(opd));
2858         break;
2859       }
2860       case T_LONG: {
2861         // no conversion needed
2862         break;
2863       }
2864       default: fatal("%s", type2name(elem_bt));
2865     }
2866   }
2867   set_result(opd);
2868   return true;
2869 }
2870 
2871 // public static
2872 // <V extends Vector<E>,
2873 //  M extends VectorMask<E>,
2874 //  E>
2875 //  V compressExpandOp(int opr,
2876 //                    Class<? extends V> vClass, Class<? extends M> mClass, Class<E> eClass,
2877 //                    int length, V v, M m,
2878 //                    CompressExpandOperation<V, M> defaultImpl)
2879 bool LibraryCallKit::inline_vector_compress_expand() {
2880   const TypeInt*     opr          = gvn().type(argument(0))->isa_int();
2881   const TypeInstPtr* vector_klass = gvn().type(argument(1))->isa_instptr();
2882   const TypeInstPtr* mask_klass   = gvn().type(argument(2))->isa_instptr();
2883   const TypeInstPtr* elem_klass   = gvn().type(argument(3))->isa_instptr();
2884   const TypeInt*     vlen         = gvn().type(argument(4))->isa_int();
2885 
2886   if (opr          == nullptr || !opr->is_con() ||
2887       vector_klass == nullptr || vector_klass->const_oop() == nullptr ||
2888       mask_klass   == nullptr || mask_klass->const_oop()   == nullptr ||
2889       elem_klass   == nullptr || elem_klass->const_oop()   == nullptr ||
2890       vlen         == nullptr || !vlen->is_con()) {
2891     log_if_needed("  ** missing constant: opr=%s vclass=%s mclass=%s etype=%s vlen=%s",
2892                     NodeClassNames[argument(0)->Opcode()],
2893                     NodeClassNames[argument(1)->Opcode()],
2894                     NodeClassNames[argument(2)->Opcode()],
2895                     NodeClassNames[argument(3)->Opcode()],
2896                     NodeClassNames[argument(4)->Opcode()]);
2897     return false; // not enough info for intrinsification
2898   }
2899 
2900   if (!is_klass_initialized(vector_klass) || !is_klass_initialized(mask_klass)) {
2901     log_if_needed("  ** klass argument not initialized");
2902     return false;
2903   }
2904 
2905   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
2906   if (!elem_type->is_primitive_type()) {
2907     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
2908     return false; // should be primitive type
2909   }
2910 
2911   int num_elem = vlen->get_con();
2912   BasicType elem_bt = elem_type->basic_type();
2913   int opc = VectorSupport::vop2ideal(opr->get_con(), elem_bt);
2914 
2915   if (!arch_supports_vector(opc, num_elem, elem_bt, VecMaskUseLoad)) {
2916     log_if_needed("  ** not supported: opc=%d vlen=%d etype=%s ismask=useload",
2917                     opc, num_elem, type2name(elem_bt));
2918     return false; // not supported
2919   }
2920 
2921   Node* opd1 = nullptr;
2922   const TypeInstPtr* vbox_type = nullptr;
2923   if (opc != Op_CompressM) {
2924     ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
2925     vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
2926     opd1 = unbox_vector(argument(5), vbox_type, elem_bt, num_elem);
2927     if (opd1 == nullptr) {
2928       log_if_needed("  ** unbox failed vector=%s",
2929                       NodeClassNames[argument(5)->Opcode()]);
2930       return false;
2931     }
2932   }
2933 
2934   ciKlass* mbox_klass = mask_klass->const_oop()->as_instance()->java_lang_Class_klass();
2935   assert(is_vector_mask(mbox_klass), "argument(6) should be a mask class");
2936   const TypeInstPtr* mbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, mbox_klass);
2937 
2938   Node* mask = unbox_vector(argument(6), mbox_type, elem_bt, num_elem);
2939   if (mask == nullptr) {
2940     log_if_needed("  ** unbox failed mask=%s",
2941                     NodeClassNames[argument(6)->Opcode()]);
2942     return false;
2943   }
2944 
2945   const TypeVect* vt = TypeVect::make(elem_bt, num_elem, opc == Op_CompressM);
2946   Node* operation = gvn().transform(VectorNode::make(opc, opd1, mask, vt));
2947 
2948   // Wrap it up in VectorBox to keep object type information.
2949   const TypeInstPtr* box_type = opc == Op_CompressM ? mbox_type : vbox_type;
2950   Node* vbox = box_vector(operation, box_type, elem_bt, num_elem);
2951   set_result(vbox);
2952   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
2953   return true;
2954 }
2955 
2956 // public static
2957 // <V extends Vector<E>,
2958 //  E,
2959 //  S extends VectorSpecies<E>>
2960 //  V indexVector(Class<? extends V> vClass, Class<E> eClass,
2961 //                int length,
2962 //                V v, int step, S s,
2963 //                IndexOperation<V, S> defaultImpl)
2964 bool LibraryCallKit::inline_index_vector() {
2965   const TypeInstPtr* vector_klass = gvn().type(argument(0))->isa_instptr();
2966   const TypeInstPtr* elem_klass   = gvn().type(argument(1))->isa_instptr();
2967   const TypeInt*     vlen         = gvn().type(argument(2))->isa_int();
2968 
2969   if (vector_klass == nullptr || vector_klass->const_oop() == nullptr ||
2970       elem_klass   == nullptr || elem_klass->const_oop()   == nullptr ||
2971       vlen         == nullptr || !vlen->is_con() ) {
2972     log_if_needed("  ** missing constant: vclass=%s etype=%s vlen=%s",
2973                     NodeClassNames[argument(0)->Opcode()],
2974                     NodeClassNames[argument(1)->Opcode()],
2975                     NodeClassNames[argument(2)->Opcode()]);
2976     return false; // not enough info for intrinsification
2977   }
2978 
2979   if (!is_klass_initialized(vector_klass)) {
2980     log_if_needed("  ** klass argument not initialized");
2981     return false;
2982   }
2983 
2984   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
2985   if (!elem_type->is_primitive_type()) {
2986     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
2987     return false; // should be primitive type
2988   }
2989 
2990   int num_elem = vlen->get_con();
2991   BasicType elem_bt = elem_type->basic_type();
2992 
2993   // Check whether the iota index generation op is supported by the current hardware
2994   if (!arch_supports_vector(Op_VectorLoadConst, num_elem, elem_bt, VecMaskNotUsed)) {
2995     log_if_needed("  ** not supported: vlen=%d etype=%s", num_elem, type2name(elem_bt));
2996     return false; // not supported
2997   }
2998 
2999   int mul_op = VectorSupport::vop2ideal(VectorSupport::VECTOR_OP_MUL, elem_bt);
3000   int vmul_op = VectorNode::opcode(mul_op, elem_bt);
3001   bool needs_mul = true;
3002   Node* scale = argument(4);
3003   const TypeInt* scale_type = gvn().type(scale)->isa_int();
3004   // Multiply is not needed if the scale is a constant "1".
3005   if (scale_type && scale_type->is_con() && scale_type->get_con() == 1) {
3006     needs_mul = false;
3007   } else {
3008     // Check whether the vector multiply op is supported by the current hardware
3009     if (!arch_supports_vector(vmul_op, num_elem, elem_bt, VecMaskNotUsed)) {
3010       log_if_needed("  ** not supported: vlen=%d etype=%s", num_elem, type2name(elem_bt));
3011       return false; // not supported
3012     }
3013 
3014     // Check whether the scalar cast op is supported by the current hardware
3015     if (is_floating_point_type(elem_bt) || elem_bt == T_LONG) {
3016       int cast_op = elem_bt == T_LONG ? Op_ConvI2L :
3017                     elem_bt == T_FLOAT? Op_ConvI2F : Op_ConvI2D;
3018       if (!Matcher::match_rule_supported(cast_op)) {
3019         log_if_needed("  ** Rejected op (%s) because architecture does not support it",
3020                         NodeClassNames[cast_op]);
3021         return false; // not supported
3022       }
3023     }
3024   }
3025 
3026   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
3027   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
3028   Node* opd = unbox_vector(argument(3), vbox_type, elem_bt, num_elem);
3029   if (opd == nullptr) {
3030     log_if_needed("  ** unbox failed vector=%s",
3031                     NodeClassNames[argument(3)->Opcode()]);
3032     return false;
3033   }
3034 
3035   int add_op = VectorSupport::vop2ideal(VectorSupport::VECTOR_OP_ADD, elem_bt);
3036   int vadd_op = VectorNode::opcode(add_op, elem_bt);
3037   bool needs_add = true;
3038   // The addition is not needed if all the element values of "opd" are zero
3039   if (VectorNode::is_all_zeros_vector(opd)) {
3040     needs_add = false;
3041   } else {
3042     // Check whether the vector addition op is supported by the current hardware
3043     if (!arch_supports_vector(vadd_op, num_elem, elem_bt, VecMaskNotUsed)) {
3044       log_if_needed("  ** not supported: vlen=%d etype=%s", num_elem, type2name(elem_bt));
3045       return false; // not supported
3046     }
3047   }
3048 
3049   // Compute the iota indice vector
3050   const TypeVect* vt = TypeVect::make(elem_bt, num_elem);
3051   Node* index = gvn().transform(new VectorLoadConstNode(gvn().makecon(TypeInt::ZERO), vt));
3052 
3053   // Broadcast the "scale" to a vector, and multiply the "scale" with iota indice vector.
3054   if (needs_mul) {
3055     switch (elem_bt) {
3056       case T_BOOLEAN: // fall-through
3057       case T_BYTE:    // fall-through
3058       case T_SHORT:   // fall-through
3059       case T_CHAR:    // fall-through
3060       case T_INT: {
3061         // no conversion needed
3062         break;
3063       }
3064       case T_LONG: {
3065         scale = gvn().transform(new ConvI2LNode(scale));
3066         break;
3067       }
3068       case T_FLOAT: {
3069         scale = gvn().transform(new ConvI2FNode(scale));
3070         break;
3071       }
3072       case T_DOUBLE: {
3073         scale = gvn().transform(new ConvI2DNode(scale));
3074         break;
3075       }
3076       default: fatal("%s", type2name(elem_bt));
3077     }
3078     scale = gvn().transform(VectorNode::scalar2vector(scale, num_elem, Type::get_const_basic_type(elem_bt)));
3079     index = gvn().transform(VectorNode::make(vmul_op, index, scale, vt));
3080   }
3081 
3082   // Add "opd" if addition is needed.
3083   if (needs_add) {
3084     index = gvn().transform(VectorNode::make(vadd_op, opd, index, vt));
3085   }
3086   Node* vbox = box_vector(index, vbox_type, elem_bt, num_elem);
3087   set_result(vbox);
3088   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
3089   return true;
3090 }
3091 
3092 // public static
3093 // <E,
3094 //  M extends VectorMask<E>>
3095 // M indexPartiallyInUpperRange(Class<? extends M> mClass, Class<E> eClass, int length,
3096 //                              long offset, long limit,
3097 //                              IndexPartiallyInUpperRangeOperation<E, M> defaultImpl)
3098 bool LibraryCallKit::inline_index_partially_in_upper_range() {
3099   const TypeInstPtr* mask_klass   = gvn().type(argument(0))->isa_instptr();
3100   const TypeInstPtr* elem_klass   = gvn().type(argument(1))->isa_instptr();
3101   const TypeInt*     vlen         = gvn().type(argument(2))->isa_int();
3102 
3103   if (mask_klass == nullptr || mask_klass->const_oop() == nullptr ||
3104       elem_klass == nullptr || elem_klass->const_oop() == nullptr ||
3105       vlen       == nullptr || !vlen->is_con()) {
3106     log_if_needed("  ** missing constant: mclass=%s etype=%s vlen=%s",
3107                     NodeClassNames[argument(0)->Opcode()],
3108                     NodeClassNames[argument(1)->Opcode()],
3109                     NodeClassNames[argument(2)->Opcode()]);
3110     return false; // not enough info for intrinsification
3111   }
3112 
3113   if (!is_klass_initialized(mask_klass)) {
3114     log_if_needed("  ** klass argument not initialized");
3115     return false;
3116   }
3117 
3118   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
3119   if (!elem_type->is_primitive_type()) {
3120     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
3121     return false; // should be primitive type
3122   }
3123 
3124   int num_elem = vlen->get_con();
3125   BasicType elem_bt = elem_type->basic_type();
3126 
3127   // Check whether the necessary ops are supported by current hardware.
3128   bool supports_mask_gen = arch_supports_vector(Op_VectorMaskGen, num_elem, elem_bt, VecMaskUseStore);
3129   if (!supports_mask_gen) {
3130     if (!arch_supports_vector(Op_VectorLoadConst, num_elem, elem_bt, VecMaskNotUsed) ||
3131         !arch_supports_vector(Op_Replicate, num_elem, elem_bt, VecMaskNotUsed) ||
3132         !arch_supports_vector(Op_VectorMaskCmp, num_elem, elem_bt, VecMaskUseStore)) {
3133       log_if_needed("  ** not supported: vlen=%d etype=%s", num_elem, type2name(elem_bt));
3134       return false; // not supported
3135     }
3136 
3137     // Check whether the scalar cast operation is supported by current hardware.
3138     if (elem_bt != T_LONG) {
3139       int cast_op = is_integral_type(elem_bt) ? Op_ConvL2I
3140                                               : (elem_bt == T_FLOAT ? Op_ConvL2F : Op_ConvL2D);
3141       if (!Matcher::match_rule_supported(cast_op)) {
3142         log_if_needed("  ** Rejected op (%s) because architecture does not support it",
3143                         NodeClassNames[cast_op]);
3144         return false; // not supported
3145       }
3146     }
3147   }
3148 
3149   Node* offset = argument(3);
3150   Node* limit = argument(5);
3151   if (offset == nullptr || limit == nullptr) {
3152     log_if_needed("  ** offset or limit argument is null");
3153     return false; // not supported
3154   }
3155 
3156   ciKlass* box_klass = mask_klass->const_oop()->as_instance()->java_lang_Class_klass();
3157   assert(is_vector_mask(box_klass), "argument(0) should be a mask class");
3158   const TypeInstPtr* box_type = TypeInstPtr::make_exact(TypePtr::NotNull, box_klass);
3159 
3160   // We assume "offset > 0 && limit >= offset && limit - offset < num_elem".
3161   // So directly get indexLimit with "indexLimit = limit - offset".
3162   Node* indexLimit = gvn().transform(new SubLNode(limit, offset));
3163   Node* mask = nullptr;
3164   if (supports_mask_gen) {
3165     mask = gvn().transform(VectorMaskGenNode::make(indexLimit, elem_bt, num_elem));
3166   } else {
3167     // Generate the vector mask based on "mask = iota < indexLimit".
3168     // Broadcast "indexLimit" to a vector.
3169     switch (elem_bt) {
3170       case T_BOOLEAN: // fall-through
3171       case T_BYTE:    // fall-through
3172       case T_SHORT:   // fall-through
3173       case T_CHAR:    // fall-through
3174       case T_INT: {
3175         indexLimit = gvn().transform(new ConvL2INode(indexLimit));
3176         break;
3177       }
3178       case T_DOUBLE: {
3179         indexLimit = gvn().transform(new ConvL2DNode(indexLimit));
3180         break;
3181       }
3182       case T_FLOAT: {
3183         indexLimit = gvn().transform(new ConvL2FNode(indexLimit));
3184         break;
3185       }
3186       case T_LONG: {
3187         // no conversion needed
3188         break;
3189       }
3190       default: fatal("%s", type2name(elem_bt));
3191     }
3192     indexLimit = gvn().transform(VectorNode::scalar2vector(indexLimit, num_elem, Type::get_const_basic_type(elem_bt)));
3193 
3194     // Load the "iota" vector.
3195     const TypeVect* vt = TypeVect::make(elem_bt, num_elem);
3196     Node* iota = gvn().transform(new VectorLoadConstNode(gvn().makecon(TypeInt::ZERO), vt));
3197 
3198     // Compute the vector mask with "mask = iota < indexLimit".
3199     ConINode* pred_node = (ConINode*)gvn().makecon(TypeInt::make(BoolTest::lt));
3200     const TypeVect* vmask_type = TypeVect::makemask(elem_bt, num_elem);
3201     mask = gvn().transform(new VectorMaskCmpNode(BoolTest::lt, iota, indexLimit, pred_node, vmask_type));
3202   }
3203   Node* vbox = box_vector(mask, box_type, elem_bt, num_elem);
3204   set_result(vbox);
3205   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
3206   return true;
3207 }
3208 
3209 #undef non_product_log_if_needed
3210 #undef log_if_needed