1 /* 2 * Copyright (c) 1997, 2014, 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 "opto/ad.hpp" 27 #include "opto/compile.hpp" 28 #include "opto/regmask.hpp" 29 30 #define RM_SIZE _RM_SIZE /* a constant private to the class RegMask */ 31 32 //-------------Non-zero bit search methods used by RegMask--------------------- 33 // Find lowest 1, or return 32 if empty 34 int find_lowest_bit( uint32_t mask ) { 35 int n = 0; 36 if( (mask & 0xffff) == 0 ) { 37 mask >>= 16; 38 n += 16; 39 } 40 if( (mask & 0xff) == 0 ) { 41 mask >>= 8; 42 n += 8; 43 } 44 if( (mask & 0xf) == 0 ) { 45 mask >>= 4; 46 n += 4; 47 } 48 if( (mask & 0x3) == 0 ) { 49 mask >>= 2; 50 n += 2; 51 } 52 if( (mask & 0x1) == 0 ) { 53 mask >>= 1; 54 n += 1; 55 } 56 if( mask == 0 ) { 57 n = 32; 58 } 59 return n; 60 } 61 62 // Find highest 1, or return 32 if empty 63 int find_hihghest_bit( uint32_t mask ) { 64 int n = 0; 65 if( mask > 0xffff ) { 66 mask >>= 16; 67 n += 16; 68 } 69 if( mask > 0xff ) { 70 mask >>= 8; 71 n += 8; 72 } 73 if( mask > 0xf ) { 74 mask >>= 4; 75 n += 4; 76 } 77 if( mask > 0x3 ) { 78 mask >>= 2; 79 n += 2; 80 } 81 if( mask > 0x1 ) { 82 mask >>= 1; 83 n += 1; 84 } 85 if( mask == 0 ) { 86 n = 32; 87 } 88 return n; 89 } 90 91 //------------------------------dump------------------------------------------- 92 93 #ifndef PRODUCT 94 void OptoReg::dump(int r, outputStream *st) { 95 switch (r) { 96 case Special: st->print("r---"); break; 97 case Bad: st->print("rBAD"); break; 98 default: 99 if (r < _last_Mach_Reg) st->print("%s", Matcher::regName[r]); 100 else st->print("rS%d",r); 101 break; 102 } 103 } 104 #endif 105 106 107 //============================================================================= 108 const RegMask RegMask::Empty( 109 # define BODY(I) 0, 110 FORALL_BODY 111 # undef BODY 112 0 113 ); 114 115 //============================================================================= 116 bool RegMask::is_vector(uint ireg) { 117 return (ireg == Op_VecS || ireg == Op_VecD || 118 ireg == Op_VecX || ireg == Op_VecY || ireg == Op_VecZ ); 119 } 120 121 int RegMask::num_registers(uint ireg) { 122 switch(ireg) { 123 case Op_VecZ: 124 return 16; 125 case Op_VecY: 126 return 8; 127 case Op_VecX: 128 return 4; 129 case Op_VecD: 130 case Op_RegD: 131 case Op_RegL: 132 #ifdef _LP64 133 case Op_RegP: 134 #endif 135 return 2; 136 } 137 // Op_VecS and the rest ideal registers. 138 return 1; 139 } 140 141 //------------------------------find_first_pair-------------------------------- 142 // Find the lowest-numbered register pair in the mask. Return the 143 // HIGHEST register number in the pair, or BAD if no pairs. 144 OptoReg::Name RegMask::find_first_pair() const { 145 verify_pairs(); 146 for( int i = 0; i < RM_SIZE; i++ ) { 147 if( _A[i] ) { // Found some bits 148 int bit = _A[i] & -_A[i]; // Extract low bit 149 // Convert to bit number, return hi bit in pair 150 return OptoReg::Name((i<<_LogWordBits)+find_lowest_bit(bit)+1); 151 } 152 } 153 return OptoReg::Bad; 154 } 155 156 //------------------------------ClearToPairs----------------------------------- 157 // Clear out partial bits; leave only bit pairs 158 void RegMask::clear_to_pairs() { 159 for( int i = 0; i < RM_SIZE; i++ ) { 160 int bits = _A[i]; 161 bits &= ((bits & 0x55555555)<<1); // 1 hi-bit set for each pair 162 bits |= (bits>>1); // Smear 1 hi-bit into a pair 163 _A[i] = bits; 164 } 165 verify_pairs(); 166 } 167 168 //------------------------------SmearToPairs----------------------------------- 169 // Smear out partial bits; leave only bit pairs 170 void RegMask::smear_to_pairs() { 171 for( int i = 0; i < RM_SIZE; i++ ) { 172 int bits = _A[i]; 173 bits |= ((bits & 0x55555555)<<1); // Smear lo bit hi per pair 174 bits |= ((bits & 0xAAAAAAAA)>>1); // Smear hi bit lo per pair 175 _A[i] = bits; 176 } 177 verify_pairs(); 178 } 179 180 //------------------------------is_aligned_pairs------------------------------- 181 bool RegMask::is_aligned_pairs() const { 182 // Assert that the register mask contains only bit pairs. 183 for( int i = 0; i < RM_SIZE; i++ ) { 184 int bits = _A[i]; 185 while( bits ) { // Check bits for pairing 186 int bit = bits & -bits; // Extract low bit 187 // Low bit is not odd means its mis-aligned. 188 if( (bit & 0x55555555) == 0 ) return false; 189 bits -= bit; // Remove bit from mask 190 // Check for aligned adjacent bit 191 if( (bits & (bit<<1)) == 0 ) return false; 192 bits -= (bit<<1); // Remove other halve of pair 193 } 194 } 195 return true; 196 } 197 198 //------------------------------is_bound1-------------------------------------- 199 // Return TRUE if the mask contains a single bit 200 int RegMask::is_bound1() const { 201 if( is_AllStack() ) return false; 202 int bit = -1; // Set to hold the one bit allowed 203 for( int i = 0; i < RM_SIZE; i++ ) { 204 if( _A[i] ) { // Found some bits 205 if( bit != -1 ) return false; // Already had bits, so fail 206 bit = _A[i] & -_A[i]; // Extract 1 bit from mask 207 if( bit != _A[i] ) return false; // Found many bits, so fail 208 } 209 } 210 // True for both the empty mask and for a single bit 211 return true; 212 } 213 214 //------------------------------is_bound2-------------------------------------- 215 // Return TRUE if the mask contains an adjacent pair of bits and no other bits. 216 int RegMask::is_bound_pair() const { 217 if( is_AllStack() ) return false; 218 219 int bit = -1; // Set to hold the one bit allowed 220 for( int i = 0; i < RM_SIZE; i++ ) { 221 if( _A[i] ) { // Found some bits 222 if( bit != -1 ) return false; // Already had bits, so fail 223 bit = _A[i] & -(_A[i]); // Extract 1 bit from mask 224 if( (bit << 1) != 0 ) { // Bit pair stays in same word? 225 if( (bit | (bit<<1)) != _A[i] ) 226 return false; // Require adjacent bit pair and no more bits 227 } else { // Else its a split-pair case 228 if( bit != _A[i] ) return false; // Found many bits, so fail 229 i++; // Skip iteration forward 230 if( i >= RM_SIZE || _A[i] != 1 ) 231 return false; // Require 1 lo bit in next word 232 } 233 } 234 } 235 // True for both the empty mask and for a bit pair 236 return true; 237 } 238 239 // only indicies of power 2 are accessed, so index 3 is only filled in for storage. 240 static int low_bits[5] = { 0x55555555, 0x11111111, 0x01010101, 0x00000000, 0x00010001 }; 241 //------------------------------find_first_set--------------------------------- 242 // Find the lowest-numbered register set in the mask. Return the 243 // HIGHEST register number in the set, or BAD if no sets. 244 // Works also for size 1. 245 OptoReg::Name RegMask::find_first_set(const int size) const { 246 verify_sets(size); 247 for (int i = 0; i < RM_SIZE; i++) { 248 if (_A[i]) { // Found some bits 249 int bit = _A[i] & -_A[i]; // Extract low bit 250 // Convert to bit number, return hi bit in pair 251 return OptoReg::Name((i<<_LogWordBits)+find_lowest_bit(bit)+(size-1)); 252 } 253 } 254 return OptoReg::Bad; 255 } 256 257 //------------------------------clear_to_sets---------------------------------- 258 // Clear out partial bits; leave only aligned adjacent bit pairs 259 void RegMask::clear_to_sets(const int size) { 260 if (size == 1) return; 261 assert(2 <= size && size <= 16, "update low bits table"); 262 assert(is_power_of_2(size), "sanity"); 263 int low_bits_mask = low_bits[size>>2]; 264 for (int i = 0; i < RM_SIZE; i++) { 265 int bits = _A[i]; 266 int sets = (bits & low_bits_mask); 267 for (int j = 1; j < size; j++) { 268 sets = (bits & (sets<<1)); // filter bits which produce whole sets 269 } 270 sets |= (sets>>1); // Smear 1 hi-bit into a set 271 if (size > 2) { 272 sets |= (sets>>2); // Smear 2 hi-bits into a set 273 if (size > 4) { 274 sets |= (sets>>4); // Smear 4 hi-bits into a set 275 if (size > 8) { 276 sets |= (sets>>8); // Smear 8 hi-bits into a set 277 } 278 } 279 } 280 _A[i] = sets; 281 } 282 verify_sets(size); 283 } 284 285 //------------------------------smear_to_sets---------------------------------- 286 // Smear out partial bits to aligned adjacent bit sets 287 void RegMask::smear_to_sets(const int size) { 288 if (size == 1) return; 289 assert(2 <= size && size <= 16, "update low bits table"); 290 assert(is_power_of_2(size), "sanity"); 291 int low_bits_mask = low_bits[size>>2]; 292 for (int i = 0; i < RM_SIZE; i++) { 293 int bits = _A[i]; 294 int sets = 0; 295 for (int j = 0; j < size; j++) { 296 sets |= (bits & low_bits_mask); // collect partial bits 297 bits = bits>>1; 298 } 299 sets |= (sets<<1); // Smear 1 lo-bit into a set 300 if (size > 2) { 301 sets |= (sets<<2); // Smear 2 lo-bits into a set 302 if (size > 4) { 303 sets |= (sets<<4); // Smear 4 lo-bits into a set 304 if (size > 8) { 305 sets |= (sets<<8); // Smear 8 lo-bits into a set 306 } 307 } 308 } 309 _A[i] = sets; 310 } 311 verify_sets(size); 312 } 313 314 //------------------------------is_aligned_set-------------------------------- 315 bool RegMask::is_aligned_sets(const int size) const { 316 if (size == 1) return true; 317 assert(2 <= size && size <= 16, "update low bits table"); 318 assert(is_power_of_2(size), "sanity"); 319 int low_bits_mask = low_bits[size>>2]; 320 // Assert that the register mask contains only bit sets. 321 for (int i = 0; i < RM_SIZE; i++) { 322 int bits = _A[i]; 323 while (bits) { // Check bits for pairing 324 int bit = bits & -bits; // Extract low bit 325 // Low bit is not odd means its mis-aligned. 326 if ((bit & low_bits_mask) == 0) return false; 327 // Do extra work since (bit << size) may overflow. 328 int hi_bit = bit << (size-1); // high bit 329 int set = hi_bit + ((hi_bit-1) & ~(bit-1)); 330 // Check for aligned adjacent bits in this set 331 if ((bits & set) != set) return false; 332 bits -= set; // Remove this set 333 } 334 } 335 return true; 336 } 337 338 //------------------------------is_bound_set----------------------------------- 339 // Return TRUE if the mask contains one adjacent set of bits and no other bits. 340 // Works also for size 1. 341 int RegMask::is_bound_set(const int size) const { 342 if( is_AllStack() ) return false; 343 assert(1 <= size && size <= 16, "update low bits table"); 344 int bit = -1; // Set to hold the one bit allowed 345 for (int i = 0; i < RM_SIZE; i++) { 346 if (_A[i] ) { // Found some bits 347 if (bit != -1) 348 return false; // Already had bits, so fail 349 bit = _A[i] & -_A[i]; // Extract low bit from mask 350 int hi_bit = bit << (size-1); // high bit 351 if (hi_bit != 0) { // Bit set stays in same word? 352 int set = hi_bit + ((hi_bit-1) & ~(bit-1)); 353 if (set != _A[i]) 354 return false; // Require adjacent bit set and no more bits 355 } else { // Else its a split-set case 356 if (((-1) & ~(bit-1)) != _A[i]) 357 return false; // Found many bits, so fail 358 i++; // Skip iteration forward and check high part 359 // The lower (32-size) bits should be 0 since it is split case. 360 int clear_bit_size = 32-size; 361 int shift_back_size = 32-clear_bit_size; 362 int set = bit>>clear_bit_size; 363 set = set & -set; // Remove sign extension. 364 set = (((set << size) - 1) >> shift_back_size); 365 if (i >= RM_SIZE || _A[i] != set) 366 return false; // Require expected low bits in next word 367 } 368 } 369 } 370 // True for both the empty mask and for a bit set 371 return true; 372 } 373 374 //------------------------------is_UP------------------------------------------ 375 // UP means register only, Register plus stack, or stack only is DOWN 376 bool RegMask::is_UP() const { 377 // Quick common case check for DOWN (any stack slot is legal) 378 if( is_AllStack() ) 379 return false; 380 // Slower check for any stack bits set (also DOWN) 381 if( overlap(Matcher::STACK_ONLY_mask) ) 382 return false; 383 // Not DOWN, so must be UP 384 return true; 385 } 386 387 //------------------------------Size------------------------------------------- 388 // Compute size of register mask in bits 389 uint RegMask::Size() const { 390 extern uint8_t bitsInByte[512]; 391 uint sum = 0; 392 for( int i = 0; i < RM_SIZE; i++ ) 393 sum += 394 bitsInByte[(_A[i]>>24) & 0xff] + 395 bitsInByte[(_A[i]>>16) & 0xff] + 396 bitsInByte[(_A[i]>> 8) & 0xff] + 397 bitsInByte[ _A[i] & 0xff]; 398 return sum; 399 } 400 401 #ifndef PRODUCT 402 //------------------------------print------------------------------------------ 403 void RegMask::dump(outputStream *st) const { 404 st->print("["); 405 RegMask rm = *this; // Structure copy into local temp 406 407 OptoReg::Name start = rm.find_first_elem(); // Get a register 408 if (OptoReg::is_valid(start)) { // Check for empty mask 409 rm.Remove(start); // Yank from mask 410 OptoReg::dump(start, st); // Print register 411 OptoReg::Name last = start; 412 413 // Now I have printed an initial register. 414 // Print adjacent registers as "rX-rZ" instead of "rX,rY,rZ". 415 // Begin looping over the remaining registers. 416 while (1) { // 417 OptoReg::Name reg = rm.find_first_elem(); // Get a register 418 if (!OptoReg::is_valid(reg)) 419 break; // Empty mask, end loop 420 rm.Remove(reg); // Yank from mask 421 422 if (last+1 == reg) { // See if they are adjacent 423 // Adjacent registers just collect into long runs, no printing. 424 last = reg; 425 } else { // Ending some kind of run 426 if (start == last) { // 1-register run; no special printing 427 } else if (start+1 == last) { 428 st->print(","); // 2-register run; print as "rX,rY" 429 OptoReg::dump(last, st); 430 } else { // Multi-register run; print as "rX-rZ" 431 st->print("-"); 432 OptoReg::dump(last, st); 433 } 434 st->print(","); // Seperate start of new run 435 start = last = reg; // Start a new register run 436 OptoReg::dump(start, st); // Print register 437 } // End of if ending a register run or not 438 } // End of while regmask not empty 439 440 if (start == last) { // 1-register run; no special printing 441 } else if (start+1 == last) { 442 st->print(","); // 2-register run; print as "rX,rY" 443 OptoReg::dump(last, st); 444 } else { // Multi-register run; print as "rX-rZ" 445 st->print("-"); 446 OptoReg::dump(last, st); 447 } 448 if (rm.is_AllStack()) st->print("..."); 449 } 450 st->print("]"); 451 } 452 #endif