Merge branch 'master' into atomics
[ghc.git] / testsuite / tests / simplCore / should_compile / T7865.hs
1 module T7865 where
2
3 -- Our very expensive operation that we don't want to perform more than once
4 -- Don't inline it so we can see exactly where it's called in the core
5 {-# NOINLINE expensive #-}
6 expensive :: Int -> Int
7 expensive x = x * 100000
8
9 -- SpecConstr this function:
10 recursive :: [Int] -> (Int,Int) -> (Int,Int)
11 recursive list acc
12 = case list of
13 [] -> acc
14 (x:xs) ->
15 -- Our expensive tuple:
16 -- The case is here mainly so that the expensive let isn't floated out before SpecConstr.
17 let a' = case xs of
18 [] -> acc
19 (_:_) -> (let b = expensive x in (b * 2), x)
20 -- Use the expensive value once and recurse.
21 -- We recurse with (_:_:_) so that a specialisation is made for that pattern,
22 -- which simplifies the case xs above. This exposes the expensive let.
23 (p,q) = case a' of (p',q') -> recursive (x:x:xs) (q',p')
24
25 -- Use the expensive value again.
26 -- Our problem is that this shows up as a separate let-binding for expensive, instead of reusing
27 -- the already computed value from above.
28 in (p + fst a', q + snd a')
29