Blame SOURCES/8145913-pr3466-rh1498309.patch

045ef6
# HG changeset patch
045ef6
# User mdoerr
045ef6
# Date 1507750779 -3600
045ef6
#      Wed Oct 11 20:39:39 2017 +0100
045ef6
# Node ID 92f0dbe76a13992cc27188e0f68e4b1771c7004a
045ef6
# Parent  542c122b1d7d30c29189565248074aa28f21ae58
045ef6
8145913, PR3466, RH1498309: PPC64: add Montgomery multiply intrinsic
045ef6
Reviewed-by: aph, goetz
045ef6
045ef6
diff --git a/src/cpu/ppc/vm/assembler_ppc.hpp b/src/cpu/ppc/vm/assembler_ppc.hpp
045ef6
--- openjdk/hotspot/src/cpu/ppc/vm/assembler_ppc.hpp
045ef6
+++ openjdk/hotspot/src/cpu/ppc/vm/assembler_ppc.hpp
3fd527
@@ -1179,6 +1179,8 @@
045ef6
   inline void mullw_( Register d, Register a, Register b);
045ef6
   inline void mulhw(  Register d, Register a, Register b);
045ef6
   inline void mulhw_( Register d, Register a, Register b);
045ef6
+  inline void mulhwu( Register d, Register a, Register b);
045ef6
+  inline void mulhwu_(Register d, Register a, Register b);
045ef6
   inline void mulhd(  Register d, Register a, Register b);
045ef6
   inline void mulhd_( Register d, Register a, Register b);
045ef6
   inline void mulhdu( Register d, Register a, Register b);
045ef6
diff --git a/src/cpu/ppc/vm/assembler_ppc.inline.hpp b/src/cpu/ppc/vm/assembler_ppc.inline.hpp
045ef6
--- openjdk/hotspot/src/cpu/ppc/vm/assembler_ppc.inline.hpp
045ef6
+++ openjdk/hotspot/src/cpu/ppc/vm/assembler_ppc.inline.hpp
045ef6
@@ -109,6 +109,8 @@
045ef6
 inline void Assembler::mullw_( Register d, Register a, Register b) { emit_int32(MULLW_OPCODE  | rt(d) | ra(a) | rb(b) | oe(0) | rc(1)); }
045ef6
 inline void Assembler::mulhw(  Register d, Register a, Register b) { emit_int32(MULHW_OPCODE  | rt(d) | ra(a) | rb(b) | rc(0)); }
045ef6
 inline void Assembler::mulhw_( Register d, Register a, Register b) { emit_int32(MULHW_OPCODE  | rt(d) | ra(a) | rb(b) | rc(1)); }
045ef6
+inline void Assembler::mulhwu( Register d, Register a, Register b) { emit_int32(MULHWU_OPCODE | rt(d) | ra(a) | rb(b) | rc(0)); }
045ef6
+inline void Assembler::mulhwu_(Register d, Register a, Register b) { emit_int32(MULHWU_OPCODE | rt(d) | ra(a) | rb(b) | rc(1)); }
045ef6
 inline void Assembler::mulhd(  Register d, Register a, Register b) { emit_int32(MULHD_OPCODE  | rt(d) | ra(a) | rb(b) | rc(0)); }
045ef6
 inline void Assembler::mulhd_( Register d, Register a, Register b) { emit_int32(MULHD_OPCODE  | rt(d) | ra(a) | rb(b) | rc(1)); }
045ef6
 inline void Assembler::mulhdu( Register d, Register a, Register b) { emit_int32(MULHDU_OPCODE | rt(d) | ra(a) | rb(b) | rc(0)); }
045ef6
diff --git a/src/cpu/ppc/vm/c2_init_ppc.cpp b/src/cpu/ppc/vm/c2_init_ppc.cpp
045ef6
--- openjdk/hotspot/src/cpu/ppc/vm/c2_init_ppc.cpp
045ef6
+++ openjdk/hotspot/src/cpu/ppc/vm/c2_init_ppc.cpp
045ef6
@@ -45,4 +45,10 @@
045ef6
       FLAG_SET_ERGO(bool, InsertEndGroupPPC64, true);
045ef6
     }
045ef6
   }
045ef6
+
045ef6
+  if (OptimizeFill) {
045ef6
+    warning("OptimizeFill is not supported on this CPU.");
045ef6
+    FLAG_SET_DEFAULT(OptimizeFill, false);
045ef6
+  }
045ef6
+
045ef6
 }
045ef6
diff --git a/src/cpu/ppc/vm/sharedRuntime_ppc.cpp b/src/cpu/ppc/vm/sharedRuntime_ppc.cpp
045ef6
--- openjdk/hotspot/src/cpu/ppc/vm/sharedRuntime_ppc.cpp
045ef6
+++ openjdk/hotspot/src/cpu/ppc/vm/sharedRuntime_ppc.cpp
045ef6
@@ -42,6 +42,8 @@
045ef6
 #include "opto/runtime.hpp"
045ef6
 #endif
045ef6
 
045ef6
+#include <alloca.h>
045ef6
+
045ef6
 #define __ masm->
045ef6
 
045ef6
 #ifdef PRODUCT
045ef6
@@ -3269,3 +3271,245 @@
045ef6
   return RuntimeStub::new_runtime_stub(name, &buffer, frame_complete, frame_size_in_bytes/wordSize,
045ef6
                                        oop_maps, true);
045ef6
 }
045ef6
+
045ef6
+
045ef6
+//------------------------------Montgomery multiplication------------------------
045ef6
+//
045ef6
+
045ef6
+// Subtract 0:b from carry:a. Return carry.
045ef6
+static unsigned long
045ef6
+sub(unsigned long a[], unsigned long b[], unsigned long carry, long len) {
045ef6
+  long i = 0;
045ef6
+  unsigned long tmp, tmp2;
045ef6
+  __asm__ __volatile__ (
045ef6
+    "subfc  %[tmp], %[tmp], %[tmp]   \n" // pre-set CA
045ef6
+    "mtctr  %[len]                   \n"
045ef6
+    "0:                              \n"
045ef6
+    "ldx    %[tmp], %[i], %[a]       \n"
045ef6
+    "ldx    %[tmp2], %[i], %[b]      \n"
045ef6
+    "subfe  %[tmp], %[tmp2], %[tmp]  \n" // subtract extended
045ef6
+    "stdx   %[tmp], %[i], %[a]       \n"
045ef6
+    "addi   %[i], %[i], 8            \n"
045ef6
+    "bdnz   0b                       \n"
045ef6
+    "addme  %[tmp], %[carry]         \n" // carry + CA - 1
045ef6
+    : [i]"+b"(i), [tmp]"=&r"(tmp), [tmp2]"=&r"(tmp2)
045ef6
+    : [a]"r"(a), [b]"r"(b), [carry]"r"(carry), [len]"r"(len)
045ef6
+    : "ctr", "xer", "memory"
045ef6
+  );
045ef6
+  return tmp;
045ef6
+}
045ef6
+
045ef6
+// Multiply (unsigned) Long A by Long B, accumulating the double-
045ef6
+// length result into the accumulator formed of T0, T1, and T2.
045ef6
+inline void MACC(unsigned long A, unsigned long B, unsigned long &T0, unsigned long &T1, unsigned long &T2) {
045ef6
+  unsigned long hi, lo;
045ef6
+  __asm__ __volatile__ (
045ef6
+    "mulld  %[lo], %[A], %[B]    \n"
045ef6
+    "mulhdu %[hi], %[A], %[B]    \n"
045ef6
+    "addc   %[T0], %[T0], %[lo]  \n"
045ef6
+    "adde   %[T1], %[T1], %[hi]  \n"
045ef6
+    "addze  %[T2], %[T2]         \n"
045ef6
+    : [hi]"=&r"(hi), [lo]"=&r"(lo), [T0]"+r"(T0), [T1]"+r"(T1), [T2]"+r"(T2)
045ef6
+    : [A]"r"(A), [B]"r"(B)
045ef6
+    : "xer"
045ef6
+  );
045ef6
+}
045ef6
+
045ef6
+// As above, but add twice the double-length result into the
045ef6
+// accumulator.
045ef6
+inline void MACC2(unsigned long A, unsigned long B, unsigned long &T0, unsigned long &T1, unsigned long &T2) {
045ef6
+  unsigned long hi, lo;
045ef6
+  __asm__ __volatile__ (
045ef6
+    "mulld  %[lo], %[A], %[B]    \n"
045ef6
+    "mulhdu %[hi], %[A], %[B]    \n"
045ef6
+    "addc   %[T0], %[T0], %[lo]  \n"
045ef6
+    "adde   %[T1], %[T1], %[hi]  \n"
045ef6
+    "addze  %[T2], %[T2]         \n"
045ef6
+    "addc   %[T0], %[T0], %[lo]  \n"
045ef6
+    "adde   %[T1], %[T1], %[hi]  \n"
045ef6
+    "addze  %[T2], %[T2]         \n"
045ef6
+    : [hi]"=&r"(hi), [lo]"=&r"(lo), [T0]"+r"(T0), [T1]"+r"(T1), [T2]"+r"(T2)
045ef6
+    : [A]"r"(A), [B]"r"(B)
045ef6
+    : "xer"
045ef6
+  );
045ef6
+}
045ef6
+
045ef6
+// Fast Montgomery multiplication. The derivation of the algorithm is
045ef6
+// in "A Cryptographic Library for the Motorola DSP56000,
045ef6
+// Dusse and Kaliski, Proc. EUROCRYPT 90, pp. 230-237".
045ef6
+static void
045ef6
+montgomery_multiply(unsigned long a[], unsigned long b[], unsigned long n[],
045ef6
+                    unsigned long m[], unsigned long inv, int len) {
045ef6
+  unsigned long t0 = 0, t1 = 0, t2 = 0; // Triple-precision accumulator
045ef6
+  int i;
045ef6
+
045ef6
+  assert(inv * n[0] == -1UL, "broken inverse in Montgomery multiply");
045ef6
+
045ef6
+  for (i = 0; i < len; i++) {
045ef6
+    int j;
045ef6
+    for (j = 0; j < i; j++) {
045ef6
+      MACC(a[j], b[i-j], t0, t1, t2);
045ef6
+      MACC(m[j], n[i-j], t0, t1, t2);
045ef6
+    }
045ef6
+    MACC(a[i], b[0], t0, t1, t2);
045ef6
+    m[i] = t0 * inv;
045ef6
+    MACC(m[i], n[0], t0, t1, t2);
045ef6
+
045ef6
+    assert(t0 == 0, "broken Montgomery multiply");
045ef6
+
045ef6
+    t0 = t1; t1 = t2; t2 = 0;
045ef6
+  }
045ef6
+
045ef6
+  for (i = len; i < 2*len; i++) {
045ef6
+    int j;
045ef6
+    for (j = i-len+1; j < len; j++) {
045ef6
+      MACC(a[j], b[i-j], t0, t1, t2);
045ef6
+      MACC(m[j], n[i-j], t0, t1, t2);
045ef6
+    }
045ef6
+    m[i-len] = t0;
045ef6
+    t0 = t1; t1 = t2; t2 = 0;
045ef6
+  }
045ef6
+
045ef6
+  while (t0) {
045ef6
+    t0 = sub(m, n, t0, len);
045ef6
+  }
045ef6
+}
045ef6
+
045ef6
+// Fast Montgomery squaring. This uses asymptotically 25% fewer
045ef6
+// multiplies so it should be up to 25% faster than Montgomery
045ef6
+// multiplication. However, its loop control is more complex and it
045ef6
+// may actually run slower on some machines.
045ef6
+static void
045ef6
+montgomery_square(unsigned long a[], unsigned long n[],
045ef6
+                  unsigned long m[], unsigned long inv, int len) {
045ef6
+  unsigned long t0 = 0, t1 = 0, t2 = 0; // Triple-precision accumulator
045ef6
+  int i;
045ef6
+
045ef6
+  assert(inv * n[0] == -1UL, "broken inverse in Montgomery multiply");
045ef6
+
045ef6
+  for (i = 0; i < len; i++) {
045ef6
+    int j;
045ef6
+    int end = (i+1)/2;
045ef6
+    for (j = 0; j < end; j++) {
045ef6
+      MACC2(a[j], a[i-j], t0, t1, t2);
045ef6
+      MACC(m[j], n[i-j], t0, t1, t2);
045ef6
+    }
045ef6
+    if ((i & 1) == 0) {
045ef6
+      MACC(a[j], a[j], t0, t1, t2);
045ef6
+    }
045ef6
+    for (; j < i; j++) {
045ef6
+      MACC(m[j], n[i-j], t0, t1, t2);
045ef6
+    }
045ef6
+    m[i] = t0 * inv;
045ef6
+    MACC(m[i], n[0], t0, t1, t2);
045ef6
+
045ef6
+    assert(t0 == 0, "broken Montgomery square");
045ef6
+
045ef6
+    t0 = t1; t1 = t2; t2 = 0;
045ef6
+  }
045ef6
+
045ef6
+  for (i = len; i < 2*len; i++) {
045ef6
+    int start = i-len+1;
045ef6
+    int end = start + (len - start)/2;
045ef6
+    int j;
045ef6
+    for (j = start; j < end; j++) {
045ef6
+      MACC2(a[j], a[i-j], t0, t1, t2);
045ef6
+      MACC(m[j], n[i-j], t0, t1, t2);
045ef6
+    }
045ef6
+    if ((i & 1) == 0) {
045ef6
+      MACC(a[j], a[j], t0, t1, t2);
045ef6
+    }
045ef6
+    for (; j < len; j++) {
045ef6
+      MACC(m[j], n[i-j], t0, t1, t2);
045ef6
+    }
045ef6
+    m[i-len] = t0;
045ef6
+    t0 = t1; t1 = t2; t2 = 0;
045ef6
+  }
045ef6
+
045ef6
+  while (t0) {
045ef6
+    t0 = sub(m, n, t0, len);
045ef6
+  }
045ef6
+}
045ef6
+
045ef6
+// The threshold at which squaring is advantageous was determined
045ef6
+// experimentally on an i7-3930K (Ivy Bridge) CPU @ 3.5GHz.
045ef6
+// Doesn't seem to be relevant for Power8 so we use the same value.
045ef6
+#define MONTGOMERY_SQUARING_THRESHOLD 64
045ef6
+
045ef6
+// Copy len longwords from s to d, word-swapping as we go. The
045ef6
+// destination array is reversed.
045ef6
+static void reverse_words(unsigned long *s, unsigned long *d, int len) {
045ef6
+  d += len;
045ef6
+  while(len-- > 0) {
045ef6
+    d--;
045ef6
+    unsigned long s_val = *s;
045ef6
+    // Swap words in a longword on little endian machines.
045ef6
+#ifdef VM_LITTLE_ENDIAN
045ef6
+     s_val = (s_val << 32) | (s_val >> 32);
045ef6
+#endif
045ef6
+    *d = s_val;
045ef6
+    s++;
045ef6
+  }
045ef6
+}
045ef6
+
045ef6
+void SharedRuntime::montgomery_multiply(jint *a_ints, jint *b_ints, jint *n_ints,
045ef6
+                                        jint len, jlong inv,
045ef6
+                                        jint *m_ints) {
045ef6
+  assert(len % 2 == 0, "array length in montgomery_multiply must be even");
045ef6
+  int longwords = len/2;
045ef6
+  assert(longwords > 0, "unsupported");
045ef6
+
045ef6
+  // Make very sure we don't use so much space that the stack might
045ef6
+  // overflow. 512 jints corresponds to an 16384-bit integer and
045ef6
+  // will use here a total of 8k bytes of stack space.
045ef6
+  int total_allocation = longwords * sizeof (unsigned long) * 4;
045ef6
+  guarantee(total_allocation <= 8192, "must be");
045ef6
+  unsigned long *scratch = (unsigned long *)alloca(total_allocation);
045ef6
+
045ef6
+  // Local scratch arrays
045ef6
+  unsigned long
045ef6
+    *a = scratch + 0 * longwords,
045ef6
+    *b = scratch + 1 * longwords,
045ef6
+    *n = scratch + 2 * longwords,
045ef6
+    *m = scratch + 3 * longwords;
045ef6
+
045ef6
+  reverse_words((unsigned long *)a_ints, a, longwords);
045ef6
+  reverse_words((unsigned long *)b_ints, b, longwords);
045ef6
+  reverse_words((unsigned long *)n_ints, n, longwords);
045ef6
+
045ef6
+  ::montgomery_multiply(a, b, n, m, (unsigned long)inv, longwords);
045ef6
+
045ef6
+  reverse_words(m, (unsigned long *)m_ints, longwords);
045ef6
+}
045ef6
+
045ef6
+void SharedRuntime::montgomery_square(jint *a_ints, jint *n_ints,
045ef6
+                                      jint len, jlong inv,
045ef6
+                                      jint *m_ints) {
045ef6
+  assert(len % 2 == 0, "array length in montgomery_square must be even");
045ef6
+  int longwords = len/2;
045ef6
+  assert(longwords > 0, "unsupported");
045ef6
+
045ef6
+  // Make very sure we don't use so much space that the stack might
045ef6
+  // overflow. 512 jints corresponds to an 16384-bit integer and
045ef6
+  // will use here a total of 6k bytes of stack space.
045ef6
+  int total_allocation = longwords * sizeof (unsigned long) * 3;
045ef6
+  guarantee(total_allocation <= 8192, "must be");
045ef6
+  unsigned long *scratch = (unsigned long *)alloca(total_allocation);
045ef6
+
045ef6
+  // Local scratch arrays
045ef6
+  unsigned long
045ef6
+    *a = scratch + 0 * longwords,
045ef6
+    *n = scratch + 1 * longwords,
045ef6
+    *m = scratch + 2 * longwords;
045ef6
+
045ef6
+  reverse_words((unsigned long *)a_ints, a, longwords);
045ef6
+  reverse_words((unsigned long *)n_ints, n, longwords);
045ef6
+
045ef6
+  if (len >= MONTGOMERY_SQUARING_THRESHOLD) {
045ef6
+    ::montgomery_square(a, n, m, (unsigned long)inv, longwords);
045ef6
+  } else {
045ef6
+    ::montgomery_multiply(a, a, n, m, (unsigned long)inv, longwords);
045ef6
+  }
045ef6
+
045ef6
+  reverse_words(m, (unsigned long *)m_ints, longwords);
045ef6
+}
045ef6
diff --git a/src/cpu/ppc/vm/stubGenerator_ppc.cpp b/src/cpu/ppc/vm/stubGenerator_ppc.cpp
045ef6
--- openjdk/hotspot/src/cpu/ppc/vm/stubGenerator_ppc.cpp
045ef6
+++ openjdk/hotspot/src/cpu/ppc/vm/stubGenerator_ppc.cpp
b13a06
@@ -2518,6 +2518,15 @@
045ef6
     generate_safefetch("SafeFetchN", sizeof(intptr_t), &StubRoutines::_safefetchN_entry,
045ef6
                                                        &StubRoutines::_safefetchN_fault_pc,
045ef6
                                                        &StubRoutines::_safefetchN_continuation_pc);
b13a06
+
045ef6
+    if (UseMontgomeryMultiplyIntrinsic) {
045ef6
+      StubRoutines::_montgomeryMultiply
045ef6
+        = CAST_FROM_FN_PTR(address, SharedRuntime::montgomery_multiply);
045ef6
+    }
045ef6
+    if (UseMontgomerySquareIntrinsic) {
045ef6
+      StubRoutines::_montgomerySquare
045ef6
+        = CAST_FROM_FN_PTR(address, SharedRuntime::montgomery_square);
045ef6
+    }
045ef6
 
b13a06
     if (UseAESIntrinsics) {
b13a06
       StubRoutines::_aescrypt_encryptBlock = generate_aescrypt_encryptBlock();
045ef6
diff --git a/src/cpu/ppc/vm/templateInterpreter_ppc.cpp b/src/cpu/ppc/vm/templateInterpreter_ppc.cpp
045ef6
--- openjdk/hotspot/src/cpu/ppc/vm/templateInterpreter_ppc.cpp
045ef6
+++ openjdk/hotspot/src/cpu/ppc/vm/templateInterpreter_ppc.cpp
045ef6
@@ -265,7 +265,7 @@
045ef6
       __ cmpdi(CCR0, Rmdo, 0);
045ef6
       __ beq(CCR0, no_mdo);
045ef6
 
045ef6
-      // Increment backedge counter in the MDO.
045ef6
+      // Increment invocation counter in the MDO.
045ef6
       const int mdo_bc_offs = in_bytes(MethodData::backedge_counter_offset()) + in_bytes(InvocationCounter::counter_offset());
045ef6
       __ lwz(Rscratch2, mdo_bc_offs, Rmdo);
045ef6
       __ addi(Rscratch2, Rscratch2, increment);
045ef6
@@ -277,12 +277,12 @@
045ef6
     }
045ef6
 
045ef6
     // Increment counter in MethodCounters*.
045ef6
-    const int mo_bc_offs = in_bytes(MethodCounters::backedge_counter_offset()) + in_bytes(InvocationCounter::counter_offset());
045ef6
+    const int mo_ic_offs = in_bytes(MethodCounters::invocation_counter_offset()) + in_bytes(InvocationCounter::counter_offset());
045ef6
     __ bind(no_mdo);
045ef6
     __ get_method_counters(R19_method, R3_counters, done);
045ef6
-    __ lwz(Rscratch2, mo_bc_offs, R3_counters);
045ef6
+    __ lwz(Rscratch2, mo_ic_offs, R3_counters);
045ef6
     __ addi(Rscratch2, Rscratch2, increment);
045ef6
-    __ stw(Rscratch2, mo_bc_offs, R3_counters);
045ef6
+    __ stw(Rscratch2, mo_ic_offs, R3_counters);
045ef6
     __ load_const_optimized(Rscratch1, mask, R0);
045ef6
     __ and_(Rscratch1, Rscratch2, Rscratch1);
045ef6
     __ beq(CCR0, *overflow);
045ef6
diff --git a/src/cpu/ppc/vm/vm_version_ppc.cpp b/src/cpu/ppc/vm/vm_version_ppc.cpp
045ef6
--- openjdk/hotspot/src/cpu/ppc/vm/vm_version_ppc.cpp
045ef6
+++ openjdk/hotspot/src/cpu/ppc/vm/vm_version_ppc.cpp
3fd527
@@ -177,6 +177,12 @@
045ef6
     FLAG_SET_DEFAULT(UseSHA512Intrinsics, false);
045ef6
   }
045ef6
 
045ef6
+  if (FLAG_IS_DEFAULT(UseMontgomeryMultiplyIntrinsic)) {
045ef6
+    UseMontgomeryMultiplyIntrinsic = true;
045ef6
+  }
045ef6
+  if (FLAG_IS_DEFAULT(UseMontgomerySquareIntrinsic)) {
045ef6
+    UseMontgomerySquareIntrinsic = true;
045ef6
+  }
045ef6
 }
045ef6
 
045ef6
 void VM_Version::print_features() {
045ef6
diff --git a/src/share/vm/opto/library_call.cpp b/src/share/vm/opto/library_call.cpp
045ef6
--- openjdk/hotspot/src/share/vm/opto/library_call.cpp
045ef6
+++ openjdk/hotspot/src/share/vm/opto/library_call.cpp
3fd527
@@ -6031,11 +6031,21 @@
045ef6
     Node* n_start = array_element_address(n, intcon(0), n_elem);
045ef6
     Node* m_start = array_element_address(m, intcon(0), m_elem);
045ef6
 
045ef6
-    Node* call = make_runtime_call(RC_LEAF,
045ef6
-                                   OptoRuntime::montgomeryMultiply_Type(),
045ef6
-                                   stubAddr, stubName, TypePtr::BOTTOM,
045ef6
-                                   a_start, b_start, n_start, len, inv, top(),
045ef6
-                                   m_start);
045ef6
+    Node* call = NULL;
045ef6
+    if (CCallingConventionRequiresIntsAsLongs) {
045ef6
+      Node* len_I2L = ConvI2L(len);
045ef6
+      call = make_runtime_call(RC_LEAF,
045ef6
+                               OptoRuntime::montgomeryMultiply_Type(),
045ef6
+                               stubAddr, stubName, TypePtr::BOTTOM,
045ef6
+                               a_start, b_start, n_start, len_I2L XTOP, inv,
045ef6
+                               top(), m_start);
045ef6
+    } else {
045ef6
+      call = make_runtime_call(RC_LEAF,
045ef6
+                               OptoRuntime::montgomeryMultiply_Type(),
045ef6
+                               stubAddr, stubName, TypePtr::BOTTOM,
045ef6
+                               a_start, b_start, n_start, len, inv, top(),
045ef6
+                               m_start);
045ef6
+    }
045ef6
     set_result(m);
045ef6
   }
045ef6
 
3fd527
@@ -6085,11 +6095,22 @@
045ef6
     Node* n_start = array_element_address(n, intcon(0), n_elem);
045ef6
     Node* m_start = array_element_address(m, intcon(0), m_elem);
045ef6
 
045ef6
-    Node* call = make_runtime_call(RC_LEAF,
045ef6
-                                   OptoRuntime::montgomerySquare_Type(),
045ef6
-                                   stubAddr, stubName, TypePtr::BOTTOM,
045ef6
-                                   a_start, n_start, len, inv, top(),
045ef6
-                                   m_start);
045ef6
+    Node* call = NULL;
045ef6
+    if (CCallingConventionRequiresIntsAsLongs) {
045ef6
+      Node* len_I2L = ConvI2L(len);
045ef6
+      call = make_runtime_call(RC_LEAF,
045ef6
+                               OptoRuntime::montgomerySquare_Type(),
045ef6
+                               stubAddr, stubName, TypePtr::BOTTOM,
045ef6
+                               a_start, n_start, len_I2L XTOP, inv, top(),
045ef6
+                               m_start);
045ef6
+    } else {
045ef6
+      call = make_runtime_call(RC_LEAF,
045ef6
+                               OptoRuntime::montgomerySquare_Type(),
045ef6
+                               stubAddr, stubName, TypePtr::BOTTOM,
045ef6
+                               a_start, n_start, len, inv, top(),
045ef6
+                               m_start);
045ef6
+    }
045ef6
+
045ef6
     set_result(m);
045ef6
   }
045ef6
 
045ef6
diff --git a/src/share/vm/opto/runtime.cpp b/src/share/vm/opto/runtime.cpp
045ef6
--- openjdk/hotspot/src/share/vm/opto/runtime.cpp
045ef6
+++ openjdk/hotspot/src/share/vm/opto/runtime.cpp
3fd527
@@ -1005,12 +1005,20 @@
045ef6
   // create input type (domain)
045ef6
   int num_args      = 7;
045ef6
   int argcnt = num_args;
045ef6
+  if (CCallingConventionRequiresIntsAsLongs) {
045ef6
+    argcnt++;                           // additional placeholder
045ef6
+  }
045ef6
   const Type** fields = TypeTuple::fields(argcnt);
045ef6
   int argp = TypeFunc::Parms;
045ef6
   fields[argp++] = TypePtr::NOTNULL;    // a
045ef6
   fields[argp++] = TypePtr::NOTNULL;    // b
045ef6
   fields[argp++] = TypePtr::NOTNULL;    // n
045ef6
-  fields[argp++] = TypeInt::INT;        // len
045ef6
+  if (CCallingConventionRequiresIntsAsLongs) {
045ef6
+    fields[argp++] = TypeLong::LONG;    // len
045ef6
+    fields[argp++] = TypeLong::HALF;    // placeholder
045ef6
+  } else {
045ef6
+    fields[argp++] = TypeInt::INT;      // len
045ef6
+  }
045ef6
   fields[argp++] = TypeLong::LONG;      // inv
045ef6
   fields[argp++] = Type::HALF;
045ef6
   fields[argp++] = TypePtr::NOTNULL;    // result
3fd527
@@ -1029,11 +1037,19 @@
045ef6
   // create input type (domain)
045ef6
   int num_args      = 6;
045ef6
   int argcnt = num_args;
045ef6
+  if (CCallingConventionRequiresIntsAsLongs) {
045ef6
+    argcnt++;                           // additional placeholder
045ef6
+  }
045ef6
   const Type** fields = TypeTuple::fields(argcnt);
045ef6
   int argp = TypeFunc::Parms;
045ef6
   fields[argp++] = TypePtr::NOTNULL;    // a
045ef6
   fields[argp++] = TypePtr::NOTNULL;    // n
045ef6
-  fields[argp++] = TypeInt::INT;        // len
045ef6
+  if (CCallingConventionRequiresIntsAsLongs) {
045ef6
+    fields[argp++] = TypeLong::LONG;    // len
045ef6
+    fields[argp++] = TypeLong::HALF;    // placeholder
045ef6
+  } else {
045ef6
+    fields[argp++] = TypeInt::INT;      // len
045ef6
+  }
045ef6
   fields[argp++] = TypeLong::LONG;      // inv
045ef6
   fields[argp++] = Type::HALF;
045ef6
   fields[argp++] = TypePtr::NOTNULL;    // result