duke@435: /* xdono@631: * Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // FORMS.CPP - Definitions for ADL Parser Forms Classes duke@435: #include "adlc.hpp" duke@435: duke@435: //==============================Instructions=================================== duke@435: //------------------------------InstructForm----------------------------------- duke@435: InstructForm::InstructForm(const char *id, bool ideal_only) duke@435: : _ident(id), _ideal_only(ideal_only), duke@435: _localNames(cmpstr, hashstr, Form::arena), duke@435: _effects(cmpstr, hashstr, Form::arena) { duke@435: _ftype = Form::INS; duke@435: duke@435: _matrule = NULL; duke@435: _insencode = NULL; duke@435: _opcode = NULL; duke@435: _size = NULL; duke@435: _attribs = NULL; duke@435: _predicate = NULL; duke@435: _exprule = NULL; duke@435: _rewrule = NULL; duke@435: _format = NULL; duke@435: _peephole = NULL; duke@435: _ins_pipe = NULL; duke@435: _uniq_idx = NULL; duke@435: _num_uniq = 0; duke@435: _cisc_spill_operand = Not_cisc_spillable;// Which operand may cisc-spill duke@435: _cisc_spill_alternate = NULL; // possible cisc replacement duke@435: _cisc_reg_mask_name = NULL; duke@435: _is_cisc_alternate = false; duke@435: _is_short_branch = false; duke@435: _short_branch_form = NULL; duke@435: _alignment = 1; duke@435: } duke@435: duke@435: InstructForm::InstructForm(const char *id, InstructForm *instr, MatchRule *rule) duke@435: : _ident(id), _ideal_only(false), duke@435: _localNames(instr->_localNames), duke@435: _effects(instr->_effects) { duke@435: _ftype = Form::INS; duke@435: duke@435: _matrule = rule; duke@435: _insencode = instr->_insencode; duke@435: _opcode = instr->_opcode; duke@435: _size = instr->_size; duke@435: _attribs = instr->_attribs; duke@435: _predicate = instr->_predicate; duke@435: _exprule = instr->_exprule; duke@435: _rewrule = instr->_rewrule; duke@435: _format = instr->_format; duke@435: _peephole = instr->_peephole; duke@435: _ins_pipe = instr->_ins_pipe; duke@435: _uniq_idx = instr->_uniq_idx; duke@435: _num_uniq = instr->_num_uniq; duke@435: _cisc_spill_operand = Not_cisc_spillable;// Which operand may cisc-spill duke@435: _cisc_spill_alternate = NULL; // possible cisc replacement duke@435: _cisc_reg_mask_name = NULL; duke@435: _is_cisc_alternate = false; duke@435: _is_short_branch = false; duke@435: _short_branch_form = NULL; duke@435: _alignment = 1; duke@435: // Copy parameters duke@435: const char *name; duke@435: instr->_parameters.reset(); duke@435: for (; (name = instr->_parameters.iter()) != NULL;) duke@435: _parameters.addName(name); duke@435: } duke@435: duke@435: InstructForm::~InstructForm() { duke@435: } duke@435: duke@435: InstructForm *InstructForm::is_instruction() const { duke@435: return (InstructForm*)this; duke@435: } duke@435: duke@435: bool InstructForm::ideal_only() const { duke@435: return _ideal_only; duke@435: } duke@435: duke@435: bool InstructForm::sets_result() const { duke@435: return (_matrule != NULL && _matrule->sets_result()); duke@435: } duke@435: duke@435: bool InstructForm::needs_projections() { duke@435: _components.reset(); duke@435: for( Component *comp; (comp = _components.iter()) != NULL; ) { duke@435: if (comp->isa(Component::KILL)) { duke@435: return true; duke@435: } duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: duke@435: bool InstructForm::has_temps() { duke@435: if (_matrule) { duke@435: // Examine each component to see if it is a TEMP duke@435: _components.reset(); duke@435: // Skip the first component, if already handled as (SET dst (...)) duke@435: Component *comp = NULL; duke@435: if (sets_result()) comp = _components.iter(); duke@435: while ((comp = _components.iter()) != NULL) { duke@435: if (comp->isa(Component::TEMP)) { duke@435: return true; duke@435: } duke@435: } duke@435: } duke@435: duke@435: return false; duke@435: } duke@435: duke@435: uint InstructForm::num_defs_or_kills() { duke@435: uint defs_or_kills = 0; duke@435: duke@435: _components.reset(); duke@435: for( Component *comp; (comp = _components.iter()) != NULL; ) { duke@435: if( comp->isa(Component::DEF) || comp->isa(Component::KILL) ) { duke@435: ++defs_or_kills; duke@435: } duke@435: } duke@435: duke@435: return defs_or_kills; duke@435: } duke@435: duke@435: // This instruction has an expand rule? duke@435: bool InstructForm::expands() const { duke@435: return ( _exprule != NULL ); duke@435: } duke@435: duke@435: // This instruction has a peephole rule? duke@435: Peephole *InstructForm::peepholes() const { duke@435: return _peephole; duke@435: } duke@435: duke@435: // This instruction has a peephole rule? duke@435: void InstructForm::append_peephole(Peephole *peephole) { duke@435: if( _peephole == NULL ) { duke@435: _peephole = peephole; duke@435: } else { duke@435: _peephole->append_peephole(peephole); duke@435: } duke@435: } duke@435: duke@435: duke@435: // ideal opcode enumeration duke@435: const char *InstructForm::ideal_Opcode( FormDict &globalNames ) const { duke@435: if( !_matrule ) return "Node"; // Something weird duke@435: // Chain rules do not really have ideal Opcodes; use their source duke@435: // operand ideal Opcode instead. duke@435: if( is_simple_chain_rule(globalNames) ) { duke@435: const char *src = _matrule->_rChild->_opType; duke@435: OperandForm *src_op = globalNames[src]->is_operand(); duke@435: assert( src_op, "Not operand class of chain rule" ); duke@435: if( !src_op->_matrule ) return "Node"; duke@435: return src_op->_matrule->_opType; duke@435: } duke@435: // Operand chain rules do not really have ideal Opcodes duke@435: if( _matrule->is_chain_rule(globalNames) ) duke@435: return "Node"; duke@435: return strcmp(_matrule->_opType,"Set") duke@435: ? _matrule->_opType duke@435: : _matrule->_rChild->_opType; duke@435: } duke@435: duke@435: // Recursive check on all operands' match rules in my match rule duke@435: bool InstructForm::is_pinned(FormDict &globals) { duke@435: if ( ! _matrule) return false; duke@435: duke@435: int index = 0; duke@435: if (_matrule->find_type("Goto", index)) return true; duke@435: if (_matrule->find_type("If", index)) return true; duke@435: if (_matrule->find_type("CountedLoopEnd",index)) return true; duke@435: if (_matrule->find_type("Return", index)) return true; duke@435: if (_matrule->find_type("Rethrow", index)) return true; duke@435: if (_matrule->find_type("TailCall", index)) return true; duke@435: if (_matrule->find_type("TailJump", index)) return true; duke@435: if (_matrule->find_type("Halt", index)) return true; duke@435: if (_matrule->find_type("Jump", index)) return true; duke@435: duke@435: return is_parm(globals); duke@435: } duke@435: duke@435: // Recursive check on all operands' match rules in my match rule duke@435: bool InstructForm::is_projection(FormDict &globals) { duke@435: if ( ! _matrule) return false; duke@435: duke@435: int index = 0; duke@435: if (_matrule->find_type("Goto", index)) return true; duke@435: if (_matrule->find_type("Return", index)) return true; duke@435: if (_matrule->find_type("Rethrow", index)) return true; duke@435: if (_matrule->find_type("TailCall",index)) return true; duke@435: if (_matrule->find_type("TailJump",index)) return true; duke@435: if (_matrule->find_type("Halt", index)) return true; duke@435: duke@435: return false; duke@435: } duke@435: duke@435: // Recursive check on all operands' match rules in my match rule duke@435: bool InstructForm::is_parm(FormDict &globals) { duke@435: if ( ! _matrule) return false; duke@435: duke@435: int index = 0; duke@435: if (_matrule->find_type("Parm",index)) return true; duke@435: duke@435: return false; duke@435: } duke@435: duke@435: duke@435: // Return 'true' if this instruction matches an ideal 'Copy*' node duke@435: int InstructForm::is_ideal_copy() const { duke@435: return _matrule ? _matrule->is_ideal_copy() : 0; duke@435: } duke@435: duke@435: // Return 'true' if this instruction is too complex to rematerialize. duke@435: int InstructForm::is_expensive() const { duke@435: // We can prove it is cheap if it has an empty encoding. duke@435: // This helps with platform-specific nops like ThreadLocal and RoundFloat. duke@435: if (is_empty_encoding()) duke@435: return 0; duke@435: duke@435: if (is_tls_instruction()) duke@435: return 1; duke@435: duke@435: if (_matrule == NULL) return 0; duke@435: duke@435: return _matrule->is_expensive(); duke@435: } duke@435: duke@435: // Has an empty encoding if _size is a constant zero or there duke@435: // are no ins_encode tokens. duke@435: int InstructForm::is_empty_encoding() const { duke@435: if (_insencode != NULL) { duke@435: _insencode->reset(); duke@435: if (_insencode->encode_class_iter() == NULL) { duke@435: return 1; duke@435: } duke@435: } duke@435: if (_size != NULL && strcmp(_size, "0") == 0) { duke@435: return 1; duke@435: } duke@435: return 0; duke@435: } duke@435: duke@435: int InstructForm::is_tls_instruction() const { duke@435: if (_ident != NULL && duke@435: ( ! strcmp( _ident,"tlsLoadP") || duke@435: ! strncmp(_ident,"tlsLoadP_",9)) ) { duke@435: return 1; duke@435: } duke@435: duke@435: if (_matrule != NULL && _insencode != NULL) { duke@435: const char* opType = _matrule->_opType; duke@435: if (strcmp(opType, "Set")==0) duke@435: opType = _matrule->_rChild->_opType; duke@435: if (strcmp(opType,"ThreadLocal")==0) { duke@435: fprintf(stderr, "Warning: ThreadLocal instruction %s should be named 'tlsLoadP_*'\n", duke@435: (_ident == NULL ? "NULL" : _ident)); duke@435: return 1; duke@435: } duke@435: } duke@435: duke@435: return 0; duke@435: } duke@435: duke@435: duke@435: // Return 'true' if this instruction matches an ideal 'Copy*' node duke@435: bool InstructForm::is_ideal_unlock() const { duke@435: return _matrule ? _matrule->is_ideal_unlock() : false; duke@435: } duke@435: duke@435: bool InstructForm::is_ideal_call_leaf() const { duke@435: return _matrule ? _matrule->is_ideal_call_leaf() : false; duke@435: } duke@435: duke@435: // Return 'true' if this instruction matches an ideal 'If' node duke@435: bool InstructForm::is_ideal_if() const { duke@435: if( _matrule == NULL ) return false; duke@435: duke@435: return _matrule->is_ideal_if(); duke@435: } duke@435: duke@435: // Return 'true' if this instruction matches an ideal 'FastLock' node duke@435: bool InstructForm::is_ideal_fastlock() const { duke@435: if( _matrule == NULL ) return false; duke@435: duke@435: return _matrule->is_ideal_fastlock(); duke@435: } duke@435: duke@435: // Return 'true' if this instruction matches an ideal 'MemBarXXX' node duke@435: bool InstructForm::is_ideal_membar() const { duke@435: if( _matrule == NULL ) return false; duke@435: duke@435: return _matrule->is_ideal_membar(); duke@435: } duke@435: duke@435: // Return 'true' if this instruction matches an ideal 'LoadPC' node duke@435: bool InstructForm::is_ideal_loadPC() const { duke@435: if( _matrule == NULL ) return false; duke@435: duke@435: return _matrule->is_ideal_loadPC(); duke@435: } duke@435: duke@435: // Return 'true' if this instruction matches an ideal 'Box' node duke@435: bool InstructForm::is_ideal_box() const { duke@435: if( _matrule == NULL ) return false; duke@435: duke@435: return _matrule->is_ideal_box(); duke@435: } duke@435: duke@435: // Return 'true' if this instruction matches an ideal 'Goto' node duke@435: bool InstructForm::is_ideal_goto() const { duke@435: if( _matrule == NULL ) return false; duke@435: duke@435: return _matrule->is_ideal_goto(); duke@435: } duke@435: duke@435: // Return 'true' if this instruction matches an ideal 'Jump' node duke@435: bool InstructForm::is_ideal_jump() const { duke@435: if( _matrule == NULL ) return false; duke@435: duke@435: return _matrule->is_ideal_jump(); duke@435: } duke@435: duke@435: // Return 'true' if instruction matches ideal 'If' | 'Goto' | duke@435: // 'CountedLoopEnd' | 'Jump' duke@435: bool InstructForm::is_ideal_branch() const { duke@435: if( _matrule == NULL ) return false; duke@435: duke@435: return _matrule->is_ideal_if() || _matrule->is_ideal_goto() || _matrule->is_ideal_jump(); duke@435: } duke@435: duke@435: duke@435: // Return 'true' if this instruction matches an ideal 'Return' node duke@435: bool InstructForm::is_ideal_return() const { duke@435: if( _matrule == NULL ) return false; duke@435: duke@435: // Check MatchRule to see if the first entry is the ideal "Return" node duke@435: int index = 0; duke@435: if (_matrule->find_type("Return",index)) return true; duke@435: if (_matrule->find_type("Rethrow",index)) return true; duke@435: if (_matrule->find_type("TailCall",index)) return true; duke@435: if (_matrule->find_type("TailJump",index)) return true; duke@435: duke@435: return false; duke@435: } duke@435: duke@435: // Return 'true' if this instruction matches an ideal 'Halt' node duke@435: bool InstructForm::is_ideal_halt() const { duke@435: int index = 0; duke@435: return _matrule && _matrule->find_type("Halt",index); duke@435: } duke@435: duke@435: // Return 'true' if this instruction matches an ideal 'SafePoint' node duke@435: bool InstructForm::is_ideal_safepoint() const { duke@435: int index = 0; duke@435: return _matrule && _matrule->find_type("SafePoint",index); duke@435: } duke@435: duke@435: // Return 'true' if this instruction matches an ideal 'Nop' node duke@435: bool InstructForm::is_ideal_nop() const { duke@435: return _ident && _ident[0] == 'N' && _ident[1] == 'o' && _ident[2] == 'p' && _ident[3] == '_'; duke@435: } duke@435: duke@435: bool InstructForm::is_ideal_control() const { duke@435: if ( ! _matrule) return false; duke@435: duke@435: return is_ideal_return() || is_ideal_branch() || is_ideal_halt(); duke@435: } duke@435: duke@435: // Return 'true' if this instruction matches an ideal 'Call' node duke@435: Form::CallType InstructForm::is_ideal_call() const { duke@435: if( _matrule == NULL ) return Form::invalid_type; duke@435: duke@435: // Check MatchRule to see if the first entry is the ideal "Call" node duke@435: int idx = 0; duke@435: if(_matrule->find_type("CallStaticJava",idx)) return Form::JAVA_STATIC; duke@435: idx = 0; duke@435: if(_matrule->find_type("Lock",idx)) return Form::JAVA_STATIC; duke@435: idx = 0; duke@435: if(_matrule->find_type("Unlock",idx)) return Form::JAVA_STATIC; duke@435: idx = 0; duke@435: if(_matrule->find_type("CallDynamicJava",idx)) return Form::JAVA_DYNAMIC; duke@435: idx = 0; duke@435: if(_matrule->find_type("CallRuntime",idx)) return Form::JAVA_RUNTIME; duke@435: idx = 0; duke@435: if(_matrule->find_type("CallLeaf",idx)) return Form::JAVA_LEAF; duke@435: idx = 0; duke@435: if(_matrule->find_type("CallLeafNoFP",idx)) return Form::JAVA_LEAF; duke@435: idx = 0; duke@435: duke@435: return Form::invalid_type; duke@435: } duke@435: duke@435: // Return 'true' if this instruction matches an ideal 'Load?' node duke@435: Form::DataType InstructForm::is_ideal_load() const { duke@435: if( _matrule == NULL ) return Form::none; duke@435: duke@435: return _matrule->is_ideal_load(); duke@435: } duke@435: duke@435: // Return 'true' if this instruction matches an ideal 'Load?' node duke@435: Form::DataType InstructForm::is_ideal_store() const { duke@435: if( _matrule == NULL ) return Form::none; duke@435: duke@435: return _matrule->is_ideal_store(); duke@435: } duke@435: duke@435: // Return the input register that must match the output register duke@435: // If this is not required, return 0 duke@435: uint InstructForm::two_address(FormDict &globals) { duke@435: uint matching_input = 0; duke@435: if(_components.count() == 0) return 0; duke@435: duke@435: _components.reset(); duke@435: Component *comp = _components.iter(); duke@435: // Check if there is a DEF duke@435: if( comp->isa(Component::DEF) ) { duke@435: // Check that this is a register duke@435: const char *def_type = comp->_type; duke@435: const Form *form = globals[def_type]; duke@435: OperandForm *op = form->is_operand(); duke@435: if( op ) { duke@435: if( op->constrained_reg_class() != NULL && duke@435: op->interface_type(globals) == Form::register_interface ) { duke@435: // Remember the local name for equality test later duke@435: const char *def_name = comp->_name; duke@435: // Check if a component has the same name and is a USE duke@435: do { duke@435: if( comp->isa(Component::USE) && strcmp(comp->_name,def_name)==0 ) { duke@435: return operand_position_format(def_name); duke@435: } duke@435: } while( (comp = _components.iter()) != NULL); duke@435: } duke@435: } duke@435: } duke@435: duke@435: return 0; duke@435: } duke@435: duke@435: duke@435: // when chaining a constant to an instruction, returns 'true' and sets opType duke@435: Form::DataType InstructForm::is_chain_of_constant(FormDict &globals) { duke@435: const char *dummy = NULL; duke@435: const char *dummy2 = NULL; duke@435: return is_chain_of_constant(globals, dummy, dummy2); duke@435: } duke@435: Form::DataType InstructForm::is_chain_of_constant(FormDict &globals, duke@435: const char * &opTypeParam) { duke@435: const char *result = NULL; duke@435: duke@435: return is_chain_of_constant(globals, opTypeParam, result); duke@435: } duke@435: duke@435: Form::DataType InstructForm::is_chain_of_constant(FormDict &globals, duke@435: const char * &opTypeParam, const char * &resultParam) { duke@435: Form::DataType data_type = Form::none; duke@435: if ( ! _matrule) return data_type; duke@435: duke@435: // !!!!! duke@435: // The source of the chain rule is 'position = 1' duke@435: uint position = 1; duke@435: const char *result = NULL; duke@435: const char *name = NULL; duke@435: const char *opType = NULL; duke@435: // Here base_operand is looking for an ideal type to be returned (opType). duke@435: if ( _matrule->is_chain_rule(globals) duke@435: && _matrule->base_operand(position, globals, result, name, opType) ) { duke@435: data_type = ideal_to_const_type(opType); duke@435: duke@435: // if it isn't an ideal constant type, just return duke@435: if ( data_type == Form::none ) return data_type; duke@435: duke@435: // Ideal constant types also adjust the opType parameter. duke@435: resultParam = result; duke@435: opTypeParam = opType; duke@435: return data_type; duke@435: } duke@435: duke@435: return data_type; duke@435: } duke@435: duke@435: // Check if a simple chain rule duke@435: bool InstructForm::is_simple_chain_rule(FormDict &globals) const { duke@435: if( _matrule && _matrule->sets_result() duke@435: && _matrule->_rChild->_lChild == NULL duke@435: && globals[_matrule->_rChild->_opType] duke@435: && globals[_matrule->_rChild->_opType]->is_opclass() ) { duke@435: return true; duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: // check for structural rematerialization duke@435: bool InstructForm::rematerialize(FormDict &globals, RegisterForm *registers ) { duke@435: bool rematerialize = false; duke@435: duke@435: Form::DataType data_type = is_chain_of_constant(globals); duke@435: if( data_type != Form::none ) duke@435: rematerialize = true; duke@435: duke@435: // Constants duke@435: if( _components.count() == 1 && _components[0]->is(Component::USE_DEF) ) duke@435: rematerialize = true; duke@435: duke@435: // Pseudo-constants (values easily available to the runtime) duke@435: if (is_empty_encoding() && is_tls_instruction()) duke@435: rematerialize = true; duke@435: duke@435: // 1-input, 1-output, such as copies or increments. duke@435: if( _components.count() == 2 && duke@435: _components[0]->is(Component::DEF) && duke@435: _components[1]->isa(Component::USE) ) duke@435: rematerialize = true; duke@435: duke@435: // Check for an ideal 'Load?' and eliminate rematerialize option duke@435: if ( is_ideal_load() != Form::none || // Ideal load? Do not rematerialize duke@435: is_ideal_copy() != Form::none || // Ideal copy? Do not rematerialize duke@435: is_expensive() != Form::none) { // Expensive? Do not rematerialize duke@435: rematerialize = false; duke@435: } duke@435: duke@435: // Always rematerialize the flags. They are more expensive to save & duke@435: // restore than to recompute (and possibly spill the compare's inputs). duke@435: if( _components.count() >= 1 ) { duke@435: Component *c = _components[0]; duke@435: const Form *form = globals[c->_type]; duke@435: OperandForm *opform = form->is_operand(); duke@435: if( opform ) { duke@435: // Avoid the special stack_slots register classes duke@435: const char *rc_name = opform->constrained_reg_class(); duke@435: if( rc_name ) { duke@435: if( strcmp(rc_name,"stack_slots") ) { duke@435: // Check for ideal_type of RegFlags duke@435: const char *type = opform->ideal_type( globals, registers ); duke@435: if( !strcmp(type,"RegFlags") ) duke@435: rematerialize = true; duke@435: } else duke@435: rematerialize = false; // Do not rematerialize things target stk duke@435: } duke@435: } duke@435: } duke@435: duke@435: return rematerialize; duke@435: } duke@435: duke@435: // loads from memory, so must check for anti-dependence duke@435: bool InstructForm::needs_anti_dependence_check(FormDict &globals) const { duke@435: // Machine independent loads must be checked for anti-dependences duke@435: if( is_ideal_load() != Form::none ) return true; duke@435: duke@435: // !!!!! !!!!! !!!!! duke@435: // TEMPORARY duke@435: // if( is_simple_chain_rule(globals) ) return false; duke@435: duke@435: // String-compare uses many memorys edges, but writes none duke@435: if( _matrule && _matrule->_rChild && duke@435: strcmp(_matrule->_rChild->_opType,"StrComp")==0 ) duke@435: return true; duke@435: duke@435: // Check if instruction has a USE of a memory operand class, but no defs duke@435: bool USE_of_memory = false; duke@435: bool DEF_of_memory = false; duke@435: Component *comp = NULL; duke@435: ComponentList &components = (ComponentList &)_components; duke@435: duke@435: components.reset(); duke@435: while( (comp = components.iter()) != NULL ) { duke@435: const Form *form = globals[comp->_type]; duke@435: if( !form ) continue; duke@435: OpClassForm *op = form->is_opclass(); duke@435: if( !op ) continue; duke@435: if( form->interface_type(globals) == Form::memory_interface ) { duke@435: if( comp->isa(Component::USE) ) USE_of_memory = true; duke@435: if( comp->isa(Component::DEF) ) { duke@435: OperandForm *oper = form->is_operand(); duke@435: if( oper && oper->is_user_name_for_sReg() ) { duke@435: // Stack slots are unaliased memory handled by allocator duke@435: oper = oper; // debug stopping point !!!!! duke@435: } else { duke@435: DEF_of_memory = true; duke@435: } duke@435: } duke@435: } duke@435: } duke@435: return (USE_of_memory && !DEF_of_memory); duke@435: } duke@435: duke@435: duke@435: bool InstructForm::is_wide_memory_kill(FormDict &globals) const { duke@435: if( _matrule == NULL ) return false; duke@435: if( !_matrule->_opType ) return false; duke@435: duke@435: if( strcmp(_matrule->_opType,"MemBarRelease") == 0 ) return true; duke@435: if( strcmp(_matrule->_opType,"MemBarAcquire") == 0 ) return true; duke@435: duke@435: return false; duke@435: } duke@435: duke@435: int InstructForm::memory_operand(FormDict &globals) const { duke@435: // Machine independent loads must be checked for anti-dependences duke@435: // Check if instruction has a USE of a memory operand class, or a def. duke@435: int USE_of_memory = 0; duke@435: int DEF_of_memory = 0; duke@435: const char* last_memory_DEF = NULL; // to test DEF/USE pairing in asserts duke@435: Component *unique = NULL; duke@435: Component *comp = NULL; duke@435: ComponentList &components = (ComponentList &)_components; duke@435: duke@435: components.reset(); duke@435: while( (comp = components.iter()) != NULL ) { duke@435: const Form *form = globals[comp->_type]; duke@435: if( !form ) continue; duke@435: OpClassForm *op = form->is_opclass(); duke@435: if( !op ) continue; duke@435: if( op->stack_slots_only(globals) ) continue; duke@435: if( form->interface_type(globals) == Form::memory_interface ) { duke@435: if( comp->isa(Component::DEF) ) { duke@435: last_memory_DEF = comp->_name; duke@435: DEF_of_memory++; duke@435: unique = comp; duke@435: } else if( comp->isa(Component::USE) ) { duke@435: if( last_memory_DEF != NULL ) { duke@435: assert(0 == strcmp(last_memory_DEF, comp->_name), "every memory DEF is followed by a USE of the same name"); duke@435: last_memory_DEF = NULL; duke@435: } duke@435: USE_of_memory++; duke@435: if (DEF_of_memory == 0) // defs take precedence duke@435: unique = comp; duke@435: } else { duke@435: assert(last_memory_DEF == NULL, "unpaired memory DEF"); duke@435: } duke@435: } duke@435: } duke@435: assert(last_memory_DEF == NULL, "unpaired memory DEF"); duke@435: assert(USE_of_memory >= DEF_of_memory, "unpaired memory DEF"); duke@435: USE_of_memory -= DEF_of_memory; // treat paired DEF/USE as one occurrence duke@435: if( (USE_of_memory + DEF_of_memory) > 0 ) { duke@435: if( is_simple_chain_rule(globals) ) { duke@435: //fprintf(stderr, "Warning: chain rule is not really a memory user.\n"); duke@435: //((InstructForm*)this)->dump(); duke@435: // Preceding code prints nothing on sparc and these insns on intel: duke@435: // leaP8 leaP32 leaPIdxOff leaPIdxScale leaPIdxScaleOff leaP8 leaP32 duke@435: // leaPIdxOff leaPIdxScale leaPIdxScaleOff duke@435: return NO_MEMORY_OPERAND; duke@435: } duke@435: duke@435: if( DEF_of_memory == 1 ) { duke@435: assert(unique != NULL, ""); duke@435: if( USE_of_memory == 0 ) { duke@435: // unique def, no uses duke@435: } else { duke@435: // // unique def, some uses duke@435: // // must return bottom unless all uses match def duke@435: // unique = NULL; duke@435: } duke@435: } else if( DEF_of_memory > 0 ) { duke@435: // multiple defs, don't care about uses duke@435: unique = NULL; duke@435: } else if( USE_of_memory == 1) { duke@435: // unique use, no defs duke@435: assert(unique != NULL, ""); duke@435: } else if( USE_of_memory > 0 ) { duke@435: // multiple uses, no defs duke@435: unique = NULL; duke@435: } else { duke@435: assert(false, "bad case analysis"); duke@435: } duke@435: // process the unique DEF or USE, if there is one duke@435: if( unique == NULL ) { duke@435: return MANY_MEMORY_OPERANDS; duke@435: } else { duke@435: int pos = components.operand_position(unique->_name); duke@435: if( unique->isa(Component::DEF) ) { duke@435: pos += 1; // get corresponding USE from DEF duke@435: } duke@435: assert(pos >= 1, "I was just looking at it!"); duke@435: return pos; duke@435: } duke@435: } duke@435: duke@435: // missed the memory op?? duke@435: if( true ) { // %%% should not be necessary duke@435: if( is_ideal_store() != Form::none ) { duke@435: fprintf(stderr, "Warning: cannot find memory opnd in instr.\n"); duke@435: ((InstructForm*)this)->dump(); duke@435: // pretend it has multiple defs and uses duke@435: return MANY_MEMORY_OPERANDS; duke@435: } duke@435: if( is_ideal_load() != Form::none ) { duke@435: fprintf(stderr, "Warning: cannot find memory opnd in instr.\n"); duke@435: ((InstructForm*)this)->dump(); duke@435: // pretend it has multiple uses and no defs duke@435: return MANY_MEMORY_OPERANDS; duke@435: } duke@435: } duke@435: duke@435: return NO_MEMORY_OPERAND; duke@435: } duke@435: duke@435: duke@435: // This instruction captures the machine-independent bottom_type duke@435: // Expected use is for pointer vs oop determination for LoadP duke@435: bool InstructForm::captures_bottom_type() const { duke@435: if( _matrule && _matrule->_rChild && duke@435: (!strcmp(_matrule->_rChild->_opType,"CastPP") || // new result type duke@435: !strcmp(_matrule->_rChild->_opType,"CastX2P") || // new result type coleenp@548: !strcmp(_matrule->_rChild->_opType,"DecodeN") || coleenp@548: !strcmp(_matrule->_rChild->_opType,"EncodeP") || coleenp@548: !strcmp(_matrule->_rChild->_opType,"LoadN") || duke@435: !strcmp(_matrule->_rChild->_opType,"CreateEx") || // type of exception duke@435: !strcmp(_matrule->_rChild->_opType,"CheckCastPP")) ) return true; duke@435: else if ( is_ideal_load() == Form::idealP ) return true; duke@435: else if ( is_ideal_store() != Form::none ) return true; duke@435: duke@435: return false; duke@435: } duke@435: duke@435: duke@435: // Access instr_cost attribute or return NULL. duke@435: const char* InstructForm::cost() { duke@435: for (Attribute* cur = _attribs; cur != NULL; cur = (Attribute*)cur->_next) { duke@435: if( strcmp(cur->_ident,AttributeForm::_ins_cost) == 0 ) { duke@435: return cur->_val; duke@435: } duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: // Return count of top-level operands. duke@435: uint InstructForm::num_opnds() { duke@435: int num_opnds = _components.num_operands(); duke@435: duke@435: // Need special handling for matching some ideal nodes duke@435: // i.e. Matching a return node duke@435: /* duke@435: if( _matrule ) { duke@435: if( strcmp(_matrule->_opType,"Return" )==0 || duke@435: strcmp(_matrule->_opType,"Halt" )==0 ) duke@435: return 3; duke@435: } duke@435: */ duke@435: return num_opnds; duke@435: } duke@435: duke@435: // Return count of unmatched operands. duke@435: uint InstructForm::num_post_match_opnds() { duke@435: uint num_post_match_opnds = _components.count(); duke@435: uint num_match_opnds = _components.match_count(); duke@435: num_post_match_opnds = num_post_match_opnds - num_match_opnds; duke@435: duke@435: return num_post_match_opnds; duke@435: } duke@435: duke@435: // Return the number of leaves below this complex operand duke@435: uint InstructForm::num_consts(FormDict &globals) const { duke@435: if ( ! _matrule) return 0; duke@435: duke@435: // This is a recursive invocation on all operands in the matchrule duke@435: return _matrule->num_consts(globals); duke@435: } duke@435: duke@435: // Constants in match rule with specified type duke@435: uint InstructForm::num_consts(FormDict &globals, Form::DataType type) const { duke@435: if ( ! _matrule) return 0; duke@435: duke@435: // This is a recursive invocation on all operands in the matchrule duke@435: return _matrule->num_consts(globals, type); duke@435: } duke@435: duke@435: duke@435: // Return the register class associated with 'leaf'. duke@435: const char *InstructForm::out_reg_class(FormDict &globals) { duke@435: assert( false, "InstructForm::out_reg_class(FormDict &globals); Not Implemented"); duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: duke@435: duke@435: // Lookup the starting position of inputs we are interested in wrt. ideal nodes duke@435: uint InstructForm::oper_input_base(FormDict &globals) { duke@435: if( !_matrule ) return 1; // Skip control for most nodes duke@435: duke@435: // Need special handling for matching some ideal nodes duke@435: // i.e. Matching a return node duke@435: if( strcmp(_matrule->_opType,"Return" )==0 || duke@435: strcmp(_matrule->_opType,"Rethrow" )==0 || duke@435: strcmp(_matrule->_opType,"TailCall" )==0 || duke@435: strcmp(_matrule->_opType,"TailJump" )==0 || duke@435: strcmp(_matrule->_opType,"SafePoint" )==0 || duke@435: strcmp(_matrule->_opType,"Halt" )==0 ) duke@435: return AdlcVMDeps::Parms; // Skip the machine-state edges duke@435: duke@435: if( _matrule->_rChild && duke@435: strcmp(_matrule->_rChild->_opType,"StrComp")==0 ) { duke@435: // String compare takes 1 control and 4 memory edges. duke@435: return 5; duke@435: } duke@435: duke@435: // Check for handling of 'Memory' input/edge in the ideal world. duke@435: // The AD file writer is shielded from knowledge of these edges. duke@435: int base = 1; // Skip control duke@435: base += _matrule->needs_ideal_memory_edge(globals); duke@435: duke@435: // Also skip the base-oop value for uses of derived oops. duke@435: // The AD file writer is shielded from knowledge of these edges. duke@435: base += needs_base_oop_edge(globals); duke@435: duke@435: return base; duke@435: } duke@435: duke@435: // Implementation does not modify state of internal structures duke@435: void InstructForm::build_components() { duke@435: // Add top-level operands to the components duke@435: if (_matrule) _matrule->append_components(_localNames, _components); duke@435: duke@435: // Add parameters that "do not appear in match rule". duke@435: bool has_temp = false; duke@435: const char *name; duke@435: const char *kill_name = NULL; duke@435: for (_parameters.reset(); (name = _parameters.iter()) != NULL;) { duke@435: OperandForm *opForm = (OperandForm*)_localNames[name]; duke@435: duke@435: const Form *form = _effects[name]; duke@435: Effect *e = form ? form->is_effect() : NULL; duke@435: if (e != NULL) { duke@435: has_temp |= e->is(Component::TEMP); duke@435: duke@435: // KILLs must be declared after any TEMPs because TEMPs are real duke@435: // uses so their operand numbering must directly follow the real duke@435: // inputs from the match rule. Fixing the numbering seems duke@435: // complex so simply enforce the restriction during parse. duke@435: if (kill_name != NULL && duke@435: e->isa(Component::TEMP) && !e->isa(Component::DEF)) { duke@435: OperandForm* kill = (OperandForm*)_localNames[kill_name]; duke@435: globalAD->syntax_err(_linenum, "%s: %s %s must be at the end of the argument list\n", duke@435: _ident, kill->_ident, kill_name); duke@435: } else if (e->isa(Component::KILL)) { duke@435: kill_name = name; duke@435: } duke@435: duke@435: // TEMPs are real uses and need to be among the first parameters duke@435: // listed, otherwise the numbering of operands and inputs gets duke@435: // screwy, so enforce this restriction during parse. duke@435: if (kill_name != NULL && duke@435: e->isa(Component::TEMP) && !e->isa(Component::DEF)) { duke@435: OperandForm* kill = (OperandForm*)_localNames[kill_name]; duke@435: globalAD->syntax_err(_linenum, "%s: %s %s must follow %s %s in the argument list\n", duke@435: _ident, kill->_ident, kill_name, opForm->_ident, name); duke@435: } else if (e->isa(Component::KILL)) { duke@435: kill_name = name; duke@435: } duke@435: } duke@435: duke@435: const Component *component = _components.search(name); duke@435: if ( component == NULL ) { duke@435: if (e) { duke@435: _components.insert(name, opForm->_ident, e->_use_def, false); duke@435: component = _components.search(name); duke@435: if (component->isa(Component::USE) && !component->isa(Component::TEMP) && _matrule) { duke@435: const Form *form = globalAD->globalNames()[component->_type]; duke@435: assert( form, "component type must be a defined form"); duke@435: OperandForm *op = form->is_operand(); duke@435: if (op->_interface && op->_interface->is_RegInterface()) { duke@435: globalAD->syntax_err(_linenum, "%s: illegal USE of non-input: %s %s\n", duke@435: _ident, opForm->_ident, name); duke@435: } duke@435: } duke@435: } else { duke@435: // This would be a nice warning but it triggers in a few places in a benign way duke@435: // if (_matrule != NULL && !expands()) { duke@435: // globalAD->syntax_err(_linenum, "%s: %s %s not mentioned in effect or match rule\n", duke@435: // _ident, opForm->_ident, name); duke@435: // } duke@435: _components.insert(name, opForm->_ident, Component::INVALID, false); duke@435: } duke@435: } duke@435: else if (e) { duke@435: // Component was found in the list duke@435: // Check if there is a new effect that requires an extra component. duke@435: // This happens when adding 'USE' to a component that is not yet one. duke@435: if ((!component->isa( Component::USE) && ((e->_use_def & Component::USE) != 0))) { duke@435: if (component->isa(Component::USE) && _matrule) { duke@435: const Form *form = globalAD->globalNames()[component->_type]; duke@435: assert( form, "component type must be a defined form"); duke@435: OperandForm *op = form->is_operand(); duke@435: if (op->_interface && op->_interface->is_RegInterface()) { duke@435: globalAD->syntax_err(_linenum, "%s: illegal USE of non-input: %s %s\n", duke@435: _ident, opForm->_ident, name); duke@435: } duke@435: } duke@435: _components.insert(name, opForm->_ident, e->_use_def, false); duke@435: } else { duke@435: Component *comp = (Component*)component; duke@435: comp->promote_use_def_info(e->_use_def); duke@435: } duke@435: // Component positions are zero based. duke@435: int pos = _components.operand_position(name); duke@435: assert( ! (component->isa(Component::DEF) && (pos >= 1)), duke@435: "Component::DEF can only occur in the first position"); duke@435: } duke@435: } duke@435: duke@435: // Resolving the interactions between expand rules and TEMPs would duke@435: // be complex so simply disallow it. duke@435: if (_matrule == NULL && has_temp) { duke@435: globalAD->syntax_err(_linenum, "%s: TEMPs without match rule isn't supported\n", _ident); duke@435: } duke@435: duke@435: return; duke@435: } duke@435: duke@435: // Return zero-based position in component list; -1 if not in list. duke@435: int InstructForm::operand_position(const char *name, int usedef) { duke@435: return unique_opnds_idx(_components.operand_position(name, usedef)); duke@435: } duke@435: duke@435: int InstructForm::operand_position_format(const char *name) { duke@435: return unique_opnds_idx(_components.operand_position_format(name)); duke@435: } duke@435: duke@435: // Return zero-based position in component list; -1 if not in list. duke@435: int InstructForm::label_position() { duke@435: return unique_opnds_idx(_components.label_position()); duke@435: } duke@435: duke@435: int InstructForm::method_position() { duke@435: return unique_opnds_idx(_components.method_position()); duke@435: } duke@435: duke@435: // Return number of relocation entries needed for this instruction. duke@435: uint InstructForm::reloc(FormDict &globals) { duke@435: uint reloc_entries = 0; duke@435: // Check for "Call" nodes duke@435: if ( is_ideal_call() ) ++reloc_entries; duke@435: if ( is_ideal_return() ) ++reloc_entries; duke@435: if ( is_ideal_safepoint() ) ++reloc_entries; duke@435: duke@435: duke@435: // Check if operands MAYBE oop pointers, by checking for ConP elements duke@435: // Proceed through the leaves of the match-tree and check for ConPs duke@435: if ( _matrule != NULL ) { duke@435: uint position = 0; duke@435: const char *result = NULL; duke@435: const char *name = NULL; duke@435: const char *opType = NULL; duke@435: while (_matrule->base_operand(position, globals, result, name, opType)) { duke@435: if ( strcmp(opType,"ConP") == 0 ) { duke@435: #ifdef SPARC duke@435: reloc_entries += 2; // 1 for sethi + 1 for setlo duke@435: #else duke@435: ++reloc_entries; duke@435: #endif duke@435: } duke@435: ++position; duke@435: } duke@435: } duke@435: duke@435: // Above is only a conservative estimate duke@435: // because it did not check contents of operand classes. duke@435: // !!!!! !!!!! duke@435: // Add 1 to reloc info for each operand class in the component list. duke@435: Component *comp; duke@435: _components.reset(); duke@435: while ( (comp = _components.iter()) != NULL ) { duke@435: const Form *form = globals[comp->_type]; duke@435: assert( form, "Did not find component's type in global names"); duke@435: const OpClassForm *opc = form->is_opclass(); duke@435: const OperandForm *oper = form->is_operand(); duke@435: if ( opc && (oper == NULL) ) { duke@435: ++reloc_entries; duke@435: } else if ( oper ) { duke@435: // floats and doubles loaded out of method's constant pool require reloc info duke@435: Form::DataType type = oper->is_base_constant(globals); duke@435: if ( (type == Form::idealF) || (type == Form::idealD) ) { duke@435: ++reloc_entries; duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Float and Double constants may come from the CodeBuffer table duke@435: // and require relocatable addresses for access duke@435: // !!!!! duke@435: // Check for any component being an immediate float or double. duke@435: Form::DataType data_type = is_chain_of_constant(globals); duke@435: if( data_type==idealD || data_type==idealF ) { duke@435: #ifdef SPARC duke@435: // sparc required more relocation entries for floating constants duke@435: // (expires 9/98) duke@435: reloc_entries += 6; duke@435: #else duke@435: reloc_entries++; duke@435: #endif duke@435: } duke@435: duke@435: return reloc_entries; duke@435: } duke@435: duke@435: // Utility function defined in archDesc.cpp duke@435: extern bool is_def(int usedef); duke@435: duke@435: // Return the result of reducing an instruction duke@435: const char *InstructForm::reduce_result() { duke@435: const char* result = "Universe"; // default duke@435: _components.reset(); duke@435: Component *comp = _components.iter(); duke@435: if (comp != NULL && comp->isa(Component::DEF)) { duke@435: result = comp->_type; duke@435: // Override this if the rule is a store operation: duke@435: if (_matrule && _matrule->_rChild && duke@435: is_store_to_memory(_matrule->_rChild->_opType)) duke@435: result = "Universe"; duke@435: } duke@435: return result; duke@435: } duke@435: duke@435: // Return the name of the operand on the right hand side of the binary match duke@435: // Return NULL if there is no right hand side duke@435: const char *InstructForm::reduce_right(FormDict &globals) const { duke@435: if( _matrule == NULL ) return NULL; duke@435: return _matrule->reduce_right(globals); duke@435: } duke@435: duke@435: // Similar for left duke@435: const char *InstructForm::reduce_left(FormDict &globals) const { duke@435: if( _matrule == NULL ) return NULL; duke@435: return _matrule->reduce_left(globals); duke@435: } duke@435: duke@435: duke@435: // Base class for this instruction, MachNode except for calls duke@435: const char *InstructForm::mach_base_class() const { duke@435: if( is_ideal_call() == Form::JAVA_STATIC ) { duke@435: return "MachCallStaticJavaNode"; duke@435: } duke@435: else if( is_ideal_call() == Form::JAVA_DYNAMIC ) { duke@435: return "MachCallDynamicJavaNode"; duke@435: } duke@435: else if( is_ideal_call() == Form::JAVA_RUNTIME ) { duke@435: return "MachCallRuntimeNode"; duke@435: } duke@435: else if( is_ideal_call() == Form::JAVA_LEAF ) { duke@435: return "MachCallLeafNode"; duke@435: } duke@435: else if (is_ideal_return()) { duke@435: return "MachReturnNode"; duke@435: } duke@435: else if (is_ideal_halt()) { duke@435: return "MachHaltNode"; duke@435: } duke@435: else if (is_ideal_safepoint()) { duke@435: return "MachSafePointNode"; duke@435: } duke@435: else if (is_ideal_if()) { duke@435: return "MachIfNode"; duke@435: } duke@435: else if (is_ideal_fastlock()) { duke@435: return "MachFastLockNode"; duke@435: } duke@435: else if (is_ideal_nop()) { duke@435: return "MachNopNode"; duke@435: } duke@435: else if (captures_bottom_type()) { duke@435: return "MachTypeNode"; duke@435: } else { duke@435: return "MachNode"; duke@435: } duke@435: assert( false, "ShouldNotReachHere()"); duke@435: return NULL; duke@435: } duke@435: duke@435: // Compare the instruction predicates for textual equality duke@435: bool equivalent_predicates( const InstructForm *instr1, const InstructForm *instr2 ) { duke@435: const Predicate *pred1 = instr1->_predicate; duke@435: const Predicate *pred2 = instr2->_predicate; duke@435: if( pred1 == NULL && pred2 == NULL ) { duke@435: // no predicates means they are identical duke@435: return true; duke@435: } duke@435: if( pred1 != NULL && pred2 != NULL ) { duke@435: // compare the predicates duke@435: const char *str1 = pred1->_pred; duke@435: const char *str2 = pred2->_pred; duke@435: if( (str1 == NULL && str2 == NULL) duke@435: || (str1 != NULL && str2 != NULL && strcmp(str1,str2) == 0) ) { duke@435: return true; duke@435: } duke@435: } duke@435: duke@435: return false; duke@435: } duke@435: duke@435: // Check if this instruction can cisc-spill to 'alternate' duke@435: bool InstructForm::cisc_spills_to(ArchDesc &AD, InstructForm *instr) { duke@435: assert( _matrule != NULL && instr->_matrule != NULL, "must have match rules"); duke@435: // Do not replace if a cisc-version has been found. duke@435: if( cisc_spill_operand() != Not_cisc_spillable ) return false; duke@435: duke@435: int cisc_spill_operand = Maybe_cisc_spillable; duke@435: char *result = NULL; duke@435: char *result2 = NULL; duke@435: const char *op_name = NULL; duke@435: const char *reg_type = NULL; duke@435: FormDict &globals = AD.globalNames(); duke@435: cisc_spill_operand = _matrule->cisc_spill_match(globals, AD.get_registers(), instr->_matrule, op_name, reg_type); duke@435: if( (cisc_spill_operand != Not_cisc_spillable) && (op_name != NULL) && equivalent_predicates(this, instr) ) { duke@435: cisc_spill_operand = operand_position(op_name, Component::USE); duke@435: int def_oper = operand_position(op_name, Component::DEF); duke@435: if( def_oper == NameList::Not_in_list && instr->num_opnds() == num_opnds()) { duke@435: // Do not support cisc-spilling for destination operands and duke@435: // make sure they have the same number of operands. duke@435: _cisc_spill_alternate = instr; duke@435: instr->set_cisc_alternate(true); duke@435: if( AD._cisc_spill_debug ) { duke@435: fprintf(stderr, "Instruction %s cisc-spills-to %s\n", _ident, instr->_ident); duke@435: fprintf(stderr, " using operand %s %s at index %d\n", reg_type, op_name, cisc_spill_operand); duke@435: } duke@435: // Record that a stack-version of the reg_mask is needed duke@435: // !!!!! duke@435: OperandForm *oper = (OperandForm*)(globals[reg_type]->is_operand()); duke@435: assert( oper != NULL, "cisc-spilling non operand"); duke@435: const char *reg_class_name = oper->constrained_reg_class(); duke@435: AD.set_stack_or_reg(reg_class_name); duke@435: const char *reg_mask_name = AD.reg_mask(*oper); duke@435: set_cisc_reg_mask_name(reg_mask_name); duke@435: const char *stack_or_reg_mask_name = AD.stack_or_reg_mask(*oper); duke@435: } else { duke@435: cisc_spill_operand = Not_cisc_spillable; duke@435: } duke@435: } else { duke@435: cisc_spill_operand = Not_cisc_spillable; duke@435: } duke@435: duke@435: set_cisc_spill_operand(cisc_spill_operand); duke@435: return (cisc_spill_operand != Not_cisc_spillable); duke@435: } duke@435: duke@435: // Check to see if this instruction can be replaced with the short branch duke@435: // instruction `short-branch' duke@435: bool InstructForm::check_branch_variant(ArchDesc &AD, InstructForm *short_branch) { duke@435: if (_matrule != NULL && duke@435: this != short_branch && // Don't match myself duke@435: !is_short_branch() && // Don't match another short branch variant duke@435: reduce_result() != NULL && duke@435: strcmp(reduce_result(), short_branch->reduce_result()) == 0 && duke@435: _matrule->equivalent(AD.globalNames(), short_branch->_matrule)) { duke@435: // The instructions are equivalent. duke@435: if (AD._short_branch_debug) { duke@435: fprintf(stderr, "Instruction %s has short form %s\n", _ident, short_branch->_ident); duke@435: } duke@435: _short_branch_form = short_branch; duke@435: return true; duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: duke@435: // --------------------------- FILE *output_routines duke@435: // duke@435: // Generate the format call for the replacement variable duke@435: void InstructForm::rep_var_format(FILE *fp, const char *rep_var) { duke@435: // Find replacement variable's type duke@435: const Form *form = _localNames[rep_var]; duke@435: if (form == NULL) { duke@435: fprintf(stderr, "unknown replacement variable in format statement: '%s'\n", rep_var); duke@435: assert(false, "ShouldNotReachHere()"); duke@435: } duke@435: OpClassForm *opc = form->is_opclass(); duke@435: assert( opc, "replacement variable was not found in local names"); duke@435: // Lookup the index position of the replacement variable duke@435: int idx = operand_position_format(rep_var); duke@435: if ( idx == -1 ) { duke@435: assert( strcmp(opc->_ident,"label")==0, "Unimplemented"); duke@435: assert( false, "ShouldNotReachHere()"); duke@435: } duke@435: duke@435: if (is_noninput_operand(idx)) { duke@435: // This component isn't in the input array. Print out the static duke@435: // name of the register. duke@435: OperandForm* oper = form->is_operand(); duke@435: if (oper != NULL && oper->is_bound_register()) { duke@435: const RegDef* first = oper->get_RegClass()->find_first_elem(); duke@435: fprintf(fp, " tty->print(\"%s\");\n", first->_regname); duke@435: } else { duke@435: globalAD->syntax_err(_linenum, "In %s can't find format for %s %s", _ident, opc->_ident, rep_var); duke@435: } duke@435: } else { duke@435: // Output the format call for this operand duke@435: fprintf(fp,"opnd_array(%d)->",idx); duke@435: if (idx == 0) duke@435: fprintf(fp,"int_format(ra, this, st); // %s\n", rep_var); duke@435: else duke@435: fprintf(fp,"ext_format(ra, this,idx%d, st); // %s\n", idx, rep_var ); duke@435: } duke@435: } duke@435: duke@435: // Seach through operands to determine parameters unique positions. duke@435: void InstructForm::set_unique_opnds() { duke@435: uint* uniq_idx = NULL; duke@435: uint nopnds = num_opnds(); duke@435: uint num_uniq = nopnds; duke@435: uint i; duke@435: if ( nopnds > 0 ) { duke@435: // Allocate index array with reserve. duke@435: uniq_idx = (uint*) malloc(sizeof(uint)*(nopnds + 2)); duke@435: for( i = 0; i < nopnds+2; i++ ) { duke@435: uniq_idx[i] = i; duke@435: } duke@435: } duke@435: // Do it only if there is a match rule and no expand rule. With an duke@435: // expand rule it is done by creating new mach node in Expand() duke@435: // method. duke@435: if ( nopnds > 0 && _matrule != NULL && _exprule == NULL ) { duke@435: const char *name; duke@435: uint count; duke@435: bool has_dupl_use = false; duke@435: duke@435: _parameters.reset(); duke@435: while( (name = _parameters.iter()) != NULL ) { duke@435: count = 0; duke@435: uint position = 0; duke@435: uint uniq_position = 0; duke@435: _components.reset(); duke@435: Component *comp = NULL; duke@435: if( sets_result() ) { duke@435: comp = _components.iter(); duke@435: position++; duke@435: } duke@435: // The next code is copied from the method operand_position(). duke@435: for (; (comp = _components.iter()) != NULL; ++position) { duke@435: // When the first component is not a DEF, duke@435: // leave space for the result operand! duke@435: if ( position==0 && (! comp->isa(Component::DEF)) ) { duke@435: ++position; duke@435: } duke@435: if( strcmp(name, comp->_name)==0 ) { duke@435: if( ++count > 1 ) { duke@435: uniq_idx[position] = uniq_position; duke@435: has_dupl_use = true; duke@435: } else { duke@435: uniq_position = position; duke@435: } duke@435: } duke@435: if( comp->isa(Component::DEF) duke@435: && comp->isa(Component::USE) ) { duke@435: ++position; duke@435: if( position != 1 ) duke@435: --position; // only use two slots for the 1st USE_DEF duke@435: } duke@435: } duke@435: } duke@435: if( has_dupl_use ) { duke@435: for( i = 1; i < nopnds; i++ ) duke@435: if( i != uniq_idx[i] ) duke@435: break; duke@435: int j = i; duke@435: for( ; i < nopnds; i++ ) duke@435: if( i == uniq_idx[i] ) duke@435: uniq_idx[i] = j++; duke@435: num_uniq = j; duke@435: } duke@435: } duke@435: _uniq_idx = uniq_idx; duke@435: _num_uniq = num_uniq; duke@435: } duke@435: duke@435: // Generate index values needed for determing the operand position duke@435: void InstructForm::index_temps(FILE *fp, FormDict &globals, const char *prefix, const char *receiver) { duke@435: uint idx = 0; // position of operand in match rule duke@435: int cur_num_opnds = num_opnds(); duke@435: duke@435: // Compute the index into vector of operand pointers: duke@435: // idx0=0 is used to indicate that info comes from this same node, not from input edge. duke@435: // idx1 starts at oper_input_base() duke@435: if ( cur_num_opnds >= 1 ) { duke@435: fprintf(fp," // Start at oper_input_base() and count operands\n"); duke@435: fprintf(fp," unsigned %sidx0 = %d;\n", prefix, oper_input_base(globals)); duke@435: fprintf(fp," unsigned %sidx1 = %d;\n", prefix, oper_input_base(globals)); duke@435: duke@435: // Generate starting points for other unique operands if they exist duke@435: for ( idx = 2; idx < num_unique_opnds(); ++idx ) { duke@435: if( *receiver == 0 ) { duke@435: fprintf(fp," unsigned %sidx%d = %sidx%d + opnd_array(%d)->num_edges();\n", duke@435: prefix, idx, prefix, idx-1, idx-1 ); duke@435: } else { duke@435: fprintf(fp," unsigned %sidx%d = %sidx%d + %s_opnds[%d]->num_edges();\n", duke@435: prefix, idx, prefix, idx-1, receiver, idx-1 ); duke@435: } duke@435: } duke@435: } duke@435: if( *receiver != 0 ) { duke@435: // This value is used by generate_peepreplace when copying a node. duke@435: // Don't emit it in other cases since it can hide bugs with the duke@435: // use invalid idx's. duke@435: fprintf(fp," unsigned %sidx%d = %sreq(); \n", prefix, idx, receiver); duke@435: } duke@435: duke@435: } duke@435: duke@435: // --------------------------- duke@435: bool InstructForm::verify() { duke@435: // !!!!! !!!!! duke@435: // Check that a "label" operand occurs last in the operand list, if present duke@435: return true; duke@435: } duke@435: duke@435: void InstructForm::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: void InstructForm::output(FILE *fp) { duke@435: fprintf(fp,"\nInstruction: %s\n", (_ident?_ident:"")); duke@435: if (_matrule) _matrule->output(fp); duke@435: if (_insencode) _insencode->output(fp); duke@435: if (_opcode) _opcode->output(fp); duke@435: if (_attribs) _attribs->output(fp); duke@435: if (_predicate) _predicate->output(fp); duke@435: if (_effects.Size()) { duke@435: fprintf(fp,"Effects\n"); duke@435: _effects.dump(); duke@435: } duke@435: if (_exprule) _exprule->output(fp); duke@435: if (_rewrule) _rewrule->output(fp); duke@435: if (_format) _format->output(fp); duke@435: if (_peephole) _peephole->output(fp); duke@435: } duke@435: duke@435: void MachNodeForm::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: void MachNodeForm::output(FILE *fp) { duke@435: fprintf(fp,"\nMachNode: %s\n", (_ident?_ident:"")); duke@435: } duke@435: duke@435: //------------------------------build_predicate-------------------------------- duke@435: // Build instruction predicates. If the user uses the same operand name duke@435: // twice, we need to check that the operands are pointer-eequivalent in duke@435: // the DFA during the labeling process. duke@435: Predicate *InstructForm::build_predicate() { duke@435: char buf[1024], *s=buf; duke@435: Dict names(cmpstr,hashstr,Form::arena); // Map Names to counts duke@435: duke@435: MatchNode *mnode = duke@435: strcmp(_matrule->_opType, "Set") ? _matrule : _matrule->_rChild; duke@435: mnode->count_instr_names(names); duke@435: duke@435: uint first = 1; duke@435: // Start with the predicate supplied in the .ad file. duke@435: if( _predicate ) { duke@435: if( first ) first=0; duke@435: strcpy(s,"("); s += strlen(s); duke@435: strcpy(s,_predicate->_pred); duke@435: s += strlen(s); duke@435: strcpy(s,")"); s += strlen(s); duke@435: } duke@435: for( DictI i(&names); i.test(); ++i ) { duke@435: uintptr_t cnt = (uintptr_t)i._value; duke@435: if( cnt > 1 ) { // Need a predicate at all? duke@435: assert( cnt == 2, "Unimplemented" ); duke@435: // Handle many pairs duke@435: if( first ) first=0; duke@435: else { // All tests must pass, so use '&&' duke@435: strcpy(s," && "); duke@435: s += strlen(s); duke@435: } duke@435: // Add predicate to working buffer duke@435: sprintf(s,"/*%s*/(",(char*)i._key); duke@435: s += strlen(s); duke@435: mnode->build_instr_pred(s,(char*)i._key,0); duke@435: s += strlen(s); duke@435: strcpy(s," == "); s += strlen(s); duke@435: mnode->build_instr_pred(s,(char*)i._key,1); duke@435: s += strlen(s); duke@435: strcpy(s,")"); s += strlen(s); duke@435: } duke@435: } duke@435: if( s == buf ) s = NULL; duke@435: else { duke@435: assert( strlen(buf) < sizeof(buf), "String buffer overflow" ); duke@435: s = strdup(buf); duke@435: } duke@435: return new Predicate(s); duke@435: } duke@435: duke@435: //------------------------------EncodeForm------------------------------------- duke@435: // Constructor duke@435: EncodeForm::EncodeForm() duke@435: : _encClass(cmpstr,hashstr, Form::arena) { duke@435: } duke@435: EncodeForm::~EncodeForm() { duke@435: } duke@435: duke@435: // record a new register class duke@435: EncClass *EncodeForm::add_EncClass(const char *className) { duke@435: EncClass *encClass = new EncClass(className); duke@435: _eclasses.addName(className); duke@435: _encClass.Insert(className,encClass); duke@435: return encClass; duke@435: } duke@435: duke@435: // Lookup the function body for an encoding class duke@435: EncClass *EncodeForm::encClass(const char *className) { duke@435: assert( className != NULL, "Must provide a defined encoding name"); duke@435: duke@435: EncClass *encClass = (EncClass*)_encClass[className]; duke@435: return encClass; duke@435: } duke@435: duke@435: // Lookup the function body for an encoding class duke@435: const char *EncodeForm::encClassBody(const char *className) { duke@435: if( className == NULL ) return NULL; duke@435: duke@435: EncClass *encClass = (EncClass*)_encClass[className]; duke@435: assert( encClass != NULL, "Encode Class is missing."); duke@435: encClass->_code.reset(); duke@435: const char *code = (const char*)encClass->_code.iter(); duke@435: assert( code != NULL, "Found an empty encode class body."); duke@435: duke@435: return code; duke@435: } duke@435: duke@435: // Lookup the function body for an encoding class duke@435: const char *EncodeForm::encClassPrototype(const char *className) { duke@435: assert( className != NULL, "Encode class name must be non NULL."); duke@435: duke@435: return className; duke@435: } duke@435: duke@435: void EncodeForm::dump() { // Debug printer duke@435: output(stderr); duke@435: } duke@435: duke@435: void EncodeForm::output(FILE *fp) { // Write info to output files duke@435: const char *name; duke@435: fprintf(fp,"\n"); duke@435: fprintf(fp,"-------------------- Dump EncodeForm --------------------\n"); duke@435: for (_eclasses.reset(); (name = _eclasses.iter()) != NULL;) { duke@435: ((EncClass*)_encClass[name])->output(fp); duke@435: } duke@435: fprintf(fp,"-------------------- end EncodeForm --------------------\n"); duke@435: } duke@435: //------------------------------EncClass--------------------------------------- duke@435: EncClass::EncClass(const char *name) duke@435: : _localNames(cmpstr,hashstr, Form::arena), _name(name) { duke@435: } duke@435: EncClass::~EncClass() { duke@435: } duke@435: duke@435: // Add a parameter pair duke@435: void EncClass::add_parameter(const char *parameter_type, const char *parameter_name) { duke@435: _parameter_type.addName( parameter_type ); duke@435: _parameter_name.addName( parameter_name ); duke@435: } duke@435: duke@435: // Verify operand types in parameter list duke@435: bool EncClass::check_parameter_types(FormDict &globals) { duke@435: // !!!!! duke@435: return false; duke@435: } duke@435: duke@435: // Add the decomposed "code" sections of an encoding's code-block duke@435: void EncClass::add_code(const char *code) { duke@435: _code.addName(code); duke@435: } duke@435: duke@435: // Add the decomposed "replacement variables" of an encoding's code-block duke@435: void EncClass::add_rep_var(char *replacement_var) { duke@435: _code.addName(NameList::_signal); duke@435: _rep_vars.addName(replacement_var); duke@435: } duke@435: duke@435: // Lookup the function body for an encoding class duke@435: int EncClass::rep_var_index(const char *rep_var) { duke@435: uint position = 0; duke@435: const char *name = NULL; duke@435: duke@435: _parameter_name.reset(); duke@435: while ( (name = _parameter_name.iter()) != NULL ) { duke@435: if ( strcmp(rep_var,name) == 0 ) return position; duke@435: ++position; duke@435: } duke@435: duke@435: return -1; duke@435: } duke@435: duke@435: // Check after parsing duke@435: bool EncClass::verify() { duke@435: // 1!!!! duke@435: // Check that each replacement variable, '$name' in architecture description duke@435: // is actually a local variable for this encode class, or a reserved name duke@435: // "primary, secondary, tertiary" duke@435: return true; duke@435: } duke@435: duke@435: void EncClass::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: // Write info to output files duke@435: void EncClass::output(FILE *fp) { duke@435: fprintf(fp,"EncClass: %s", (_name ? _name : "")); duke@435: duke@435: // Output the parameter list duke@435: _parameter_type.reset(); duke@435: _parameter_name.reset(); duke@435: const char *type = _parameter_type.iter(); duke@435: const char *name = _parameter_name.iter(); duke@435: fprintf(fp, " ( "); duke@435: for ( ; (type != NULL) && (name != NULL); duke@435: (type = _parameter_type.iter()), (name = _parameter_name.iter()) ) { duke@435: fprintf(fp, " %s %s,", type, name); duke@435: } duke@435: fprintf(fp, " ) "); duke@435: duke@435: // Output the code block duke@435: _code.reset(); duke@435: _rep_vars.reset(); duke@435: const char *code; duke@435: while ( (code = _code.iter()) != NULL ) { duke@435: if ( _code.is_signal(code) ) { duke@435: // A replacement variable duke@435: const char *rep_var = _rep_vars.iter(); duke@435: fprintf(fp,"($%s)", rep_var); duke@435: } else { duke@435: // A section of code duke@435: fprintf(fp,"%s", code); duke@435: } duke@435: } duke@435: duke@435: } duke@435: duke@435: //------------------------------Opcode----------------------------------------- duke@435: Opcode::Opcode(char *primary, char *secondary, char *tertiary) duke@435: : _primary(primary), _secondary(secondary), _tertiary(tertiary) { duke@435: } duke@435: duke@435: Opcode::~Opcode() { duke@435: } duke@435: duke@435: Opcode::opcode_type Opcode::as_opcode_type(const char *param) { duke@435: if( strcmp(param,"primary") == 0 ) { duke@435: return Opcode::PRIMARY; duke@435: } duke@435: else if( strcmp(param,"secondary") == 0 ) { duke@435: return Opcode::SECONDARY; duke@435: } duke@435: else if( strcmp(param,"tertiary") == 0 ) { duke@435: return Opcode::TERTIARY; duke@435: } duke@435: return Opcode::NOT_AN_OPCODE; duke@435: } duke@435: duke@435: void Opcode::print_opcode(FILE *fp, Opcode::opcode_type desired_opcode) { duke@435: // Default values previously provided by MachNode::primary()... duke@435: const char *description = "default_opcode()"; duke@435: const char *value = "-1"; duke@435: // Check if user provided any opcode definitions duke@435: if( this != NULL ) { duke@435: // Update 'value' if user provided a definition in the instruction duke@435: switch (desired_opcode) { duke@435: case PRIMARY: duke@435: description = "primary()"; duke@435: if( _primary != NULL) { value = _primary; } duke@435: break; duke@435: case SECONDARY: duke@435: description = "secondary()"; duke@435: if( _secondary != NULL ) { value = _secondary; } duke@435: break; duke@435: case TERTIARY: duke@435: description = "tertiary()"; duke@435: if( _tertiary != NULL ) { value = _tertiary; } duke@435: break; duke@435: default: duke@435: assert( false, "ShouldNotReachHere();"); duke@435: break; duke@435: } duke@435: } duke@435: fprintf(fp, "(%s /*%s*/)", value, description); duke@435: } duke@435: duke@435: void Opcode::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: // Write info to output files duke@435: void Opcode::output(FILE *fp) { duke@435: if (_primary != NULL) fprintf(fp,"Primary opcode: %s\n", _primary); duke@435: if (_secondary != NULL) fprintf(fp,"Secondary opcode: %s\n", _secondary); duke@435: if (_tertiary != NULL) fprintf(fp,"Tertiary opcode: %s\n", _tertiary); duke@435: } duke@435: duke@435: //------------------------------InsEncode-------------------------------------- duke@435: InsEncode::InsEncode() { duke@435: } duke@435: InsEncode::~InsEncode() { duke@435: } duke@435: duke@435: // Add "encode class name" and its parameters duke@435: NameAndList *InsEncode::add_encode(char *encoding) { duke@435: assert( encoding != NULL, "Must provide name for encoding"); duke@435: duke@435: // add_parameter(NameList::_signal); duke@435: NameAndList *encode = new NameAndList(encoding); duke@435: _encoding.addName((char*)encode); duke@435: duke@435: return encode; duke@435: } duke@435: duke@435: // Access the list of encodings duke@435: void InsEncode::reset() { duke@435: _encoding.reset(); duke@435: // _parameter.reset(); duke@435: } duke@435: const char* InsEncode::encode_class_iter() { duke@435: NameAndList *encode_class = (NameAndList*)_encoding.iter(); duke@435: return ( encode_class != NULL ? encode_class->name() : NULL ); duke@435: } duke@435: // Obtain parameter name from zero based index duke@435: const char *InsEncode::rep_var_name(InstructForm &inst, uint param_no) { duke@435: NameAndList *params = (NameAndList*)_encoding.current(); duke@435: assert( params != NULL, "Internal Error"); duke@435: const char *param = (*params)[param_no]; duke@435: duke@435: // Remove '$' if parser placed it there. duke@435: return ( param != NULL && *param == '$') ? (param+1) : param; duke@435: } duke@435: duke@435: void InsEncode::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: // Write info to output files duke@435: void InsEncode::output(FILE *fp) { duke@435: NameAndList *encoding = NULL; duke@435: const char *parameter = NULL; duke@435: duke@435: fprintf(fp,"InsEncode: "); duke@435: _encoding.reset(); duke@435: duke@435: while ( (encoding = (NameAndList*)_encoding.iter()) != 0 ) { duke@435: // Output the encoding being used duke@435: fprintf(fp,"%s(", encoding->name() ); duke@435: duke@435: // Output its parameter list, if any duke@435: bool first_param = true; duke@435: encoding->reset(); duke@435: while ( (parameter = encoding->iter()) != 0 ) { duke@435: // Output the ',' between parameters duke@435: if ( ! first_param ) fprintf(fp,", "); duke@435: first_param = false; duke@435: // Output the parameter duke@435: fprintf(fp,"%s", parameter); duke@435: } // done with parameters duke@435: fprintf(fp,") "); duke@435: } // done with encodings duke@435: duke@435: fprintf(fp,"\n"); duke@435: } duke@435: duke@435: //------------------------------Effect----------------------------------------- duke@435: static int effect_lookup(const char *name) { duke@435: if(!strcmp(name, "USE")) return Component::USE; duke@435: if(!strcmp(name, "DEF")) return Component::DEF; duke@435: if(!strcmp(name, "USE_DEF")) return Component::USE_DEF; duke@435: if(!strcmp(name, "KILL")) return Component::KILL; duke@435: if(!strcmp(name, "USE_KILL")) return Component::USE_KILL; duke@435: if(!strcmp(name, "TEMP")) return Component::TEMP; duke@435: if(!strcmp(name, "INVALID")) return Component::INVALID; duke@435: assert( false,"Invalid effect name specified\n"); duke@435: return Component::INVALID; duke@435: } duke@435: duke@435: Effect::Effect(const char *name) : _name(name), _use_def(effect_lookup(name)) { duke@435: _ftype = Form::EFF; duke@435: } duke@435: Effect::~Effect() { duke@435: } duke@435: duke@435: // Dynamic type check duke@435: Effect *Effect::is_effect() const { duke@435: return (Effect*)this; duke@435: } duke@435: duke@435: duke@435: // True if this component is equal to the parameter. duke@435: bool Effect::is(int use_def_kill_enum) const { duke@435: return (_use_def == use_def_kill_enum ? true : false); duke@435: } duke@435: // True if this component is used/def'd/kill'd as the parameter suggests. duke@435: bool Effect::isa(int use_def_kill_enum) const { duke@435: return (_use_def & use_def_kill_enum) == use_def_kill_enum; duke@435: } duke@435: duke@435: void Effect::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: void Effect::output(FILE *fp) { // Write info to output files duke@435: fprintf(fp,"Effect: %s\n", (_name?_name:"")); duke@435: } duke@435: duke@435: //------------------------------ExpandRule------------------------------------- duke@435: ExpandRule::ExpandRule() : _expand_instrs(), duke@435: _newopconst(cmpstr, hashstr, Form::arena) { duke@435: _ftype = Form::EXP; duke@435: } duke@435: duke@435: ExpandRule::~ExpandRule() { // Destructor duke@435: } duke@435: duke@435: void ExpandRule::add_instruction(NameAndList *instruction_name_and_operand_list) { duke@435: _expand_instrs.addName((char*)instruction_name_and_operand_list); duke@435: } duke@435: duke@435: void ExpandRule::reset_instructions() { duke@435: _expand_instrs.reset(); duke@435: } duke@435: duke@435: NameAndList* ExpandRule::iter_instructions() { duke@435: return (NameAndList*)_expand_instrs.iter(); duke@435: } duke@435: duke@435: duke@435: void ExpandRule::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: void ExpandRule::output(FILE *fp) { // Write info to output files duke@435: NameAndList *expand_instr = NULL; duke@435: const char *opid = NULL; duke@435: duke@435: fprintf(fp,"\nExpand Rule:\n"); duke@435: duke@435: // Iterate over the instructions 'node' expands into duke@435: for(reset_instructions(); (expand_instr = iter_instructions()) != NULL; ) { duke@435: fprintf(fp,"%s(", expand_instr->name()); duke@435: duke@435: // iterate over the operand list duke@435: for( expand_instr->reset(); (opid = expand_instr->iter()) != NULL; ) { duke@435: fprintf(fp,"%s ", opid); duke@435: } duke@435: fprintf(fp,");\n"); duke@435: } duke@435: } duke@435: duke@435: //------------------------------RewriteRule------------------------------------ duke@435: RewriteRule::RewriteRule(char* params, char* block) duke@435: : _tempParams(params), _tempBlock(block) { }; // Constructor duke@435: RewriteRule::~RewriteRule() { // Destructor duke@435: } duke@435: duke@435: void RewriteRule::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: void RewriteRule::output(FILE *fp) { // Write info to output files duke@435: fprintf(fp,"\nRewrite Rule:\n%s\n%s\n", duke@435: (_tempParams?_tempParams:""), duke@435: (_tempBlock?_tempBlock:"")); duke@435: } duke@435: duke@435: duke@435: //==============================MachNodes====================================== duke@435: //------------------------------MachNodeForm----------------------------------- duke@435: MachNodeForm::MachNodeForm(char *id) duke@435: : _ident(id) { duke@435: } duke@435: duke@435: MachNodeForm::~MachNodeForm() { duke@435: } duke@435: duke@435: MachNodeForm *MachNodeForm::is_machnode() const { duke@435: return (MachNodeForm*)this; duke@435: } duke@435: duke@435: //==============================Operand Classes================================ duke@435: //------------------------------OpClassForm------------------------------------ duke@435: OpClassForm::OpClassForm(const char* id) : _ident(id) { duke@435: _ftype = Form::OPCLASS; duke@435: } duke@435: duke@435: OpClassForm::~OpClassForm() { duke@435: } duke@435: duke@435: bool OpClassForm::ideal_only() const { return 0; } duke@435: duke@435: OpClassForm *OpClassForm::is_opclass() const { duke@435: return (OpClassForm*)this; duke@435: } duke@435: duke@435: Form::InterfaceType OpClassForm::interface_type(FormDict &globals) const { duke@435: if( _oplst.count() == 0 ) return Form::no_interface; duke@435: duke@435: // Check that my operands have the same interface type duke@435: Form::InterfaceType interface; duke@435: bool first = true; duke@435: NameList &op_list = (NameList &)_oplst; duke@435: op_list.reset(); duke@435: const char *op_name; duke@435: while( (op_name = op_list.iter()) != NULL ) { duke@435: const Form *form = globals[op_name]; duke@435: OperandForm *operand = form->is_operand(); duke@435: assert( operand, "Entry in operand class that is not an operand"); duke@435: if( first ) { duke@435: first = false; duke@435: interface = operand->interface_type(globals); duke@435: } else { duke@435: interface = (interface == operand->interface_type(globals) ? interface : Form::no_interface); duke@435: } duke@435: } duke@435: return interface; duke@435: } duke@435: duke@435: bool OpClassForm::stack_slots_only(FormDict &globals) const { duke@435: if( _oplst.count() == 0 ) return false; // how? duke@435: duke@435: NameList &op_list = (NameList &)_oplst; duke@435: op_list.reset(); duke@435: const char *op_name; duke@435: while( (op_name = op_list.iter()) != NULL ) { duke@435: const Form *form = globals[op_name]; duke@435: OperandForm *operand = form->is_operand(); duke@435: assert( operand, "Entry in operand class that is not an operand"); duke@435: if( !operand->stack_slots_only(globals) ) return false; duke@435: } duke@435: return true; duke@435: } duke@435: duke@435: duke@435: void OpClassForm::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: void OpClassForm::output(FILE *fp) { duke@435: const char *name; duke@435: fprintf(fp,"\nOperand Class: %s\n", (_ident?_ident:"")); duke@435: fprintf(fp,"\nCount = %d\n", _oplst.count()); duke@435: for(_oplst.reset(); (name = _oplst.iter()) != NULL;) { duke@435: fprintf(fp,"%s, ",name); duke@435: } duke@435: fprintf(fp,"\n"); duke@435: } duke@435: duke@435: duke@435: //==============================Operands======================================= duke@435: //------------------------------OperandForm------------------------------------ duke@435: OperandForm::OperandForm(const char* id) duke@435: : OpClassForm(id), _ideal_only(false), duke@435: _localNames(cmpstr, hashstr, Form::arena) { duke@435: _ftype = Form::OPER; duke@435: duke@435: _matrule = NULL; duke@435: _interface = NULL; duke@435: _attribs = NULL; duke@435: _predicate = NULL; duke@435: _constraint= NULL; duke@435: _construct = NULL; duke@435: _format = NULL; duke@435: } duke@435: OperandForm::OperandForm(const char* id, bool ideal_only) duke@435: : OpClassForm(id), _ideal_only(ideal_only), duke@435: _localNames(cmpstr, hashstr, Form::arena) { duke@435: _ftype = Form::OPER; duke@435: duke@435: _matrule = NULL; duke@435: _interface = NULL; duke@435: _attribs = NULL; duke@435: _predicate = NULL; duke@435: _constraint= NULL; duke@435: _construct = NULL; duke@435: _format = NULL; duke@435: } duke@435: OperandForm::~OperandForm() { duke@435: } duke@435: duke@435: duke@435: OperandForm *OperandForm::is_operand() const { duke@435: return (OperandForm*)this; duke@435: } duke@435: duke@435: bool OperandForm::ideal_only() const { duke@435: return _ideal_only; duke@435: } duke@435: duke@435: Form::InterfaceType OperandForm::interface_type(FormDict &globals) const { duke@435: if( _interface == NULL ) return Form::no_interface; duke@435: duke@435: return _interface->interface_type(globals); duke@435: } duke@435: duke@435: duke@435: bool OperandForm::stack_slots_only(FormDict &globals) const { duke@435: if( _constraint == NULL ) return false; duke@435: return _constraint->stack_slots_only(); duke@435: } duke@435: duke@435: duke@435: // Access op_cost attribute or return NULL. duke@435: const char* OperandForm::cost() { duke@435: for (Attribute* cur = _attribs; cur != NULL; cur = (Attribute*)cur->_next) { duke@435: if( strcmp(cur->_ident,AttributeForm::_op_cost) == 0 ) { duke@435: return cur->_val; duke@435: } duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: // Return the number of leaves below this complex operand duke@435: uint OperandForm::num_leaves() const { duke@435: if ( ! _matrule) return 0; duke@435: duke@435: int num_leaves = _matrule->_numleaves; duke@435: return num_leaves; duke@435: } duke@435: duke@435: // Return the number of constants contained within this complex operand duke@435: uint OperandForm::num_consts(FormDict &globals) const { duke@435: if ( ! _matrule) return 0; duke@435: duke@435: // This is a recursive invocation on all operands in the matchrule duke@435: return _matrule->num_consts(globals); duke@435: } duke@435: duke@435: // Return the number of constants in match rule with specified type duke@435: uint OperandForm::num_consts(FormDict &globals, Form::DataType type) const { duke@435: if ( ! _matrule) return 0; duke@435: duke@435: // This is a recursive invocation on all operands in the matchrule duke@435: return _matrule->num_consts(globals, type); duke@435: } duke@435: duke@435: // Return the number of pointer constants contained within this complex operand duke@435: uint OperandForm::num_const_ptrs(FormDict &globals) const { duke@435: if ( ! _matrule) return 0; duke@435: duke@435: // This is a recursive invocation on all operands in the matchrule duke@435: return _matrule->num_const_ptrs(globals); duke@435: } duke@435: duke@435: uint OperandForm::num_edges(FormDict &globals) const { duke@435: uint edges = 0; duke@435: uint leaves = num_leaves(); duke@435: uint consts = num_consts(globals); duke@435: duke@435: // If we are matching a constant directly, there are no leaves. duke@435: edges = ( leaves > consts ) ? leaves - consts : 0; duke@435: duke@435: // !!!!! duke@435: // Special case operands that do not have a corresponding ideal node. duke@435: if( (edges == 0) && (consts == 0) ) { duke@435: if( constrained_reg_class() != NULL ) { duke@435: edges = 1; duke@435: } else { duke@435: if( _matrule duke@435: && (_matrule->_lChild == NULL) && (_matrule->_rChild == NULL) ) { duke@435: const Form *form = globals[_matrule->_opType]; duke@435: OperandForm *oper = form ? form->is_operand() : NULL; duke@435: if( oper ) { duke@435: return oper->num_edges(globals); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: return edges; duke@435: } duke@435: duke@435: duke@435: // Check if this operand is usable for cisc-spilling duke@435: bool OperandForm::is_cisc_reg(FormDict &globals) const { duke@435: const char *ideal = ideal_type(globals); duke@435: bool is_cisc_reg = (ideal && (ideal_to_Reg_type(ideal) != none)); duke@435: return is_cisc_reg; duke@435: } duke@435: duke@435: bool OpClassForm::is_cisc_mem(FormDict &globals) const { duke@435: Form::InterfaceType my_interface = interface_type(globals); duke@435: return (my_interface == memory_interface); duke@435: } duke@435: duke@435: duke@435: // node matches ideal 'Bool' duke@435: bool OperandForm::is_ideal_bool() const { duke@435: if( _matrule == NULL ) return false; duke@435: duke@435: return _matrule->is_ideal_bool(); duke@435: } duke@435: duke@435: // Require user's name for an sRegX to be stackSlotX duke@435: Form::DataType OperandForm::is_user_name_for_sReg() const { duke@435: DataType data_type = none; duke@435: if( _ident != NULL ) { duke@435: if( strcmp(_ident,"stackSlotI") == 0 ) data_type = Form::idealI; duke@435: else if( strcmp(_ident,"stackSlotP") == 0 ) data_type = Form::idealP; duke@435: else if( strcmp(_ident,"stackSlotD") == 0 ) data_type = Form::idealD; duke@435: else if( strcmp(_ident,"stackSlotF") == 0 ) data_type = Form::idealF; duke@435: else if( strcmp(_ident,"stackSlotL") == 0 ) data_type = Form::idealL; duke@435: } duke@435: assert((data_type == none) || (_matrule == NULL), "No match-rule for stackSlotX"); duke@435: duke@435: return data_type; duke@435: } duke@435: duke@435: duke@435: // Return ideal type, if there is a single ideal type for this operand duke@435: const char *OperandForm::ideal_type(FormDict &globals, RegisterForm *registers) const { duke@435: const char *type = NULL; duke@435: if (ideal_only()) type = _ident; duke@435: else if( _matrule == NULL ) { duke@435: // Check for condition code register duke@435: const char *rc_name = constrained_reg_class(); duke@435: // !!!!! duke@435: if (rc_name == NULL) return NULL; duke@435: // !!!!! !!!!! duke@435: // Check constraints on result's register class duke@435: if( registers ) { duke@435: RegClass *reg_class = registers->getRegClass(rc_name); duke@435: assert( reg_class != NULL, "Register class is not defined"); duke@435: duke@435: // Check for ideal type of entries in register class, all are the same type duke@435: reg_class->reset(); duke@435: RegDef *reg_def = reg_class->RegDef_iter(); duke@435: assert( reg_def != NULL, "No entries in register class"); duke@435: assert( reg_def->_idealtype != NULL, "Did not define ideal type for register"); duke@435: // Return substring that names the register's ideal type duke@435: type = reg_def->_idealtype + 3; duke@435: assert( *(reg_def->_idealtype + 0) == 'O', "Expect Op_ prefix"); duke@435: assert( *(reg_def->_idealtype + 1) == 'p', "Expect Op_ prefix"); duke@435: assert( *(reg_def->_idealtype + 2) == '_', "Expect Op_ prefix"); duke@435: } duke@435: } duke@435: else if( _matrule->_lChild == NULL && _matrule->_rChild == NULL ) { duke@435: // This operand matches a single type, at the top level. duke@435: // Check for ideal type duke@435: type = _matrule->_opType; duke@435: if( strcmp(type,"Bool") == 0 ) duke@435: return "Bool"; duke@435: // transitive lookup duke@435: const Form *frm = globals[type]; duke@435: OperandForm *op = frm->is_operand(); duke@435: type = op->ideal_type(globals, registers); duke@435: } duke@435: return type; duke@435: } duke@435: duke@435: duke@435: // If there is a single ideal type for this interface field, return it. duke@435: const char *OperandForm::interface_ideal_type(FormDict &globals, duke@435: const char *field) const { duke@435: const char *ideal_type = NULL; duke@435: const char *value = NULL; duke@435: duke@435: // Check if "field" is valid for this operand's interface duke@435: if ( ! is_interface_field(field, value) ) return ideal_type; duke@435: duke@435: // !!!!! !!!!! !!!!! duke@435: // If a valid field has a constant value, identify "ConI" or "ConP" or ... duke@435: duke@435: // Else, lookup type of field's replacement variable duke@435: duke@435: return ideal_type; duke@435: } duke@435: duke@435: duke@435: RegClass* OperandForm::get_RegClass() const { duke@435: if (_interface && !_interface->is_RegInterface()) return NULL; duke@435: return globalAD->get_registers()->getRegClass(constrained_reg_class()); duke@435: } duke@435: duke@435: duke@435: bool OperandForm::is_bound_register() const { duke@435: RegClass *reg_class = get_RegClass(); duke@435: if (reg_class == NULL) return false; duke@435: duke@435: const char * name = ideal_type(globalAD->globalNames()); duke@435: if (name == NULL) return false; duke@435: duke@435: int size = 0; duke@435: if (strcmp(name,"RegFlags")==0) size = 1; duke@435: if (strcmp(name,"RegI")==0) size = 1; duke@435: if (strcmp(name,"RegF")==0) size = 1; duke@435: if (strcmp(name,"RegD")==0) size = 2; duke@435: if (strcmp(name,"RegL")==0) size = 2; coleenp@548: if (strcmp(name,"RegN")==0) size = 1; duke@435: if (strcmp(name,"RegP")==0) size = globalAD->get_preproc_def("_LP64") ? 2 : 1; duke@435: if (size == 0) return false; duke@435: return size == reg_class->size(); duke@435: } duke@435: duke@435: duke@435: // Check if this is a valid field for this operand, duke@435: // Return 'true' if valid, and set the value to the string the user provided. duke@435: bool OperandForm::is_interface_field(const char *field, duke@435: const char * &value) const { duke@435: return false; duke@435: } duke@435: duke@435: duke@435: // Return register class name if a constraint specifies the register class. duke@435: const char *OperandForm::constrained_reg_class() const { duke@435: const char *reg_class = NULL; duke@435: if ( _constraint ) { duke@435: // !!!!! duke@435: Constraint *constraint = _constraint; duke@435: if ( strcmp(_constraint->_func,"ALLOC_IN_RC") == 0 ) { duke@435: reg_class = _constraint->_arg; duke@435: } duke@435: } duke@435: duke@435: return reg_class; duke@435: } duke@435: duke@435: duke@435: // Return the register class associated with 'leaf'. duke@435: const char *OperandForm::in_reg_class(uint leaf, FormDict &globals) { duke@435: const char *reg_class = NULL; // "RegMask::Empty"; duke@435: duke@435: if((_matrule == NULL) || (_matrule->is_chain_rule(globals))) { duke@435: reg_class = constrained_reg_class(); duke@435: return reg_class; duke@435: } duke@435: const char *result = NULL; duke@435: const char *name = NULL; duke@435: const char *type = NULL; duke@435: // iterate through all base operands duke@435: // until we reach the register that corresponds to "leaf" duke@435: // This function is not looking for an ideal type. It needs the first duke@435: // level user type associated with the leaf. duke@435: for(uint idx = 0;_matrule->base_operand(idx,globals,result,name,type);++idx) { duke@435: const Form *form = (_localNames[name] ? _localNames[name] : globals[result]); duke@435: OperandForm *oper = form ? form->is_operand() : NULL; duke@435: if( oper ) { duke@435: reg_class = oper->constrained_reg_class(); duke@435: if( reg_class ) { duke@435: reg_class = reg_class; duke@435: } else { duke@435: // ShouldNotReachHere(); duke@435: } duke@435: } else { duke@435: // ShouldNotReachHere(); duke@435: } duke@435: duke@435: // Increment our target leaf position if current leaf is not a candidate. duke@435: if( reg_class == NULL) ++leaf; duke@435: // Exit the loop with the value of reg_class when at the correct index duke@435: if( idx == leaf ) break; duke@435: // May iterate through all base operands if reg_class for 'leaf' is NULL duke@435: } duke@435: return reg_class; duke@435: } duke@435: duke@435: duke@435: // Recursive call to construct list of top-level operands. duke@435: // Implementation does not modify state of internal structures duke@435: void OperandForm::build_components() { duke@435: if (_matrule) _matrule->append_components(_localNames, _components); duke@435: duke@435: // Add parameters that "do not appear in match rule". duke@435: const char *name; duke@435: for (_parameters.reset(); (name = _parameters.iter()) != NULL;) { duke@435: OperandForm *opForm = (OperandForm*)_localNames[name]; duke@435: duke@435: if ( _components.operand_position(name) == -1 ) { duke@435: _components.insert(name, opForm->_ident, Component::INVALID, false); duke@435: } duke@435: } duke@435: duke@435: return; duke@435: } duke@435: duke@435: int OperandForm::operand_position(const char *name, int usedef) { duke@435: return _components.operand_position(name, usedef); duke@435: } duke@435: duke@435: duke@435: // Return zero-based position in component list, only counting constants; duke@435: // Return -1 if not in list. duke@435: int OperandForm::constant_position(FormDict &globals, const Component *last) { duke@435: // Iterate through components and count constants preceeding 'constant' duke@435: uint position = 0; duke@435: Component *comp; duke@435: _components.reset(); duke@435: while( (comp = _components.iter()) != NULL && (comp != last) ) { duke@435: // Special case for operands that take a single user-defined operand duke@435: // Skip the initial definition in the component list. duke@435: if( strcmp(comp->_name,this->_ident) == 0 ) continue; duke@435: duke@435: const char *type = comp->_type; duke@435: // Lookup operand form for replacement variable's type duke@435: const Form *form = globals[type]; duke@435: assert( form != NULL, "Component's type not found"); duke@435: OperandForm *oper = form ? form->is_operand() : NULL; duke@435: if( oper ) { duke@435: if( oper->_matrule->is_base_constant(globals) != Form::none ) { duke@435: ++position; duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Check for being passed a component that was not in the list duke@435: if( comp != last ) position = -1; duke@435: duke@435: return position; duke@435: } duke@435: // Provide position of constant by "name" duke@435: int OperandForm::constant_position(FormDict &globals, const char *name) { duke@435: const Component *comp = _components.search(name); duke@435: int idx = constant_position( globals, comp ); duke@435: duke@435: return idx; duke@435: } duke@435: duke@435: duke@435: // Return zero-based position in component list, only counting constants; duke@435: // Return -1 if not in list. duke@435: int OperandForm::register_position(FormDict &globals, const char *reg_name) { duke@435: // Iterate through components and count registers preceeding 'last' duke@435: uint position = 0; duke@435: Component *comp; duke@435: _components.reset(); duke@435: while( (comp = _components.iter()) != NULL duke@435: && (strcmp(comp->_name,reg_name) != 0) ) { duke@435: // Special case for operands that take a single user-defined operand duke@435: // Skip the initial definition in the component list. duke@435: if( strcmp(comp->_name,this->_ident) == 0 ) continue; duke@435: duke@435: const char *type = comp->_type; duke@435: // Lookup operand form for component's type duke@435: const Form *form = globals[type]; duke@435: assert( form != NULL, "Component's type not found"); duke@435: OperandForm *oper = form ? form->is_operand() : NULL; duke@435: if( oper ) { duke@435: if( oper->_matrule->is_base_register(globals) ) { duke@435: ++position; duke@435: } duke@435: } duke@435: } duke@435: duke@435: return position; duke@435: } duke@435: duke@435: duke@435: const char *OperandForm::reduce_result() const { duke@435: return _ident; duke@435: } duke@435: // Return the name of the operand on the right hand side of the binary match duke@435: // Return NULL if there is no right hand side duke@435: const char *OperandForm::reduce_right(FormDict &globals) const { duke@435: return ( _matrule ? _matrule->reduce_right(globals) : NULL ); duke@435: } duke@435: duke@435: // Similar for left duke@435: const char *OperandForm::reduce_left(FormDict &globals) const { duke@435: return ( _matrule ? _matrule->reduce_left(globals) : NULL ); duke@435: } duke@435: duke@435: duke@435: // --------------------------- FILE *output_routines duke@435: // duke@435: // Output code for disp_is_oop, if true. duke@435: void OperandForm::disp_is_oop(FILE *fp, FormDict &globals) { duke@435: // Check it is a memory interface with a non-user-constant disp field duke@435: if ( this->_interface == NULL ) return; duke@435: MemInterface *mem_interface = this->_interface->is_MemInterface(); duke@435: if ( mem_interface == NULL ) return; duke@435: const char *disp = mem_interface->_disp; duke@435: if ( *disp != '$' ) return; duke@435: duke@435: // Lookup replacement variable in operand's component list duke@435: const char *rep_var = disp + 1; duke@435: const Component *comp = this->_components.search(rep_var); duke@435: assert( comp != NULL, "Replacement variable not found in components"); duke@435: // Lookup operand form for replacement variable's type duke@435: const char *type = comp->_type; duke@435: Form *form = (Form*)globals[type]; duke@435: assert( form != NULL, "Replacement variable's type not found"); duke@435: OperandForm *op = form->is_operand(); duke@435: assert( op, "Memory Interface 'disp' can only emit an operand form"); duke@435: // Check if this is a ConP, which may require relocation duke@435: if ( op->is_base_constant(globals) == Form::idealP ) { duke@435: // Find the constant's index: _c0, _c1, _c2, ... , _cN duke@435: uint idx = op->constant_position( globals, rep_var); duke@435: fprintf(fp," virtual bool disp_is_oop() const {", _ident); duke@435: fprintf(fp, " return _c%d->isa_oop_ptr();", idx); duke@435: fprintf(fp, " }\n"); duke@435: } duke@435: } duke@435: duke@435: // Generate code for internal and external format methods duke@435: // duke@435: // internal access to reg# node->_idx duke@435: // access to subsumed constant _c0, _c1, duke@435: void OperandForm::int_format(FILE *fp, FormDict &globals, uint index) { duke@435: Form::DataType dtype; duke@435: if (_matrule && (_matrule->is_base_register(globals) || duke@435: strcmp(ideal_type(globalAD->globalNames()), "RegFlags") == 0)) { duke@435: // !!!!! !!!!! duke@435: fprintf(fp, "{ char reg_str[128];\n"); duke@435: fprintf(fp," ra->dump_register(node,reg_str);\n"); duke@435: fprintf(fp," tty->print(\"%cs\",reg_str);\n",'%'); duke@435: fprintf(fp," }\n"); duke@435: } else if (_matrule && (dtype = _matrule->is_base_constant(globals)) != Form::none) { duke@435: format_constant( fp, index, dtype ); duke@435: } else if (ideal_to_sReg_type(_ident) != Form::none) { duke@435: // Special format for Stack Slot Register duke@435: fprintf(fp, "{ char reg_str[128];\n"); duke@435: fprintf(fp," ra->dump_register(node,reg_str);\n"); duke@435: fprintf(fp," tty->print(\"%cs\",reg_str);\n",'%'); duke@435: fprintf(fp," }\n"); duke@435: } else { duke@435: fprintf(fp,"tty->print(\"No format defined for %s\n\");\n", _ident); duke@435: fflush(fp); duke@435: fprintf(stderr,"No format defined for %s\n", _ident); duke@435: dump(); duke@435: assert( false,"Internal error:\n output_internal_operand() attempting to output other than a Register or Constant"); duke@435: } duke@435: } duke@435: duke@435: // Similar to "int_format" but for cases where data is external to operand duke@435: // external access to reg# node->in(idx)->_idx, duke@435: void OperandForm::ext_format(FILE *fp, FormDict &globals, uint index) { duke@435: Form::DataType dtype; duke@435: if (_matrule && (_matrule->is_base_register(globals) || duke@435: strcmp(ideal_type(globalAD->globalNames()), "RegFlags") == 0)) { duke@435: fprintf(fp, "{ char reg_str[128];\n"); duke@435: fprintf(fp," ra->dump_register(node->in(idx"); duke@435: if ( index != 0 ) fprintf(fp, "+%d",index); duke@435: fprintf(fp, "),reg_str);\n"); duke@435: fprintf(fp," tty->print(\"%cs\",reg_str);\n",'%'); duke@435: fprintf(fp," }\n"); duke@435: } else if (_matrule && (dtype = _matrule->is_base_constant(globals)) != Form::none) { duke@435: format_constant( fp, index, dtype ); duke@435: } else if (ideal_to_sReg_type(_ident) != Form::none) { duke@435: // Special format for Stack Slot Register duke@435: fprintf(fp, "{ char reg_str[128];\n"); duke@435: fprintf(fp," ra->dump_register(node->in(idx"); duke@435: if ( index != 0 ) fprintf(fp, "+%d",index); duke@435: fprintf(fp, "),reg_str);\n"); duke@435: fprintf(fp," tty->print(\"%cs\",reg_str);\n",'%'); duke@435: fprintf(fp," }\n"); duke@435: } else { duke@435: fprintf(fp,"tty->print(\"No format defined for %s\n\");\n", _ident); duke@435: assert( false,"Internal error:\n output_external_operand() attempting to output other than a Register or Constant"); duke@435: } duke@435: } duke@435: duke@435: void OperandForm::format_constant(FILE *fp, uint const_index, uint const_type) { duke@435: switch(const_type) { coleenp@548: case Form::idealI: fprintf(fp,"st->print(\"#%%d\", _c%d);\n", const_index); break; coleenp@548: case Form::idealP: fprintf(fp,"_c%d->dump_on(st);\n", const_index); break; coleenp@548: case Form::idealN: fprintf(fp,"_c%d->dump_on(st);\n", const_index); break; coleenp@548: case Form::idealL: fprintf(fp,"st->print(\"#%%lld\", _c%d);\n", const_index); break; coleenp@548: case Form::idealF: fprintf(fp,"st->print(\"#%%f\", _c%d);\n", const_index); break; coleenp@548: case Form::idealD: fprintf(fp,"st->print(\"#%%f\", _c%d);\n", const_index); break; duke@435: default: duke@435: assert( false, "ShouldNotReachHere()"); duke@435: } duke@435: } duke@435: duke@435: // Return the operand form corresponding to the given index, else NULL. duke@435: OperandForm *OperandForm::constant_operand(FormDict &globals, duke@435: uint index) { duke@435: // !!!!! duke@435: // Check behavior on complex operands duke@435: uint n_consts = num_consts(globals); duke@435: if( n_consts > 0 ) { duke@435: uint i = 0; duke@435: const char *type; duke@435: Component *comp; duke@435: _components.reset(); duke@435: if ((comp = _components.iter()) == NULL) { duke@435: assert(n_consts == 1, "Bad component list detected.\n"); duke@435: // Current operand is THE operand duke@435: if ( index == 0 ) { duke@435: return this; duke@435: } duke@435: } // end if NULL duke@435: else { duke@435: // Skip the first component, it can not be a DEF of a constant duke@435: do { duke@435: type = comp->base_type(globals); duke@435: // Check that "type" is a 'ConI', 'ConP', ... duke@435: if ( ideal_to_const_type(type) != Form::none ) { duke@435: // When at correct component, get corresponding Operand duke@435: if ( index == 0 ) { duke@435: return globals[comp->_type]->is_operand(); duke@435: } duke@435: // Decrement number of constants to go duke@435: --index; duke@435: } duke@435: } while((comp = _components.iter()) != NULL); duke@435: } duke@435: } duke@435: duke@435: // Did not find a constant for this index. duke@435: return NULL; duke@435: } duke@435: duke@435: // If this operand has a single ideal type, return its type duke@435: Form::DataType OperandForm::simple_type(FormDict &globals) const { duke@435: const char *type_name = ideal_type(globals); duke@435: Form::DataType type = type_name ? ideal_to_const_type( type_name ) duke@435: : Form::none; duke@435: return type; duke@435: } duke@435: duke@435: Form::DataType OperandForm::is_base_constant(FormDict &globals) const { duke@435: if ( _matrule == NULL ) return Form::none; duke@435: duke@435: return _matrule->is_base_constant(globals); duke@435: } duke@435: duke@435: // "true" if this operand is a simple type that is swallowed duke@435: bool OperandForm::swallowed(FormDict &globals) const { duke@435: Form::DataType type = simple_type(globals); duke@435: if( type != Form::none ) { duke@435: return true; duke@435: } duke@435: duke@435: return false; duke@435: } duke@435: duke@435: // Output code to access the value of the index'th constant duke@435: void OperandForm::access_constant(FILE *fp, FormDict &globals, duke@435: uint const_index) { duke@435: OperandForm *oper = constant_operand(globals, const_index); duke@435: assert( oper, "Index exceeds number of constants in operand"); duke@435: Form::DataType dtype = oper->is_base_constant(globals); duke@435: duke@435: switch(dtype) { duke@435: case idealI: fprintf(fp,"_c%d", const_index); break; duke@435: case idealP: fprintf(fp,"_c%d->get_con()",const_index); break; duke@435: case idealL: fprintf(fp,"_c%d", const_index); break; duke@435: case idealF: fprintf(fp,"_c%d", const_index); break; duke@435: case idealD: fprintf(fp,"_c%d", const_index); break; duke@435: default: duke@435: assert( false, "ShouldNotReachHere()"); duke@435: } duke@435: } duke@435: duke@435: duke@435: void OperandForm::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: void OperandForm::output(FILE *fp) { duke@435: fprintf(fp,"\nOperand: %s\n", (_ident?_ident:"")); duke@435: if (_matrule) _matrule->dump(); duke@435: if (_interface) _interface->dump(); duke@435: if (_attribs) _attribs->dump(); duke@435: if (_predicate) _predicate->dump(); duke@435: if (_constraint) _constraint->dump(); duke@435: if (_construct) _construct->dump(); duke@435: if (_format) _format->dump(); duke@435: } duke@435: duke@435: //------------------------------Constraint------------------------------------- duke@435: Constraint::Constraint(const char *func, const char *arg) duke@435: : _func(func), _arg(arg) { duke@435: } duke@435: Constraint::~Constraint() { /* not owner of char* */ duke@435: } duke@435: duke@435: bool Constraint::stack_slots_only() const { duke@435: return strcmp(_func, "ALLOC_IN_RC") == 0 duke@435: && strcmp(_arg, "stack_slots") == 0; duke@435: } duke@435: duke@435: void Constraint::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: void Constraint::output(FILE *fp) { // Write info to output files duke@435: assert((_func != NULL && _arg != NULL),"missing constraint function or arg"); duke@435: fprintf(fp,"Constraint: %s ( %s )\n", _func, _arg); duke@435: } duke@435: duke@435: //------------------------------Predicate-------------------------------------- duke@435: Predicate::Predicate(char *pr) duke@435: : _pred(pr) { duke@435: } duke@435: Predicate::~Predicate() { duke@435: } duke@435: duke@435: void Predicate::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: void Predicate::output(FILE *fp) { duke@435: fprintf(fp,"Predicate"); // Write to output files duke@435: } duke@435: //------------------------------Interface-------------------------------------- duke@435: Interface::Interface(const char *name) : _name(name) { duke@435: } duke@435: Interface::~Interface() { duke@435: } duke@435: duke@435: Form::InterfaceType Interface::interface_type(FormDict &globals) const { duke@435: Interface *thsi = (Interface*)this; duke@435: if ( thsi->is_RegInterface() ) return Form::register_interface; duke@435: if ( thsi->is_MemInterface() ) return Form::memory_interface; duke@435: if ( thsi->is_ConstInterface() ) return Form::constant_interface; duke@435: if ( thsi->is_CondInterface() ) return Form::conditional_interface; duke@435: duke@435: return Form::no_interface; duke@435: } duke@435: duke@435: RegInterface *Interface::is_RegInterface() { duke@435: if ( strcmp(_name,"REG_INTER") != 0 ) duke@435: return NULL; duke@435: return (RegInterface*)this; duke@435: } duke@435: MemInterface *Interface::is_MemInterface() { duke@435: if ( strcmp(_name,"MEMORY_INTER") != 0 ) return NULL; duke@435: return (MemInterface*)this; duke@435: } duke@435: ConstInterface *Interface::is_ConstInterface() { duke@435: if ( strcmp(_name,"CONST_INTER") != 0 ) return NULL; duke@435: return (ConstInterface*)this; duke@435: } duke@435: CondInterface *Interface::is_CondInterface() { duke@435: if ( strcmp(_name,"COND_INTER") != 0 ) return NULL; duke@435: return (CondInterface*)this; duke@435: } duke@435: duke@435: duke@435: void Interface::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: // Write info to output files duke@435: void Interface::output(FILE *fp) { duke@435: fprintf(fp,"Interface: %s\n", (_name ? _name : "") ); duke@435: } duke@435: duke@435: //------------------------------RegInterface----------------------------------- duke@435: RegInterface::RegInterface() : Interface("REG_INTER") { duke@435: } duke@435: RegInterface::~RegInterface() { duke@435: } duke@435: duke@435: void RegInterface::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: // Write info to output files duke@435: void RegInterface::output(FILE *fp) { duke@435: Interface::output(fp); duke@435: } duke@435: duke@435: //------------------------------ConstInterface--------------------------------- duke@435: ConstInterface::ConstInterface() : Interface("CONST_INTER") { duke@435: } duke@435: ConstInterface::~ConstInterface() { duke@435: } duke@435: duke@435: void ConstInterface::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: // Write info to output files duke@435: void ConstInterface::output(FILE *fp) { duke@435: Interface::output(fp); duke@435: } duke@435: duke@435: //------------------------------MemInterface----------------------------------- duke@435: MemInterface::MemInterface(char *base, char *index, char *scale, char *disp) duke@435: : Interface("MEMORY_INTER"), _base(base), _index(index), _scale(scale), _disp(disp) { duke@435: } duke@435: MemInterface::~MemInterface() { duke@435: // not owner of any character arrays duke@435: } duke@435: duke@435: void MemInterface::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: // Write info to output files duke@435: void MemInterface::output(FILE *fp) { duke@435: Interface::output(fp); duke@435: if ( _base != NULL ) fprintf(fp," base == %s\n", _base); duke@435: if ( _index != NULL ) fprintf(fp," index == %s\n", _index); duke@435: if ( _scale != NULL ) fprintf(fp," scale == %s\n", _scale); duke@435: if ( _disp != NULL ) fprintf(fp," disp == %s\n", _disp); duke@435: // fprintf(fp,"\n"); duke@435: } duke@435: duke@435: //------------------------------CondInterface---------------------------------- duke@435: CondInterface::CondInterface(char *equal, char *not_equal, duke@435: char *less, char *greater_equal, duke@435: char *less_equal, char *greater) duke@435: : Interface("COND_INTER"), duke@435: _equal(equal), _not_equal(not_equal), duke@435: _less(less), _greater_equal(greater_equal), duke@435: _less_equal(less_equal), _greater(greater) { duke@435: // duke@435: } duke@435: CondInterface::~CondInterface() { duke@435: // not owner of any character arrays duke@435: } duke@435: duke@435: void CondInterface::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: // Write info to output files duke@435: void CondInterface::output(FILE *fp) { duke@435: Interface::output(fp); duke@435: if ( _equal != NULL ) fprintf(fp," equal == %s\n", _equal); duke@435: if ( _not_equal != NULL ) fprintf(fp," not_equal == %s\n", _not_equal); duke@435: if ( _less != NULL ) fprintf(fp," less == %s\n", _less); duke@435: if ( _greater_equal != NULL ) fprintf(fp," greater_equal == %s\n", _greater_equal); duke@435: if ( _less_equal != NULL ) fprintf(fp," less_equal == %s\n", _less_equal); duke@435: if ( _greater != NULL ) fprintf(fp," greater == %s\n", _greater); duke@435: // fprintf(fp,"\n"); duke@435: } duke@435: duke@435: //------------------------------ConstructRule---------------------------------- duke@435: ConstructRule::ConstructRule(char *cnstr) duke@435: : _construct(cnstr) { duke@435: } duke@435: ConstructRule::~ConstructRule() { duke@435: } duke@435: duke@435: void ConstructRule::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: void ConstructRule::output(FILE *fp) { duke@435: fprintf(fp,"\nConstruct Rule\n"); // Write to output files duke@435: } duke@435: duke@435: duke@435: //==============================Shared Forms=================================== duke@435: //------------------------------AttributeForm---------------------------------- duke@435: int AttributeForm::_insId = 0; // start counter at 0 duke@435: int AttributeForm::_opId = 0; // start counter at 0 duke@435: const char* AttributeForm::_ins_cost = "ins_cost"; // required name duke@435: const char* AttributeForm::_ins_pc_relative = "ins_pc_relative"; duke@435: const char* AttributeForm::_op_cost = "op_cost"; // required name duke@435: duke@435: AttributeForm::AttributeForm(char *attr, int type, char *attrdef) duke@435: : Form(Form::ATTR), _attrname(attr), _atype(type), _attrdef(attrdef) { duke@435: if (type==OP_ATTR) { duke@435: id = ++_opId; duke@435: } duke@435: else if (type==INS_ATTR) { duke@435: id = ++_insId; duke@435: } duke@435: else assert( false,""); duke@435: } duke@435: AttributeForm::~AttributeForm() { duke@435: } duke@435: duke@435: // Dynamic type check duke@435: AttributeForm *AttributeForm::is_attribute() const { duke@435: return (AttributeForm*)this; duke@435: } duke@435: duke@435: duke@435: // inlined // int AttributeForm::type() { return id;} duke@435: duke@435: void AttributeForm::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: void AttributeForm::output(FILE *fp) { duke@435: if( _attrname && _attrdef ) { duke@435: fprintf(fp,"\n// AttributeForm \nstatic const int %s = %s;\n", duke@435: _attrname, _attrdef); duke@435: } duke@435: else { duke@435: fprintf(fp,"\n// AttributeForm missing name %s or definition %s\n", duke@435: (_attrname?_attrname:""), (_attrdef?_attrdef:"") ); duke@435: } duke@435: } duke@435: duke@435: //------------------------------Component-------------------------------------- duke@435: Component::Component(const char *name, const char *type, int usedef) duke@435: : _name(name), _type(type), _usedef(usedef) { duke@435: _ftype = Form::COMP; duke@435: } duke@435: Component::~Component() { duke@435: } duke@435: duke@435: // True if this component is equal to the parameter. duke@435: bool Component::is(int use_def_kill_enum) const { duke@435: return (_usedef == use_def_kill_enum ? true : false); duke@435: } duke@435: // True if this component is used/def'd/kill'd as the parameter suggests. duke@435: bool Component::isa(int use_def_kill_enum) const { duke@435: return (_usedef & use_def_kill_enum) == use_def_kill_enum; duke@435: } duke@435: duke@435: // Extend this component with additional use/def/kill behavior duke@435: int Component::promote_use_def_info(int new_use_def) { duke@435: _usedef |= new_use_def; duke@435: duke@435: return _usedef; duke@435: } duke@435: duke@435: // Check the base type of this component, if it has one duke@435: const char *Component::base_type(FormDict &globals) { duke@435: const Form *frm = globals[_type]; duke@435: if (frm == NULL) return NULL; duke@435: OperandForm *op = frm->is_operand(); duke@435: if (op == NULL) return NULL; duke@435: if (op->ideal_only()) return op->_ident; duke@435: return (char *)op->ideal_type(globals); duke@435: } duke@435: duke@435: void Component::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: void Component::output(FILE *fp) { duke@435: fprintf(fp,"Component:"); // Write to output files duke@435: fprintf(fp, " name = %s", _name); duke@435: fprintf(fp, ", type = %s", _type); duke@435: const char * usedef = "Undefined Use/Def info"; duke@435: switch (_usedef) { duke@435: case USE: usedef = "USE"; break; duke@435: case USE_DEF: usedef = "USE_DEF"; break; duke@435: case USE_KILL: usedef = "USE_KILL"; break; duke@435: case KILL: usedef = "KILL"; break; duke@435: case TEMP: usedef = "TEMP"; break; duke@435: case DEF: usedef = "DEF"; break; duke@435: default: assert(false, "unknown effect"); duke@435: } duke@435: fprintf(fp, ", use/def = %s\n", usedef); duke@435: } duke@435: duke@435: duke@435: //------------------------------ComponentList--------------------------------- duke@435: ComponentList::ComponentList() : NameList(), _matchcnt(0) { duke@435: } duke@435: ComponentList::~ComponentList() { duke@435: // // This list may not own its elements if copied via assignment duke@435: // Component *component; duke@435: // for (reset(); (component = iter()) != NULL;) { duke@435: // delete component; duke@435: // } duke@435: } duke@435: duke@435: void ComponentList::insert(Component *component, bool mflag) { duke@435: NameList::addName((char *)component); duke@435: if(mflag) _matchcnt++; duke@435: } duke@435: void ComponentList::insert(const char *name, const char *opType, int usedef, duke@435: bool mflag) { duke@435: Component * component = new Component(name, opType, usedef); duke@435: insert(component, mflag); duke@435: } duke@435: Component *ComponentList::current() { return (Component*)NameList::current(); } duke@435: Component *ComponentList::iter() { return (Component*)NameList::iter(); } duke@435: Component *ComponentList::match_iter() { duke@435: if(_iter < _matchcnt) return (Component*)NameList::iter(); duke@435: return NULL; duke@435: } duke@435: Component *ComponentList::post_match_iter() { duke@435: Component *comp = iter(); duke@435: // At end of list? duke@435: if ( comp == NULL ) { duke@435: return comp; duke@435: } duke@435: // In post-match components? duke@435: if (_iter > match_count()-1) { duke@435: return comp; duke@435: } duke@435: duke@435: return post_match_iter(); duke@435: } duke@435: duke@435: void ComponentList::reset() { NameList::reset(); } duke@435: int ComponentList::count() { return NameList::count(); } duke@435: duke@435: Component *ComponentList::operator[](int position) { duke@435: // Shortcut complete iteration if there are not enough entries duke@435: if (position >= count()) return NULL; duke@435: duke@435: int index = 0; duke@435: Component *component = NULL; duke@435: for (reset(); (component = iter()) != NULL;) { duke@435: if (index == position) { duke@435: return component; duke@435: } duke@435: ++index; duke@435: } duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: const Component *ComponentList::search(const char *name) { duke@435: PreserveIter pi(this); duke@435: reset(); duke@435: for( Component *comp = NULL; ((comp = iter()) != NULL); ) { duke@435: if( strcmp(comp->_name,name) == 0 ) return comp; duke@435: } duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: // Return number of USEs + number of DEFs duke@435: // When there are no components, or the first component is a USE, duke@435: // then we add '1' to hold a space for the 'result' operand. duke@435: int ComponentList::num_operands() { duke@435: PreserveIter pi(this); duke@435: uint count = 1; // result operand duke@435: uint position = 0; duke@435: duke@435: Component *component = NULL; duke@435: for( reset(); (component = iter()) != NULL; ++position ) { duke@435: if( component->isa(Component::USE) || duke@435: ( position == 0 && (! component->isa(Component::DEF))) ) { duke@435: ++count; duke@435: } duke@435: } duke@435: duke@435: return count; duke@435: } duke@435: duke@435: // Return zero-based position in list; -1 if not in list. duke@435: // if parameter 'usedef' is ::USE, it will match USE, USE_DEF, ... duke@435: int ComponentList::operand_position(const char *name, int usedef) { duke@435: PreserveIter pi(this); duke@435: int position = 0; duke@435: int num_opnds = num_operands(); duke@435: Component *component; duke@435: Component* preceding_non_use = NULL; duke@435: Component* first_def = NULL; duke@435: for (reset(); (component = iter()) != NULL; ++position) { duke@435: // When the first component is not a DEF, duke@435: // leave space for the result operand! duke@435: if ( position==0 && (! component->isa(Component::DEF)) ) { duke@435: ++position; duke@435: ++num_opnds; duke@435: } duke@435: if (strcmp(name, component->_name)==0 && (component->isa(usedef))) { duke@435: // When the first entry in the component list is a DEF and a USE duke@435: // Treat them as being separate, a DEF first, then a USE duke@435: if( position==0 duke@435: && usedef==Component::USE && component->isa(Component::DEF) ) { duke@435: assert(position+1 < num_opnds, "advertised index in bounds"); duke@435: return position+1; duke@435: } else { duke@435: if( preceding_non_use && strcmp(component->_name, preceding_non_use->_name) ) { duke@435: fprintf(stderr, "the name '%s' should not precede the name '%s'\n", preceding_non_use->_name, name); duke@435: } duke@435: if( position >= num_opnds ) { duke@435: fprintf(stderr, "the name '%s' is too late in its name list\n", name); duke@435: } duke@435: assert(position < num_opnds, "advertised index in bounds"); duke@435: return position; duke@435: } duke@435: } duke@435: if( component->isa(Component::DEF) duke@435: && component->isa(Component::USE) ) { duke@435: ++position; duke@435: if( position != 1 ) --position; // only use two slots for the 1st USE_DEF duke@435: } duke@435: if( component->isa(Component::DEF) && !first_def ) { duke@435: first_def = component; duke@435: } duke@435: if( !component->isa(Component::USE) && component != first_def ) { duke@435: preceding_non_use = component; duke@435: } else if( preceding_non_use && !strcmp(component->_name, preceding_non_use->_name) ) { duke@435: preceding_non_use = NULL; duke@435: } duke@435: } duke@435: return Not_in_list; duke@435: } duke@435: duke@435: // Find position for this name, regardless of use/def information duke@435: int ComponentList::operand_position(const char *name) { duke@435: PreserveIter pi(this); duke@435: int position = 0; duke@435: Component *component; duke@435: for (reset(); (component = iter()) != NULL; ++position) { duke@435: // When the first component is not a DEF, duke@435: // leave space for the result operand! duke@435: if ( position==0 && (! component->isa(Component::DEF)) ) { duke@435: ++position; duke@435: } duke@435: if (strcmp(name, component->_name)==0) { duke@435: return position; duke@435: } duke@435: if( component->isa(Component::DEF) duke@435: && component->isa(Component::USE) ) { duke@435: ++position; duke@435: if( position != 1 ) --position; // only use two slots for the 1st USE_DEF duke@435: } duke@435: } duke@435: return Not_in_list; duke@435: } duke@435: duke@435: int ComponentList::operand_position_format(const char *name) { duke@435: PreserveIter pi(this); duke@435: int first_position = operand_position(name); duke@435: int use_position = operand_position(name, Component::USE); duke@435: duke@435: return ((first_position < use_position) ? use_position : first_position); duke@435: } duke@435: duke@435: int ComponentList::label_position() { duke@435: PreserveIter pi(this); duke@435: int position = 0; duke@435: reset(); duke@435: for( Component *comp; (comp = iter()) != NULL; ++position) { duke@435: // When the first component is not a DEF, duke@435: // leave space for the result operand! duke@435: if ( position==0 && (! comp->isa(Component::DEF)) ) { duke@435: ++position; duke@435: } duke@435: if (strcmp(comp->_type, "label")==0) { duke@435: return position; duke@435: } duke@435: if( comp->isa(Component::DEF) duke@435: && comp->isa(Component::USE) ) { duke@435: ++position; duke@435: if( position != 1 ) --position; // only use two slots for the 1st USE_DEF duke@435: } duke@435: } duke@435: duke@435: return -1; duke@435: } duke@435: duke@435: int ComponentList::method_position() { duke@435: PreserveIter pi(this); duke@435: int position = 0; duke@435: reset(); duke@435: for( Component *comp; (comp = iter()) != NULL; ++position) { duke@435: // When the first component is not a DEF, duke@435: // leave space for the result operand! duke@435: if ( position==0 && (! comp->isa(Component::DEF)) ) { duke@435: ++position; duke@435: } duke@435: if (strcmp(comp->_type, "method")==0) { duke@435: return position; duke@435: } duke@435: if( comp->isa(Component::DEF) duke@435: && comp->isa(Component::USE) ) { duke@435: ++position; duke@435: if( position != 1 ) --position; // only use two slots for the 1st USE_DEF duke@435: } duke@435: } duke@435: duke@435: return -1; duke@435: } duke@435: duke@435: void ComponentList::dump() { output(stderr); } duke@435: duke@435: void ComponentList::output(FILE *fp) { duke@435: PreserveIter pi(this); duke@435: fprintf(fp, "\n"); duke@435: Component *component; duke@435: for (reset(); (component = iter()) != NULL;) { duke@435: component->output(fp); duke@435: } duke@435: fprintf(fp, "\n"); duke@435: } duke@435: duke@435: //------------------------------MatchNode-------------------------------------- duke@435: MatchNode::MatchNode(ArchDesc &ad, const char *result, const char *mexpr, duke@435: const char *opType, MatchNode *lChild, MatchNode *rChild) duke@435: : _AD(ad), _result(result), _name(mexpr), _opType(opType), duke@435: _lChild(lChild), _rChild(rChild), _internalop(0), _numleaves(0), duke@435: _commutative_id(0) { duke@435: _numleaves = (lChild ? lChild->_numleaves : 0) duke@435: + (rChild ? rChild->_numleaves : 0); duke@435: } duke@435: duke@435: MatchNode::MatchNode(ArchDesc &ad, MatchNode& mnode) duke@435: : _AD(ad), _result(mnode._result), _name(mnode._name), duke@435: _opType(mnode._opType), _lChild(mnode._lChild), _rChild(mnode._rChild), duke@435: _internalop(0), _numleaves(mnode._numleaves), duke@435: _commutative_id(mnode._commutative_id) { duke@435: } duke@435: duke@435: MatchNode::MatchNode(ArchDesc &ad, MatchNode& mnode, int clone) duke@435: : _AD(ad), _result(mnode._result), _name(mnode._name), duke@435: _opType(mnode._opType), duke@435: _internalop(0), _numleaves(mnode._numleaves), duke@435: _commutative_id(mnode._commutative_id) { duke@435: if (mnode._lChild) { duke@435: _lChild = new MatchNode(ad, *mnode._lChild, clone); duke@435: } else { duke@435: _lChild = NULL; duke@435: } duke@435: if (mnode._rChild) { duke@435: _rChild = new MatchNode(ad, *mnode._rChild, clone); duke@435: } else { duke@435: _rChild = NULL; duke@435: } duke@435: } duke@435: duke@435: MatchNode::~MatchNode() { duke@435: // // This node may not own its children if copied via assignment duke@435: // if( _lChild ) delete _lChild; duke@435: // if( _rChild ) delete _rChild; duke@435: } duke@435: duke@435: bool MatchNode::find_type(const char *type, int &position) const { duke@435: if ( (_lChild != NULL) && (_lChild->find_type(type, position)) ) return true; duke@435: if ( (_rChild != NULL) && (_rChild->find_type(type, position)) ) return true; duke@435: duke@435: if (strcmp(type,_opType)==0) { duke@435: return true; duke@435: } else { duke@435: ++position; duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: // Recursive call collecting info on top-level operands, not transitive. duke@435: // Implementation does not modify state of internal structures. duke@435: void MatchNode::append_components(FormDict &locals, ComponentList &components, duke@435: bool deflag) const { duke@435: int usedef = deflag ? Component::DEF : Component::USE; duke@435: FormDict &globals = _AD.globalNames(); duke@435: duke@435: assert (_name != NULL, "MatchNode::build_components encountered empty node\n"); duke@435: // Base case duke@435: if (_lChild==NULL && _rChild==NULL) { duke@435: // If _opType is not an operation, do not build a component for it ##### duke@435: const Form *f = globals[_opType]; duke@435: if( f != NULL ) { duke@435: // Add non-ideals that are operands, operand-classes, duke@435: if( ! f->ideal_only() duke@435: && (f->is_opclass() || f->is_operand()) ) { duke@435: components.insert(_name, _opType, usedef, true); duke@435: } duke@435: } duke@435: return; duke@435: } duke@435: // Promote results of "Set" to DEF duke@435: bool def_flag = (!strcmp(_opType, "Set")) ? true : false; duke@435: if (_lChild) _lChild->append_components(locals, components, def_flag); duke@435: def_flag = false; // only applies to component immediately following 'Set' duke@435: if (_rChild) _rChild->append_components(locals, components, def_flag); duke@435: } duke@435: duke@435: // Find the n'th base-operand in the match node, duke@435: // recursively investigates match rules of user-defined operands. duke@435: // duke@435: // Implementation does not modify state of internal structures since they duke@435: // can be shared. duke@435: bool MatchNode::base_operand(uint &position, FormDict &globals, duke@435: const char * &result, const char * &name, duke@435: const char * &opType) const { duke@435: assert (_name != NULL, "MatchNode::base_operand encountered empty node\n"); duke@435: // Base case duke@435: if (_lChild==NULL && _rChild==NULL) { duke@435: // Check for special case: "Universe", "label" duke@435: if (strcmp(_opType,"Universe") == 0 || strcmp(_opType,"label")==0 ) { duke@435: if (position == 0) { duke@435: result = _result; duke@435: name = _name; duke@435: opType = _opType; duke@435: return 1; duke@435: } else { duke@435: -- position; duke@435: return 0; duke@435: } duke@435: } duke@435: duke@435: const Form *form = globals[_opType]; duke@435: MatchNode *matchNode = NULL; duke@435: // Check for user-defined type duke@435: if (form) { duke@435: // User operand or instruction? duke@435: OperandForm *opForm = form->is_operand(); duke@435: InstructForm *inForm = form->is_instruction(); duke@435: if ( opForm ) { duke@435: matchNode = (MatchNode*)opForm->_matrule; duke@435: } else if ( inForm ) { duke@435: matchNode = (MatchNode*)inForm->_matrule; duke@435: } duke@435: } duke@435: // if this is user-defined, recurse on match rule duke@435: // User-defined operand and instruction forms have a match-rule. duke@435: if (matchNode) { duke@435: return (matchNode->base_operand(position,globals,result,name,opType)); duke@435: } else { duke@435: // Either not a form, or a system-defined form (no match rule). duke@435: if (position==0) { duke@435: result = _result; duke@435: name = _name; duke@435: opType = _opType; duke@435: return 1; duke@435: } else { duke@435: --position; duke@435: return 0; duke@435: } duke@435: } duke@435: duke@435: } else { duke@435: // Examine the left child and right child as well duke@435: if (_lChild) { duke@435: if (_lChild->base_operand(position, globals, result, name, opType)) duke@435: return 1; duke@435: } duke@435: duke@435: if (_rChild) { duke@435: if (_rChild->base_operand(position, globals, result, name, opType)) duke@435: return 1; duke@435: } duke@435: } duke@435: duke@435: return 0; duke@435: } duke@435: duke@435: // Recursive call on all operands' match rules in my match rule. duke@435: uint MatchNode::num_consts(FormDict &globals) const { duke@435: uint index = 0; duke@435: uint num_consts = 0; duke@435: const char *result; duke@435: const char *name; duke@435: const char *opType; duke@435: duke@435: for (uint position = index; duke@435: base_operand(position,globals,result,name,opType); position = index) { duke@435: ++index; duke@435: if( ideal_to_const_type(opType) ) num_consts++; duke@435: } duke@435: duke@435: return num_consts; duke@435: } duke@435: duke@435: // Recursive call on all operands' match rules in my match rule. duke@435: // Constants in match rule subtree with specified type duke@435: uint MatchNode::num_consts(FormDict &globals, Form::DataType type) const { duke@435: uint index = 0; duke@435: uint num_consts = 0; duke@435: const char *result; duke@435: const char *name; duke@435: const char *opType; duke@435: duke@435: for (uint position = index; duke@435: base_operand(position,globals,result,name,opType); position = index) { duke@435: ++index; duke@435: if( ideal_to_const_type(opType) == type ) num_consts++; duke@435: } duke@435: duke@435: return num_consts; duke@435: } duke@435: duke@435: // Recursive call on all operands' match rules in my match rule. duke@435: uint MatchNode::num_const_ptrs(FormDict &globals) const { duke@435: return num_consts( globals, Form::idealP ); duke@435: } duke@435: duke@435: bool MatchNode::sets_result() const { duke@435: return ( (strcmp(_name,"Set") == 0) ? true : false ); duke@435: } duke@435: duke@435: const char *MatchNode::reduce_right(FormDict &globals) const { duke@435: // If there is no right reduction, return NULL. duke@435: const char *rightStr = NULL; duke@435: duke@435: // If we are a "Set", start from the right child. duke@435: const MatchNode *const mnode = sets_result() ? duke@435: (const MatchNode *const)this->_rChild : duke@435: (const MatchNode *const)this; duke@435: duke@435: // If our right child exists, it is the right reduction duke@435: if ( mnode->_rChild ) { duke@435: rightStr = mnode->_rChild->_internalop ? mnode->_rChild->_internalop duke@435: : mnode->_rChild->_opType; duke@435: } duke@435: // Else, May be simple chain rule: (Set dst operand_form), rightStr=NULL; duke@435: return rightStr; duke@435: } duke@435: duke@435: const char *MatchNode::reduce_left(FormDict &globals) const { duke@435: // If there is no left reduction, return NULL. duke@435: const char *leftStr = NULL; duke@435: duke@435: // If we are a "Set", start from the right child. duke@435: const MatchNode *const mnode = sets_result() ? duke@435: (const MatchNode *const)this->_rChild : duke@435: (const MatchNode *const)this; duke@435: duke@435: // If our left child exists, it is the left reduction duke@435: if ( mnode->_lChild ) { duke@435: leftStr = mnode->_lChild->_internalop ? mnode->_lChild->_internalop duke@435: : mnode->_lChild->_opType; duke@435: } else { duke@435: // May be simple chain rule: (Set dst operand_form_source) duke@435: if ( sets_result() ) { duke@435: OperandForm *oper = globals[mnode->_opType]->is_operand(); duke@435: if( oper ) { duke@435: leftStr = mnode->_opType; duke@435: } duke@435: } duke@435: } duke@435: return leftStr; duke@435: } duke@435: duke@435: //------------------------------count_instr_names------------------------------ duke@435: // Count occurrences of operands names in the leaves of the instruction duke@435: // match rule. duke@435: void MatchNode::count_instr_names( Dict &names ) { duke@435: if( !this ) return; duke@435: if( _lChild ) _lChild->count_instr_names(names); duke@435: if( _rChild ) _rChild->count_instr_names(names); duke@435: if( !_lChild && !_rChild ) { duke@435: uintptr_t cnt = (uintptr_t)names[_name]; duke@435: cnt++; // One more name found duke@435: names.Insert(_name,(void*)cnt); duke@435: } duke@435: } duke@435: duke@435: //------------------------------build_instr_pred------------------------------- duke@435: // Build a path to 'name' in buf. Actually only build if cnt is zero, so we duke@435: // can skip some leading instances of 'name'. duke@435: int MatchNode::build_instr_pred( char *buf, const char *name, int cnt ) { duke@435: if( _lChild ) { duke@435: if( !cnt ) strcpy( buf, "_kids[0]->" ); duke@435: cnt = _lChild->build_instr_pred( buf+strlen(buf), name, cnt ); duke@435: if( cnt < 0 ) return cnt; // Found it, all done duke@435: } duke@435: if( _rChild ) { duke@435: if( !cnt ) strcpy( buf, "_kids[1]->" ); duke@435: cnt = _rChild->build_instr_pred( buf+strlen(buf), name, cnt ); duke@435: if( cnt < 0 ) return cnt; // Found it, all done duke@435: } duke@435: if( !_lChild && !_rChild ) { // Found a leaf duke@435: // Wrong name? Give up... duke@435: if( strcmp(name,_name) ) return cnt; duke@435: if( !cnt ) strcpy(buf,"_leaf"); duke@435: return cnt-1; duke@435: } duke@435: return cnt; duke@435: } duke@435: duke@435: duke@435: //------------------------------build_internalop------------------------------- duke@435: // Build string representation of subtree duke@435: void MatchNode::build_internalop( ) { duke@435: char *iop, *subtree; duke@435: const char *lstr, *rstr; duke@435: // Build string representation of subtree duke@435: // Operation lchildType rchildType duke@435: int len = (int)strlen(_opType) + 4; duke@435: lstr = (_lChild) ? ((_lChild->_internalop) ? duke@435: _lChild->_internalop : _lChild->_opType) : ""; duke@435: rstr = (_rChild) ? ((_rChild->_internalop) ? duke@435: _rChild->_internalop : _rChild->_opType) : ""; duke@435: len += (int)strlen(lstr) + (int)strlen(rstr); duke@435: subtree = (char *)malloc(len); duke@435: sprintf(subtree,"_%s_%s_%s", _opType, lstr, rstr); duke@435: // Hash the subtree string in _internalOps; if a name exists, use it duke@435: iop = (char *)_AD._internalOps[subtree]; duke@435: // Else create a unique name, and add it to the hash table duke@435: if (iop == NULL) { duke@435: iop = subtree; duke@435: _AD._internalOps.Insert(subtree, iop); duke@435: _AD._internalOpNames.addName(iop); duke@435: _AD._internalMatch.Insert(iop, this); duke@435: } duke@435: // Add the internal operand name to the MatchNode duke@435: _internalop = iop; duke@435: _result = iop; duke@435: } duke@435: duke@435: duke@435: void MatchNode::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: void MatchNode::output(FILE *fp) { duke@435: if (_lChild==0 && _rChild==0) { duke@435: fprintf(fp," %s",_name); // operand duke@435: } duke@435: else { duke@435: fprintf(fp," (%s ",_name); // " (opcodeName " duke@435: if(_lChild) _lChild->output(fp); // left operand duke@435: if(_rChild) _rChild->output(fp); // right operand duke@435: fprintf(fp,")"); // ")" duke@435: } duke@435: } duke@435: duke@435: int MatchNode::needs_ideal_memory_edge(FormDict &globals) const { duke@435: static const char *needs_ideal_memory_list[] = { coleenp@548: "StoreI","StoreL","StoreP","StoreN","StoreD","StoreF" , duke@435: "StoreB","StoreC","Store" ,"StoreFP", coleenp@548: "LoadI" ,"LoadL", "LoadP" ,"LoadN", "LoadD" ,"LoadF" , duke@435: "LoadB" ,"LoadC" ,"LoadS" ,"Load" , duke@435: "Store4I","Store2I","Store2L","Store2D","Store4F","Store2F","Store16B", duke@435: "Store8B","Store4B","Store8C","Store4C","Store2C", duke@435: "Load4I" ,"Load2I" ,"Load2L" ,"Load2D" ,"Load4F" ,"Load2F" ,"Load16B" , duke@435: "Load8B" ,"Load4B" ,"Load8C" ,"Load4C" ,"Load2C" ,"Load8S", "Load4S","Load2S", kvn@599: "LoadRange", "LoadKlass", "LoadNKlass", "LoadL_unaligned", "LoadD_unaligned", duke@435: "LoadPLocked", "LoadLLocked", duke@435: "StorePConditional", "StoreLConditional", coleenp@548: "CompareAndSwapI", "CompareAndSwapL", "CompareAndSwapP", "CompareAndSwapN", duke@435: "StoreCM", duke@435: "ClearArray" duke@435: }; duke@435: int cnt = sizeof(needs_ideal_memory_list)/sizeof(char*); duke@435: if( strcmp(_opType,"PrefetchRead")==0 || strcmp(_opType,"PrefetchWrite")==0 ) duke@435: return 1; duke@435: if( _lChild ) { duke@435: const char *opType = _lChild->_opType; duke@435: for( int i=0; ineeds_ideal_memory_edge(globals) ) duke@435: return 1; duke@435: } duke@435: if( _rChild ) { duke@435: const char *opType = _rChild->_opType; duke@435: for( int i=0; ineeds_ideal_memory_edge(globals) ) duke@435: return 1; duke@435: } duke@435: duke@435: return 0; duke@435: } duke@435: duke@435: // TRUE if defines a derived oop, and so needs a base oop edge present duke@435: // post-matching. duke@435: int MatchNode::needs_base_oop_edge() const { duke@435: if( !strcmp(_opType,"AddP") ) return 1; duke@435: if( strcmp(_opType,"Set") ) return 0; duke@435: return !strcmp(_rChild->_opType,"AddP"); duke@435: } duke@435: duke@435: int InstructForm::needs_base_oop_edge(FormDict &globals) const { duke@435: if( is_simple_chain_rule(globals) ) { duke@435: const char *src = _matrule->_rChild->_opType; duke@435: OperandForm *src_op = globals[src]->is_operand(); duke@435: assert( src_op, "Not operand class of chain rule" ); duke@435: return src_op->_matrule ? src_op->_matrule->needs_base_oop_edge() : 0; duke@435: } // Else check instruction duke@435: duke@435: return _matrule ? _matrule->needs_base_oop_edge() : 0; duke@435: } duke@435: duke@435: duke@435: //-------------------------cisc spilling methods------------------------------- duke@435: // helper routines and methods for detecting cisc-spilling instructions duke@435: //-------------------------cisc_spill_merge------------------------------------ duke@435: int MatchNode::cisc_spill_merge(int left_spillable, int right_spillable) { duke@435: int cisc_spillable = Maybe_cisc_spillable; duke@435: duke@435: // Combine results of left and right checks duke@435: if( (left_spillable == Maybe_cisc_spillable) && (right_spillable == Maybe_cisc_spillable) ) { duke@435: // neither side is spillable, nor prevents cisc spilling duke@435: cisc_spillable = Maybe_cisc_spillable; duke@435: } duke@435: else if( (left_spillable == Maybe_cisc_spillable) && (right_spillable > Maybe_cisc_spillable) ) { duke@435: // right side is spillable duke@435: cisc_spillable = right_spillable; duke@435: } duke@435: else if( (right_spillable == Maybe_cisc_spillable) && (left_spillable > Maybe_cisc_spillable) ) { duke@435: // left side is spillable duke@435: cisc_spillable = left_spillable; duke@435: } duke@435: else if( (left_spillable == Not_cisc_spillable) || (right_spillable == Not_cisc_spillable) ) { duke@435: // left or right prevents cisc spilling this instruction duke@435: cisc_spillable = Not_cisc_spillable; duke@435: } duke@435: else { duke@435: // Only allow one to spill duke@435: cisc_spillable = Not_cisc_spillable; duke@435: } duke@435: duke@435: return cisc_spillable; duke@435: } duke@435: duke@435: //-------------------------root_ops_match-------------------------------------- duke@435: bool static root_ops_match(FormDict &globals, const char *op1, const char *op2) { duke@435: // Base Case: check that the current operands/operations match duke@435: assert( op1, "Must have op's name"); duke@435: assert( op2, "Must have op's name"); duke@435: const Form *form1 = globals[op1]; duke@435: const Form *form2 = globals[op2]; duke@435: duke@435: return (form1 == form2); duke@435: } duke@435: duke@435: //-------------------------cisc_spill_match------------------------------------ duke@435: // Recursively check two MatchRules for legal conversion via cisc-spilling duke@435: int MatchNode::cisc_spill_match(FormDict &globals, RegisterForm *registers, MatchNode *mRule2, const char * &operand, const char * ®_type) { duke@435: int cisc_spillable = Maybe_cisc_spillable; duke@435: int left_spillable = Maybe_cisc_spillable; duke@435: int right_spillable = Maybe_cisc_spillable; duke@435: duke@435: // Check that each has same number of operands at this level duke@435: if( (_lChild && !(mRule2->_lChild)) || (_rChild && !(mRule2->_rChild)) ) duke@435: return Not_cisc_spillable; duke@435: duke@435: // Base Case: check that the current operands/operations match duke@435: // or are CISC spillable duke@435: assert( _opType, "Must have _opType"); duke@435: assert( mRule2->_opType, "Must have _opType"); duke@435: const Form *form = globals[_opType]; duke@435: const Form *form2 = globals[mRule2->_opType]; duke@435: if( form == form2 ) { duke@435: cisc_spillable = Maybe_cisc_spillable; duke@435: } else { duke@435: const InstructForm *form2_inst = form2 ? form2->is_instruction() : NULL; duke@435: const char *name_left = mRule2->_lChild ? mRule2->_lChild->_opType : NULL; duke@435: const char *name_right = mRule2->_rChild ? mRule2->_rChild->_opType : NULL; duke@435: // Detect reg vs (loadX memory) duke@435: if( form->is_cisc_reg(globals) duke@435: && form2_inst duke@435: && (is_load_from_memory(mRule2->_opType) != Form::none) // reg vs. (load memory) duke@435: && (name_left != NULL) // NOT (load) duke@435: && (name_right == NULL) ) { // NOT (load memory foo) duke@435: const Form *form2_left = name_left ? globals[name_left] : NULL; duke@435: if( form2_left && form2_left->is_cisc_mem(globals) ) { duke@435: cisc_spillable = Is_cisc_spillable; duke@435: operand = _name; duke@435: reg_type = _result; duke@435: return Is_cisc_spillable; duke@435: } else { duke@435: cisc_spillable = Not_cisc_spillable; duke@435: } duke@435: } duke@435: // Detect reg vs memory duke@435: else if( form->is_cisc_reg(globals) && form2->is_cisc_mem(globals) ) { duke@435: cisc_spillable = Is_cisc_spillable; duke@435: operand = _name; duke@435: reg_type = _result; duke@435: return Is_cisc_spillable; duke@435: } else { duke@435: cisc_spillable = Not_cisc_spillable; duke@435: } duke@435: } duke@435: duke@435: // If cisc is still possible, check rest of tree duke@435: if( cisc_spillable == Maybe_cisc_spillable ) { duke@435: // Check that each has same number of operands at this level duke@435: if( (_lChild && !(mRule2->_lChild)) || (_rChild && !(mRule2->_rChild)) ) return Not_cisc_spillable; duke@435: duke@435: // Check left operands duke@435: if( (_lChild == NULL) && (mRule2->_lChild == NULL) ) { duke@435: left_spillable = Maybe_cisc_spillable; duke@435: } else { duke@435: left_spillable = _lChild->cisc_spill_match(globals, registers, mRule2->_lChild, operand, reg_type); duke@435: } duke@435: duke@435: // Check right operands duke@435: if( (_rChild == NULL) && (mRule2->_rChild == NULL) ) { duke@435: right_spillable = Maybe_cisc_spillable; duke@435: } else { duke@435: right_spillable = _rChild->cisc_spill_match(globals, registers, mRule2->_rChild, operand, reg_type); duke@435: } duke@435: duke@435: // Combine results of left and right checks duke@435: cisc_spillable = cisc_spill_merge(left_spillable, right_spillable); duke@435: } duke@435: duke@435: return cisc_spillable; duke@435: } duke@435: duke@435: //---------------------------cisc_spill_match---------------------------------- duke@435: // Recursively check two MatchRules for legal conversion via cisc-spilling duke@435: // This method handles the root of Match tree, duke@435: // general recursive checks done in MatchNode duke@435: int MatchRule::cisc_spill_match(FormDict &globals, RegisterForm *registers, duke@435: MatchRule *mRule2, const char * &operand, duke@435: const char * ®_type) { duke@435: int cisc_spillable = Maybe_cisc_spillable; duke@435: int left_spillable = Maybe_cisc_spillable; duke@435: int right_spillable = Maybe_cisc_spillable; duke@435: duke@435: // Check that each sets a result duke@435: if( !(sets_result() && mRule2->sets_result()) ) return Not_cisc_spillable; duke@435: // Check that each has same number of operands at this level duke@435: if( (_lChild && !(mRule2->_lChild)) || (_rChild && !(mRule2->_rChild)) ) return Not_cisc_spillable; duke@435: duke@435: // Check left operands: at root, must be target of 'Set' duke@435: if( (_lChild == NULL) || (mRule2->_lChild == NULL) ) { duke@435: left_spillable = Not_cisc_spillable; duke@435: } else { duke@435: // Do not support cisc-spilling instruction's target location duke@435: if( root_ops_match(globals, _lChild->_opType, mRule2->_lChild->_opType) ) { duke@435: left_spillable = Maybe_cisc_spillable; duke@435: } else { duke@435: left_spillable = Not_cisc_spillable; duke@435: } duke@435: } duke@435: duke@435: // Check right operands: recursive walk to identify reg->mem operand duke@435: if( (_rChild == NULL) && (mRule2->_rChild == NULL) ) { duke@435: right_spillable = Maybe_cisc_spillable; duke@435: } else { duke@435: right_spillable = _rChild->cisc_spill_match(globals, registers, mRule2->_rChild, operand, reg_type); duke@435: } duke@435: duke@435: // Combine results of left and right checks duke@435: cisc_spillable = cisc_spill_merge(left_spillable, right_spillable); duke@435: duke@435: return cisc_spillable; duke@435: } duke@435: duke@435: //----------------------------- equivalent ------------------------------------ duke@435: // Recursively check to see if two match rules are equivalent. duke@435: // This rule handles the root. duke@435: bool MatchRule::equivalent(FormDict &globals, MatchRule *mRule2) { duke@435: // Check that each sets a result duke@435: if (sets_result() != mRule2->sets_result()) { duke@435: return false; duke@435: } duke@435: duke@435: // Check that the current operands/operations match duke@435: assert( _opType, "Must have _opType"); duke@435: assert( mRule2->_opType, "Must have _opType"); duke@435: const Form *form = globals[_opType]; duke@435: const Form *form2 = globals[mRule2->_opType]; duke@435: if( form != form2 ) { duke@435: return false; duke@435: } duke@435: duke@435: if (_lChild ) { duke@435: if( !_lChild->equivalent(globals, mRule2->_lChild) ) duke@435: return false; duke@435: } else if (mRule2->_lChild) { duke@435: return false; // I have NULL left child, mRule2 has non-NULL left child. duke@435: } duke@435: duke@435: if (_rChild ) { duke@435: if( !_rChild->equivalent(globals, mRule2->_rChild) ) duke@435: return false; duke@435: } else if (mRule2->_rChild) { duke@435: return false; // I have NULL right child, mRule2 has non-NULL right child. duke@435: } duke@435: duke@435: // We've made it through the gauntlet. duke@435: return true; duke@435: } duke@435: duke@435: //----------------------------- equivalent ------------------------------------ duke@435: // Recursively check to see if two match rules are equivalent. duke@435: // This rule handles the operands. duke@435: bool MatchNode::equivalent(FormDict &globals, MatchNode *mNode2) { duke@435: if( !mNode2 ) duke@435: return false; duke@435: duke@435: // Check that the current operands/operations match duke@435: assert( _opType, "Must have _opType"); duke@435: assert( mNode2->_opType, "Must have _opType"); duke@435: const Form *form = globals[_opType]; duke@435: const Form *form2 = globals[mNode2->_opType]; duke@435: return (form == form2); duke@435: } duke@435: duke@435: //-------------------------- has_commutative_op ------------------------------- duke@435: // Recursively check for commutative operations with subtree operands duke@435: // which could be swapped. duke@435: void MatchNode::count_commutative_op(int& count) { duke@435: static const char *commut_op_list[] = { duke@435: "AddI","AddL","AddF","AddD", duke@435: "AndI","AndL", duke@435: "MaxI","MinI", duke@435: "MulI","MulL","MulF","MulD", duke@435: "OrI" ,"OrL" , duke@435: "XorI","XorL" duke@435: }; duke@435: int cnt = sizeof(commut_op_list)/sizeof(char*); duke@435: duke@435: if( _lChild && _rChild && (_lChild->_lChild || _rChild->_lChild) ) { duke@435: // Don't swap if right operand is an immediate constant. duke@435: bool is_const = false; duke@435: if( _rChild->_lChild == NULL && _rChild->_rChild == NULL ) { duke@435: FormDict &globals = _AD.globalNames(); duke@435: const Form *form = globals[_rChild->_opType]; duke@435: if ( form ) { duke@435: OperandForm *oper = form->is_operand(); duke@435: if( oper && oper->interface_type(globals) == Form::constant_interface ) duke@435: is_const = true; duke@435: } duke@435: } duke@435: if( !is_const ) { duke@435: for( int i=0; i 0 duke@435: break; duke@435: } duke@435: } duke@435: } duke@435: } duke@435: if( _lChild ) duke@435: _lChild->count_commutative_op(count); duke@435: if( _rChild ) duke@435: _rChild->count_commutative_op(count); duke@435: } duke@435: duke@435: //-------------------------- swap_commutative_op ------------------------------ duke@435: // Recursively swap specified commutative operation with subtree operands. duke@435: void MatchNode::swap_commutative_op(bool atroot, int id) { duke@435: if( _commutative_id == id ) { // id should be > 0 duke@435: assert(_lChild && _rChild && (_lChild->_lChild || _rChild->_lChild ), duke@435: "not swappable operation"); duke@435: MatchNode* tmp = _lChild; duke@435: _lChild = _rChild; duke@435: _rChild = tmp; duke@435: // Don't exit here since we need to build internalop. duke@435: } duke@435: duke@435: bool is_set = ( strcmp(_opType, "Set") == 0 ); duke@435: if( _lChild ) duke@435: _lChild->swap_commutative_op(is_set, id); duke@435: if( _rChild ) duke@435: _rChild->swap_commutative_op(is_set, id); duke@435: duke@435: // If not the root, reduce this subtree to an internal operand duke@435: if( !atroot && (_lChild || _rChild) ) { duke@435: build_internalop(); duke@435: } duke@435: } duke@435: duke@435: //-------------------------- swap_commutative_op ------------------------------ duke@435: // Recursively swap specified commutative operation with subtree operands. duke@435: void MatchRule::swap_commutative_op(const char* instr_ident, int count, int& match_rules_cnt) { duke@435: assert(match_rules_cnt < 100," too many match rule clones"); duke@435: // Clone duke@435: MatchRule* clone = new MatchRule(_AD, this); duke@435: // Swap operands of commutative operation duke@435: ((MatchNode*)clone)->swap_commutative_op(true, count); duke@435: char* buf = (char*) malloc(strlen(instr_ident) + 4); duke@435: sprintf(buf, "%s_%d", instr_ident, match_rules_cnt++); duke@435: clone->_result = buf; duke@435: duke@435: clone->_next = this->_next; duke@435: this-> _next = clone; duke@435: if( (--count) > 0 ) { duke@435: this-> swap_commutative_op(instr_ident, count, match_rules_cnt); duke@435: clone->swap_commutative_op(instr_ident, count, match_rules_cnt); duke@435: } duke@435: } duke@435: duke@435: //------------------------------MatchRule-------------------------------------- duke@435: MatchRule::MatchRule(ArchDesc &ad) duke@435: : MatchNode(ad), _depth(0), _construct(NULL), _numchilds(0) { duke@435: _next = NULL; duke@435: } duke@435: duke@435: MatchRule::MatchRule(ArchDesc &ad, MatchRule* mRule) duke@435: : MatchNode(ad, *mRule, 0), _depth(mRule->_depth), duke@435: _construct(mRule->_construct), _numchilds(mRule->_numchilds) { duke@435: _next = NULL; duke@435: } duke@435: duke@435: MatchRule::MatchRule(ArchDesc &ad, MatchNode* mroot, int depth, char *cnstr, duke@435: int numleaves) duke@435: : MatchNode(ad,*mroot), _depth(depth), _construct(cnstr), duke@435: _numchilds(0) { duke@435: _next = NULL; duke@435: mroot->_lChild = NULL; duke@435: mroot->_rChild = NULL; duke@435: delete mroot; duke@435: _numleaves = numleaves; duke@435: _numchilds = (_lChild ? 1 : 0) + (_rChild ? 1 : 0); duke@435: } duke@435: MatchRule::~MatchRule() { duke@435: } duke@435: duke@435: // Recursive call collecting info on top-level operands, not transitive. duke@435: // Implementation does not modify state of internal structures. duke@435: void MatchRule::append_components(FormDict &locals, ComponentList &components) const { duke@435: assert (_name != NULL, "MatchNode::build_components encountered empty node\n"); duke@435: duke@435: MatchNode::append_components(locals, components, duke@435: false /* not necessarily a def */); duke@435: } duke@435: duke@435: // Recursive call on all operands' match rules in my match rule. duke@435: // Implementation does not modify state of internal structures since they duke@435: // can be shared. duke@435: // The MatchNode that is called first treats its duke@435: bool MatchRule::base_operand(uint &position0, FormDict &globals, duke@435: const char *&result, const char * &name, duke@435: const char * &opType)const{ duke@435: uint position = position0; duke@435: duke@435: return (MatchNode::base_operand( position, globals, result, name, opType)); duke@435: } duke@435: duke@435: duke@435: bool MatchRule::is_base_register(FormDict &globals) const { duke@435: uint position = 1; duke@435: const char *result = NULL; duke@435: const char *name = NULL; duke@435: const char *opType = NULL; duke@435: if (!base_operand(position, globals, result, name, opType)) { duke@435: position = 0; duke@435: if( base_operand(position, globals, result, name, opType) && duke@435: (strcmp(opType,"RegI")==0 || duke@435: strcmp(opType,"RegP")==0 || coleenp@548: strcmp(opType,"RegN")==0 || duke@435: strcmp(opType,"RegL")==0 || duke@435: strcmp(opType,"RegF")==0 || duke@435: strcmp(opType,"RegD")==0 || duke@435: strcmp(opType,"Reg" )==0) ) { duke@435: return 1; duke@435: } duke@435: } duke@435: return 0; duke@435: } duke@435: duke@435: Form::DataType MatchRule::is_base_constant(FormDict &globals) const { duke@435: uint position = 1; duke@435: const char *result = NULL; duke@435: const char *name = NULL; duke@435: const char *opType = NULL; duke@435: if (!base_operand(position, globals, result, name, opType)) { duke@435: position = 0; duke@435: if (base_operand(position, globals, result, name, opType)) { duke@435: return ideal_to_const_type(opType); duke@435: } duke@435: } duke@435: return Form::none; duke@435: } duke@435: duke@435: bool MatchRule::is_chain_rule(FormDict &globals) const { duke@435: duke@435: // Check for chain rule, and do not generate a match list for it duke@435: if ((_lChild == NULL) && (_rChild == NULL) ) { duke@435: const Form *form = globals[_opType]; duke@435: // If this is ideal, then it is a base match, not a chain rule. duke@435: if ( form && form->is_operand() && (!form->ideal_only())) { duke@435: return true; duke@435: } duke@435: } duke@435: // Check for "Set" form of chain rule, and do not generate a match list duke@435: if (_rChild) { duke@435: const char *rch = _rChild->_opType; duke@435: const Form *form = globals[rch]; duke@435: if ((!strcmp(_opType,"Set") && duke@435: ((form) && form->is_operand()))) { duke@435: return true; duke@435: } duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: int MatchRule::is_ideal_copy() const { duke@435: if( _rChild ) { duke@435: const char *opType = _rChild->_opType; duke@435: if( strcmp(opType,"CastII")==0 ) duke@435: return 1; duke@435: // Do not treat *CastPP this way, because it duke@435: // may transfer a raw pointer to an oop. duke@435: // If the register allocator were to coalesce this duke@435: // into a single LRG, the GC maps would be incorrect. duke@435: //if( strcmp(opType,"CastPP")==0 ) duke@435: // return 1; duke@435: //if( strcmp(opType,"CheckCastPP")==0 ) duke@435: // return 1; duke@435: // duke@435: // Do not treat CastX2P or CastP2X this way, because duke@435: // raw pointers and int types are treated differently duke@435: // when saving local & stack info for safepoints in duke@435: // Output(). duke@435: //if( strcmp(opType,"CastX2P")==0 ) duke@435: // return 1; duke@435: //if( strcmp(opType,"CastP2X")==0 ) duke@435: // return 1; duke@435: } duke@435: if( is_chain_rule(_AD.globalNames()) && duke@435: _lChild && strncmp(_lChild->_opType,"stackSlot",9)==0 ) duke@435: return 1; duke@435: return 0; duke@435: } duke@435: duke@435: duke@435: int MatchRule::is_expensive() const { duke@435: if( _rChild ) { duke@435: const char *opType = _rChild->_opType; duke@435: if( strcmp(opType,"AtanD")==0 || duke@435: strcmp(opType,"CosD")==0 || duke@435: strcmp(opType,"DivD")==0 || duke@435: strcmp(opType,"DivF")==0 || duke@435: strcmp(opType,"DivI")==0 || duke@435: strcmp(opType,"ExpD")==0 || duke@435: strcmp(opType,"LogD")==0 || duke@435: strcmp(opType,"Log10D")==0 || duke@435: strcmp(opType,"ModD")==0 || duke@435: strcmp(opType,"ModF")==0 || duke@435: strcmp(opType,"ModI")==0 || duke@435: strcmp(opType,"PowD")==0 || duke@435: strcmp(opType,"SinD")==0 || duke@435: strcmp(opType,"SqrtD")==0 || duke@435: strcmp(opType,"TanD")==0 || duke@435: strcmp(opType,"ConvD2F")==0 || duke@435: strcmp(opType,"ConvD2I")==0 || duke@435: strcmp(opType,"ConvD2L")==0 || duke@435: strcmp(opType,"ConvF2D")==0 || duke@435: strcmp(opType,"ConvF2I")==0 || duke@435: strcmp(opType,"ConvF2L")==0 || duke@435: strcmp(opType,"ConvI2D")==0 || duke@435: strcmp(opType,"ConvI2F")==0 || duke@435: strcmp(opType,"ConvI2L")==0 || duke@435: strcmp(opType,"ConvL2D")==0 || duke@435: strcmp(opType,"ConvL2F")==0 || duke@435: strcmp(opType,"ConvL2I")==0 || duke@435: strcmp(opType,"RoundDouble")==0 || duke@435: strcmp(opType,"RoundFloat")==0 || duke@435: strcmp(opType,"ReverseBytesI")==0 || duke@435: strcmp(opType,"ReverseBytesL")==0 || duke@435: strcmp(opType,"Replicate16B")==0 || duke@435: strcmp(opType,"Replicate8B")==0 || duke@435: strcmp(opType,"Replicate4B")==0 || duke@435: strcmp(opType,"Replicate8C")==0 || duke@435: strcmp(opType,"Replicate4C")==0 || duke@435: strcmp(opType,"Replicate8S")==0 || duke@435: strcmp(opType,"Replicate4S")==0 || duke@435: strcmp(opType,"Replicate4I")==0 || duke@435: strcmp(opType,"Replicate2I")==0 || duke@435: strcmp(opType,"Replicate2L")==0 || duke@435: strcmp(opType,"Replicate4F")==0 || duke@435: strcmp(opType,"Replicate2F")==0 || duke@435: strcmp(opType,"Replicate2D")==0 || duke@435: 0 /* 0 to line up columns nicely */ ) duke@435: return 1; duke@435: } duke@435: return 0; duke@435: } duke@435: duke@435: bool MatchRule::is_ideal_unlock() const { duke@435: if( !_opType ) return false; duke@435: return !strcmp(_opType,"Unlock") || !strcmp(_opType,"FastUnlock"); duke@435: } duke@435: duke@435: duke@435: bool MatchRule::is_ideal_call_leaf() const { duke@435: if( !_opType ) return false; duke@435: return !strcmp(_opType,"CallLeaf") || duke@435: !strcmp(_opType,"CallLeafNoFP"); duke@435: } duke@435: duke@435: duke@435: bool MatchRule::is_ideal_if() const { duke@435: if( !_opType ) return false; duke@435: return duke@435: !strcmp(_opType,"If" ) || duke@435: !strcmp(_opType,"CountedLoopEnd"); duke@435: } duke@435: duke@435: bool MatchRule::is_ideal_fastlock() const { duke@435: if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) { duke@435: return (strcmp(_rChild->_opType,"FastLock") == 0); duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: bool MatchRule::is_ideal_membar() const { duke@435: if( !_opType ) return false; duke@435: return duke@435: !strcmp(_opType,"MemBarAcquire" ) || duke@435: !strcmp(_opType,"MemBarRelease" ) || duke@435: !strcmp(_opType,"MemBarVolatile" ) || duke@435: !strcmp(_opType,"MemBarCPUOrder" ) ; duke@435: } duke@435: duke@435: bool MatchRule::is_ideal_loadPC() const { duke@435: if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) { duke@435: return (strcmp(_rChild->_opType,"LoadPC") == 0); duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: bool MatchRule::is_ideal_box() const { duke@435: if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) { duke@435: return (strcmp(_rChild->_opType,"Box") == 0); duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: bool MatchRule::is_ideal_goto() const { duke@435: bool ideal_goto = false; duke@435: duke@435: if( _opType && (strcmp(_opType,"Goto") == 0) ) { duke@435: ideal_goto = true; duke@435: } duke@435: return ideal_goto; duke@435: } duke@435: duke@435: bool MatchRule::is_ideal_jump() const { duke@435: if( _opType ) { duke@435: if( !strcmp(_opType,"Jump") ) duke@435: return true; duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: bool MatchRule::is_ideal_bool() const { duke@435: if( _opType ) { duke@435: if( !strcmp(_opType,"Bool") ) duke@435: return true; duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: duke@435: Form::DataType MatchRule::is_ideal_load() const { duke@435: Form::DataType ideal_load = Form::none; duke@435: duke@435: if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) { duke@435: const char *opType = _rChild->_opType; duke@435: ideal_load = is_load_from_memory(opType); duke@435: } duke@435: duke@435: return ideal_load; duke@435: } duke@435: duke@435: duke@435: Form::DataType MatchRule::is_ideal_store() const { duke@435: Form::DataType ideal_store = Form::none; duke@435: duke@435: if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) { duke@435: const char *opType = _rChild->_opType; duke@435: ideal_store = is_store_to_memory(opType); duke@435: } duke@435: duke@435: return ideal_store; duke@435: } duke@435: duke@435: duke@435: void MatchRule::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: void MatchRule::output(FILE *fp) { duke@435: fprintf(fp,"MatchRule: ( %s",_name); duke@435: if (_lChild) _lChild->output(fp); duke@435: if (_rChild) _rChild->output(fp); duke@435: fprintf(fp," )\n"); duke@435: fprintf(fp," nesting depth = %d\n", _depth); duke@435: if (_result) fprintf(fp," Result Type = %s", _result); duke@435: fprintf(fp,"\n"); duke@435: } duke@435: duke@435: //------------------------------Attribute-------------------------------------- duke@435: Attribute::Attribute(char *id, char* val, int type) duke@435: : _ident(id), _val(val), _atype(type) { duke@435: } duke@435: Attribute::~Attribute() { duke@435: } duke@435: duke@435: int Attribute::int_val(ArchDesc &ad) { duke@435: // Make sure it is an integer constant: duke@435: int result = 0; duke@435: if (!_val || !ADLParser::is_int_token(_val, result)) { duke@435: ad.syntax_err(0, "Attribute %s must have an integer value: %s", duke@435: _ident, _val ? _val : ""); duke@435: } duke@435: return result; duke@435: } duke@435: duke@435: void Attribute::dump() { duke@435: output(stderr); duke@435: } // Debug printer duke@435: duke@435: // Write to output files duke@435: void Attribute::output(FILE *fp) { duke@435: fprintf(fp,"Attribute: %s %s\n", (_ident?_ident:""), (_val?_val:"")); duke@435: } duke@435: duke@435: //------------------------------FormatRule---------------------------------- duke@435: FormatRule::FormatRule(char *temp) duke@435: : _temp(temp) { duke@435: } duke@435: FormatRule::~FormatRule() { duke@435: } duke@435: duke@435: void FormatRule::dump() { duke@435: output(stderr); duke@435: } duke@435: duke@435: // Write to output files duke@435: void FormatRule::output(FILE *fp) { duke@435: fprintf(fp,"\nFormat Rule: \n%s", (_temp?_temp:"")); duke@435: fprintf(fp,"\n"); duke@435: }