commit 6b719f16d9ecd53e9ad34e77e683b10d896e0ea3
Author:     Andreas Chmielewski <a.chmielewski@ibm.com>
AuthorDate: Mon Apr 13 12:07:02 2026 +0200
Commit:     Andrew John Hughes <gnu_andrew@member.fsf.org>
CommitDate: Mon Apr 27 23:25:17 2026 +0100

    8364465: Enhance behavior of some intrinsics
    
    Reviewed-by: andrew
    Backport-of: 304331b052c177359f09ba7d2710666fa6a4a7d1

diff --git openjdk.orig/hotspot/src/cpu/aarch64/vm/aarch64.ad openjdk/hotspot/src/cpu/aarch64/vm/aarch64.ad
index 7341ddf87f..705464f80f 100644
--- openjdk.orig/hotspot/src/cpu/aarch64/vm/aarch64.ad
+++ openjdk/hotspot/src/cpu/aarch64/vm/aarch64.ad
@@ -7511,10 +7511,11 @@ instruct bytes_reverse_unsigned_short(iRegINoSp dst, iRegIorL2I src) %{
   match(Set dst (ReverseBytesUS src));
 
   ins_cost(INSN_COST);
-  format %{ "rev16w  $dst, $src" %}
+  format %{ "rev16w  $dst, $src\t# $dst -> unsigned short" %}
 
   ins_encode %{
     __ rev16w(as_Register($dst$$reg), as_Register($src$$reg));
+    __ narrow_subword_type(as_Register($dst$$reg), T_CHAR);
   %}
 
   ins_pipe(ialu_reg);
diff --git openjdk.orig/hotspot/src/cpu/aarch64/vm/macroAssembler_aarch64.cpp openjdk/hotspot/src/cpu/aarch64/vm/macroAssembler_aarch64.cpp
index 4725e16c49..93c99a8b56 100644
--- openjdk.orig/hotspot/src/cpu/aarch64/vm/macroAssembler_aarch64.cpp
+++ openjdk/hotspot/src/cpu/aarch64/vm/macroAssembler_aarch64.cpp
@@ -1827,6 +1827,17 @@ void MacroAssembler::store_sized_value(Address dst, Register src, size_t size_in
   }
 }
 
+void MacroAssembler::narrow_subword_type(Register reg, BasicType bt) {
+  assert(is_subword_type(bt), "required");
+  switch (bt) {
+  case T_BOOLEAN: andw(reg, reg, 1); break;
+  case T_BYTE:    sxtbw(reg, reg); break;
+  case T_CHAR:    uxthw(reg, reg); break;
+  case T_SHORT:   sxthw(reg, reg); break;
+  default:        ShouldNotReachHere();
+  }
+}
+
 void MacroAssembler::decrementw(Register reg, int value)
 {
   if (value < 0)  { incrementw(reg, -value);      return; }
diff --git openjdk.orig/hotspot/src/cpu/aarch64/vm/macroAssembler_aarch64.hpp openjdk/hotspot/src/cpu/aarch64/vm/macroAssembler_aarch64.hpp
index 593411634b..5135d0b10e 100644
--- openjdk.orig/hotspot/src/cpu/aarch64/vm/macroAssembler_aarch64.hpp
+++ openjdk/hotspot/src/cpu/aarch64/vm/macroAssembler_aarch64.hpp
@@ -28,6 +28,7 @@
 #define CPU_AARCH64_VM_MACROASSEMBLER_AARCH64_HPP
 
 #include "asm/assembler.inline.hpp"
+#include "utilities/globalDefinitions.hpp"
 
 // MacroAssembler extends Assembler by frequently used macros.
 //
@@ -620,6 +621,9 @@ public:
   // Support for sign-extension (hi:lo = extend_sign(lo))
   void extend_sign(Register hi, Register lo);
 
+  // Clean up a subword typed value to the representation in compliance with JVMS §2.3
+  void narrow_subword_type(Register reg, BasicType bt);
+
   // Load and store values by size and signed-ness
   void load_sized_value(Register dst, Address src, size_t size_in_bytes, bool is_signed, Register dst2 = noreg);
   void store_sized_value(Address dst, Register src, size_t size_in_bytes, Register src2 = noreg);
