Merge in new code generator branch.
[ghc-hetmet.git] / compiler / cmm / OptimizationFuel.hs
1 {-# LANGUAGE TypeFamilies #-}
2 -- | Optimisation fuel is used to control the amount of work the optimiser does.
3 --
4 -- Every optimisation step consumes a certain amount of fuel and stops when
5 -- it runs out of fuel.  This can be used e.g. to debug optimiser bugs: Run
6 -- the optimiser with varying amount of fuel to find out the exact number of
7 -- steps where a bug is introduced in the output.
8 module OptimizationFuel
9     ( OptimizationFuel, amountOfFuel, tankFilledTo, anyFuelLeft, oneLessFuel
10     , OptFuelState, initOptFuelState
11     , FuelConsumer, FuelUsingMonad, FuelState
12     , fuelGet, fuelSet, lastFuelPass, setFuelPass
13     , fuelExhausted, fuelDec1, tryWithFuel
14     , runFuelIO, fuelConsumingPass
15     , FuelUniqSM
16     , liftUniq
17     )
18 where
19
20 import Data.IORef
21 import Control.Monad
22 import StaticFlags (opt_Fuel)
23 import UniqSupply
24 import Panic ()
25
26 import Compiler.Hoopl
27 import Compiler.Hoopl.GHC (getFuel, setFuel)
28
29 #include "HsVersions.h"
30
31
32 -- We limit the number of transactions executed using a record of flags
33 -- stored in an HscEnv. The flags store the name of the last optimization
34 -- pass and the amount of optimization fuel remaining.
35 data OptFuelState =
36   OptFuelState { pass_ref :: IORef String
37                , fuel_ref :: IORef OptimizationFuel
38                }
39 initOptFuelState :: IO OptFuelState
40 initOptFuelState =
41   do pass_ref' <- newIORef "unoptimized program"
42      fuel_ref' <- newIORef (tankFilledTo opt_Fuel)
43      return OptFuelState {pass_ref = pass_ref', fuel_ref = fuel_ref'}
44
45 type FuelConsumer a = OptimizationFuel -> (a, OptimizationFuel)
46
47 tankFilledTo :: Int -> OptimizationFuel
48 amountOfFuel :: OptimizationFuel -> Int
49
50 anyFuelLeft :: OptimizationFuel -> Bool
51 oneLessFuel :: OptimizationFuel -> OptimizationFuel
52
53 #ifdef DEBUG
54 newtype OptimizationFuel = OptimizationFuel Int
55   deriving Show
56
57 tankFilledTo = OptimizationFuel
58 amountOfFuel (OptimizationFuel f) = f
59
60 anyFuelLeft (OptimizationFuel f) = f > 0
61 oneLessFuel (OptimizationFuel f) = ASSERT (f > 0) (OptimizationFuel (f - 1))
62 #else
63 -- type OptimizationFuel = State# () -- would like this, but it won't work
64 data OptimizationFuel = OptimizationFuel
65   deriving Show
66 tankFilledTo _ = OptimizationFuel
67 amountOfFuel _ = maxBound
68
69 anyFuelLeft _ = True
70 oneLessFuel _ = OptimizationFuel
71 #endif
72
73 data FuelState = FuelState { fs_fuel :: OptimizationFuel, fs_lastpass :: String }
74 newtype FuelUniqSM a = FUSM { unFUSM :: FuelState -> UniqSM (a, FuelState) }
75
76 fuelConsumingPass :: String -> FuelConsumer a -> FuelUniqSM a
77 fuelConsumingPass name f = do setFuelPass name
78                               fuel <- fuelGet
79                               let (a, fuel') = f fuel
80                               fuelSet fuel'
81                               return a
82
83 runFuelIO :: OptFuelState -> FuelUniqSM a -> IO a
84 runFuelIO fs (FUSM f) =
85     do pass <- readIORef (pass_ref fs)
86        fuel <- readIORef (fuel_ref fs)
87        u    <- mkSplitUniqSupply 'u'
88        let (a, FuelState fuel' pass') = initUs_ u $ f (FuelState fuel pass)
89        writeIORef (pass_ref fs) pass'
90        writeIORef (fuel_ref fs) fuel'
91        return a
92
93 instance Monad FuelUniqSM where
94   FUSM f >>= k = FUSM (\s -> f s >>= \(a, s') -> unFUSM (k a) s')
95   return a     = FUSM (\s -> return (a, s))
96
97 instance MonadUnique FuelUniqSM where
98     getUniqueSupplyM = liftUniq getUniqueSupplyM
99     getUniqueM       = liftUniq getUniqueM
100     getUniquesM      = liftUniq getUniquesM
101
102 liftUniq :: UniqSM x -> FuelUniqSM x
103 liftUniq x = FUSM (\s -> x >>= (\u -> return (u, s)))
104
105 class Monad m => FuelUsingMonad m where
106   fuelGet      :: m OptimizationFuel
107   fuelSet      :: OptimizationFuel -> m ()
108   lastFuelPass :: m String
109   setFuelPass  :: String -> m ()
110
111 fuelExhausted :: FuelUsingMonad m => m Bool
112 fuelExhausted = fuelGet >>= return . anyFuelLeft
113
114 fuelDec1 :: FuelUsingMonad m => m ()
115 fuelDec1 = fuelGet >>= fuelSet . oneLessFuel
116
117 tryWithFuel :: FuelUsingMonad m => a -> m (Maybe a)
118 tryWithFuel r = do f <- fuelGet
119                    if anyFuelLeft f then fuelSet (oneLessFuel f) >> return (Just r)
120                                     else return Nothing
121
122 instance FuelUsingMonad FuelUniqSM where
123   fuelGet          = extract fs_fuel
124   lastFuelPass     = extract fs_lastpass
125   fuelSet fuel     = FUSM (\s -> return ((), s { fs_fuel     = fuel }))
126   setFuelPass pass = FUSM (\s -> return ((), s { fs_lastpass = pass }))
127
128 extract :: (FuelState -> a) -> FuelUniqSM a
129 extract f = FUSM (\s -> return (f s, s))
130
131 instance FuelMonad FuelUniqSM where
132   getFuel = liftM amountOfFuel fuelGet
133   setFuel = fuelSet . tankFilledTo
134
135 -- Don't bother to checkpoint the unique supply; it doesn't matter
136 instance CheckpointMonad FuelUniqSM where
137     type Checkpoint FuelUniqSM = FuelState
138     checkpoint = FUSM $ \fuel -> return (fuel, fuel) 
139     restart fuel = FUSM $ \_ -> return ((), fuel)
140