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 7, 2024
1 parent 9a0559c commit 39cf48a
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 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,17 @@ 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 offset_signed_nbits = min_bit_width(offset);
int raw_idx_nbits = 1 + std::max(idx_signed_nbits, offset_signed_nbits);
AstNode *node_offset = mkconst_int(offset, true, offset_signed_nbits);
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_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_offset, shift_expr);
}

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

0 comments on commit 39cf48a

Please sign in to comment.