|
58 | 58 | (uextend bigty (bor smallty x y)))
|
59 | 59 | (rule (simplify (bxor bigty (uextend _ x@(value_type smallty)) (uextend _ y@(value_type smallty))))
|
60 | 60 | (uextend bigty (bxor smallty x y)))
|
| 61 | + |
| 62 | +;; Matches values where `ireducing` them will not actually introduce another |
| 63 | +;; instruction, since other rules will collapse them with the reduction. |
| 64 | +(decl pure multi will_simplify_with_ireduce (Value) Value) |
| 65 | +(rule (will_simplify_with_ireduce x@(uextend _ _)) x) |
| 66 | +(rule (will_simplify_with_ireduce x@(sextend _ _)) x) |
| 67 | +(rule (will_simplify_with_ireduce x@(iconst _ _)) x) |
| 68 | +(rule (will_simplify_with_ireduce x@(unary_op _ _ a)) |
| 69 | + (if-let _ (will_simplify_with_ireduce a)) |
| 70 | + x) |
| 71 | +(rule (will_simplify_with_ireduce x@(binary_op _ _ a b)) |
| 72 | + (if-let _ (will_simplify_with_ireduce a)) |
| 73 | + (if-let _ (will_simplify_with_ireduce b)) |
| 74 | + x) |
| 75 | + |
| 76 | +;; Matches values where the high bits of the input don't affect lower bits of |
| 77 | +;; the output, and thus the inputs can be reduced before the operation rather |
| 78 | +;; than doing the wide operation then reducing afterwards. |
| 79 | +(decl pure multi reducible_modular_op (Value) Value) |
| 80 | +(rule (reducible_modular_op x@(ineg _ _)) x) |
| 81 | +(rule (reducible_modular_op x@(bnot _ _)) x) |
| 82 | +(rule (reducible_modular_op x@(iadd _ _ _)) x) |
| 83 | +(rule (reducible_modular_op x@(isub _ _ _)) x) |
| 84 | +(rule (reducible_modular_op x@(imul _ _ _)) x) |
| 85 | +(rule (reducible_modular_op x@(bor _ _ _)) x) |
| 86 | +(rule (reducible_modular_op x@(bxor _ _ _)) x) |
| 87 | +(rule (reducible_modular_op x@(band _ _ _)) x) |
| 88 | + |
| 89 | +;; Replace `(small)(x OP y)` with `(small)x OP (small)y` in cases where that's |
| 90 | +;; legal and it reduces the total number of instructions since the reductions |
| 91 | +;; to the arguments simplify further. |
| 92 | +(rule (simplify (ireduce smallty val@(unary_op _ op x))) |
| 93 | + (if-let _ (reducible_modular_op val)) |
| 94 | + (if-let _ (will_simplify_with_ireduce x)) |
| 95 | + (unary_op smallty op (ireduce smallty x))) |
| 96 | +(rule (simplify (ireduce smallty val@(binary_op _ op x y))) |
| 97 | + (if-let _ (reducible_modular_op val)) |
| 98 | + (if-let _ (will_simplify_with_ireduce x)) |
| 99 | + (if-let _ (will_simplify_with_ireduce y)) |
| 100 | + (binary_op smallty op (ireduce smallty x) (ireduce smallty y))) |
0 commit comments