module Optimizer.Dataflow(getLiveVariables) where

import Datatypes
import Data.List


getLiveVariables :: [IR] -> [[Int]]
getLiveVariables ir =
  let live = foldr analyseLiveness [[]] ir
      shiftedLive = tail live ++ [[]] in 
  map (uncurry union) (zip live shiftedLive)


analyseLiveness :: IR -> [[Int]] -> [[Int]]
analyseLiveness (ThreeIR _ r1 r2 r3 _) (live:lives) =
  let gen = (toI r2) ++ (toI r3)
      kill = toI r1
      live' = gen `union` (live \\ kill)
  in live':live:lives

analyseLiveness (TwoIR r1 _ _) (live:lives) =
  let kill = toI r1
      live' = live \\ kill
  in live':live:lives

analyseLiveness _ lives = lives


toI :: IRItem -> [Int]
toI (R r)
  | r < 7 = []
  | otherwise = [r]

toI _ = []

module Optimizer.Dataflow(getLiveVariables) where

import Datatypes
import Data.List
import Debug.Trace


getLiveVariables :: [IR] -> [[Int]]
getLiveVariables ir =
  map (uncurry union) (zip live shiftedLive)
  where live = foldr analyseLiveness [[]] ir
        shiftedLive = tail live ++ [[]]

analyseLiveness :: IR -> [[Int]] -> [[Int]]
analyseLiveness (ThreeIR _ r1 r2 r3 masked) (live:lives)
  | masked = live:live:lives
  | otherwise =
    live':live:lives
  where gen = (toI r2) `union` (toI r3)
        kill = toI r1
        live' = gen `union` (live \\ kill)

analyseLiveness (TwoIR r1 _ masked) (live:lives)
  | masked = live:live:lives
  | otherwise = live':live:lives
  where live' = live \\ (toI r1)

analyseLiveness _ lives = lives


toI :: IRItem -> [Int]
toI (R r)
  | r < 7 = []
  | otherwise = [r]

toI _ = []

Fix bug in liveness when same operand appears twice in righthand
Fix bug in liveness when same operand appears twice in righthand
