-
Notifications
You must be signed in to change notification settings - Fork 136
SSA optimizer enhancements #242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
a8695c2
to
9ecf518
Compare
cdd443a
to
98643c5
Compare
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. |
- 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)
/* 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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
This extends the SSA optimization framework with multiple optimization techniques that operate at the basic block level:
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.