1 -----------------------------------------------------------------------------
3 -- The register liveness determinator
5 -- (c) The University of Glasgow 2004
7 -----------------------------------------------------------------------------
8 {-# OPTIONS -Wall -fno-warn-name-shadowing #-}
10 module RegAlloc.Liveness (
13 BlockMap, emptyBlockMap,
21 mapBlockTop, mapBlockTopM,
22 mapGenBlockTop, mapGenBlockTopM,
40 import Cmm hiding (RegSet)
56 -----------------------------------------------------------------------------
57 type RegSet = UniqSet Reg
59 type RegMap a = UniqFM a
61 emptyRegMap :: UniqFM a
62 emptyRegMap = emptyUFM
64 type BlockMap a = BlockEnv a
66 emptyBlockMap :: BlockEnv a
67 emptyBlockMap = emptyBlockEnv
70 -- | A top level thing which carries liveness information.
75 [SCC (LiveBasicBlock instr)]
78 -- | The register allocator also wants to use SPILL/RELOAD meta instructions,
79 -- so we'll keep those here.
81 -- | A real machine instruction
84 -- | spill this reg to a stack slot
87 -- | reload this reg from a stack slot
90 instance Instruction instr => Instruction (InstrSR instr) where
93 Instr instr -> regUsageOfInstr instr
94 SPILL reg _ -> RU [reg] []
95 RELOAD _ reg -> RU [] [reg]
99 Instr instr -> Instr (patchRegsOfInstr instr f)
100 SPILL reg slot -> SPILL (f reg) slot
101 RELOAD slot reg -> RELOAD slot (f reg)
105 Instr instr -> isJumpishInstr instr
110 Instr instr -> jumpDestsOfInstr instr
115 Instr instr -> Instr (patchJumpInstr instr f)
118 mkSpillInstr = error "mkSpillInstr[InstrSR]: Not making SPILL meta-instr"
119 mkLoadInstr = error "mkLoadInstr[InstrSR]: Not making LOAD meta-instr"
123 Instr instr -> takeDeltaInstr instr
128 Instr instr -> isMetaInstr instr
131 mkRegRegMoveInstr r1 r2 = Instr (mkRegRegMoveInstr r1 r2)
133 takeRegRegMoveInstr i
135 Instr instr -> takeRegRegMoveInstr instr
138 mkJumpInstr target = map Instr (mkJumpInstr target)
142 -- | An instruction with liveness information.
144 = LiveInstr (InstrSR instr) (Maybe Liveness)
146 -- | Liveness information.
147 -- The regs which die are ones which are no longer live in the *next* instruction
149 -- (NB. if the instruction is a jump, these registers might still be live
150 -- at the jump target(s) - you have to check the liveness at the destination
151 -- block to find out).
155 { liveBorn :: RegSet -- ^ registers born in this instruction (written to for first time).
156 , liveDieRead :: RegSet -- ^ registers that died because they were read for the last time.
157 , liveDieWrite :: RegSet } -- ^ registers that died because they were clobbered by something.
160 -- | Stash regs live on entry to each basic block in the info part of the cmm code.
163 [CmmStatic] -- cmm static stuff
164 (Maybe BlockId) -- id of the first block
165 (Maybe (BlockMap RegSet)) -- argument locals live on entry to this block
167 -- | A basic block with liveness information.
168 type LiveBasicBlock instr
169 = GenBasicBlock (LiveInstr instr)
172 instance Outputable instr
173 => Outputable (InstrSR instr) where
175 ppr (Instr realInstr)
180 ptext (sLit "\tSPILL"),
184 ptext (sLit "SLOT") <> parens (int slot)]
186 ppr (RELOAD slot reg)
188 ptext (sLit "\tRELOAD"),
190 ptext (sLit "SLOT") <> parens (int slot),
194 instance Outputable instr
195 => Outputable (LiveInstr instr) where
197 ppr (LiveInstr instr Nothing)
200 ppr (LiveInstr instr (Just live))
204 [ pprRegs (ptext (sLit "# born: ")) (liveBorn live)
205 , pprRegs (ptext (sLit "# r_dying: ")) (liveDieRead live)
206 , pprRegs (ptext (sLit "# w_dying: ")) (liveDieWrite live) ]
209 where pprRegs :: SDoc -> RegSet -> SDoc
211 | isEmptyUniqSet regs = empty
212 | otherwise = name <> (hcat $ punctuate space $ map ppr $ uniqSetToList regs)
214 instance Outputable LiveInfo where
215 ppr (LiveInfo static firstId liveOnEntry)
216 = (vcat $ map ppr static)
217 $$ text "# firstId = " <> ppr firstId
218 $$ text "# liveOnEntry = " <> ppr liveOnEntry
222 -- | map a function across all the basic blocks in this code
225 :: (LiveBasicBlock instr -> LiveBasicBlock instr)
226 -> LiveCmmTop instr -> LiveCmmTop instr
229 = evalState (mapBlockTopM (\x -> return $ f x) cmm) ()
232 -- | map a function across all the basic blocks in this code (monadic version)
236 => (LiveBasicBlock instr -> m (LiveBasicBlock instr))
237 -> LiveCmmTop instr -> m (LiveCmmTop instr)
239 mapBlockTopM _ cmm@(CmmData{})
242 mapBlockTopM f (CmmProc header label params sccs)
243 = do sccs' <- mapM (mapSCCM f) sccs
244 return $ CmmProc header label params sccs'
246 mapSCCM :: Monad m => (a -> m b) -> SCC a -> m (SCC b)
247 mapSCCM f (AcyclicSCC x)
249 return $ AcyclicSCC x'
251 mapSCCM f (CyclicSCC xs)
252 = do xs' <- mapM f xs
253 return $ CyclicSCC xs'
256 -- map a function across all the basic blocks in this code
258 :: (GenBasicBlock i -> GenBasicBlock i)
259 -> (GenCmmTop d h (ListGraph i) -> GenCmmTop d h (ListGraph i))
262 = evalState (mapGenBlockTopM (\x -> return $ f x) cmm) ()
265 -- | map a function across all the basic blocks in this code (monadic version)
268 => (GenBasicBlock i -> m (GenBasicBlock i))
269 -> (GenCmmTop d h (ListGraph i) -> m (GenCmmTop d h (ListGraph i)))
271 mapGenBlockTopM _ cmm@(CmmData{})
274 mapGenBlockTopM f (CmmProc header label params (ListGraph blocks))
275 = do blocks' <- mapM f blocks
276 return $ CmmProc header label params (ListGraph blocks')
279 -- | Slurp out the list of register conflicts and reg-reg moves from this top level thing.
280 -- Slurping of conflicts and moves is wrapped up together so we don't have
281 -- to make two passes over the same code when we want to build the graph.
286 -> (Bag (UniqSet Reg), Bag (Reg, Reg))
289 = slurpCmm (emptyBag, emptyBag) live
291 where slurpCmm rs CmmData{} = rs
292 slurpCmm rs (CmmProc info _ _ sccs)
293 = foldl' (slurpSCC info) rs sccs
295 slurpSCC info rs (AcyclicSCC b)
296 = slurpBlock info rs b
298 slurpSCC info rs (CyclicSCC bs)
299 = foldl' (slurpBlock info) rs bs
301 slurpBlock info rs (BasicBlock blockId instrs)
302 | LiveInfo _ _ (Just blockLive) <- info
303 , Just rsLiveEntry <- lookupBlockEnv blockLive blockId
304 , (conflicts, moves) <- slurpLIs rsLiveEntry rs instrs
305 = (consBag rsLiveEntry conflicts, moves)
308 = panic "Liveness.slurpConflicts: bad block"
310 slurpLIs rsLive (conflicts, moves) []
311 = (consBag rsLive conflicts, moves)
313 slurpLIs rsLive rs (LiveInstr _ Nothing : lis)
314 = slurpLIs rsLive rs lis
316 slurpLIs rsLiveEntry (conflicts, moves) (LiveInstr instr (Just live) : lis)
318 -- regs that die because they are read for the last time at the start of an instruction
319 -- are not live across it.
320 rsLiveAcross = rsLiveEntry `minusUniqSet` (liveDieRead live)
322 -- regs live on entry to the next instruction.
323 -- be careful of orphans, make sure to delete dying regs _after_ unioning
324 -- in the ones that are born here.
325 rsLiveNext = (rsLiveAcross `unionUniqSets` (liveBorn live))
326 `minusUniqSet` (liveDieWrite live)
328 -- orphan vregs are the ones that die in the same instruction they are born in.
329 -- these are likely to be results that are never used, but we still
330 -- need to assign a hreg to them..
331 rsOrphans = intersectUniqSets
333 (unionUniqSets (liveDieWrite live) (liveDieRead live))
336 rsConflicts = unionUniqSets rsLiveNext rsOrphans
338 in case takeRegRegMoveInstr instr of
339 Just rr -> slurpLIs rsLiveNext
340 ( consBag rsConflicts conflicts
341 , consBag rr moves) lis
343 Nothing -> slurpLIs rsLiveNext
344 ( consBag rsConflicts conflicts
348 -- | For spill\/reloads
354 -- If we can arrange that v1 and v2 are allocated to the same hreg it's more likely
355 -- the spill\/reload instrs can be cleaned and replaced by a nop reg-reg move.
363 slurpReloadCoalesce live
364 = slurpCmm emptyBag live
366 where slurpCmm cs CmmData{} = cs
367 slurpCmm cs (CmmProc _ _ _ sccs)
368 = slurpComp cs (flattenSCCs sccs)
371 = let (moveBags, _) = runState (slurpCompM blocks) emptyUFM
372 in unionManyBags (cs : moveBags)
375 = do -- run the analysis once to record the mapping across jumps.
376 mapM_ (slurpBlock False) blocks
378 -- run it a second time while using the information from the last pass.
379 -- We /could/ run this many more times to deal with graphical control
380 -- flow and propagating info across multiple jumps, but it's probably
381 -- not worth the trouble.
382 mapM (slurpBlock True) blocks
384 slurpBlock propagate (BasicBlock blockId instrs)
385 = do -- grab the slot map for entry to this block
386 slotMap <- if propagate
387 then getSlotMap blockId
390 (_, mMoves) <- mapAccumLM slurpLI slotMap instrs
391 return $ listToBag $ catMaybes mMoves
393 slurpLI :: Instruction instr
394 => UniqFM Reg -- current slotMap
396 -> State (UniqFM [UniqFM Reg]) -- blockId -> [slot -> reg]
397 -- for tracking slotMaps across jumps
399 ( UniqFM Reg -- new slotMap
400 , Maybe (Reg, Reg)) -- maybe a new coalesce edge
404 -- remember what reg was stored into the slot
405 | LiveInstr (SPILL reg slot) _ <- li
406 , slotMap' <- addToUFM slotMap slot reg
407 = return (slotMap', Nothing)
409 -- add an edge betwen the this reg and the last one stored into the slot
410 | LiveInstr (RELOAD slot reg) _ <- li
411 = case lookupUFM slotMap slot of
413 | reg /= reg2 -> return (slotMap, Just (reg, reg2))
414 | otherwise -> return (slotMap, Nothing)
416 Nothing -> return (slotMap, Nothing)
418 -- if we hit a jump, remember the current slotMap
419 | LiveInstr (Instr instr) _ <- li
420 , targets <- jumpDestsOfInstr instr
422 = do mapM_ (accSlotMap slotMap) targets
423 return (slotMap, Nothing)
426 = return (slotMap, Nothing)
428 -- record a slotmap for an in edge to this block
429 accSlotMap slotMap blockId
430 = modify (\s -> addToUFM_C (++) s blockId [slotMap])
432 -- work out the slot map on entry to this block
433 -- if we have slot maps for multiple in-edges then we need to merge them.
436 let slotMaps = fromMaybe [] (lookupUFM map blockId)
437 return $ foldr mergeSlotMaps emptyUFM slotMaps
439 mergeSlotMaps :: UniqFM Reg -> UniqFM Reg -> UniqFM Reg
440 mergeSlotMaps map1 map2
442 $ [ (k, r1) | (k, r1) <- ufmToList map1
443 , case lookupUFM map2 k of
445 Just r2 -> r1 == r2 ]
448 -- | Strip away liveness information, yielding NatCmmTop
450 :: (Outputable instr, Instruction instr)
457 where stripCmm (CmmData sec ds) = CmmData sec ds
459 stripCmm (CmmProc (LiveInfo info (Just first_id) _) label params sccs)
460 = let final_blocks = flattenSCCs sccs
462 -- make sure the block that was first in the input list
463 -- stays at the front of the output. This is the entry point
464 -- of the proc, and it needs to come first.
466 = partition ((== first_id) . blockId) final_blocks
468 in CmmProc info label params
469 (ListGraph $ map stripLiveBlock $ first' : rest')
471 -- procs used for stg_split_markers don't contain any blocks, and have no first_id.
472 stripCmm (CmmProc (LiveInfo info Nothing _) label params [])
473 = CmmProc info label params (ListGraph [])
475 -- If the proc has blocks but we don't know what the first one was, then we're dead.
477 = pprPanic "RegAlloc.Liveness.stripLive: no first_id on proc" (ppr proc)
480 -- | Strip away liveness information from a basic block,
481 -- and make real spill instructions out of SPILL, RELOAD pseudos along the way.
485 => LiveBasicBlock instr
486 -> NatBasicBlock instr
488 stripLiveBlock (BasicBlock i lis)
489 = BasicBlock i instrs'
492 = runState (spillNat [] lis) 0
495 = return (reverse acc)
497 spillNat acc (LiveInstr (SPILL reg slot) _ : instrs)
499 spillNat (mkSpillInstr reg delta slot : acc) instrs
501 spillNat acc (LiveInstr (RELOAD slot reg) _ : instrs)
503 spillNat (mkLoadInstr reg delta slot : acc) instrs
505 spillNat acc (LiveInstr (Instr instr) _ : instrs)
506 | Just i <- takeDeltaInstr instr
510 spillNat acc (LiveInstr (Instr instr) _ : instrs)
511 = spillNat (instr : acc) instrs
514 -- | Erase Delta instructions.
522 = mapBlockTop eraseBlock cmm
524 eraseBlock (BasicBlock id lis)
526 $ filter (\(LiveInstr i _) -> not $ isJust $ takeDeltaInstr i)
530 -- | Patch the registers in this code according to this register mapping.
531 -- also erase reg -> reg moves when the reg is the same.
532 -- also erase reg -> reg moves when the destination dies in this instr.
537 -> LiveCmmTop instr -> LiveCmmTop instr
539 patchEraseLive patchF cmm
542 patchCmm cmm@CmmData{} = cmm
544 patchCmm (CmmProc info label params sccs)
545 | LiveInfo static id (Just blockMap) <- info
547 patchRegSet set = mkUniqSet $ map patchF $ uniqSetToList set
548 blockMap' = mapBlockEnv patchRegSet blockMap
550 info' = LiveInfo static id (Just blockMap')
551 in CmmProc info' label params $ map patchSCC sccs
554 = panic "RegAlloc.Liveness.patchEraseLive: no blockMap"
556 patchSCC (AcyclicSCC b) = AcyclicSCC (patchBlock b)
557 patchSCC (CyclicSCC bs) = CyclicSCC (map patchBlock bs)
559 patchBlock (BasicBlock id lis)
560 = BasicBlock id $ patchInstrs lis
563 patchInstrs (li : lis)
565 | LiveInstr i (Just live) <- li'
566 , Just (r1, r2) <- takeRegRegMoveInstr i
571 = li' : patchInstrs lis
573 where li' = patchRegsLiveInstr patchF li
576 -- source and destination regs are the same
579 -- desination reg is never used
580 | elementOfUniqSet r2 (liveBorn live)
581 , elementOfUniqSet r2 (liveDieRead live) || elementOfUniqSet r2 (liveDieWrite live)
587 -- | Patch registers in this LiveInstr, including the liveness information.
592 -> LiveInstr instr -> LiveInstr instr
594 patchRegsLiveInstr patchF li
596 LiveInstr instr Nothing
597 -> LiveInstr (patchRegsOfInstr instr patchF) Nothing
599 LiveInstr instr (Just live)
601 (patchRegsOfInstr instr patchF)
603 { -- WARNING: have to go via lists here because patchF changes the uniq in the Reg
604 liveBorn = mkUniqSet $ map patchF $ uniqSetToList $ liveBorn live
605 , liveDieRead = mkUniqSet $ map patchF $ uniqSetToList $ liveDieRead live
606 , liveDieWrite = mkUniqSet $ map patchF $ uniqSetToList $ liveDieWrite live })
609 --------------------------------------------------------------------------------
610 -- | Convert a NatCmmTop to a LiveCmmTop, with empty liveness information
617 natCmmTopToLive (CmmData i d)
620 natCmmTopToLive (CmmProc info lbl params (ListGraph []))
621 = CmmProc (LiveInfo info Nothing Nothing)
624 natCmmTopToLive (CmmProc info lbl params (ListGraph blocks@(first : _)))
625 = let first_id = blockId first
626 sccs = sccBlocks blocks
627 sccsLive = map (fmap (\(BasicBlock l instrs) ->
628 BasicBlock l (map (\i -> LiveInstr (Instr i) Nothing) instrs)))
631 in CmmProc (LiveInfo info (Just first_id) Nothing)
637 => [NatBasicBlock instr]
638 -> [SCC (NatBasicBlock instr)]
640 sccBlocks blocks = stronglyConnCompFromEdgedVertices graph
642 getOutEdges :: Instruction instr => [instr] -> [BlockId]
643 getOutEdges instrs = concat $ map jumpDestsOfInstr instrs
645 graph = [ (block, getUnique id, map getUnique (getOutEdges instrs))
646 | block@(BasicBlock id instrs) <- blocks ]
649 ---------------------------------------------------------------------------------
650 -- Annotate code with register liveness information
653 :: (Outputable instr, Instruction instr)
655 -> UniqSM (LiveCmmTop instr)
657 regLiveness (CmmData i d)
658 = returnUs $ CmmData i d
660 regLiveness (CmmProc info lbl params [])
661 | LiveInfo static mFirst _ <- info
663 (LiveInfo static mFirst (Just emptyBlockEnv))
666 regLiveness (CmmProc info lbl params sccs)
667 | LiveInfo static mFirst _ <- info
668 = let (ann_sccs, block_live) = computeLiveness sccs
670 in returnUs $ CmmProc (LiveInfo static mFirst (Just block_live))
674 -- -----------------------------------------------------------------------------
675 -- | Check ordering of Blocks
676 -- The computeLiveness function requires SCCs to be in reverse dependent order.
677 -- If they're not the liveness information will be wrong, and we'll get a bad allocation.
678 -- Better to check for this precondition explicitly or some other poor sucker will
679 -- waste a day staring at bad assembly code..
681 checkIsReverseDependent
683 => [SCC (LiveBasicBlock instr)] -- ^ SCCs of blocks that we're about to run the liveness determinator on.
684 -> Maybe BlockId -- ^ BlockIds that fail the test (if any)
686 checkIsReverseDependent sccs'
687 = go emptyUniqSet sccs'
692 go blocksSeen (AcyclicSCC block : sccs)
693 = let dests = slurpJumpDestsOfBlock block
694 blocksSeen' = unionUniqSets blocksSeen $ mkUniqSet [blockId block]
695 badDests = dests `minusUniqSet` blocksSeen'
696 in case uniqSetToList badDests of
697 [] -> go blocksSeen' sccs
700 go blocksSeen (CyclicSCC blocks : sccs)
701 = let dests = unionManyUniqSets $ map slurpJumpDestsOfBlock blocks
702 blocksSeen' = unionUniqSets blocksSeen $ mkUniqSet $ map blockId blocks
703 badDests = dests `minusUniqSet` blocksSeen'
704 in case uniqSetToList badDests of
705 [] -> go blocksSeen' sccs
708 slurpJumpDestsOfBlock (BasicBlock _ instrs)
710 $ map (mkUniqSet . jumpDestsOfInstr)
711 [ i | LiveInstr i _ <- instrs]
714 -- | If we've compute liveness info for this code already we have to reverse
715 -- the SCCs in each top to get them back to the right order so we can do it again.
716 reverseBlocksInTops :: LiveCmmTop instr -> LiveCmmTop instr
717 reverseBlocksInTops top
720 CmmProc info lbl params sccs -> CmmProc info lbl params (reverse sccs)
723 -- | Computing liveness
725 -- On entry, the SCCs must be in "reverse" order: later blocks may transfer
726 -- control to earlier ones only, else `panic`.
728 -- The SCCs returned are in the *opposite* order, which is exactly what we
729 -- want for the next pass.
732 :: (Outputable instr, Instruction instr)
733 => [SCC (LiveBasicBlock instr)]
734 -> ([SCC (LiveBasicBlock instr)], -- instructions annotated with list of registers
735 -- which are "dead after this instruction".
736 BlockMap RegSet) -- blocks annontated with set of live registers
737 -- on entry to the block.
740 = case checkIsReverseDependent sccs of
741 Nothing -> livenessSCCs emptyBlockMap [] sccs
742 Just bad -> pprPanic "RegAlloc.Liveness.computeLivenss"
743 (vcat [ text "SCCs aren't in reverse dependent order"
744 , text "bad blockId" <+> ppr bad
750 -> [SCC (LiveBasicBlock instr)] -- accum
751 -> [SCC (LiveBasicBlock instr)]
752 -> ( [SCC (LiveBasicBlock instr)]
755 livenessSCCs blockmap done []
758 livenessSCCs blockmap done (AcyclicSCC block : sccs)
759 = let (blockmap', block') = livenessBlock blockmap block
760 in livenessSCCs blockmap' (AcyclicSCC block' : done) sccs
762 livenessSCCs blockmap done
763 (CyclicSCC blocks : sccs) =
764 livenessSCCs blockmap' (CyclicSCC blocks':done) sccs
765 where (blockmap', blocks')
766 = iterateUntilUnchanged linearLiveness equalBlockMaps
769 iterateUntilUnchanged
770 :: (a -> b -> (a,c)) -> (a -> a -> Bool)
774 iterateUntilUnchanged f eq a b
777 groupBy (\(a1, _) (a2, _) -> eq a1 a2) $
778 iterate (\(a, _) -> f a b) $
779 (a, panic "RegLiveness.livenessSCCs")
784 => BlockMap RegSet -> [LiveBasicBlock instr]
785 -> (BlockMap RegSet, [LiveBasicBlock instr])
787 linearLiveness = mapAccumL livenessBlock
789 -- probably the least efficient way to compare two
790 -- BlockMaps for equality.
793 where a' = map f $ blockEnvToList a
794 b' = map f $ blockEnvToList b
795 f (key,elt) = (key, uniqSetToList elt)
799 -- | Annotate a basic block with register liveness information.
804 -> LiveBasicBlock instr
805 -> (BlockMap RegSet, LiveBasicBlock instr)
807 livenessBlock blockmap (BasicBlock block_id instrs)
809 (regsLiveOnEntry, instrs1)
810 = livenessBack emptyUniqSet blockmap [] (reverse instrs)
811 blockmap' = extendBlockEnv blockmap block_id regsLiveOnEntry
813 instrs2 = livenessForward regsLiveOnEntry instrs1
815 output = BasicBlock block_id instrs2
817 in ( blockmap', output)
819 -- | Calculate liveness going forwards,
820 -- filling in when regs are born
824 => RegSet -- regs live on this instr
825 -> [LiveInstr instr] -> [LiveInstr instr]
827 livenessForward _ [] = []
828 livenessForward rsLiveEntry (li@(LiveInstr instr mLive) : lis)
830 = li : livenessForward rsLiveEntry lis
833 , RU _ written <- regUsageOfInstr instr
835 -- Regs that are written to but weren't live on entry to this instruction
836 -- are recorded as being born here.
838 $ filter (\r -> not $ elementOfUniqSet r rsLiveEntry) written
840 rsLiveNext = (rsLiveEntry `unionUniqSets` rsBorn)
841 `minusUniqSet` (liveDieRead live)
842 `minusUniqSet` (liveDieWrite live)
844 in LiveInstr instr (Just live { liveBorn = rsBorn })
845 : livenessForward rsLiveNext lis
847 livenessForward _ _ = panic "RegLiveness.livenessForward: no match"
850 -- | Calculate liveness going backwards,
851 -- filling in when regs die, and what regs are live across each instruction
855 => RegSet -- regs live on this instr
856 -> BlockMap RegSet -- regs live on entry to other BBs
857 -> [LiveInstr instr] -- instructions (accum)
858 -> [LiveInstr instr] -- instructions
859 -> (RegSet, [LiveInstr instr])
861 livenessBack liveregs _ done [] = (liveregs, done)
863 livenessBack liveregs blockmap acc (instr : instrs)
864 = let (liveregs', instr') = liveness1 liveregs blockmap instr
865 in livenessBack liveregs' blockmap (instr' : acc) instrs
868 -- don't bother tagging comments or deltas with liveness
874 -> (RegSet, LiveInstr instr)
876 liveness1 liveregs _ (LiveInstr instr _)
878 = (liveregs, LiveInstr instr Nothing)
880 liveness1 liveregs blockmap (LiveInstr instr _)
883 = (liveregs1, LiveInstr instr
885 { liveBorn = emptyUniqSet
886 , liveDieRead = mkUniqSet r_dying
887 , liveDieWrite = mkUniqSet w_dying }))
890 = (liveregs_br, LiveInstr instr
892 { liveBorn = emptyUniqSet
893 , liveDieRead = mkUniqSet r_dying_br
894 , liveDieWrite = mkUniqSet w_dying }))
897 RU read written = regUsageOfInstr instr
899 -- registers that were written here are dead going backwards.
900 -- registers that were read here are live going backwards.
901 liveregs1 = (liveregs `delListFromUniqSet` written)
902 `addListToUniqSet` read
904 -- registers that are not live beyond this point, are recorded
906 r_dying = [ reg | reg <- read, reg `notElem` written,
907 not (elementOfUniqSet reg liveregs) ]
909 w_dying = [ reg | reg <- written,
910 not (elementOfUniqSet reg liveregs) ]
912 -- union in the live regs from all the jump destinations of this
914 targets = jumpDestsOfInstr instr -- where we go from here
915 not_a_branch = null targets
917 targetLiveRegs target
918 = case lookupBlockEnv blockmap target of
920 Nothing -> emptyRegMap
922 live_from_branch = unionManyUniqSets (map targetLiveRegs targets)
924 liveregs_br = liveregs1 `unionUniqSets` live_from_branch
926 -- registers that are live only in the branch targets should
927 -- be listed as dying here.
928 live_branch_only = live_from_branch `minusUniqSet` liveregs
929 r_dying_br = uniqSetToList (mkUniqSet r_dying `unionUniqSets`