@@ -16,6 +16,7 @@ import IRVariants
1616import Name
1717import Subst
1818import Occurrence hiding (Var )
19+ import Optimize
1920import Types.Core
2021import Types.Primitives
2122
@@ -90,7 +91,7 @@ inlineDeclsSubst = \case
9091 s <- getSubst
9192 extendSubst (b @> SubstVal (SuspEx expr s)) $ inlineDeclsSubst rest
9293 else do
93- expr' <- inlineExpr Stop expr
94+ expr' <- inlineExpr Stop expr >>= (liftEnvReaderM . peepholeExpr)
9495 -- If the inliner starts moving effectful expressions, it may become
9596 -- necessary to query the effects of the new expression here.
9697 let presInfo = resolveWorkConservation ann expr'
@@ -248,6 +249,9 @@ data Context (from::E) (to::E) (o::S) where
248249 Stop :: Context e e o
249250 TabAppCtx :: [SAtom i ] -> Subst InlineSubstVal i o
250251 -> Context SExpr e o -> Context SExpr e o
252+ CaseCtx :: [SAlt i ] -> SType i -> EffectRow SimpIR i
253+ -> Subst InlineSubstVal i o
254+ -> Context SExpr e o -> Context SExpr e o
251255 EmitToAtomCtx :: Context SAtom e o -> Context SExpr e o
252256 EmitToNameCtx :: Context SAtomName e o -> Context SAtom e o
253257
@@ -271,6 +275,9 @@ inlineExpr ctx = \case
271275 TabApp tbl ixs -> do
272276 s <- getSubst
273277 inlineAtom (TabAppCtx ixs s ctx) tbl
278+ Case scrut alts resultTy effs -> do
279+ s <- getSubst
280+ inlineAtom (CaseCtx alts resultTy effs s ctx) scrut
274281 expr -> visitGeneric expr >>= reconstruct ctx
275282
276283inlineAtom :: Emits o => Context SExpr e o -> SAtom i -> InlineM i o (e o )
@@ -340,12 +347,18 @@ instance Inlinable SBlock where
340347 effs' <- inline Stop effs -- TODO Really?
341348 reconstruct ctx $ Block (BlockAnn ty' effs') decls' ans'
342349
350+ inlineBlockEmits :: Emits o => Context SExpr e2 o -> SBlock i -> InlineM i o (e2 o )
351+ inlineBlockEmits ctx (Block _ decls ans) = do
352+ inlineDecls decls $ inlineAtom ctx ans
353+
343354-- Still using InlineM because we may call back into inlining, and we wish to
344355-- retain our output binding environment.
345356reconstruct :: Emits o => Context e1 e2 o -> e1 o -> InlineM i o (e2 o )
346357reconstruct ctx e = case ctx of
347358 Stop -> return e
348359 TabAppCtx ixs s ctx' -> withSubst s $ reconstructTabApp ctx' e ixs
360+ CaseCtx alts resultTy effs s ctx' ->
361+ withSubst s $ reconstructCase ctx' e alts resultTy effs
349362 EmitToAtomCtx ctx' -> emitExprToAtom e >>= reconstruct ctx'
350363 EmitToNameCtx ctx' -> emit (Atom e) >>= reconstruct ctx'
351364{-# INLINE reconstruct #-}
@@ -404,5 +417,23 @@ reconstructTabApp ctx expr ixs =
404417 ixs' <- mapM (inline Stop ) ixs
405418 reconstruct ctx $ TabApp array' ixs'
406419
420+ reconstructCase :: Emits o
421+ => Context SExpr e o -> SExpr o -> [SAlt i ] -> SType i -> EffectRow SimpIR i
422+ -> InlineM i o (e o )
423+ reconstructCase ctx scrutExpr alts resultTy effs = do
424+ -- TODO Opportunity here to inspect `scrutExpr` and perform case-of-case
425+ -- optimization
426+ scrut <- emitExprToAtom scrutExpr
427+ case trySelectBranch scrut of
428+ Just (i, val) -> do
429+ Abs b body <- return $ alts !! i
430+ extendSubst (b @> (SubstVal $ DoneEx $ Atom val)) do
431+ inlineBlockEmits ctx body
432+ Nothing -> do
433+ alts' <- mapM visitAlt alts
434+ resultTy' <- inline Stop resultTy
435+ effs' <- inline Stop effs
436+ reconstruct ctx $ Case scrut alts' resultTy' effs'
437+
407438instance Inlinable (EffectRow SimpIR )
408439instance Inlinable (EffectAndType SimpIR )
0 commit comments