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