|
196 | 196 | (rule (simplify (fmul ty (fneg ty x) (fneg ty y))) |
197 | 197 | (fmul ty x y)) |
198 | 198 |
|
199 | | -;; (a op (b op (c op d))) ==> ((a op b) op (c op d)) |
200 | | -;; |
201 | | -;; and |
202 | | -;; |
203 | | -;; (((a op b) op c) op d) ==> ((a op b) op (c op d)) |
204 | | -;; |
205 | | -;; where `op` is an associative operation: `iadd`, `imul`, `band`, or `bxor`. |
206 | | -;; |
207 | | -;; This increases instruction-level parallelism and shrinks live ranges. It also |
208 | | -;; canonicalizes into the shallow-and-wide form for reassociating constants |
209 | | -;; together for cprop. |
210 | | -;; |
211 | | -;; NB: We subsume to avoid exponential e-node blow up due to reassociating very |
212 | | -;; large chains of operations. |
213 | | -;; |
214 | | -;; TODO: We should add `bor` rules for this as well. Unfortunately, they |
215 | | -;; conflict with our `bswap` recognizing rules when we `subsume`. |
216 | | - |
217 | | -(rule (simplify (iadd ty a (iadd ty b (iadd ty c d)))) |
218 | | - (subsume (iadd ty (iadd ty a b) (iadd ty c d)))) |
219 | | -(rule (simplify (iadd ty (iadd ty (iadd ty a b) c) d)) |
220 | | - (subsume (iadd ty (iadd ty a b) (iadd ty c d)))) |
221 | | - |
222 | | -(rule (simplify (imul ty a (imul ty b (imul ty c d)))) |
223 | | - (subsume (imul ty (imul ty a b) (imul ty c d)))) |
224 | | -(rule (simplify (imul ty (imul ty (imul ty a b) c) d)) |
225 | | - (subsume (imul ty (imul ty a b) (imul ty c d)))) |
226 | | - |
227 | | -(rule (simplify (band ty a (band ty b (band ty c d)))) |
228 | | - (subsume (band ty (band ty a b) (band ty c d)))) |
229 | | -(rule (simplify (band ty (band ty (band ty a b) c) d)) |
230 | | - (subsume (band ty (band ty a b) (band ty c d)))) |
231 | | - |
232 | | -(rule (simplify (bxor ty a (bxor ty b (bxor ty c d)))) |
233 | | - (subsume (bxor ty (bxor ty a b) (bxor ty c d)))) |
234 | | -(rule (simplify (bxor ty (bxor ty (bxor ty a b) c) d)) |
235 | | - (subsume (bxor ty (bxor ty a b) (bxor ty c d)))) |
236 | | - |
237 | | - |
238 | | -;; Similar rules but for associating combinations of + and - |
239 | | - |
240 | | -;; a -(b-(c-d)) = (a-b) + (c-d) |
241 | | -(rule (simplify (isub ty a (isub ty b (isub ty c d)))) |
242 | | - (subsume (iadd ty (isub ty a b) (isub ty c d)))) |
243 | | - |
244 | | -;; a -(b-(c+d)) = (a-b) + (c+d) |
245 | | -(rule (simplify (isub ty a (isub ty b (iadd ty c d)))) |
246 | | - (subsume (iadd ty (isub ty a b) (iadd ty c d)))) |
247 | | - |
248 | | -;; a -(b+(c-d)) = (a-b) - (c-d) |
249 | | -(rule (simplify (isub ty a (iadd ty b (isub ty c d)))) |
250 | | - (subsume (isub ty (isub ty a b) (isub ty c d)))) |
251 | | - |
252 | | -;; a -(b+(c+d)) = (a-b) - (c+d) |
253 | | -(rule (simplify (isub ty a (iadd ty b (iadd ty c d)))) |
254 | | - (subsume (isub ty (isub ty a b) (iadd ty c d)))) |
255 | | - |
256 | | -;; a +(b-(c-d)) = (a+b) - (c-d) |
257 | | -(rule (simplify (iadd ty a (isub ty b (isub ty c d)))) |
258 | | - (subsume (isub ty (iadd ty a b) (isub ty c d)))) |
259 | | - |
260 | | -;; a +(b-(c+d)) = (a+b) - (c+d) |
261 | | -(rule (simplify (iadd ty a (isub ty b (iadd ty c d)))) |
262 | | - (subsume (isub ty (iadd ty a b) (iadd ty c d)))) |
263 | | - |
264 | | -;; a +(b+(c-d)) = (a+b) + (c-d) |
265 | | -(rule (simplify (iadd ty a (iadd ty b (isub ty c d)))) |
266 | | - (subsume (iadd ty (iadd ty a b) (isub ty c d)))) |
267 | | - |
268 | | -;; and nested the other way |
269 | | - |
270 | | -;; ((a-b)-c)-d = (a-b) - (c+d) |
271 | | -(rule (simplify (isub ty (isub ty (isub ty a b) c) d)) |
272 | | - (subsume (isub ty (isub ty a b) (iadd ty c d)))) |
273 | | - |
274 | | -;; ((a-b)-c)+d = (a-b) - (c-d) |
275 | | -(rule (simplify (iadd ty (isub ty (isub ty a b) c) d)) |
276 | | - (subsume (isub ty (isub ty a b) (isub ty c d)))) |
277 | | - |
278 | | -;; ((a-b)+c)-d = (a-b) + (c-d) |
279 | | -(rule (simplify (isub ty (iadd ty (isub ty a b) c) d)) |
280 | | - (subsume (iadd ty (isub ty a b) (isub ty c d)))) |
281 | | - |
282 | | -;; ((a-b)+c)+d = (a-b) + (c+d) |
283 | | -(rule (simplify (iadd ty (iadd ty (isub ty a b) c) d)) |
284 | | - (subsume (iadd ty (isub ty a b) (iadd ty c d)))) |
285 | | - |
286 | | -;; ((a+b)-c)-d = (a+b) - (c+d) |
287 | | -(rule (simplify (isub ty (isub ty (iadd ty a b) c) d)) |
288 | | - (subsume (isub ty (iadd ty a b) (iadd ty c d)))) |
289 | | - |
290 | | -;; ((a+b)-c)+d = (a+b) - (c-d) |
291 | | -(rule (simplify (iadd ty (isub ty (iadd ty a b) c) d)) |
292 | | - (subsume (isub ty (iadd ty a b) (isub ty c d)))) |
293 | | - |
294 | | -;; ((a+b)+c)-d = (a+b) + (c-d) |
295 | | -(rule (simplify (isub ty (iadd ty (iadd ty a b) c) d)) |
296 | | - (subsume (iadd ty (iadd ty a b) (isub ty c d)))) |
297 | | - |
298 | 199 | ;; Detect people open-coding `mulhi`: (x as big * y as big) >> bits |
299 | 200 | ;; LLVM doesn't have an intrinsic for it, so you'll see it in code like |
300 | 201 | ;; <https://github.com/rust-lang/rust/blob/767453eb7ca188e991ac5568c17b984dd4893e77/library/core/src/num/mod.rs#L174-L180> |
|
0 commit comments