Skip to content

Commit 09889af

Browse files
authored
Merge pull request #1628 from GaloisInc/heapster/simplify-lifetimes-before-others
Heapster: simplify lifetime permissions before others
2 parents c08a5ea + 0913d73 commit 09889af

File tree

1 file changed

+49
-25
lines changed

1 file changed

+49
-25
lines changed

heapster-saw/src/Verifier/SAW/Heapster/TypedCrucible.hs

+49-25
Original file line numberDiff line numberDiff line change
@@ -3735,6 +3735,35 @@ castUnDetVarsForVar det_vars x =
37353735
implCastPermM Proxy x eqp >>>
37363736
implPopM x (someEqProofRHS eqp)
37373737

3738+
3739+
-- | Simplify @lowned@ permissions @p@ on variable @x@ so they only depend on
3740+
-- the determined variables given in the supplied list. This function only ends
3741+
-- lifetimes, so that all lifetime ending happens before other unneeded
3742+
-- permissions are dropped.
3743+
simplify1LOwnedPermForDetVars :: NuMatchingAny1 r =>
3744+
NameSet CrucibleType -> Name a -> ValuePerm a ->
3745+
ImplM vars s r RNil RNil ()
3746+
3747+
-- For permission l:lowned[ls](ps_in -o ps_out) where l or some free variable in
3748+
-- ps_in or ps_out is not determined, end l
3749+
simplify1LOwnedPermForDetVars det_vars l (ValPerm_LOwned _ ps_in ps_out)
3750+
| vars <- NameSet.insert l $ freeVars (ps_in,ps_out)
3751+
, not $ NameSet.nameSetIsSubsetOf vars det_vars
3752+
= implEndLifetimeRecM l
3753+
3754+
-- For lowned permission l:lowned[ls](ps_in -o ps_out), end any lifetimes in ls
3755+
-- that are not determined and remove them from the lowned permission for ls
3756+
simplify1LOwnedPermForDetVars det_vars l (ValPerm_LOwned ls _ _)
3757+
| l':_ <- flip mapMaybe ls (asVar >=> \l' ->
3758+
if NameSet.member l' det_vars then Nothing
3759+
else return l') =
3760+
implEndLifetimeRecM l' >>>
3761+
getPerm l >>>= \p' -> simplify1PermForDetVars det_vars l p'
3762+
3763+
-- Otherwise do nothing
3764+
simplify1LOwnedPermForDetVars _ _ _ = return ()
3765+
3766+
37383767
-- | Simplify and drop permissions @p@ on variable @x@ so they only depend on
37393768
-- the determined variables given in the supplied list
37403769
simplify1PermForDetVars :: NuMatchingAny1 r =>
@@ -3768,22 +3797,6 @@ simplify1PermForDetVars det_vars x (ValPerm_Conj ps)
37683797
getPerm x >>>= \new_p ->
37693798
simplify1PermForDetVars det_vars x new_p
37703799

3771-
-- For permission l:lowned[ls](ps_in -o ps_out) where l or some free variable in
3772-
-- ps_in or ps_out is not determined, end l
3773-
simplify1PermForDetVars det_vars l (ValPerm_LOwned _ ps_in ps_out)
3774-
| vars <- NameSet.insert l $ freeVars (ps_in,ps_out)
3775-
, not $ NameSet.nameSetIsSubsetOf vars det_vars
3776-
= implEndLifetimeRecM l
3777-
3778-
-- For lowned permission l:lowned[ls](ps_in -o ps_out), end any lifetimes in ls
3779-
-- that are not determined and remove them from the lowned permission for ls
3780-
simplify1PermForDetVars det_vars l (ValPerm_LOwned ls _ _)
3781-
| l':_ <- flip mapMaybe ls (asVar >=> \l' ->
3782-
if NameSet.member l' det_vars then Nothing
3783-
else return l') =
3784-
implEndLifetimeRecM l' >>>
3785-
getPerm l >>>= \p' -> simplify1PermForDetVars det_vars l p'
3786-
37873800
-- If none of the above cases match but p has only determined free variables,
37883801
-- just leave p as is
37893802
simplify1PermForDetVars det_vars _ p
@@ -3808,8 +3821,15 @@ simplifyPermsForDetVars :: NuMatchingAny1 r => [SomeName CrucibleType] ->
38083821
simplifyPermsForDetVars det_vars_list =
38093822
let det_vars = NameSet.fromList det_vars_list in
38103823
(permSetVars <$> getPerms) >>>= \vars ->
3824+
-- Step 1: cast all the primary permissions using non-determined variables, to
3825+
-- try to simplify them out
3826+
mapM_ (\(SomeName x) -> castUnDetVarsForVar det_vars x) vars >>>
3827+
-- Step 2: end any unneeded lifetimes, but do this before any other unneeded
3828+
-- permissions have been dropped, in case they are needed to end lifetimes
3829+
mapM_ (\(SomeName x) ->
3830+
getPerm x >>>= simplify1LOwnedPermForDetVars det_vars x) vars >>>
3831+
-- Step 3: simplify any other remaining permissions
38113832
mapM_ (\(SomeName x) ->
3812-
castUnDetVarsForVar det_vars x >>>
38133833
getPerm x >>>= simplify1PermForDetVars det_vars x) vars
38143834

38153835

@@ -3920,6 +3940,18 @@ tcJumpTarget ctx (JumpTarget blkID args_tps args) =
39203940
namesToNamesList tops_args_ext_ns ++
39213941
determinedVars orig_cur_perms tops_args_ext_ns in
39223942

3943+
implTraceM (\i ->
3944+
pretty ("tcJumpTarget " ++ show blkID) <>
3945+
{- (if gen_perms_hint then pretty "(gen)" else emptyDoc) <> -}
3946+
line <>
3947+
(case permSetAllVarPerms orig_cur_perms of
3948+
Some all_perms ->
3949+
pretty "Current perms:" <+>
3950+
align (permPretty i all_perms))
3951+
<> line <>
3952+
pretty "Determined vars:"<+>
3953+
align (list (map (permPretty i) det_vars))) >>>
3954+
39233955
-- Step 2: Simplify and drop permissions so they do not depend on undetermined
39243956
-- variables
39253957
simplifyPermsForDetVars det_vars >>>
@@ -3950,15 +3982,7 @@ tcJumpTarget ctx (JumpTarget blkID args_tps args) =
39503982
tops_perms args_perms) ghosts_perms in
39513983
implTraceM (\i ->
39523984
pretty ("tcJumpTarget " ++ show blkID) <>
3953-
{- (if gen_perms_hint then pretty "(gen)" else emptyDoc) <> -}
39543985
line <>
3955-
(case permSetAllVarPerms orig_cur_perms of
3956-
Some all_perms ->
3957-
pretty "Current perms:" <+>
3958-
align (permPretty i all_perms))
3959-
<> line <>
3960-
pretty "Determined vars:"<+>
3961-
align (list (map (permPretty i) det_vars)) <> line <>
39623986
pretty "Input perms:" <+>
39633987
hang 2 (permPretty i perms_in)) >>>
39643988

0 commit comments

Comments
 (0)