@@ -2046,15 +2046,19 @@ void optimize(void)
20462046 insn -> opcode = OP_assign ;
20472047 insn -> rs2 = NULL ;
20482048 }
2049- /* x / x = 1 (if x != 0) */
2049+ /* x / x = 1 (undefined if x = 0, but optimization is still
2050+ * safe)
2051+ */
20502052 else if (insn -> opcode == OP_div && insn -> rd ) {
20512053 insn -> opcode = OP_load_constant ;
20522054 insn -> rd -> is_const = true;
20532055 insn -> rd -> init_val = 1 ;
20542056 insn -> rs1 = NULL ;
20552057 insn -> rs2 = NULL ;
20562058 }
2057- /* x % x = 0 */
2059+ /* x % x = 0 (undefined if x = 0, but optimization is still
2060+ * safe)
2061+ */
20582062 else if (insn -> opcode == OP_mod && insn -> rd ) {
20592063 insn -> opcode = OP_load_constant ;
20602064 insn -> rd -> is_const = true;
@@ -2100,7 +2104,8 @@ void optimize(void)
21002104 }
21012105
21022106 /* Comprehensive algebraic simplifications with identity
2103- * operations */
2107+ * operations
2108+ */
21042109 if (insn -> rs2 && insn -> rs2 -> is_const && insn -> rd ) {
21052110 int val = insn -> rs2 -> init_val ;
21062111
@@ -2170,10 +2175,9 @@ void optimize(void)
21702175 insn -> rs1 = insn -> rs2 ;
21712176 insn -> rs2 = NULL ;
21722177 }
2173- /* 0 * x = 0, 0 & x = 0, 0 / x = 0 */
2178+ /* 0 * x = 0, 0 & x = 0 */
21742179 else if (insn -> opcode == OP_mul ||
2175- insn -> opcode == OP_bit_and ||
2176- insn -> opcode == OP_div ) {
2180+ insn -> opcode == OP_bit_and ) {
21772181 insn -> opcode = OP_load_constant ;
21782182 insn -> rd -> is_const = true;
21792183 insn -> rd -> init_val = 0 ;
@@ -2246,7 +2250,13 @@ void optimize(void)
22462250 }
22472251 }
22482252
2249- /* Strength reduction for division and modulo by power of 2 */
2253+ /* Strength reduction for division and modulo by power of 2.
2254+ * Note: This optimization assumes unsigned semantics or
2255+ * non-negative dividend values. For signed division with
2256+ * potentially negative dividends, this may produce different
2257+ * results than true division (rounding toward zero vs toward
2258+ * negative infinity).
2259+ */
22502260 if (insn -> rs2 && insn -> rs2 -> is_const ) {
22512261 int divisor = insn -> rs2 -> init_val ;
22522262
0 commit comments