Disabled const folding of ternary op when select is undef
authorClifford Wolf <clifford@clifford.at>
Thu, 7 Nov 2013 17:18:16 +0000 (18:18 +0100)
committerClifford Wolf <clifford@clifford.at>
Thu, 7 Nov 2013 17:18:16 +0000 (18:18 +0100)
frontends/ast/simplify.cc

index 7712023c56883ecaf2c50992a7535eff67219994..7b5ae064cc5fb9a2d76a825376b58427bd481acb 100644 (file)
@@ -1058,8 +1058,20 @@ skip_dynamic_range_lvalue_expansion:;
                        break;
                case AST_TERNARY:
                        if (children[0]->type == AST_CONSTANT) {
-                               AstNode *choice = children[children[0]->integer ? 1 : 2];
-                               if (choice->type == AST_CONSTANT) {
+                               bool found_sure_true = false;
+                               bool found_maybe_true = false;
+                               for (auto &bit : children[0]->bits) {
+                                       if (bit == RTLIL::State::S1)
+                                               found_sure_true = true;
+                                       if (bit > RTLIL::State::S1)
+                                               found_maybe_true = true;
+                               }
+                               AstNode *choice = NULL;
+                               if (found_sure_true)
+                                       choice = children[1];
+                               else if (!found_maybe_true)
+                                       choice = children[2];
+                               if (choice != NULL && choice->type == AST_CONSTANT) {
                                        RTLIL::Const y = choice->bitsAsConst(width_hint, sign_hint);
                                        newNode = mkconst_bits(y.bits, sign_hint);
                                }