0N/A/*
1879N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
0N/A// DFA.CPP - Method definitions for outputting the matcher DFA from ADLC
0N/A#include "adlc.hpp"
0N/A
0N/A//---------------------------Switches for debugging output---------------------
0N/Astatic bool debug_output = false;
0N/Astatic bool debug_output1 = false; // top level chain rules
0N/A
0N/A//---------------------------Access to internals of class State----------------
0N/Astatic const char *sLeft = "_kids[0]";
0N/Astatic const char *sRight = "_kids[1]";
0N/A
0N/A//---------------------------DFA productions-----------------------------------
0N/Astatic const char *dfa_production = "DFA_PRODUCTION";
0N/Astatic const char *dfa_production_set_valid = "DFA_PRODUCTION__SET_VALID";
0N/A
0N/A//---------------------------Production State----------------------------------
0N/Astatic const char *knownInvalid = "knownInvalid"; // The result does NOT have a rule defined
0N/Astatic const char *knownValid = "knownValid"; // The result must be produced by a rule
0N/Astatic const char *unknownValid = "unknownValid"; // Unknown (probably due to a child or predicate constraint)
0N/A
0N/Astatic const char *noConstraint = "noConstraint"; // No constraints seen so far
0N/Astatic const char *hasConstraint = "hasConstraint"; // Within the first constraint
0N/A
0N/A
0N/A//------------------------------Production------------------------------------
0N/A// Track the status of productions for a particular result
0N/Aclass Production {
0N/Apublic:
0N/A const char *_result;
0N/A const char *_constraint;
0N/A const char *_valid;
0N/A Expr *_cost_lb; // Cost lower bound for this production
0N/A Expr *_cost_ub; // Cost upper bound for this production
0N/A
0N/Apublic:
0N/A Production(const char *result, const char *constraint, const char *valid);
0N/A ~Production() {};
0N/A
0N/A void initialize(); // reset to be an empty container
0N/A
0N/A const char *valid() const { return _valid; }
0N/A Expr *cost_lb() const { return (Expr *)_cost_lb; }
0N/A Expr *cost_ub() const { return (Expr *)_cost_ub; }
0N/A
0N/A void print();
0N/A};
0N/A
0N/A
0N/A//------------------------------ProductionState--------------------------------
0N/A// Track the status of all production rule results
0N/A// Reset for each root opcode (e.g., Op_RegI, Op_AddI, ...)
0N/Aclass ProductionState {
0N/Aprivate:
0N/A Dict _production; // map result of production, char*, to information or NULL
0N/A const char *_constraint;
0N/A
0N/Apublic:
0N/A // cmpstr does string comparisions. hashstr computes a key.
0N/A ProductionState(Arena *arena) : _production(cmpstr, hashstr, arena) { initialize(); };
0N/A ~ProductionState() { };
0N/A
0N/A void initialize(); // reset local and dictionary state
0N/A
0N/A const char *constraint();
0N/A void set_constraint(const char *constraint); // currently working inside of constraints
0N/A
0N/A const char *valid(const char *result); // unknownValid, or status for this production
0N/A void set_valid(const char *result); // if not constrained, set status to knownValid
0N/A
0N/A Expr *cost_lb(const char *result);
0N/A Expr *cost_ub(const char *result);
0N/A void set_cost_bounds(const char *result, const Expr *cost, bool has_state_check, bool has_cost_check);
0N/A
0N/A // Return the Production associated with the result,
0N/A // or create a new Production and insert it into the dictionary.
0N/A Production *getProduction(const char *result);
0N/A
0N/A void print();
0N/A
0N/Aprivate:
0N/A // Disable public use of constructor, copy-ctor, ...
0N/A ProductionState( ) : _production(cmpstr, hashstr, Form::arena) { assert( false, "NotImplemented"); };
0N/A ProductionState( const ProductionState & ) : _production(cmpstr, hashstr, Form::arena) { assert( false, "NotImplemented"); }; // Deep-copy
0N/A};
0N/A
0N/A
0N/A//---------------------------Helper Functions----------------------------------
0N/A// cost_check template:
0N/A// 1) if (STATE__NOT_YET_VALID(EBXREGI) || _cost[EBXREGI] > c) {
0N/A// 2) DFA_PRODUCTION__SET_VALID(EBXREGI, cmovI_memu_rule, c)
0N/A// 3) }
0N/A//
0N/Astatic void cost_check(FILE *fp, const char *spaces,
0N/A const char *arrayIdx, const Expr *cost, const char *rule, ProductionState &status) {
0N/A bool state_check = false; // true if this production needs to check validity
0N/A bool cost_check = false; // true if this production needs to check cost
0N/A bool cost_is_above_upper_bound = false; // true if this production is unnecessary due to high cost
0N/A bool cost_is_below_lower_bound = false; // true if this production replaces a higher cost production
0N/A
0N/A // Get information about this production
0N/A const Expr *previous_ub = status.cost_ub(arrayIdx);
0N/A if( !previous_ub->is_unknown() ) {
0N/A if( previous_ub->less_than_or_equal(cost) ) {
0N/A cost_is_above_upper_bound = true;
0N/A if( debug_output ) { fprintf(fp, "// Previous rule with lower cost than: %s === %s_rule costs %s\n", arrayIdx, rule, cost->as_string()); }
0N/A }
0N/A }
0N/A
0N/A const Expr *previous_lb = status.cost_lb(arrayIdx);
0N/A if( !previous_lb->is_unknown() ) {
0N/A if( cost->less_than_or_equal(previous_lb) ) {
0N/A cost_is_below_lower_bound = true;
0N/A if( debug_output ) { fprintf(fp, "// Previous rule with higher cost\n"); }
0N/A }
0N/A }
0N/A
0N/A // line 1)
0N/A // Check for validity and compare to other match costs
0N/A const char *validity_check = status.valid(arrayIdx);
0N/A if( validity_check == unknownValid ) {
0N/A fprintf(fp, "%sif (STATE__NOT_YET_VALID(%s) || _cost[%s] > %s) {\n", spaces, arrayIdx, arrayIdx, cost->as_string());
0N/A state_check = true;
0N/A cost_check = true;
0N/A }
0N/A else if( validity_check == knownInvalid ) {
0N/A if( debug_output ) { fprintf(fp, "%s// %s KNOWN_INVALID \n", spaces, arrayIdx); }
0N/A }
0N/A else if( validity_check == knownValid ) {
0N/A if( cost_is_above_upper_bound ) {
0N/A // production cost is known to be too high.
0N/A return;
0N/A } else if( cost_is_below_lower_bound ) {
0N/A // production will unconditionally overwrite a previous production that had higher cost
0N/A } else {
0N/A fprintf(fp, "%sif ( /* %s KNOWN_VALID || */ _cost[%s] > %s) {\n", spaces, arrayIdx, arrayIdx, cost->as_string());
0N/A cost_check = true;
0N/A }
0N/A }
0N/A
0N/A // line 2)
0N/A // no need to set State vector if our state is knownValid
0N/A const char *production = (validity_check == knownValid) ? dfa_production : dfa_production_set_valid;
0N/A fprintf(fp, "%s %s(%s, %s_rule, %s)", spaces, production, arrayIdx, rule, cost->as_string() );
0N/A if( validity_check == knownValid ) {
0N/A if( cost_is_below_lower_bound ) { fprintf(fp, "\t // overwrites higher cost rule"); }
0N/A }
0N/A fprintf(fp, "\n");
0N/A
0N/A // line 3)
0N/A if( cost_check || state_check ) {
0N/A fprintf(fp, "%s}\n", spaces);
0N/A }
0N/A
0N/A status.set_cost_bounds(arrayIdx, cost, state_check, cost_check);
0N/A
0N/A // Update ProductionState
0N/A if( validity_check != knownValid ) {
0N/A // set State vector if not previously known
0N/A status.set_valid(arrayIdx);
0N/A }
0N/A}
0N/A
0N/A
0N/A//---------------------------child_test----------------------------------------
0N/A// Example:
0N/A// STATE__VALID_CHILD(_kids[0], FOO) && STATE__VALID_CHILD(_kids[1], BAR)
0N/A// Macro equivalent to: _kids[0]->valid(FOO) && _kids[1]->valid(BAR)
0N/A//
0N/Astatic void child_test(FILE *fp, MatchList &mList) {
4445N/A if (mList._lchild) { // If left child, check it
4445N/A const char* lchild_to_upper = ArchDesc::getMachOperEnum(mList._lchild);
4445N/A fprintf(fp, "STATE__VALID_CHILD(_kids[0], %s)", lchild_to_upper);
4445N/A delete[] lchild_to_upper;
4445N/A }
4445N/A if (mList._lchild && mList._rchild) { // If both, add the "&&"
4445N/A fprintf(fp, " && ");
4445N/A }
4445N/A if (mList._rchild) { // If right child, check it
4445N/A const char* rchild_to_upper = ArchDesc::getMachOperEnum(mList._rchild);
4445N/A fprintf(fp, "STATE__VALID_CHILD(_kids[1], %s)", rchild_to_upper);
4445N/A delete[] rchild_to_upper;
4445N/A }
0N/A}
0N/A
0N/A//---------------------------calc_cost-----------------------------------------
0N/A// Example:
0N/A// unsigned int c = _kids[0]->_cost[FOO] + _kids[1]->_cost[BAR] + 5;
0N/A//
0N/AExpr *ArchDesc::calc_cost(FILE *fp, const char *spaces, MatchList &mList, ProductionState &status) {
0N/A fprintf(fp, "%sunsigned int c = ", spaces);
0N/A Expr *c = new Expr("0");
4445N/A if (mList._lchild) { // If left child, add it in
4445N/A const char* lchild_to_upper = ArchDesc::getMachOperEnum(mList._lchild);
4445N/A sprintf(Expr::buffer(), "_kids[0]->_cost[%s]", lchild_to_upper);
0N/A c->add(Expr::buffer());
4445N/A delete[] lchild_to_upper;
0N/A}
4445N/A if (mList._rchild) { // If right child, add it in
4445N/A const char* rchild_to_upper = ArchDesc::getMachOperEnum(mList._rchild);
4445N/A sprintf(Expr::buffer(), "_kids[1]->_cost[%s]", rchild_to_upper);
0N/A c->add(Expr::buffer());
4445N/A delete[] rchild_to_upper;
0N/A }
0N/A // Add in cost of this rule
0N/A const char *mList_cost = mList.get_cost();
0N/A c->add(mList_cost, *this);
0N/A
0N/A fprintf(fp, "%s;\n", c->as_string());
0N/A c->set_external_name("c");
0N/A return c;
0N/A}
0N/A
0N/A
0N/A//---------------------------gen_match-----------------------------------------
0N/Avoid ArchDesc::gen_match(FILE *fp, MatchList &mList, ProductionState &status, Dict &operands_chained_from) {
0N/A const char *spaces4 = " ";
0N/A const char *spaces6 = " ";
0N/A
0N/A fprintf(fp, "%s", spaces4);
0N/A // Only generate child tests if this is not a leaf node
0N/A bool has_child_constraints = mList._lchild || mList._rchild;
4445N/A const char *predicate_test = mList.get_pred();
4445N/A if (has_child_constraints || predicate_test) {
0N/A // Open the child-and-predicate-test braces
0N/A fprintf(fp, "if( ");
0N/A status.set_constraint(hasConstraint);
0N/A child_test(fp, mList);
0N/A // Only generate predicate test if one exists for this match
4445N/A if (predicate_test) {
4445N/A if (has_child_constraints) {
4445N/A fprintf(fp," &&\n");
4445N/A }
0N/A fprintf(fp, "%s %s", spaces6, predicate_test);
0N/A }
0N/A // End of outer tests
0N/A fprintf(fp," ) ");
0N/A } else {
0N/A // No child or predicate test needed
0N/A status.set_constraint(noConstraint);
0N/A }
0N/A
0N/A // End of outer tests
0N/A fprintf(fp,"{\n");
0N/A
0N/A // Calculate cost of this match
0N/A const Expr *cost = calc_cost(fp, spaces6, mList, status);
0N/A // Check against other match costs, and update cost & rule vectors
0N/A cost_check(fp, spaces6, ArchDesc::getMachOperEnum(mList._resultStr), cost, mList._opcode, status);
0N/A
0N/A // If this is a member of an operand class, update the class cost & rule
0N/A expand_opclass( fp, spaces6, cost, mList._resultStr, status);
0N/A
0N/A // Check if this rule should be used to generate the chains as well.
0N/A const char *rule = /* set rule to "Invalid" for internal operands */
0N/A strcmp(mList._opcode,mList._resultStr) ? mList._opcode : "Invalid";
0N/A
0N/A // If this rule produces an operand which has associated chain rules,
0N/A // update the operands with the chain rule + this rule cost & this rule.
0N/A chain_rule(fp, spaces6, mList._resultStr, cost, rule, operands_chained_from, status);
0N/A
0N/A // Close the child-and-predicate-test braces
0N/A fprintf(fp, " }\n");
0N/A
0N/A}
0N/A
0N/A
0N/A//---------------------------expand_opclass------------------------------------
0N/A// Chain from one result_type to all other members of its operand class
0N/Avoid ArchDesc::expand_opclass(FILE *fp, const char *indent, const Expr *cost,
0N/A const char *result_type, ProductionState &status) {
0N/A const Form *form = _globalNames[result_type];
0N/A OperandForm *op = form ? form->is_operand() : NULL;
0N/A if( op && op->_classes.count() > 0 ) {
0N/A if( debug_output ) { fprintf(fp, "// expand operand classes for operand: %s \n", (char *)op->_ident ); } // %%%%% Explanation
0N/A // Iterate through all operand classes which include this operand
0N/A op->_classes.reset();
0N/A const char *oclass;
0N/A // Expr *cCost = new Expr(cost);
0N/A while( (oclass = op->_classes.iter()) != NULL )
0N/A // Check against other match costs, and update cost & rule vectors
0N/A cost_check(fp, indent, ArchDesc::getMachOperEnum(oclass), cost, result_type, status);
0N/A }
0N/A}
0N/A
0N/A//---------------------------chain_rule----------------------------------------
0N/A// Starting at 'operand', check if we know how to automatically generate other results
0N/Avoid ArchDesc::chain_rule(FILE *fp, const char *indent, const char *operand,
0N/A const Expr *icost, const char *irule, Dict &operands_chained_from, ProductionState &status) {
0N/A
0N/A // Check if we have already generated chains from this starting point
0N/A if( operands_chained_from[operand] != NULL ) {
0N/A return;
0N/A } else {
0N/A operands_chained_from.Insert( operand, operand);
0N/A }
0N/A if( debug_output ) { fprintf(fp, "// chain rules starting from: %s and %s \n", (char *)operand, (char *)irule); } // %%%%% Explanation
0N/A
0N/A ChainList *lst = (ChainList *)_chainRules[operand];
0N/A if (lst) {
0N/A // printf("\nChain from <%s> at cost #%s\n",operand, icost ? icost : "_");
0N/A const char *result, *cost, *rule;
0N/A for(lst->reset(); (lst->iter(result,cost,rule)) == true; ) {
0N/A // Do not generate operands that are already available
0N/A if( operands_chained_from[result] != NULL ) {
0N/A continue;
0N/A } else {
0N/A // Compute the cost for previous match + chain_rule_cost
0N/A // total_cost = icost + cost;
0N/A Expr *total_cost = icost->clone(); // icost + cost
0N/A total_cost->add(cost, *this);
0N/A
0N/A // Check for transitive chain rules
0N/A Form *form = (Form *)_globalNames[rule];
0N/A if ( ! form->is_instruction()) {
0N/A // printf(" result=%s cost=%s rule=%s\n", result, total_cost, rule);
0N/A // Check against other match costs, and update cost & rule vectors
0N/A const char *reduce_rule = strcmp(irule,"Invalid") ? irule : rule;
0N/A cost_check(fp, indent, ArchDesc::getMachOperEnum(result), total_cost, reduce_rule, status);
0N/A chain_rule(fp, indent, result, total_cost, irule, operands_chained_from, status);
0N/A } else {
0N/A // printf(" result=%s cost=%s rule=%s\n", result, total_cost, rule);
0N/A // Check against other match costs, and update cost & rule vectors
0N/A cost_check(fp, indent, ArchDesc::getMachOperEnum(result), total_cost, rule, status);
0N/A chain_rule(fp, indent, result, total_cost, rule, operands_chained_from, status);
0N/A }
0N/A
0N/A // If this is a member of an operand class, update class cost & rule
0N/A expand_opclass( fp, indent, total_cost, result, status );
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A//---------------------------prune_matchlist-----------------------------------
0N/A// Check for duplicate entries in a matchlist, and prune out the higher cost
0N/A// entry.
0N/Avoid ArchDesc::prune_matchlist(Dict &minimize, MatchList &mlist) {
0N/A
0N/A}
0N/A
0N/A//---------------------------buildDFA------------------------------------------
0N/A// DFA is a large switch with case statements for each ideal opcode encountered
0N/A// in any match rule in the ad file. Each case has a series of if's to handle
0N/A// the match or fail decisions. The matches test the cost function of that
0N/A// rule, and prune any cases which are higher cost for the same reduction.
0N/A// In order to generate the DFA we walk the table of ideal opcode/MatchList
0N/A// pairs generated by the ADLC front end to build the contents of the case
0N/A// statements (a series of if statements).
0N/Avoid ArchDesc::buildDFA(FILE* fp) {
0N/A int i;
0N/A // Remember operands that are the starting points for chain rules.
0N/A // Prevent cycles by checking if we have already generated chain.
0N/A Dict operands_chained_from(cmpstr, hashstr, Form::arena);
0N/A
0N/A // Hash inputs to match rules so that final DFA contains only one entry for
0N/A // each match pattern which is the low cost entry.
0N/A Dict minimize(cmpstr, hashstr, Form::arena);
0N/A
0N/A // Track status of dfa for each resulting production
0N/A // reset for each ideal root.
0N/A ProductionState status(Form::arena);
0N/A
0N/A // Output the start of the DFA method into the output file
0N/A
0N/A fprintf(fp, "\n");
0N/A fprintf(fp, "//------------------------- Source -----------------------------------------\n");
0N/A // Do not put random source code into the DFA.
0N/A // If there are constants which need sharing, put them in "source_hpp" forms.
0N/A // _source.output(fp);
0N/A fprintf(fp, "\n");
0N/A fprintf(fp, "//------------------------- Attributes -------------------------------------\n");
0N/A _attributes.output(fp);
0N/A fprintf(fp, "\n");
0N/A fprintf(fp, "//------------------------- Macros -----------------------------------------\n");
0N/A // #define DFA_PRODUCTION(result, rule, cost)\
0N/A // _cost[ (result) ] = cost; _rule[ (result) ] = rule;
0N/A fprintf(fp, "#define %s(result, rule, cost)\\\n", dfa_production);
0N/A fprintf(fp, " _cost[ (result) ] = cost; _rule[ (result) ] = rule;\n");
0N/A fprintf(fp, "\n");
0N/A
0N/A // #define DFA_PRODUCTION__SET_VALID(result, rule, cost)\
0N/A // DFA_PRODUCTION( (result), (rule), (cost) ); STATE__SET_VALID( (result) );
0N/A fprintf(fp, "#define %s(result, rule, cost)\\\n", dfa_production_set_valid);
0N/A fprintf(fp, " %s( (result), (rule), (cost) ); STATE__SET_VALID( (result) );\n", dfa_production);
0N/A fprintf(fp, "\n");
0N/A
0N/A fprintf(fp, "//------------------------- DFA --------------------------------------------\n");
0N/A
0N/A fprintf(fp,
0N/A"// DFA is a large switch with case statements for each ideal opcode encountered\n"
0N/A"// in any match rule in the ad file. Each case has a series of if's to handle\n"
0N/A"// the match or fail decisions. The matches test the cost function of that\n"
0N/A"// rule, and prune any cases which are higher cost for the same reduction.\n"
0N/A"// In order to generate the DFA we walk the table of ideal opcode/MatchList\n"
0N/A"// pairs generated by the ADLC front end to build the contents of the case\n"
0N/A"// statements (a series of if statements).\n"
0N/A);
0N/A fprintf(fp, "\n");
0N/A fprintf(fp, "\n");
0N/A if (_dfa_small) {
0N/A // Now build the individual routines just like the switch entries in large version
0N/A // Iterate over the table of MatchLists, start at first valid opcode of 1
0N/A for (i = 1; i < _last_opcode; i++) {
0N/A if (_mlistab[i] == NULL) continue;
0N/A // Generate the routine header statement for this opcode
0N/A fprintf(fp, "void State::_sub_Op_%s(const Node *n){\n", NodeClassNames[i]);
0N/A // Generate body. Shared for both inline and out-of-line version
0N/A gen_dfa_state_body(fp, minimize, status, operands_chained_from, i);
0N/A // End of routine
0N/A fprintf(fp, "}\n");
0N/A }
0N/A }
0N/A fprintf(fp, "bool State::DFA");
0N/A fprintf(fp, "(int opcode, const Node *n) {\n");
0N/A fprintf(fp, " switch(opcode) {\n");
0N/A
0N/A // Iterate over the table of MatchLists, start at first valid opcode of 1
0N/A for (i = 1; i < _last_opcode; i++) {
0N/A if (_mlistab[i] == NULL) continue;
0N/A // Generate the case statement for this opcode
0N/A if (_dfa_small) {
0N/A fprintf(fp, " case Op_%s: { _sub_Op_%s(n);\n", NodeClassNames[i], NodeClassNames[i]);
0N/A } else {
0N/A fprintf(fp, " case Op_%s: {\n", NodeClassNames[i]);
0N/A // Walk the list, compacting it
0N/A gen_dfa_state_body(fp, minimize, status, operands_chained_from, i);
0N/A }
0N/A // Print the "break"
0N/A fprintf(fp, " break;\n");
0N/A fprintf(fp, " }\n");
0N/A }
0N/A
0N/A // Generate the default case for switch(opcode)
0N/A fprintf(fp, " \n");
0N/A fprintf(fp, " default:\n");
0N/A fprintf(fp, " tty->print(\"Default case invoked for: \\n\");\n");
0N/A fprintf(fp, " tty->print(\" opcode = %cd, \\\"%cs\\\"\\n\", opcode, NodeClassNames[opcode]);\n", '%', '%');
0N/A fprintf(fp, " return false;\n");
0N/A fprintf(fp, " }\n");
0N/A
0N/A // Return status, indicating a successful match.
0N/A fprintf(fp, " return true;\n");
0N/A // Generate the closing brace for method Matcher::DFA
0N/A fprintf(fp, "}\n");
0N/A Expr::check_buffers();
0N/A}
0N/A
0N/A
0N/Aclass dfa_shared_preds {
475N/A enum { count = 4 };
0N/A
0N/A static bool _found[count];
0N/A static const char* _type [count];
0N/A static const char* _var [count];
0N/A static const char* _pred [count];
0N/A
0N/A static void check_index(int index) { assert( 0 <= index && index < count, "Invalid index"); }
0N/A
0N/A // Confirm that this is a separate sub-expression.
0N/A // Only need to catch common cases like " ... && shared ..."
0N/A // and avoid hazardous ones like "...->shared"
0N/A static bool valid_loc(char *pred, char *shared) {
0N/A // start of predicate is valid
0N/A if( shared == pred ) return true;
0N/A
0N/A // Check previous character and recurse if needed
0N/A char *prev = shared - 1;
0N/A char c = *prev;
0N/A switch( c ) {
0N/A case ' ':
475N/A case '\n':
0N/A return dfa_shared_preds::valid_loc(pred, prev);
0N/A case '!':
0N/A case '(':
0N/A case '<':
0N/A case '=':
0N/A return true;
475N/A case '"': // such as: #line 10 "myfile.ad"\n mypredicate
475N/A return true;
0N/A case '|':
0N/A if( prev != pred && *(prev-1) == '|' ) return true;
0N/A case '&':
0N/A if( prev != pred && *(prev-1) == '&' ) return true;
0N/A default:
0N/A return false;
0N/A }
0N/A
0N/A return false;
0N/A }
0N/A
0N/Apublic:
0N/A
0N/A static bool found(int index){ check_index(index); return _found[index]; }
0N/A static void set_found(int index, bool val) { check_index(index); _found[index] = val; }
0N/A static void reset_found() {
0N/A for( int i = 0; i < count; ++i ) { _found[i] = false; }
0N/A };
0N/A
0N/A static const char* type(int index) { check_index(index); return _type[index]; }
0N/A static const char* var (int index) { check_index(index); return _var [index]; }
0N/A static const char* pred(int index) { check_index(index); return _pred[index]; }
0N/A
0N/A // Check each predicate in the MatchList for common sub-expressions
0N/A static void cse_matchlist(MatchList *matchList) {
0N/A for( MatchList *mList = matchList; mList != NULL; mList = mList->get_next() ) {
0N/A Predicate* predicate = mList->get_pred_obj();
0N/A char* pred = mList->get_pred();
0N/A if( pred != NULL ) {
0N/A for(int index = 0; index < count; ++index ) {
0N/A const char *shared_pred = dfa_shared_preds::pred(index);
0N/A const char *shared_pred_var = dfa_shared_preds::var(index);
0N/A bool result = dfa_shared_preds::cse_predicate(predicate, shared_pred, shared_pred_var);
0N/A if( result ) dfa_shared_preds::set_found(index, true);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A // If the Predicate contains a common sub-expression, replace the Predicate's
0N/A // string with one that uses the variable name.
0N/A static bool cse_predicate(Predicate* predicate, const char *shared_pred, const char *shared_pred_var) {
0N/A bool result = false;
0N/A char *pred = predicate->_pred;
0N/A if( pred != NULL ) {
0N/A char *new_pred = pred;
0N/A for( char *shared_pred_loc = strstr(new_pred, shared_pred);
0N/A shared_pred_loc != NULL && dfa_shared_preds::valid_loc(new_pred,shared_pred_loc);
0N/A shared_pred_loc = strstr(new_pred, shared_pred) ) {
0N/A // Do not modify the original predicate string, it is shared
0N/A if( new_pred == pred ) {
0N/A new_pred = strdup(pred);
0N/A shared_pred_loc = strstr(new_pred, shared_pred);
0N/A }
0N/A // Replace shared_pred with variable name
0N/A strncpy(shared_pred_loc, shared_pred_var, strlen(shared_pred_var));
0N/A }
0N/A // Install new predicate
0N/A if( new_pred != pred ) {
0N/A predicate->_pred = new_pred;
0N/A result = true;
0N/A }
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A // Output the hoisted common sub-expression if we found it in predicates
0N/A static void generate_cse(FILE *fp) {
0N/A for(int j = 0; j < count; ++j ) {
0N/A if( dfa_shared_preds::found(j) ) {
0N/A const char *shared_pred_type = dfa_shared_preds::type(j);
0N/A const char *shared_pred_var = dfa_shared_preds::var(j);
0N/A const char *shared_pred = dfa_shared_preds::pred(j);
0N/A fprintf(fp, " %s %s = %s;\n", shared_pred_type, shared_pred_var, shared_pred);
0N/A }
0N/A }
0N/A }
0N/A};
0N/A// shared predicates, _var and _pred entry should be the same length
475N/Abool dfa_shared_preds::_found[dfa_shared_preds::count]
475N/A = { false, false, false, false };
475N/Aconst char* dfa_shared_preds::_type[dfa_shared_preds::count]
475N/A = { "int", "jlong", "intptr_t", "bool" };
475N/Aconst char* dfa_shared_preds::_var [dfa_shared_preds::count]
475N/A = { "_n_get_int__", "_n_get_long__", "_n_get_intptr_t__", "Compile__current____select_24_bit_instr__" };
475N/Aconst char* dfa_shared_preds::_pred[dfa_shared_preds::count]
475N/A = { "n->get_int()", "n->get_long()", "n->get_intptr_t()", "Compile::current()->select_24_bit_instr()" };
0N/A
0N/A
0N/Avoid ArchDesc::gen_dfa_state_body(FILE* fp, Dict &minimize, ProductionState &status, Dict &operands_chained_from, int i) {
0N/A // Start the body of each Op_XXX sub-dfa with a clean state.
0N/A status.initialize();
0N/A
0N/A // Walk the list, compacting it
0N/A MatchList* mList = _mlistab[i];
0N/A do {
0N/A // Hash each entry using inputs as key and pointer as data.
0N/A // If there is already an entry, keep the one with lower cost, and
0N/A // remove the other one from the list.
0N/A prune_matchlist(minimize, *mList);
0N/A // Iterate
0N/A mList = mList->get_next();
0N/A } while(mList != NULL);
0N/A
0N/A // Hoist previously specified common sub-expressions out of predicates
0N/A dfa_shared_preds::reset_found();
0N/A dfa_shared_preds::cse_matchlist(_mlistab[i]);
0N/A dfa_shared_preds::generate_cse(fp);
0N/A
0N/A mList = _mlistab[i];
0N/A
0N/A // Walk the list again, generating code
0N/A do {
0N/A // Each match can generate its own chains
0N/A operands_chained_from.Clear();
0N/A gen_match(fp, *mList, status, operands_chained_from);
0N/A mList = mList->get_next();
0N/A } while(mList != NULL);
0N/A // Fill in any chain rules which add instructions
0N/A // These can generate their own chains as well.
0N/A operands_chained_from.Clear(); //
0N/A if( debug_output1 ) { fprintf(fp, "// top level chain rules for: %s \n", (char *)NodeClassNames[i]); } // %%%%% Explanation
0N/A const Expr *zeroCost = new Expr("0");
0N/A chain_rule(fp, " ", (char *)NodeClassNames[i], zeroCost, "Invalid",
0N/A operands_chained_from, status);
0N/A}
0N/A
0N/A
0N/A
0N/A//------------------------------Expr------------------------------------------
0N/AExpr *Expr::_unknown_expr = NULL;
0N/Achar Expr::string_buffer[STRING_BUFFER_LENGTH];
0N/Achar Expr::external_buffer[STRING_BUFFER_LENGTH];
0N/Abool Expr::_init_buffers = Expr::init_buffers();
0N/A
0N/AExpr::Expr() {
0N/A _external_name = NULL;
0N/A _expr = "Invalid_Expr";
0N/A _min_value = Expr::Max;
0N/A _max_value = Expr::Zero;
0N/A}
0N/AExpr::Expr(const char *cost) {
0N/A _external_name = NULL;
0N/A
0N/A int intval = 0;
0N/A if( cost == NULL ) {
0N/A _expr = "0";
0N/A _min_value = Expr::Zero;
0N/A _max_value = Expr::Zero;
0N/A }
0N/A else if( ADLParser::is_int_token(cost, intval) ) {
0N/A _expr = cost;
0N/A _min_value = intval;
0N/A _max_value = intval;
0N/A }
0N/A else {
0N/A assert( strcmp(cost,"0") != 0, "Recognize string zero as an int");
0N/A _expr = cost;
0N/A _min_value = Expr::Zero;
0N/A _max_value = Expr::Max;
0N/A }
0N/A}
0N/A
0N/AExpr::Expr(const char *name, const char *expression, int min_value, int max_value) {
0N/A _external_name = name;
0N/A _expr = expression ? expression : name;
0N/A _min_value = min_value;
0N/A _max_value = max_value;
0N/A assert(_min_value >= 0 && _min_value <= Expr::Max, "value out of range");
0N/A assert(_max_value >= 0 && _max_value <= Expr::Max, "value out of range");
0N/A}
0N/A
0N/AExpr *Expr::clone() const {
0N/A Expr *cost = new Expr();
0N/A cost->_external_name = _external_name;
0N/A cost->_expr = _expr;
0N/A cost->_min_value = _min_value;
0N/A cost->_max_value = _max_value;
0N/A
0N/A return cost;
0N/A}
0N/A
0N/Avoid Expr::add(const Expr *c) {
0N/A // Do not update fields until all computation is complete
0N/A const char *external = compute_external(this, c);
0N/A const char *expr = compute_expr(this, c);
0N/A int min_value = compute_min (this, c);
0N/A int max_value = compute_max (this, c);
0N/A
0N/A _external_name = external;
0N/A _expr = expr;
0N/A _min_value = min_value;
0N/A _max_value = max_value;
0N/A}
0N/A
0N/Avoid Expr::add(const char *c) {
0N/A Expr *cost = new Expr(c);
0N/A add(cost);
0N/A}
0N/A
0N/Avoid Expr::add(const char *c, ArchDesc &AD) {
0N/A const Expr *e = AD.globalDefs()[c];
0N/A if( e != NULL ) {
0N/A // use the value of 'c' defined in <arch>.ad
0N/A add(e);
0N/A } else {
0N/A Expr *cost = new Expr(c);
0N/A add(cost);
0N/A }
0N/A}
0N/A
0N/Aconst char *Expr::compute_external(const Expr *c1, const Expr *c2) {
0N/A const char * result = NULL;
0N/A
0N/A // Preserve use of external name which has a zero value
0N/A if( c1->_external_name != NULL ) {
0N/A sprintf( string_buffer, "%s", c1->as_string());
0N/A if( !c2->is_zero() ) {
0N/A strcat( string_buffer, "+");
0N/A strcat( string_buffer, c2->as_string());
0N/A }
0N/A result = strdup(string_buffer);
0N/A }
0N/A else if( c2->_external_name != NULL ) {
0N/A if( !c1->is_zero() ) {
0N/A sprintf( string_buffer, "%s", c1->as_string());
0N/A strcat( string_buffer, " + ");
0N/A } else {
0N/A string_buffer[0] = '\0';
0N/A }
0N/A strcat( string_buffer, c2->_external_name );
0N/A result = strdup(string_buffer);
0N/A }
0N/A return result;
0N/A}
0N/A
0N/Aconst char *Expr::compute_expr(const Expr *c1, const Expr *c2) {
0N/A if( !c1->is_zero() ) {
0N/A sprintf( string_buffer, "%s", c1->_expr);
0N/A if( !c2->is_zero() ) {
0N/A strcat( string_buffer, "+");
0N/A strcat( string_buffer, c2->_expr);
0N/A }
0N/A }
0N/A else if( !c2->is_zero() ) {
0N/A sprintf( string_buffer, "%s", c2->_expr);
0N/A }
0N/A else {
0N/A sprintf( string_buffer, "0");
0N/A }
0N/A char *cost = strdup(string_buffer);
0N/A
0N/A return cost;
0N/A}
0N/A
0N/Aint Expr::compute_min(const Expr *c1, const Expr *c2) {
0N/A int result = c1->_min_value + c2->_min_value;
0N/A assert( result >= 0, "Invalid cost computation");
0N/A
0N/A return result;
0N/A}
0N/A
0N/Aint Expr::compute_max(const Expr *c1, const Expr *c2) {
0N/A int result = c1->_max_value + c2->_max_value;
0N/A if( result < 0 ) { // check for overflow
0N/A result = Expr::Max;
0N/A }
0N/A
0N/A return result;
0N/A}
0N/A
0N/Avoid Expr::print() const {
0N/A if( _external_name != NULL ) {
0N/A printf(" %s == (%s) === [%d, %d]\n", _external_name, _expr, _min_value, _max_value);
0N/A } else {
0N/A printf(" %s === [%d, %d]\n", _expr, _min_value, _max_value);
0N/A }
0N/A}
0N/A
0N/Avoid Expr::print_define(FILE *fp) const {
0N/A assert( _external_name != NULL, "definition does not have a name");
0N/A assert( _min_value == _max_value, "Expect user definitions to have constant value");
0N/A fprintf(fp, "#define %s (%s) \n", _external_name, _expr);
0N/A fprintf(fp, "// value == %d \n", _min_value);
0N/A}
0N/A
0N/Avoid Expr::print_assert(FILE *fp) const {
0N/A assert( _external_name != NULL, "definition does not have a name");
0N/A assert( _min_value == _max_value, "Expect user definitions to have constant value");
0N/A fprintf(fp, " assert( %s == %d, \"Expect (%s) to equal %d\");\n", _external_name, _min_value, _expr, _min_value);
0N/A}
0N/A
0N/AExpr *Expr::get_unknown() {
0N/A if( Expr::_unknown_expr == NULL ) {
0N/A Expr::_unknown_expr = new Expr();
0N/A }
0N/A
0N/A return Expr::_unknown_expr;
0N/A}
0N/A
0N/Abool Expr::init_buffers() {
0N/A // Fill buffers with 0
0N/A for( int i = 0; i < STRING_BUFFER_LENGTH; ++i ) {
0N/A external_buffer[i] = '\0';
0N/A string_buffer[i] = '\0';
0N/A }
0N/A
0N/A return true;
0N/A}
0N/A
0N/Abool Expr::check_buffers() {
0N/A // returns 'true' if buffer use may have overflowed
0N/A bool ok = true;
0N/A for( int i = STRING_BUFFER_LENGTH - 100; i < STRING_BUFFER_LENGTH; ++i) {
0N/A if( external_buffer[i] != '\0' || string_buffer[i] != '\0' ) {
0N/A ok = false;
0N/A assert( false, "Expr:: Buffer overflow");
0N/A }
0N/A }
0N/A
0N/A return ok;
0N/A}
0N/A
0N/A
0N/A//------------------------------ExprDict---------------------------------------
0N/A// Constructor
0N/AExprDict::ExprDict( CmpKey cmp, Hash hash, Arena *arena )
0N/A : _expr(cmp, hash, arena), _defines() {
0N/A}
0N/AExprDict::~ExprDict() {
0N/A}
0N/A
0N/A// Return # of name-Expr pairs in dict
0N/Aint ExprDict::Size(void) const {
0N/A return _expr.Size();
0N/A}
0N/A
0N/A// define inserts the given key-value pair into the dictionary,
0N/A// and records the name in order for later output, ...
0N/Aconst Expr *ExprDict::define(const char *name, Expr *expr) {
0N/A const Expr *old_expr = (*this)[name];
0N/A assert(old_expr == NULL, "Implementation does not support redefinition");
0N/A
0N/A _expr.Insert(name, expr);
0N/A _defines.addName(name);
0N/A
0N/A return old_expr;
0N/A}
0N/A
0N/A// Insert inserts the given key-value pair into the dictionary. The prior
0N/A// value of the key is returned; NULL if the key was not previously defined.
0N/Aconst Expr *ExprDict::Insert(const char *name, Expr *expr) {
0N/A return (Expr*)_expr.Insert((void*)name, (void*)expr);
0N/A}
0N/A
0N/A// Finds the value of a given key; or NULL if not found.
0N/A// The dictionary is NOT changed.
0N/Aconst Expr *ExprDict::operator [](const char *name) const {
0N/A return (Expr*)_expr[name];
0N/A}
0N/A
0N/Avoid ExprDict::print_defines(FILE *fp) {
0N/A fprintf(fp, "\n");
0N/A const char *name = NULL;
0N/A for( _defines.reset(); (name = _defines.iter()) != NULL; ) {
0N/A const Expr *expr = (const Expr*)_expr[name];
0N/A assert( expr != NULL, "name in ExprDict without matching Expr in dictionary");
0N/A expr->print_define(fp);
0N/A }
0N/A}
0N/Avoid ExprDict::print_asserts(FILE *fp) {
0N/A fprintf(fp, "\n");
0N/A fprintf(fp, " // Following assertions generated from definition section\n");
0N/A const char *name = NULL;
0N/A for( _defines.reset(); (name = _defines.iter()) != NULL; ) {
0N/A const Expr *expr = (const Expr*)_expr[name];
0N/A assert( expr != NULL, "name in ExprDict without matching Expr in dictionary");
0N/A expr->print_assert(fp);
0N/A }
0N/A}
0N/A
0N/A// Print out the dictionary contents as key-value pairs
603N/Astatic void dumpekey(const void* key) { fprintf(stdout, "%s", (char*) key); }
0N/Astatic void dumpexpr(const void* expr) { fflush(stdout); ((Expr*)expr)->print(); }
0N/A
0N/Avoid ExprDict::dump() {
0N/A _expr.print(dumpekey, dumpexpr);
0N/A}
0N/A
0N/A
0N/A//------------------------------ExprDict::private------------------------------
0N/A// Disable public use of constructor, copy-ctor, operator =, operator ==
0N/AExprDict::ExprDict( ) : _expr(cmpkey,hashkey), _defines() {
0N/A assert( false, "NotImplemented");
0N/A}
0N/AExprDict::ExprDict( const ExprDict & ) : _expr(cmpkey,hashkey), _defines() {
0N/A assert( false, "NotImplemented");
0N/A}
0N/AExprDict &ExprDict::operator =( const ExprDict &rhs) {
0N/A assert( false, "NotImplemented");
0N/A _expr = rhs._expr;
0N/A return *this;
0N/A}
0N/A// == compares two dictionaries; they must have the same keys (their keys
0N/A// must match using CmpKey) and they must have the same values (pointer
0N/A// comparison). If so 1 is returned, if not 0 is returned.
0N/Abool ExprDict::operator ==(const ExprDict &d) const {
0N/A assert( false, "NotImplemented");
0N/A return false;
0N/A}
0N/A
0N/A
0N/A//------------------------------Production-------------------------------------
0N/AProduction::Production(const char *result, const char *constraint, const char *valid) {
0N/A initialize();
0N/A _result = result;
0N/A _constraint = constraint;
0N/A _valid = valid;
0N/A}
0N/A
0N/Avoid Production::initialize() {
0N/A _result = NULL;
0N/A _constraint = NULL;
0N/A _valid = knownInvalid;
0N/A _cost_lb = Expr::get_unknown();
0N/A _cost_ub = Expr::get_unknown();
0N/A}
0N/A
0N/Avoid Production::print() {
0N/A printf("%s", (_result == NULL ? "NULL" : _result ) );
0N/A printf("%s", (_constraint == NULL ? "NULL" : _constraint ) );
0N/A printf("%s", (_valid == NULL ? "NULL" : _valid ) );
0N/A _cost_lb->print();
0N/A _cost_ub->print();
0N/A}
0N/A
0N/A
0N/A//------------------------------ProductionState--------------------------------
0N/Avoid ProductionState::initialize() {
0N/A _constraint = noConstraint;
0N/A
0N/A // reset each Production currently in the dictionary
0N/A DictI iter( &_production );
0N/A const void *x, *y = NULL;
0N/A for( ; iter.test(); ++iter) {
0N/A x = iter._key;
0N/A y = iter._value;
0N/A Production *p = (Production*)y;
0N/A if( p != NULL ) {
0N/A p->initialize();
0N/A }
0N/A }
0N/A}
0N/A
0N/AProduction *ProductionState::getProduction(const char *result) {
0N/A Production *p = (Production *)_production[result];
0N/A if( p == NULL ) {
0N/A p = new Production(result, _constraint, knownInvalid);
0N/A _production.Insert(result, p);
0N/A }
0N/A
0N/A return p;
0N/A}
0N/A
0N/Avoid ProductionState::set_constraint(const char *constraint) {
0N/A _constraint = constraint;
0N/A}
0N/A
0N/Aconst char *ProductionState::valid(const char *result) {
0N/A return getProduction(result)->valid();
0N/A}
0N/A
0N/Avoid ProductionState::set_valid(const char *result) {
0N/A Production *p = getProduction(result);
0N/A
0N/A // Update valid as allowed by current constraints
0N/A if( _constraint == noConstraint ) {
0N/A p->_valid = knownValid;
0N/A } else {
0N/A if( p->_valid != knownValid ) {
0N/A p->_valid = unknownValid;
0N/A }
0N/A }
0N/A}
0N/A
0N/AExpr *ProductionState::cost_lb(const char *result) {
0N/A return getProduction(result)->cost_lb();
0N/A}
0N/A
0N/AExpr *ProductionState::cost_ub(const char *result) {
0N/A return getProduction(result)->cost_ub();
0N/A}
0N/A
0N/Avoid ProductionState::set_cost_bounds(const char *result, const Expr *cost, bool has_state_check, bool has_cost_check) {
0N/A Production *p = getProduction(result);
0N/A
0N/A if( p->_valid == knownInvalid ) {
0N/A // Our cost bounds are not unknown, just not defined.
0N/A p->_cost_lb = cost->clone();
0N/A p->_cost_ub = cost->clone();
0N/A } else if (has_state_check || _constraint != noConstraint) {
0N/A // The production is protected by a condition, so
0N/A // the cost bounds may expand.
0N/A // _cost_lb = min(cost, _cost_lb)
0N/A if( cost->less_than_or_equal(p->_cost_lb) ) {
0N/A p->_cost_lb = cost->clone();
0N/A }
0N/A // _cost_ub = max(cost, _cost_ub)
0N/A if( p->_cost_ub->less_than_or_equal(cost) ) {
0N/A p->_cost_ub = cost->clone();
0N/A }
0N/A } else if (has_cost_check) {
0N/A // The production has no condition check, but does
0N/A // have a cost check that could reduce the upper
0N/A // and/or lower bound.
0N/A // _cost_lb = min(cost, _cost_lb)
0N/A if( cost->less_than_or_equal(p->_cost_lb) ) {
0N/A p->_cost_lb = cost->clone();
0N/A }
0N/A // _cost_ub = min(cost, _cost_ub)
0N/A if( cost->less_than_or_equal(p->_cost_ub) ) {
0N/A p->_cost_ub = cost->clone();
0N/A }
0N/A } else {
0N/A // The costs are unconditionally set.
0N/A p->_cost_lb = cost->clone();
0N/A p->_cost_ub = cost->clone();
0N/A }
0N/A
0N/A}
0N/A
0N/A// Print out the dictionary contents as key-value pairs
603N/Astatic void print_key (const void* key) { fprintf(stdout, "%s", (char*) key); }
0N/Astatic void print_production(const void* production) { fflush(stdout); ((Production*)production)->print(); }
0N/A
0N/Avoid ProductionState::print() {
0N/A _production.print(print_key, print_production);
0N/A}