src/share/vm/adlc/dfa.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 // DFA.CPP - Method definitions for outputting the matcher DFA from ADLC
aoqi@0 26 #include "adlc.hpp"
aoqi@0 27
aoqi@0 28 //---------------------------Switches for debugging output---------------------
aoqi@0 29 static bool debug_output = false;
aoqi@0 30 static bool debug_output1 = false; // top level chain rules
aoqi@0 31
aoqi@0 32 //---------------------------Access to internals of class State----------------
aoqi@0 33 static const char *sLeft = "_kids[0]";
aoqi@0 34 static const char *sRight = "_kids[1]";
aoqi@0 35
aoqi@0 36 //---------------------------DFA productions-----------------------------------
aoqi@0 37 static const char *dfa_production = "DFA_PRODUCTION";
aoqi@0 38 static const char *dfa_production_set_valid = "DFA_PRODUCTION__SET_VALID";
aoqi@0 39
aoqi@0 40 //---------------------------Production State----------------------------------
aoqi@0 41 static const char *knownInvalid = "knownInvalid"; // The result does NOT have a rule defined
aoqi@0 42 static const char *knownValid = "knownValid"; // The result must be produced by a rule
aoqi@0 43 static const char *unknownValid = "unknownValid"; // Unknown (probably due to a child or predicate constraint)
aoqi@0 44
aoqi@0 45 static const char *noConstraint = "noConstraint"; // No constraints seen so far
aoqi@0 46 static const char *hasConstraint = "hasConstraint"; // Within the first constraint
aoqi@0 47
aoqi@0 48
aoqi@0 49 //------------------------------Production------------------------------------
aoqi@0 50 // Track the status of productions for a particular result
aoqi@0 51 class Production {
aoqi@0 52 public:
aoqi@0 53 const char *_result;
aoqi@0 54 const char *_constraint;
aoqi@0 55 const char *_valid;
aoqi@0 56 Expr *_cost_lb; // Cost lower bound for this production
aoqi@0 57 Expr *_cost_ub; // Cost upper bound for this production
aoqi@0 58
aoqi@0 59 public:
aoqi@0 60 Production(const char *result, const char *constraint, const char *valid);
aoqi@0 61 ~Production() {};
aoqi@0 62
aoqi@0 63 void initialize(); // reset to be an empty container
aoqi@0 64
aoqi@0 65 const char *valid() const { return _valid; }
aoqi@0 66 Expr *cost_lb() const { return (Expr *)_cost_lb; }
aoqi@0 67 Expr *cost_ub() const { return (Expr *)_cost_ub; }
aoqi@0 68
aoqi@0 69 void print();
aoqi@0 70 };
aoqi@0 71
aoqi@0 72
aoqi@0 73 //------------------------------ProductionState--------------------------------
aoqi@0 74 // Track the status of all production rule results
aoqi@0 75 // Reset for each root opcode (e.g., Op_RegI, Op_AddI, ...)
aoqi@0 76 class ProductionState {
aoqi@0 77 private:
aoqi@0 78 Dict _production; // map result of production, char*, to information or NULL
aoqi@0 79 const char *_constraint;
aoqi@0 80
aoqi@0 81 public:
aoqi@0 82 // cmpstr does string comparisions. hashstr computes a key.
aoqi@0 83 ProductionState(Arena *arena) : _production(cmpstr, hashstr, arena) { initialize(); };
aoqi@0 84 ~ProductionState() { };
aoqi@0 85
aoqi@0 86 void initialize(); // reset local and dictionary state
aoqi@0 87
aoqi@0 88 const char *constraint();
aoqi@0 89 void set_constraint(const char *constraint); // currently working inside of constraints
aoqi@0 90
aoqi@0 91 const char *valid(const char *result); // unknownValid, or status for this production
aoqi@0 92 void set_valid(const char *result); // if not constrained, set status to knownValid
aoqi@0 93
aoqi@0 94 Expr *cost_lb(const char *result);
aoqi@0 95 Expr *cost_ub(const char *result);
aoqi@0 96 void set_cost_bounds(const char *result, const Expr *cost, bool has_state_check, bool has_cost_check);
aoqi@0 97
aoqi@0 98 // Return the Production associated with the result,
aoqi@0 99 // or create a new Production and insert it into the dictionary.
aoqi@0 100 Production *getProduction(const char *result);
aoqi@0 101
aoqi@0 102 void print();
aoqi@0 103
aoqi@0 104 private:
aoqi@0 105 // Disable public use of constructor, copy-ctor, ...
aoqi@0 106 ProductionState( ) : _production(cmpstr, hashstr, Form::arena) { assert( false, "NotImplemented"); };
aoqi@0 107 ProductionState( const ProductionState & ) : _production(cmpstr, hashstr, Form::arena) { assert( false, "NotImplemented"); }; // Deep-copy
aoqi@0 108 };
aoqi@0 109
aoqi@0 110
aoqi@0 111 //---------------------------Helper Functions----------------------------------
aoqi@0 112 // cost_check template:
aoqi@0 113 // 1) if (STATE__NOT_YET_VALID(EBXREGI) || _cost[EBXREGI] > c) {
aoqi@0 114 // 2) DFA_PRODUCTION__SET_VALID(EBXREGI, cmovI_memu_rule, c)
aoqi@0 115 // 3) }
aoqi@0 116 //
aoqi@0 117 static void cost_check(FILE *fp, const char *spaces,
aoqi@0 118 const char *arrayIdx, const Expr *cost, const char *rule, ProductionState &status) {
aoqi@0 119 bool state_check = false; // true if this production needs to check validity
aoqi@0 120 bool cost_check = false; // true if this production needs to check cost
aoqi@0 121 bool cost_is_above_upper_bound = false; // true if this production is unnecessary due to high cost
aoqi@0 122 bool cost_is_below_lower_bound = false; // true if this production replaces a higher cost production
aoqi@0 123
aoqi@0 124 // Get information about this production
aoqi@0 125 const Expr *previous_ub = status.cost_ub(arrayIdx);
aoqi@0 126 if( !previous_ub->is_unknown() ) {
aoqi@0 127 if( previous_ub->less_than_or_equal(cost) ) {
aoqi@0 128 cost_is_above_upper_bound = true;
aoqi@0 129 if( debug_output ) { fprintf(fp, "// Previous rule with lower cost than: %s === %s_rule costs %s\n", arrayIdx, rule, cost->as_string()); }
aoqi@0 130 }
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 const Expr *previous_lb = status.cost_lb(arrayIdx);
aoqi@0 134 if( !previous_lb->is_unknown() ) {
aoqi@0 135 if( cost->less_than_or_equal(previous_lb) ) {
aoqi@0 136 cost_is_below_lower_bound = true;
aoqi@0 137 if( debug_output ) { fprintf(fp, "// Previous rule with higher cost\n"); }
aoqi@0 138 }
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 // line 1)
aoqi@0 142 // Check for validity and compare to other match costs
aoqi@0 143 const char *validity_check = status.valid(arrayIdx);
aoqi@0 144 if( validity_check == unknownValid ) {
aoqi@0 145 fprintf(fp, "%sif (STATE__NOT_YET_VALID(%s) || _cost[%s] > %s) {\n", spaces, arrayIdx, arrayIdx, cost->as_string());
aoqi@0 146 state_check = true;
aoqi@0 147 cost_check = true;
aoqi@0 148 }
aoqi@0 149 else if( validity_check == knownInvalid ) {
aoqi@0 150 if( debug_output ) { fprintf(fp, "%s// %s KNOWN_INVALID \n", spaces, arrayIdx); }
aoqi@0 151 }
aoqi@0 152 else if( validity_check == knownValid ) {
aoqi@0 153 if( cost_is_above_upper_bound ) {
aoqi@0 154 // production cost is known to be too high.
aoqi@0 155 return;
aoqi@0 156 } else if( cost_is_below_lower_bound ) {
aoqi@0 157 // production will unconditionally overwrite a previous production that had higher cost
aoqi@0 158 } else {
aoqi@0 159 fprintf(fp, "%sif ( /* %s KNOWN_VALID || */ _cost[%s] > %s) {\n", spaces, arrayIdx, arrayIdx, cost->as_string());
aoqi@0 160 cost_check = true;
aoqi@0 161 }
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 // line 2)
aoqi@0 165 // no need to set State vector if our state is knownValid
aoqi@0 166 const char *production = (validity_check == knownValid) ? dfa_production : dfa_production_set_valid;
aoqi@0 167 fprintf(fp, "%s %s(%s, %s_rule, %s)", spaces, production, arrayIdx, rule, cost->as_string() );
aoqi@0 168 if( validity_check == knownValid ) {
aoqi@0 169 if( cost_is_below_lower_bound ) { fprintf(fp, "\t // overwrites higher cost rule"); }
aoqi@0 170 }
aoqi@0 171 fprintf(fp, "\n");
aoqi@0 172
aoqi@0 173 // line 3)
aoqi@0 174 if( cost_check || state_check ) {
aoqi@0 175 fprintf(fp, "%s}\n", spaces);
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 status.set_cost_bounds(arrayIdx, cost, state_check, cost_check);
aoqi@0 179
aoqi@0 180 // Update ProductionState
aoqi@0 181 if( validity_check != knownValid ) {
aoqi@0 182 // set State vector if not previously known
aoqi@0 183 status.set_valid(arrayIdx);
aoqi@0 184 }
aoqi@0 185 }
aoqi@0 186
aoqi@0 187
aoqi@0 188 //---------------------------child_test----------------------------------------
aoqi@0 189 // Example:
aoqi@0 190 // STATE__VALID_CHILD(_kids[0], FOO) && STATE__VALID_CHILD(_kids[1], BAR)
aoqi@0 191 // Macro equivalent to: _kids[0]->valid(FOO) && _kids[1]->valid(BAR)
aoqi@0 192 //
aoqi@0 193 static void child_test(FILE *fp, MatchList &mList) {
aoqi@0 194 if (mList._lchild) { // If left child, check it
aoqi@0 195 const char* lchild_to_upper = ArchDesc::getMachOperEnum(mList._lchild);
aoqi@0 196 fprintf(fp, "STATE__VALID_CHILD(_kids[0], %s)", lchild_to_upper);
aoqi@0 197 delete[] lchild_to_upper;
aoqi@0 198 }
aoqi@0 199 if (mList._lchild && mList._rchild) { // If both, add the "&&"
aoqi@0 200 fprintf(fp, " && ");
aoqi@0 201 }
aoqi@0 202 if (mList._rchild) { // If right child, check it
aoqi@0 203 const char* rchild_to_upper = ArchDesc::getMachOperEnum(mList._rchild);
aoqi@0 204 fprintf(fp, "STATE__VALID_CHILD(_kids[1], %s)", rchild_to_upper);
aoqi@0 205 delete[] rchild_to_upper;
aoqi@0 206 }
aoqi@0 207 }
aoqi@0 208
aoqi@0 209 //---------------------------calc_cost-----------------------------------------
aoqi@0 210 // Example:
aoqi@0 211 // unsigned int c = _kids[0]->_cost[FOO] + _kids[1]->_cost[BAR] + 5;
aoqi@0 212 //
aoqi@0 213 Expr *ArchDesc::calc_cost(FILE *fp, const char *spaces, MatchList &mList, ProductionState &status) {
aoqi@0 214 fprintf(fp, "%sunsigned int c = ", spaces);
aoqi@0 215 Expr *c = new Expr("0");
aoqi@0 216 if (mList._lchild) { // If left child, add it in
aoqi@0 217 const char* lchild_to_upper = ArchDesc::getMachOperEnum(mList._lchild);
aoqi@0 218 sprintf(Expr::buffer(), "_kids[0]->_cost[%s]", lchild_to_upper);
aoqi@0 219 c->add(Expr::buffer());
aoqi@0 220 delete[] lchild_to_upper;
aoqi@0 221 }
aoqi@0 222 if (mList._rchild) { // If right child, add it in
aoqi@0 223 const char* rchild_to_upper = ArchDesc::getMachOperEnum(mList._rchild);
aoqi@0 224 sprintf(Expr::buffer(), "_kids[1]->_cost[%s]", rchild_to_upper);
aoqi@0 225 c->add(Expr::buffer());
aoqi@0 226 delete[] rchild_to_upper;
aoqi@0 227 }
aoqi@0 228 // Add in cost of this rule
aoqi@0 229 const char *mList_cost = mList.get_cost();
aoqi@0 230 c->add(mList_cost, *this);
aoqi@0 231
aoqi@0 232 fprintf(fp, "%s;\n", c->as_string());
aoqi@0 233 c->set_external_name("c");
aoqi@0 234 return c;
aoqi@0 235 }
aoqi@0 236
aoqi@0 237
aoqi@0 238 //---------------------------gen_match-----------------------------------------
aoqi@0 239 void ArchDesc::gen_match(FILE *fp, MatchList &mList, ProductionState &status, Dict &operands_chained_from) {
aoqi@0 240 const char *spaces4 = " ";
aoqi@0 241 const char *spaces6 = " ";
aoqi@0 242
aoqi@0 243 fprintf(fp, "%s", spaces4);
aoqi@0 244 // Only generate child tests if this is not a leaf node
aoqi@0 245 bool has_child_constraints = mList._lchild || mList._rchild;
aoqi@0 246 const char *predicate_test = mList.get_pred();
aoqi@0 247 if (has_child_constraints || predicate_test) {
aoqi@0 248 // Open the child-and-predicate-test braces
aoqi@0 249 fprintf(fp, "if( ");
aoqi@0 250 status.set_constraint(hasConstraint);
aoqi@0 251 child_test(fp, mList);
aoqi@0 252 // Only generate predicate test if one exists for this match
aoqi@0 253 if (predicate_test) {
aoqi@0 254 if (has_child_constraints) {
aoqi@0 255 fprintf(fp," &&\n");
aoqi@0 256 }
aoqi@0 257 fprintf(fp, "%s %s", spaces6, predicate_test);
aoqi@0 258 }
aoqi@0 259 // End of outer tests
aoqi@0 260 fprintf(fp," ) ");
aoqi@0 261 } else {
aoqi@0 262 // No child or predicate test needed
aoqi@0 263 status.set_constraint(noConstraint);
aoqi@0 264 }
aoqi@0 265
aoqi@0 266 // End of outer tests
aoqi@0 267 fprintf(fp,"{\n");
aoqi@0 268
aoqi@0 269 // Calculate cost of this match
aoqi@0 270 const Expr *cost = calc_cost(fp, spaces6, mList, status);
aoqi@0 271 // Check against other match costs, and update cost & rule vectors
aoqi@0 272 cost_check(fp, spaces6, ArchDesc::getMachOperEnum(mList._resultStr), cost, mList._opcode, status);
aoqi@0 273
aoqi@0 274 // If this is a member of an operand class, update the class cost & rule
aoqi@0 275 expand_opclass( fp, spaces6, cost, mList._resultStr, status);
aoqi@0 276
aoqi@0 277 // Check if this rule should be used to generate the chains as well.
aoqi@0 278 const char *rule = /* set rule to "Invalid" for internal operands */
aoqi@0 279 strcmp(mList._opcode,mList._resultStr) ? mList._opcode : "Invalid";
aoqi@0 280
aoqi@0 281 // If this rule produces an operand which has associated chain rules,
aoqi@0 282 // update the operands with the chain rule + this rule cost & this rule.
aoqi@0 283 chain_rule(fp, spaces6, mList._resultStr, cost, rule, operands_chained_from, status);
aoqi@0 284
aoqi@0 285 // Close the child-and-predicate-test braces
aoqi@0 286 fprintf(fp, " }\n");
aoqi@0 287
aoqi@0 288 }
aoqi@0 289
aoqi@0 290
aoqi@0 291 //---------------------------expand_opclass------------------------------------
aoqi@0 292 // Chain from one result_type to all other members of its operand class
aoqi@0 293 void ArchDesc::expand_opclass(FILE *fp, const char *indent, const Expr *cost,
aoqi@0 294 const char *result_type, ProductionState &status) {
aoqi@0 295 const Form *form = _globalNames[result_type];
aoqi@0 296 OperandForm *op = form ? form->is_operand() : NULL;
aoqi@0 297 if( op && op->_classes.count() > 0 ) {
aoqi@0 298 if( debug_output ) { fprintf(fp, "// expand operand classes for operand: %s \n", (char *)op->_ident ); } // %%%%% Explanation
aoqi@0 299 // Iterate through all operand classes which include this operand
aoqi@0 300 op->_classes.reset();
aoqi@0 301 const char *oclass;
aoqi@0 302 // Expr *cCost = new Expr(cost);
aoqi@0 303 while( (oclass = op->_classes.iter()) != NULL )
aoqi@0 304 // Check against other match costs, and update cost & rule vectors
aoqi@0 305 cost_check(fp, indent, ArchDesc::getMachOperEnum(oclass), cost, result_type, status);
aoqi@0 306 }
aoqi@0 307 }
aoqi@0 308
aoqi@0 309 //---------------------------chain_rule----------------------------------------
aoqi@0 310 // Starting at 'operand', check if we know how to automatically generate other results
aoqi@0 311 void ArchDesc::chain_rule(FILE *fp, const char *indent, const char *operand,
aoqi@0 312 const Expr *icost, const char *irule, Dict &operands_chained_from, ProductionState &status) {
aoqi@0 313
aoqi@0 314 // Check if we have already generated chains from this starting point
aoqi@0 315 if( operands_chained_from[operand] != NULL ) {
aoqi@0 316 return;
aoqi@0 317 } else {
aoqi@0 318 operands_chained_from.Insert( operand, operand);
aoqi@0 319 }
aoqi@0 320 if( debug_output ) { fprintf(fp, "// chain rules starting from: %s and %s \n", (char *)operand, (char *)irule); } // %%%%% Explanation
aoqi@0 321
aoqi@0 322 ChainList *lst = (ChainList *)_chainRules[operand];
aoqi@0 323 if (lst) {
aoqi@0 324 // printf("\nChain from <%s> at cost #%s\n",operand, icost ? icost : "_");
aoqi@0 325 const char *result, *cost, *rule;
aoqi@0 326 for(lst->reset(); (lst->iter(result,cost,rule)) == true; ) {
aoqi@0 327 // Do not generate operands that are already available
aoqi@0 328 if( operands_chained_from[result] != NULL ) {
aoqi@0 329 continue;
aoqi@0 330 } else {
aoqi@0 331 // Compute the cost for previous match + chain_rule_cost
aoqi@0 332 // total_cost = icost + cost;
aoqi@0 333 Expr *total_cost = icost->clone(); // icost + cost
aoqi@0 334 total_cost->add(cost, *this);
aoqi@0 335
aoqi@0 336 // Check for transitive chain rules
aoqi@0 337 Form *form = (Form *)_globalNames[rule];
aoqi@0 338 if ( ! form->is_instruction()) {
aoqi@0 339 // printf(" result=%s cost=%s rule=%s\n", result, total_cost, rule);
aoqi@0 340 // Check against other match costs, and update cost & rule vectors
aoqi@0 341 const char *reduce_rule = strcmp(irule,"Invalid") ? irule : rule;
aoqi@0 342 cost_check(fp, indent, ArchDesc::getMachOperEnum(result), total_cost, reduce_rule, status);
aoqi@0 343 chain_rule(fp, indent, result, total_cost, irule, operands_chained_from, status);
aoqi@0 344 } else {
aoqi@0 345 // printf(" result=%s cost=%s rule=%s\n", result, total_cost, rule);
aoqi@0 346 // Check against other match costs, and update cost & rule vectors
aoqi@0 347 cost_check(fp, indent, ArchDesc::getMachOperEnum(result), total_cost, rule, status);
aoqi@0 348 chain_rule(fp, indent, result, total_cost, rule, operands_chained_from, status);
aoqi@0 349 }
aoqi@0 350
aoqi@0 351 // If this is a member of an operand class, update class cost & rule
aoqi@0 352 expand_opclass( fp, indent, total_cost, result, status );
aoqi@0 353 }
aoqi@0 354 }
aoqi@0 355 }
aoqi@0 356 }
aoqi@0 357
aoqi@0 358 //---------------------------prune_matchlist-----------------------------------
aoqi@0 359 // Check for duplicate entries in a matchlist, and prune out the higher cost
aoqi@0 360 // entry.
aoqi@0 361 void ArchDesc::prune_matchlist(Dict &minimize, MatchList &mlist) {
aoqi@0 362
aoqi@0 363 }
aoqi@0 364
aoqi@0 365 //---------------------------buildDFA------------------------------------------
aoqi@0 366 // DFA is a large switch with case statements for each ideal opcode encountered
aoqi@0 367 // in any match rule in the ad file. Each case has a series of if's to handle
aoqi@0 368 // the match or fail decisions. The matches test the cost function of that
aoqi@0 369 // rule, and prune any cases which are higher cost for the same reduction.
aoqi@0 370 // In order to generate the DFA we walk the table of ideal opcode/MatchList
aoqi@0 371 // pairs generated by the ADLC front end to build the contents of the case
aoqi@0 372 // statements (a series of if statements).
aoqi@0 373 void ArchDesc::buildDFA(FILE* fp) {
aoqi@0 374 int i;
aoqi@0 375 // Remember operands that are the starting points for chain rules.
aoqi@0 376 // Prevent cycles by checking if we have already generated chain.
aoqi@0 377 Dict operands_chained_from(cmpstr, hashstr, Form::arena);
aoqi@0 378
aoqi@0 379 // Hash inputs to match rules so that final DFA contains only one entry for
aoqi@0 380 // each match pattern which is the low cost entry.
aoqi@0 381 Dict minimize(cmpstr, hashstr, Form::arena);
aoqi@0 382
aoqi@0 383 // Track status of dfa for each resulting production
aoqi@0 384 // reset for each ideal root.
aoqi@0 385 ProductionState status(Form::arena);
aoqi@0 386
aoqi@0 387 // Output the start of the DFA method into the output file
aoqi@0 388
aoqi@0 389 fprintf(fp, "\n");
aoqi@0 390 fprintf(fp, "//------------------------- Source -----------------------------------------\n");
aoqi@0 391 // Do not put random source code into the DFA.
aoqi@0 392 // If there are constants which need sharing, put them in "source_hpp" forms.
aoqi@0 393 // _source.output(fp);
aoqi@0 394 fprintf(fp, "\n");
aoqi@0 395 fprintf(fp, "//------------------------- Attributes -------------------------------------\n");
aoqi@0 396 _attributes.output(fp);
aoqi@0 397 fprintf(fp, "\n");
aoqi@0 398 fprintf(fp, "//------------------------- Macros -----------------------------------------\n");
aoqi@0 399 // #define DFA_PRODUCTION(result, rule, cost)\
aoqi@0 400 // _cost[ (result) ] = cost; _rule[ (result) ] = rule;
aoqi@0 401 fprintf(fp, "#define %s(result, rule, cost)\\\n", dfa_production);
aoqi@0 402 fprintf(fp, " _cost[ (result) ] = cost; _rule[ (result) ] = rule;\n");
aoqi@0 403 fprintf(fp, "\n");
aoqi@0 404
aoqi@0 405 // #define DFA_PRODUCTION__SET_VALID(result, rule, cost)\
aoqi@0 406 // DFA_PRODUCTION( (result), (rule), (cost) ); STATE__SET_VALID( (result) );
aoqi@0 407 fprintf(fp, "#define %s(result, rule, cost)\\\n", dfa_production_set_valid);
aoqi@0 408 fprintf(fp, " %s( (result), (rule), (cost) ); STATE__SET_VALID( (result) );\n", dfa_production);
aoqi@0 409 fprintf(fp, "\n");
aoqi@0 410
aoqi@0 411 fprintf(fp, "//------------------------- DFA --------------------------------------------\n");
aoqi@0 412
aoqi@0 413 fprintf(fp,
aoqi@0 414 "// DFA is a large switch with case statements for each ideal opcode encountered\n"
aoqi@0 415 "// in any match rule in the ad file. Each case has a series of if's to handle\n"
aoqi@0 416 "// the match or fail decisions. The matches test the cost function of that\n"
aoqi@0 417 "// rule, and prune any cases which are higher cost for the same reduction.\n"
aoqi@0 418 "// In order to generate the DFA we walk the table of ideal opcode/MatchList\n"
aoqi@0 419 "// pairs generated by the ADLC front end to build the contents of the case\n"
aoqi@0 420 "// statements (a series of if statements).\n"
aoqi@0 421 );
aoqi@0 422 fprintf(fp, "\n");
aoqi@0 423 fprintf(fp, "\n");
aoqi@0 424 if (_dfa_small) {
aoqi@0 425 // Now build the individual routines just like the switch entries in large version
aoqi@0 426 // Iterate over the table of MatchLists, start at first valid opcode of 1
aoqi@0 427 for (i = 1; i < _last_opcode; i++) {
aoqi@0 428 if (_mlistab[i] == NULL) continue;
aoqi@0 429 // Generate the routine header statement for this opcode
aoqi@0 430 fprintf(fp, "void State::_sub_Op_%s(const Node *n){\n", NodeClassNames[i]);
aoqi@0 431 // Generate body. Shared for both inline and out-of-line version
aoqi@0 432 gen_dfa_state_body(fp, minimize, status, operands_chained_from, i);
aoqi@0 433 // End of routine
aoqi@0 434 fprintf(fp, "}\n");
aoqi@0 435 }
aoqi@0 436 }
aoqi@0 437 fprintf(fp, "bool State::DFA");
aoqi@0 438 fprintf(fp, "(int opcode, const Node *n) {\n");
aoqi@0 439 fprintf(fp, " switch(opcode) {\n");
aoqi@0 440
aoqi@0 441 // Iterate over the table of MatchLists, start at first valid opcode of 1
aoqi@0 442 for (i = 1; i < _last_opcode; i++) {
aoqi@0 443 if (_mlistab[i] == NULL) continue;
aoqi@0 444 // Generate the case statement for this opcode
aoqi@0 445 if (_dfa_small) {
aoqi@0 446 fprintf(fp, " case Op_%s: { _sub_Op_%s(n);\n", NodeClassNames[i], NodeClassNames[i]);
aoqi@0 447 } else {
aoqi@0 448 fprintf(fp, " case Op_%s: {\n", NodeClassNames[i]);
aoqi@0 449 // Walk the list, compacting it
aoqi@0 450 gen_dfa_state_body(fp, minimize, status, operands_chained_from, i);
aoqi@0 451 }
aoqi@0 452 // Print the "break"
aoqi@0 453 fprintf(fp, " break;\n");
aoqi@0 454 fprintf(fp, " }\n");
aoqi@0 455 }
aoqi@0 456
aoqi@0 457 // Generate the default case for switch(opcode)
aoqi@0 458 fprintf(fp, " \n");
aoqi@0 459 fprintf(fp, " default:\n");
aoqi@0 460 fprintf(fp, " tty->print(\"Default case invoked for: \\n\");\n");
aoqi@0 461 fprintf(fp, " tty->print(\" opcode = %cd, \\\"%cs\\\"\\n\", opcode, NodeClassNames[opcode]);\n", '%', '%');
aoqi@0 462 fprintf(fp, " return false;\n");
aoqi@0 463 fprintf(fp, " }\n");
aoqi@0 464
aoqi@0 465 // Return status, indicating a successful match.
aoqi@0 466 fprintf(fp, " return true;\n");
aoqi@0 467 // Generate the closing brace for method Matcher::DFA
aoqi@0 468 fprintf(fp, "}\n");
aoqi@0 469 Expr::check_buffers();
aoqi@0 470 }
aoqi@0 471
aoqi@0 472
aoqi@0 473 class dfa_shared_preds {
aoqi@0 474 enum { count = 4 };
aoqi@0 475
aoqi@0 476 static bool _found[count];
aoqi@0 477 static const char* _type [count];
aoqi@0 478 static const char* _var [count];
aoqi@0 479 static const char* _pred [count];
aoqi@0 480
aoqi@0 481 static void check_index(int index) { assert( 0 <= index && index < count, "Invalid index"); }
aoqi@0 482
aoqi@0 483 // Confirm that this is a separate sub-expression.
aoqi@0 484 // Only need to catch common cases like " ... && shared ..."
aoqi@0 485 // and avoid hazardous ones like "...->shared"
aoqi@0 486 static bool valid_loc(char *pred, char *shared) {
aoqi@0 487 // start of predicate is valid
aoqi@0 488 if( shared == pred ) return true;
aoqi@0 489
aoqi@0 490 // Check previous character and recurse if needed
aoqi@0 491 char *prev = shared - 1;
aoqi@0 492 char c = *prev;
aoqi@0 493 switch( c ) {
aoqi@0 494 case ' ':
aoqi@0 495 case '\n':
aoqi@0 496 return dfa_shared_preds::valid_loc(pred, prev);
aoqi@0 497 case '!':
aoqi@0 498 case '(':
aoqi@0 499 case '<':
aoqi@0 500 case '=':
aoqi@0 501 return true;
aoqi@0 502 case '"': // such as: #line 10 "myfile.ad"\n mypredicate
aoqi@0 503 return true;
aoqi@0 504 case '|':
aoqi@0 505 if( prev != pred && *(prev-1) == '|' ) return true;
aoqi@0 506 case '&':
aoqi@0 507 if( prev != pred && *(prev-1) == '&' ) return true;
aoqi@0 508 default:
aoqi@0 509 return false;
aoqi@0 510 }
aoqi@0 511
aoqi@0 512 return false;
aoqi@0 513 }
aoqi@0 514
aoqi@0 515 public:
aoqi@0 516
aoqi@0 517 static bool found(int index){ check_index(index); return _found[index]; }
aoqi@0 518 static void set_found(int index, bool val) { check_index(index); _found[index] = val; }
aoqi@0 519 static void reset_found() {
aoqi@0 520 for( int i = 0; i < count; ++i ) { _found[i] = false; }
aoqi@0 521 };
aoqi@0 522
aoqi@0 523 static const char* type(int index) { check_index(index); return _type[index]; }
aoqi@0 524 static const char* var (int index) { check_index(index); return _var [index]; }
aoqi@0 525 static const char* pred(int index) { check_index(index); return _pred[index]; }
aoqi@0 526
aoqi@0 527 // Check each predicate in the MatchList for common sub-expressions
aoqi@0 528 static void cse_matchlist(MatchList *matchList) {
aoqi@0 529 for( MatchList *mList = matchList; mList != NULL; mList = mList->get_next() ) {
aoqi@0 530 Predicate* predicate = mList->get_pred_obj();
aoqi@0 531 char* pred = mList->get_pred();
aoqi@0 532 if( pred != NULL ) {
aoqi@0 533 for(int index = 0; index < count; ++index ) {
aoqi@0 534 const char *shared_pred = dfa_shared_preds::pred(index);
aoqi@0 535 const char *shared_pred_var = dfa_shared_preds::var(index);
aoqi@0 536 bool result = dfa_shared_preds::cse_predicate(predicate, shared_pred, shared_pred_var);
aoqi@0 537 if( result ) dfa_shared_preds::set_found(index, true);
aoqi@0 538 }
aoqi@0 539 }
aoqi@0 540 }
aoqi@0 541 }
aoqi@0 542
aoqi@0 543 // If the Predicate contains a common sub-expression, replace the Predicate's
aoqi@0 544 // string with one that uses the variable name.
aoqi@0 545 static bool cse_predicate(Predicate* predicate, const char *shared_pred, const char *shared_pred_var) {
aoqi@0 546 bool result = false;
aoqi@0 547 char *pred = predicate->_pred;
aoqi@0 548 if( pred != NULL ) {
aoqi@0 549 char *new_pred = pred;
aoqi@0 550 for( char *shared_pred_loc = strstr(new_pred, shared_pred);
aoqi@0 551 shared_pred_loc != NULL && dfa_shared_preds::valid_loc(new_pred,shared_pred_loc);
aoqi@0 552 shared_pred_loc = strstr(new_pred, shared_pred) ) {
aoqi@0 553 // Do not modify the original predicate string, it is shared
aoqi@0 554 if( new_pred == pred ) {
aoqi@0 555 new_pred = strdup(pred);
aoqi@0 556 shared_pred_loc = strstr(new_pred, shared_pred);
aoqi@0 557 }
aoqi@0 558 // Replace shared_pred with variable name
aoqi@0 559 strncpy(shared_pred_loc, shared_pred_var, strlen(shared_pred_var));
aoqi@0 560 }
aoqi@0 561 // Install new predicate
aoqi@0 562 if( new_pred != pred ) {
aoqi@0 563 predicate->_pred = new_pred;
aoqi@0 564 result = true;
aoqi@0 565 }
aoqi@0 566 }
aoqi@0 567 return result;
aoqi@0 568 }
aoqi@0 569
aoqi@0 570 // Output the hoisted common sub-expression if we found it in predicates
aoqi@0 571 static void generate_cse(FILE *fp) {
aoqi@0 572 for(int j = 0; j < count; ++j ) {
aoqi@0 573 if( dfa_shared_preds::found(j) ) {
aoqi@0 574 const char *shared_pred_type = dfa_shared_preds::type(j);
aoqi@0 575 const char *shared_pred_var = dfa_shared_preds::var(j);
aoqi@0 576 const char *shared_pred = dfa_shared_preds::pred(j);
aoqi@0 577 fprintf(fp, " %s %s = %s;\n", shared_pred_type, shared_pred_var, shared_pred);
aoqi@0 578 }
aoqi@0 579 }
aoqi@0 580 }
aoqi@0 581 };
aoqi@0 582 // shared predicates, _var and _pred entry should be the same length
aoqi@0 583 bool dfa_shared_preds::_found[dfa_shared_preds::count]
aoqi@0 584 = { false, false, false, false };
aoqi@0 585 const char* dfa_shared_preds::_type[dfa_shared_preds::count]
aoqi@0 586 = { "int", "jlong", "intptr_t", "bool" };
aoqi@0 587 const char* dfa_shared_preds::_var [dfa_shared_preds::count]
aoqi@0 588 = { "_n_get_int__", "_n_get_long__", "_n_get_intptr_t__", "Compile__current____select_24_bit_instr__" };
aoqi@0 589 const char* dfa_shared_preds::_pred[dfa_shared_preds::count]
aoqi@0 590 = { "n->get_int()", "n->get_long()", "n->get_intptr_t()", "Compile::current()->select_24_bit_instr()" };
aoqi@0 591
aoqi@0 592
aoqi@0 593 void ArchDesc::gen_dfa_state_body(FILE* fp, Dict &minimize, ProductionState &status, Dict &operands_chained_from, int i) {
aoqi@0 594 // Start the body of each Op_XXX sub-dfa with a clean state.
aoqi@0 595 status.initialize();
aoqi@0 596
aoqi@0 597 // Walk the list, compacting it
aoqi@0 598 MatchList* mList = _mlistab[i];
aoqi@0 599 do {
aoqi@0 600 // Hash each entry using inputs as key and pointer as data.
aoqi@0 601 // If there is already an entry, keep the one with lower cost, and
aoqi@0 602 // remove the other one from the list.
aoqi@0 603 prune_matchlist(minimize, *mList);
aoqi@0 604 // Iterate
aoqi@0 605 mList = mList->get_next();
aoqi@0 606 } while(mList != NULL);
aoqi@0 607
aoqi@0 608 // Hoist previously specified common sub-expressions out of predicates
aoqi@0 609 dfa_shared_preds::reset_found();
aoqi@0 610 dfa_shared_preds::cse_matchlist(_mlistab[i]);
aoqi@0 611 dfa_shared_preds::generate_cse(fp);
aoqi@0 612
aoqi@0 613 mList = _mlistab[i];
aoqi@0 614
aoqi@0 615 // Walk the list again, generating code
aoqi@0 616 do {
aoqi@0 617 // Each match can generate its own chains
aoqi@0 618 operands_chained_from.Clear();
aoqi@0 619 gen_match(fp, *mList, status, operands_chained_from);
aoqi@0 620 mList = mList->get_next();
aoqi@0 621 } while(mList != NULL);
aoqi@0 622 // Fill in any chain rules which add instructions
aoqi@0 623 // These can generate their own chains as well.
aoqi@0 624 operands_chained_from.Clear(); //
aoqi@0 625 if( debug_output1 ) { fprintf(fp, "// top level chain rules for: %s \n", (char *)NodeClassNames[i]); } // %%%%% Explanation
aoqi@0 626 const Expr *zeroCost = new Expr("0");
aoqi@0 627 chain_rule(fp, " ", (char *)NodeClassNames[i], zeroCost, "Invalid",
aoqi@0 628 operands_chained_from, status);
aoqi@0 629 }
aoqi@0 630
aoqi@0 631
aoqi@0 632
aoqi@0 633 //------------------------------Expr------------------------------------------
aoqi@0 634 Expr *Expr::_unknown_expr = NULL;
aoqi@0 635 char Expr::string_buffer[STRING_BUFFER_LENGTH];
aoqi@0 636 char Expr::external_buffer[STRING_BUFFER_LENGTH];
aoqi@0 637 bool Expr::_init_buffers = Expr::init_buffers();
aoqi@0 638
aoqi@0 639 Expr::Expr() {
aoqi@0 640 _external_name = NULL;
aoqi@0 641 _expr = "Invalid_Expr";
aoqi@0 642 _min_value = Expr::Max;
aoqi@0 643 _max_value = Expr::Zero;
aoqi@0 644 }
aoqi@0 645 Expr::Expr(const char *cost) {
aoqi@0 646 _external_name = NULL;
aoqi@0 647
aoqi@0 648 int intval = 0;
aoqi@0 649 if( cost == NULL ) {
aoqi@0 650 _expr = "0";
aoqi@0 651 _min_value = Expr::Zero;
aoqi@0 652 _max_value = Expr::Zero;
aoqi@0 653 }
aoqi@0 654 else if( ADLParser::is_int_token(cost, intval) ) {
aoqi@0 655 _expr = cost;
aoqi@0 656 _min_value = intval;
aoqi@0 657 _max_value = intval;
aoqi@0 658 }
aoqi@0 659 else {
aoqi@0 660 assert( strcmp(cost,"0") != 0, "Recognize string zero as an int");
aoqi@0 661 _expr = cost;
aoqi@0 662 _min_value = Expr::Zero;
aoqi@0 663 _max_value = Expr::Max;
aoqi@0 664 }
aoqi@0 665 }
aoqi@0 666
aoqi@0 667 Expr::Expr(const char *name, const char *expression, int min_value, int max_value) {
aoqi@0 668 _external_name = name;
aoqi@0 669 _expr = expression ? expression : name;
aoqi@0 670 _min_value = min_value;
aoqi@0 671 _max_value = max_value;
aoqi@0 672 assert(_min_value >= 0 && _min_value <= Expr::Max, "value out of range");
aoqi@0 673 assert(_max_value >= 0 && _max_value <= Expr::Max, "value out of range");
aoqi@0 674 }
aoqi@0 675
aoqi@0 676 Expr *Expr::clone() const {
aoqi@0 677 Expr *cost = new Expr();
aoqi@0 678 cost->_external_name = _external_name;
aoqi@0 679 cost->_expr = _expr;
aoqi@0 680 cost->_min_value = _min_value;
aoqi@0 681 cost->_max_value = _max_value;
aoqi@0 682
aoqi@0 683 return cost;
aoqi@0 684 }
aoqi@0 685
aoqi@0 686 void Expr::add(const Expr *c) {
aoqi@0 687 // Do not update fields until all computation is complete
aoqi@0 688 const char *external = compute_external(this, c);
aoqi@0 689 const char *expr = compute_expr(this, c);
aoqi@0 690 int min_value = compute_min (this, c);
aoqi@0 691 int max_value = compute_max (this, c);
aoqi@0 692
aoqi@0 693 _external_name = external;
aoqi@0 694 _expr = expr;
aoqi@0 695 _min_value = min_value;
aoqi@0 696 _max_value = max_value;
aoqi@0 697 }
aoqi@0 698
aoqi@0 699 void Expr::add(const char *c) {
aoqi@0 700 Expr *cost = new Expr(c);
aoqi@0 701 add(cost);
aoqi@0 702 }
aoqi@0 703
aoqi@0 704 void Expr::add(const char *c, ArchDesc &AD) {
aoqi@0 705 const Expr *e = AD.globalDefs()[c];
aoqi@0 706 if( e != NULL ) {
aoqi@0 707 // use the value of 'c' defined in <arch>.ad
aoqi@0 708 add(e);
aoqi@0 709 } else {
aoqi@0 710 Expr *cost = new Expr(c);
aoqi@0 711 add(cost);
aoqi@0 712 }
aoqi@0 713 }
aoqi@0 714
aoqi@0 715 const char *Expr::compute_external(const Expr *c1, const Expr *c2) {
aoqi@0 716 const char * result = NULL;
aoqi@0 717
aoqi@0 718 // Preserve use of external name which has a zero value
aoqi@0 719 if( c1->_external_name != NULL ) {
aoqi@0 720 sprintf( string_buffer, "%s", c1->as_string());
aoqi@0 721 if( !c2->is_zero() ) {
aoqi@0 722 strcat( string_buffer, "+");
aoqi@0 723 strcat( string_buffer, c2->as_string());
aoqi@0 724 }
aoqi@0 725 result = strdup(string_buffer);
aoqi@0 726 }
aoqi@0 727 else if( c2->_external_name != NULL ) {
aoqi@0 728 if( !c1->is_zero() ) {
aoqi@0 729 sprintf( string_buffer, "%s", c1->as_string());
aoqi@0 730 strcat( string_buffer, " + ");
aoqi@0 731 } else {
aoqi@0 732 string_buffer[0] = '\0';
aoqi@0 733 }
aoqi@0 734 strcat( string_buffer, c2->_external_name );
aoqi@0 735 result = strdup(string_buffer);
aoqi@0 736 }
aoqi@0 737 return result;
aoqi@0 738 }
aoqi@0 739
aoqi@0 740 const char *Expr::compute_expr(const Expr *c1, const Expr *c2) {
aoqi@0 741 if( !c1->is_zero() ) {
aoqi@0 742 sprintf( string_buffer, "%s", c1->_expr);
aoqi@0 743 if( !c2->is_zero() ) {
aoqi@0 744 strcat( string_buffer, "+");
aoqi@0 745 strcat( string_buffer, c2->_expr);
aoqi@0 746 }
aoqi@0 747 }
aoqi@0 748 else if( !c2->is_zero() ) {
aoqi@0 749 sprintf( string_buffer, "%s", c2->_expr);
aoqi@0 750 }
aoqi@0 751 else {
aoqi@0 752 sprintf( string_buffer, "0");
aoqi@0 753 }
aoqi@0 754 char *cost = strdup(string_buffer);
aoqi@0 755
aoqi@0 756 return cost;
aoqi@0 757 }
aoqi@0 758
aoqi@0 759 int Expr::compute_min(const Expr *c1, const Expr *c2) {
aoqi@0 760 int result = c1->_min_value + c2->_min_value;
aoqi@0 761 assert( result >= 0, "Invalid cost computation");
aoqi@0 762
aoqi@0 763 return result;
aoqi@0 764 }
aoqi@0 765
aoqi@0 766 int Expr::compute_max(const Expr *c1, const Expr *c2) {
aoqi@0 767 int result = c1->_max_value + c2->_max_value;
aoqi@0 768 if( result < 0 ) { // check for overflow
aoqi@0 769 result = Expr::Max;
aoqi@0 770 }
aoqi@0 771
aoqi@0 772 return result;
aoqi@0 773 }
aoqi@0 774
aoqi@0 775 void Expr::print() const {
aoqi@0 776 if( _external_name != NULL ) {
aoqi@0 777 printf(" %s == (%s) === [%d, %d]\n", _external_name, _expr, _min_value, _max_value);
aoqi@0 778 } else {
aoqi@0 779 printf(" %s === [%d, %d]\n", _expr, _min_value, _max_value);
aoqi@0 780 }
aoqi@0 781 }
aoqi@0 782
aoqi@0 783 void Expr::print_define(FILE *fp) const {
aoqi@0 784 assert( _external_name != NULL, "definition does not have a name");
aoqi@0 785 assert( _min_value == _max_value, "Expect user definitions to have constant value");
aoqi@0 786 fprintf(fp, "#define %s (%s) \n", _external_name, _expr);
aoqi@0 787 fprintf(fp, "// value == %d \n", _min_value);
aoqi@0 788 }
aoqi@0 789
aoqi@0 790 void Expr::print_assert(FILE *fp) const {
aoqi@0 791 assert( _external_name != NULL, "definition does not have a name");
aoqi@0 792 assert( _min_value == _max_value, "Expect user definitions to have constant value");
aoqi@0 793 fprintf(fp, " assert( %s == %d, \"Expect (%s) to equal %d\");\n", _external_name, _min_value, _expr, _min_value);
aoqi@0 794 }
aoqi@0 795
aoqi@0 796 Expr *Expr::get_unknown() {
aoqi@0 797 if( Expr::_unknown_expr == NULL ) {
aoqi@0 798 Expr::_unknown_expr = new Expr();
aoqi@0 799 }
aoqi@0 800
aoqi@0 801 return Expr::_unknown_expr;
aoqi@0 802 }
aoqi@0 803
aoqi@0 804 bool Expr::init_buffers() {
aoqi@0 805 // Fill buffers with 0
aoqi@0 806 for( int i = 0; i < STRING_BUFFER_LENGTH; ++i ) {
aoqi@0 807 external_buffer[i] = '\0';
aoqi@0 808 string_buffer[i] = '\0';
aoqi@0 809 }
aoqi@0 810
aoqi@0 811 return true;
aoqi@0 812 }
aoqi@0 813
aoqi@0 814 bool Expr::check_buffers() {
aoqi@0 815 // returns 'true' if buffer use may have overflowed
aoqi@0 816 bool ok = true;
aoqi@0 817 for( int i = STRING_BUFFER_LENGTH - 100; i < STRING_BUFFER_LENGTH; ++i) {
aoqi@0 818 if( external_buffer[i] != '\0' || string_buffer[i] != '\0' ) {
aoqi@0 819 ok = false;
aoqi@0 820 assert( false, "Expr:: Buffer overflow");
aoqi@0 821 }
aoqi@0 822 }
aoqi@0 823
aoqi@0 824 return ok;
aoqi@0 825 }
aoqi@0 826
aoqi@0 827
aoqi@0 828 //------------------------------ExprDict---------------------------------------
aoqi@0 829 // Constructor
aoqi@0 830 ExprDict::ExprDict( CmpKey cmp, Hash hash, Arena *arena )
aoqi@0 831 : _expr(cmp, hash, arena), _defines() {
aoqi@0 832 }
aoqi@0 833 ExprDict::~ExprDict() {
aoqi@0 834 }
aoqi@0 835
aoqi@0 836 // Return # of name-Expr pairs in dict
aoqi@0 837 int ExprDict::Size(void) const {
aoqi@0 838 return _expr.Size();
aoqi@0 839 }
aoqi@0 840
aoqi@0 841 // define inserts the given key-value pair into the dictionary,
aoqi@0 842 // and records the name in order for later output, ...
aoqi@0 843 const Expr *ExprDict::define(const char *name, Expr *expr) {
aoqi@0 844 const Expr *old_expr = (*this)[name];
aoqi@0 845 assert(old_expr == NULL, "Implementation does not support redefinition");
aoqi@0 846
aoqi@0 847 _expr.Insert(name, expr);
aoqi@0 848 _defines.addName(name);
aoqi@0 849
aoqi@0 850 return old_expr;
aoqi@0 851 }
aoqi@0 852
aoqi@0 853 // Insert inserts the given key-value pair into the dictionary. The prior
aoqi@0 854 // value of the key is returned; NULL if the key was not previously defined.
aoqi@0 855 const Expr *ExprDict::Insert(const char *name, Expr *expr) {
aoqi@0 856 return (Expr*)_expr.Insert((void*)name, (void*)expr);
aoqi@0 857 }
aoqi@0 858
aoqi@0 859 // Finds the value of a given key; or NULL if not found.
aoqi@0 860 // The dictionary is NOT changed.
aoqi@0 861 const Expr *ExprDict::operator [](const char *name) const {
aoqi@0 862 return (Expr*)_expr[name];
aoqi@0 863 }
aoqi@0 864
aoqi@0 865 void ExprDict::print_defines(FILE *fp) {
aoqi@0 866 fprintf(fp, "\n");
aoqi@0 867 const char *name = NULL;
aoqi@0 868 for( _defines.reset(); (name = _defines.iter()) != NULL; ) {
aoqi@0 869 const Expr *expr = (const Expr*)_expr[name];
aoqi@0 870 assert( expr != NULL, "name in ExprDict without matching Expr in dictionary");
aoqi@0 871 expr->print_define(fp);
aoqi@0 872 }
aoqi@0 873 }
aoqi@0 874 void ExprDict::print_asserts(FILE *fp) {
aoqi@0 875 fprintf(fp, "\n");
aoqi@0 876 fprintf(fp, " // Following assertions generated from definition section\n");
aoqi@0 877 const char *name = NULL;
aoqi@0 878 for( _defines.reset(); (name = _defines.iter()) != NULL; ) {
aoqi@0 879 const Expr *expr = (const Expr*)_expr[name];
aoqi@0 880 assert( expr != NULL, "name in ExprDict without matching Expr in dictionary");
aoqi@0 881 expr->print_assert(fp);
aoqi@0 882 }
aoqi@0 883 }
aoqi@0 884
aoqi@0 885 // Print out the dictionary contents as key-value pairs
aoqi@0 886 static void dumpekey(const void* key) { fprintf(stdout, "%s", (char*) key); }
aoqi@0 887 static void dumpexpr(const void* expr) { fflush(stdout); ((Expr*)expr)->print(); }
aoqi@0 888
aoqi@0 889 void ExprDict::dump() {
aoqi@0 890 _expr.print(dumpekey, dumpexpr);
aoqi@0 891 }
aoqi@0 892
aoqi@0 893
aoqi@0 894 //------------------------------ExprDict::private------------------------------
aoqi@0 895 // Disable public use of constructor, copy-ctor, operator =, operator ==
aoqi@0 896 ExprDict::ExprDict( ) : _expr(cmpkey,hashkey), _defines() {
aoqi@0 897 assert( false, "NotImplemented");
aoqi@0 898 }
aoqi@0 899 ExprDict::ExprDict( const ExprDict & ) : _expr(cmpkey,hashkey), _defines() {
aoqi@0 900 assert( false, "NotImplemented");
aoqi@0 901 }
aoqi@0 902 ExprDict &ExprDict::operator =( const ExprDict &rhs) {
aoqi@0 903 assert( false, "NotImplemented");
aoqi@0 904 _expr = rhs._expr;
aoqi@0 905 return *this;
aoqi@0 906 }
aoqi@0 907 // == compares two dictionaries; they must have the same keys (their keys
aoqi@0 908 // must match using CmpKey) and they must have the same values (pointer
aoqi@0 909 // comparison). If so 1 is returned, if not 0 is returned.
aoqi@0 910 bool ExprDict::operator ==(const ExprDict &d) const {
aoqi@0 911 assert( false, "NotImplemented");
aoqi@0 912 return false;
aoqi@0 913 }
aoqi@0 914
aoqi@0 915
aoqi@0 916 //------------------------------Production-------------------------------------
aoqi@0 917 Production::Production(const char *result, const char *constraint, const char *valid) {
aoqi@0 918 initialize();
aoqi@0 919 _result = result;
aoqi@0 920 _constraint = constraint;
aoqi@0 921 _valid = valid;
aoqi@0 922 }
aoqi@0 923
aoqi@0 924 void Production::initialize() {
aoqi@0 925 _result = NULL;
aoqi@0 926 _constraint = NULL;
aoqi@0 927 _valid = knownInvalid;
aoqi@0 928 _cost_lb = Expr::get_unknown();
aoqi@0 929 _cost_ub = Expr::get_unknown();
aoqi@0 930 }
aoqi@0 931
aoqi@0 932 void Production::print() {
aoqi@0 933 printf("%s", (_result == NULL ? "NULL" : _result ) );
aoqi@0 934 printf("%s", (_constraint == NULL ? "NULL" : _constraint ) );
aoqi@0 935 printf("%s", (_valid == NULL ? "NULL" : _valid ) );
aoqi@0 936 _cost_lb->print();
aoqi@0 937 _cost_ub->print();
aoqi@0 938 }
aoqi@0 939
aoqi@0 940
aoqi@0 941 //------------------------------ProductionState--------------------------------
aoqi@0 942 void ProductionState::initialize() {
aoqi@0 943 _constraint = noConstraint;
aoqi@0 944
aoqi@0 945 // reset each Production currently in the dictionary
aoqi@0 946 DictI iter( &_production );
aoqi@0 947 const void *x, *y = NULL;
aoqi@0 948 for( ; iter.test(); ++iter) {
aoqi@0 949 x = iter._key;
aoqi@0 950 y = iter._value;
aoqi@0 951 Production *p = (Production*)y;
aoqi@0 952 if( p != NULL ) {
aoqi@0 953 p->initialize();
aoqi@0 954 }
aoqi@0 955 }
aoqi@0 956 }
aoqi@0 957
aoqi@0 958 Production *ProductionState::getProduction(const char *result) {
aoqi@0 959 Production *p = (Production *)_production[result];
aoqi@0 960 if( p == NULL ) {
aoqi@0 961 p = new Production(result, _constraint, knownInvalid);
aoqi@0 962 _production.Insert(result, p);
aoqi@0 963 }
aoqi@0 964
aoqi@0 965 return p;
aoqi@0 966 }
aoqi@0 967
aoqi@0 968 void ProductionState::set_constraint(const char *constraint) {
aoqi@0 969 _constraint = constraint;
aoqi@0 970 }
aoqi@0 971
aoqi@0 972 const char *ProductionState::valid(const char *result) {
aoqi@0 973 return getProduction(result)->valid();
aoqi@0 974 }
aoqi@0 975
aoqi@0 976 void ProductionState::set_valid(const char *result) {
aoqi@0 977 Production *p = getProduction(result);
aoqi@0 978
aoqi@0 979 // Update valid as allowed by current constraints
aoqi@0 980 if( _constraint == noConstraint ) {
aoqi@0 981 p->_valid = knownValid;
aoqi@0 982 } else {
aoqi@0 983 if( p->_valid != knownValid ) {
aoqi@0 984 p->_valid = unknownValid;
aoqi@0 985 }
aoqi@0 986 }
aoqi@0 987 }
aoqi@0 988
aoqi@0 989 Expr *ProductionState::cost_lb(const char *result) {
aoqi@0 990 return getProduction(result)->cost_lb();
aoqi@0 991 }
aoqi@0 992
aoqi@0 993 Expr *ProductionState::cost_ub(const char *result) {
aoqi@0 994 return getProduction(result)->cost_ub();
aoqi@0 995 }
aoqi@0 996
aoqi@0 997 void ProductionState::set_cost_bounds(const char *result, const Expr *cost, bool has_state_check, bool has_cost_check) {
aoqi@0 998 Production *p = getProduction(result);
aoqi@0 999
aoqi@0 1000 if( p->_valid == knownInvalid ) {
aoqi@0 1001 // Our cost bounds are not unknown, just not defined.
aoqi@0 1002 p->_cost_lb = cost->clone();
aoqi@0 1003 p->_cost_ub = cost->clone();
aoqi@0 1004 } else if (has_state_check || _constraint != noConstraint) {
aoqi@0 1005 // The production is protected by a condition, so
aoqi@0 1006 // the cost bounds may expand.
aoqi@0 1007 // _cost_lb = min(cost, _cost_lb)
aoqi@0 1008 if( cost->less_than_or_equal(p->_cost_lb) ) {
aoqi@0 1009 p->_cost_lb = cost->clone();
aoqi@0 1010 }
aoqi@0 1011 // _cost_ub = max(cost, _cost_ub)
aoqi@0 1012 if( p->_cost_ub->less_than_or_equal(cost) ) {
aoqi@0 1013 p->_cost_ub = cost->clone();
aoqi@0 1014 }
aoqi@0 1015 } else if (has_cost_check) {
aoqi@0 1016 // The production has no condition check, but does
aoqi@0 1017 // have a cost check that could reduce the upper
aoqi@0 1018 // and/or lower bound.
aoqi@0 1019 // _cost_lb = min(cost, _cost_lb)
aoqi@0 1020 if( cost->less_than_or_equal(p->_cost_lb) ) {
aoqi@0 1021 p->_cost_lb = cost->clone();
aoqi@0 1022 }
aoqi@0 1023 // _cost_ub = min(cost, _cost_ub)
aoqi@0 1024 if( cost->less_than_or_equal(p->_cost_ub) ) {
aoqi@0 1025 p->_cost_ub = cost->clone();
aoqi@0 1026 }
aoqi@0 1027 } else {
aoqi@0 1028 // The costs are unconditionally set.
aoqi@0 1029 p->_cost_lb = cost->clone();
aoqi@0 1030 p->_cost_ub = cost->clone();
aoqi@0 1031 }
aoqi@0 1032
aoqi@0 1033 }
aoqi@0 1034
aoqi@0 1035 // Print out the dictionary contents as key-value pairs
aoqi@0 1036 static void print_key (const void* key) { fprintf(stdout, "%s", (char*) key); }
aoqi@0 1037 static void print_production(const void* production) { fflush(stdout); ((Production*)production)->print(); }
aoqi@0 1038
aoqi@0 1039 void ProductionState::print() {
aoqi@0 1040 _production.print(print_key, print_production);
aoqi@0 1041 }

mercurial