Missed constant folding oportunities (associativity)
Consider this simple example:
import System.Environment
main :: IO ()
main = do
args <- getArgs
print ((length args + 10) - 10)
Compiled with -O2, we get the following Core:
case GHC.Show.$wshowSignedInt
0#
(GHC.Prim.-# (GHC.Prim.+# ww2_a6Pd 10#) 10#)
(GHC.Types.[] @ Char)
of
I would expect GHC to perform constant folding there to remove the unnecessary operation.
Basically, for any expression composed of Int#/Word# +/-/* primops, I think we should use associativity and distributivity laws to push the constants in the outer operation. It would allow the "scrutinee constant folding" optimization to be applied more often too.
I have a patch that does this for case scrutinees, but where should I put it in GHC so that the optimization gets applied more generally?
Trac metadata
Trac field | Value |
---|---|
Version | 8.0.1 |
Type | FeatureRequest |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Compiler |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | |
Operating system | |
Architecture |