-
Notifications
You must be signed in to change notification settings - Fork 641
Description
Hey,
Currently folding rules act on a instruction-by-instruction basis, which works for a lot of cases, but struggles to deal with temporaries in the middle of an expression.
e.g:
10 + a + 10 => a + 20
10 + b + a + 10 => no fold
(3 * a) + (-3 *a) => 0
(3 * a) + 1 + (-3 *a) => no fold
(3 * a) + (3 * b) => 3 * (a + b)
(3 * a) + c + (3 * b) => no fold
Because each rule would need to account for every possibility and combination, it's not very practical to
make rules for these sorts of situations.
Locally I've been working on a reassociation pass (for FP arithmetic) and was wondering if it is something that you would want (since it's not simply adding extra folding rules).
The general premise is to reassociate independant chains of: OpFAdd, OpFSub, OpFNegate, OpFMul, OpFDiv, OpVectorTimesScalar.
Constants are moved closer together e.g:
(A + 5 + B - C - 7) => (5 - 7 + A + B - C) => (-2 + A + B - C)
(A * 5 * B * C * 7) => (5 * 7 * A * B * C) => (35 * A * B * C)
Variables far apart can cancel each other out e.g:
(A + B + C - A - B) => C
(A * B * C / (A * B)) => C
Variables can be factored e.g:
(A * x * x + B * x + C) => x * (A * x + B) + C
A * x + B * x + C * x => x * (A + B + C)
With a few extra folding rules that operate on the chains directly.
Many thanks,
Alister