C Rodrigues | 20 Jan 22:18 2013
Picon

Global constant propagation


I'm curious about global constant propagation in GHC.  It's a fairly basic optimization in the CFG-based
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 -funbox-strict-fields #-}

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 case-of-case transformation, GHC produces

main = let n = read "7"

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

Simon Peyton-Jones | 21 Jan 10:48 2013
Picon

RE: Global constant propagation

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: glasgow-haskell-users-bounces <at> haskell.org [mailto:glasgow-haskell-users-
|  bounces <at> haskell.org] On Behalf Of C Rodrigues
|  Sent: 20 January 2013 21:18
|  To: glasgow-haskell-users <at> haskell.org
|  Subject: Global constant propagation
|  
|  
|  I'm curious about global constant propagation in GHC.  It's a fairly basic
|  optimization in the CFG-based 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 -funbox-strict-fields #-}
|  
(Continue reading)


Gmane