diff --git a/src/Juvix/Compiler/Reg/Data/TransformationId.hs b/src/Juvix/Compiler/Reg/Data/TransformationId.hs index dce2e83247..6ec735f736 100644 --- a/src/Juvix/Compiler/Reg/Data/TransformationId.hs +++ b/src/Juvix/Compiler/Reg/Data/TransformationId.hs @@ -20,6 +20,8 @@ data PipelineId type TransformationLikeId = TransformationLikeId' TransformationId PipelineId +-- Note: this works only because for now we mark all variables as live. Liveness +-- information needs to be re-computed after copy propagation. toCTransformations :: [TransformationId] toCTransformations = [Cleanup, CopyPropagation] diff --git a/src/Juvix/Compiler/Reg/Extra/Base.hs b/src/Juvix/Compiler/Reg/Extra/Base.hs index ac036508f7..f42c5d3c54 100644 --- a/src/Juvix/Compiler/Reg/Extra/Base.hs +++ b/src/Juvix/Compiler/Reg/Extra/Base.hs @@ -148,3 +148,13 @@ overValueRefs f = \case goBlock :: InstrBlock -> InstrBlock goBlock x = x + +updateLiveVars' :: (VarRef -> Maybe VarRef) -> Instruction -> Instruction +updateLiveVars' f = \case + Prealloc x -> Prealloc $ over instrPreallocLiveVars (mapMaybe f) x + Call x -> Call $ over instrCallLiveVars (mapMaybe f) x + CallClosures x -> CallClosures $ over instrCallClosuresLiveVars (mapMaybe f) x + instr -> instr + +updateLiveVars :: (VarRef -> VarRef) -> Instruction -> Instruction +updateLiveVars f = updateLiveVars' (Just . f) diff --git a/src/Juvix/Compiler/Reg/Transformation/CopyPropagation.hs b/src/Juvix/Compiler/Reg/Transformation/CopyPropagation.hs index 0c92ba61ca..a296535a2b 100644 --- a/src/Juvix/Compiler/Reg/Transformation/CopyPropagation.hs +++ b/src/Juvix/Compiler/Reg/Transformation/CopyPropagation.hs @@ -27,7 +27,7 @@ copyPropagateFunction = (mpv', instr') where instr' = overValueRefs (adjustVarRef mpv) instr - mpv' = maybe mpv (filterOutVars mpv) (getResultVar instr') + mpv' = maybe mpv (filterOutVars mpv) (getResultVar instr) filterOutVars :: VarMap -> VarRef -> VarMap filterOutVars mpv v = HashMap.delete v $ HashMap.filter (/= v) mpv diff --git a/src/Juvix/Compiler/Reg/Transformation/SSA.hs b/src/Juvix/Compiler/Reg/Transformation/SSA.hs index 4b6604958f..fce911dd86 100644 --- a/src/Juvix/Compiler/Reg/Transformation/SSA.hs +++ b/src/Juvix/Compiler/Reg/Transformation/SSA.hs @@ -22,20 +22,13 @@ computeFunctionSSA = where go :: Instruction -> IndexMap VarRef -> (IndexMap VarRef, Instruction) go instr mp = case getResultVar instr' of - Just vref -> (mp', updateLiveVars mp' (setResultVar instr' (mkVarRef VarGroupLocal idx))) + Just vref -> (mp', updateLiveVars' (adjustVarRef' mp') (setResultVar instr' (mkVarRef VarGroupLocal idx))) where (idx, mp') = IndexMap.assign mp vref - Nothing -> (mp, updateLiveVars mp instr') + Nothing -> (mp, updateLiveVars' (adjustVarRef' mp) instr') where instr' = overValueRefs (adjustVarRef mp) instr - updateLiveVars :: IndexMap VarRef -> Instruction -> Instruction - updateLiveVars mp = \case - Prealloc x -> Prealloc $ over instrPreallocLiveVars (mapMaybe (adjustVarRef' mp)) x - Call x -> Call $ over instrCallLiveVars (mapMaybe (adjustVarRef' mp)) x - CallClosures x -> CallClosures $ over instrCallClosuresLiveVars (mapMaybe (adjustVarRef' mp)) x - instr -> instr - -- For branches, when necessary we insert assignments unifying the renamed -- output variables into a single output variable for both branches. combine :: Instruction -> NonEmpty (IndexMap VarRef) -> (IndexMap VarRef, Instruction)