1 /*
   2  * Copyright (c) 1997, 2011, 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
  44 # include "adfiles/ad_ppc.hpp"
  45 #endif
  46 
  47 #define RM_SIZE _RM_SIZE /* a constant private to the class RegMask */
  48 
  49 //-------------Non-zero bit search methods used by RegMask---------------------
  50 // Find lowest 1, or return 32 if empty
  51 int find_lowest_bit( uint32 mask ) {
  52   int n = 0;
  53   if( (mask & 0xffff) == 0 ) {
  54     mask >>= 16;
  55     n += 16;
  56   }
  57   if( (mask & 0xff) == 0 ) {
  58     mask >>= 8;
  59     n += 8;
  60   }
  61   if( (mask & 0xf) == 0 ) {
  62     mask >>= 4;
  63     n += 4;
  64   }
  65   if( (mask & 0x3) == 0 ) {
  66     mask >>= 2;
  67     n += 2;
  68   }
  69   if( (mask & 0x1) == 0 ) {
  70     mask >>= 1;
  71      n += 1;
  72   }
  73   if( mask == 0 ) {
  74     n = 32;
  75   }
  76   return n;
  77 }
  78 
  79 // Find highest 1, or return 32 if empty
  80 int find_hihghest_bit( uint32 mask ) {
  81   int n = 0;
  82   if( mask > 0xffff ) {
  83     mask >>= 16;
  84     n += 16;
  85   }
  86   if( mask > 0xff ) {
  87     mask >>= 8;
  88     n += 8;
  89   }
  90   if( mask > 0xf ) {
  91     mask >>= 4;
  92     n += 4;
  93   }
  94   if( mask > 0x3 ) {
  95     mask >>= 2;
  96     n += 2;
  97   }
  98   if( mask > 0x1 ) {
  99     mask >>= 1;
 100     n += 1;
 101   }
 102   if( mask == 0 ) {
 103     n = 32;
 104   }
 105   return n;
 106 }
 107 
 108 //------------------------------dump-------------------------------------------
 109 
 110 #ifndef PRODUCT
 111 void OptoReg::dump( int r ) {
 112   switch( r ) {
 113   case Special: tty->print("r---");   break;
 114   case Bad:     tty->print("rBAD");   break;
 115   default:
 116     if( r < _last_Mach_Reg ) tty->print(Matcher::regName[r]);
 117     else tty->print("rS%d",r);
 118     break;
 119   }
 120 }
 121 #endif
 122 
 123 
 124 //=============================================================================
 125 const RegMask RegMask::Empty(
 126 # define BODY(I) 0,
 127   FORALL_BODY
 128 # undef BODY
 129   0
 130 );
 131 
 132 //------------------------------find_first_pair--------------------------------
 133 // Find the lowest-numbered register pair in the mask.  Return the
 134 // HIGHEST register number in the pair, or BAD if no pairs.
 135 OptoReg::Name RegMask::find_first_pair() const {
 136   VerifyPairs();
 137   for( int i = 0; i < RM_SIZE; i++ ) {
 138     if( _A[i] ) {               // Found some bits
 139       int bit = _A[i] & -_A[i]; // Extract low bit
 140       // Convert to bit number, return hi bit in pair
 141       return OptoReg::Name((i<<_LogWordBits)+find_lowest_bit(bit)+1);
 142     }
 143   }
 144   return OptoReg::Bad;
 145 }
 146 
 147 //------------------------------ClearToPairs-----------------------------------
 148 // Clear out partial bits; leave only bit pairs
 149 void RegMask::ClearToPairs() {
 150   for( int i = 0; i < RM_SIZE; i++ ) {
 151     int bits = _A[i];
 152     bits &= ((bits & 0x55555555)<<1); // 1 hi-bit set for each pair
 153     bits |= (bits>>1);          // Smear 1 hi-bit into a pair
 154     _A[i] = bits;
 155   }
 156   VerifyPairs();
 157 }
 158 
 159 //------------------------------SmearToPairs-----------------------------------
 160 // Smear out partial bits; leave only bit pairs
 161 void RegMask::SmearToPairs() {
 162   for( int i = 0; i < RM_SIZE; i++ ) {
 163     int bits = _A[i];
 164     bits |= ((bits & 0x55555555)<<1); // Smear lo bit hi per pair
 165     bits |= ((bits & 0xAAAAAAAA)>>1); // Smear hi bit lo per pair
 166     _A[i] = bits;
 167   }
 168   VerifyPairs();
 169 }
 170 
 171 //------------------------------is_aligned_pairs-------------------------------
 172 bool RegMask::is_aligned_Pairs() const {
 173   // Assert that the register mask contains only bit pairs.
 174   for( int i = 0; i < RM_SIZE; i++ ) {
 175     int bits = _A[i];
 176     while( bits ) {             // Check bits for pairing
 177       int bit = bits & -bits;   // Extract low bit
 178       // Low bit is not odd means its mis-aligned.
 179       if( (bit & 0x55555555) == 0 ) return false;
 180       bits -= bit;              // Remove bit from mask
 181       // Check for aligned adjacent bit
 182       if( (bits & (bit<<1)) == 0 ) return false;
 183       bits -= (bit<<1);         // Remove other halve of pair
 184     }
 185   }
 186   return true;
 187 }
 188 
 189 //------------------------------is_bound1--------------------------------------
 190 // Return TRUE if the mask contains a single bit
 191 int RegMask::is_bound1() const {
 192   if( is_AllStack() ) return false;
 193   int bit = -1;                 // Set to hold the one bit allowed
 194   for( int i = 0; i < RM_SIZE; i++ ) {
 195     if( _A[i] ) {               // Found some bits
 196       if( bit != -1 ) return false; // Already had bits, so fail
 197       bit = _A[i] & -_A[i];     // Extract 1 bit from mask
 198       if( bit != _A[i] ) return false; // Found many bits, so fail
 199     }
 200   }
 201   // True for both the empty mask and for a single bit
 202   return true;
 203 }
 204 
 205 //------------------------------is_bound2--------------------------------------
 206 // Return TRUE if the mask contains an adjacent pair of bits and no other bits.
 207 int RegMask::is_bound2() const {
 208   if( is_AllStack() ) return false;
 209 
 210   int bit = -1;                 // Set to hold the one bit allowed
 211   for( int i = 0; i < RM_SIZE; i++ ) {
 212     if( _A[i] ) {               // Found some bits
 213       if( bit != -1 ) return false; // Already had bits, so fail
 214       bit = _A[i] & -(_A[i]);   // Extract 1 bit from mask
 215       if( (bit << 1) != 0 ) {   // Bit pair stays in same word?
 216         if( (bit | (bit<<1)) != _A[i] )
 217           return false;         // Require adjacent bit pair and no more bits
 218       } else {                  // Else its a split-pair case
 219         if( bit != _A[i] ) return false; // Found many bits, so fail
 220         i++;                    // Skip iteration forward
 221         if( _A[i] != 1 ) return false; // Require 1 lo bit in next word
 222       }
 223     }
 224   }
 225   // True for both the empty mask and for a bit pair
 226   return true;
 227 }
 228 
 229 //------------------------------is_UP------------------------------------------
 230 // UP means register only, Register plus stack, or stack only is DOWN
 231 bool RegMask::is_UP() const {
 232   // Quick common case check for DOWN (any stack slot is legal)
 233   if( is_AllStack() )
 234     return false;
 235   // Slower check for any stack bits set (also DOWN)
 236   if( overlap(Matcher::STACK_ONLY_mask) )
 237     return false;
 238   // Not DOWN, so must be UP
 239   return true;
 240 }
 241 
 242 //------------------------------Size-------------------------------------------
 243 // Compute size of register mask in bits
 244 uint RegMask::Size() const {
 245   extern uint8 bitsInByte[256];
 246   uint sum = 0;
 247   for( int i = 0; i < RM_SIZE; i++ )
 248     sum +=
 249       bitsInByte[(_A[i]>>24) & 0xff] +
 250       bitsInByte[(_A[i]>>16) & 0xff] +
 251       bitsInByte[(_A[i]>> 8) & 0xff] +
 252       bitsInByte[ _A[i]      & 0xff];
 253   return sum;
 254 }
 255 
 256 #ifndef PRODUCT
 257 //------------------------------print------------------------------------------
 258 void RegMask::dump( ) const {
 259   tty->print("[");
 260   RegMask rm = *this;           // Structure copy into local temp
 261 
 262   OptoReg::Name start = rm.find_first_elem(); // Get a register
 263   if( OptoReg::is_valid(start) ) { // Check for empty mask
 264     rm.Remove(start);           // Yank from mask
 265     OptoReg::dump(start);       // Print register
 266     OptoReg::Name last = start;
 267 
 268     // Now I have printed an initial register.
 269     // Print adjacent registers as "rX-rZ" instead of "rX,rY,rZ".
 270     // Begin looping over the remaining registers.
 271     while( 1 ) {                //
 272       OptoReg::Name reg = rm.find_first_elem(); // Get a register
 273       if( !OptoReg::is_valid(reg) )
 274         break;                  // Empty mask, end loop
 275       rm.Remove(reg);           // Yank from mask
 276 
 277       if( last+1 == reg ) {     // See if they are adjacent
 278         // Adjacent registers just collect into long runs, no printing.
 279         last = reg;
 280       } else {                  // Ending some kind of run
 281         if( start == last ) {   // 1-register run; no special printing
 282         } else if( start+1 == last ) {
 283           tty->print(",");      // 2-register run; print as "rX,rY"
 284           OptoReg::dump(last);
 285         } else {                // Multi-register run; print as "rX-rZ"
 286           tty->print("-");
 287           OptoReg::dump(last);
 288         }
 289         tty->print(",");        // Seperate start of new run
 290         start = last = reg;     // Start a new register run
 291         OptoReg::dump(start); // Print register
 292       } // End of if ending a register run or not
 293     } // End of while regmask not empty
 294 
 295     if( start == last ) {       // 1-register run; no special printing
 296     } else if( start+1 == last ) {
 297       tty->print(",");          // 2-register run; print as "rX,rY"
 298       OptoReg::dump(last);
 299     } else {                    // Multi-register run; print as "rX-rZ"
 300       tty->print("-");
 301       OptoReg::dump(last);
 302     }
 303     if( rm.is_AllStack() ) tty->print("...");
 304   }
 305   tty->print("]");
 306 }
 307 #endif