Fixed algorithmic complexity of AST simplification of long expressions
authorClifford Wolf <clifford@clifford.at>
Mon, 20 Jan 2014 19:25:20 +0000 (20:25 +0100)
committerClifford Wolf <clifford@clifford.at>
Mon, 20 Jan 2014 19:25:20 +0000 (20:25 +0100)
frontends/ast/ast.cc
frontends/ast/ast.h
frontends/ast/simplify.cc

index ecc58cf63d658fee4c73ec0693d182feaafee8a5..20720051aa25507a9295e85ce48b07f1d27b2408 100644 (file)
@@ -181,6 +181,7 @@ AstNode::AstNode(AstNodeType type, AstNode *child1, AstNode *child2)
        range_right = 0;
        integer = 0;
        id2ast = NULL;
+       basic_prep = false;
 
        if (child1)
                children.push_back(child1);
index 6aaa90e86dd22eedf85fa99c05a1c01a05f9be99..14e7803bf5ba4dea14bd3fc456bd96373afa8798 100644 (file)
@@ -153,6 +153,9 @@ namespace AST
                // this is set by simplify and used during RTLIL generation
                AstNode *id2ast;
 
+               // this is used by simplify to detect if basic analysis has been performed already on the node
+               bool basic_prep;
+
                // this is the original sourcecode location that resulted in this AST node
                // it is automatically set by the constructor using AST::current_filename and
                // the AST::get_line_num() callback function.
index c266800e934eabb7b060afb31291b78d159b6221..bd5da14e3d3a2fb62e3c7c5166eeec826fda753a 100644 (file)
@@ -237,8 +237,8 @@ bool AstNode::simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage,
        case AST_ASSIGN_EQ:
        case AST_ASSIGN_LE:
        case AST_ASSIGN:
-               while (children[0]->simplify(false, false, true, stage, -1, false) == true) { }
-               while (children[1]->simplify(false, false, false, stage, -1, false) == true) { }
+               while (!children[0]->basic_prep && children[0]->simplify(false, false, true, stage, -1, false) == true) { }
+               while (!children[1]->basic_prep && children[1]->simplify(false, false, false, stage, -1, false) == true) { }
                children[0]->detectSignWidth(backup_width_hint, backup_sign_hint);
                children[1]->detectSignWidth(width_hint, sign_hint);
                width_hint = std::max(width_hint, backup_width_hint);
@@ -247,11 +247,11 @@ bool AstNode::simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage,
 
        case AST_PARAMETER:
        case AST_LOCALPARAM:
-               while (children[0]->simplify(false, false, false, stage, -1, false) == true) { }
+               while (!children[0]->basic_prep && children[0]->simplify(false, false, false, stage, -1, false) == true) { }
                children[0]->detectSignWidth(width_hint, sign_hint);
                if (children.size() > 1) {
                        assert(children[1]->type == AST_RANGE);
-                       while (children[1]->simplify(false, false, false, stage, -1, false) == true) { }
+                       while (!children[1]->basic_prep && children[1]->simplify(false, false, false, stage, -1, false) == true) { }
                        if (!children[1]->range_valid)
                                log_error("Non-constant width range on parameter decl at %s:%d.\n", filename.c_str(), linenum);
                        width_hint = std::max(width_hint, children[1]->range_left - children[1]->range_right + 1);
@@ -306,7 +306,7 @@ bool AstNode::simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage,
                width_hint = -1;
                sign_hint = true;
                for (auto child : children) {
-                       while (child->simplify(false, false, in_lvalue, stage, -1, false) == true) { }
+                       while (!child->basic_prep && child->simplify(false, false, in_lvalue, stage, -1, false) == true) { }
                        child->detectSignWidthWorker(width_hint, sign_hint);
                }
                reset_width_after_children = true;
@@ -336,7 +336,7 @@ bool AstNode::simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage,
 
        if (detect_width_simple && width_hint < 0) {
                for (auto child : children)
-                       while (child->simplify(false, false, in_lvalue, stage, -1, false) == true) { }
+                       while (!child->basic_prep && child->simplify(false, false, in_lvalue, stage, -1, false) == true) { }
                if (type == AST_REPLICATE)
                        while (children[0]->simplify(true, false, in_lvalue, stage, -1, false) == true) { }
                detectSignWidth(width_hint, sign_hint);
@@ -1425,6 +1425,9 @@ apply_newNode:
                did_something = true;
        }
 
+       if (!did_something)
+               basic_prep = true;
+
        return did_something;
 }