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_svml(opr->get_con(), elem_bt, num_elem, opd1, opd2);
 472     if (operation == nullptr) {
 473       log_if_needed("  ** svml call failed for %s_%s_%d",
 474                          (elem_bt == T_FLOAT)?"float":"double",
 475                          VectorSupport::svmlname[opr->get_con() - VectorSupport::VECTOR_OP_SVML_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_svml_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_SVML_START) && (vop <= VectorSupport::VECTOR_OP_SVML_END), "unexpected");
2079   int op = vop - VectorSupport::VECTOR_OP_SVML_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%d", VectorSupport::svmlname[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%d", VectorSupport::svmlname[op], bits);
2092         addr = StubRoutines::_vector_d_math[exact_log2(bits/64)][op];
2093       }
2094       break;
2095     default:
2096       snprintf(name_ptr, name_len, "invalid");
2097       addr = nullptr;
2098       Unimplemented();
2099       break;
2100   }
2101 
2102   return addr;
2103 }
2104 
2105 //    public static
2106 //    <V extends Vector<E>,
2107 //     M  extends VectorMask<E>,
2108 //     E>
2109 //    V selectFromOp(Class<? extends V> vClass, Class<M> mClass, Class<E> eClass,
2110 //                   int length, V v1, V v2, M m,
2111 //                   VectorSelectFromOp<V, M> defaultImpl)
2112 bool LibraryCallKit::inline_vector_select_from() {
2113   const TypeInstPtr* vector_klass  = gvn().type(argument(0))->isa_instptr();
2114   const TypeInstPtr* mask_klass    = gvn().type(argument(1))->isa_instptr();
2115   const TypeInstPtr* elem_klass    = gvn().type(argument(2))->isa_instptr();
2116   const TypeInt*     vlen          = gvn().type(argument(3))->isa_int();
2117 
2118   if (vector_klass == nullptr  || elem_klass == nullptr || vlen == nullptr ||
2119       vector_klass->const_oop()  == nullptr ||
2120       elem_klass->const_oop()    == nullptr ||
2121       !vlen->is_con()) {
2122     log_if_needed("  ** missing constant: vclass=%s etype=%s vlen=%s",
2123                     NodeClassNames[argument(0)->Opcode()],
2124                     NodeClassNames[argument(2)->Opcode()],
2125                     NodeClassNames[argument(3)->Opcode()]);
2126     return false; // not enough info for intrinsification
2127   }
2128   if (!is_klass_initialized(vector_klass)) {
2129     log_if_needed("  ** klass argument not initialized");
2130     return false;
2131   }
2132   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
2133   if (!elem_type->is_primitive_type()) {
2134     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
2135     return false; // should be primitive type
2136   }
2137   BasicType elem_bt = elem_type->basic_type();
2138   int num_elem = vlen->get_con();
2139   if (!is_power_of_2(num_elem)) {
2140     log_if_needed("  ** vlen not power of two=%d", num_elem);
2141     return false;
2142   }
2143 
2144   int cast_vopc = VectorCastNode::opcode(-1, elem_bt); // from vector of type elem_bt
2145   if (!arch_supports_vector(Op_VectorLoadShuffle, num_elem, elem_bt, VecMaskNotUsed)||
2146       !arch_supports_vector(Op_AndV, num_elem, T_BYTE, VecMaskNotUsed)              ||
2147       !arch_supports_vector(Op_Replicate, num_elem, T_BYTE, VecMaskNotUsed)         ||
2148       !arch_supports_vector(cast_vopc, num_elem, T_BYTE, VecMaskNotUsed)) {
2149     log_if_needed("  ** not supported: arity=0 op=selectFrom vlen=%d etype=%s ismask=no",
2150                     num_elem, type2name(elem_bt));
2151     return false; // not supported
2152   }
2153 
2154   bool is_masked_op = argument(6)->bottom_type() != TypePtr::NULL_PTR;
2155   bool use_predicate = is_masked_op;
2156   if (is_masked_op &&
2157       (mask_klass == nullptr ||
2158        mask_klass->const_oop() == nullptr ||
2159        !is_klass_initialized(mask_klass))) {
2160     log_if_needed("  ** mask_klass argument not initialized");
2161     return false; // not supported
2162   }
2163   VectorMaskUseType checkFlags = (VectorMaskUseType)(is_masked_op ? (VecMaskUseLoad | VecMaskUsePred) : VecMaskNotUsed);
2164   if (!arch_supports_vector(Op_VectorRearrange, num_elem, elem_bt, checkFlags)) {
2165     use_predicate = false;
2166     if(!is_masked_op ||
2167        (!arch_supports_vector(Op_VectorRearrange, num_elem, elem_bt, VecMaskNotUsed) ||
2168         !arch_supports_vector(Op_VectorBlend, num_elem, elem_bt, VecMaskUseLoad)     ||
2169         !arch_supports_vector(Op_Replicate, num_elem, elem_bt, VecMaskNotUsed))) {
2170       log_if_needed("  ** not supported: op=selectFrom vlen=%d etype=%s is_masked_op=%d",
2171                       num_elem, type2name(elem_bt), is_masked_op);
2172       return false; // not supported
2173     }
2174   }
2175   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
2176   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
2177 
2178   // v1 is the index vector
2179   Node* v1 = unbox_vector(argument(4), vbox_type, elem_bt, num_elem);
2180   // v2 is the vector being rearranged
2181   Node* v2 = unbox_vector(argument(5), vbox_type, elem_bt, num_elem);
2182 
2183   if (v1 == nullptr) {
2184     log_if_needed("  ** unbox failed v1=%s", NodeClassNames[argument(4)->Opcode()]);
2185     return false; // operand unboxing failed
2186   }
2187 
2188   if (v2 == nullptr) {
2189     log_if_needed("  ** unbox failed v2=%s", NodeClassNames[argument(5)->Opcode()]);
2190     return false; // operand unboxing failed
2191   }
2192 
2193   Node* mask = nullptr;
2194   if (is_masked_op) {
2195     ciKlass* mbox_klass = mask_klass->const_oop()->as_instance()->java_lang_Class_klass();
2196     const TypeInstPtr* mbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, mbox_klass);
2197     mask = unbox_vector(argument(6), mbox_type, elem_bt, num_elem);
2198     if (mask == nullptr) {
2199       log_if_needed("  ** unbox failed mask=%s", NodeClassNames[argument(6)->Opcode()]);
2200       return false;
2201     }
2202   }
2203 
2204   // cast index vector from elem_bt vector to byte vector
2205   const Type * byte_bt = Type::get_const_basic_type(T_BYTE);
2206   const TypeVect * byte_vt  = TypeVect::make(byte_bt, num_elem);
2207   Node* byte_shuffle = gvn().transform(VectorCastNode::make(cast_vopc, v1, T_BYTE, num_elem));
2208 
2209   // wrap the byte vector lanes to (num_elem - 1) to form the shuffle vector where num_elem is vector length
2210   // this is a simple AND operation as we come here only for power of two vector length
2211   Node* mod_val = gvn().makecon(TypeInt::make(num_elem-1));
2212   Node* bcast_mod  = gvn().transform(VectorNode::scalar2vector(mod_val, num_elem, byte_bt));
2213   byte_shuffle = gvn().transform(VectorNode::make(Op_AndV, byte_shuffle, bcast_mod, byte_vt));
2214 
2215   // load the shuffle to use in rearrange
2216   const TypeVect * shuffle_vt  = TypeVect::make(elem_bt, num_elem);
2217   Node* load_shuffle = gvn().transform(new VectorLoadShuffleNode(byte_shuffle, shuffle_vt));
2218 
2219   // and finally rearrange
2220   Node* rearrange = new VectorRearrangeNode(v2, load_shuffle);
2221   if (is_masked_op) {
2222     if (use_predicate) {
2223       // masked rearrange is supported so use that directly
2224       rearrange->add_req(mask);
2225       rearrange->add_flag(Node::Flag_is_predicated_vector);
2226     } else {
2227       // masked rearrange is not supported so emulate usig blend
2228       const TypeVect* vt = v1->bottom_type()->is_vect();
2229       rearrange = gvn().transform(rearrange);
2230 
2231       // create a zero vector with each lane element set as zero
2232       Node* zero = gvn().makecon(Type::get_zero_type(elem_bt));
2233       Node* zerovec = gvn().transform(VectorNode::scalar2vector(zero, num_elem, Type::get_const_basic_type(elem_bt)));
2234 
2235       // For each lane for which mask is set, blend in the rearranged lane into zero vector
2236       rearrange = new VectorBlendNode(zerovec, rearrange, mask);
2237     }
2238   }
2239   rearrange = gvn().transform(rearrange);
2240 
2241   // box the result
2242   Node* box = box_vector(rearrange, vbox_type, elem_bt, num_elem);
2243   set_result(box);
2244 
2245   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
2246   return true;
2247 }
2248 
2249 Node* LibraryCallKit::gen_call_to_svml(int vector_api_op_id, BasicType bt, int num_elem, Node* opd1, Node* opd2) {
2250   assert(UseVectorStubs, "sanity");
2251   assert(vector_api_op_id >= VectorSupport::VECTOR_OP_SVML_START && vector_api_op_id <= VectorSupport::VECTOR_OP_SVML_END, "need valid op id");
2252   assert(opd1 != nullptr, "must not be null");
2253   const TypeVect* vt = TypeVect::make(bt, num_elem);
2254   const TypeFunc* call_type = OptoRuntime::Math_Vector_Vector_Type(opd2 != nullptr ? 2 : 1, vt, vt);
2255   char name[100] = "";
2256 
2257   // Get address for svml method.
2258   address addr = get_svml_address(vector_api_op_id, vt->length_in_bytes() * BitsPerByte, bt, name, 100);
2259 
2260   if (addr == nullptr) {
2261     return nullptr;
2262   }
2263 
2264   assert(name[0] != '\0', "name must not be null");
2265   Node* operation = make_runtime_call(RC_VECTOR,
2266                                       call_type,
2267                                       addr,
2268                                       name,
2269                                       TypePtr::BOTTOM,
2270                                       opd1,
2271                                       opd2);
2272   return gvn().transform(new ProjNode(gvn().transform(operation), TypeFunc::Parms));
2273 }
2274 
2275 //  public static
2276 //  <V extends Vector<E>,
2277 //   M extends VectorMask<E>,
2278 //   E>
2279 //  V broadcastInt(int opr, Class<? extends V> vectorClass, Class<? extends M> maskClass,
2280 //                 Class<E> elementType, int length,
2281 //                 V v, int n, M m,
2282 //                 VectorBroadcastIntOp<V, M> defaultImpl)
2283 bool LibraryCallKit::inline_vector_broadcast_int() {
2284   const TypeInt*     opr          = gvn().type(argument(0))->isa_int();
2285   const TypeInstPtr* vector_klass = gvn().type(argument(1))->isa_instptr();
2286   const TypeInstPtr* mask_klass   = gvn().type(argument(2))->isa_instptr();
2287   const TypeInstPtr* elem_klass   = gvn().type(argument(3))->isa_instptr();
2288   const TypeInt*     vlen         = gvn().type(argument(4))->isa_int();
2289 
2290   if (opr == nullptr || vector_klass == nullptr || elem_klass == nullptr || vlen == nullptr) {
2291     return false; // dead code
2292   }
2293   if (!opr->is_con() || vector_klass->const_oop() == nullptr || elem_klass->const_oop() == nullptr || !vlen->is_con()) {
2294     log_if_needed("  ** missing constant: opr=%s vclass=%s etype=%s vlen=%s",
2295                     NodeClassNames[argument(0)->Opcode()],
2296                     NodeClassNames[argument(1)->Opcode()],
2297                     NodeClassNames[argument(3)->Opcode()],
2298                     NodeClassNames[argument(4)->Opcode()]);
2299     return false; // not enough info for intrinsification
2300   }
2301   if (!is_klass_initialized(vector_klass)) {
2302     log_if_needed("  ** klass argument not initialized");
2303     return false;
2304   }
2305 
2306   const Type* vmask_type = gvn().type(argument(7));
2307   bool is_masked_op = vmask_type != TypePtr::NULL_PTR;
2308   if (is_masked_op) {
2309     if (mask_klass == nullptr || mask_klass->const_oop() == nullptr) {
2310       log_if_needed("  ** missing constant: maskclass=%s", NodeClassNames[argument(2)->Opcode()]);
2311       return false; // not enough info for intrinsification
2312     }
2313 
2314     if (!is_klass_initialized(mask_klass)) {
2315       log_if_needed("  ** mask klass argument not initialized");
2316       return false;
2317     }
2318 
2319     if (vmask_type->maybe_null()) {
2320       log_if_needed("  ** null mask values are not allowed for masked op");
2321       return false;
2322     }
2323   }
2324 
2325   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
2326   if (!elem_type->is_primitive_type()) {
2327     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
2328     return false; // should be primitive type
2329   }
2330 
2331   int num_elem = vlen->get_con();
2332   BasicType elem_bt = elem_type->basic_type();
2333   int opc = VectorSupport::vop2ideal(opr->get_con(), elem_bt);
2334 
2335   bool is_shift  = VectorNode::is_shift_opcode(opc);
2336   bool is_rotate = VectorNode::is_rotate_opcode(opc);
2337 
2338   if (opc == 0 || (!is_shift && !is_rotate)) {
2339     log_if_needed("  ** operation not supported: op=%d bt=%s", opr->get_con(), type2name(elem_bt));
2340     return false; // operation not supported
2341   }
2342 
2343   int sopc = VectorNode::opcode(opc, elem_bt);
2344   if (sopc == 0) {
2345     log_if_needed("  ** operation not supported: opc=%s bt=%s", NodeClassNames[opc], type2name(elem_bt));
2346     return false; // operation not supported
2347   }
2348 
2349   Node* cnt  = argument(6);
2350   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
2351   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
2352   const TypeInt* cnt_type = cnt->bottom_type()->isa_int();
2353 
2354   // If CPU supports vector constant rotate instructions pass it directly
2355   bool is_const_rotate = is_rotate && cnt_type && cnt_type->is_con() &&
2356                          Matcher::supports_vector_constant_rotates(cnt_type->get_con());
2357   bool has_scalar_args = is_rotate ? !is_const_rotate : true;
2358 
2359   VectorMaskUseType checkFlags = (VectorMaskUseType)(is_masked_op ? (VecMaskUseLoad | VecMaskUsePred) : VecMaskNotUsed);
2360   bool use_predicate = is_masked_op;
2361 
2362   if (!arch_supports_vector(sopc, num_elem, elem_bt, checkFlags, has_scalar_args)) {
2363     use_predicate = false;
2364     if (!is_masked_op ||
2365         (!arch_supports_vector(sopc, num_elem, elem_bt, VecMaskNotUsed, has_scalar_args) ||
2366          !arch_supports_vector(Op_VectorBlend, num_elem, elem_bt, VecMaskUseLoad))) {
2367 
2368       log_if_needed("  ** not supported: arity=0 op=int/%d vlen=%d etype=%s is_masked_op=%d",
2369                       sopc, num_elem, type2name(elem_bt), is_masked_op ? 1 : 0);
2370       return false; // not supported
2371     }
2372   }
2373 
2374   Node* opd1 = unbox_vector(argument(5), vbox_type, elem_bt, num_elem);
2375   Node* opd2 = nullptr;
2376   if (is_shift) {
2377     opd2 = vector_shift_count(cnt, opc, elem_bt, num_elem);
2378   } else {
2379     assert(is_rotate, "unexpected operation");
2380     if (!is_const_rotate) {
2381       const Type * type_bt = Type::get_const_basic_type(elem_bt);
2382       cnt = elem_bt == T_LONG ? gvn().transform(new ConvI2LNode(cnt)) : cnt;
2383       opd2 = gvn().transform(VectorNode::scalar2vector(cnt, num_elem, type_bt));
2384     } else {
2385       // Constant shift value.
2386       opd2 = cnt;
2387     }
2388   }
2389 
2390   if (opd1 == nullptr || opd2 == nullptr) {
2391     return false;
2392   }
2393 
2394   Node* mask = nullptr;
2395   if (is_masked_op) {
2396     ciKlass* mbox_klass = mask_klass->const_oop()->as_instance()->java_lang_Class_klass();
2397     const TypeInstPtr* mbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, mbox_klass);
2398     mask = unbox_vector(argument(7), mbox_type, elem_bt, num_elem);
2399     if (mask == nullptr) {
2400       log_if_needed("  ** unbox failed mask=%s", NodeClassNames[argument(7)->Opcode()]);
2401       return false;
2402     }
2403   }
2404 
2405   Node* operation = VectorNode::make(opc, opd1, opd2, num_elem, elem_bt);
2406   if (is_masked_op && mask != nullptr) {
2407     if (use_predicate) {
2408       operation->add_req(mask);
2409       operation->add_flag(Node::Flag_is_predicated_vector);
2410     } else {
2411       operation = gvn().transform(operation);
2412       operation = new VectorBlendNode(opd1, operation, mask);
2413     }
2414   }
2415   operation = gvn().transform(operation);
2416   Node* vbox = box_vector(operation, vbox_type, elem_bt, num_elem);
2417   set_result(vbox);
2418   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
2419   return true;
2420 }
2421 
2422 // public static <VOUT extends VectorPayload,
2423 //                 VIN extends VectorPayload,
2424 //                   S extends VectorSpecies>
2425 // VOUT convert(int oprId,
2426 //           Class<?> fromVectorClass, Class<?> fromElementType, int fromVLen,
2427 //           Class<?>   toVectorClass, Class<?>   toElementType, int   toVLen,
2428 //           VIN v, S s,
2429 //           VectorConvertOp<VOUT, VIN, S> defaultImpl)
2430 //
2431 bool LibraryCallKit::inline_vector_convert() {
2432   const TypeInt*     opr               = gvn().type(argument(0))->isa_int();
2433 
2434   const TypeInstPtr* vector_klass_from = gvn().type(argument(1))->isa_instptr();
2435   const TypeInstPtr* elem_klass_from   = gvn().type(argument(2))->isa_instptr();
2436   const TypeInt*     vlen_from         = gvn().type(argument(3))->isa_int();
2437 
2438   const TypeInstPtr* vector_klass_to   = gvn().type(argument(4))->isa_instptr();
2439   const TypeInstPtr* elem_klass_to     = gvn().type(argument(5))->isa_instptr();
2440   const TypeInt*     vlen_to           = gvn().type(argument(6))->isa_int();
2441 
2442   if (opr == nullptr ||
2443       vector_klass_from == nullptr || elem_klass_from == nullptr || vlen_from == nullptr ||
2444       vector_klass_to   == nullptr || elem_klass_to   == nullptr || vlen_to   == nullptr) {
2445     return false; // dead code
2446   }
2447   if (!opr->is_con() ||
2448       vector_klass_from->const_oop() == nullptr || elem_klass_from->const_oop() == nullptr || !vlen_from->is_con() ||
2449       vector_klass_to->const_oop() == nullptr || elem_klass_to->const_oop() == nullptr || !vlen_to->is_con()) {
2450     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",
2451                     NodeClassNames[argument(0)->Opcode()],
2452                     NodeClassNames[argument(1)->Opcode()],
2453                     NodeClassNames[argument(2)->Opcode()],
2454                     NodeClassNames[argument(3)->Opcode()],
2455                     NodeClassNames[argument(4)->Opcode()],
2456                     NodeClassNames[argument(5)->Opcode()],
2457                     NodeClassNames[argument(6)->Opcode()]);
2458     return false; // not enough info for intrinsification
2459   }
2460   if (!is_klass_initialized(vector_klass_from) || !is_klass_initialized(vector_klass_to)) {
2461     log_if_needed("  ** klass argument not initialized");
2462     return false;
2463   }
2464 
2465   assert(opr->get_con() == VectorSupport::VECTOR_OP_CAST  ||
2466          opr->get_con() == VectorSupport::VECTOR_OP_UCAST ||
2467          opr->get_con() == VectorSupport::VECTOR_OP_REINTERPRET, "wrong opcode");
2468   bool is_cast = (opr->get_con() == VectorSupport::VECTOR_OP_CAST || opr->get_con() == VectorSupport::VECTOR_OP_UCAST);
2469   bool is_ucast = (opr->get_con() == VectorSupport::VECTOR_OP_UCAST);
2470 
2471   ciKlass* vbox_klass_from = vector_klass_from->const_oop()->as_instance()->java_lang_Class_klass();
2472   ciKlass* vbox_klass_to = vector_klass_to->const_oop()->as_instance()->java_lang_Class_klass();
2473   if (is_vector_shuffle(vbox_klass_from)) {
2474     return false; // vector shuffles aren't supported
2475   }
2476   bool is_mask = is_vector_mask(vbox_klass_from);
2477 
2478   ciType* elem_type_from = elem_klass_from->const_oop()->as_instance()->java_mirror_type();
2479   if (!elem_type_from->is_primitive_type()) {
2480     return false; // should be primitive type
2481   }
2482   BasicType elem_bt_from = elem_type_from->basic_type();
2483   ciType* elem_type_to = elem_klass_to->const_oop()->as_instance()->java_mirror_type();
2484   if (!elem_type_to->is_primitive_type()) {
2485     return false; // should be primitive type
2486   }
2487   BasicType elem_bt_to = elem_type_to->basic_type();
2488 
2489   int num_elem_from = vlen_from->get_con();
2490   int num_elem_to = vlen_to->get_con();
2491 
2492   // Check whether we can unbox to appropriate size. Even with casting, checking for reinterpret is needed
2493   // since we may need to change size.
2494   if (!arch_supports_vector(Op_VectorReinterpret,
2495                             num_elem_from,
2496                             elem_bt_from,
2497                             is_mask ? VecMaskUseAll : VecMaskNotUsed)) {
2498     log_if_needed("  ** not supported: arity=1 op=%s/1 vlen1=%d etype1=%s ismask=%d",
2499                     is_cast ? "cast" : "reinterpret",
2500                     num_elem_from, type2name(elem_bt_from), is_mask);
2501     return false;
2502   }
2503 
2504   // Check whether we can support resizing/reinterpreting to the new size.
2505   if (!arch_supports_vector(Op_VectorReinterpret,
2506                             num_elem_to,
2507                             elem_bt_to,
2508                             is_mask ? VecMaskUseAll : VecMaskNotUsed)) {
2509     log_if_needed("  ** not supported: arity=1 op=%s/2 vlen2=%d etype2=%s ismask=%d",
2510                     is_cast ? "cast" : "reinterpret",
2511                     num_elem_to, type2name(elem_bt_to), is_mask);
2512     return false;
2513   }
2514 
2515 
2516   if (is_vector_shuffle(vbox_klass_to) &&
2517       (!arch_supports_vector(Op_SubVB, num_elem_to, elem_bt_to, VecMaskNotUsed)           ||
2518        !arch_supports_vector(Op_VectorBlend, num_elem_to, elem_bt_to, VecMaskNotUsed)     ||
2519        !arch_supports_vector(Op_VectorMaskCmp, num_elem_to, elem_bt_to, VecMaskNotUsed)   ||
2520        !arch_supports_vector(Op_AndV, num_elem_to, elem_bt_to, VecMaskNotUsed)            ||
2521        !arch_supports_vector(Op_Replicate, num_elem_to, elem_bt_to, VecMaskNotUsed))) {
2522     log_if_needed("  ** not supported: arity=1 op=shuffle_index_wrap vlen2=%d etype2=%s",
2523                     num_elem_to, type2name(elem_bt_to));
2524     return false;
2525   }
2526 
2527   // At this point, we know that both input and output vector registers are supported
2528   // by the architecture. Next check if the casted type is simply to same type - which means
2529   // that it is actually a resize and not a cast.
2530   if (is_cast && elem_bt_from == elem_bt_to) {
2531     is_cast = false;
2532   }
2533 
2534   const TypeInstPtr* vbox_type_from = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass_from);
2535 
2536   Node* opd1 = unbox_vector(argument(7), vbox_type_from, elem_bt_from, num_elem_from);
2537   if (opd1 == nullptr) {
2538     return false;
2539   }
2540 
2541   const TypeVect* src_type = TypeVect::make(elem_bt_from, num_elem_from, is_mask);
2542   const TypeVect* dst_type = TypeVect::make(elem_bt_to, num_elem_to, is_mask);
2543 
2544   // Safety check to prevent casting if source mask is of type vector
2545   // and destination mask of type predicate vector and vice-versa.
2546   // From X86 standpoint, this case will only arise over KNL target,
2547   // where certain masks (depending on the species) are either propagated
2548   // through a vector or predicate register.
2549   if (is_mask &&
2550       ((src_type->isa_vectmask() == nullptr && dst_type->isa_vectmask()) ||
2551        (dst_type->isa_vectmask() == nullptr && src_type->isa_vectmask()))) {
2552     return false;
2553   }
2554 
2555   Node* op = opd1;
2556   if (is_cast) {
2557     assert(!is_mask || num_elem_from == num_elem_to, "vector mask cast needs the same elem num");
2558     int cast_vopc = VectorCastNode::opcode(-1, elem_bt_from, !is_ucast);
2559 
2560     // Make sure that vector cast is implemented to particular type/size combination if it is
2561     // not a mask casting.
2562     if (!is_mask && !arch_supports_vector(cast_vopc, num_elem_to, elem_bt_to, VecMaskNotUsed)) {
2563       log_if_needed("  ** not supported: arity=1 op=cast#%d/3 vlen2=%d etype2=%s ismask=%d",
2564                       cast_vopc, num_elem_to, type2name(elem_bt_to), is_mask);
2565       return false;
2566     }
2567 
2568     if (num_elem_from < num_elem_to) {
2569       // Since input and output number of elements are not consistent, we need to make sure we
2570       // properly size. Thus, first make a cast that retains the number of elements from source.
2571       int num_elem_for_cast = num_elem_from;
2572 
2573       // It is possible that arch does not support this intermediate vector size
2574       // TODO More complex logic required here to handle this corner case for the sizes.
2575       if (!arch_supports_vector(cast_vopc, num_elem_for_cast, elem_bt_to, VecMaskNotUsed)) {
2576         log_if_needed("  ** not supported: arity=1 op=cast#%d/4 vlen1=%d etype2=%s ismask=%d",
2577                         cast_vopc,
2578                         num_elem_for_cast, type2name(elem_bt_to), is_mask);
2579         return false;
2580       }
2581 
2582       op = gvn().transform(VectorCastNode::make(cast_vopc, op, elem_bt_to, num_elem_for_cast));
2583       // Now ensure that the destination gets properly resized to needed size.
2584       op = gvn().transform(new VectorReinterpretNode(op, op->bottom_type()->is_vect(), dst_type));
2585     } else if (num_elem_from > num_elem_to) {
2586       // Since number of elements from input is larger than output, simply reduce size of input
2587       // (we are supposed to drop top elements anyway).
2588       int num_elem_for_resize = num_elem_to;
2589 
2590       // It is possible that arch does not support this intermediate vector size
2591       // TODO More complex logic required here to handle this corner case for the sizes.
2592       if (!arch_supports_vector(Op_VectorReinterpret,
2593                                 num_elem_for_resize,
2594                                 elem_bt_from,
2595                                 VecMaskNotUsed)) {
2596         log_if_needed("  ** not supported: arity=1 op=cast/5 vlen2=%d etype1=%s ismask=%d",
2597                         num_elem_for_resize, type2name(elem_bt_from), is_mask);
2598         return false;
2599       }
2600 
2601       const TypeVect* resize_type = TypeVect::make(elem_bt_from, num_elem_for_resize);
2602       op = gvn().transform(new VectorReinterpretNode(op, src_type, resize_type));
2603       op = gvn().transform(VectorCastNode::make(cast_vopc, op, elem_bt_to, num_elem_to));
2604     } else { // num_elem_from == num_elem_to
2605       if (is_mask) {
2606         // Make sure that cast for vector mask is implemented to particular type/size combination.
2607         if (!arch_supports_vector(Op_VectorMaskCast, num_elem_to, elem_bt_to, VecMaskNotUsed)) {
2608           log_if_needed("  ** not supported: arity=1 op=maskcast vlen2=%d etype2=%s ismask=%d",
2609                           num_elem_to, type2name(elem_bt_to), is_mask);
2610           return false;
2611         }
2612         op = gvn().transform(new VectorMaskCastNode(op, dst_type));
2613       } else {
2614         // Since input and output number of elements match, and since we know this vector size is
2615         // supported, simply do a cast with no resize needed.
2616         op = gvn().transform(VectorCastNode::make(cast_vopc, op, elem_bt_to, num_elem_to));
2617       }
2618     }
2619   } else if (!Type::equals(src_type, dst_type)) {
2620     assert(!is_cast, "must be reinterpret");
2621     op = gvn().transform(new VectorReinterpretNode(op, src_type, dst_type));
2622   }
2623 
2624   if (is_vector_shuffle(vbox_klass_to)) {
2625      op = partially_wrap_indexes(op, num_elem_to, elem_bt_to);
2626   }
2627 
2628   const TypeInstPtr* vbox_type_to = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass_to);
2629   Node* vbox = box_vector(op, vbox_type_to, elem_bt_to, num_elem_to);
2630   set_result(vbox);
2631   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem_to * type2aelembytes(elem_bt_to))));
2632   return true;
2633 }
2634 
2635 //  public static
2636 //  <V extends Vector<E>,
2637 //   E>
2638 //  V insert(Class<? extends V> vectorClass, Class<E> elementType, int vlen,
2639 //           V vec, int ix, long val,
2640 //           VecInsertOp<V> defaultImpl)
2641 bool LibraryCallKit::inline_vector_insert() {
2642   const TypeInstPtr* vector_klass = gvn().type(argument(0))->isa_instptr();
2643   const TypeInstPtr* elem_klass   = gvn().type(argument(1))->isa_instptr();
2644   const TypeInt*     vlen         = gvn().type(argument(2))->isa_int();
2645   const TypeInt*     idx          = gvn().type(argument(4))->isa_int();
2646 
2647   if (vector_klass == nullptr || elem_klass == nullptr || vlen == nullptr || idx == nullptr) {
2648     return false; // dead code
2649   }
2650   if (vector_klass->const_oop() == nullptr || elem_klass->const_oop() == nullptr || !vlen->is_con() || !idx->is_con()) {
2651     log_if_needed("  ** missing constant: vclass=%s etype=%s vlen=%s idx=%s",
2652                     NodeClassNames[argument(0)->Opcode()],
2653                     NodeClassNames[argument(1)->Opcode()],
2654                     NodeClassNames[argument(2)->Opcode()],
2655                     NodeClassNames[argument(4)->Opcode()]);
2656     return false; // not enough info for intrinsification
2657   }
2658   if (!is_klass_initialized(vector_klass)) {
2659     log_if_needed("  ** klass argument not initialized");
2660     return false;
2661   }
2662   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
2663   if (!elem_type->is_primitive_type()) {
2664     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
2665     return false; // should be primitive type
2666   }
2667   BasicType elem_bt = elem_type->basic_type();
2668   int num_elem = vlen->get_con();
2669   if (!arch_supports_vector(Op_VectorInsert, num_elem, elem_bt, VecMaskNotUsed)) {
2670     log_if_needed("  ** not supported: arity=1 op=insert vlen=%d etype=%s ismask=no",
2671                     num_elem, type2name(elem_bt));
2672     return false; // not supported
2673   }
2674 
2675   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
2676   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
2677 
2678   Node* opd = unbox_vector(argument(3), vbox_type, elem_bt, num_elem);
2679   if (opd == nullptr) {
2680     return false;
2681   }
2682 
2683   Node* insert_val = argument(5);
2684   assert(gvn().type(insert_val)->isa_long() != nullptr, "expected to be long");
2685 
2686   // Convert insert value back to its appropriate type.
2687   switch (elem_bt) {
2688     case T_BYTE:
2689       insert_val = gvn().transform(new ConvL2INode(insert_val, TypeInt::BYTE));
2690       break;
2691     case T_SHORT:
2692       insert_val = gvn().transform(new ConvL2INode(insert_val, TypeInt::SHORT));
2693       break;
2694     case T_INT:
2695       insert_val = gvn().transform(new ConvL2INode(insert_val));
2696       break;
2697     case T_FLOAT:
2698       insert_val = gvn().transform(new ConvL2INode(insert_val));
2699       insert_val = gvn().transform(new MoveI2FNode(insert_val));
2700       break;
2701     case T_DOUBLE:
2702       insert_val = gvn().transform(new MoveL2DNode(insert_val));
2703       break;
2704     case T_LONG:
2705       // no conversion needed
2706       break;
2707     default: fatal("%s", type2name(elem_bt)); break;
2708   }
2709 
2710   Node* operation = gvn().transform(VectorInsertNode::make(opd, insert_val, idx->get_con(), gvn()));
2711 
2712   Node* vbox = box_vector(operation, vbox_type, elem_bt, num_elem);
2713   set_result(vbox);
2714   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
2715   return true;
2716 }
2717 
2718 //  public static
2719 //  <VM extends VectorPayload,
2720 //   E>
2721 //  long extract(Class<? extends VM> vClass, Class<E> eClass,
2722 //               int length,
2723 //               VM vm, int i,
2724 //               VecExtractOp<VM> defaultImpl)
2725 bool LibraryCallKit::inline_vector_extract() {
2726   const TypeInstPtr* vector_klass = gvn().type(argument(0))->isa_instptr();
2727   const TypeInstPtr* elem_klass   = gvn().type(argument(1))->isa_instptr();
2728   const TypeInt*     vlen         = gvn().type(argument(2))->isa_int();
2729   const TypeInt*     idx          = gvn().type(argument(4))->isa_int();
2730 
2731   if (vector_klass == nullptr || elem_klass == nullptr || vlen == nullptr || idx == nullptr) {
2732     return false; // dead code
2733   }
2734   if (vector_klass->const_oop() == nullptr || elem_klass->const_oop() == nullptr || !vlen->is_con()) {
2735     log_if_needed("  ** missing constant: vclass=%s etype=%s vlen=%s",
2736                     NodeClassNames[argument(0)->Opcode()],
2737                     NodeClassNames[argument(1)->Opcode()],
2738                     NodeClassNames[argument(2)->Opcode()]);
2739     return false; // not enough info for intrinsification
2740   }
2741   if (!is_klass_initialized(vector_klass)) {
2742     log_if_needed("  ** klass argument not initialized");
2743     return false;
2744   }
2745   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
2746   if (!elem_type->is_primitive_type()) {
2747     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
2748     return false; // should be primitive type
2749   }
2750   BasicType elem_bt = elem_type->basic_type();
2751   int num_elem = vlen->get_con();
2752 
2753   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
2754   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
2755 
2756   Node* opd = nullptr;
2757 
2758   if (is_vector_mask(vbox_klass)) {
2759     // vbox_klass is mask. This is used for VectorMask.laneIsSet(int).
2760 
2761     Node* pos = argument(4); // can be variable
2762     if (arch_supports_vector(Op_ExtractUB, num_elem, elem_bt, VecMaskUseAll)) {
2763       // Transform mask to vector with type of boolean and utilize ExtractUB node.
2764       opd = unbox_vector(argument(3), vbox_type, elem_bt, num_elem);
2765       if (opd == nullptr) {
2766         return false;
2767       }
2768       opd = gvn().transform(VectorStoreMaskNode::make(gvn(), opd, elem_bt, num_elem));
2769       opd = gvn().transform(new ExtractUBNode(opd, pos));
2770       opd = gvn().transform(new ConvI2LNode(opd));
2771     } else if (arch_supports_vector(Op_VectorMaskToLong, num_elem, elem_bt, VecMaskUseLoad)) {
2772       opd = unbox_vector(argument(3), vbox_type, elem_bt, num_elem);
2773       if (opd == nullptr) {
2774         return false;
2775       }
2776       // VectorMaskToLongNode requires the input is either a mask or a vector with BOOLEAN type.
2777       if (opd->bottom_type()->isa_vectmask() == nullptr) {
2778         opd = gvn().transform(VectorStoreMaskNode::make(gvn(), opd, elem_bt, num_elem));
2779       }
2780       // ((toLong() >>> pos) & 1L
2781       opd = gvn().transform(new VectorMaskToLongNode(opd, TypeLong::LONG));
2782       opd = gvn().transform(new URShiftLNode(opd, pos));
2783       opd = gvn().transform(new AndLNode(opd, gvn().makecon(TypeLong::ONE)));
2784     } else {
2785       log_if_needed("  ** Rejected mask extraction because architecture does not support it");
2786       return false; // not supported
2787     }
2788   } else {
2789     // vbox_klass is vector. This is used for Vector.lane(int).
2790     if (!idx->is_con()) {
2791       log_if_needed("  ** missing constant: idx=%s", NodeClassNames[argument(4)->Opcode()]);
2792       return false; // not enough info for intrinsification
2793     }
2794 
2795     int vopc = ExtractNode::opcode(elem_bt);
2796     if (!arch_supports_vector(vopc, num_elem, elem_bt, VecMaskNotUsed)) {
2797       log_if_needed("  ** not supported: arity=1 op=extract vlen=%d etype=%s ismask=no",
2798                       num_elem, type2name(elem_bt));
2799       return false; // not supported
2800     }
2801 
2802     opd = unbox_vector(argument(3), vbox_type, elem_bt, num_elem);
2803     if (opd == nullptr) {
2804       return false;
2805     }
2806     ConINode* idx_con = gvn().intcon(idx->get_con())->as_ConI();
2807 
2808     opd = gvn().transform(ExtractNode::make(opd, idx_con, elem_bt));
2809     switch (elem_bt) {
2810       case T_BYTE:
2811       case T_SHORT:
2812       case T_INT: {
2813         opd = gvn().transform(new ConvI2LNode(opd));
2814         break;
2815       }
2816       case T_FLOAT: {
2817         opd = gvn().transform(new MoveF2INode(opd));
2818         opd = gvn().transform(new ConvI2LNode(opd));
2819         break;
2820       }
2821       case T_DOUBLE: {
2822         opd = gvn().transform(new MoveD2LNode(opd));
2823         break;
2824       }
2825       case T_LONG: {
2826         // no conversion needed
2827         break;
2828       }
2829       default: fatal("%s", type2name(elem_bt));
2830     }
2831   }
2832   set_result(opd);
2833   return true;
2834 }
2835 
2836 // public static
2837 // <V extends Vector<E>,
2838 //  M extends VectorMask<E>,
2839 //  E>
2840 //  V compressExpandOp(int opr,
2841 //                    Class<? extends V> vClass, Class<? extends M> mClass, Class<E> eClass,
2842 //                    int length, V v, M m,
2843 //                    CompressExpandOperation<V, M> defaultImpl)
2844 bool LibraryCallKit::inline_vector_compress_expand() {
2845   const TypeInt*     opr          = gvn().type(argument(0))->isa_int();
2846   const TypeInstPtr* vector_klass = gvn().type(argument(1))->isa_instptr();
2847   const TypeInstPtr* mask_klass   = gvn().type(argument(2))->isa_instptr();
2848   const TypeInstPtr* elem_klass   = gvn().type(argument(3))->isa_instptr();
2849   const TypeInt*     vlen         = gvn().type(argument(4))->isa_int();
2850 
2851   if (vector_klass == nullptr || elem_klass == nullptr || mask_klass == nullptr || vlen == nullptr ||
2852       vector_klass->const_oop() == nullptr || mask_klass->const_oop() == nullptr ||
2853       elem_klass->const_oop() == nullptr || !vlen->is_con() || !opr->is_con()) {
2854     log_if_needed("  ** missing constant: opr=%s vclass=%s mclass=%s etype=%s vlen=%s",
2855                     NodeClassNames[argument(0)->Opcode()],
2856                     NodeClassNames[argument(1)->Opcode()],
2857                     NodeClassNames[argument(2)->Opcode()],
2858                     NodeClassNames[argument(3)->Opcode()],
2859                     NodeClassNames[argument(4)->Opcode()]);
2860     return false; // not enough info for intrinsification
2861   }
2862 
2863   if (!is_klass_initialized(vector_klass) || !is_klass_initialized(mask_klass)) {
2864     log_if_needed("  ** klass argument not initialized");
2865     return false;
2866   }
2867 
2868   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
2869   if (!elem_type->is_primitive_type()) {
2870     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
2871     return false; // should be primitive type
2872   }
2873 
2874   int num_elem = vlen->get_con();
2875   BasicType elem_bt = elem_type->basic_type();
2876   int opc = VectorSupport::vop2ideal(opr->get_con(), elem_bt);
2877 
2878   if (!arch_supports_vector(opc, num_elem, elem_bt, VecMaskUseLoad)) {
2879     log_if_needed("  ** not supported: opc=%d vlen=%d etype=%s ismask=useload",
2880                     opc, num_elem, type2name(elem_bt));
2881     return false; // not supported
2882   }
2883 
2884   Node* opd1 = nullptr;
2885   const TypeInstPtr* vbox_type = nullptr;
2886   if (opc != Op_CompressM) {
2887     ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
2888     vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
2889     opd1 = unbox_vector(argument(5), vbox_type, elem_bt, num_elem);
2890     if (opd1 == nullptr) {
2891       log_if_needed("  ** unbox failed vector=%s",
2892                       NodeClassNames[argument(5)->Opcode()]);
2893       return false;
2894     }
2895   }
2896 
2897   ciKlass* mbox_klass = mask_klass->const_oop()->as_instance()->java_lang_Class_klass();
2898   assert(is_vector_mask(mbox_klass), "argument(6) should be a mask class");
2899   const TypeInstPtr* mbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, mbox_klass);
2900 
2901   Node* mask = unbox_vector(argument(6), mbox_type, elem_bt, num_elem);
2902   if (mask == nullptr) {
2903     log_if_needed("  ** unbox failed mask=%s",
2904                     NodeClassNames[argument(6)->Opcode()]);
2905     return false;
2906   }
2907 
2908   const TypeVect* vt = TypeVect::make(elem_bt, num_elem, opc == Op_CompressM);
2909   Node* operation = gvn().transform(VectorNode::make(opc, opd1, mask, vt));
2910 
2911   // Wrap it up in VectorBox to keep object type information.
2912   const TypeInstPtr* box_type = opc == Op_CompressM ? mbox_type : vbox_type;
2913   Node* vbox = box_vector(operation, box_type, elem_bt, num_elem);
2914   set_result(vbox);
2915   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
2916   return true;
2917 }
2918 
2919 // public static
2920 // <V extends Vector<E>,
2921 //  E,
2922 //  S extends VectorSpecies<E>>
2923 //  V indexVector(Class<? extends V> vClass, Class<E> eClass,
2924 //                int length,
2925 //                V v, int step, S s,
2926 //                IndexOperation<V, S> defaultImpl)
2927 bool LibraryCallKit::inline_index_vector() {
2928   const TypeInstPtr* vector_klass = gvn().type(argument(0))->isa_instptr();
2929   const TypeInstPtr* elem_klass   = gvn().type(argument(1))->isa_instptr();
2930   const TypeInt*     vlen         = gvn().type(argument(2))->isa_int();
2931 
2932   if (vector_klass == nullptr || elem_klass == nullptr || vlen == nullptr ||
2933       vector_klass->const_oop() == nullptr || !vlen->is_con() ||
2934       elem_klass->const_oop() == nullptr) {
2935     log_if_needed("  ** missing constant: vclass=%s etype=%s vlen=%s",
2936                     NodeClassNames[argument(0)->Opcode()],
2937                     NodeClassNames[argument(1)->Opcode()],
2938                     NodeClassNames[argument(2)->Opcode()]);
2939     return false; // not enough info for intrinsification
2940   }
2941 
2942   if (!is_klass_initialized(vector_klass)) {
2943     log_if_needed("  ** klass argument not initialized");
2944     return false;
2945   }
2946 
2947   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
2948   if (!elem_type->is_primitive_type()) {
2949     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
2950     return false; // should be primitive type
2951   }
2952 
2953   int num_elem = vlen->get_con();
2954   BasicType elem_bt = elem_type->basic_type();
2955 
2956   // Check whether the iota index generation op is supported by the current hardware
2957   if (!arch_supports_vector(Op_VectorLoadConst, num_elem, elem_bt, VecMaskNotUsed)) {
2958     log_if_needed("  ** not supported: vlen=%d etype=%s", num_elem, type2name(elem_bt));
2959     return false; // not supported
2960   }
2961 
2962   int mul_op = VectorSupport::vop2ideal(VectorSupport::VECTOR_OP_MUL, elem_bt);
2963   int vmul_op = VectorNode::opcode(mul_op, elem_bt);
2964   bool needs_mul = true;
2965   Node* scale = argument(4);
2966   const TypeInt* scale_type = gvn().type(scale)->isa_int();
2967   // Multiply is not needed if the scale is a constant "1".
2968   if (scale_type && scale_type->is_con() && scale_type->get_con() == 1) {
2969     needs_mul = false;
2970   } else {
2971     // Check whether the vector multiply op is supported by the current hardware
2972     if (!arch_supports_vector(vmul_op, num_elem, elem_bt, VecMaskNotUsed)) {
2973       log_if_needed("  ** not supported: vlen=%d etype=%s", num_elem, type2name(elem_bt));
2974       return false; // not supported
2975     }
2976 
2977     // Check whether the scalar cast op is supported by the current hardware
2978     if (is_floating_point_type(elem_bt) || elem_bt == T_LONG) {
2979       int cast_op = elem_bt == T_LONG ? Op_ConvI2L :
2980                     elem_bt == T_FLOAT? Op_ConvI2F : Op_ConvI2D;
2981       if (!Matcher::match_rule_supported(cast_op)) {
2982         log_if_needed("  ** Rejected op (%s) because architecture does not support it",
2983                         NodeClassNames[cast_op]);
2984         return false; // not supported
2985       }
2986     }
2987   }
2988 
2989   ciKlass* vbox_klass = vector_klass->const_oop()->as_instance()->java_lang_Class_klass();
2990   const TypeInstPtr* vbox_type = TypeInstPtr::make_exact(TypePtr::NotNull, vbox_klass);
2991   Node* opd = unbox_vector(argument(3), vbox_type, elem_bt, num_elem);
2992   if (opd == nullptr) {
2993     log_if_needed("  ** unbox failed vector=%s",
2994                     NodeClassNames[argument(3)->Opcode()]);
2995     return false;
2996   }
2997 
2998   int add_op = VectorSupport::vop2ideal(VectorSupport::VECTOR_OP_ADD, elem_bt);
2999   int vadd_op = VectorNode::opcode(add_op, elem_bt);
3000   bool needs_add = true;
3001   // The addition is not needed if all the element values of "opd" are zero
3002   if (VectorNode::is_all_zeros_vector(opd)) {
3003     needs_add = false;
3004   } else {
3005     // Check whether the vector addition op is supported by the current hardware
3006     if (!arch_supports_vector(vadd_op, num_elem, elem_bt, VecMaskNotUsed)) {
3007       log_if_needed("  ** not supported: vlen=%d etype=%s", num_elem, type2name(elem_bt));
3008       return false; // not supported
3009     }
3010   }
3011 
3012   // Compute the iota indice vector
3013   const TypeVect* vt = TypeVect::make(elem_bt, num_elem);
3014   Node* index = gvn().transform(new VectorLoadConstNode(gvn().makecon(TypeInt::ZERO), vt));
3015 
3016   // Broadcast the "scale" to a vector, and multiply the "scale" with iota indice vector.
3017   if (needs_mul) {
3018     switch (elem_bt) {
3019       case T_BOOLEAN: // fall-through
3020       case T_BYTE:    // fall-through
3021       case T_SHORT:   // fall-through
3022       case T_CHAR:    // fall-through
3023       case T_INT: {
3024         // no conversion needed
3025         break;
3026       }
3027       case T_LONG: {
3028         scale = gvn().transform(new ConvI2LNode(scale));
3029         break;
3030       }
3031       case T_FLOAT: {
3032         scale = gvn().transform(new ConvI2FNode(scale));
3033         break;
3034       }
3035       case T_DOUBLE: {
3036         scale = gvn().transform(new ConvI2DNode(scale));
3037         break;
3038       }
3039       default: fatal("%s", type2name(elem_bt));
3040     }
3041     scale = gvn().transform(VectorNode::scalar2vector(scale, num_elem, Type::get_const_basic_type(elem_bt)));
3042     index = gvn().transform(VectorNode::make(vmul_op, index, scale, vt));
3043   }
3044 
3045   // Add "opd" if addition is needed.
3046   if (needs_add) {
3047     index = gvn().transform(VectorNode::make(vadd_op, opd, index, vt));
3048   }
3049   Node* vbox = box_vector(index, vbox_type, elem_bt, num_elem);
3050   set_result(vbox);
3051   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
3052   return true;
3053 }
3054 
3055 // public static
3056 // <E,
3057 //  M extends VectorMask<E>>
3058 // M indexPartiallyInUpperRange(Class<? extends M> mClass, Class<E> eClass, int length,
3059 //                              long offset, long limit,
3060 //                              IndexPartiallyInUpperRangeOperation<E, M> defaultImpl)
3061 bool LibraryCallKit::inline_index_partially_in_upper_range() {
3062   const TypeInstPtr* mask_klass   = gvn().type(argument(0))->isa_instptr();
3063   const TypeInstPtr* elem_klass   = gvn().type(argument(1))->isa_instptr();
3064   const TypeInt*     vlen         = gvn().type(argument(2))->isa_int();
3065 
3066   if (mask_klass == nullptr || elem_klass == nullptr || vlen == nullptr ||
3067       mask_klass->const_oop() == nullptr || elem_klass->const_oop() == nullptr || !vlen->is_con()) {
3068     log_if_needed("  ** missing constant: mclass=%s etype=%s vlen=%s",
3069                     NodeClassNames[argument(0)->Opcode()],
3070                     NodeClassNames[argument(1)->Opcode()],
3071                     NodeClassNames[argument(2)->Opcode()]);
3072     return false; // not enough info for intrinsification
3073   }
3074 
3075   if (!is_klass_initialized(mask_klass)) {
3076     log_if_needed("  ** klass argument not initialized");
3077     return false;
3078   }
3079 
3080   ciType* elem_type = elem_klass->const_oop()->as_instance()->java_mirror_type();
3081   if (!elem_type->is_primitive_type()) {
3082     log_if_needed("  ** not a primitive bt=%d", elem_type->basic_type());
3083     return false; // should be primitive type
3084   }
3085 
3086   int num_elem = vlen->get_con();
3087   BasicType elem_bt = elem_type->basic_type();
3088 
3089   // Check whether the necessary ops are supported by current hardware.
3090   bool supports_mask_gen = arch_supports_vector(Op_VectorMaskGen, num_elem, elem_bt, VecMaskUseStore);
3091   if (!supports_mask_gen) {
3092     if (!arch_supports_vector(Op_VectorLoadConst, num_elem, elem_bt, VecMaskNotUsed) ||
3093         !arch_supports_vector(Op_Replicate, num_elem, elem_bt, VecMaskNotUsed) ||
3094         !arch_supports_vector(Op_VectorMaskCmp, num_elem, elem_bt, VecMaskUseStore)) {
3095       log_if_needed("  ** not supported: vlen=%d etype=%s", num_elem, type2name(elem_bt));
3096       return false; // not supported
3097     }
3098 
3099     // Check whether the scalar cast operation is supported by current hardware.
3100     if (elem_bt != T_LONG) {
3101       int cast_op = is_integral_type(elem_bt) ? Op_ConvL2I
3102                                               : (elem_bt == T_FLOAT ? Op_ConvL2F : Op_ConvL2D);
3103       if (!Matcher::match_rule_supported(cast_op)) {
3104         log_if_needed("  ** Rejected op (%s) because architecture does not support it",
3105                         NodeClassNames[cast_op]);
3106         return false; // not supported
3107       }
3108     }
3109   }
3110 
3111   Node* offset = argument(3);
3112   Node* limit = argument(5);
3113   if (offset == nullptr || limit == nullptr) {
3114     log_if_needed("  ** offset or limit argument is null");
3115     return false; // not supported
3116   }
3117 
3118   ciKlass* box_klass = mask_klass->const_oop()->as_instance()->java_lang_Class_klass();
3119   assert(is_vector_mask(box_klass), "argument(0) should be a mask class");
3120   const TypeInstPtr* box_type = TypeInstPtr::make_exact(TypePtr::NotNull, box_klass);
3121 
3122   // We assume "offset > 0 && limit >= offset && limit - offset < num_elem".
3123   // So directly get indexLimit with "indexLimit = limit - offset".
3124   Node* indexLimit = gvn().transform(new SubLNode(limit, offset));
3125   Node* mask = nullptr;
3126   if (supports_mask_gen) {
3127     mask = gvn().transform(VectorMaskGenNode::make(indexLimit, elem_bt, num_elem));
3128   } else {
3129     // Generate the vector mask based on "mask = iota < indexLimit".
3130     // Broadcast "indexLimit" to a vector.
3131     switch (elem_bt) {
3132       case T_BOOLEAN: // fall-through
3133       case T_BYTE:    // fall-through
3134       case T_SHORT:   // fall-through
3135       case T_CHAR:    // fall-through
3136       case T_INT: {
3137         indexLimit = gvn().transform(new ConvL2INode(indexLimit));
3138         break;
3139       }
3140       case T_DOUBLE: {
3141         indexLimit = gvn().transform(new ConvL2DNode(indexLimit));
3142         break;
3143       }
3144       case T_FLOAT: {
3145         indexLimit = gvn().transform(new ConvL2FNode(indexLimit));
3146         break;
3147       }
3148       case T_LONG: {
3149         // no conversion needed
3150         break;
3151       }
3152       default: fatal("%s", type2name(elem_bt));
3153     }
3154     indexLimit = gvn().transform(VectorNode::scalar2vector(indexLimit, num_elem, Type::get_const_basic_type(elem_bt)));
3155 
3156     // Load the "iota" vector.
3157     const TypeVect* vt = TypeVect::make(elem_bt, num_elem);
3158     Node* iota = gvn().transform(new VectorLoadConstNode(gvn().makecon(TypeInt::ZERO), vt));
3159 
3160     // Compute the vector mask with "mask = iota < indexLimit".
3161     ConINode* pred_node = (ConINode*)gvn().makecon(TypeInt::make(BoolTest::lt));
3162     const TypeVect* vmask_type = TypeVect::makemask(elem_bt, num_elem);
3163     mask = gvn().transform(new VectorMaskCmpNode(BoolTest::lt, iota, indexLimit, pred_node, vmask_type));
3164   }
3165   Node* vbox = box_vector(mask, box_type, elem_bt, num_elem);
3166   set_result(vbox);
3167   C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
3168   return true;
3169 }
3170 
3171 #undef non_product_log_if_needed
3172 #undef log_if_needed