Skip to content

Conversation

jserv
Copy link
Collaborator

@jserv jserv commented Aug 12, 2025

This extends the SSA optimization framework with multiple optimization techniques that operate at the basic block level:

  1. simple_sccp() - Implements Sparse Conditional Constant Propagation, performing constant propagation through assignments, constant folding for arithmetic and comparison operations, and branch folding when conditions are compile-time constants 2. eval_const_arithmetic() - Enhanced constant folding for binary operations including arithmetic (+, -, *, /, %), bitwise (<<, >>, &, |, ^), logical (&&, ||), and comparison operations (==, !=, <, <=, >, >=) with zero-division protection
  2. eval_const_unary() - Constant folding for unary operations including negation (-), bitwise NOT (~), and logical NOT (!)
  3. is_cse_candidate() - Identifies operations eligible for Common Subexpression Elimination across all binary operation types
  4. cse() - Extended Common Subexpression Elimination supporting general binary operations with commutative operation detection for add, mul, bit_and, bit_or, bit_xor, log_and, log_or, eq, and neq

Summary by Bito

This pull request enhances the SSA optimization framework by introducing Sparse Conditional Constant Propagation (SCCP), improved constant folding techniques, and Common Subexpression Elimination (CSE). These optimizations operate at the basic block level, significantly improving performance, efficiency, and robustness in handling constant values and reducing redundant computations.

@jserv jserv requested a review from DrXiao August 13, 2025 08:35
@jserv jserv force-pushed the improve-ssa branch 3 times, most recently from a8695c2 to 9ecf518 Compare August 13, 2025 10:44
@sysprog21 sysprog21 deleted a comment from bito-code-review bot Aug 13, 2025
@jserv jserv force-pushed the improve-ssa branch 2 times, most recently from cdd443a to 98643c5 Compare August 14, 2025 05:56
@ChAoSUnItY
Copy link
Collaborator

In practice, would constant sign extension / truncation also be handled by SCCP or other optimization units? If so, maybe it's good to implement them as well.

jserv added 8 commits August 17, 2025 17:50
- Add comprehensive constant folding for arithmetic, bitwise, logical,
  and comparison operations
- Include zero-division protection for division and modulo operations
- Add unary operation constant folding (negate, bit_not, log_not)
- Implement SCCP (Sparse Conditional Constant Propagation) optimization
  pass
- Add constant propagation through assignments
- Add constant folding for arithmetic and comparison operations
- Add branch folding when conditions are compile-time constants
- Include dead code elimination through unreachable branch removal
- Replace magic numbers with configuration constants
  (PHI_WORKLIST_SIZE, DCE_WORKLIST_SIZE)
- Add overflow protection for PHI worklist to prevent buffer overruns
- Improve safety checks in dead code elimination
- Add comprehensive CSE support for arithmetic, bitwise, logical, and
  comparison operations
- Support commutative operation detection (add, mul, and, or, xor, eq,
  neq)
- Maintain existing array access pattern optimization (add + read)
- Add safety checks to prevent CSE with global variables
- Add unreachable block detection using dominator analysis
- Implement conservative dead store elimination for immediate overwrites
- Add redundant assignment elimination (x = x patterns)
- Enhance DCE worklist algorithm with better bounds checking
- Simplify escape analysis to be maximally conservative
- Add instruction elimination metrics tracking
- Fix phi operand marking in DCE worklist processing
This enhances the SCCP optimizer with support for constant truncation
operations, providing multiple optimization strategies to handle
compile-time constant cast operations efficiently.

The optimization correctly handles patterns like:
  const %.t977, 300
  %.t978 = trunc %.t977, 1

And performs compile-time evaluation (e.g., 300 & 0xFF = 44 for char).
This extends the SCCP optimizer to handle compile-time constant sign
extension operations (OP_sign_ext), complementing the existing constant
truncation optimization.
- 8-bit to 32-bit sign extension (char to int)
- 16-bit to 32-bit sign extension (short to int)
- Proper sign bit propagation for negative values

When sign extension operations have constant operands, they are
evaluated at compile-time and replaced with direct constant loads,
eliminating runtime overhead.

Example transformations:
- char c = -1; int i = c; → i directly loads 0xFFFFFFFF (-1)
- char c = 127; int i = c; → i directly loads 0x0000007F (127)
Comment on lines +271 to +278
/* Remove the truncation instruction by converting it to
* NOP-like
*/
next_insn->opcode = OP_load_constant;
next_insn->rd->is_const = true;
next_insn->rd->init_val = result;
next_insn->rs1 = NULL;
next_insn->sz = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about processing it as following?

insn->next = next_insn->next;
if (next_insn->next)
    next_insn->next->prev = insn;

Instead of converting it to NOP-like instruction, we can directly link the previous and next instructions to skip this unused instruction.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of converting it to NOP-like instruction, we can directly link the previous and next instructions to skip this unused instruction.

Direct instruction linking was investigated, but it causes bootstrap failures.

This unified the constant folding logic for binary operations (add, sub,
mul) and comparison operations (eq, neq, lt, leq, gt, geq) into a single
case statement.
@jserv jserv requested a review from DrXiao August 17, 2025 15:01
@jserv jserv merged commit 0bd9bd9 into master Aug 19, 2025
12 checks passed
@jserv jserv deleted the improve-ssa branch August 19, 2025 02:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants