Skip to content

Commit

Permalink
remove unnecessary CFGNode structure
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangjunphy committed Jun 13, 2024
1 parent 3f12ef7 commit 05e6847
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 41 deletions.
18 changes: 9 additions & 9 deletions src/CFG.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@
-- FOR A PARTICULAR PURPOSE. See the X11 license for more details.

-- CFG -- Control Flow Graph with SSA nodes
module CFG (plot, CFGContext (..), buildCFG, Condition (..), BasicBlock (..), CFGNode (..), CFGEdge (..), CFG (..)) where
module CFG (plot, CFGContext (..), buildCFG, Condition (..), BasicBlock (..), CFGEdge (..), CFG (..)) where

import AST qualified
import CFG.Build (CFGContext (..), buildCFG)
import CFG.Optimizations.RemoveNoOp (removeNoOp)
import CFG.Optimizations.Types (CFGOptimizer, runOptimizerOnCFG)
import CFG.Plot (generateDotPlot)
import CFG.Types
import qualified AST
import qualified Semantic as SE
import Types
import Control.Monad (mapM_)
import Data.Functor ((<&>))
import Data.Map (Map)
import Data.Map qualified as Map
import CFG.Optimizations.Types (CFGOptimizer, runOptimizerOnCFG)
import CFG.Optimizations.RemoveNoOp (removeNoOp)
import Data.Functor ((<&>))
import Data.Text qualified as Text
import Control.Monad (mapM_)
import Semantic qualified as SE
import Types

{-
Refactor and clean up.
TODO:
1. Find better ways to add phi nodes. [DROP]
2. Refactor control start/exit related code. [DONE]
3. Produce dot plot with some proper library. [DONE]
4. Add unit tests.
4. Add unit tests.
5. Other chores.
-}

Expand Down
20 changes: 10 additions & 10 deletions src/CFG/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import Control.Monad.State
import Control.Monad.Writer
import Data.Functor ((<&>))
import Data.Generics.Labels
import GHC.Generics (Generic)
import Data.List qualified as List
import Data.Map (Map)
import Data.Map.Strict qualified as Map
Expand All @@ -31,13 +30,14 @@ import Data.Set (Set)
import Data.Set qualified as Set
import Data.Text (Text)
import Data.Text qualified as Text
import Debug.Trace (traceShow)
import Formatting
import GHC.Generics (Generic)
import SSA
import Semantic qualified as SE
import Types
import Util.Graph qualified as G
import Util.SourceLoc qualified as SL
import Debug.Trace (traceShow)

{------------------------------------------------
Data types
Expand Down Expand Up @@ -85,13 +85,13 @@ initialState sig =
{------------------------------------------------
Helps for CFGBuild monad
------------------------------------------------}
getGraph :: CFGBuild (G.Graph BBID CFGNode CFGEdge)
getGraph :: CFGBuild (G.Graph BBID BasicBlock CFGEdge)
getGraph = use $ #cfg . #graph

setGraph :: G.Graph BBID CFGNode CFGEdge -> CFGBuild ()
setGraph :: G.Graph BBID BasicBlock CFGEdge -> CFGBuild ()
setGraph g = #cfg . #graph .= g

updateCFG :: G.GraphBuilder BBID CFGNode CFGEdge a -> CFGBuild ()
updateCFG :: G.GraphBuilder BBID BasicBlock CFGEdge a -> CFGBuild ()
updateCFG update = do
g <- getGraph
let g' = G.update update g
Expand Down Expand Up @@ -169,7 +169,7 @@ getBasicBlock' bbid = do
g <- getGraph
case G.lookupNode bbid g of
Nothing -> throwError $ CompileError Nothing $ sformat ("Unable to find basic block" %+ int) bbid
Just node -> return $ node ^. #bb
Just node -> return node

{-----------------------------------------------------------
Add/lookup symbols or variables
Expand Down Expand Up @@ -308,7 +308,7 @@ createEmptyBB = do
bbid <- use #currentBBID
sid <- use #astScope
let bb = BasicBlock bbid sid []
updateCFG (G.addNode bbid (CFGNode bb))
updateCFG (G.addNode bbid bb)
return bbid

finishCurrentBB :: CFGBuild BBID
Expand All @@ -318,7 +318,7 @@ finishCurrentBB = do
sid <- use #astScope
#statements .= []
let bb = BasicBlock bbid sid stmts
updateCFG (G.addNode bbid (CFGNode bb))
updateCFG (G.addNode bbid bb)
#currentBBID += 1
return bbid

Expand Down Expand Up @@ -412,9 +412,9 @@ patchPhiNode bb s1 varMap1 s2 varMap2 = do
case G.lookupNode bb g of
Nothing -> throwError $ CompileError Nothing $ sformat ("Basic block" %+ int %+ "not found.") bb
Just node -> do
let ssaList = node ^. (#bb . #statements)
let ssaList = node ^. #statements
ssaList' <- mapM patch ssaList
updateCFG $ G.adjustNode bb ((#bb . #statements) .~ ssaList')
updateCFG $ G.adjustNode bb (#statements .~ ssaList')
where
patch :: SSA -> CFGBuild SSA
patch (Phi dst []) = do
Expand Down
15 changes: 7 additions & 8 deletions src/CFG/Optimizations/RemoveNoOp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import Control.Lens (use, uses, view, (%=), (%~), (&), (+=), (.=), (.~), (^.), _
import Control.Monad.Except
import Data.List (find)
import Data.Map.Strict qualified as Map
import SSA (SSA)
import SSA qualified
import Types
import Util.Graph qualified as G
import SSA qualified
import SSA (SSA)

removeNoOp :: CFGOptimizer () ()
removeNoOp = do
Expand All @@ -39,7 +39,7 @@ findNoOpNode (CFG g@(G.Graph nodes edges) entry exit) =
noOpPred bbid node
| bbid == entry = False
| bbid == exit = False
| otherwise = null $ node ^. (#bb . #statements)
| otherwise = null $ node ^. #statements
inboundPred bbid _ = length (G.inBound bbid g) == 1
outboundPred bbid _ = length (G.outBound bbid g) == 1
outEdgePred bbid _ =
Expand Down Expand Up @@ -68,16 +68,15 @@ removeNodeAndPatchPhi bbid = do
G.addEdge bbidIn bbidOut edgeIn
-- patch Phi in successor nodes
updateCFG $ do
G.adjustNode bbidOut (patchCFGNode bbidIn)
G.adjustNode bbidOut (patchBasicBlock bbidIn)
G.deleteNode bbid
where
isSeqEdge SeqEdge = True
isSeqEdge _ = False
patchPhi :: BBID -> SSA.SSA -> SSA.SSA
patchPhi bbidIn (SSA.Phi dst predecessors) =
let replace (var, bbid') = if bbid' == bbid then (var, bbidIn) else (var, bbid')
in SSA.Phi dst $ replace <$> predecessors
in SSA.Phi dst $ replace <$> predecessors
patchPhi _ ssa = ssa
patchCFGNode :: BBID -> CFGNode -> CFGNode
patchCFGNode bbidIn node = node & #bb . #statements %~ fmap (patchPhi bbidIn)

patchBasicBlock :: BBID -> BasicBlock -> BasicBlock
patchBasicBlock bbidIn node = node & #statements %~ fmap (patchPhi bbidIn)
2 changes: 1 addition & 1 deletion src/CFG/Optimizations/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ getCFG = gets cfg
getOptState :: CFGOptimizer s s
getOptState = gets optState

updateCFG :: G.GraphBuilder BBID CFGNode CFGEdge a -> CFGOptimizer s ()
updateCFG :: G.GraphBuilder BBID BasicBlock CFGEdge a -> CFGOptimizer s ()
updateCFG update = do
(CFG g _ _) <- getCFG
let g' = G.update update g
Expand Down
6 changes: 3 additions & 3 deletions src/CFG/Plot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ edges (CFG graph _ _) = fmap (\((src, dst), d) -> (src, dst, d)) $ Map.toList $
backEdges :: CFG -> Set (BBID, BBID)
backEdges cfg@(CFG _ _ exit) = Set.fromList $ filter (\(b1, b2) -> b1 > b2 && b2 /= exit) $ fmap (\(b1, b2, _) -> (b1, b2)) (edges cfg)

gvizParams :: CFG -> GViz.GraphvizParams BBID CFGNode CFGEdge () CFGNode
gvizParams :: CFG -> GViz.GraphvizParams BBID BasicBlock CFGEdge () BasicBlock
gvizParams cfg@(CFG _ entry exit) =
GViz.nonClusteredParams
{ GViz.globalAttributes = globalAttrs,
Expand Down Expand Up @@ -70,8 +70,8 @@ cfgToDot cfg@(CFG graph entry exit) =
then (b2, b1, e)
else (b1, b2, e)

prettyPrintNode :: BBID -> CFGNode -> CFG -> LT.Text
prettyPrintNode bbid CFGNode {bb = BasicBlock {bbid = id, statements = stmts}} (CFG _ entry exit) =
prettyPrintNode :: BBID -> BasicBlock -> CFG -> LT.Text
prettyPrintNode bbid BasicBlock {bbid = id, statements = stmts} (CFG _ entry exit) =
let idText = [format ("<id:" %+ int %+ stext % ">") id entryExit]
segments = stmts <&> format shown
in LT.intercalate "\n" $ idText ++ segments
Expand Down
16 changes: 6 additions & 10 deletions src/CFG/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ data BasicBlock = BasicBlock
}
deriving (Generic, Show)

newtype CFGNode = CFGNode
{ bb :: BasicBlock
}
deriving (Generic, Show)

data CFGEdge
= SeqEdge
| CondEdge !Condition
Expand All @@ -41,9 +36,10 @@ data CFGEdge
-- type CFG = G.Graph BBID CFGNode CFGEdge

data CFG = CFG
{ graph :: !(G.Graph BBID CFGNode CFGEdge)
, entry :: !BBID
, exit :: !BBID
} deriving (Generic)
{ graph :: !(G.Graph BBID BasicBlock CFGEdge),
entry :: !BBID,
exit :: !BBID
}
deriving (Generic)

type CFGBuilder = G.GraphBuilder BBID CFGNode CFGEdge
type CFGBuilder = G.GraphBuilder BBID BasicBlock CFGEdge

0 comments on commit 05e6847

Please sign in to comment.