1 /*
   2  * Copyright 1998-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 // FORMS.CPP - Definitions for ADL Parser Forms Classes
  26 #include "adlc.hpp"
  27 
  28 //==============================Instructions===================================
  29 //------------------------------InstructForm-----------------------------------
  30 InstructForm::InstructForm(const char *id, bool ideal_only)
  31   : _ident(id), _ideal_only(ideal_only),
  32     _localNames(cmpstr, hashstr, Form::arena),
  33     _effects(cmpstr, hashstr, Form::arena) {
  34       _ftype = Form::INS;
  35 
  36       _matrule   = NULL;
  37       _insencode = NULL;
  38       _opcode    = NULL;
  39       _size      = NULL;
  40       _attribs   = NULL;
  41       _predicate = NULL;
  42       _exprule   = NULL;
  43       _rewrule   = NULL;
  44       _format    = NULL;
  45       _peephole  = NULL;
  46       _ins_pipe  = NULL;
  47       _uniq_idx  = NULL;
  48       _num_uniq  = 0;
  49       _cisc_spill_operand = Not_cisc_spillable;// Which operand may cisc-spill
  50       _cisc_spill_alternate = NULL;            // possible cisc replacement
  51       _cisc_reg_mask_name = NULL;
  52       _is_cisc_alternate = false;
  53       _is_short_branch = false;
  54       _short_branch_form = NULL;
  55       _alignment = 1;
  56 }
  57 
  58 InstructForm::InstructForm(const char *id, InstructForm *instr, MatchRule *rule)
  59   : _ident(id), _ideal_only(false),
  60     _localNames(instr->_localNames),
  61     _effects(instr->_effects) {
  62       _ftype = Form::INS;
  63 
  64       _matrule   = rule;
  65       _insencode = instr->_insencode;
  66       _opcode    = instr->_opcode;
  67       _size      = instr->_size;
  68       _attribs   = instr->_attribs;
  69       _predicate = instr->_predicate;
  70       _exprule   = instr->_exprule;
  71       _rewrule   = instr->_rewrule;
  72       _format    = instr->_format;
  73       _peephole  = instr->_peephole;
  74       _ins_pipe  = instr->_ins_pipe;
  75       _uniq_idx  = instr->_uniq_idx;
  76       _num_uniq  = instr->_num_uniq;
  77       _cisc_spill_operand = Not_cisc_spillable;// Which operand may cisc-spill
  78       _cisc_spill_alternate = NULL;            // possible cisc replacement
  79       _cisc_reg_mask_name = NULL;
  80       _is_cisc_alternate = false;
  81       _is_short_branch = false;
  82       _short_branch_form = NULL;
  83       _alignment = 1;
  84      // Copy parameters
  85      const char *name;
  86      instr->_parameters.reset();
  87      for (; (name = instr->_parameters.iter()) != NULL;)
  88        _parameters.addName(name);
  89 }
  90 
  91 InstructForm::~InstructForm() {
  92 }
  93 
  94 InstructForm *InstructForm::is_instruction() const {
  95   return (InstructForm*)this;
  96 }
  97 
  98 bool InstructForm::ideal_only() const {
  99   return _ideal_only;
 100 }
 101 
 102 bool InstructForm::sets_result() const {
 103   return (_matrule != NULL && _matrule->sets_result());
 104 }
 105 
 106 bool InstructForm::needs_projections() {
 107   _components.reset();
 108   for( Component *comp; (comp = _components.iter()) != NULL; ) {
 109     if (comp->isa(Component::KILL)) {
 110       return true;
 111     }
 112   }
 113   return false;
 114 }
 115 
 116 
 117 bool InstructForm::has_temps() {
 118   if (_matrule) {
 119     // Examine each component to see if it is a TEMP
 120     _components.reset();
 121     // Skip the first component, if already handled as (SET dst (...))
 122     Component *comp = NULL;
 123     if (sets_result())  comp = _components.iter();
 124     while ((comp = _components.iter()) != NULL) {
 125       if (comp->isa(Component::TEMP)) {
 126         return true;
 127       }
 128     }
 129   }
 130 
 131   return false;
 132 }
 133 
 134 uint InstructForm::num_defs_or_kills() {
 135   uint   defs_or_kills = 0;
 136 
 137   _components.reset();
 138   for( Component *comp; (comp = _components.iter()) != NULL; ) {
 139     if( comp->isa(Component::DEF) || comp->isa(Component::KILL) ) {
 140       ++defs_or_kills;
 141     }
 142   }
 143 
 144   return  defs_or_kills;
 145 }
 146 
 147 // This instruction has an expand rule?
 148 bool InstructForm::expands() const {
 149   return ( _exprule != NULL );
 150 }
 151 
 152 // This instruction has a peephole rule?
 153 Peephole *InstructForm::peepholes() const {
 154   return _peephole;
 155 }
 156 
 157 // This instruction has a peephole rule?
 158 void InstructForm::append_peephole(Peephole *peephole) {
 159   if( _peephole == NULL ) {
 160     _peephole = peephole;
 161   } else {
 162     _peephole->append_peephole(peephole);
 163   }
 164 }
 165 
 166 
 167 // ideal opcode enumeration
 168 const char *InstructForm::ideal_Opcode( FormDict &globalNames )  const {
 169   if( !_matrule ) return "Node"; // Something weird
 170   // Chain rules do not really have ideal Opcodes; use their source
 171   // operand ideal Opcode instead.
 172   if( is_simple_chain_rule(globalNames) ) {
 173     const char *src = _matrule->_rChild->_opType;
 174     OperandForm *src_op = globalNames[src]->is_operand();
 175     assert( src_op, "Not operand class of chain rule" );
 176     if( !src_op->_matrule ) return "Node";
 177     return src_op->_matrule->_opType;
 178   }
 179   // Operand chain rules do not really have ideal Opcodes
 180   if( _matrule->is_chain_rule(globalNames) )
 181     return "Node";
 182   return strcmp(_matrule->_opType,"Set")
 183     ? _matrule->_opType
 184     : _matrule->_rChild->_opType;
 185 }
 186 
 187 // Recursive check on all operands' match rules in my match rule
 188 bool InstructForm::is_pinned(FormDict &globals) {
 189   if ( ! _matrule)  return false;
 190 
 191   int  index   = 0;
 192   if (_matrule->find_type("Goto",          index)) return true;
 193   if (_matrule->find_type("If",            index)) return true;
 194   if (_matrule->find_type("CountedLoopEnd",index)) return true;
 195   if (_matrule->find_type("Return",        index)) return true;
 196   if (_matrule->find_type("Rethrow",       index)) return true;
 197   if (_matrule->find_type("TailCall",      index)) return true;
 198   if (_matrule->find_type("TailJump",      index)) return true;
 199   if (_matrule->find_type("Halt",          index)) return true;
 200   if (_matrule->find_type("Jump",          index)) return true;
 201 
 202   return is_parm(globals);
 203 }
 204 
 205 // Recursive check on all operands' match rules in my match rule
 206 bool InstructForm::is_projection(FormDict &globals) {
 207   if ( ! _matrule)  return false;
 208 
 209   int  index   = 0;
 210   if (_matrule->find_type("Goto",    index)) return true;
 211   if (_matrule->find_type("Return",  index)) return true;
 212   if (_matrule->find_type("Rethrow", index)) return true;
 213   if (_matrule->find_type("TailCall",index)) return true;
 214   if (_matrule->find_type("TailJump",index)) return true;
 215   if (_matrule->find_type("Halt",    index)) return true;
 216 
 217   return false;
 218 }
 219 
 220 // Recursive check on all operands' match rules in my match rule
 221 bool InstructForm::is_parm(FormDict &globals) {
 222   if ( ! _matrule)  return false;
 223 
 224   int  index   = 0;
 225   if (_matrule->find_type("Parm",index)) return true;
 226 
 227   return false;
 228 }
 229 
 230 
 231 // Return 'true' if this instruction matches an ideal 'Copy*' node
 232 int InstructForm::is_ideal_copy() const {
 233   return _matrule ? _matrule->is_ideal_copy() : 0;
 234 }
 235 
 236 // Return 'true' if this instruction is too complex to rematerialize.
 237 int InstructForm::is_expensive() const {
 238   // We can prove it is cheap if it has an empty encoding.
 239   // This helps with platform-specific nops like ThreadLocal and RoundFloat.
 240   if (is_empty_encoding())
 241     return 0;
 242 
 243   if (is_tls_instruction())
 244     return 1;
 245 
 246   if (_matrule == NULL)  return 0;
 247 
 248   return _matrule->is_expensive();
 249 }
 250 
 251 // Has an empty encoding if _size is a constant zero or there
 252 // are no ins_encode tokens.
 253 int InstructForm::is_empty_encoding() const {
 254   if (_insencode != NULL) {
 255     _insencode->reset();
 256     if (_insencode->encode_class_iter() == NULL) {
 257       return 1;
 258     }
 259   }
 260   if (_size != NULL && strcmp(_size, "0") == 0) {
 261     return 1;
 262   }
 263   return 0;
 264 }
 265 
 266 int InstructForm::is_tls_instruction() const {
 267   if (_ident != NULL &&
 268       ( ! strcmp( _ident,"tlsLoadP") ||
 269         ! strncmp(_ident,"tlsLoadP_",9)) ) {
 270     return 1;
 271   }
 272 
 273   if (_matrule != NULL && _insencode != NULL) {
 274     const char* opType = _matrule->_opType;
 275     if (strcmp(opType, "Set")==0)
 276       opType = _matrule->_rChild->_opType;
 277     if (strcmp(opType,"ThreadLocal")==0) {
 278       fprintf(stderr, "Warning: ThreadLocal instruction %s should be named 'tlsLoadP_*'\n",
 279               (_ident == NULL ? "NULL" : _ident));
 280       return 1;
 281     }
 282   }
 283 
 284   return 0;
 285 }
 286 
 287 
 288 // Return 'true' if this instruction matches an ideal 'Copy*' node
 289 bool InstructForm::is_ideal_unlock() const {
 290   return _matrule ? _matrule->is_ideal_unlock() : false;
 291 }
 292 
 293 bool InstructForm::is_ideal_call_leaf() const {
 294   return _matrule ? _matrule->is_ideal_call_leaf() : false;
 295 }
 296 
 297 // Return 'true' if this instruction matches an ideal 'If' node
 298 bool InstructForm::is_ideal_if() const {
 299   if( _matrule == NULL ) return false;
 300 
 301   return _matrule->is_ideal_if();
 302 }
 303 
 304 // Return 'true' if this instruction matches an ideal 'FastLock' node
 305 bool InstructForm::is_ideal_fastlock() const {
 306   if( _matrule == NULL ) return false;
 307 
 308   return _matrule->is_ideal_fastlock();
 309 }
 310 
 311 // Return 'true' if this instruction matches an ideal 'MemBarXXX' node
 312 bool InstructForm::is_ideal_membar() const {
 313   if( _matrule == NULL ) return false;
 314 
 315   return _matrule->is_ideal_membar();
 316 }
 317 
 318 // Return 'true' if this instruction matches an ideal 'LoadPC' node
 319 bool InstructForm::is_ideal_loadPC() const {
 320   if( _matrule == NULL ) return false;
 321 
 322   return _matrule->is_ideal_loadPC();
 323 }
 324 
 325 // Return 'true' if this instruction matches an ideal 'Box' node
 326 bool InstructForm::is_ideal_box() const {
 327   if( _matrule == NULL ) return false;
 328 
 329   return _matrule->is_ideal_box();
 330 }
 331 
 332 // Return 'true' if this instruction matches an ideal 'Goto' node
 333 bool InstructForm::is_ideal_goto() const {
 334   if( _matrule == NULL ) return false;
 335 
 336   return _matrule->is_ideal_goto();
 337 }
 338 
 339 // Return 'true' if this instruction matches an ideal 'Jump' node
 340 bool InstructForm::is_ideal_jump() const {
 341   if( _matrule == NULL ) return false;
 342 
 343   return _matrule->is_ideal_jump();
 344 }
 345 
 346 // Return 'true' if instruction matches ideal 'If' | 'Goto' |
 347 //                    'CountedLoopEnd' | 'Jump'
 348 bool InstructForm::is_ideal_branch() const {
 349   if( _matrule == NULL ) return false;
 350 
 351   return _matrule->is_ideal_if() || _matrule->is_ideal_goto() || _matrule->is_ideal_jump();
 352 }
 353 
 354 
 355 // Return 'true' if this instruction matches an ideal 'Return' node
 356 bool InstructForm::is_ideal_return() const {
 357   if( _matrule == NULL ) return false;
 358 
 359   // Check MatchRule to see if the first entry is the ideal "Return" node
 360   int  index   = 0;
 361   if (_matrule->find_type("Return",index)) return true;
 362   if (_matrule->find_type("Rethrow",index)) return true;
 363   if (_matrule->find_type("TailCall",index)) return true;
 364   if (_matrule->find_type("TailJump",index)) return true;
 365 
 366   return false;
 367 }
 368 
 369 // Return 'true' if this instruction matches an ideal 'Halt' node
 370 bool InstructForm::is_ideal_halt() const {
 371   int  index   = 0;
 372   return _matrule && _matrule->find_type("Halt",index);
 373 }
 374 
 375 // Return 'true' if this instruction matches an ideal 'SafePoint' node
 376 bool InstructForm::is_ideal_safepoint() const {
 377   int  index   = 0;
 378   return _matrule && _matrule->find_type("SafePoint",index);
 379 }
 380 
 381 // Return 'true' if this instruction matches an ideal 'Nop' node
 382 bool InstructForm::is_ideal_nop() const {
 383   return _ident && _ident[0] == 'N' && _ident[1] == 'o' && _ident[2] == 'p' && _ident[3] == '_';
 384 }
 385 
 386 bool InstructForm::is_ideal_control() const {
 387   if ( ! _matrule)  return false;
 388 
 389   return is_ideal_return() || is_ideal_branch() || is_ideal_halt();
 390 }
 391 
 392 // Return 'true' if this instruction matches an ideal 'Call' node
 393 Form::CallType InstructForm::is_ideal_call() const {
 394   if( _matrule == NULL ) return Form::invalid_type;
 395 
 396   // Check MatchRule to see if the first entry is the ideal "Call" node
 397   int  idx   = 0;
 398   if(_matrule->find_type("CallStaticJava",idx))   return Form::JAVA_STATIC;
 399   idx = 0;
 400   if(_matrule->find_type("Lock",idx))             return Form::JAVA_STATIC;
 401   idx = 0;
 402   if(_matrule->find_type("Unlock",idx))           return Form::JAVA_STATIC;
 403   idx = 0;
 404   if(_matrule->find_type("CallDynamicJava",idx))  return Form::JAVA_DYNAMIC;
 405   idx = 0;
 406   if(_matrule->find_type("CallRuntime",idx))      return Form::JAVA_RUNTIME;
 407   idx = 0;
 408   if(_matrule->find_type("CallLeaf",idx))         return Form::JAVA_LEAF;
 409   idx = 0;
 410   if(_matrule->find_type("CallLeafNoFP",idx))     return Form::JAVA_LEAF;
 411   idx = 0;
 412 
 413   return Form::invalid_type;
 414 }
 415 
 416 // Return 'true' if this instruction matches an ideal 'Load?' node
 417 Form::DataType InstructForm::is_ideal_load() const {
 418   if( _matrule == NULL ) return Form::none;
 419 
 420   return  _matrule->is_ideal_load();
 421 }
 422 
 423 // Return 'true' if this instruction matches an ideal 'LoadKlass' node
 424 bool InstructForm::skip_antidep_check() const {
 425   if( _matrule == NULL ) return false;
 426 
 427   return  _matrule->skip_antidep_check();
 428 }
 429 
 430 // Return 'true' if this instruction matches an ideal 'Load?' node
 431 Form::DataType InstructForm::is_ideal_store() const {
 432   if( _matrule == NULL ) return Form::none;
 433 
 434   return  _matrule->is_ideal_store();
 435 }
 436 
 437 // Return the input register that must match the output register
 438 // If this is not required, return 0
 439 uint InstructForm::two_address(FormDict &globals) {
 440   uint  matching_input = 0;
 441   if(_components.count() == 0) return 0;
 442 
 443   _components.reset();
 444   Component *comp = _components.iter();
 445   // Check if there is a DEF
 446   if( comp->isa(Component::DEF) ) {
 447     // Check that this is a register
 448     const char  *def_type = comp->_type;
 449     const Form  *form     = globals[def_type];
 450     OperandForm *op       = form->is_operand();
 451     if( op ) {
 452       if( op->constrained_reg_class() != NULL &&
 453           op->interface_type(globals) == Form::register_interface ) {
 454         // Remember the local name for equality test later
 455         const char *def_name = comp->_name;
 456         // Check if a component has the same name and is a USE
 457         do {
 458           if( comp->isa(Component::USE) && strcmp(comp->_name,def_name)==0 ) {
 459             return operand_position_format(def_name);
 460           }
 461         } while( (comp = _components.iter()) != NULL);
 462       }
 463     }
 464   }
 465 
 466   return 0;
 467 }
 468 
 469 
 470 // when chaining a constant to an instruction, returns 'true' and sets opType
 471 Form::DataType InstructForm::is_chain_of_constant(FormDict &globals) {
 472   const char *dummy  = NULL;
 473   const char *dummy2 = NULL;
 474   return is_chain_of_constant(globals, dummy, dummy2);
 475 }
 476 Form::DataType InstructForm::is_chain_of_constant(FormDict &globals,
 477                 const char * &opTypeParam) {
 478   const char *result = NULL;
 479 
 480   return is_chain_of_constant(globals, opTypeParam, result);
 481 }
 482 
 483 Form::DataType InstructForm::is_chain_of_constant(FormDict &globals,
 484                 const char * &opTypeParam, const char * &resultParam) {
 485   Form::DataType  data_type = Form::none;
 486   if ( ! _matrule)  return data_type;
 487 
 488   // !!!!!
 489   // The source of the chain rule is 'position = 1'
 490   uint         position = 1;
 491   const char  *result   = NULL;
 492   const char  *name     = NULL;
 493   const char  *opType   = NULL;
 494   // Here base_operand is looking for an ideal type to be returned (opType).
 495   if ( _matrule->is_chain_rule(globals)
 496        && _matrule->base_operand(position, globals, result, name, opType) ) {
 497     data_type = ideal_to_const_type(opType);
 498 
 499     // if it isn't an ideal constant type, just return
 500     if ( data_type == Form::none ) return data_type;
 501 
 502     // Ideal constant types also adjust the opType parameter.
 503     resultParam = result;
 504     opTypeParam = opType;
 505     return data_type;
 506   }
 507 
 508   return data_type;
 509 }
 510 
 511 // Check if a simple chain rule
 512 bool InstructForm::is_simple_chain_rule(FormDict &globals) const {
 513   if( _matrule && _matrule->sets_result()
 514       && _matrule->_rChild->_lChild == NULL
 515       && globals[_matrule->_rChild->_opType]
 516       && globals[_matrule->_rChild->_opType]->is_opclass() ) {
 517     return true;
 518   }
 519   return false;
 520 }
 521 
 522 // check for structural rematerialization
 523 bool InstructForm::rematerialize(FormDict &globals, RegisterForm *registers ) {
 524   bool   rematerialize = false;
 525 
 526   Form::DataType data_type = is_chain_of_constant(globals);
 527   if( data_type != Form::none )
 528     rematerialize = true;
 529 
 530   // Constants
 531   if( _components.count() == 1 && _components[0]->is(Component::USE_DEF) )
 532     rematerialize = true;
 533 
 534   // Pseudo-constants (values easily available to the runtime)
 535   if (is_empty_encoding() && is_tls_instruction())
 536     rematerialize = true;
 537 
 538   // 1-input, 1-output, such as copies or increments.
 539   if( _components.count() == 2 &&
 540       _components[0]->is(Component::DEF) &&
 541       _components[1]->isa(Component::USE) )
 542     rematerialize = true;
 543 
 544   // Check for an ideal 'Load?' and eliminate rematerialize option
 545   if ( is_ideal_load() != Form::none || // Ideal load?  Do not rematerialize
 546        is_ideal_copy() != Form::none || // Ideal copy?  Do not rematerialize
 547        is_expensive()  != Form::none) { // Expensive?   Do not rematerialize
 548     rematerialize = false;
 549   }
 550 
 551   // Always rematerialize the flags.  They are more expensive to save &
 552   // restore than to recompute (and possibly spill the compare's inputs).
 553   if( _components.count() >= 1 ) {
 554     Component *c = _components[0];
 555     const Form *form = globals[c->_type];
 556     OperandForm *opform = form->is_operand();
 557     if( opform ) {
 558       // Avoid the special stack_slots register classes
 559       const char *rc_name = opform->constrained_reg_class();
 560       if( rc_name ) {
 561         if( strcmp(rc_name,"stack_slots") ) {
 562           // Check for ideal_type of RegFlags
 563           const char *type = opform->ideal_type( globals, registers );
 564           if( !strcmp(type,"RegFlags") )
 565             rematerialize = true;
 566         } else
 567           rematerialize = false; // Do not rematerialize things target stk
 568       }
 569     }
 570   }
 571 
 572   return rematerialize;
 573 }
 574 
 575 // loads from memory, so must check for anti-dependence
 576 bool InstructForm::needs_anti_dependence_check(FormDict &globals) const {
 577   if ( skip_antidep_check() ) return false;
 578 
 579   // Machine independent loads must be checked for anti-dependences
 580   if( is_ideal_load() != Form::none )  return true;
 581 
 582   // !!!!! !!!!! !!!!!
 583   // TEMPORARY
 584   // if( is_simple_chain_rule(globals) )  return false;
 585 
 586   // String.(compareTo/equals/indexOf) and Arrays.equals use many memorys edges,
 587   // but writes none
 588   if( _matrule && _matrule->_rChild &&
 589       ( strcmp(_matrule->_rChild->_opType,"StrComp"    )==0 ||
 590         strcmp(_matrule->_rChild->_opType,"StrEquals"  )==0 ||
 591         strcmp(_matrule->_rChild->_opType,"StrIndexOf" )==0 ||
 592         strcmp(_matrule->_rChild->_opType,"AryEq"      )==0 ))
 593     return true;
 594 
 595   // Check if instruction has a USE of a memory operand class, but no defs
 596   bool USE_of_memory  = false;
 597   bool DEF_of_memory  = false;
 598   Component     *comp = NULL;
 599   ComponentList &components = (ComponentList &)_components;
 600 
 601   components.reset();
 602   while( (comp = components.iter()) != NULL ) {
 603     const Form  *form = globals[comp->_type];
 604     if( !form ) continue;
 605     OpClassForm *op   = form->is_opclass();
 606     if( !op ) continue;
 607     if( form->interface_type(globals) == Form::memory_interface ) {
 608       if( comp->isa(Component::USE) ) USE_of_memory = true;
 609       if( comp->isa(Component::DEF) ) {
 610         OperandForm *oper = form->is_operand();
 611         if( oper && oper->is_user_name_for_sReg() ) {
 612           // Stack slots are unaliased memory handled by allocator
 613           oper = oper;  // debug stopping point !!!!!
 614         } else {
 615           DEF_of_memory = true;
 616         }
 617       }
 618     }
 619   }
 620   return (USE_of_memory && !DEF_of_memory);
 621 }
 622 
 623 
 624 bool InstructForm::is_wide_memory_kill(FormDict &globals) const {
 625   if( _matrule == NULL ) return false;
 626   if( !_matrule->_opType ) return false;
 627 
 628   if( strcmp(_matrule->_opType,"MemBarRelease") == 0 ) return true;
 629   if( strcmp(_matrule->_opType,"MemBarAcquire") == 0 ) return true;
 630 
 631   return false;
 632 }
 633 
 634 int InstructForm::memory_operand(FormDict &globals) const {
 635   // Machine independent loads must be checked for anti-dependences
 636   // Check if instruction has a USE of a memory operand class, or a def.
 637   int USE_of_memory  = 0;
 638   int DEF_of_memory  = 0;
 639   const char*    last_memory_DEF = NULL; // to test DEF/USE pairing in asserts
 640   Component     *unique          = NULL;
 641   Component     *comp            = NULL;
 642   ComponentList &components      = (ComponentList &)_components;
 643 
 644   components.reset();
 645   while( (comp = components.iter()) != NULL ) {
 646     const Form  *form = globals[comp->_type];
 647     if( !form ) continue;
 648     OpClassForm *op   = form->is_opclass();
 649     if( !op ) continue;
 650     if( op->stack_slots_only(globals) )  continue;
 651     if( form->interface_type(globals) == Form::memory_interface ) {
 652       if( comp->isa(Component::DEF) ) {
 653         last_memory_DEF = comp->_name;
 654         DEF_of_memory++;
 655         unique = comp;
 656       } else if( comp->isa(Component::USE) ) {
 657         if( last_memory_DEF != NULL ) {
 658           assert(0 == strcmp(last_memory_DEF, comp->_name), "every memory DEF is followed by a USE of the same name");
 659           last_memory_DEF = NULL;
 660         }
 661         USE_of_memory++;
 662         if (DEF_of_memory == 0)  // defs take precedence
 663           unique = comp;
 664       } else {
 665         assert(last_memory_DEF == NULL, "unpaired memory DEF");
 666       }
 667     }
 668   }
 669   assert(last_memory_DEF == NULL, "unpaired memory DEF");
 670   assert(USE_of_memory >= DEF_of_memory, "unpaired memory DEF");
 671   USE_of_memory -= DEF_of_memory;   // treat paired DEF/USE as one occurrence
 672   if( (USE_of_memory + DEF_of_memory) > 0 ) {
 673     if( is_simple_chain_rule(globals) ) {
 674       //fprintf(stderr, "Warning: chain rule is not really a memory user.\n");
 675       //((InstructForm*)this)->dump();
 676       // Preceding code prints nothing on sparc and these insns on intel:
 677       // leaP8 leaP32 leaPIdxOff leaPIdxScale leaPIdxScaleOff leaP8 leaP32
 678       // leaPIdxOff leaPIdxScale leaPIdxScaleOff
 679       return NO_MEMORY_OPERAND;
 680     }
 681 
 682     if( DEF_of_memory == 1 ) {
 683       assert(unique != NULL, "");
 684       if( USE_of_memory == 0 ) {
 685         // unique def, no uses
 686       } else {
 687         // // unique def, some uses
 688         // // must return bottom unless all uses match def
 689         // unique = NULL;
 690       }
 691     } else if( DEF_of_memory > 0 ) {
 692       // multiple defs, don't care about uses
 693       unique = NULL;
 694     } else if( USE_of_memory == 1) {
 695       // unique use, no defs
 696       assert(unique != NULL, "");
 697     } else if( USE_of_memory > 0 ) {
 698       // multiple uses, no defs
 699       unique = NULL;
 700     } else {
 701       assert(false, "bad case analysis");
 702     }
 703     // process the unique DEF or USE, if there is one
 704     if( unique == NULL ) {
 705       return MANY_MEMORY_OPERANDS;
 706     } else {
 707       int pos = components.operand_position(unique->_name);
 708       if( unique->isa(Component::DEF) ) {
 709         pos += 1;                // get corresponding USE from DEF
 710       }
 711       assert(pos >= 1, "I was just looking at it!");
 712       return pos;
 713     }
 714   }
 715 
 716   // missed the memory op??
 717   if( true ) {  // %%% should not be necessary
 718     if( is_ideal_store() != Form::none ) {
 719       fprintf(stderr, "Warning: cannot find memory opnd in instr.\n");
 720       ((InstructForm*)this)->dump();
 721       // pretend it has multiple defs and uses
 722       return MANY_MEMORY_OPERANDS;
 723     }
 724     if( is_ideal_load()  != Form::none ) {
 725       fprintf(stderr, "Warning: cannot find memory opnd in instr.\n");
 726       ((InstructForm*)this)->dump();
 727       // pretend it has multiple uses and no defs
 728       return MANY_MEMORY_OPERANDS;
 729     }
 730   }
 731 
 732   return NO_MEMORY_OPERAND;
 733 }
 734 
 735 
 736 // This instruction captures the machine-independent bottom_type
 737 // Expected use is for pointer vs oop determination for LoadP
 738 bool InstructForm::captures_bottom_type() const {
 739   if( _matrule && _matrule->_rChild &&
 740        (!strcmp(_matrule->_rChild->_opType,"CastPP")     ||  // new result type
 741         !strcmp(_matrule->_rChild->_opType,"CastX2P")    ||  // new result type
 742         !strcmp(_matrule->_rChild->_opType,"DecodeN")    ||
 743         !strcmp(_matrule->_rChild->_opType,"EncodeP")    ||
 744         !strcmp(_matrule->_rChild->_opType,"LoadN")      ||
 745         !strcmp(_matrule->_rChild->_opType,"LoadNKlass") ||
 746         !strcmp(_matrule->_rChild->_opType,"CreateEx")   ||  // type of exception
 747         !strcmp(_matrule->_rChild->_opType,"CheckCastPP")) ) return true;
 748   else if ( is_ideal_load() == Form::idealP )                return true;
 749   else if ( is_ideal_store() != Form::none  )                return true;
 750 
 751   return  false;
 752 }
 753 
 754 
 755 // Access instr_cost attribute or return NULL.
 756 const char* InstructForm::cost() {
 757   for (Attribute* cur = _attribs; cur != NULL; cur = (Attribute*)cur->_next) {
 758     if( strcmp(cur->_ident,AttributeForm::_ins_cost) == 0 ) {
 759       return cur->_val;
 760     }
 761   }
 762   return NULL;
 763 }
 764 
 765 // Return count of top-level operands.
 766 uint InstructForm::num_opnds() {
 767   int  num_opnds = _components.num_operands();
 768 
 769   // Need special handling for matching some ideal nodes
 770   // i.e. Matching a return node
 771   /*
 772   if( _matrule ) {
 773     if( strcmp(_matrule->_opType,"Return"   )==0 ||
 774         strcmp(_matrule->_opType,"Halt"     )==0 )
 775       return 3;
 776   }
 777     */
 778   return num_opnds;
 779 }
 780 
 781 // Return count of unmatched operands.
 782 uint InstructForm::num_post_match_opnds() {
 783   uint  num_post_match_opnds = _components.count();
 784   uint  num_match_opnds = _components.match_count();
 785   num_post_match_opnds = num_post_match_opnds - num_match_opnds;
 786 
 787   return num_post_match_opnds;
 788 }
 789 
 790 // Return the number of leaves below this complex operand
 791 uint InstructForm::num_consts(FormDict &globals) const {
 792   if ( ! _matrule) return 0;
 793 
 794   // This is a recursive invocation on all operands in the matchrule
 795   return _matrule->num_consts(globals);
 796 }
 797 
 798 // Constants in match rule with specified type
 799 uint InstructForm::num_consts(FormDict &globals, Form::DataType type) const {
 800   if ( ! _matrule) return 0;
 801 
 802   // This is a recursive invocation on all operands in the matchrule
 803   return _matrule->num_consts(globals, type);
 804 }
 805 
 806 
 807 // Return the register class associated with 'leaf'.
 808 const char *InstructForm::out_reg_class(FormDict &globals) {
 809   assert( false, "InstructForm::out_reg_class(FormDict &globals); Not Implemented");
 810 
 811   return NULL;
 812 }
 813 
 814 
 815 
 816 // Lookup the starting position of inputs we are interested in wrt. ideal nodes
 817 uint InstructForm::oper_input_base(FormDict &globals) {
 818   if( !_matrule ) return 1;     // Skip control for most nodes
 819 
 820   // Need special handling for matching some ideal nodes
 821   // i.e. Matching a return node
 822   if( strcmp(_matrule->_opType,"Return"    )==0 ||
 823       strcmp(_matrule->_opType,"Rethrow"   )==0 ||
 824       strcmp(_matrule->_opType,"TailCall"  )==0 ||
 825       strcmp(_matrule->_opType,"TailJump"  )==0 ||
 826       strcmp(_matrule->_opType,"SafePoint" )==0 ||
 827       strcmp(_matrule->_opType,"Halt"      )==0 )
 828     return AdlcVMDeps::Parms;   // Skip the machine-state edges
 829 
 830   if( _matrule->_rChild &&
 831       ( strcmp(_matrule->_rChild->_opType,"AryEq"     )==0 ||
 832         strcmp(_matrule->_rChild->_opType,"StrComp"   )==0 ||
 833         strcmp(_matrule->_rChild->_opType,"StrEquals" )==0 ||
 834         strcmp(_matrule->_rChild->_opType,"StrIndexOf")==0 )) {
 835         // String.(compareTo/equals/indexOf) and Arrays.equals
 836         // take 1 control and 1 memory edges.
 837     return 2;
 838   }
 839 
 840   // Check for handling of 'Memory' input/edge in the ideal world.
 841   // The AD file writer is shielded from knowledge of these edges.
 842   int base = 1;                 // Skip control
 843   base += _matrule->needs_ideal_memory_edge(globals);
 844 
 845   // Also skip the base-oop value for uses of derived oops.
 846   // The AD file writer is shielded from knowledge of these edges.
 847   base += needs_base_oop_edge(globals);
 848 
 849   return base;
 850 }
 851 
 852 // Implementation does not modify state of internal structures
 853 void InstructForm::build_components() {
 854   // Add top-level operands to the components
 855   if (_matrule)  _matrule->append_components(_localNames, _components);
 856 
 857   // Add parameters that "do not appear in match rule".
 858   bool has_temp = false;
 859   const char *name;
 860   const char *kill_name = NULL;
 861   for (_parameters.reset(); (name = _parameters.iter()) != NULL;) {
 862     OperandForm *opForm = (OperandForm*)_localNames[name];
 863 
 864     Effect* e = NULL;
 865     {
 866       const Form* form = _effects[name];
 867       e = form ? form->is_effect() : NULL;
 868     }
 869 
 870     if (e != NULL) {
 871       has_temp |= e->is(Component::TEMP);
 872 
 873       // KILLs must be declared after any TEMPs because TEMPs are real
 874       // uses so their operand numbering must directly follow the real
 875       // inputs from the match rule.  Fixing the numbering seems
 876       // complex so simply enforce the restriction during parse.
 877       if (kill_name != NULL &&
 878           e->isa(Component::TEMP) && !e->isa(Component::DEF)) {
 879         OperandForm* kill = (OperandForm*)_localNames[kill_name];
 880         globalAD->syntax_err(_linenum, "%s: %s %s must be at the end of the argument list\n",
 881                              _ident, kill->_ident, kill_name);
 882       } else if (e->isa(Component::KILL) && !e->isa(Component::USE)) {
 883         kill_name = name;
 884       }
 885     }
 886 
 887     const Component *component  = _components.search(name);
 888     if ( component  == NULL ) {
 889       if (e) {
 890         _components.insert(name, opForm->_ident, e->_use_def, false);
 891         component = _components.search(name);
 892         if (component->isa(Component::USE) && !component->isa(Component::TEMP) && _matrule) {
 893           const Form *form = globalAD->globalNames()[component->_type];
 894           assert( form, "component type must be a defined form");
 895           OperandForm *op   = form->is_operand();
 896           if (op->_interface && op->_interface->is_RegInterface()) {
 897             globalAD->syntax_err(_linenum, "%s: illegal USE of non-input: %s %s\n",
 898                                  _ident, opForm->_ident, name);
 899           }
 900         }
 901       } else {
 902         // This would be a nice warning but it triggers in a few places in a benign way
 903         // if (_matrule != NULL && !expands()) {
 904         //   globalAD->syntax_err(_linenum, "%s: %s %s not mentioned in effect or match rule\n",
 905         //                        _ident, opForm->_ident, name);
 906         // }
 907         _components.insert(name, opForm->_ident, Component::INVALID, false);
 908       }
 909     }
 910     else if (e) {
 911       // Component was found in the list
 912       // Check if there is a new effect that requires an extra component.
 913       // This happens when adding 'USE' to a component that is not yet one.
 914       if ((!component->isa( Component::USE) && ((e->_use_def & Component::USE) != 0))) {
 915         if (component->isa(Component::USE) && _matrule) {
 916           const Form *form = globalAD->globalNames()[component->_type];
 917           assert( form, "component type must be a defined form");
 918           OperandForm *op   = form->is_operand();
 919           if (op->_interface && op->_interface->is_RegInterface()) {
 920             globalAD->syntax_err(_linenum, "%s: illegal USE of non-input: %s %s\n",
 921                                  _ident, opForm->_ident, name);
 922           }
 923         }
 924         _components.insert(name, opForm->_ident, e->_use_def, false);
 925       } else {
 926         Component  *comp = (Component*)component;
 927         comp->promote_use_def_info(e->_use_def);
 928       }
 929       // Component positions are zero based.
 930       int  pos  = _components.operand_position(name);
 931       assert( ! (component->isa(Component::DEF) && (pos >= 1)),
 932               "Component::DEF can only occur in the first position");
 933     }
 934   }
 935 
 936   // Resolving the interactions between expand rules and TEMPs would
 937   // be complex so simply disallow it.
 938   if (_matrule == NULL && has_temp) {
 939     globalAD->syntax_err(_linenum, "%s: TEMPs without match rule isn't supported\n", _ident);
 940   }
 941 
 942   return;
 943 }
 944 
 945 // Return zero-based position in component list;  -1 if not in list.
 946 int   InstructForm::operand_position(const char *name, int usedef) {
 947   return unique_opnds_idx(_components.operand_position(name, usedef));
 948 }
 949 
 950 int   InstructForm::operand_position_format(const char *name) {
 951   return unique_opnds_idx(_components.operand_position_format(name));
 952 }
 953 
 954 // Return zero-based position in component list; -1 if not in list.
 955 int   InstructForm::label_position() {
 956   return unique_opnds_idx(_components.label_position());
 957 }
 958 
 959 int   InstructForm::method_position() {
 960   return unique_opnds_idx(_components.method_position());
 961 }
 962 
 963 // Return number of relocation entries needed for this instruction.
 964 uint  InstructForm::reloc(FormDict &globals) {
 965   uint reloc_entries  = 0;
 966   // Check for "Call" nodes
 967   if ( is_ideal_call() )      ++reloc_entries;
 968   if ( is_ideal_return() )    ++reloc_entries;
 969   if ( is_ideal_safepoint() ) ++reloc_entries;
 970 
 971 
 972   // Check if operands MAYBE oop pointers, by checking for ConP elements
 973   // Proceed through the leaves of the match-tree and check for ConPs
 974   if ( _matrule != NULL ) {
 975     uint         position = 0;
 976     const char  *result   = NULL;
 977     const char  *name     = NULL;
 978     const char  *opType   = NULL;
 979     while (_matrule->base_operand(position, globals, result, name, opType)) {
 980       if ( strcmp(opType,"ConP") == 0 ) {
 981 #ifdef SPARC
 982         reloc_entries += 2; // 1 for sethi + 1 for setlo
 983 #else
 984         ++reloc_entries;
 985 #endif
 986       }
 987       ++position;
 988     }
 989   }
 990 
 991   // Above is only a conservative estimate
 992   // because it did not check contents of operand classes.
 993   // !!!!! !!!!!
 994   // Add 1 to reloc info for each operand class in the component list.
 995   Component  *comp;
 996   _components.reset();
 997   while ( (comp = _components.iter()) != NULL ) {
 998     const Form        *form = globals[comp->_type];
 999     assert( form, "Did not find component's type in global names");
1000     const OpClassForm *opc  = form->is_opclass();
1001     const OperandForm *oper = form->is_operand();
1002     if ( opc && (oper == NULL) ) {
1003       ++reloc_entries;
1004     } else if ( oper ) {
1005       // floats and doubles loaded out of method's constant pool require reloc info
1006       Form::DataType type = oper->is_base_constant(globals);
1007       if ( (type == Form::idealF) || (type == Form::idealD) ) {
1008         ++reloc_entries;
1009       }
1010     }
1011   }
1012 
1013   // Float and Double constants may come from the CodeBuffer table
1014   // and require relocatable addresses for access
1015   // !!!!!
1016   // Check for any component being an immediate float or double.
1017   Form::DataType data_type = is_chain_of_constant(globals);
1018   if( data_type==idealD || data_type==idealF ) {
1019 #ifdef SPARC
1020     // sparc required more relocation entries for floating constants
1021     // (expires 9/98)
1022     reloc_entries += 6;
1023 #else
1024     reloc_entries++;
1025 #endif
1026   }
1027 
1028   return reloc_entries;
1029 }
1030 
1031 // Utility function defined in archDesc.cpp
1032 extern bool is_def(int usedef);
1033 
1034 // Return the result of reducing an instruction
1035 const char *InstructForm::reduce_result() {
1036   const char* result = "Universe";  // default
1037   _components.reset();
1038   Component *comp = _components.iter();
1039   if (comp != NULL && comp->isa(Component::DEF)) {
1040     result = comp->_type;
1041     // Override this if the rule is a store operation:
1042     if (_matrule && _matrule->_rChild &&
1043         is_store_to_memory(_matrule->_rChild->_opType))
1044       result = "Universe";
1045   }
1046   return result;
1047 }
1048 
1049 // Return the name of the operand on the right hand side of the binary match
1050 // Return NULL if there is no right hand side
1051 const char *InstructForm::reduce_right(FormDict &globals)  const {
1052   if( _matrule == NULL ) return NULL;
1053   return  _matrule->reduce_right(globals);
1054 }
1055 
1056 // Similar for left
1057 const char *InstructForm::reduce_left(FormDict &globals)   const {
1058   if( _matrule == NULL ) return NULL;
1059   return  _matrule->reduce_left(globals);
1060 }
1061 
1062 
1063 // Base class for this instruction, MachNode except for calls
1064 const char *InstructForm::mach_base_class()  const {
1065   if( is_ideal_call() == Form::JAVA_STATIC ) {
1066     return "MachCallStaticJavaNode";
1067   }
1068   else if( is_ideal_call() == Form::JAVA_DYNAMIC ) {
1069     return "MachCallDynamicJavaNode";
1070   }
1071   else if( is_ideal_call() == Form::JAVA_RUNTIME ) {
1072     return "MachCallRuntimeNode";
1073   }
1074   else if( is_ideal_call() == Form::JAVA_LEAF ) {
1075     return "MachCallLeafNode";
1076   }
1077   else if (is_ideal_return()) {
1078     return "MachReturnNode";
1079   }
1080   else if (is_ideal_halt()) {
1081     return "MachHaltNode";
1082   }
1083   else if (is_ideal_safepoint()) {
1084     return "MachSafePointNode";
1085   }
1086   else if (is_ideal_if()) {
1087     return "MachIfNode";
1088   }
1089   else if (is_ideal_fastlock()) {
1090     return "MachFastLockNode";
1091   }
1092   else if (is_ideal_nop()) {
1093     return "MachNopNode";
1094   }
1095   else if (captures_bottom_type()) {
1096     return "MachTypeNode";
1097   } else {
1098     return "MachNode";
1099   }
1100   assert( false, "ShouldNotReachHere()");
1101   return NULL;
1102 }
1103 
1104 // Compare the instruction predicates for textual equality
1105 bool equivalent_predicates( const InstructForm *instr1, const InstructForm *instr2 ) {
1106   const Predicate *pred1  = instr1->_predicate;
1107   const Predicate *pred2  = instr2->_predicate;
1108   if( pred1 == NULL && pred2 == NULL ) {
1109     // no predicates means they are identical
1110     return true;
1111   }
1112   if( pred1 != NULL && pred2 != NULL ) {
1113     // compare the predicates
1114     if (ADLParser::equivalent_expressions(pred1->_pred, pred2->_pred)) {
1115       return true;
1116     }
1117   }
1118 
1119   return false;
1120 }
1121 
1122 // Check if this instruction can cisc-spill to 'alternate'
1123 bool InstructForm::cisc_spills_to(ArchDesc &AD, InstructForm *instr) {
1124   assert( _matrule != NULL && instr->_matrule != NULL, "must have match rules");
1125   // Do not replace if a cisc-version has been found.
1126   if( cisc_spill_operand() != Not_cisc_spillable ) return false;
1127 
1128   int         cisc_spill_operand = Maybe_cisc_spillable;
1129   char       *result             = NULL;
1130   char       *result2            = NULL;
1131   const char *op_name            = NULL;
1132   const char *reg_type           = NULL;
1133   FormDict   &globals            = AD.globalNames();
1134   cisc_spill_operand = _matrule->matchrule_cisc_spill_match(globals, AD.get_registers(), instr->_matrule, op_name, reg_type);
1135   if( (cisc_spill_operand != Not_cisc_spillable) && (op_name != NULL) && equivalent_predicates(this, instr) ) {
1136     cisc_spill_operand = operand_position(op_name, Component::USE);
1137     int def_oper  = operand_position(op_name, Component::DEF);
1138     if( def_oper == NameList::Not_in_list && instr->num_opnds() == num_opnds()) {
1139       // Do not support cisc-spilling for destination operands and
1140       // make sure they have the same number of operands.
1141       _cisc_spill_alternate = instr;
1142       instr->set_cisc_alternate(true);
1143       if( AD._cisc_spill_debug ) {
1144         fprintf(stderr, "Instruction %s cisc-spills-to %s\n", _ident, instr->_ident);
1145         fprintf(stderr, "   using operand %s %s at index %d\n", reg_type, op_name, cisc_spill_operand);
1146       }
1147       // Record that a stack-version of the reg_mask is needed
1148       // !!!!!
1149       OperandForm *oper = (OperandForm*)(globals[reg_type]->is_operand());
1150       assert( oper != NULL, "cisc-spilling non operand");
1151       const char *reg_class_name = oper->constrained_reg_class();
1152       AD.set_stack_or_reg(reg_class_name);
1153       const char *reg_mask_name  = AD.reg_mask(*oper);
1154       set_cisc_reg_mask_name(reg_mask_name);
1155       const char *stack_or_reg_mask_name = AD.stack_or_reg_mask(*oper);
1156     } else {
1157       cisc_spill_operand = Not_cisc_spillable;
1158     }
1159   } else {
1160     cisc_spill_operand = Not_cisc_spillable;
1161   }
1162 
1163   set_cisc_spill_operand(cisc_spill_operand);
1164   return (cisc_spill_operand != Not_cisc_spillable);
1165 }
1166 
1167 // Check to see if this instruction can be replaced with the short branch
1168 // instruction `short-branch'
1169 bool InstructForm::check_branch_variant(ArchDesc &AD, InstructForm *short_branch) {
1170   if (_matrule != NULL &&
1171       this != short_branch &&   // Don't match myself
1172       !is_short_branch() &&     // Don't match another short branch variant
1173       reduce_result() != NULL &&
1174       strcmp(reduce_result(), short_branch->reduce_result()) == 0 &&
1175       _matrule->equivalent(AD.globalNames(), short_branch->_matrule)) {
1176     // The instructions are equivalent.
1177     if (AD._short_branch_debug) {
1178       fprintf(stderr, "Instruction %s has short form %s\n", _ident, short_branch->_ident);
1179     }
1180     _short_branch_form = short_branch;
1181     return true;
1182   }
1183   return false;
1184 }
1185 
1186 
1187 // --------------------------- FILE *output_routines
1188 //
1189 // Generate the format call for the replacement variable
1190 void InstructForm::rep_var_format(FILE *fp, const char *rep_var) {
1191   // Find replacement variable's type
1192   const Form *form   = _localNames[rep_var];
1193   if (form == NULL) {
1194     fprintf(stderr, "unknown replacement variable in format statement: '%s'\n", rep_var);
1195     assert(false, "ShouldNotReachHere()");
1196   }
1197   OpClassForm *opc   = form->is_opclass();
1198   assert( opc, "replacement variable was not found in local names");
1199   // Lookup the index position of the replacement variable
1200   int idx  = operand_position_format(rep_var);
1201   if ( idx == -1 ) {
1202     assert( strcmp(opc->_ident,"label")==0, "Unimplemented");
1203     assert( false, "ShouldNotReachHere()");
1204   }
1205 
1206   if (is_noninput_operand(idx)) {
1207     // This component isn't in the input array.  Print out the static
1208     // name of the register.
1209     OperandForm* oper = form->is_operand();
1210     if (oper != NULL && oper->is_bound_register()) {
1211       const RegDef* first = oper->get_RegClass()->find_first_elem();
1212       fprintf(fp, "    tty->print(\"%s\");\n", first->_regname);
1213     } else {
1214       globalAD->syntax_err(_linenum, "In %s can't find format for %s %s", _ident, opc->_ident, rep_var);
1215     }
1216   } else {
1217     // Output the format call for this operand
1218     fprintf(fp,"opnd_array(%d)->",idx);
1219     if (idx == 0)
1220       fprintf(fp,"int_format(ra, this, st); // %s\n", rep_var);
1221     else
1222       fprintf(fp,"ext_format(ra, this,idx%d, st); // %s\n", idx, rep_var );
1223   }
1224 }
1225 
1226 // Seach through operands to determine parameters unique positions.
1227 void InstructForm::set_unique_opnds() {
1228   uint* uniq_idx = NULL;
1229   int  nopnds = num_opnds();
1230   uint  num_uniq = nopnds;
1231   int i;
1232   _uniq_idx_length = 0;
1233   if ( nopnds > 0 ) {
1234     // Allocate index array.  Worst case we're mapping from each
1235     // component back to an index and any DEF always goes at 0 so the
1236     // length of the array has to be the number of components + 1.
1237     _uniq_idx_length = _components.count() + 1;
1238     uniq_idx = (uint*) malloc(sizeof(uint)*(_uniq_idx_length));
1239     for( i = 0; i < _uniq_idx_length; i++ ) {
1240       uniq_idx[i] = i;
1241     }
1242   }
1243   // Do it only if there is a match rule and no expand rule.  With an
1244   // expand rule it is done by creating new mach node in Expand()
1245   // method.
1246   if ( nopnds > 0 && _matrule != NULL && _exprule == NULL ) {
1247     const char *name;
1248     uint count;
1249     bool has_dupl_use = false;
1250 
1251     _parameters.reset();
1252     while( (name = _parameters.iter()) != NULL ) {
1253       count = 0;
1254       int position = 0;
1255       int uniq_position = 0;
1256       _components.reset();
1257       Component *comp = NULL;
1258       if( sets_result() ) {
1259         comp = _components.iter();
1260         position++;
1261       }
1262       // The next code is copied from the method operand_position().
1263       for (; (comp = _components.iter()) != NULL; ++position) {
1264         // When the first component is not a DEF,
1265         // leave space for the result operand!
1266         if ( position==0 && (! comp->isa(Component::DEF)) ) {
1267           ++position;
1268         }
1269         if( strcmp(name, comp->_name)==0 ) {
1270           if( ++count > 1 ) {
1271             assert(position < _uniq_idx_length, "out of bounds");
1272             uniq_idx[position] = uniq_position;
1273             has_dupl_use = true;
1274           } else {
1275             uniq_position = position;
1276           }
1277         }
1278         if( comp->isa(Component::DEF)
1279             && comp->isa(Component::USE) ) {
1280           ++position;
1281           if( position != 1 )
1282             --position;   // only use two slots for the 1st USE_DEF
1283         }
1284       }
1285     }
1286     if( has_dupl_use ) {
1287       for( i = 1; i < nopnds; i++ )
1288         if( i != uniq_idx[i] )
1289           break;
1290       int  j = i;
1291       for( ; i < nopnds; i++ )
1292         if( i == uniq_idx[i] )
1293           uniq_idx[i] = j++;
1294       num_uniq = j;
1295     }
1296   }
1297   _uniq_idx = uniq_idx;
1298   _num_uniq = num_uniq;
1299 }
1300 
1301 // Generate index values needed for determining the operand position
1302 void InstructForm::index_temps(FILE *fp, FormDict &globals, const char *prefix, const char *receiver) {
1303   uint  idx = 0;                  // position of operand in match rule
1304   int   cur_num_opnds = num_opnds();
1305 
1306   // Compute the index into vector of operand pointers:
1307   // idx0=0 is used to indicate that info comes from this same node, not from input edge.
1308   // idx1 starts at oper_input_base()
1309   if ( cur_num_opnds >= 1 ) {
1310     fprintf(fp,"    // Start at oper_input_base() and count operands\n");
1311     fprintf(fp,"    unsigned %sidx0 = %d;\n", prefix, oper_input_base(globals));
1312     fprintf(fp,"    unsigned %sidx1 = %d;\n", prefix, oper_input_base(globals));
1313 
1314     // Generate starting points for other unique operands if they exist
1315     for ( idx = 2; idx < num_unique_opnds(); ++idx ) {
1316       if( *receiver == 0 ) {
1317         fprintf(fp,"    unsigned %sidx%d = %sidx%d + opnd_array(%d)->num_edges();\n",
1318                 prefix, idx, prefix, idx-1, idx-1 );
1319       } else {
1320         fprintf(fp,"    unsigned %sidx%d = %sidx%d + %s_opnds[%d]->num_edges();\n",
1321                 prefix, idx, prefix, idx-1, receiver, idx-1 );
1322       }
1323     }
1324   }
1325   if( *receiver != 0 ) {
1326     // This value is used by generate_peepreplace when copying a node.
1327     // Don't emit it in other cases since it can hide bugs with the
1328     // use invalid idx's.
1329     fprintf(fp,"    unsigned %sidx%d = %sreq(); \n", prefix, idx, receiver);
1330   }
1331 
1332 }
1333 
1334 // ---------------------------
1335 bool InstructForm::verify() {
1336   // !!!!! !!!!!
1337   // Check that a "label" operand occurs last in the operand list, if present
1338   return true;
1339 }
1340 
1341 void InstructForm::dump() {
1342   output(stderr);
1343 }
1344 
1345 void InstructForm::output(FILE *fp) {
1346   fprintf(fp,"\nInstruction: %s\n", (_ident?_ident:""));
1347   if (_matrule)   _matrule->output(fp);
1348   if (_insencode) _insencode->output(fp);
1349   if (_opcode)    _opcode->output(fp);
1350   if (_attribs)   _attribs->output(fp);
1351   if (_predicate) _predicate->output(fp);
1352   if (_effects.Size()) {
1353     fprintf(fp,"Effects\n");
1354     _effects.dump();
1355   }
1356   if (_exprule)   _exprule->output(fp);
1357   if (_rewrule)   _rewrule->output(fp);
1358   if (_format)    _format->output(fp);
1359   if (_peephole)  _peephole->output(fp);
1360 }
1361 
1362 void MachNodeForm::dump() {
1363   output(stderr);
1364 }
1365 
1366 void MachNodeForm::output(FILE *fp) {
1367   fprintf(fp,"\nMachNode: %s\n", (_ident?_ident:""));
1368 }
1369 
1370 //------------------------------build_predicate--------------------------------
1371 // Build instruction predicates.  If the user uses the same operand name
1372 // twice, we need to check that the operands are pointer-eequivalent in
1373 // the DFA during the labeling process.
1374 Predicate *InstructForm::build_predicate() {
1375   char buf[1024], *s=buf;
1376   Dict names(cmpstr,hashstr,Form::arena);       // Map Names to counts
1377 
1378   MatchNode *mnode =
1379     strcmp(_matrule->_opType, "Set") ? _matrule : _matrule->_rChild;
1380   mnode->count_instr_names(names);
1381 
1382   uint first = 1;
1383   // Start with the predicate supplied in the .ad file.
1384   if( _predicate ) {
1385     if( first ) first=0;
1386     strcpy(s,"("); s += strlen(s);
1387     strcpy(s,_predicate->_pred);
1388     s += strlen(s);
1389     strcpy(s,")"); s += strlen(s);
1390   }
1391   for( DictI i(&names); i.test(); ++i ) {
1392     uintptr_t cnt = (uintptr_t)i._value;
1393     if( cnt > 1 ) {             // Need a predicate at all?
1394       assert( cnt == 2, "Unimplemented" );
1395       // Handle many pairs
1396       if( first ) first=0;
1397       else {                    // All tests must pass, so use '&&'
1398         strcpy(s," && ");
1399         s += strlen(s);
1400       }
1401       // Add predicate to working buffer
1402       sprintf(s,"/*%s*/(",(char*)i._key);
1403       s += strlen(s);
1404       mnode->build_instr_pred(s,(char*)i._key,0);
1405       s += strlen(s);
1406       strcpy(s," == "); s += strlen(s);
1407       mnode->build_instr_pred(s,(char*)i._key,1);
1408       s += strlen(s);
1409       strcpy(s,")"); s += strlen(s);
1410     }
1411   }
1412   if( s == buf ) s = NULL;
1413   else {
1414     assert( strlen(buf) < sizeof(buf), "String buffer overflow" );
1415     s = strdup(buf);
1416   }
1417   return new Predicate(s);
1418 }
1419 
1420 //------------------------------EncodeForm-------------------------------------
1421 // Constructor
1422 EncodeForm::EncodeForm()
1423   : _encClass(cmpstr,hashstr, Form::arena) {
1424 }
1425 EncodeForm::~EncodeForm() {
1426 }
1427 
1428 // record a new register class
1429 EncClass *EncodeForm::add_EncClass(const char *className) {
1430   EncClass *encClass = new EncClass(className);
1431   _eclasses.addName(className);
1432   _encClass.Insert(className,encClass);
1433   return encClass;
1434 }
1435 
1436 // Lookup the function body for an encoding class
1437 EncClass  *EncodeForm::encClass(const char *className) {
1438   assert( className != NULL, "Must provide a defined encoding name");
1439 
1440   EncClass *encClass = (EncClass*)_encClass[className];
1441   return encClass;
1442 }
1443 
1444 // Lookup the function body for an encoding class
1445 const char *EncodeForm::encClassBody(const char *className) {
1446   if( className == NULL ) return NULL;
1447 
1448   EncClass *encClass = (EncClass*)_encClass[className];
1449   assert( encClass != NULL, "Encode Class is missing.");
1450   encClass->_code.reset();
1451   const char *code = (const char*)encClass->_code.iter();
1452   assert( code != NULL, "Found an empty encode class body.");
1453 
1454   return code;
1455 }
1456 
1457 // Lookup the function body for an encoding class
1458 const char *EncodeForm::encClassPrototype(const char *className) {
1459   assert( className != NULL, "Encode class name must be non NULL.");
1460 
1461   return className;
1462 }
1463 
1464 void EncodeForm::dump() {                  // Debug printer
1465   output(stderr);
1466 }
1467 
1468 void EncodeForm::output(FILE *fp) {          // Write info to output files
1469   const char *name;
1470   fprintf(fp,"\n");
1471   fprintf(fp,"-------------------- Dump EncodeForm --------------------\n");
1472   for (_eclasses.reset(); (name = _eclasses.iter()) != NULL;) {
1473     ((EncClass*)_encClass[name])->output(fp);
1474   }
1475   fprintf(fp,"-------------------- end  EncodeForm --------------------\n");
1476 }
1477 //------------------------------EncClass---------------------------------------
1478 EncClass::EncClass(const char *name)
1479   : _localNames(cmpstr,hashstr, Form::arena), _name(name) {
1480 }
1481 EncClass::~EncClass() {
1482 }
1483 
1484 // Add a parameter <type,name> pair
1485 void EncClass::add_parameter(const char *parameter_type, const char *parameter_name) {
1486   _parameter_type.addName( parameter_type );
1487   _parameter_name.addName( parameter_name );
1488 }
1489 
1490 // Verify operand types in parameter list
1491 bool EncClass::check_parameter_types(FormDict &globals) {
1492   // !!!!!
1493   return false;
1494 }
1495 
1496 // Add the decomposed "code" sections of an encoding's code-block
1497 void EncClass::add_code(const char *code) {
1498   _code.addName(code);
1499 }
1500 
1501 // Add the decomposed "replacement variables" of an encoding's code-block
1502 void EncClass::add_rep_var(char *replacement_var) {
1503   _code.addName(NameList::_signal);
1504   _rep_vars.addName(replacement_var);
1505 }
1506 
1507 // Lookup the function body for an encoding class
1508 int EncClass::rep_var_index(const char *rep_var) {
1509   uint        position = 0;
1510   const char *name     = NULL;
1511 
1512   _parameter_name.reset();
1513   while ( (name = _parameter_name.iter()) != NULL ) {
1514     if ( strcmp(rep_var,name) == 0 ) return position;
1515     ++position;
1516   }
1517 
1518   return -1;
1519 }
1520 
1521 // Check after parsing
1522 bool EncClass::verify() {
1523   // 1!!!!
1524   // Check that each replacement variable, '$name' in architecture description
1525   // is actually a local variable for this encode class, or a reserved name
1526   // "primary, secondary, tertiary"
1527   return true;
1528 }
1529 
1530 void EncClass::dump() {
1531   output(stderr);
1532 }
1533 
1534 // Write info to output files
1535 void EncClass::output(FILE *fp) {
1536   fprintf(fp,"EncClass: %s", (_name ? _name : ""));
1537 
1538   // Output the parameter list
1539   _parameter_type.reset();
1540   _parameter_name.reset();
1541   const char *type = _parameter_type.iter();
1542   const char *name = _parameter_name.iter();
1543   fprintf(fp, " ( ");
1544   for ( ; (type != NULL) && (name != NULL);
1545         (type = _parameter_type.iter()), (name = _parameter_name.iter()) ) {
1546     fprintf(fp, " %s %s,", type, name);
1547   }
1548   fprintf(fp, " ) ");
1549 
1550   // Output the code block
1551   _code.reset();
1552   _rep_vars.reset();
1553   const char *code;
1554   while ( (code = _code.iter()) != NULL ) {
1555     if ( _code.is_signal(code) ) {
1556       // A replacement variable
1557       const char *rep_var = _rep_vars.iter();
1558       fprintf(fp,"($%s)", rep_var);
1559     } else {
1560       // A section of code
1561       fprintf(fp,"%s", code);
1562     }
1563   }
1564 
1565 }
1566 
1567 //------------------------------Opcode-----------------------------------------
1568 Opcode::Opcode(char *primary, char *secondary, char *tertiary)
1569   : _primary(primary), _secondary(secondary), _tertiary(tertiary) {
1570 }
1571 
1572 Opcode::~Opcode() {
1573 }
1574 
1575 Opcode::opcode_type Opcode::as_opcode_type(const char *param) {
1576   if( strcmp(param,"primary") == 0 ) {
1577     return Opcode::PRIMARY;
1578   }
1579   else if( strcmp(param,"secondary") == 0 ) {
1580     return Opcode::SECONDARY;
1581   }
1582   else if( strcmp(param,"tertiary") == 0 ) {
1583     return Opcode::TERTIARY;
1584   }
1585   return Opcode::NOT_AN_OPCODE;
1586 }
1587 
1588 bool Opcode::print_opcode(FILE *fp, Opcode::opcode_type desired_opcode) {
1589   // Default values previously provided by MachNode::primary()...
1590   const char *description = NULL;
1591   const char *value       = NULL;
1592   // Check if user provided any opcode definitions
1593   if( this != NULL ) {
1594     // Update 'value' if user provided a definition in the instruction
1595     switch (desired_opcode) {
1596     case PRIMARY:
1597       description = "primary()";
1598       if( _primary   != NULL)  { value = _primary;     }
1599       break;
1600     case SECONDARY:
1601       description = "secondary()";
1602       if( _secondary != NULL ) { value = _secondary;   }
1603       break;
1604     case TERTIARY:
1605       description = "tertiary()";
1606       if( _tertiary  != NULL ) { value = _tertiary;    }
1607       break;
1608     default:
1609       assert( false, "ShouldNotReachHere();");
1610       break;
1611     }
1612   }
1613   if (value != NULL) {
1614     fprintf(fp, "(%s /*%s*/)", value, description);
1615   }
1616   return value != NULL;
1617 }
1618 
1619 void Opcode::dump() {
1620   output(stderr);
1621 }
1622 
1623 // Write info to output files
1624 void Opcode::output(FILE *fp) {
1625   if (_primary   != NULL) fprintf(fp,"Primary   opcode: %s\n", _primary);
1626   if (_secondary != NULL) fprintf(fp,"Secondary opcode: %s\n", _secondary);
1627   if (_tertiary  != NULL) fprintf(fp,"Tertiary  opcode: %s\n", _tertiary);
1628 }
1629 
1630 //------------------------------InsEncode--------------------------------------
1631 InsEncode::InsEncode() {
1632 }
1633 InsEncode::~InsEncode() {
1634 }
1635 
1636 // Add "encode class name" and its parameters
1637 NameAndList *InsEncode::add_encode(char *encoding) {
1638   assert( encoding != NULL, "Must provide name for encoding");
1639 
1640   // add_parameter(NameList::_signal);
1641   NameAndList *encode = new NameAndList(encoding);
1642   _encoding.addName((char*)encode);
1643 
1644   return encode;
1645 }
1646 
1647 // Access the list of encodings
1648 void InsEncode::reset() {
1649   _encoding.reset();
1650   // _parameter.reset();
1651 }
1652 const char* InsEncode::encode_class_iter() {
1653   NameAndList  *encode_class = (NameAndList*)_encoding.iter();
1654   return  ( encode_class != NULL ? encode_class->name() : NULL );
1655 }
1656 // Obtain parameter name from zero based index
1657 const char *InsEncode::rep_var_name(InstructForm &inst, uint param_no) {
1658   NameAndList *params = (NameAndList*)_encoding.current();
1659   assert( params != NULL, "Internal Error");
1660   const char *param = (*params)[param_no];
1661 
1662   // Remove '$' if parser placed it there.
1663   return ( param != NULL && *param == '$') ? (param+1) : param;
1664 }
1665 
1666 void InsEncode::dump() {
1667   output(stderr);
1668 }
1669 
1670 // Write info to output files
1671 void InsEncode::output(FILE *fp) {
1672   NameAndList *encoding  = NULL;
1673   const char  *parameter = NULL;
1674 
1675   fprintf(fp,"InsEncode: ");
1676   _encoding.reset();
1677 
1678   while ( (encoding = (NameAndList*)_encoding.iter()) != 0 ) {
1679     // Output the encoding being used
1680     fprintf(fp,"%s(", encoding->name() );
1681 
1682     // Output its parameter list, if any
1683     bool first_param = true;
1684     encoding->reset();
1685     while (  (parameter = encoding->iter()) != 0 ) {
1686       // Output the ',' between parameters
1687       if ( ! first_param )  fprintf(fp,", ");
1688       first_param = false;
1689       // Output the parameter
1690       fprintf(fp,"%s", parameter);
1691     } // done with parameters
1692     fprintf(fp,")  ");
1693   } // done with encodings
1694 
1695   fprintf(fp,"\n");
1696 }
1697 
1698 //------------------------------Effect-----------------------------------------
1699 static int effect_lookup(const char *name) {
1700   if(!strcmp(name, "USE")) return Component::USE;
1701   if(!strcmp(name, "DEF")) return Component::DEF;
1702   if(!strcmp(name, "USE_DEF")) return Component::USE_DEF;
1703   if(!strcmp(name, "KILL")) return Component::KILL;
1704   if(!strcmp(name, "USE_KILL")) return Component::USE_KILL;
1705   if(!strcmp(name, "TEMP")) return Component::TEMP;
1706   if(!strcmp(name, "INVALID")) return Component::INVALID;
1707   assert( false,"Invalid effect name specified\n");
1708   return Component::INVALID;
1709 }
1710 
1711 Effect::Effect(const char *name) : _name(name), _use_def(effect_lookup(name)) {
1712   _ftype = Form::EFF;
1713 }
1714 Effect::~Effect() {
1715 }
1716 
1717 // Dynamic type check
1718 Effect *Effect::is_effect() const {
1719   return (Effect*)this;
1720 }
1721 
1722 
1723 // True if this component is equal to the parameter.
1724 bool Effect::is(int use_def_kill_enum) const {
1725   return (_use_def == use_def_kill_enum ? true : false);
1726 }
1727 // True if this component is used/def'd/kill'd as the parameter suggests.
1728 bool Effect::isa(int use_def_kill_enum) const {
1729   return (_use_def & use_def_kill_enum) == use_def_kill_enum;
1730 }
1731 
1732 void Effect::dump() {
1733   output(stderr);
1734 }
1735 
1736 void Effect::output(FILE *fp) {          // Write info to output files
1737   fprintf(fp,"Effect: %s\n", (_name?_name:""));
1738 }
1739 
1740 //------------------------------ExpandRule-------------------------------------
1741 ExpandRule::ExpandRule() : _expand_instrs(),
1742                            _newopconst(cmpstr, hashstr, Form::arena) {
1743   _ftype = Form::EXP;
1744 }
1745 
1746 ExpandRule::~ExpandRule() {                  // Destructor
1747 }
1748 
1749 void ExpandRule::add_instruction(NameAndList *instruction_name_and_operand_list) {
1750   _expand_instrs.addName((char*)instruction_name_and_operand_list);
1751 }
1752 
1753 void ExpandRule::reset_instructions() {
1754   _expand_instrs.reset();
1755 }
1756 
1757 NameAndList* ExpandRule::iter_instructions() {
1758   return (NameAndList*)_expand_instrs.iter();
1759 }
1760 
1761 
1762 void ExpandRule::dump() {
1763   output(stderr);
1764 }
1765 
1766 void ExpandRule::output(FILE *fp) {         // Write info to output files
1767   NameAndList *expand_instr = NULL;
1768   const char *opid = NULL;
1769 
1770   fprintf(fp,"\nExpand Rule:\n");
1771 
1772   // Iterate over the instructions 'node' expands into
1773   for(reset_instructions(); (expand_instr = iter_instructions()) != NULL; ) {
1774     fprintf(fp,"%s(", expand_instr->name());
1775 
1776     // iterate over the operand list
1777     for( expand_instr->reset(); (opid = expand_instr->iter()) != NULL; ) {
1778       fprintf(fp,"%s ", opid);
1779     }
1780     fprintf(fp,");\n");
1781   }
1782 }
1783 
1784 //------------------------------RewriteRule------------------------------------
1785 RewriteRule::RewriteRule(char* params, char* block)
1786   : _tempParams(params), _tempBlock(block) { };  // Constructor
1787 RewriteRule::~RewriteRule() {                 // Destructor
1788 }
1789 
1790 void RewriteRule::dump() {
1791   output(stderr);
1792 }
1793 
1794 void RewriteRule::output(FILE *fp) {         // Write info to output files
1795   fprintf(fp,"\nRewrite Rule:\n%s\n%s\n",
1796           (_tempParams?_tempParams:""),
1797           (_tempBlock?_tempBlock:""));
1798 }
1799 
1800 
1801 //==============================MachNodes======================================
1802 //------------------------------MachNodeForm-----------------------------------
1803 MachNodeForm::MachNodeForm(char *id)
1804   : _ident(id) {
1805 }
1806 
1807 MachNodeForm::~MachNodeForm() {
1808 }
1809 
1810 MachNodeForm *MachNodeForm::is_machnode() const {
1811   return (MachNodeForm*)this;
1812 }
1813 
1814 //==============================Operand Classes================================
1815 //------------------------------OpClassForm------------------------------------
1816 OpClassForm::OpClassForm(const char* id) : _ident(id) {
1817   _ftype = Form::OPCLASS;
1818 }
1819 
1820 OpClassForm::~OpClassForm() {
1821 }
1822 
1823 bool OpClassForm::ideal_only() const { return 0; }
1824 
1825 OpClassForm *OpClassForm::is_opclass() const {
1826   return (OpClassForm*)this;
1827 }
1828 
1829 Form::InterfaceType OpClassForm::interface_type(FormDict &globals) const {
1830   if( _oplst.count() == 0 ) return Form::no_interface;
1831 
1832   // Check that my operands have the same interface type
1833   Form::InterfaceType  interface;
1834   bool  first = true;
1835   NameList &op_list = (NameList &)_oplst;
1836   op_list.reset();
1837   const char *op_name;
1838   while( (op_name = op_list.iter()) != NULL ) {
1839     const Form  *form    = globals[op_name];
1840     OperandForm *operand = form->is_operand();
1841     assert( operand, "Entry in operand class that is not an operand");
1842     if( first ) {
1843       first     = false;
1844       interface = operand->interface_type(globals);
1845     } else {
1846       interface = (interface == operand->interface_type(globals) ? interface : Form::no_interface);
1847     }
1848   }
1849   return interface;
1850 }
1851 
1852 bool OpClassForm::stack_slots_only(FormDict &globals) const {
1853   if( _oplst.count() == 0 ) return false;  // how?
1854 
1855   NameList &op_list = (NameList &)_oplst;
1856   op_list.reset();
1857   const char *op_name;
1858   while( (op_name = op_list.iter()) != NULL ) {
1859     const Form  *form    = globals[op_name];
1860     OperandForm *operand = form->is_operand();
1861     assert( operand, "Entry in operand class that is not an operand");
1862     if( !operand->stack_slots_only(globals) )  return false;
1863   }
1864   return true;
1865 }
1866 
1867 
1868 void OpClassForm::dump() {
1869   output(stderr);
1870 }
1871 
1872 void OpClassForm::output(FILE *fp) {
1873   const char *name;
1874   fprintf(fp,"\nOperand Class: %s\n", (_ident?_ident:""));
1875   fprintf(fp,"\nCount = %d\n", _oplst.count());
1876   for(_oplst.reset(); (name = _oplst.iter()) != NULL;) {
1877     fprintf(fp,"%s, ",name);
1878   }
1879   fprintf(fp,"\n");
1880 }
1881 
1882 
1883 //==============================Operands=======================================
1884 //------------------------------OperandForm------------------------------------
1885 OperandForm::OperandForm(const char* id)
1886   : OpClassForm(id), _ideal_only(false),
1887     _localNames(cmpstr, hashstr, Form::arena) {
1888       _ftype = Form::OPER;
1889 
1890       _matrule   = NULL;
1891       _interface = NULL;
1892       _attribs   = NULL;
1893       _predicate = NULL;
1894       _constraint= NULL;
1895       _construct = NULL;
1896       _format    = NULL;
1897 }
1898 OperandForm::OperandForm(const char* id, bool ideal_only)
1899   : OpClassForm(id), _ideal_only(ideal_only),
1900     _localNames(cmpstr, hashstr, Form::arena) {
1901       _ftype = Form::OPER;
1902 
1903       _matrule   = NULL;
1904       _interface = NULL;
1905       _attribs   = NULL;
1906       _predicate = NULL;
1907       _constraint= NULL;
1908       _construct = NULL;
1909       _format    = NULL;
1910 }
1911 OperandForm::~OperandForm() {
1912 }
1913 
1914 
1915 OperandForm *OperandForm::is_operand() const {
1916   return (OperandForm*)this;
1917 }
1918 
1919 bool OperandForm::ideal_only() const {
1920   return _ideal_only;
1921 }
1922 
1923 Form::InterfaceType OperandForm::interface_type(FormDict &globals) const {
1924   if( _interface == NULL )  return Form::no_interface;
1925 
1926   return _interface->interface_type(globals);
1927 }
1928 
1929 
1930 bool OperandForm::stack_slots_only(FormDict &globals) const {
1931   if( _constraint == NULL )  return false;
1932   return _constraint->stack_slots_only();
1933 }
1934 
1935 
1936 // Access op_cost attribute or return NULL.
1937 const char* OperandForm::cost() {
1938   for (Attribute* cur = _attribs; cur != NULL; cur = (Attribute*)cur->_next) {
1939     if( strcmp(cur->_ident,AttributeForm::_op_cost) == 0 ) {
1940       return cur->_val;
1941     }
1942   }
1943   return NULL;
1944 }
1945 
1946 // Return the number of leaves below this complex operand
1947 uint OperandForm::num_leaves() const {
1948   if ( ! _matrule) return 0;
1949 
1950   int num_leaves = _matrule->_numleaves;
1951   return num_leaves;
1952 }
1953 
1954 // Return the number of constants contained within this complex operand
1955 uint OperandForm::num_consts(FormDict &globals) const {
1956   if ( ! _matrule) return 0;
1957 
1958   // This is a recursive invocation on all operands in the matchrule
1959   return _matrule->num_consts(globals);
1960 }
1961 
1962 // Return the number of constants in match rule with specified type
1963 uint OperandForm::num_consts(FormDict &globals, Form::DataType type) const {
1964   if ( ! _matrule) return 0;
1965 
1966   // This is a recursive invocation on all operands in the matchrule
1967   return _matrule->num_consts(globals, type);
1968 }
1969 
1970 // Return the number of pointer constants contained within this complex operand
1971 uint OperandForm::num_const_ptrs(FormDict &globals) const {
1972   if ( ! _matrule) return 0;
1973 
1974   // This is a recursive invocation on all operands in the matchrule
1975   return _matrule->num_const_ptrs(globals);
1976 }
1977 
1978 uint OperandForm::num_edges(FormDict &globals) const {
1979   uint edges  = 0;
1980   uint leaves = num_leaves();
1981   uint consts = num_consts(globals);
1982 
1983   // If we are matching a constant directly, there are no leaves.
1984   edges = ( leaves > consts ) ? leaves - consts : 0;
1985 
1986   // !!!!!
1987   // Special case operands that do not have a corresponding ideal node.
1988   if( (edges == 0) && (consts == 0) ) {
1989     if( constrained_reg_class() != NULL ) {
1990       edges = 1;
1991     } else {
1992       if( _matrule
1993           && (_matrule->_lChild == NULL) && (_matrule->_rChild == NULL) ) {
1994         const Form *form = globals[_matrule->_opType];
1995         OperandForm *oper = form ? form->is_operand() : NULL;
1996         if( oper ) {
1997           return oper->num_edges(globals);
1998         }
1999       }
2000     }
2001   }
2002 
2003   return edges;
2004 }
2005 
2006 
2007 // Check if this operand is usable for cisc-spilling
2008 bool  OperandForm::is_cisc_reg(FormDict &globals) const {
2009   const char *ideal = ideal_type(globals);
2010   bool is_cisc_reg = (ideal && (ideal_to_Reg_type(ideal) != none));
2011   return is_cisc_reg;
2012 }
2013 
2014 bool  OpClassForm::is_cisc_mem(FormDict &globals) const {
2015   Form::InterfaceType my_interface = interface_type(globals);
2016   return (my_interface == memory_interface);
2017 }
2018 
2019 
2020 // node matches ideal 'Bool'
2021 bool OperandForm::is_ideal_bool() const {
2022   if( _matrule == NULL ) return false;
2023 
2024   return _matrule->is_ideal_bool();
2025 }
2026 
2027 // Require user's name for an sRegX to be stackSlotX
2028 Form::DataType OperandForm::is_user_name_for_sReg() const {
2029   DataType data_type = none;
2030   if( _ident != NULL ) {
2031     if(      strcmp(_ident,"stackSlotI") == 0 ) data_type = Form::idealI;
2032     else if( strcmp(_ident,"stackSlotP") == 0 ) data_type = Form::idealP;
2033     else if( strcmp(_ident,"stackSlotD") == 0 ) data_type = Form::idealD;
2034     else if( strcmp(_ident,"stackSlotF") == 0 ) data_type = Form::idealF;
2035     else if( strcmp(_ident,"stackSlotL") == 0 ) data_type = Form::idealL;
2036   }
2037   assert((data_type == none) || (_matrule == NULL), "No match-rule for stackSlotX");
2038 
2039   return data_type;
2040 }
2041 
2042 
2043 // Return ideal type, if there is a single ideal type for this operand
2044 const char *OperandForm::ideal_type(FormDict &globals, RegisterForm *registers) const {
2045   const char *type = NULL;
2046   if (ideal_only()) type = _ident;
2047   else if( _matrule == NULL ) {
2048     // Check for condition code register
2049     const char *rc_name = constrained_reg_class();
2050     // !!!!!
2051     if (rc_name == NULL) return NULL;
2052     // !!!!! !!!!!
2053     // Check constraints on result's register class
2054     if( registers ) {
2055       RegClass *reg_class  = registers->getRegClass(rc_name);
2056       assert( reg_class != NULL, "Register class is not defined");
2057 
2058       // Check for ideal type of entries in register class, all are the same type
2059       reg_class->reset();
2060       RegDef *reg_def = reg_class->RegDef_iter();
2061       assert( reg_def != NULL, "No entries in register class");
2062       assert( reg_def->_idealtype != NULL, "Did not define ideal type for register");
2063       // Return substring that names the register's ideal type
2064       type = reg_def->_idealtype + 3;
2065       assert( *(reg_def->_idealtype + 0) == 'O', "Expect Op_ prefix");
2066       assert( *(reg_def->_idealtype + 1) == 'p', "Expect Op_ prefix");
2067       assert( *(reg_def->_idealtype + 2) == '_', "Expect Op_ prefix");
2068     }
2069   }
2070   else if( _matrule->_lChild == NULL && _matrule->_rChild == NULL ) {
2071     // This operand matches a single type, at the top level.
2072     // Check for ideal type
2073     type = _matrule->_opType;
2074     if( strcmp(type,"Bool") == 0 )
2075       return "Bool";
2076     // transitive lookup
2077     const Form *frm = globals[type];
2078     OperandForm *op = frm->is_operand();
2079     type = op->ideal_type(globals, registers);
2080   }
2081   return type;
2082 }
2083 
2084 
2085 // If there is a single ideal type for this interface field, return it.
2086 const char *OperandForm::interface_ideal_type(FormDict &globals,
2087                                               const char *field) const {
2088   const char  *ideal_type = NULL;
2089   const char  *value      = NULL;
2090 
2091   // Check if "field" is valid for this operand's interface
2092   if ( ! is_interface_field(field, value) )   return ideal_type;
2093 
2094   // !!!!! !!!!! !!!!!
2095   // If a valid field has a constant value, identify "ConI" or "ConP" or ...
2096 
2097   // Else, lookup type of field's replacement variable
2098 
2099   return ideal_type;
2100 }
2101 
2102 
2103 RegClass* OperandForm::get_RegClass() const {
2104   if (_interface && !_interface->is_RegInterface()) return NULL;
2105   return globalAD->get_registers()->getRegClass(constrained_reg_class());
2106 }
2107 
2108 
2109 bool OperandForm::is_bound_register() const {
2110   RegClass *reg_class  = get_RegClass();
2111   if (reg_class == NULL) return false;
2112 
2113   const char * name = ideal_type(globalAD->globalNames());
2114   if (name == NULL) return false;
2115 
2116   int size = 0;
2117   if (strcmp(name,"RegFlags")==0) size =  1;
2118   if (strcmp(name,"RegI")==0) size =  1;
2119   if (strcmp(name,"RegF")==0) size =  1;
2120   if (strcmp(name,"RegD")==0) size =  2;
2121   if (strcmp(name,"RegL")==0) size =  2;
2122   if (strcmp(name,"RegN")==0) size =  1;
2123   if (strcmp(name,"RegP")==0) size =  globalAD->get_preproc_def("_LP64") ? 2 : 1;
2124   if (size == 0) return false;
2125   return size == reg_class->size();
2126 }
2127 
2128 
2129 // Check if this is a valid field for this operand,
2130 // Return 'true' if valid, and set the value to the string the user provided.
2131 bool  OperandForm::is_interface_field(const char *field,
2132                                       const char * &value) const {
2133   return false;
2134 }
2135 
2136 
2137 // Return register class name if a constraint specifies the register class.
2138 const char *OperandForm::constrained_reg_class() const {
2139   const char *reg_class  = NULL;
2140   if ( _constraint ) {
2141     // !!!!!
2142     Constraint *constraint = _constraint;
2143     if ( strcmp(_constraint->_func,"ALLOC_IN_RC") == 0 ) {
2144       reg_class = _constraint->_arg;
2145     }
2146   }
2147 
2148   return reg_class;
2149 }
2150 
2151 
2152 // Return the register class associated with 'leaf'.
2153 const char *OperandForm::in_reg_class(uint leaf, FormDict &globals) {
2154   const char *reg_class = NULL; // "RegMask::Empty";
2155 
2156   if((_matrule == NULL) || (_matrule->is_chain_rule(globals))) {
2157     reg_class = constrained_reg_class();
2158     return reg_class;
2159   }
2160   const char *result   = NULL;
2161   const char *name     = NULL;
2162   const char *type     = NULL;
2163   // iterate through all base operands
2164   // until we reach the register that corresponds to "leaf"
2165   // This function is not looking for an ideal type.  It needs the first
2166   // level user type associated with the leaf.
2167   for(uint idx = 0;_matrule->base_operand(idx,globals,result,name,type);++idx) {
2168     const Form *form = (_localNames[name] ? _localNames[name] : globals[result]);
2169     OperandForm *oper = form ? form->is_operand() : NULL;
2170     if( oper ) {
2171       reg_class = oper->constrained_reg_class();
2172       if( reg_class ) {
2173         reg_class = reg_class;
2174       } else {
2175         // ShouldNotReachHere();
2176       }
2177     } else {
2178       // ShouldNotReachHere();
2179     }
2180 
2181     // Increment our target leaf position if current leaf is not a candidate.
2182     if( reg_class == NULL)    ++leaf;
2183     // Exit the loop with the value of reg_class when at the correct index
2184     if( idx == leaf )         break;
2185     // May iterate through all base operands if reg_class for 'leaf' is NULL
2186   }
2187   return reg_class;
2188 }
2189 
2190 
2191 // Recursive call to construct list of top-level operands.
2192 // Implementation does not modify state of internal structures
2193 void OperandForm::build_components() {
2194   if (_matrule)  _matrule->append_components(_localNames, _components);
2195 
2196   // Add parameters that "do not appear in match rule".
2197   const char *name;
2198   for (_parameters.reset(); (name = _parameters.iter()) != NULL;) {
2199     OperandForm *opForm = (OperandForm*)_localNames[name];
2200 
2201     if ( _components.operand_position(name) == -1 ) {
2202       _components.insert(name, opForm->_ident, Component::INVALID, false);
2203     }
2204   }
2205 
2206   return;
2207 }
2208 
2209 int OperandForm::operand_position(const char *name, int usedef) {
2210   return _components.operand_position(name, usedef);
2211 }
2212 
2213 
2214 // Return zero-based position in component list, only counting constants;
2215 // Return -1 if not in list.
2216 int OperandForm::constant_position(FormDict &globals, const Component *last) {
2217   // Iterate through components and count constants preceding 'constant'
2218   int position = 0;
2219   Component *comp;
2220   _components.reset();
2221   while( (comp = _components.iter()) != NULL  && (comp != last) ) {
2222     // Special case for operands that take a single user-defined operand
2223     // Skip the initial definition in the component list.
2224     if( strcmp(comp->_name,this->_ident) == 0 ) continue;
2225 
2226     const char *type = comp->_type;
2227     // Lookup operand form for replacement variable's type
2228     const Form *form = globals[type];
2229     assert( form != NULL, "Component's type not found");
2230     OperandForm *oper = form ? form->is_operand() : NULL;
2231     if( oper ) {
2232       if( oper->_matrule->is_base_constant(globals) != Form::none ) {
2233         ++position;
2234       }
2235     }
2236   }
2237 
2238   // Check for being passed a component that was not in the list
2239   if( comp != last )  position = -1;
2240 
2241   return position;
2242 }
2243 // Provide position of constant by "name"
2244 int OperandForm::constant_position(FormDict &globals, const char *name) {
2245   const Component *comp = _components.search(name);
2246   int idx = constant_position( globals, comp );
2247 
2248   return idx;
2249 }
2250 
2251 
2252 // Return zero-based position in component list, only counting constants;
2253 // Return -1 if not in list.
2254 int OperandForm::register_position(FormDict &globals, const char *reg_name) {
2255   // Iterate through components and count registers preceding 'last'
2256   uint  position = 0;
2257   Component *comp;
2258   _components.reset();
2259   while( (comp = _components.iter()) != NULL
2260          && (strcmp(comp->_name,reg_name) != 0) ) {
2261     // Special case for operands that take a single user-defined operand
2262     // Skip the initial definition in the component list.
2263     if( strcmp(comp->_name,this->_ident) == 0 ) continue;
2264 
2265     const char *type = comp->_type;
2266     // Lookup operand form for component's type
2267     const Form *form = globals[type];
2268     assert( form != NULL, "Component's type not found");
2269     OperandForm *oper = form ? form->is_operand() : NULL;
2270     if( oper ) {
2271       if( oper->_matrule->is_base_register(globals) ) {
2272         ++position;
2273       }
2274     }
2275   }
2276 
2277   return position;
2278 }
2279 
2280 
2281 const char *OperandForm::reduce_result()  const {
2282   return _ident;
2283 }
2284 // Return the name of the operand on the right hand side of the binary match
2285 // Return NULL if there is no right hand side
2286 const char *OperandForm::reduce_right(FormDict &globals)  const {
2287   return  ( _matrule ? _matrule->reduce_right(globals) : NULL );
2288 }
2289 
2290 // Similar for left
2291 const char *OperandForm::reduce_left(FormDict &globals)   const {
2292   return  ( _matrule ? _matrule->reduce_left(globals) : NULL );
2293 }
2294 
2295 
2296 // --------------------------- FILE *output_routines
2297 //
2298 // Output code for disp_is_oop, if true.
2299 void OperandForm::disp_is_oop(FILE *fp, FormDict &globals) {
2300   //  Check it is a memory interface with a non-user-constant disp field
2301   if ( this->_interface == NULL ) return;
2302   MemInterface *mem_interface = this->_interface->is_MemInterface();
2303   if ( mem_interface == NULL )    return;
2304   const char   *disp  = mem_interface->_disp;
2305   if ( *disp != '$' )             return;
2306 
2307   // Lookup replacement variable in operand's component list
2308   const char   *rep_var = disp + 1;
2309   const Component *comp = this->_components.search(rep_var);
2310   assert( comp != NULL, "Replacement variable not found in components");
2311   // Lookup operand form for replacement variable's type
2312   const char      *type = comp->_type;
2313   Form            *form = (Form*)globals[type];
2314   assert( form != NULL, "Replacement variable's type not found");
2315   OperandForm     *op   = form->is_operand();
2316   assert( op, "Memory Interface 'disp' can only emit an operand form");
2317   // Check if this is a ConP, which may require relocation
2318   if ( op->is_base_constant(globals) == Form::idealP ) {
2319     // Find the constant's index:  _c0, _c1, _c2, ... , _cN
2320     uint idx  = op->constant_position( globals, rep_var);
2321     fprintf(fp,"  virtual bool disp_is_oop() const {");
2322     fprintf(fp,  "  return _c%d->isa_oop_ptr();", idx);
2323     fprintf(fp, " }\n");
2324   }
2325 }
2326 
2327 // Generate code for internal and external format methods
2328 //
2329 // internal access to reg# node->_idx
2330 // access to subsumed constant _c0, _c1,
2331 void  OperandForm::int_format(FILE *fp, FormDict &globals, uint index) {
2332   Form::DataType dtype;
2333   if (_matrule && (_matrule->is_base_register(globals) ||
2334                    strcmp(ideal_type(globalAD->globalNames()), "RegFlags") == 0)) {
2335     // !!!!! !!!!!
2336     fprintf(fp,    "{ char reg_str[128];\n");
2337     fprintf(fp,"      ra->dump_register(node,reg_str);\n");
2338     fprintf(fp,"      tty->print(\"%cs\",reg_str);\n",'%');
2339     fprintf(fp,"    }\n");
2340   } else if (_matrule && (dtype = _matrule->is_base_constant(globals)) != Form::none) {
2341     format_constant( fp, index, dtype );
2342   } else if (ideal_to_sReg_type(_ident) != Form::none) {
2343     // Special format for Stack Slot Register
2344     fprintf(fp,    "{ char reg_str[128];\n");
2345     fprintf(fp,"      ra->dump_register(node,reg_str);\n");
2346     fprintf(fp,"      tty->print(\"%cs\",reg_str);\n",'%');
2347     fprintf(fp,"    }\n");
2348   } else {
2349     fprintf(fp,"tty->print(\"No format defined for %s\n\");\n", _ident);
2350     fflush(fp);
2351     fprintf(stderr,"No format defined for %s\n", _ident);
2352     dump();
2353     assert( false,"Internal error:\n  output_internal_operand() attempting to output other than a Register or Constant");
2354   }
2355 }
2356 
2357 // Similar to "int_format" but for cases where data is external to operand
2358 // external access to reg# node->in(idx)->_idx,
2359 void  OperandForm::ext_format(FILE *fp, FormDict &globals, uint index) {
2360   Form::DataType dtype;
2361   if (_matrule && (_matrule->is_base_register(globals) ||
2362                    strcmp(ideal_type(globalAD->globalNames()), "RegFlags") == 0)) {
2363     fprintf(fp,    "{ char reg_str[128];\n");
2364     fprintf(fp,"      ra->dump_register(node->in(idx");
2365     if ( index != 0 ) fprintf(fp,                  "+%d",index);
2366     fprintf(fp,                                       "),reg_str);\n");
2367     fprintf(fp,"      tty->print(\"%cs\",reg_str);\n",'%');
2368     fprintf(fp,"    }\n");
2369   } else if (_matrule && (dtype = _matrule->is_base_constant(globals)) != Form::none) {
2370     format_constant( fp, index, dtype );
2371   } else if (ideal_to_sReg_type(_ident) != Form::none) {
2372     // Special format for Stack Slot Register
2373     fprintf(fp,    "{ char reg_str[128];\n");
2374     fprintf(fp,"      ra->dump_register(node->in(idx");
2375     if ( index != 0 ) fprintf(fp,                  "+%d",index);
2376     fprintf(fp,                                       "),reg_str);\n");
2377     fprintf(fp,"      tty->print(\"%cs\",reg_str);\n",'%');
2378     fprintf(fp,"    }\n");
2379   } else {
2380     fprintf(fp,"tty->print(\"No format defined for %s\n\");\n", _ident);
2381     assert( false,"Internal error:\n  output_external_operand() attempting to output other than a Register or Constant");
2382   }
2383 }
2384 
2385 void OperandForm::format_constant(FILE *fp, uint const_index, uint const_type) {
2386   switch(const_type) {
2387   case Form::idealI:  fprintf(fp,"st->print(\"#%%d\", _c%d);\n", const_index); break;
2388   case Form::idealP:  fprintf(fp,"_c%d->dump_on(st);\n",         const_index); break;
2389   case Form::idealN:  fprintf(fp,"_c%d->dump_on(st);\n",         const_index); break;
2390   case Form::idealL:  fprintf(fp,"st->print(\"#%%lld\", _c%d);\n", const_index); break;
2391   case Form::idealF:  fprintf(fp,"st->print(\"#%%f\", _c%d);\n", const_index); break;
2392   case Form::idealD:  fprintf(fp,"st->print(\"#%%f\", _c%d);\n", const_index); break;
2393   default:
2394     assert( false, "ShouldNotReachHere()");
2395   }
2396 }
2397 
2398 // Return the operand form corresponding to the given index, else NULL.
2399 OperandForm *OperandForm::constant_operand(FormDict &globals,
2400                                            uint      index) {
2401   // !!!!!
2402   // Check behavior on complex operands
2403   uint n_consts = num_consts(globals);
2404   if( n_consts > 0 ) {
2405     uint i = 0;
2406     const char *type;
2407     Component  *comp;
2408     _components.reset();
2409     if ((comp = _components.iter()) == NULL) {
2410       assert(n_consts == 1, "Bad component list detected.\n");
2411       // Current operand is THE operand
2412       if ( index == 0 ) {
2413         return this;
2414       }
2415     } // end if NULL
2416     else {
2417       // Skip the first component, it can not be a DEF of a constant
2418       do {
2419         type = comp->base_type(globals);
2420         // Check that "type" is a 'ConI', 'ConP', ...
2421         if ( ideal_to_const_type(type) != Form::none ) {
2422           // When at correct component, get corresponding Operand
2423           if ( index == 0 ) {
2424             return globals[comp->_type]->is_operand();
2425           }
2426           // Decrement number of constants to go
2427           --index;
2428         }
2429       } while((comp = _components.iter()) != NULL);
2430     }
2431   }
2432 
2433   // Did not find a constant for this index.
2434   return NULL;
2435 }
2436 
2437 // If this operand has a single ideal type, return its type
2438 Form::DataType OperandForm::simple_type(FormDict &globals) const {
2439   const char *type_name = ideal_type(globals);
2440   Form::DataType type   = type_name ? ideal_to_const_type( type_name )
2441                                     : Form::none;
2442   return type;
2443 }
2444 
2445 Form::DataType OperandForm::is_base_constant(FormDict &globals) const {
2446   if ( _matrule == NULL )    return Form::none;
2447 
2448   return _matrule->is_base_constant(globals);
2449 }
2450 
2451 // "true" if this operand is a simple type that is swallowed
2452 bool  OperandForm::swallowed(FormDict &globals) const {
2453   Form::DataType type   = simple_type(globals);
2454   if( type != Form::none ) {
2455     return true;
2456   }
2457 
2458   return false;
2459 }
2460 
2461 // Output code to access the value of the index'th constant
2462 void OperandForm::access_constant(FILE *fp, FormDict &globals,
2463                                   uint const_index) {
2464   OperandForm *oper = constant_operand(globals, const_index);
2465   assert( oper, "Index exceeds number of constants in operand");
2466   Form::DataType dtype = oper->is_base_constant(globals);
2467 
2468   switch(dtype) {
2469   case idealI: fprintf(fp,"_c%d",           const_index); break;
2470   case idealP: fprintf(fp,"_c%d->get_con()",const_index); break;
2471   case idealL: fprintf(fp,"_c%d",           const_index); break;
2472   case idealF: fprintf(fp,"_c%d",           const_index); break;
2473   case idealD: fprintf(fp,"_c%d",           const_index); break;
2474   default:
2475     assert( false, "ShouldNotReachHere()");
2476   }
2477 }
2478 
2479 
2480 void OperandForm::dump() {
2481   output(stderr);
2482 }
2483 
2484 void OperandForm::output(FILE *fp) {
2485   fprintf(fp,"\nOperand: %s\n", (_ident?_ident:""));
2486   if (_matrule)    _matrule->dump();
2487   if (_interface)  _interface->dump();
2488   if (_attribs)    _attribs->dump();
2489   if (_predicate)  _predicate->dump();
2490   if (_constraint) _constraint->dump();
2491   if (_construct)  _construct->dump();
2492   if (_format)     _format->dump();
2493 }
2494 
2495 //------------------------------Constraint-------------------------------------
2496 Constraint::Constraint(const char *func, const char *arg)
2497   : _func(func), _arg(arg) {
2498 }
2499 Constraint::~Constraint() { /* not owner of char* */
2500 }
2501 
2502 bool Constraint::stack_slots_only() const {
2503   return strcmp(_func, "ALLOC_IN_RC") == 0
2504       && strcmp(_arg,  "stack_slots") == 0;
2505 }
2506 
2507 void Constraint::dump() {
2508   output(stderr);
2509 }
2510 
2511 void Constraint::output(FILE *fp) {           // Write info to output files
2512   assert((_func != NULL && _arg != NULL),"missing constraint function or arg");
2513   fprintf(fp,"Constraint: %s ( %s )\n", _func, _arg);
2514 }
2515 
2516 //------------------------------Predicate--------------------------------------
2517 Predicate::Predicate(char *pr)
2518   : _pred(pr) {
2519 }
2520 Predicate::~Predicate() {
2521 }
2522 
2523 void Predicate::dump() {
2524   output(stderr);
2525 }
2526 
2527 void Predicate::output(FILE *fp) {
2528   fprintf(fp,"Predicate");  // Write to output files
2529 }
2530 //------------------------------Interface--------------------------------------
2531 Interface::Interface(const char *name) : _name(name) {
2532 }
2533 Interface::~Interface() {
2534 }
2535 
2536 Form::InterfaceType Interface::interface_type(FormDict &globals) const {
2537   Interface *thsi = (Interface*)this;
2538   if ( thsi->is_RegInterface()   ) return Form::register_interface;
2539   if ( thsi->is_MemInterface()   ) return Form::memory_interface;
2540   if ( thsi->is_ConstInterface() ) return Form::constant_interface;
2541   if ( thsi->is_CondInterface()  ) return Form::conditional_interface;
2542 
2543   return Form::no_interface;
2544 }
2545 
2546 RegInterface   *Interface::is_RegInterface() {
2547   if ( strcmp(_name,"REG_INTER") != 0 )
2548     return NULL;
2549   return (RegInterface*)this;
2550 }
2551 MemInterface   *Interface::is_MemInterface() {
2552   if ( strcmp(_name,"MEMORY_INTER") != 0 )  return NULL;
2553   return (MemInterface*)this;
2554 }
2555 ConstInterface *Interface::is_ConstInterface() {
2556   if ( strcmp(_name,"CONST_INTER") != 0 )  return NULL;
2557   return (ConstInterface*)this;
2558 }
2559 CondInterface  *Interface::is_CondInterface() {
2560   if ( strcmp(_name,"COND_INTER") != 0 )  return NULL;
2561   return (CondInterface*)this;
2562 }
2563 
2564 
2565 void Interface::dump() {
2566   output(stderr);
2567 }
2568 
2569 // Write info to output files
2570 void Interface::output(FILE *fp) {
2571   fprintf(fp,"Interface: %s\n", (_name ? _name : "") );
2572 }
2573 
2574 //------------------------------RegInterface-----------------------------------
2575 RegInterface::RegInterface() : Interface("REG_INTER") {
2576 }
2577 RegInterface::~RegInterface() {
2578 }
2579 
2580 void RegInterface::dump() {
2581   output(stderr);
2582 }
2583 
2584 // Write info to output files
2585 void RegInterface::output(FILE *fp) {
2586   Interface::output(fp);
2587 }
2588 
2589 //------------------------------ConstInterface---------------------------------
2590 ConstInterface::ConstInterface() : Interface("CONST_INTER") {
2591 }
2592 ConstInterface::~ConstInterface() {
2593 }
2594 
2595 void ConstInterface::dump() {
2596   output(stderr);
2597 }
2598 
2599 // Write info to output files
2600 void ConstInterface::output(FILE *fp) {
2601   Interface::output(fp);
2602 }
2603 
2604 //------------------------------MemInterface-----------------------------------
2605 MemInterface::MemInterface(char *base, char *index, char *scale, char *disp)
2606   : Interface("MEMORY_INTER"), _base(base), _index(index), _scale(scale), _disp(disp) {
2607 }
2608 MemInterface::~MemInterface() {
2609   // not owner of any character arrays
2610 }
2611 
2612 void MemInterface::dump() {
2613   output(stderr);
2614 }
2615 
2616 // Write info to output files
2617 void MemInterface::output(FILE *fp) {
2618   Interface::output(fp);
2619   if ( _base  != NULL ) fprintf(fp,"  base  == %s\n", _base);
2620   if ( _index != NULL ) fprintf(fp,"  index == %s\n", _index);
2621   if ( _scale != NULL ) fprintf(fp,"  scale == %s\n", _scale);
2622   if ( _disp  != NULL ) fprintf(fp,"  disp  == %s\n", _disp);
2623   // fprintf(fp,"\n");
2624 }
2625 
2626 //------------------------------CondInterface----------------------------------
2627 CondInterface::CondInterface(const char* equal,         const char* equal_format,
2628                              const char* not_equal,     const char* not_equal_format,
2629                              const char* less,          const char* less_format,
2630                              const char* greater_equal, const char* greater_equal_format,
2631                              const char* less_equal,    const char* less_equal_format,
2632                              const char* greater,       const char* greater_format)
2633   : Interface("COND_INTER"),
2634     _equal(equal),                 _equal_format(equal_format),
2635     _not_equal(not_equal),         _not_equal_format(not_equal_format),
2636     _less(less),                   _less_format(less_format),
2637     _greater_equal(greater_equal), _greater_equal_format(greater_equal_format),
2638     _less_equal(less_equal),       _less_equal_format(less_equal_format),
2639     _greater(greater),             _greater_format(greater_format) {
2640 }
2641 CondInterface::~CondInterface() {
2642   // not owner of any character arrays
2643 }
2644 
2645 void CondInterface::dump() {
2646   output(stderr);
2647 }
2648 
2649 // Write info to output files
2650 void CondInterface::output(FILE *fp) {
2651   Interface::output(fp);
2652   if ( _equal  != NULL )     fprintf(fp," equal       == %s\n", _equal);
2653   if ( _not_equal  != NULL ) fprintf(fp," not_equal   == %s\n", _not_equal);
2654   if ( _less  != NULL )      fprintf(fp," less        == %s\n", _less);
2655   if ( _greater_equal  != NULL ) fprintf(fp," greater_equal   == %s\n", _greater_equal);
2656   if ( _less_equal  != NULL ) fprintf(fp," less_equal  == %s\n", _less_equal);
2657   if ( _greater  != NULL )    fprintf(fp," greater     == %s\n", _greater);
2658   // fprintf(fp,"\n");
2659 }
2660 
2661 //------------------------------ConstructRule----------------------------------
2662 ConstructRule::ConstructRule(char *cnstr)
2663   : _construct(cnstr) {
2664 }
2665 ConstructRule::~ConstructRule() {
2666 }
2667 
2668 void ConstructRule::dump() {
2669   output(stderr);
2670 }
2671 
2672 void ConstructRule::output(FILE *fp) {
2673   fprintf(fp,"\nConstruct Rule\n");  // Write to output files
2674 }
2675 
2676 
2677 //==============================Shared Forms===================================
2678 //------------------------------AttributeForm----------------------------------
2679 int         AttributeForm::_insId   = 0;           // start counter at 0
2680 int         AttributeForm::_opId    = 0;           // start counter at 0
2681 const char* AttributeForm::_ins_cost = "ins_cost"; // required name
2682 const char* AttributeForm::_ins_pc_relative = "ins_pc_relative";
2683 const char* AttributeForm::_op_cost  = "op_cost";  // required name
2684 
2685 AttributeForm::AttributeForm(char *attr, int type, char *attrdef)
2686   : Form(Form::ATTR), _attrname(attr), _atype(type), _attrdef(attrdef) {
2687     if (type==OP_ATTR) {
2688       id = ++_opId;
2689     }
2690     else if (type==INS_ATTR) {
2691       id = ++_insId;
2692     }
2693     else assert( false,"");
2694 }
2695 AttributeForm::~AttributeForm() {
2696 }
2697 
2698 // Dynamic type check
2699 AttributeForm *AttributeForm::is_attribute() const {
2700   return (AttributeForm*)this;
2701 }
2702 
2703 
2704 // inlined  // int  AttributeForm::type() { return id;}
2705 
2706 void AttributeForm::dump() {
2707   output(stderr);
2708 }
2709 
2710 void AttributeForm::output(FILE *fp) {
2711   if( _attrname && _attrdef ) {
2712     fprintf(fp,"\n// AttributeForm \nstatic const int %s = %s;\n",
2713             _attrname, _attrdef);
2714   }
2715   else {
2716     fprintf(fp,"\n// AttributeForm missing name %s or definition %s\n",
2717             (_attrname?_attrname:""), (_attrdef?_attrdef:"") );
2718   }
2719 }
2720 
2721 //------------------------------Component--------------------------------------
2722 Component::Component(const char *name, const char *type, int usedef)
2723   : _name(name), _type(type), _usedef(usedef) {
2724     _ftype = Form::COMP;
2725 }
2726 Component::~Component() {
2727 }
2728 
2729 // True if this component is equal to the parameter.
2730 bool Component::is(int use_def_kill_enum) const {
2731   return (_usedef == use_def_kill_enum ? true : false);
2732 }
2733 // True if this component is used/def'd/kill'd as the parameter suggests.
2734 bool Component::isa(int use_def_kill_enum) const {
2735   return (_usedef & use_def_kill_enum) == use_def_kill_enum;
2736 }
2737 
2738 // Extend this component with additional use/def/kill behavior
2739 int Component::promote_use_def_info(int new_use_def) {
2740   _usedef |= new_use_def;
2741 
2742   return _usedef;
2743 }
2744 
2745 // Check the base type of this component, if it has one
2746 const char *Component::base_type(FormDict &globals) {
2747   const Form *frm = globals[_type];
2748   if (frm == NULL) return NULL;
2749   OperandForm *op = frm->is_operand();
2750   if (op == NULL) return NULL;
2751   if (op->ideal_only()) return op->_ident;
2752   return (char *)op->ideal_type(globals);
2753 }
2754 
2755 void Component::dump() {
2756   output(stderr);
2757 }
2758 
2759 void Component::output(FILE *fp) {
2760   fprintf(fp,"Component:");  // Write to output files
2761   fprintf(fp, "  name = %s", _name);
2762   fprintf(fp, ", type = %s", _type);
2763   const char * usedef = "Undefined Use/Def info";
2764   switch (_usedef) {
2765     case USE:      usedef = "USE";      break;
2766     case USE_DEF:  usedef = "USE_DEF";  break;
2767     case USE_KILL: usedef = "USE_KILL"; break;
2768     case KILL:     usedef = "KILL";     break;
2769     case TEMP:     usedef = "TEMP";     break;
2770     case DEF:      usedef = "DEF";      break;
2771     default: assert(false, "unknown effect");
2772   }
2773   fprintf(fp, ", use/def = %s\n", usedef);
2774 }
2775 
2776 
2777 //------------------------------ComponentList---------------------------------
2778 ComponentList::ComponentList() : NameList(), _matchcnt(0) {
2779 }
2780 ComponentList::~ComponentList() {
2781   // // This list may not own its elements if copied via assignment
2782   // Component *component;
2783   // for (reset(); (component = iter()) != NULL;) {
2784   //   delete component;
2785   // }
2786 }
2787 
2788 void   ComponentList::insert(Component *component, bool mflag) {
2789   NameList::addName((char *)component);
2790   if(mflag) _matchcnt++;
2791 }
2792 void   ComponentList::insert(const char *name, const char *opType, int usedef,
2793                              bool mflag) {
2794   Component * component = new Component(name, opType, usedef);
2795   insert(component, mflag);
2796 }
2797 Component *ComponentList::current() { return (Component*)NameList::current(); }
2798 Component *ComponentList::iter()    { return (Component*)NameList::iter(); }
2799 Component *ComponentList::match_iter() {
2800   if(_iter < _matchcnt) return (Component*)NameList::iter();
2801   return NULL;
2802 }
2803 Component *ComponentList::post_match_iter() {
2804   Component *comp = iter();
2805   // At end of list?
2806   if ( comp == NULL ) {
2807     return comp;
2808   }
2809   // In post-match components?
2810   if (_iter > match_count()-1) {
2811     return comp;
2812   }
2813 
2814   return post_match_iter();
2815 }
2816 
2817 void       ComponentList::reset()   { NameList::reset(); }
2818 int        ComponentList::count()   { return NameList::count(); }
2819 
2820 Component *ComponentList::operator[](int position) {
2821   // Shortcut complete iteration if there are not enough entries
2822   if (position >= count()) return NULL;
2823 
2824   int        index     = 0;
2825   Component *component = NULL;
2826   for (reset(); (component = iter()) != NULL;) {
2827     if (index == position) {
2828       return component;
2829     }
2830     ++index;
2831   }
2832 
2833   return NULL;
2834 }
2835 
2836 const Component *ComponentList::search(const char *name) {
2837   PreserveIter pi(this);
2838   reset();
2839   for( Component *comp = NULL; ((comp = iter()) != NULL); ) {
2840     if( strcmp(comp->_name,name) == 0 ) return comp;
2841   }
2842 
2843   return NULL;
2844 }
2845 
2846 // Return number of USEs + number of DEFs
2847 // When there are no components, or the first component is a USE,
2848 // then we add '1' to hold a space for the 'result' operand.
2849 int ComponentList::num_operands() {
2850   PreserveIter pi(this);
2851   uint       count = 1;           // result operand
2852   uint       position = 0;
2853 
2854   Component *component  = NULL;
2855   for( reset(); (component = iter()) != NULL; ++position ) {
2856     if( component->isa(Component::USE) ||
2857         ( position == 0 && (! component->isa(Component::DEF))) ) {
2858       ++count;
2859     }
2860   }
2861 
2862   return count;
2863 }
2864 
2865 // Return zero-based position in list;  -1 if not in list.
2866 // if parameter 'usedef' is ::USE, it will match USE, USE_DEF, ...
2867 int ComponentList::operand_position(const char *name, int usedef) {
2868   PreserveIter pi(this);
2869   int position = 0;
2870   int num_opnds = num_operands();
2871   Component *component;
2872   Component* preceding_non_use = NULL;
2873   Component* first_def = NULL;
2874   for (reset(); (component = iter()) != NULL; ++position) {
2875     // When the first component is not a DEF,
2876     // leave space for the result operand!
2877     if ( position==0 && (! component->isa(Component::DEF)) ) {
2878       ++position;
2879       ++num_opnds;
2880     }
2881     if (strcmp(name, component->_name)==0 && (component->isa(usedef))) {
2882       // When the first entry in the component list is a DEF and a USE
2883       // Treat them as being separate, a DEF first, then a USE
2884       if( position==0
2885           && usedef==Component::USE && component->isa(Component::DEF) ) {
2886         assert(position+1 < num_opnds, "advertised index in bounds");
2887         return position+1;
2888       } else {
2889         if( preceding_non_use && strcmp(component->_name, preceding_non_use->_name) ) {
2890           fprintf(stderr, "the name '%s' should not precede the name '%s'\n", preceding_non_use->_name, name);
2891         }
2892         if( position >= num_opnds ) {
2893           fprintf(stderr, "the name '%s' is too late in its name list\n", name);
2894         }
2895         assert(position < num_opnds, "advertised index in bounds");
2896         return position;
2897       }
2898     }
2899     if( component->isa(Component::DEF)
2900         && component->isa(Component::USE) ) {
2901       ++position;
2902       if( position != 1 )  --position;   // only use two slots for the 1st USE_DEF
2903     }
2904     if( component->isa(Component::DEF) && !first_def ) {
2905       first_def = component;
2906     }
2907     if( !component->isa(Component::USE) && component != first_def ) {
2908       preceding_non_use = component;
2909     } else if( preceding_non_use && !strcmp(component->_name, preceding_non_use->_name) ) {
2910       preceding_non_use = NULL;
2911     }
2912   }
2913   return Not_in_list;
2914 }
2915 
2916 // Find position for this name, regardless of use/def information
2917 int ComponentList::operand_position(const char *name) {
2918   PreserveIter pi(this);
2919   int position = 0;
2920   Component *component;
2921   for (reset(); (component = iter()) != NULL; ++position) {
2922     // When the first component is not a DEF,
2923     // leave space for the result operand!
2924     if ( position==0 && (! component->isa(Component::DEF)) ) {
2925       ++position;
2926     }
2927     if (strcmp(name, component->_name)==0) {
2928       return position;
2929     }
2930     if( component->isa(Component::DEF)
2931         && component->isa(Component::USE) ) {
2932       ++position;
2933       if( position != 1 )  --position;   // only use two slots for the 1st USE_DEF
2934     }
2935   }
2936   return Not_in_list;
2937 }
2938 
2939 int ComponentList::operand_position_format(const char *name) {
2940   PreserveIter pi(this);
2941   int  first_position = operand_position(name);
2942   int  use_position   = operand_position(name, Component::USE);
2943 
2944   return ((first_position < use_position) ? use_position : first_position);
2945 }
2946 
2947 int ComponentList::label_position() {
2948   PreserveIter pi(this);
2949   int position = 0;
2950   reset();
2951   for( Component *comp; (comp = iter()) != NULL; ++position) {
2952     // When the first component is not a DEF,
2953     // leave space for the result operand!
2954     if ( position==0 && (! comp->isa(Component::DEF)) ) {
2955       ++position;
2956     }
2957     if (strcmp(comp->_type, "label")==0) {
2958       return position;
2959     }
2960     if( comp->isa(Component::DEF)
2961         && comp->isa(Component::USE) ) {
2962       ++position;
2963       if( position != 1 )  --position;   // only use two slots for the 1st USE_DEF
2964     }
2965   }
2966 
2967   return -1;
2968 }
2969 
2970 int ComponentList::method_position() {
2971   PreserveIter pi(this);
2972   int position = 0;
2973   reset();
2974   for( Component *comp; (comp = iter()) != NULL; ++position) {
2975     // When the first component is not a DEF,
2976     // leave space for the result operand!
2977     if ( position==0 && (! comp->isa(Component::DEF)) ) {
2978       ++position;
2979     }
2980     if (strcmp(comp->_type, "method")==0) {
2981       return position;
2982     }
2983     if( comp->isa(Component::DEF)
2984         && comp->isa(Component::USE) ) {
2985       ++position;
2986       if( position != 1 )  --position;   // only use two slots for the 1st USE_DEF
2987     }
2988   }
2989 
2990   return -1;
2991 }
2992 
2993 void ComponentList::dump() { output(stderr); }
2994 
2995 void ComponentList::output(FILE *fp) {
2996   PreserveIter pi(this);
2997   fprintf(fp, "\n");
2998   Component *component;
2999   for (reset(); (component = iter()) != NULL;) {
3000     component->output(fp);
3001   }
3002   fprintf(fp, "\n");
3003 }
3004 
3005 //------------------------------MatchNode--------------------------------------
3006 MatchNode::MatchNode(ArchDesc &ad, const char *result, const char *mexpr,
3007                      const char *opType, MatchNode *lChild, MatchNode *rChild)
3008   : _AD(ad), _result(result), _name(mexpr), _opType(opType),
3009     _lChild(lChild), _rChild(rChild), _internalop(0), _numleaves(0),
3010     _commutative_id(0) {
3011   _numleaves = (lChild ? lChild->_numleaves : 0)
3012                + (rChild ? rChild->_numleaves : 0);
3013 }
3014 
3015 MatchNode::MatchNode(ArchDesc &ad, MatchNode& mnode)
3016   : _AD(ad), _result(mnode._result), _name(mnode._name),
3017     _opType(mnode._opType), _lChild(mnode._lChild), _rChild(mnode._rChild),
3018     _internalop(0), _numleaves(mnode._numleaves),
3019     _commutative_id(mnode._commutative_id) {
3020 }
3021 
3022 MatchNode::MatchNode(ArchDesc &ad, MatchNode& mnode, int clone)
3023   : _AD(ad), _result(mnode._result), _name(mnode._name),
3024     _opType(mnode._opType),
3025     _internalop(0), _numleaves(mnode._numleaves),
3026     _commutative_id(mnode._commutative_id) {
3027   if (mnode._lChild) {
3028     _lChild = new MatchNode(ad, *mnode._lChild, clone);
3029   } else {
3030     _lChild = NULL;
3031   }
3032   if (mnode._rChild) {
3033     _rChild = new MatchNode(ad, *mnode._rChild, clone);
3034   } else {
3035     _rChild = NULL;
3036   }
3037 }
3038 
3039 MatchNode::~MatchNode() {
3040   // // This node may not own its children if copied via assignment
3041   // if( _lChild ) delete _lChild;
3042   // if( _rChild ) delete _rChild;
3043 }
3044 
3045 bool  MatchNode::find_type(const char *type, int &position) const {
3046   if ( (_lChild != NULL) && (_lChild->find_type(type, position)) ) return true;
3047   if ( (_rChild != NULL) && (_rChild->find_type(type, position)) ) return true;
3048 
3049   if (strcmp(type,_opType)==0)  {
3050     return true;
3051   } else {
3052     ++position;
3053   }
3054   return false;
3055 }
3056 
3057 // Recursive call collecting info on top-level operands, not transitive.
3058 // Implementation does not modify state of internal structures.
3059 void MatchNode::append_components(FormDict& locals, ComponentList& components,
3060                                   bool def_flag) const {
3061   int usedef = def_flag ? Component::DEF : Component::USE;
3062   FormDict &globals = _AD.globalNames();
3063 
3064   assert (_name != NULL, "MatchNode::build_components encountered empty node\n");
3065   // Base case
3066   if (_lChild==NULL && _rChild==NULL) {
3067     // If _opType is not an operation, do not build a component for it #####
3068     const Form *f = globals[_opType];
3069     if( f != NULL ) {
3070       // Add non-ideals that are operands, operand-classes,
3071       if( ! f->ideal_only()
3072           && (f->is_opclass() || f->is_operand()) ) {
3073         components.insert(_name, _opType, usedef, true);
3074       }
3075     }
3076     return;
3077   }
3078   // Promote results of "Set" to DEF
3079   bool tmpdef_flag = (!strcmp(_opType, "Set")) ? true : false;
3080   if (_lChild) _lChild->append_components(locals, components, tmpdef_flag);
3081   tmpdef_flag = false;   // only applies to component immediately following 'Set'
3082   if (_rChild) _rChild->append_components(locals, components, tmpdef_flag);
3083 }
3084 
3085 // Find the n'th base-operand in the match node,
3086 // recursively investigates match rules of user-defined operands.
3087 //
3088 // Implementation does not modify state of internal structures since they
3089 // can be shared.
3090 bool MatchNode::base_operand(uint &position, FormDict &globals,
3091                              const char * &result, const char * &name,
3092                              const char * &opType) const {
3093   assert (_name != NULL, "MatchNode::base_operand encountered empty node\n");
3094   // Base case
3095   if (_lChild==NULL && _rChild==NULL) {
3096     // Check for special case: "Universe", "label"
3097     if (strcmp(_opType,"Universe") == 0 || strcmp(_opType,"label")==0 ) {
3098       if (position == 0) {
3099         result = _result;
3100         name   = _name;
3101         opType = _opType;
3102         return 1;
3103       } else {
3104         -- position;
3105         return 0;
3106       }
3107     }
3108 
3109     const Form *form = globals[_opType];
3110     MatchNode *matchNode = NULL;
3111     // Check for user-defined type
3112     if (form) {
3113       // User operand or instruction?
3114       OperandForm  *opForm = form->is_operand();
3115       InstructForm *inForm = form->is_instruction();
3116       if ( opForm ) {
3117         matchNode = (MatchNode*)opForm->_matrule;
3118       } else if ( inForm ) {
3119         matchNode = (MatchNode*)inForm->_matrule;
3120       }
3121     }
3122     // if this is user-defined, recurse on match rule
3123     // User-defined operand and instruction forms have a match-rule.
3124     if (matchNode) {
3125       return (matchNode->base_operand(position,globals,result,name,opType));
3126     } else {
3127       // Either not a form, or a system-defined form (no match rule).
3128       if (position==0) {
3129         result = _result;
3130         name   = _name;
3131         opType = _opType;
3132         return 1;
3133       } else {
3134         --position;
3135         return 0;
3136       }
3137     }
3138 
3139   } else {
3140     // Examine the left child and right child as well
3141     if (_lChild) {
3142       if (_lChild->base_operand(position, globals, result, name, opType))
3143         return 1;
3144     }
3145 
3146     if (_rChild) {
3147       if (_rChild->base_operand(position, globals, result, name, opType))
3148         return 1;
3149     }
3150   }
3151 
3152   return 0;
3153 }
3154 
3155 // Recursive call on all operands' match rules in my match rule.
3156 uint  MatchNode::num_consts(FormDict &globals) const {
3157   uint        index      = 0;
3158   uint        num_consts = 0;
3159   const char *result;
3160   const char *name;
3161   const char *opType;
3162 
3163   for (uint position = index;
3164        base_operand(position,globals,result,name,opType); position = index) {
3165     ++index;
3166     if( ideal_to_const_type(opType) )        num_consts++;
3167   }
3168 
3169   return num_consts;
3170 }
3171 
3172 // Recursive call on all operands' match rules in my match rule.
3173 // Constants in match rule subtree with specified type
3174 uint  MatchNode::num_consts(FormDict &globals, Form::DataType type) const {
3175   uint        index      = 0;
3176   uint        num_consts = 0;
3177   const char *result;
3178   const char *name;
3179   const char *opType;
3180 
3181   for (uint position = index;
3182        base_operand(position,globals,result,name,opType); position = index) {
3183     ++index;
3184     if( ideal_to_const_type(opType) == type ) num_consts++;
3185   }
3186 
3187   return num_consts;
3188 }
3189 
3190 // Recursive call on all operands' match rules in my match rule.
3191 uint  MatchNode::num_const_ptrs(FormDict &globals) const {
3192   return  num_consts( globals, Form::idealP );
3193 }
3194 
3195 bool  MatchNode::sets_result() const {
3196   return   ( (strcmp(_name,"Set") == 0) ? true : false );
3197 }
3198 
3199 const char *MatchNode::reduce_right(FormDict &globals) const {
3200   // If there is no right reduction, return NULL.
3201   const char      *rightStr    = NULL;
3202 
3203   // If we are a "Set", start from the right child.
3204   const MatchNode *const mnode = sets_result() ?
3205     (const MatchNode *const)this->_rChild :
3206     (const MatchNode *const)this;
3207 
3208   // If our right child exists, it is the right reduction
3209   if ( mnode->_rChild ) {
3210     rightStr = mnode->_rChild->_internalop ? mnode->_rChild->_internalop
3211       : mnode->_rChild->_opType;
3212   }
3213   // Else, May be simple chain rule: (Set dst operand_form), rightStr=NULL;
3214   return rightStr;
3215 }
3216 
3217 const char *MatchNode::reduce_left(FormDict &globals) const {
3218   // If there is no left reduction, return NULL.
3219   const char  *leftStr  = NULL;
3220 
3221   // If we are a "Set", start from the right child.
3222   const MatchNode *const mnode = sets_result() ?
3223     (const MatchNode *const)this->_rChild :
3224     (const MatchNode *const)this;
3225 
3226   // If our left child exists, it is the left reduction
3227   if ( mnode->_lChild ) {
3228     leftStr = mnode->_lChild->_internalop ? mnode->_lChild->_internalop
3229       : mnode->_lChild->_opType;
3230   } else {
3231     // May be simple chain rule: (Set dst operand_form_source)
3232     if ( sets_result() ) {
3233       OperandForm *oper = globals[mnode->_opType]->is_operand();
3234       if( oper ) {
3235         leftStr = mnode->_opType;
3236       }
3237     }
3238   }
3239   return leftStr;
3240 }
3241 
3242 //------------------------------count_instr_names------------------------------
3243 // Count occurrences of operands names in the leaves of the instruction
3244 // match rule.
3245 void MatchNode::count_instr_names( Dict &names ) {
3246   if( !this ) return;
3247   if( _lChild ) _lChild->count_instr_names(names);
3248   if( _rChild ) _rChild->count_instr_names(names);
3249   if( !_lChild && !_rChild ) {
3250     uintptr_t cnt = (uintptr_t)names[_name];
3251     cnt++;                      // One more name found
3252     names.Insert(_name,(void*)cnt);
3253   }
3254 }
3255 
3256 //------------------------------build_instr_pred-------------------------------
3257 // Build a path to 'name' in buf.  Actually only build if cnt is zero, so we
3258 // can skip some leading instances of 'name'.
3259 int MatchNode::build_instr_pred( char *buf, const char *name, int cnt ) {
3260   if( _lChild ) {
3261     if( !cnt ) strcpy( buf, "_kids[0]->" );
3262     cnt = _lChild->build_instr_pred( buf+strlen(buf), name, cnt );
3263     if( cnt < 0 ) return cnt;   // Found it, all done
3264   }
3265   if( _rChild ) {
3266     if( !cnt ) strcpy( buf, "_kids[1]->" );
3267     cnt = _rChild->build_instr_pred( buf+strlen(buf), name, cnt );
3268     if( cnt < 0 ) return cnt;   // Found it, all done
3269   }
3270   if( !_lChild && !_rChild ) {  // Found a leaf
3271     // Wrong name?  Give up...
3272     if( strcmp(name,_name) ) return cnt;
3273     if( !cnt ) strcpy(buf,"_leaf");
3274     return cnt-1;
3275   }
3276   return cnt;
3277 }
3278 
3279 
3280 //------------------------------build_internalop-------------------------------
3281 // Build string representation of subtree
3282 void MatchNode::build_internalop( ) {
3283   char *iop, *subtree;
3284   const char *lstr, *rstr;
3285   // Build string representation of subtree
3286   // Operation lchildType rchildType
3287   int len = (int)strlen(_opType) + 4;
3288   lstr = (_lChild) ? ((_lChild->_internalop) ?
3289                        _lChild->_internalop : _lChild->_opType) : "";
3290   rstr = (_rChild) ? ((_rChild->_internalop) ?
3291                        _rChild->_internalop : _rChild->_opType) : "";
3292   len += (int)strlen(lstr) + (int)strlen(rstr);
3293   subtree = (char *)malloc(len);
3294   sprintf(subtree,"_%s_%s_%s", _opType, lstr, rstr);
3295   // Hash the subtree string in _internalOps; if a name exists, use it
3296   iop = (char *)_AD._internalOps[subtree];
3297   // Else create a unique name, and add it to the hash table
3298   if (iop == NULL) {
3299     iop = subtree;
3300     _AD._internalOps.Insert(subtree, iop);
3301     _AD._internalOpNames.addName(iop);
3302     _AD._internalMatch.Insert(iop, this);
3303   }
3304   // Add the internal operand name to the MatchNode
3305   _internalop = iop;
3306   _result = iop;
3307 }
3308 
3309 
3310 void MatchNode::dump() {
3311   output(stderr);
3312 }
3313 
3314 void MatchNode::output(FILE *fp) {
3315   if (_lChild==0 && _rChild==0) {
3316     fprintf(fp," %s",_name);    // operand
3317   }
3318   else {
3319     fprintf(fp," (%s ",_name);  // " (opcodeName "
3320     if(_lChild) _lChild->output(fp); //               left operand
3321     if(_rChild) _rChild->output(fp); //                    right operand
3322     fprintf(fp,")");                 //                                 ")"
3323   }
3324 }
3325 
3326 int MatchNode::needs_ideal_memory_edge(FormDict &globals) const {
3327   static const char *needs_ideal_memory_list[] = {
3328     "StoreI","StoreL","StoreP","StoreN","StoreD","StoreF" ,
3329     "StoreB","StoreC","Store" ,"StoreFP",
3330     "LoadI", "LoadUI2L", "LoadL", "LoadP" ,"LoadN", "LoadD" ,"LoadF"  ,
3331     "LoadB" , "LoadUB", "LoadUS" ,"LoadS" ,"Load"   ,
3332     "Store4I","Store2I","Store2L","Store2D","Store4F","Store2F","Store16B",
3333     "Store8B","Store4B","Store8C","Store4C","Store2C",
3334     "Load4I" ,"Load2I" ,"Load2L" ,"Load2D" ,"Load4F" ,"Load2F" ,"Load16B" ,
3335     "Load8B" ,"Load4B" ,"Load8C" ,"Load4C" ,"Load2C" ,"Load8S", "Load4S","Load2S",
3336     "LoadRange", "LoadKlass", "LoadNKlass", "LoadL_unaligned", "LoadD_unaligned",
3337     "LoadPLocked", "LoadLLocked",
3338     "StorePConditional", "StoreIConditional", "StoreLConditional",
3339     "CompareAndSwapI", "CompareAndSwapL", "CompareAndSwapP", "CompareAndSwapN",
3340     "StoreCM",
3341     "ClearArray"
3342   };
3343   int cnt = sizeof(needs_ideal_memory_list)/sizeof(char*);
3344   if( strcmp(_opType,"PrefetchRead")==0 || strcmp(_opType,"PrefetchWrite")==0 )
3345     return 1;
3346   if( _lChild ) {
3347     const char *opType = _lChild->_opType;
3348     for( int i=0; i<cnt; i++ )
3349       if( strcmp(opType,needs_ideal_memory_list[i]) == 0 )
3350         return 1;
3351     if( _lChild->needs_ideal_memory_edge(globals) )
3352       return 1;
3353   }
3354   if( _rChild ) {
3355     const char *opType = _rChild->_opType;
3356     for( int i=0; i<cnt; i++ )
3357       if( strcmp(opType,needs_ideal_memory_list[i]) == 0 )
3358         return 1;
3359     if( _rChild->needs_ideal_memory_edge(globals) )
3360       return 1;
3361   }
3362 
3363   return 0;
3364 }
3365 
3366 // TRUE if defines a derived oop, and so needs a base oop edge present
3367 // post-matching.
3368 int MatchNode::needs_base_oop_edge() const {
3369   if( !strcmp(_opType,"AddP") ) return 1;
3370   if( strcmp(_opType,"Set") ) return 0;
3371   return !strcmp(_rChild->_opType,"AddP");
3372 }
3373 
3374 int InstructForm::needs_base_oop_edge(FormDict &globals) const {
3375   if( is_simple_chain_rule(globals) ) {
3376     const char *src = _matrule->_rChild->_opType;
3377     OperandForm *src_op = globals[src]->is_operand();
3378     assert( src_op, "Not operand class of chain rule" );
3379     return src_op->_matrule ? src_op->_matrule->needs_base_oop_edge() : 0;
3380   }                             // Else check instruction
3381 
3382   return _matrule ? _matrule->needs_base_oop_edge() : 0;
3383 }
3384 
3385 
3386 //-------------------------cisc spilling methods-------------------------------
3387 // helper routines and methods for detecting cisc-spilling instructions
3388 //-------------------------cisc_spill_merge------------------------------------
3389 int MatchNode::cisc_spill_merge(int left_spillable, int right_spillable) {
3390   int cisc_spillable  = Maybe_cisc_spillable;
3391 
3392   // Combine results of left and right checks
3393   if( (left_spillable == Maybe_cisc_spillable) && (right_spillable == Maybe_cisc_spillable) ) {
3394     // neither side is spillable, nor prevents cisc spilling
3395     cisc_spillable = Maybe_cisc_spillable;
3396   }
3397   else if( (left_spillable == Maybe_cisc_spillable) && (right_spillable > Maybe_cisc_spillable) ) {
3398     // right side is spillable
3399     cisc_spillable = right_spillable;
3400   }
3401   else if( (right_spillable == Maybe_cisc_spillable) && (left_spillable > Maybe_cisc_spillable) ) {
3402     // left side is spillable
3403     cisc_spillable = left_spillable;
3404   }
3405   else if( (left_spillable == Not_cisc_spillable) || (right_spillable == Not_cisc_spillable) ) {
3406     // left or right prevents cisc spilling this instruction
3407     cisc_spillable = Not_cisc_spillable;
3408   }
3409   else {
3410     // Only allow one to spill
3411     cisc_spillable = Not_cisc_spillable;
3412   }
3413 
3414   return cisc_spillable;
3415 }
3416 
3417 //-------------------------root_ops_match--------------------------------------
3418 bool static root_ops_match(FormDict &globals, const char *op1, const char *op2) {
3419   // Base Case: check that the current operands/operations match
3420   assert( op1, "Must have op's name");
3421   assert( op2, "Must have op's name");
3422   const Form *form1 = globals[op1];
3423   const Form *form2 = globals[op2];
3424 
3425   return (form1 == form2);
3426 }
3427 
3428 //-------------------------cisc_spill_match_node-------------------------------
3429 // Recursively check two MatchRules for legal conversion via cisc-spilling
3430 int MatchNode::cisc_spill_match(FormDict& globals, RegisterForm* registers, MatchNode* mRule2, const char* &operand, const char* &reg_type) {
3431   int cisc_spillable  = Maybe_cisc_spillable;
3432   int left_spillable  = Maybe_cisc_spillable;
3433   int right_spillable = Maybe_cisc_spillable;
3434 
3435   // Check that each has same number of operands at this level
3436   if( (_lChild && !(mRule2->_lChild)) || (_rChild && !(mRule2->_rChild)) )
3437     return Not_cisc_spillable;
3438 
3439   // Base Case: check that the current operands/operations match
3440   // or are CISC spillable
3441   assert( _opType, "Must have _opType");
3442   assert( mRule2->_opType, "Must have _opType");
3443   const Form *form  = globals[_opType];
3444   const Form *form2 = globals[mRule2->_opType];
3445   if( form == form2 ) {
3446     cisc_spillable = Maybe_cisc_spillable;
3447   } else {
3448     const InstructForm *form2_inst = form2 ? form2->is_instruction() : NULL;
3449     const char *name_left  = mRule2->_lChild ? mRule2->_lChild->_opType : NULL;
3450     const char *name_right = mRule2->_rChild ? mRule2->_rChild->_opType : NULL;
3451     DataType data_type = Form::none;
3452     if (form->is_operand()) {
3453       // Make sure the loadX matches the type of the reg
3454       data_type = form->ideal_to_Reg_type(form->is_operand()->ideal_type(globals));
3455     }
3456     // Detect reg vs (loadX memory)
3457     if( form->is_cisc_reg(globals)
3458         && form2_inst
3459         && data_type != Form::none
3460         && (is_load_from_memory(mRule2->_opType) == data_type) // reg vs. (load memory)
3461         && (name_left != NULL)       // NOT (load)
3462         && (name_right == NULL) ) {  // NOT (load memory foo)
3463       const Form *form2_left = name_left ? globals[name_left] : NULL;
3464       if( form2_left && form2_left->is_cisc_mem(globals) ) {
3465         cisc_spillable = Is_cisc_spillable;
3466         operand        = _name;
3467         reg_type       = _result;
3468         return Is_cisc_spillable;
3469       } else {
3470         cisc_spillable = Not_cisc_spillable;
3471       }
3472     }
3473     // Detect reg vs memory
3474     else if( form->is_cisc_reg(globals) && form2->is_cisc_mem(globals) ) {
3475       cisc_spillable = Is_cisc_spillable;
3476       operand        = _name;
3477       reg_type       = _result;
3478       return Is_cisc_spillable;
3479     } else {
3480       cisc_spillable = Not_cisc_spillable;
3481     }
3482   }
3483 
3484   // If cisc is still possible, check rest of tree
3485   if( cisc_spillable == Maybe_cisc_spillable ) {
3486     // Check that each has same number of operands at this level
3487     if( (_lChild && !(mRule2->_lChild)) || (_rChild && !(mRule2->_rChild)) ) return Not_cisc_spillable;
3488 
3489     // Check left operands
3490     if( (_lChild == NULL) && (mRule2->_lChild == NULL) ) {
3491       left_spillable = Maybe_cisc_spillable;
3492     } else {
3493       left_spillable = _lChild->cisc_spill_match(globals, registers, mRule2->_lChild, operand, reg_type);
3494     }
3495 
3496     // Check right operands
3497     if( (_rChild == NULL) && (mRule2->_rChild == NULL) ) {
3498       right_spillable =  Maybe_cisc_spillable;
3499     } else {
3500       right_spillable = _rChild->cisc_spill_match(globals, registers, mRule2->_rChild, operand, reg_type);
3501     }
3502 
3503     // Combine results of left and right checks
3504     cisc_spillable = cisc_spill_merge(left_spillable, right_spillable);
3505   }
3506 
3507   return cisc_spillable;
3508 }
3509 
3510 //---------------------------cisc_spill_match_rule------------------------------
3511 // Recursively check two MatchRules for legal conversion via cisc-spilling
3512 // This method handles the root of Match tree,
3513 // general recursive checks done in MatchNode
3514 int  MatchRule::matchrule_cisc_spill_match(FormDict& globals, RegisterForm* registers,
3515                                            MatchRule* mRule2, const char* &operand,
3516                                            const char* &reg_type) {
3517   int cisc_spillable  = Maybe_cisc_spillable;
3518   int left_spillable  = Maybe_cisc_spillable;
3519   int right_spillable = Maybe_cisc_spillable;
3520 
3521   // Check that each sets a result
3522   if( !(sets_result() && mRule2->sets_result()) ) return Not_cisc_spillable;
3523   // Check that each has same number of operands at this level
3524   if( (_lChild && !(mRule2->_lChild)) || (_rChild && !(mRule2->_rChild)) ) return Not_cisc_spillable;
3525 
3526   // Check left operands: at root, must be target of 'Set'
3527   if( (_lChild == NULL) || (mRule2->_lChild == NULL) ) {
3528     left_spillable = Not_cisc_spillable;
3529   } else {
3530     // Do not support cisc-spilling instruction's target location
3531     if( root_ops_match(globals, _lChild->_opType, mRule2->_lChild->_opType) ) {
3532       left_spillable = Maybe_cisc_spillable;
3533     } else {
3534       left_spillable = Not_cisc_spillable;
3535     }
3536   }
3537 
3538   // Check right operands: recursive walk to identify reg->mem operand
3539   if( (_rChild == NULL) && (mRule2->_rChild == NULL) ) {
3540     right_spillable =  Maybe_cisc_spillable;
3541   } else {
3542     right_spillable = _rChild->cisc_spill_match(globals, registers, mRule2->_rChild, operand, reg_type);
3543   }
3544 
3545   // Combine results of left and right checks
3546   cisc_spillable = cisc_spill_merge(left_spillable, right_spillable);
3547 
3548   return cisc_spillable;
3549 }
3550 
3551 //----------------------------- equivalent ------------------------------------
3552 // Recursively check to see if two match rules are equivalent.
3553 // This rule handles the root.
3554 bool MatchRule::equivalent(FormDict &globals, MatchNode *mRule2) {
3555   // Check that each sets a result
3556   if (sets_result() != mRule2->sets_result()) {
3557     return false;
3558   }
3559 
3560   // Check that the current operands/operations match
3561   assert( _opType, "Must have _opType");
3562   assert( mRule2->_opType, "Must have _opType");
3563   const Form *form  = globals[_opType];
3564   const Form *form2 = globals[mRule2->_opType];
3565   if( form != form2 ) {
3566     return false;
3567   }
3568 
3569   if (_lChild ) {
3570     if( !_lChild->equivalent(globals, mRule2->_lChild) )
3571       return false;
3572   } else if (mRule2->_lChild) {
3573     return false; // I have NULL left child, mRule2 has non-NULL left child.
3574   }
3575 
3576   if (_rChild ) {
3577     if( !_rChild->equivalent(globals, mRule2->_rChild) )
3578       return false;
3579   } else if (mRule2->_rChild) {
3580     return false; // I have NULL right child, mRule2 has non-NULL right child.
3581   }
3582 
3583   // We've made it through the gauntlet.
3584   return true;
3585 }
3586 
3587 //----------------------------- equivalent ------------------------------------
3588 // Recursively check to see if two match rules are equivalent.
3589 // This rule handles the operands.
3590 bool MatchNode::equivalent(FormDict &globals, MatchNode *mNode2) {
3591   if( !mNode2 )
3592     return false;
3593 
3594   // Check that the current operands/operations match
3595   assert( _opType, "Must have _opType");
3596   assert( mNode2->_opType, "Must have _opType");
3597   const Form *form  = globals[_opType];
3598   const Form *form2 = globals[mNode2->_opType];
3599   return (form == form2);
3600 }
3601 
3602 //-------------------------- has_commutative_op -------------------------------
3603 // Recursively check for commutative operations with subtree operands
3604 // which could be swapped.
3605 void MatchNode::count_commutative_op(int& count) {
3606   static const char *commut_op_list[] = {
3607     "AddI","AddL","AddF","AddD",
3608     "AndI","AndL",
3609     "MaxI","MinI",
3610     "MulI","MulL","MulF","MulD",
3611     "OrI" ,"OrL" ,
3612     "XorI","XorL"
3613   };
3614   int cnt = sizeof(commut_op_list)/sizeof(char*);
3615 
3616   if( _lChild && _rChild && (_lChild->_lChild || _rChild->_lChild) ) {
3617     // Don't swap if right operand is an immediate constant.
3618     bool is_const = false;
3619     if( _rChild->_lChild == NULL && _rChild->_rChild == NULL ) {
3620       FormDict &globals = _AD.globalNames();
3621       const Form *form = globals[_rChild->_opType];
3622       if ( form ) {
3623         OperandForm  *oper = form->is_operand();
3624         if( oper && oper->interface_type(globals) == Form::constant_interface )
3625           is_const = true;
3626       }
3627     }
3628     if( !is_const ) {
3629       for( int i=0; i<cnt; i++ ) {
3630         if( strcmp(_opType, commut_op_list[i]) == 0 ) {
3631           count++;
3632           _commutative_id = count; // id should be > 0
3633           break;
3634         }
3635       }
3636     }
3637   }
3638   if( _lChild )
3639     _lChild->count_commutative_op(count);
3640   if( _rChild )
3641     _rChild->count_commutative_op(count);
3642 }
3643 
3644 //-------------------------- swap_commutative_op ------------------------------
3645 // Recursively swap specified commutative operation with subtree operands.
3646 void MatchNode::swap_commutative_op(bool atroot, int id) {
3647   if( _commutative_id == id ) { // id should be > 0
3648     assert(_lChild && _rChild && (_lChild->_lChild || _rChild->_lChild ),
3649             "not swappable operation");
3650     MatchNode* tmp = _lChild;
3651     _lChild = _rChild;
3652     _rChild = tmp;
3653     // Don't exit here since we need to build internalop.
3654   }
3655 
3656   bool is_set = ( strcmp(_opType, "Set") == 0 );
3657   if( _lChild )
3658     _lChild->swap_commutative_op(is_set, id);
3659   if( _rChild )
3660     _rChild->swap_commutative_op(is_set, id);
3661 
3662   // If not the root, reduce this subtree to an internal operand
3663   if( !atroot && (_lChild || _rChild) ) {
3664     build_internalop();
3665   }
3666 }
3667 
3668 //-------------------------- swap_commutative_op ------------------------------
3669 // Recursively swap specified commutative operation with subtree operands.
3670 void MatchRule::matchrule_swap_commutative_op(const char* instr_ident, int count, int& match_rules_cnt) {
3671   assert(match_rules_cnt < 100," too many match rule clones");
3672   // Clone
3673   MatchRule* clone = new MatchRule(_AD, this);
3674   // Swap operands of commutative operation
3675   ((MatchNode*)clone)->swap_commutative_op(true, count);
3676   char* buf = (char*) malloc(strlen(instr_ident) + 4);
3677   sprintf(buf, "%s_%d", instr_ident, match_rules_cnt++);
3678   clone->_result = buf;
3679 
3680   clone->_next = this->_next;
3681   this-> _next = clone;
3682   if( (--count) > 0 ) {
3683     this-> matchrule_swap_commutative_op(instr_ident, count, match_rules_cnt);
3684     clone->matchrule_swap_commutative_op(instr_ident, count, match_rules_cnt);
3685   }
3686 }
3687 
3688 //------------------------------MatchRule--------------------------------------
3689 MatchRule::MatchRule(ArchDesc &ad)
3690   : MatchNode(ad), _depth(0), _construct(NULL), _numchilds(0) {
3691     _next = NULL;
3692 }
3693 
3694 MatchRule::MatchRule(ArchDesc &ad, MatchRule* mRule)
3695   : MatchNode(ad, *mRule, 0), _depth(mRule->_depth),
3696     _construct(mRule->_construct), _numchilds(mRule->_numchilds) {
3697     _next = NULL;
3698 }
3699 
3700 MatchRule::MatchRule(ArchDesc &ad, MatchNode* mroot, int depth, char *cnstr,
3701                      int numleaves)
3702   : MatchNode(ad,*mroot), _depth(depth), _construct(cnstr),
3703     _numchilds(0) {
3704       _next = NULL;
3705       mroot->_lChild = NULL;
3706       mroot->_rChild = NULL;
3707       delete mroot;
3708       _numleaves = numleaves;
3709       _numchilds = (_lChild ? 1 : 0) + (_rChild ? 1 : 0);
3710 }
3711 MatchRule::~MatchRule() {
3712 }
3713 
3714 // Recursive call collecting info on top-level operands, not transitive.
3715 // Implementation does not modify state of internal structures.
3716 void MatchRule::append_components(FormDict& locals, ComponentList& components, bool def_flag) const {
3717   assert (_name != NULL, "MatchNode::build_components encountered empty node\n");
3718 
3719   MatchNode::append_components(locals, components,
3720                                false /* not necessarily a def */);
3721 }
3722 
3723 // Recursive call on all operands' match rules in my match rule.
3724 // Implementation does not modify state of internal structures  since they
3725 // can be shared.
3726 // The MatchNode that is called first treats its
3727 bool MatchRule::base_operand(uint &position0, FormDict &globals,
3728                              const char *&result, const char * &name,
3729                              const char * &opType)const{
3730   uint position = position0;
3731 
3732   return (MatchNode::base_operand( position, globals, result, name, opType));
3733 }
3734 
3735 
3736 bool MatchRule::is_base_register(FormDict &globals) const {
3737   uint   position = 1;
3738   const char  *result   = NULL;
3739   const char  *name     = NULL;
3740   const char  *opType   = NULL;
3741   if (!base_operand(position, globals, result, name, opType)) {
3742     position = 0;
3743     if( base_operand(position, globals, result, name, opType) &&
3744         (strcmp(opType,"RegI")==0 ||
3745          strcmp(opType,"RegP")==0 ||
3746          strcmp(opType,"RegN")==0 ||
3747          strcmp(opType,"RegL")==0 ||
3748          strcmp(opType,"RegF")==0 ||
3749          strcmp(opType,"RegD")==0 ||
3750          strcmp(opType,"Reg" )==0) ) {
3751       return 1;
3752     }
3753   }
3754   return 0;
3755 }
3756 
3757 Form::DataType MatchRule::is_base_constant(FormDict &globals) const {
3758   uint         position = 1;
3759   const char  *result   = NULL;
3760   const char  *name     = NULL;
3761   const char  *opType   = NULL;
3762   if (!base_operand(position, globals, result, name, opType)) {
3763     position = 0;
3764     if (base_operand(position, globals, result, name, opType)) {
3765       return ideal_to_const_type(opType);
3766     }
3767   }
3768   return Form::none;
3769 }
3770 
3771 bool MatchRule::is_chain_rule(FormDict &globals) const {
3772 
3773   // Check for chain rule, and do not generate a match list for it
3774   if ((_lChild == NULL) && (_rChild == NULL) ) {
3775     const Form *form = globals[_opType];
3776     // If this is ideal, then it is a base match, not a chain rule.
3777     if ( form && form->is_operand() && (!form->ideal_only())) {
3778       return true;
3779     }
3780   }
3781   // Check for "Set" form of chain rule, and do not generate a match list
3782   if (_rChild) {
3783     const char *rch = _rChild->_opType;
3784     const Form *form = globals[rch];
3785     if ((!strcmp(_opType,"Set") &&
3786          ((form) && form->is_operand()))) {
3787       return true;
3788     }
3789   }
3790   return false;
3791 }
3792 
3793 int MatchRule::is_ideal_copy() const {
3794   if( _rChild ) {
3795     const char  *opType = _rChild->_opType;
3796 #if 1
3797     if( strcmp(opType,"CastIP")==0 )
3798       return 1;
3799 #else
3800     if( strcmp(opType,"CastII")==0 )
3801       return 1;
3802     // Do not treat *CastPP this way, because it
3803     // may transfer a raw pointer to an oop.
3804     // If the register allocator were to coalesce this
3805     // into a single LRG, the GC maps would be incorrect.
3806     //if( strcmp(opType,"CastPP")==0 )
3807     //  return 1;
3808     //if( strcmp(opType,"CheckCastPP")==0 )
3809     //  return 1;
3810     //
3811     // Do not treat CastX2P or CastP2X this way, because
3812     // raw pointers and int types are treated differently
3813     // when saving local & stack info for safepoints in
3814     // Output().
3815     //if( strcmp(opType,"CastX2P")==0 )
3816     //  return 1;
3817     //if( strcmp(opType,"CastP2X")==0 )
3818     //  return 1;
3819 #endif
3820   }
3821   if( is_chain_rule(_AD.globalNames()) &&
3822       _lChild && strncmp(_lChild->_opType,"stackSlot",9)==0 )
3823     return 1;
3824   return 0;
3825 }
3826 
3827 
3828 int MatchRule::is_expensive() const {
3829   if( _rChild ) {
3830     const char  *opType = _rChild->_opType;
3831     if( strcmp(opType,"AtanD")==0 ||
3832         strcmp(opType,"CosD")==0 ||
3833         strcmp(opType,"DivD")==0 ||
3834         strcmp(opType,"DivF")==0 ||
3835         strcmp(opType,"DivI")==0 ||
3836         strcmp(opType,"ExpD")==0 ||
3837         strcmp(opType,"LogD")==0 ||
3838         strcmp(opType,"Log10D")==0 ||
3839         strcmp(opType,"ModD")==0 ||
3840         strcmp(opType,"ModF")==0 ||
3841         strcmp(opType,"ModI")==0 ||
3842         strcmp(opType,"PowD")==0 ||
3843         strcmp(opType,"SinD")==0 ||
3844         strcmp(opType,"SqrtD")==0 ||
3845         strcmp(opType,"TanD")==0 ||
3846         strcmp(opType,"ConvD2F")==0 ||
3847         strcmp(opType,"ConvD2I")==0 ||
3848         strcmp(opType,"ConvD2L")==0 ||
3849         strcmp(opType,"ConvF2D")==0 ||
3850         strcmp(opType,"ConvF2I")==0 ||
3851         strcmp(opType,"ConvF2L")==0 ||
3852         strcmp(opType,"ConvI2D")==0 ||
3853         strcmp(opType,"ConvI2F")==0 ||
3854         strcmp(opType,"ConvI2L")==0 ||
3855         strcmp(opType,"ConvL2D")==0 ||
3856         strcmp(opType,"ConvL2F")==0 ||
3857         strcmp(opType,"ConvL2I")==0 ||
3858         strcmp(opType,"DecodeN")==0 ||
3859         strcmp(opType,"EncodeP")==0 ||
3860         strcmp(opType,"RoundDouble")==0 ||
3861         strcmp(opType,"RoundFloat")==0 ||
3862         strcmp(opType,"ReverseBytesI")==0 ||
3863         strcmp(opType,"ReverseBytesL")==0 ||
3864         strcmp(opType,"ReverseBytesC")==0 ||
3865         strcmp(opType,"ReverseBytesS")==0 ||
3866         strcmp(opType,"Replicate16B")==0 ||
3867         strcmp(opType,"Replicate8B")==0 ||
3868         strcmp(opType,"Replicate4B")==0 ||
3869         strcmp(opType,"Replicate8C")==0 ||
3870         strcmp(opType,"Replicate4C")==0 ||
3871         strcmp(opType,"Replicate8S")==0 ||
3872         strcmp(opType,"Replicate4S")==0 ||
3873         strcmp(opType,"Replicate4I")==0 ||
3874         strcmp(opType,"Replicate2I")==0 ||
3875         strcmp(opType,"Replicate2L")==0 ||
3876         strcmp(opType,"Replicate4F")==0 ||
3877         strcmp(opType,"Replicate2F")==0 ||
3878         strcmp(opType,"Replicate2D")==0 ||
3879         0 /* 0 to line up columns nicely */ )
3880       return 1;
3881   }
3882   return 0;
3883 }
3884 
3885 bool MatchRule::is_ideal_unlock() const {
3886   if( !_opType ) return false;
3887   return !strcmp(_opType,"Unlock") || !strcmp(_opType,"FastUnlock");
3888 }
3889 
3890 
3891 bool MatchRule::is_ideal_call_leaf() const {
3892   if( !_opType ) return false;
3893   return !strcmp(_opType,"CallLeaf")     ||
3894          !strcmp(_opType,"CallLeafNoFP");
3895 }
3896 
3897 
3898 bool MatchRule::is_ideal_if() const {
3899   if( !_opType ) return false;
3900   return
3901     !strcmp(_opType,"If"            ) ||
3902     !strcmp(_opType,"CountedLoopEnd");
3903 }
3904 
3905 bool MatchRule::is_ideal_fastlock() const {
3906   if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) {
3907     return (strcmp(_rChild->_opType,"FastLock") == 0);
3908   }
3909   return false;
3910 }
3911 
3912 bool MatchRule::is_ideal_membar() const {
3913   if( !_opType ) return false;
3914   return
3915     !strcmp(_opType,"MemBarAcquire"  ) ||
3916     !strcmp(_opType,"MemBarRelease"  ) ||
3917     !strcmp(_opType,"MemBarVolatile" ) ||
3918     !strcmp(_opType,"MemBarCPUOrder" ) ;
3919 }
3920 
3921 bool MatchRule::is_ideal_loadPC() const {
3922   if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) {
3923     return (strcmp(_rChild->_opType,"LoadPC") == 0);
3924   }
3925   return false;
3926 }
3927 
3928 bool MatchRule::is_ideal_box() const {
3929   if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) {
3930     return (strcmp(_rChild->_opType,"Box") == 0);
3931   }
3932   return false;
3933 }
3934 
3935 bool MatchRule::is_ideal_goto() const {
3936   bool   ideal_goto = false;
3937 
3938   if( _opType && (strcmp(_opType,"Goto") == 0) ) {
3939     ideal_goto = true;
3940   }
3941   return ideal_goto;
3942 }
3943 
3944 bool MatchRule::is_ideal_jump() const {
3945   if( _opType ) {
3946     if( !strcmp(_opType,"Jump") )
3947       return true;
3948   }
3949   return false;
3950 }
3951 
3952 bool MatchRule::is_ideal_bool() const {
3953   if( _opType ) {
3954     if( !strcmp(_opType,"Bool") )
3955       return true;
3956   }
3957   return false;
3958 }
3959 
3960 
3961 Form::DataType MatchRule::is_ideal_load() const {
3962   Form::DataType ideal_load = Form::none;
3963 
3964   if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) {
3965     const char *opType = _rChild->_opType;
3966     ideal_load = is_load_from_memory(opType);
3967   }
3968 
3969   return ideal_load;
3970 }
3971 
3972 
3973 bool MatchRule::skip_antidep_check() const {
3974   // Some loads operate on what is effectively immutable memory so we
3975   // should skip the anti dep computations.  For some of these nodes
3976   // the rewritable field keeps the anti dep logic from triggering but
3977   // for certain kinds of LoadKlass it does not since they are
3978   // actually reading memory which could be rewritten by the runtime,
3979   // though never by generated code.  This disables it uniformly for
3980   // the nodes that behave like this: LoadKlass, LoadNKlass and
3981   // LoadRange.
3982   if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) {
3983     const char *opType = _rChild->_opType;
3984     if (strcmp("LoadKlass", opType) == 0 ||
3985         strcmp("LoadNKlass", opType) == 0 ||
3986         strcmp("LoadRange", opType) == 0) {
3987       return true;
3988     }
3989   }
3990 
3991   return false;
3992 }
3993 
3994 
3995 Form::DataType MatchRule::is_ideal_store() const {
3996   Form::DataType ideal_store = Form::none;
3997 
3998   if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) {
3999     const char *opType = _rChild->_opType;
4000     ideal_store = is_store_to_memory(opType);
4001   }
4002 
4003   return ideal_store;
4004 }
4005 
4006 
4007 void MatchRule::dump() {
4008   output(stderr);
4009 }
4010 
4011 void MatchRule::output(FILE *fp) {
4012   fprintf(fp,"MatchRule: ( %s",_name);
4013   if (_lChild) _lChild->output(fp);
4014   if (_rChild) _rChild->output(fp);
4015   fprintf(fp," )\n");
4016   fprintf(fp,"   nesting depth = %d\n", _depth);
4017   if (_result) fprintf(fp,"   Result Type = %s", _result);
4018   fprintf(fp,"\n");
4019 }
4020 
4021 //------------------------------Attribute--------------------------------------
4022 Attribute::Attribute(char *id, char* val, int type)
4023   : _ident(id), _val(val), _atype(type) {
4024 }
4025 Attribute::~Attribute() {
4026 }
4027 
4028 int Attribute::int_val(ArchDesc &ad) {
4029   // Make sure it is an integer constant:
4030   int result = 0;
4031   if (!_val || !ADLParser::is_int_token(_val, result)) {
4032     ad.syntax_err(0, "Attribute %s must have an integer value: %s",
4033                   _ident, _val ? _val : "");
4034   }
4035   return result;
4036 }
4037 
4038 void Attribute::dump() {
4039   output(stderr);
4040 } // Debug printer
4041 
4042 // Write to output files
4043 void Attribute::output(FILE *fp) {
4044   fprintf(fp,"Attribute: %s  %s\n", (_ident?_ident:""), (_val?_val:""));
4045 }
4046 
4047 //------------------------------FormatRule----------------------------------
4048 FormatRule::FormatRule(char *temp)
4049   : _temp(temp) {
4050 }
4051 FormatRule::~FormatRule() {
4052 }
4053 
4054 void FormatRule::dump() {
4055   output(stderr);
4056 }
4057 
4058 // Write to output files
4059 void FormatRule::output(FILE *fp) {
4060   fprintf(fp,"\nFormat Rule: \n%s", (_temp?_temp:""));
4061   fprintf(fp,"\n");
4062 }