From fbe05ec561e8d2a061be126c969c37c219b594f3 Mon Sep 17 00:00:00 2001
From: Vladimir Kempik <vkempik@openjdk.org>
Date: Thu, 17 Mar 2022 18:46:59 +0000
Subject: [PATCH] 8280476: [macOS] : hotspot arm64 bug exposed by latest clang

Backport-of: f5d6fddc6df8c5c5456a2544b131833d5227292b
Bug: https://github.com/termux/termux-packages/issues/12338
Origin: https://github.com/openjdk/jdk17u/commit/fbe05ec561e8d2a061be126c969c37c219b594f3
---
 src/hotspot/cpu/aarch64/immediate_aarch64.cpp | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/hotspot/cpu/aarch64/immediate_aarch64.cpp b/src/hotspot/cpu/aarch64/immediate_aarch64.cpp
index 3e38b7cca04..916494605bc 100644
--- a/src/hotspot/cpu/aarch64/immediate_aarch64.cpp
+++ b/src/hotspot/cpu/aarch64/immediate_aarch64.cpp
@@ -129,8 +129,17 @@ static inline uint32_t uimm(uint32_t val, int hi, int lo)
 
 uint64_t replicate(uint64_t bits, int nbits, int count)
 {
+  assert(count > 0, "must be");
+  assert(nbits > 0, "must be");
+  assert(count * nbits <= 64, "must be");
+
+  // Special case nbits == 64 since the shift below with that nbits value
+  // would result in undefined behavior.
+  if (nbits == 64) {
+    return bits;
+  }
+
   uint64_t result = 0;
-  // nbits may be 64 in which case we want mask to be -1
   uint64_t mask = ones(nbits);
   for (int i = 0; i < count ; i++) {
     result <<= nbits;
