8024927: Nashorn performance regression with CompressedOops
authorcoleenp
Fri, 25 Oct 2013 15:19:29 -0400
changeset 21188 d053e4e8f901
parent 21187 8b35dcf13513
child 21189 e851a0a007ce
8024927: Nashorn performance regression with CompressedOops Summary: Allocate compressed class space at end of Java heap. For small heap sizes, without CDS, save some space so compressed classes can have the same favorable compression as oops Reviewed-by: stefank, hseigel, goetz
hotspot/src/cpu/sparc/vm/macroAssembler_sparc.cpp
hotspot/src/cpu/sparc/vm/sparc.ad
hotspot/src/cpu/x86/vm/macroAssembler_x86.cpp
hotspot/src/share/vm/memory/metaspace.cpp
hotspot/src/share/vm/memory/metaspace.hpp
hotspot/src/share/vm/memory/universe.cpp
hotspot/src/share/vm/utilities/globalDefinitions.hpp
hotspot/test/runtime/CompressedOops/CompressedClassPointers.java
--- a/hotspot/src/cpu/sparc/vm/macroAssembler_sparc.cpp	Fri Oct 25 11:13:11 2013 -0400
+++ b/hotspot/src/cpu/sparc/vm/macroAssembler_sparc.cpp	Fri Oct 25 15:19:29 2013 -0400
@@ -4096,15 +4096,19 @@
 
 void MacroAssembler::encode_klass_not_null(Register r) {
   assert (UseCompressedClassPointers, "must be compressed");
-  assert(Universe::narrow_klass_base() != NULL, "narrow_klass_base should be initialized");
-  assert(r != G6_heapbase, "bad register choice");
-  set((intptr_t)Universe::narrow_klass_base(), G6_heapbase);
-  sub(r, G6_heapbase, r);
-  if (Universe::narrow_klass_shift() != 0) {
-    assert (LogKlassAlignmentInBytes == Universe::narrow_klass_shift(), "decode alg wrong");
-    srlx(r, LogKlassAlignmentInBytes, r);
+  if (Universe::narrow_klass_base() != NULL) {
+    assert(r != G6_heapbase, "bad register choice");
+    set((intptr_t)Universe::narrow_klass_base(), G6_heapbase);
+    sub(r, G6_heapbase, r);
+    if (Universe::narrow_klass_shift() != 0) {
+      assert (LogKlassAlignmentInBytes == Universe::narrow_klass_shift(), "decode alg wrong");
+      srlx(r, LogKlassAlignmentInBytes, r);
+    }
+    reinit_heapbase();
+  } else {
+    assert (LogKlassAlignmentInBytes == Universe::narrow_klass_shift() || Universe::narrow_klass_shift() == 0, "decode alg wrong");
+    srlx(r, Universe::narrow_klass_shift(), r);
   }
-  reinit_heapbase();
 }
 
 void MacroAssembler::encode_klass_not_null(Register src, Register dst) {
@@ -4112,11 +4116,16 @@
     encode_klass_not_null(src);
   } else {
     assert (UseCompressedClassPointers, "must be compressed");
-    assert(Universe::narrow_klass_base() != NULL, "narrow_klass_base should be initialized");
-    set((intptr_t)Universe::narrow_klass_base(), dst);
-    sub(src, dst, dst);
-    if (Universe::narrow_klass_shift() != 0) {
-      srlx(dst, LogKlassAlignmentInBytes, dst);
+    if (Universe::narrow_klass_base() != NULL) {
+      set((intptr_t)Universe::narrow_klass_base(), dst);
+      sub(src, dst, dst);
+      if (Universe::narrow_klass_shift() != 0) {
+        srlx(dst, LogKlassAlignmentInBytes, dst);
+      }
+    } else {
+      // shift src into dst
+      assert (LogKlassAlignmentInBytes == Universe::narrow_klass_shift() || Universe::narrow_klass_shift() == 0, "decode alg wrong");
+      srlx(src, Universe::narrow_klass_shift(), dst);
     }
   }
 }
@@ -4126,14 +4135,16 @@
 // the instructions they generate change, then this method needs to be updated.
 int MacroAssembler::instr_size_for_decode_klass_not_null() {
   assert (UseCompressedClassPointers, "only for compressed klass ptrs");
-  // set + add + set
-  int num_instrs = insts_for_internal_set((intptr_t)Universe::narrow_klass_base()) + 1 +
-    insts_for_internal_set((intptr_t)Universe::narrow_ptrs_base());
-  if (Universe::narrow_klass_shift() == 0) {
-    return num_instrs * BytesPerInstWord;
-  } else { // sllx
-    return (num_instrs + 1) * BytesPerInstWord;
+  int num_instrs = 1;  // shift src,dst or add
+  if (Universe::narrow_klass_base() != NULL) {
+    // set + add + set
+    num_instrs += insts_for_internal_set((intptr_t)Universe::narrow_klass_base()) +
+                  insts_for_internal_set((intptr_t)Universe::narrow_ptrs_base());
+    if (Universe::narrow_klass_shift() != 0) {
+      num_instrs += 1;  // sllx
+    }
   }
+  return num_instrs * BytesPerInstWord;
 }
 
 // !!! If the instructions that get generated here change then function
@@ -4142,13 +4153,17 @@
   // Do not add assert code to this unless you change vtableStubs_sparc.cpp
   // pd_code_size_limit.
   assert (UseCompressedClassPointers, "must be compressed");
-  assert(Universe::narrow_klass_base() != NULL, "narrow_klass_base should be initialized");
-  assert(r != G6_heapbase, "bad register choice");
-  set((intptr_t)Universe::narrow_klass_base(), G6_heapbase);
-  if (Universe::narrow_klass_shift() != 0)
-    sllx(r, LogKlassAlignmentInBytes, r);
-  add(r, G6_heapbase, r);
-  reinit_heapbase();
+  if (Universe::narrow_klass_base() != NULL) {
+    assert(r != G6_heapbase, "bad register choice");
+    set((intptr_t)Universe::narrow_klass_base(), G6_heapbase);
+    if (Universe::narrow_klass_shift() != 0)
+      sllx(r, LogKlassAlignmentInBytes, r);
+    add(r, G6_heapbase, r);
+    reinit_heapbase();
+  } else {
+    assert (LogKlassAlignmentInBytes == Universe::narrow_klass_shift() || Universe::narrow_klass_shift() == 0, "decode alg wrong");
+    sllx(r, Universe::narrow_klass_shift(), r);
+  }
 }
 
 void  MacroAssembler::decode_klass_not_null(Register src, Register dst) {
@@ -4158,16 +4173,21 @@
     // Do not add assert code to this unless you change vtableStubs_sparc.cpp
     // pd_code_size_limit.
     assert (UseCompressedClassPointers, "must be compressed");
-    assert(Universe::narrow_klass_base() != NULL, "narrow_klass_base should be initialized");
-    if (Universe::narrow_klass_shift() != 0) {
-      assert((src != G6_heapbase) && (dst != G6_heapbase), "bad register choice");
-      set((intptr_t)Universe::narrow_klass_base(), G6_heapbase);
-      sllx(src, LogKlassAlignmentInBytes, dst);
-      add(dst, G6_heapbase, dst);
-      reinit_heapbase();
+    if (Universe::narrow_klass_base() != NULL) {
+      if (Universe::narrow_klass_shift() != 0) {
+        assert((src != G6_heapbase) && (dst != G6_heapbase), "bad register choice");
+        set((intptr_t)Universe::narrow_klass_base(), G6_heapbase);
+        sllx(src, LogKlassAlignmentInBytes, dst);
+        add(dst, G6_heapbase, dst);
+        reinit_heapbase();
+      } else {
+        set((intptr_t)Universe::narrow_klass_base(), dst);
+        add(src, dst, dst);
+      }
     } else {
-      set((intptr_t)Universe::narrow_klass_base(), dst);
-      add(src, dst, dst);
+      // shift/mov src into dst.
+      assert (LogKlassAlignmentInBytes == Universe::narrow_klass_shift() || Universe::narrow_klass_shift() == 0, "decode alg wrong");
+      sllx(src, Universe::narrow_klass_shift(), dst);
     }
   }
 }
--- a/hotspot/src/cpu/sparc/vm/sparc.ad	Fri Oct 25 11:13:11 2013 -0400
+++ b/hotspot/src/cpu/sparc/vm/sparc.ad	Fri Oct 25 15:19:29 2013 -0400
@@ -1660,12 +1660,16 @@
   if (UseCompressedClassPointers) {
     assert(Universe::heap() != NULL, "java heap should be initialized");
     st->print_cr("\tLDUW   [R_O0 + oopDesc::klass_offset_in_bytes],R_G5\t! Inline cache check - compressed klass");
-    st->print_cr("\tSET    Universe::narrow_klass_base,R_G6_heap_base");
-    if (Universe::narrow_klass_shift() != 0) {
-      st->print_cr("\tSLL    R_G5,3,R_G5");
+    if (Universe::narrow_klass_base() != 0) {
+      st->print_cr("\tSET    Universe::narrow_klass_base,R_G6_heap_base");
+      if (Universe::narrow_klass_shift() != 0) {
+        st->print_cr("\tSLL    R_G5,Universe::narrow_klass_shift,R_G5");
+      }
+      st->print_cr("\tADD    R_G5,R_G6_heap_base,R_G5");
+      st->print_cr("\tSET    Universe::narrow_ptrs_base,R_G6_heap_base");
+    } else {
+      st->print_cr("\tSLL    R_G5,Universe::narrow_klass_shift,R_G5");
     }
-    st->print_cr("\tADD    R_G5,R_G6_heap_base,R_G5");
-    st->print_cr("\tSET    Universe::narrow_ptrs_base,R_G6_heap_base");
   } else {
     st->print_cr("\tLDX    [R_O0 + oopDesc::klass_offset_in_bytes],R_G5\t! Inline cache check");
   }
--- a/hotspot/src/cpu/x86/vm/macroAssembler_x86.cpp	Fri Oct 25 11:13:11 2013 -0400
+++ b/hotspot/src/cpu/x86/vm/macroAssembler_x86.cpp	Fri Oct 25 15:19:29 2013 -0400
@@ -5049,25 +5049,32 @@
 }
 
 void MacroAssembler::encode_klass_not_null(Register r) {
-  assert(Universe::narrow_klass_base() != NULL, "Base should be initialized");
-  // Use r12 as a scratch register in which to temporarily load the narrow_klass_base.
-  assert(r != r12_heapbase, "Encoding a klass in r12");
-  mov64(r12_heapbase, (int64_t)Universe::narrow_klass_base());
-  subq(r, r12_heapbase);
+  if (Universe::narrow_klass_base() != NULL) {
+    // Use r12 as a scratch register in which to temporarily load the narrow_klass_base.
+    assert(r != r12_heapbase, "Encoding a klass in r12");
+    mov64(r12_heapbase, (int64_t)Universe::narrow_klass_base());
+    subq(r, r12_heapbase);
+  }
   if (Universe::narrow_klass_shift() != 0) {
     assert (LogKlassAlignmentInBytes == Universe::narrow_klass_shift(), "decode alg wrong");
     shrq(r, LogKlassAlignmentInBytes);
   }
-  reinit_heapbase();
+  if (Universe::narrow_klass_base() != NULL) {
+    reinit_heapbase();
+  }
 }
 
 void MacroAssembler::encode_klass_not_null(Register dst, Register src) {
   if (dst == src) {
     encode_klass_not_null(src);
   } else {
-    mov64(dst, (int64_t)Universe::narrow_klass_base());
-    negq(dst);
-    addq(dst, src);
+    if (Universe::narrow_klass_base() != NULL) {
+      mov64(dst, (int64_t)Universe::narrow_klass_base());
+      negq(dst);
+      addq(dst, src);
+    } else {
+      movptr(dst, src);
+    }
     if (Universe::narrow_klass_shift() != 0) {
       assert (LogKlassAlignmentInBytes == Universe::narrow_klass_shift(), "decode alg wrong");
       shrq(dst, LogKlassAlignmentInBytes);
@@ -5081,15 +5088,19 @@
 // generate change, then this method needs to be updated.
 int MacroAssembler::instr_size_for_decode_klass_not_null() {
   assert (UseCompressedClassPointers, "only for compressed klass ptrs");
-  // mov64 + addq + shlq? + mov64  (for reinit_heapbase()).
-  return (Universe::narrow_klass_shift() == 0 ? 20 : 24);
+  if (Universe::narrow_klass_base() != NULL) {
+    // mov64 + addq + shlq? + mov64  (for reinit_heapbase()).
+    return (Universe::narrow_klass_shift() == 0 ? 20 : 24);
+  } else {
+    // longest load decode klass function, mov64, leaq
+    return 16;
+  }
 }
 
 // !!! If the instructions that get generated here change then function
 // instr_size_for_decode_klass_not_null() needs to get updated.
 void  MacroAssembler::decode_klass_not_null(Register r) {
   // Note: it will change flags
-  assert(Universe::narrow_klass_base() != NULL, "Base should be initialized");
   assert (UseCompressedClassPointers, "should only be used for compressed headers");
   assert(r != r12_heapbase, "Decoding a klass in r12");
   // Cannot assert, unverified entry point counts instructions (see .ad file)
@@ -5100,14 +5111,15 @@
     shlq(r, LogKlassAlignmentInBytes);
   }
   // Use r12 as a scratch register in which to temporarily load the narrow_klass_base.
-  mov64(r12_heapbase, (int64_t)Universe::narrow_klass_base());
-  addq(r, r12_heapbase);
-  reinit_heapbase();
+  if (Universe::narrow_klass_base() != NULL) {
+    mov64(r12_heapbase, (int64_t)Universe::narrow_klass_base());
+    addq(r, r12_heapbase);
+    reinit_heapbase();
+  }
 }
 
 void  MacroAssembler::decode_klass_not_null(Register dst, Register src) {
   // Note: it will change flags
-  assert(Universe::narrow_klass_base() != NULL, "Base should be initialized");
   assert (UseCompressedClassPointers, "should only be used for compressed headers");
   if (dst == src) {
     decode_klass_not_null(dst);
@@ -5115,7 +5127,6 @@
     // Cannot assert, unverified entry point counts instructions (see .ad file)
     // vtableStubs also counts instructions in pd_code_size_limit.
     // Also do not verify_oop as this is called by verify_oop.
-
     mov64(dst, (int64_t)Universe::narrow_klass_base());
     if (Universe::narrow_klass_shift() != 0) {
       assert(LogKlassAlignmentInBytes == Universe::narrow_klass_shift(), "decode alg wrong");
--- a/hotspot/src/share/vm/memory/metaspace.cpp	Fri Oct 25 11:13:11 2013 -0400
+++ b/hotspot/src/share/vm/memory/metaspace.cpp	Fri Oct 25 15:19:29 2013 -0400
@@ -56,7 +56,7 @@
 
 MetaWord* last_allocated = 0;
 
-size_t Metaspace::_class_metaspace_size;
+size_t Metaspace::_compressed_class_space_size;
 
 // Used in declarations in SpaceManager and ChunkManager
 enum ChunkIndex {
@@ -2799,6 +2799,8 @@
 #define VIRTUALSPACEMULTIPLIER 2
 
 #ifdef _LP64
+static const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
+
 void Metaspace::set_narrow_klass_base_and_shift(address metaspace_base, address cds_base) {
   // Figure out the narrow_klass_base and the narrow_klass_shift.  The
   // narrow_klass_base is the lower of the metaspace base and the cds base
@@ -2808,14 +2810,22 @@
   address higher_address;
   if (UseSharedSpaces) {
     higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()),
-                          (address)(metaspace_base + class_metaspace_size()));
+                          (address)(metaspace_base + compressed_class_space_size()));
     lower_base = MIN2(metaspace_base, cds_base);
   } else {
-    higher_address = metaspace_base + class_metaspace_size();
+    higher_address = metaspace_base + compressed_class_space_size();
     lower_base = metaspace_base;
+
+    uint64_t klass_encoding_max = UnscaledClassSpaceMax << LogKlassAlignmentInBytes;
+    // If compressed class space fits in lower 32G, we don't need a base.
+    if (higher_address <= (address)klass_encoding_max) {
+      lower_base = 0; // effectively lower base is zero.
+    }
   }
+
   Universe::set_narrow_klass_base(lower_base);
-  if ((uint64_t)(higher_address - lower_base) < (uint64_t)max_juint) {
+
+  if ((uint64_t)(higher_address - lower_base) < UnscaledClassSpaceMax) {
     Universe::set_narrow_klass_shift(0);
   } else {
     assert(!UseSharedSpaces, "Cannot shift with UseSharedSpaces");
@@ -2830,24 +2840,24 @@
   assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs");
   address lower_base = MIN2((address)metaspace_base, cds_base);
   address higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()),
-                                (address)(metaspace_base + class_metaspace_size()));
-  return ((uint64_t)(higher_address - lower_base) < (uint64_t)max_juint);
+                                (address)(metaspace_base + compressed_class_space_size()));
+  return ((uint64_t)(higher_address - lower_base) < UnscaledClassSpaceMax);
 }
 
 // Try to allocate the metaspace at the requested addr.
 void Metaspace::allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base) {
   assert(using_class_space(), "called improperly");
   assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs");
-  assert(class_metaspace_size() < KlassEncodingMetaspaceMax,
+  assert(compressed_class_space_size() < KlassEncodingMetaspaceMax,
          "Metaspace size is too big");
-  assert_is_ptr_aligned(requested_addr,          _reserve_alignment);
-  assert_is_ptr_aligned(cds_base,                _reserve_alignment);
-  assert_is_size_aligned(class_metaspace_size(), _reserve_alignment);
+  assert_is_ptr_aligned(requested_addr, _reserve_alignment);
+  assert_is_ptr_aligned(cds_base, _reserve_alignment);
+  assert_is_size_aligned(compressed_class_space_size(), _reserve_alignment);
 
   // Don't use large pages for the class space.
   bool large_pages = false;
 
-  ReservedSpace metaspace_rs = ReservedSpace(class_metaspace_size(),
+  ReservedSpace metaspace_rs = ReservedSpace(compressed_class_space_size(),
                                              _reserve_alignment,
                                              large_pages,
                                              requested_addr, 0);
@@ -2862,7 +2872,7 @@
       while (!metaspace_rs.is_reserved() && (addr + increment > addr) &&
              can_use_cds_with_metaspace_addr(addr + increment, cds_base)) {
         addr = addr + increment;
-        metaspace_rs = ReservedSpace(class_metaspace_size(),
+        metaspace_rs = ReservedSpace(compressed_class_space_size(),
                                      _reserve_alignment, large_pages, addr, 0);
       }
     }
@@ -2873,11 +2883,11 @@
     // initialization has happened that depends on UseCompressedClassPointers.
     // So, UseCompressedClassPointers cannot be turned off at this point.
     if (!metaspace_rs.is_reserved()) {
-      metaspace_rs = ReservedSpace(class_metaspace_size(),
+      metaspace_rs = ReservedSpace(compressed_class_space_size(),
                                    _reserve_alignment, large_pages);
       if (!metaspace_rs.is_reserved()) {
         vm_exit_during_initialization(err_msg("Could not allocate metaspace: %d bytes",
-                                              class_metaspace_size()));
+                                              compressed_class_space_size()));
       }
     }
   }
@@ -2899,8 +2909,8 @@
   if (PrintCompressedOopsMode || (PrintMiscellaneous && Verbose)) {
     gclog_or_tty->print_cr("Narrow klass base: " PTR_FORMAT ", Narrow klass shift: " SIZE_FORMAT,
                             Universe::narrow_klass_base(), Universe::narrow_klass_shift());
-    gclog_or_tty->print_cr("Metaspace Size: " SIZE_FORMAT " Address: " PTR_FORMAT " Req Addr: " PTR_FORMAT,
-                           class_metaspace_size(), metaspace_rs.base(), requested_addr);
+    gclog_or_tty->print_cr("Compressed class space size: " SIZE_FORMAT " Address: " PTR_FORMAT " Req Addr: " PTR_FORMAT,
+                           compressed_class_space_size(), metaspace_rs.base(), requested_addr);
   }
 }
 
@@ -2966,7 +2976,7 @@
   MaxMetaspaceExpansion = restricted_align_down(MaxMetaspaceExpansion, _commit_alignment);
 
   CompressedClassSpaceSize = restricted_align_down(CompressedClassSpaceSize, _reserve_alignment);
-  set_class_metaspace_size(CompressedClassSpaceSize);
+  set_compressed_class_space_size(CompressedClassSpaceSize);
 }
 
 void Metaspace::global_initialize() {
@@ -2995,12 +3005,12 @@
     }
 
 #ifdef _LP64
-    if (cds_total + class_metaspace_size() > (uint64_t)max_juint) {
+    if (cds_total + compressed_class_space_size() > UnscaledClassSpaceMax) {
       vm_exit_during_initialization("Unable to dump shared archive.",
           err_msg("Size of archive (" SIZE_FORMAT ") + compressed class space ("
                   SIZE_FORMAT ") == total (" SIZE_FORMAT ") is larger than compressed "
-                  "klass limit: " SIZE_FORMAT, cds_total, class_metaspace_size(),
-                  cds_total + class_metaspace_size(), (size_t)max_juint));
+                  "klass limit: " SIZE_FORMAT, cds_total, compressed_class_space_size(),
+                  cds_total + compressed_class_space_size(), UnscaledClassSpaceMax));
     }
 
     // Set the compressed klass pointer base so that decoding of these pointers works
@@ -3048,7 +3058,8 @@
         cds_end = (char *)align_ptr_up(cds_end, _reserve_alignment);
         allocate_metaspace_compressed_klass_ptrs(cds_end, cds_address);
       } else {
-        allocate_metaspace_compressed_klass_ptrs((char *)CompressedKlassPointersBase, 0);
+        char* base = (char*)align_ptr_up(Universe::heap()->reserved_region().end(), _reserve_alignment);
+        allocate_metaspace_compressed_klass_ptrs(base, 0);
       }
     }
 #endif
--- a/hotspot/src/share/vm/memory/metaspace.hpp	Fri Oct 25 11:13:11 2013 -0400
+++ b/hotspot/src/share/vm/memory/metaspace.hpp	Fri Oct 25 15:19:29 2013 -0400
@@ -115,13 +115,13 @@
   static size_t align_word_size_up(size_t);
 
   // Aligned size of the metaspace.
-  static size_t _class_metaspace_size;
+  static size_t _compressed_class_space_size;
 
-  static size_t class_metaspace_size() {
-    return _class_metaspace_size;
+  static size_t compressed_class_space_size() {
+    return _compressed_class_space_size;
   }
-  static void set_class_metaspace_size(size_t metaspace_size) {
-    _class_metaspace_size = metaspace_size;
+  static void set_compressed_class_space_size(size_t size) {
+    _compressed_class_space_size = size;
   }
 
   static size_t _first_chunk_word_size;
--- a/hotspot/src/share/vm/memory/universe.cpp	Fri Oct 25 11:13:11 2013 -0400
+++ b/hotspot/src/share/vm/memory/universe.cpp	Fri Oct 25 15:19:29 2013 -0400
@@ -677,13 +677,13 @@
 // HeapBased - Use compressed oops with heap base + encoding.
 
 // 4Gb
-static const uint64_t NarrowOopHeapMax = (uint64_t(max_juint) + 1);
+static const uint64_t UnscaledOopHeapMax = (uint64_t(max_juint) + 1);
 // 32Gb
-// OopEncodingHeapMax == NarrowOopHeapMax << LogMinObjAlignmentInBytes;
+// OopEncodingHeapMax == UnscaledOopHeapMax << LogMinObjAlignmentInBytes;
 
 char* Universe::preferred_heap_base(size_t heap_size, size_t alignment, NARROW_OOP_MODE mode) {
   assert(is_size_aligned((size_t)OopEncodingHeapMax, alignment), "Must be");
-  assert(is_size_aligned((size_t)NarrowOopHeapMax, alignment), "Must be");
+  assert(is_size_aligned((size_t)UnscaledOopHeapMax, alignment), "Must be");
   assert(is_size_aligned(heap_size, alignment), "Must be");
 
   uintx heap_base_min_address_aligned = align_size_up(HeapBaseMinAddress, alignment);
@@ -702,20 +702,40 @@
     // If the total size is small enough to allow UnscaledNarrowOop then
     // just use UnscaledNarrowOop.
     } else if ((total_size <= OopEncodingHeapMax) && (mode != HeapBasedNarrowOop)) {
-      if ((total_size <= NarrowOopHeapMax) && (mode == UnscaledNarrowOop) &&
+      if ((total_size <= UnscaledOopHeapMax) && (mode == UnscaledNarrowOop) &&
           (Universe::narrow_oop_shift() == 0)) {
         // Use 32-bits oops without encoding and
         // place heap's top on the 4Gb boundary
-        base = (NarrowOopHeapMax - heap_size);
+        base = (UnscaledOopHeapMax - heap_size);
       } else {
         // Can't reserve with NarrowOopShift == 0
         Universe::set_narrow_oop_shift(LogMinObjAlignmentInBytes);
+
         if (mode == UnscaledNarrowOop ||
-            mode == ZeroBasedNarrowOop && total_size <= NarrowOopHeapMax) {
+            mode == ZeroBasedNarrowOop && total_size <= UnscaledOopHeapMax) {
+
           // Use zero based compressed oops with encoding and
           // place heap's top on the 32Gb boundary in case
           // total_size > 4Gb or failed to reserve below 4Gb.
-          base = (OopEncodingHeapMax - heap_size);
+          uint64_t heap_top = OopEncodingHeapMax;
+
+          // For small heaps, save some space for compressed class pointer
+          // space so it can be decoded with no base.
+          if (UseCompressedClassPointers && !UseSharedSpaces &&
+              OopEncodingHeapMax <= 32*G) {
+
+            uint64_t class_space = align_size_up(CompressedClassSpaceSize, alignment);
+            assert(is_size_aligned((size_t)OopEncodingHeapMax-class_space,
+                   alignment), "difference must be aligned too");
+            uint64_t new_top = OopEncodingHeapMax-class_space;
+
+            if (total_size <= new_top) {
+              heap_top = new_top;
+            }
+          }
+
+          // Align base to the adjusted top of the heap
+          base = heap_top - heap_size;
         }
       }
     } else {
@@ -737,7 +757,7 @@
       // Set to a non-NULL value so the ReservedSpace ctor computes
       // the correct no-access prefix.
       // The final value will be set in initialize_heap() below.
-      Universe::set_narrow_oop_base((address)NarrowOopHeapMax);
+      Universe::set_narrow_oop_base((address)UnscaledOopHeapMax);
 #ifdef _WIN64
       if (UseLargePages) {
         // Cannot allocate guard pages for implicit checks in indexed
@@ -833,7 +853,7 @@
         Universe::set_narrow_oop_use_implicit_null_checks(true);
       }
 #endif //  _WIN64
-      if((uint64_t)Universe::heap()->reserved_region().end() > NarrowOopHeapMax) {
+      if((uint64_t)Universe::heap()->reserved_region().end() > UnscaledOopHeapMax) {
         // Can't reserve heap below 4Gb.
         Universe::set_narrow_oop_shift(LogMinObjAlignmentInBytes);
       } else {
--- a/hotspot/src/share/vm/utilities/globalDefinitions.hpp	Fri Oct 25 11:13:11 2013 -0400
+++ b/hotspot/src/share/vm/utilities/globalDefinitions.hpp	Fri Oct 25 15:19:29 2013 -0400
@@ -368,8 +368,6 @@
 // Klass encoding metaspace max size
 const uint64_t KlassEncodingMetaspaceMax = (uint64_t(max_juint) + 1) << LogKlassAlignmentInBytes;
 
-const jlong CompressedKlassPointersBase = NOT_LP64(0) LP64_ONLY(CONST64(0x800000000));  // 32*G
-
 // Machine dependent stuff
 
 #ifdef TARGET_ARCH_x86
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/runtime/CompressedOops/CompressedClassPointers.java	Fri Oct 25 15:19:29 2013 -0400
@@ -0,0 +1,136 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8024927
+ * @summary Testing address of compressed class pointer space as best as possible.
+ * @library /testlibrary
+ */
+
+import com.oracle.java.testlibrary.*;
+
+public class CompressedClassPointers {
+
+    public static void smallHeapTest() throws Exception {
+        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
+            "-XX:+UnlockDiagnosticVMOptions",
+            "-XX:SharedBaseAddress=8g",
+            "-Xmx128m",
+            "-XX:+PrintCompressedOopsMode",
+            "-XX:+VerifyBeforeGC", "-version");
+        OutputAnalyzer output = new OutputAnalyzer(pb.start());
+        output.shouldContain("Narrow klass base: 0x0000000000000000");
+        output.shouldHaveExitValue(0);
+    }
+
+    public static void smallHeapTestWith3G() throws Exception {
+        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
+            "-XX:+UnlockDiagnosticVMOptions",
+            "-XX:CompressedClassSpaceSize=3g",
+            "-Xmx128m",
+            "-XX:+PrintCompressedOopsMode",
+            "-XX:+VerifyBeforeGC", "-version");
+        OutputAnalyzer output = new OutputAnalyzer(pb.start());
+        output.shouldContain("Narrow klass base: 0x0000000000000000, Narrow klass shift: 3");
+        output.shouldHaveExitValue(0);
+    }
+
+    public static void largeHeapTest() throws Exception {
+        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
+            "-XX:+UnlockDiagnosticVMOptions",
+            "-Xmx30g",
+            "-XX:+PrintCompressedOopsMode",
+            "-XX:+VerifyBeforeGC", "-version");
+        OutputAnalyzer output = new OutputAnalyzer(pb.start());
+        output.shouldNotContain("Narrow klass base: 0x0000000000000000");
+        output.shouldContain("Narrow klass shift: 0");
+        output.shouldHaveExitValue(0);
+    }
+
+    public static void largePagesTest() throws Exception {
+        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
+            "-XX:+UnlockDiagnosticVMOptions",
+            "-Xmx128m",
+            "-XX:+UseLargePages",
+            "-XX:+PrintCompressedOopsMode",
+            "-XX:+VerifyBeforeGC", "-version");
+        OutputAnalyzer output = new OutputAnalyzer(pb.start());
+        output.shouldContain("Narrow klass base:");
+        output.shouldHaveExitValue(0);
+    }
+
+    public static void sharingTest() throws Exception {
+        // Test small heaps
+        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
+            "-XX:+UnlockDiagnosticVMOptions",
+            "-XX:SharedArchiveFile=./sample.jsa",
+            "-Xmx128m",
+            "-XX:SharedBaseAddress=8g",
+            "-XX:+PrintCompressedOopsMode",
+            "-XX:+VerifyBeforeGC",
+            "-Xshare:dump");
+        OutputAnalyzer output = new OutputAnalyzer(pb.start());
+        try {
+          output.shouldContain("Loading classes to share");
+          output.shouldHaveExitValue(0);
+
+          pb = ProcessTools.createJavaProcessBuilder(
+            "-XX:+UnlockDiagnosticVMOptions",
+            "-XX:SharedArchiveFile=./sample.jsa",
+            "-Xmx128m",
+            "-XX:SharedBaseAddress=8g",
+            "-XX:+PrintCompressedOopsMode",
+            "-Xshare:on",
+            "-version");
+          output = new OutputAnalyzer(pb.start());
+          output.shouldContain("sharing");
+          output.shouldHaveExitValue(0);
+
+        } catch (RuntimeException e) {
+          output.shouldContain("Unable to use shared archive");
+          output.shouldHaveExitValue(1);
+        }
+    }
+
+  public static void main(String[] args) throws Exception {
+      if (!Platform.is64bit()) {
+          // Can't test this on 32 bit, just pass
+          System.out.println("Skipping test on 32bit");
+          return;
+      }
+      // Solaris 10 can't mmap compressed oops space without a base
+      if (Platform.isSolaris()) {
+           String name = System.getProperty("os.version");
+           if (name.equals("5.10")) {
+               System.out.println("Skipping test on Solaris 10");
+               return;
+           }
+      }
+      smallHeapTest();
+      smallHeapTestWith3G();
+      largeHeapTest();
+      largePagesTest();
+      sharingTest();
+  }
+}