
#12984: Missed constant folding oportunities (associativity) -------------------------------------+------------------------------------- Reporter: hsyl20 | Owner: Type: feature | Status: new request | Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- Consider this simple example: {{{#!hs import System.Environment main :: IO () main = do args <- getArgs print ((length args + 10) - 10) }}} Compiled with -O2, we get the following Core: {{{#!hs 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? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12984 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler