From 26d1c72e771dec84ed4cd923f7e22d0e8803525e Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Wed, 12 Jul 2023 04:37:29 -0700 Subject: [PATCH] Avoid exponential execution time behavior in arith_expr_type() arith_expr_type() queries the expression type of its two child nodes up to two times. Since the child nodes might also need to query their child nodes expression type to determine their own this can lead to an exponential runtime. For complex expressions this can easily result in very long elaboration time. Avoid this by querying the expression type only once for each child node. Signed-off-by: Lars-Peter Clausen --- net_expr.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/net_expr.cc b/net_expr.cc index 6460302fb..c9a5db115 100644 --- a/net_expr.cc +++ b/net_expr.cc @@ -114,12 +114,15 @@ NetEBAdd::~NetEBAdd() static ivl_variable_type_t arith_expr_type(const NetExpr *l, const NetExpr *r) { - if (l->expr_type() == IVL_VT_REAL || - r->expr_type() == IVL_VT_REAL) + auto l_expr_type = l->expr_type(); + auto r_expr_type = r->expr_type(); + + if (l_expr_type == IVL_VT_REAL || + r_expr_type == IVL_VT_REAL) return IVL_VT_REAL; - if (l->expr_type() == IVL_VT_LOGIC || - r->expr_type() == IVL_VT_LOGIC) + if (l_expr_type == IVL_VT_LOGIC || + r_expr_type == IVL_VT_LOGIC) return IVL_VT_LOGIC; return IVL_VT_BOOL;