[GHC] #12984: Missed constant folding oportunities (associativity)

#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

#12984: Missed constant folding oportunities (associativity) -------------------------------------+------------------------------------- Reporter: hsyl20 | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by simonpj): Ideally, express it as a bunch of rewrite rules (`prelude/PrelRules.hs`). I don't know if that is conveniently possible for assoc/distrib laws. Don't forget to take care about overflow etc. You will know this better than I but computer arithmetic doesn't obey all the same laws as mathematics! -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12984#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12984: Missed constant folding oportunities (associativity) -------------------------------------+------------------------------------- Reporter: hsyl20 | Owner: Type: feature request | Status: patch Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2858 Wiki Page: | -------------------------------------+------------------------------------- Changes (by hsyl20): * status: new => patch * differential: => Phab:D2858 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12984#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12984: Missed constant folding oportunities (associativity) -------------------------------------+------------------------------------- Reporter: hsyl20 | Owner: Type: feature request | Status: patch Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2858 Wiki Page: | -------------------------------------+------------------------------------- Changes (by akio): * cc: akio (added) Comment: #9136 is about the same problem. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12984#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12984: Missed constant folding oportunities (associativity) -------------------------------------+------------------------------------- Reporter: hsyl20 | Owner: Type: feature request | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 Resolution: duplicate | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: #9136 | Differential Rev(s): Phab:D2858 Wiki Page: | -------------------------------------+------------------------------------- Changes (by hsyl20): * status: patch => closed * resolution: => duplicate * related: => #9136 Comment: @akio Indeed. Let's continue the discussion on the original ticket. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12984#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC