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