Global constant propagation
Simon PeytonJones
simonpj at microsoft.com
Mon Jan 21 10:48:11 CET 2013
Your example is complicated by the fact that k is overloaded (will work on any value in class Num), and in fact the numbers end up having type Integer, not Int.
But still, it is indeed like SpecConstr. Maybe we should generate a specialised version of 'k', specialised for k=0. That might be a worthwhile thing to do, and would be a fairly straightforward modification of the SpecConstr code, to deal with literals as well as constructors.
Simon
 Original Message
 From: glasgowhaskellusersbounces at haskell.org [mailto:glasgowhaskellusers
 bounces at haskell.org] On Behalf Of C Rodrigues
 Sent: 20 January 2013 21:18
 To: glasgowhaskellusers at haskell.org
 Subject: Global constant propagation


 I'm curious about global constant propagation in GHC. It's a fairly basic
 optimization in the CFGbased compiler domain, and it's similar to constructor
 specialization, but it doesn't seem to be in GHC's repertoire. Perhaps it's usually
 subsumed by other optimizations or it's more complicated than I am thinking. Is
 this optimization worth implementing?

 This optimization can help when a case expression returns a product, some fields
 of which are the same in all branches. The following program is a minimal
 example of an optimizable situation that GHC doesn't exploit.


 {# OPTIONS_GHC O3 funboxstrictfields #}


 data D = D !Int !Int


 foo n = if n > 0

 then D 0 0

 else D 0 n


 main =

 case foo $ read "7"

 of D x y > if x == 0 then return () else print y >> putStrLn "A"


 After inlining and caseofcase transformation, GHC produces


 main = let n = read "7"

 k x y = case x of {0 > return (); _ > print y >> putStrLn "A"}

 in if n > 0

 then k 0 0

 else k 0 n


 If the simplifier could discover that x is always 0, it could eliminate one parameter
 of 'k' and the case expression.


 _______________________________________________
 Glasgowhaskellusers mailing list
 Glasgowhaskellusers at haskell.org
 http://www.haskell.org/mailman/listinfo/glasgowhaskellusers
More information about the Glasgowhaskellusers
mailing list