1;; rewrites for integer and floating-point arithmetic
2;; eg: `iadd`, `isub`, `ineg`, `imul`, `fadd`, `fsub`, `fmul`
3
4;; For commutative instructions, we depend on cprop.isle pushing immediates to
5;; the right, and thus only simplify patterns like `x+0`, not `0+x`.
6
7;; x+0 == x.
8(rule (simplify (iadd ty
9                      x
10                      (iconst_u ty 0)))
11      (subsume x))
12;; x-0 == x.
13(rule (simplify (isub ty
14                      x
15                      (iconst_u ty 0)))
16      (subsume x))
17;; 0-x == (ineg x).
18(rule (simplify (isub ty
19                      (iconst_u ty 0)
20                      x))
21      (ineg ty x))
22
23;; x + -y == -y + x == -(y - x) == x - y
24(rule (simplify (iadd ty x (ineg ty y)))
25      (isub ty x y))
26(rule (simplify (iadd ty (ineg ty y) x))
27      (isub ty x y))
28(rule (simplify (ineg ty (isub ty y x)))
29      (isub ty x y))
30;; x - -y == x + y
31(rule (simplify (isub ty x (ineg ty y)))
32      (iadd ty x y))
33
34;; ineg(ineg(x)) == x.
35(rule (simplify (ineg ty (ineg ty x))) (subsume x))
36
37;; ineg(x) * ineg(y) == x*y.
38(rule (simplify (imul ty (ineg ty x) (ineg ty y)))
39      (subsume (imul ty x y)))
40
41;; iabs(ineg(x)) == iabs(x).
42(rule (simplify (iabs ty (ineg ty x)))
43      (iabs ty x))
44
45;; iabs(iabs(x)) == iabs(x).
46(rule (simplify (iabs ty inner @ (iabs ty x)))
47      (subsume inner))
48
49;; x-x == 0.
50(rule (simplify (isub (ty_int ty) x x)) (subsume (iconst_u ty 0)))
51
52;; x*1 == x.
53(rule (simplify (imul ty
54                      x
55                      (iconst_u ty 1)))
56      (subsume x))
57
58;; x*0 == 0.
59(rule (simplify (imul ty
60                      _
61                      zero @ (iconst_u ty 0)))
62      (subsume zero))
63
64;; x*-1 == ineg(x).
65(rule (simplify (imul ty x (iconst_s ty -1)))
66      (ineg ty x))
67
68;; (!x) + 1 == ineg(x)
69(rule (simplify (iadd ty (bnot ty x) (iconst_u ty 1)))
70      (ineg ty x))
71
72;; !(x - 1) == !(x + (-1)) == ineg(x)
73(rule (simplify (bnot ty (isub ty x (iconst_s ty 1))))
74      (ineg ty x))
75(rule (simplify (bnot ty (iadd ty x (iconst_s ty -1))))
76      (ineg ty x))
77
78;; x/1 == x.
79(rule (simplify_skeleton (sdiv x (iconst_s ty 1))) x)
80(rule (simplify_skeleton (udiv x (iconst_u ty 1))) x)
81
82;; TODO: strength reduction: div to shifts
83;; TODO: div/rem by constants -> magic multiplications
84
85;; x*2 == x+x.
86(rule (simplify (imul ty x (iconst_u _ 2)))
87      (iadd ty x x))
88
89;; x*c == x<<log2(c) when c is a power of two.
90;; Note that the type of `iconst` must be the same as the type of `imul`,
91;; so these rules can only fire in situations where it's safe to construct an
92;; `iconst` of that type.
93(rule (simplify (imul ty x (iconst _ (imm64_power_of_two c))))
94      (ishl ty x (iconst ty (imm64 c))))
95(rule (simplify (imul ty (iconst _ (imm64_power_of_two c)) x))
96      (ishl ty x (iconst ty (imm64 c))))
97
98;; fneg(fneg(x)) == x.
99(rule (simplify (fneg ty (fneg ty x))) (subsume x))
100
101;; If both of the multiplied arguments to an `fma` are negated then remove
102;; both of them since they cancel out.
103(rule (simplify (fma ty (fneg ty x) (fneg ty y) z))
104      (fma ty x y z))
105
106;; If both of the multiplied arguments to an `fmul` are negated then remove
107;; both of them since they cancel out.
108(rule (simplify (fmul ty (fneg ty x) (fneg ty y)))
109      (fmul ty x y))
110
111;; (a op (b op (c op d))) ==> ((a op b) op (c op d))
112;;
113;; and
114;;
115;; (((a op b) op c) op d) ==> ((a op b) op (c op d))
116;;
117;; where `op` is an associative operation: `iadd`, `imul`, `band`, or `bxor`.
118;;
119;; This increases instruction-level parallelism and shrinks live ranges. It also
120;; canonicalizes into the shallow-and-wide form for reassociating constants
121;; together for cprop.
122;;
123;; NB: We subsume to avoid exponential e-node blow up due to reassociating very
124;; large chains of operations.
125;;
126;; TODO: We should add `bor` rules for this as well. Unfortunately, they
127;; conflict with our `bswap` recognizing rules when we `subsume`.
128
129(rule (simplify (iadd ty a (iadd ty b (iadd ty c d))))
130      (subsume (iadd ty (iadd ty a b) (iadd ty c d))))
131(rule (simplify (iadd ty (iadd ty (iadd ty a b) c) d))
132      (subsume (iadd ty (iadd ty a b) (iadd ty c d))))
133
134(rule (simplify (imul ty a (imul ty b (imul ty c d))))
135      (subsume (imul ty (imul ty a b) (imul ty c d))))
136(rule (simplify (imul ty (imul ty (imul ty a b) c) d))
137      (subsume (imul ty (imul ty a b) (imul ty c d))))
138
139(rule (simplify (band ty a (band ty b (band ty c d))))
140      (subsume (band ty (band ty a b) (band ty c d))))
141(rule (simplify (band ty (band ty (band ty a b) c) d))
142      (subsume (band ty (band ty a b) (band ty c d))))
143
144(rule (simplify (bxor ty a (bxor ty b (bxor ty c d))))
145      (subsume (bxor ty (bxor ty a b) (bxor ty c d))))
146(rule (simplify (bxor ty (bxor ty (bxor ty a b) c) d))
147      (subsume (bxor ty (bxor ty a b) (bxor ty c d))))
148
149
150;; Similar rules but for associating combinations of + and -
151
152;; a -(b-(c-d)) = (a-b) + (c-d)
153(rule (simplify (isub ty a (isub ty b (isub ty c d))))
154      (subsume (iadd ty (isub ty a b) (isub ty c d))))
155
156;; a -(b-(c+d)) = (a-b) + (c+d)
157(rule (simplify (isub ty a (isub ty b (iadd ty c d))))
158      (subsume (iadd ty (isub ty a b) (iadd ty c d))))
159
160;; a -(b+(c-d)) = (a-b) - (c-d)
161(rule (simplify (isub ty a (iadd ty b (isub ty c d))))
162      (subsume (isub ty (isub ty a b) (isub ty c d))))
163
164;; a -(b+(c+d)) = (a-b) - (c+d)
165(rule (simplify (isub ty a (iadd ty b (iadd ty c d))))
166      (subsume (isub ty (isub ty a b) (iadd ty c d))))
167
168;; a +(b-(c-d)) = (a+b) - (c-d)
169(rule (simplify (iadd ty a (isub ty b (isub ty c d))))
170      (subsume (isub ty (iadd ty a b) (isub ty c d))))
171
172;; a +(b-(c+d)) = (a+b) - (c+d)
173(rule (simplify (iadd ty a (isub ty b (iadd ty c d))))
174      (subsume (isub ty (iadd ty a b) (iadd ty c d))))
175
176;; a +(b+(c-d)) = (a+b) + (c-d)
177(rule (simplify (iadd ty a (iadd ty b (isub ty c d))))
178      (subsume (iadd ty (iadd ty a b) (isub ty c d))))
179
180;; and nested the other way
181
182;; ((a-b)-c)-d = (a-b) - (c+d)
183(rule (simplify (isub ty (isub ty (isub ty a b) c) d))
184      (subsume (isub ty (isub ty a b) (iadd ty c d))))
185
186;; ((a-b)-c)+d = (a-b) - (c-d)
187(rule (simplify (iadd ty (isub ty (isub ty a b) c) d))
188      (subsume (isub ty (isub ty a b) (isub ty c d))))
189
190;; ((a-b)+c)-d = (a-b) + (c-d)
191(rule (simplify (isub ty (iadd ty (isub ty a b) c) d))
192      (subsume (iadd ty (isub ty a b) (isub ty c d))))
193
194;; ((a-b)+c)+d = (a-b) + (c+d)
195(rule (simplify (iadd ty (iadd ty (isub ty a b) c) d))
196      (subsume (iadd ty (isub ty a b) (iadd ty c d))))
197
198;; ((a+b)-c)-d = (a+b) - (c+d)
199(rule (simplify (isub ty (isub ty (iadd ty a b) c) d))
200      (subsume (isub ty (iadd ty a b) (iadd ty c d))))
201
202;; ((a+b)-c)+d = (a+b) - (c-d)
203(rule (simplify (iadd ty (isub ty (iadd ty a b) c) d))
204      (subsume (isub ty (iadd ty a b) (isub ty c d))))
205
206;; ((a+b)+c)-d = (a+b) + (c-d)
207(rule (simplify (isub ty (iadd ty (iadd ty a b) c) d))
208      (subsume (iadd ty (iadd ty a b) (isub ty c d))))
209
210;; Detect people open-coding `mulhi`: (x as big * y as big) >> bits
211;; LLVM doesn't have an intrinsic for it, so you'll see it in code like
212;; <https://github.com/rust-lang/rust/blob/767453eb7ca188e991ac5568c17b984dd4893e77/library/core/src/num/mod.rs#L174-L180>
213(rule (simplify (sshr ty (imul ty (sextend _ x@(value_type half_ty))
214                                  (sextend _ y@(value_type half_ty)))
215                         (iconst_u _ k)))
216      (if-let true (ty_equal half_ty (ty_half_width ty)))
217      (if-let true (u64_eq k (ty_bits_u64 half_ty)))
218      (sextend ty (smulhi half_ty x y)))
219(rule (simplify (ushr ty (imul ty (uextend _ x@(value_type half_ty))
220                                  (uextend _ y@(value_type half_ty)))
221                         (iconst_u _ k)))
222      (if-let true (ty_equal half_ty (ty_half_width ty)))
223      (if-let true (u64_eq k (ty_bits_u64 half_ty)))
224      (uextend ty (umulhi half_ty x y)))
225
226;; Cranelift's `fcvt_from_{u,s}int` instructions are polymorphic over the input
227;; type so remove any unnecessary `uextend` or `sextend` to give backends
228;; the chance to convert from the smallest integral type to the float. This
229;; can help lowerings on x64 for example which has a less efficient u64-to-float
230;; conversion than other bit widths.
231(rule (simplify (fcvt_from_uint ty (uextend _ val)))
232      (fcvt_from_uint ty val))
233(rule (simplify (fcvt_from_sint ty (sextend _ val)))
234      (fcvt_from_sint ty val))
235