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