Skip to content

Commit

Permalink
Further simplify rewriting of dynamic indexing
Browse files Browse the repository at this point in the history
This also corrects the calculation of bit widths, using the new
function min_bit_width.
  • Loading branch information
daglem committed Mar 6, 2024
1 parent 9e8dd90 commit f6bbe47
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions frontends/ast/simplify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2465,7 +2465,6 @@ bool AstNode::simplify(bool const_fold, int stage, int width_hint, bool sign_hin
newNode = lvalue;
} else {
// Shift to access the indexed bit slice.
// This is adapted from old code in genrtlil.cc
AstNode *rvalue = clone();
rvalue->delete_children();
if (member_node)
Expand All @@ -2477,22 +2476,15 @@ bool AstNode::simplify(bool const_fold, int stage, int width_hint, bool sign_hin

// Decode the index based on wire dimensions
int idx_signed_nbits = shift_expr_width_hint + !shift_expr_sign_hint;
int offset = !id2ast->range_swapped ? wire_offset : wire_width - result_width + wire_offset;
int raw_idx_nbits = 1 + std::max(idx_signed_nbits, min_bit_width(offset));
shift_expr = new AstNode(AST_TO_SIGNED,
new AstNode(AST_CAST_SIZE, node_int(raw_idx_nbits), shift_expr)
);
if (!id2ast->range_swapped) {
int raw_idx_nbits = 1 + std::max(idx_signed_nbits, ceil_log2(std::abs(wire_offset) + 1) + 1);
shift_expr = new AstNode(AST_SUB,
new AstNode(AST_TO_SIGNED,
new AstNode(AST_CAST_SIZE, node_int(raw_idx_nbits), shift_expr)
),
node_int(wire_offset));
shift_expr = new AstNode(AST_SUB, shift_expr, node_int(offset));
} else {
int offset = wire_width - result_width + wire_offset;
int raw_idx_nbits = 1 + std::max(idx_signed_nbits, ceil_log2(std::abs(offset)) + 1);

shift_expr = new AstNode(AST_SUB, node_int(offset),
new AstNode(AST_TO_SIGNED,
new AstNode(AST_CAST_SIZE, node_int(raw_idx_nbits), shift_expr)
)
);
shift_expr = new AstNode(AST_SUB, node_int(offset), shift_expr);
}

// Shift rvalue to the right so that the bit slice starts at bit 0.
Expand Down

0 comments on commit f6bbe47

Please sign in to comment.