Enhanced constant folding
authorSylvain Henry <hsyl20@gmail.com>
Fri, 13 Apr 2018 17:29:07 +0000 (13:29 -0400)
committerBen Gamari <ben@smart-cactus.org>
Fri, 13 Apr 2018 20:25:43 +0000 (16:25 -0400)
commitfea04defa64871caab6339ff3fc5511a272f37c7
treed19cfb3062f3edb1229998f93c4e382ff366fd42
parent4b831c27926d643b0b6fad82c1e946d05cde8645
Enhanced constant folding

Until now GHC only supported basic constant folding (lit op lit, expr op
0, etc.).

This patch uses laws of +/-/* (associativity, commutativity,
distributivity) to support some constant folding into nested
expressions.

Examples of new transformations:

   - simple nesting: (10 + x) + 10 becomes 20 + x
   - deep nesting: 5 + x + (y + (z + (t + 5))) becomes 10 + (x + (y + (z + t)))
   - distribution: (5 + x) * 6 becomes 30 + 6*x
   - simple factorization: 5 + x + (x + (x + (x + 5))) becomes 10 + (4 *x)
   - siblings: (5 + 4*x) - (3*x + 2) becomes 3 + x

Test Plan: validate

Reviewers: simonpj, austin, bgamari

Reviewed By: bgamari

Subscribers: thomie

GHC Trac Issues: #9136

Differential Revision: https://phabricator.haskell.org/D2858
compiler/main/DynFlags.hs
compiler/prelude/PrelRules.hs
testsuite/tests/simplCore/should_compile/spec-inline.stderr