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