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