# What is the name of this code transformation that uses associativity of addition?

I am learning about compiler optimization technique and there is a technique that use associativity of addition to transform code.

For example, consider the following expression:

(a + b) + ((c + d) + e)


It can be transformed into:

a + (b + (c + (d + e)))


Does anybody know the name of this transformation?

• Note that floating point addition is not associative. ​ ​ ​ – user12859 Oct 27 '15 at 5:27
• Reordering maybe ? – Yves Daoust Oct 27 '15 at 11:59
• Note that that "optimization" is actually a pessimization in this case - in the first example an out-of-order or pipelined CPU can issue [(a+b), (c+d)], then [(c+d)+e] then [(a + b) + ((c + d) + e)], taking 3 add latencies, whereas in the second case it takes 5 add latencies (as each add requires the previous one to be complete). – TLW Oct 27 '15 at 19:47

According to LLVM it is called "Reassociate expressions" and uses the flag "-reassociate"

According to here:

-reassociate: Reassociate expressions

This pass reassociates commutative expressions in an order that is designed to promote better constant propagation, GCSE, LICM, PRE, etc.

For example: 4 + (x + 5) ⇒ x + (4 + 5)

In the implementation of this algorithm, constants are assigned rank = 0, function arguments are rank = 1, and other values are assigned ranks corresponding to the reverse post order traversal of current function (starting at 2), which effectively gives values in deep loops higher rank than values not in loops.