1 /* 2 * Copyright (c) 2008, 2015, 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 "asm/assembler.hpp" 27 #include "asm/assembler.inline.hpp" 28 #include "ci/ciEnv.hpp" 29 #include "code/codeCacheExtensions.hpp" 30 #include "gc/shared/cardTableModRefBS.hpp" 31 #include "gc/shared/collectedHeap.inline.hpp" 32 #include "interpreter/interpreter.hpp" 33 #include "interpreter/interpreterRuntime.hpp" 34 #include "interpreter/templateInterpreterGenerator.hpp" 35 #include "memory/resourceArea.hpp" 36 #include "prims/jvm_misc.hpp" 37 #include "prims/methodHandles.hpp" 38 #include "runtime/biasedLocking.hpp" 39 #include "runtime/interfaceSupport.hpp" 40 #include "runtime/objectMonitor.hpp" 41 #include "runtime/os.hpp" 42 #include "runtime/sharedRuntime.hpp" 43 #include "runtime/stubRoutines.hpp" 44 #include "utilities/hashtable.hpp" 45 #include "utilities/macros.hpp" 46 #if INCLUDE_ALL_GCS 47 #include "gc/g1/g1CollectedHeap.inline.hpp" 48 #include "gc/g1/g1SATBCardTableModRefBS.hpp" 49 #include "gc/g1/heapRegion.hpp" 50 #endif // INCLUDE_ALL_GCS 51 52 // Returns whether given imm has equal bit fields <0:size-1> and <size:2*size-1>. 53 inline bool Assembler::LogicalImmediate::has_equal_subpatterns(uintx imm, int size) { 54 uintx mask = right_n_bits(size); 55 uintx subpattern1 = mask_bits(imm, mask); 56 uintx subpattern2 = mask_bits(imm >> size, mask); 57 return subpattern1 == subpattern2; 58 } 59 60 // Returns least size that is a power of two from 2 to 64 with the proviso that given 61 // imm is composed of repeating patterns of this size. 62 inline int Assembler::LogicalImmediate::least_pattern_size(uintx imm) { 63 int size = BitsPerWord; 64 while (size > 2 && has_equal_subpatterns(imm, size >> 1)) { 65 size >>= 1; 66 } 67 return size; 68 } 69 70 // Returns count of set bits in given imm. Based on variable-precision SWAR algorithm. 71 inline int Assembler::LogicalImmediate::population_count(uintx x) { 72 x -= ((x >> 1) & 0x5555555555555555L); 73 x = (((x >> 2) & 0x3333333333333333L) + (x & 0x3333333333333333L)); 74 x = (((x >> 4) + x) & 0x0f0f0f0f0f0f0f0fL); 75 x += (x >> 8); 76 x += (x >> 16); 77 x += (x >> 32); 78 return(x & 0x7f); 79 } 80 81 // Let given x be <A:B> where B = 0 and least bit of A = 1. Returns <A:C>, where C is B-size set bits. 82 inline uintx Assembler::LogicalImmediate::set_least_zeroes(uintx x) { 83 return x | (x - 1); 84 } 85 86 87 #ifdef ASSERT 88 89 // Restores immediate by encoded bit masks. 90 uintx Assembler::LogicalImmediate::decode() { 91 assert (_encoded, "should be"); 92 93 int len_code = (_immN << 6) | ((~_imms) & 0x3f); 94 assert (len_code != 0, "should be"); 95 96 int len = 6; 97 while (!is_set_nth_bit(len_code, len)) len--; 98 int esize = 1 << len; 99 assert (len > 0, "should be"); 100 assert ((_is32bit ? 32 : 64) >= esize, "should be"); 101 102 int levels = right_n_bits(len); 103 int S = _imms & levels; 104 int R = _immr & levels; 105 106 assert (S != levels, "should be"); 107 108 uintx welem = right_n_bits(S + 1); 109 uintx wmask = (R == 0) ? welem : ((welem >> R) | (welem << (esize - R))); 110 111 for (int size = esize; size < 64; size <<= 1) { 112 wmask |= (wmask << size); 113 } 114 115 return wmask; 116 } 117 118 #endif 119 120 121 // Constructs LogicalImmediate by given imm. Figures out if given imm can be used in AArch64 logical 122 // instructions (AND, ANDS, EOR, ORR) and saves its encoding. 123 void Assembler::LogicalImmediate::construct(uintx imm, bool is32) { 124 _is32bit = is32; 125 126 if (is32) { 127 assert(((imm >> 32) == 0) || (((intx)imm >> 31) == -1), "32-bit immediate is out of range"); 128 129 // Replicate low 32 bits. 130 imm &= 0xffffffff; 131 imm |= imm << 32; 132 } 133 134 // All-zeroes and all-ones can not be encoded. 135 if (imm != 0 && (~imm != 0)) { 136 137 // Let LPS (least pattern size) be the least size (power of two from 2 to 64) of repeating 138 // patterns in the immediate. If immediate value can be encoded, it is encoded by pattern 139 // of exactly LPS size (due to structure of valid patterns). In order to verify 140 // that immediate value can be encoded, LPS is calculated and <LPS-1:0> bits of immediate 141 // are verified to be valid pattern. 142 int lps = least_pattern_size(imm); 143 uintx lps_mask = right_n_bits(lps); 144 145 // A valid pattern has one of the following forms: 146 // | 0 x A | 1 x B | 0 x C |, where B > 0 and C > 0, or 147 // | 1 x A | 0 x B | 1 x C |, where B > 0 and C > 0. 148 // For simplicity, the second form of the pattern is inverted into the first form. 149 bool inverted = imm & 0x1; 150 uintx pattern = (inverted ? ~imm : imm) & lps_mask; 151 152 // | 0 x A | 1 x (B + C) | 153 uintx without_least_zeroes = set_least_zeroes(pattern); 154 155 // Pattern is valid iff without least zeroes it is a power of two - 1. 156 if ((without_least_zeroes & (without_least_zeroes + 1)) == 0) { 157 158 // Count B as population count of pattern. 159 int bits_count = population_count(pattern); 160 161 // Count B+C as population count of pattern without least zeroes 162 int left_range = population_count(without_least_zeroes); 163 164 // S-prefix is a part of imms field which encodes LPS. 165 // LPS | S prefix 166 // 64 | not defined 167 // 32 | 0b0 168 // 16 | 0b10 169 // 8 | 0b110 170 // 4 | 0b1110 171 // 2 | 0b11110 172 int s_prefix = (lps == 64) ? 0 : ~set_least_zeroes(lps) & 0x3f; 173 174 // immN bit is set iff LPS == 64. 175 _immN = (lps == 64) ? 1 : 0; 176 assert (!is32 || (_immN == 0), "32-bit immediate should be encoded with zero N-bit"); 177 178 // immr is the rotation size. 179 _immr = lps + (inverted ? 0 : bits_count) - left_range; 180 181 // imms is the field that encodes bits count and S-prefix. 182 _imms = ((inverted ? (lps - bits_count) : bits_count) - 1) | s_prefix; 183 184 _encoded = true; 185 assert (decode() == imm, "illegal encoding"); 186 187 return; 188 } 189 } 190 191 _encoded = false; 192 }