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,
39 import Cmm hiding (RegSet)
55 -----------------------------------------------------------------------------
56 type RegSet = UniqSet Reg
58 type RegMap a = UniqFM a
60 emptyRegMap :: UniqFM a
61 emptyRegMap = emptyUFM
63 type BlockMap a = BlockEnv a
65 emptyBlockMap :: BlockEnv a
66 emptyBlockMap = emptyBlockEnv
69 -- | A top level thing which carries liveness information.
74 [SCC (LiveBasicBlock instr)]
77 -- | The register allocator also wants to use SPILL/RELOAD meta instructions,
78 -- so we'll keep those here.
80 -- | A real machine instruction
83 -- | spill this reg to a stack slot
86 -- | reload this reg from a stack slot
89 instance Instruction instr => Instruction (InstrSR instr) where
92 Instr instr -> regUsageOfInstr instr
93 SPILL reg _ -> RU [reg] []
94 RELOAD _ reg -> RU [] [reg]
98 Instr instr -> Instr (patchRegsOfInstr instr f)
99 SPILL reg slot -> SPILL (f reg) slot
100 RELOAD slot reg -> RELOAD slot (f reg)
104 Instr instr -> isJumpishInstr instr
109 Instr instr -> jumpDestsOfInstr instr
114 Instr instr -> Instr (patchJumpInstr instr f)
117 mkSpillInstr = error "mkSpillInstr[InstrSR]: Not making SPILL meta-instr"
118 mkLoadInstr = error "mkLoadInstr[InstrSR]: Not making LOAD meta-instr"
122 Instr instr -> takeDeltaInstr instr
127 Instr instr -> isMetaInstr instr
130 mkRegRegMoveInstr r1 r2 = Instr (mkRegRegMoveInstr r1 r2)
132 takeRegRegMoveInstr i
134 Instr instr -> takeRegRegMoveInstr instr
137 mkJumpInstr target = map Instr (mkJumpInstr target)
141 -- | An instruction with liveness information.
143 = LiveInstr (InstrSR instr) (Maybe Liveness)
147 -- | Liveness information.
148 -- The regs which die are ones which are no longer live in the *next* instruction
150 -- (NB. if the instruction is a jump, these registers might still be live
151 -- at the jump target(s) - you have to check the liveness at the destination
152 -- block to find out).
156 { liveBorn :: RegSet -- ^ registers born in this instruction (written to for first time).
157 , liveDieRead :: RegSet -- ^ registers that died because they were read for the last time.
158 , liveDieWrite :: RegSet } -- ^ registers that died because they were clobbered by something.
161 -- | Stash regs live on entry to each basic block in the info part of the cmm code.
164 [CmmStatic] -- cmm static stuff
165 (Maybe BlockId) -- id of the first block
166 (Maybe (BlockMap RegSet)) -- argument locals live on entry to this block
168 -- | A basic block with liveness information.
169 type LiveBasicBlock instr
170 = GenBasicBlock (LiveInstr instr)
173 instance Outputable instr
174 => Outputable (InstrSR instr) where
176 ppr (Instr realInstr)
181 ptext (sLit "\tSPILL"),
185 ptext (sLit "SLOT") <> parens (int slot)]
187 ppr (RELOAD slot reg)
189 ptext (sLit "\tRELOAD"),
191 ptext (sLit "SLOT") <> parens (int slot),
195 instance Outputable instr
196 => Outputable (LiveInstr instr) where
198 ppr (LiveInstr instr Nothing)
201 ppr (LiveInstr instr (Just live))
205 [ pprRegs (ptext (sLit "# born: ")) (liveBorn live)
206 , pprRegs (ptext (sLit "# r_dying: ")) (liveDieRead live)
207 , pprRegs (ptext (sLit "# w_dying: ")) (liveDieWrite live) ]
210 where pprRegs :: SDoc -> RegSet -> SDoc
212 | isEmptyUniqSet regs = empty
213 | otherwise = name <> (hcat $ punctuate space $ map ppr $ uniqSetToList regs)
215 instance Outputable LiveInfo where
216 ppr (LiveInfo static firstId liveOnEntry)
217 = (vcat $ map ppr static)
218 $$ text "# firstId = " <> ppr firstId
219 $$ text "# liveOnEntry = " <> ppr liveOnEntry
223 -- | map a function across all the basic blocks in this code
226 :: (LiveBasicBlock instr -> LiveBasicBlock instr)
227 -> LiveCmmTop instr -> LiveCmmTop instr
230 = evalState (mapBlockTopM (\x -> return $ f x) cmm) ()
233 -- | map a function across all the basic blocks in this code (monadic version)
237 => (LiveBasicBlock instr -> m (LiveBasicBlock instr))
238 -> LiveCmmTop instr -> m (LiveCmmTop instr)
240 mapBlockTopM _ cmm@(CmmData{})
243 mapBlockTopM f (CmmProc header label params sccs)
244 = do sccs' <- mapM (mapSCCM f) sccs
245 return $ CmmProc header label params sccs'
247 mapSCCM :: Monad m => (a -> m b) -> SCC a -> m (SCC b)
248 mapSCCM f (AcyclicSCC x)
250 return $ AcyclicSCC x'
252 mapSCCM f (CyclicSCC xs)
253 = do xs' <- mapM f xs
254 return $ CyclicSCC xs'
257 -- map a function across all the basic blocks in this code
259 :: (GenBasicBlock i -> GenBasicBlock i)
260 -> (GenCmmTop d h (ListGraph i) -> GenCmmTop d h (ListGraph i))
263 = evalState (mapGenBlockTopM (\x -> return $ f x) cmm) ()
266 -- | map a function across all the basic blocks in this code (monadic version)
269 => (GenBasicBlock i -> m (GenBasicBlock i))
270 -> (GenCmmTop d h (ListGraph i) -> m (GenCmmTop d h (ListGraph i)))
272 mapGenBlockTopM _ cmm@(CmmData{})
275 mapGenBlockTopM f (CmmProc header label params (ListGraph blocks))
276 = do blocks' <- mapM f blocks
277 return $ CmmProc header label params (ListGraph blocks')
280 -- | Slurp out the list of register conflicts and reg-reg moves from this top level thing.
281 -- Slurping of conflicts and moves is wrapped up together so we don't have
282 -- to make two passes over the same code when we want to build the graph.
287 -> (Bag (UniqSet Reg), Bag (Reg, Reg))
290 = slurpCmm (emptyBag, emptyBag) live
292 where slurpCmm rs CmmData{} = rs
293 slurpCmm rs (CmmProc info _ _ sccs)
294 = foldl' (slurpSCC info) rs sccs
296 slurpSCC info rs (AcyclicSCC b)
297 = slurpBlock info rs b
299 slurpSCC info rs (CyclicSCC bs)
300 = foldl' (slurpBlock info) rs bs
302 slurpBlock info rs (BasicBlock blockId instrs)
303 | LiveInfo _ _ (Just blockLive) <- info
304 , Just rsLiveEntry <- lookupBlockEnv blockLive blockId
305 , (conflicts, moves) <- slurpLIs rsLiveEntry rs instrs
306 = (consBag rsLiveEntry conflicts, moves)
309 = panic "Liveness.slurpConflicts: bad block"
311 slurpLIs rsLive (conflicts, moves) []
312 = (consBag rsLive conflicts, moves)
314 slurpLIs rsLive rs (LiveInstr _ Nothing : lis)
315 = slurpLIs rsLive rs lis
317 slurpLIs rsLiveEntry (conflicts, moves) (LiveInstr instr (Just live) : lis)
319 -- regs that die because they are read for the last time at the start of an instruction
320 -- are not live across it.
321 rsLiveAcross = rsLiveEntry `minusUniqSet` (liveDieRead live)
323 -- regs live on entry to the next instruction.
324 -- be careful of orphans, make sure to delete dying regs _after_ unioning
325 -- in the ones that are born here.
326 rsLiveNext = (rsLiveAcross `unionUniqSets` (liveBorn live))
327 `minusUniqSet` (liveDieWrite live)
329 -- orphan vregs are the ones that die in the same instruction they are born in.
330 -- these are likely to be results that are never used, but we still
331 -- need to assign a hreg to them..
332 rsOrphans = intersectUniqSets
334 (unionUniqSets (liveDieWrite live) (liveDieRead live))
337 rsConflicts = unionUniqSets rsLiveNext rsOrphans
339 in case takeRegRegMoveInstr instr of
340 Just rr -> slurpLIs rsLiveNext
341 ( consBag rsConflicts conflicts
342 , consBag rr moves) lis
344 Nothing -> slurpLIs rsLiveNext
345 ( consBag rsConflicts conflicts
349 -- | For spill\/reloads
355 -- If we can arrange that v1 and v2 are allocated to the same hreg it's more likely
356 -- the spill\/reload instrs can be cleaned and replaced by a nop reg-reg move.
364 slurpReloadCoalesce live
365 = slurpCmm emptyBag live
367 where slurpCmm cs CmmData{} = cs
368 slurpCmm cs (CmmProc _ _ _ sccs)
369 = slurpComp cs (flattenSCCs sccs)
372 = let (moveBags, _) = runState (slurpCompM blocks) emptyUFM
373 in unionManyBags (cs : moveBags)
376 = do -- run the analysis once to record the mapping across jumps.
377 mapM_ (slurpBlock False) blocks
379 -- run it a second time while using the information from the last pass.
380 -- We /could/ run this many more times to deal with graphical control
381 -- flow and propagating info across multiple jumps, but it's probably
382 -- not worth the trouble.
383 mapM (slurpBlock True) blocks
385 slurpBlock propagate (BasicBlock blockId instrs)
386 = do -- grab the slot map for entry to this block
387 slotMap <- if propagate
388 then getSlotMap blockId
391 (_, mMoves) <- mapAccumLM slurpLI slotMap instrs
392 return $ listToBag $ catMaybes mMoves
394 slurpLI :: Instruction instr
395 => UniqFM Reg -- current slotMap
397 -> State (UniqFM [UniqFM Reg]) -- blockId -> [slot -> reg]
398 -- for tracking slotMaps across jumps
400 ( UniqFM Reg -- new slotMap
401 , Maybe (Reg, Reg)) -- maybe a new coalesce edge
405 -- remember what reg was stored into the slot
406 | LiveInstr (SPILL reg slot) _ <- li
407 , slotMap' <- addToUFM slotMap slot reg
408 = return (slotMap', Nothing)
410 -- add an edge betwen the this reg and the last one stored into the slot
411 | LiveInstr (RELOAD slot reg) _ <- li
412 = case lookupUFM slotMap slot of
414 | reg /= reg2 -> return (slotMap, Just (reg, reg2))
415 | otherwise -> return (slotMap, Nothing)
417 Nothing -> return (slotMap, Nothing)
419 -- if we hit a jump, remember the current slotMap
420 | LiveInstr (Instr instr) _ <- li
421 , targets <- jumpDestsOfInstr instr
423 = do mapM_ (accSlotMap slotMap) targets
424 return (slotMap, Nothing)
427 = return (slotMap, Nothing)
429 -- record a slotmap for an in edge to this block
430 accSlotMap slotMap blockId
431 = modify (\s -> addToUFM_C (++) s blockId [slotMap])
433 -- work out the slot map on entry to this block
434 -- if we have slot maps for multiple in-edges then we need to merge them.
437 let slotMaps = fromMaybe [] (lookupUFM map blockId)
438 return $ foldr mergeSlotMaps emptyUFM slotMaps
440 mergeSlotMaps :: UniqFM Reg -> UniqFM Reg -> UniqFM Reg
441 mergeSlotMaps map1 map2
443 $ [ (k, r1) | (k, r1) <- ufmToList map1
444 , case lookupUFM map2 k of
446 Just r2 -> r1 == r2 ]
449 -- | Strip away liveness information, yielding NatCmmTop
459 where stripCmm (CmmData sec ds) = CmmData sec ds
461 stripCmm (CmmProc (LiveInfo info (Just first_id) _) label params sccs)
462 = let final_blocks = flattenSCCs sccs
464 -- make sure the block that was first in the input list
465 -- stays at the front of the output. This is the entry point
466 -- of the proc, and it needs to come first.
468 = partition ((== first_id) . blockId) final_blocks
470 in CmmProc info label params
471 (ListGraph $ map stripLiveBlock $ first' : rest')
474 = panic "RegAlloc.Liveness.stripLive: no first_id on proc"
476 -- | Strip away liveness information from a basic block,
477 -- and make real spill instructions out of SPILL, RELOAD pseudos along the way.
481 => LiveBasicBlock instr
482 -> NatBasicBlock instr
484 stripLiveBlock (BasicBlock i lis)
485 = BasicBlock i instrs'
488 = runState (spillNat [] lis) 0
491 = return (reverse acc)
493 spillNat acc (LiveInstr (SPILL reg slot) _ : instrs)
495 spillNat (mkSpillInstr reg delta slot : acc) instrs
497 spillNat acc (LiveInstr (RELOAD slot reg) _ : instrs)
499 spillNat (mkLoadInstr reg delta slot : acc) instrs
501 spillNat acc (LiveInstr (Instr instr) _ : instrs)
502 | Just i <- takeDeltaInstr instr
506 spillNat acc (LiveInstr (Instr instr) _ : instrs)
507 = spillNat (instr : acc) instrs
510 -- | Erase Delta instructions.
518 = mapBlockTop eraseBlock cmm
520 eraseBlock (BasicBlock id lis)
522 $ filter (\(LiveInstr i _) -> not $ isJust $ takeDeltaInstr i)
526 -- | Patch the registers in this code according to this register mapping.
527 -- also erase reg -> reg moves when the reg is the same.
528 -- also erase reg -> reg moves when the destination dies in this instr.
533 -> LiveCmmTop instr -> LiveCmmTop instr
535 patchEraseLive patchF cmm
538 patchCmm cmm@CmmData{} = cmm
540 patchCmm (CmmProc info label params sccs)
541 | LiveInfo static id (Just blockMap) <- info
543 patchRegSet set = mkUniqSet $ map patchF $ uniqSetToList set
544 blockMap' = mapBlockEnv patchRegSet blockMap
546 info' = LiveInfo static id (Just blockMap')
547 in CmmProc info' label params $ map patchSCC sccs
550 = panic "RegAlloc.Liveness.patchEraseLive: no blockMap"
552 patchSCC (AcyclicSCC b) = AcyclicSCC (patchBlock b)
553 patchSCC (CyclicSCC bs) = CyclicSCC (map patchBlock bs)
555 patchBlock (BasicBlock id lis)
556 = BasicBlock id $ patchInstrs lis
559 patchInstrs (li : lis)
561 | LiveInstr i (Just live) <- li'
562 , Just (r1, r2) <- takeRegRegMoveInstr i
567 = li' : patchInstrs lis
569 where li' = patchRegsLiveInstr patchF li
572 -- source and destination regs are the same
575 -- desination reg is never used
576 | elementOfUniqSet r2 (liveBorn live)
577 , elementOfUniqSet r2 (liveDieRead live) || elementOfUniqSet r2 (liveDieWrite live)
583 -- | Patch registers in this LiveInstr, including the liveness information.
588 -> LiveInstr instr -> LiveInstr instr
590 patchRegsLiveInstr patchF li
592 LiveInstr instr Nothing
593 -> LiveInstr (patchRegsOfInstr instr patchF) Nothing
595 LiveInstr instr (Just live)
597 (patchRegsOfInstr instr patchF)
599 { -- WARNING: have to go via lists here because patchF changes the uniq in the Reg
600 liveBorn = mkUniqSet $ map patchF $ uniqSetToList $ liveBorn live
601 , liveDieRead = mkUniqSet $ map patchF $ uniqSetToList $ liveDieRead live
602 , liveDieWrite = mkUniqSet $ map patchF $ uniqSetToList $ liveDieWrite live })
605 --------------------------------------------------------------------------------
606 -- | Convert a NatCmmTop to a LiveCmmTop, with empty liveness information
613 natCmmTopToLive (CmmData i d)
616 natCmmTopToLive (CmmProc info lbl params (ListGraph []))
617 = CmmProc (LiveInfo info Nothing Nothing)
620 natCmmTopToLive (CmmProc info lbl params (ListGraph blocks@(first : _)))
621 = let first_id = blockId first
622 sccs = sccBlocks blocks
623 sccsLive = map (fmap (\(BasicBlock l instrs) ->
624 BasicBlock l (map (\i -> LiveInstr (Instr i) Nothing) instrs)))
627 in CmmProc (LiveInfo info (Just first_id) Nothing)
633 => [NatBasicBlock instr]
634 -> [SCC (NatBasicBlock instr)]
636 sccBlocks blocks = stronglyConnCompFromEdgedVertices graph
638 getOutEdges :: Instruction instr => [instr] -> [BlockId]
639 getOutEdges instrs = concat $ map jumpDestsOfInstr instrs
641 graph = [ (block, getUnique id, map getUnique (getOutEdges instrs))
642 | block@(BasicBlock id instrs) <- blocks ]
645 ---------------------------------------------------------------------------------
646 -- Annotate code with register liveness information
651 -> UniqSM (LiveCmmTop instr)
653 regLiveness (CmmData i d)
654 = returnUs $ CmmData i d
656 regLiveness (CmmProc info lbl params [])
657 | LiveInfo static mFirst _ <- info
659 (LiveInfo static mFirst (Just emptyBlockEnv))
662 regLiveness (CmmProc info lbl params sccs)
663 | LiveInfo static mFirst _ <- info
664 = let (ann_sccs, block_live) = computeLiveness sccs
666 in returnUs $ CmmProc (LiveInfo static mFirst (Just block_live))
671 -- -----------------------------------------------------------------------------
672 -- Computing liveness
676 => [SCC (LiveBasicBlock instr)]
677 -> ([SCC (LiveBasicBlock instr)], -- instructions annotated with list of registers
678 -- which are "dead after this instruction".
679 BlockMap RegSet) -- blocks annontated with set of live registers
680 -- on entry to the block.
682 -- NOTE: on entry, the SCCs are in "reverse" order: later blocks may transfer
683 -- control to earlier ones only. The SCCs returned are in the *opposite*
684 -- order, which is exactly what we want for the next pass.
687 = livenessSCCs emptyBlockMap [] sccs
693 -> [SCC (LiveBasicBlock instr)] -- accum
694 -> [SCC (LiveBasicBlock instr)]
695 -> ( [SCC (LiveBasicBlock instr)]
698 livenessSCCs blockmap done []
701 livenessSCCs blockmap done (AcyclicSCC block : sccs)
702 = let (blockmap', block') = livenessBlock blockmap block
703 in livenessSCCs blockmap' (AcyclicSCC block' : done) sccs
705 livenessSCCs blockmap done
706 (CyclicSCC blocks : sccs) =
707 livenessSCCs blockmap' (CyclicSCC blocks':done) sccs
708 where (blockmap', blocks')
709 = iterateUntilUnchanged linearLiveness equalBlockMaps
712 iterateUntilUnchanged
713 :: (a -> b -> (a,c)) -> (a -> a -> Bool)
717 iterateUntilUnchanged f eq a b
720 groupBy (\(a1, _) (a2, _) -> eq a1 a2) $
721 iterate (\(a, _) -> f a b) $
722 (a, panic "RegLiveness.livenessSCCs")
727 => BlockMap RegSet -> [LiveBasicBlock instr]
728 -> (BlockMap RegSet, [LiveBasicBlock instr])
730 linearLiveness = mapAccumL livenessBlock
732 -- probably the least efficient way to compare two
733 -- BlockMaps for equality.
736 where a' = map f $ blockEnvToList a
737 b' = map f $ blockEnvToList b
738 f (key,elt) = (key, uniqSetToList elt)
742 -- | Annotate a basic block with register liveness information.
747 -> LiveBasicBlock instr
748 -> (BlockMap RegSet, LiveBasicBlock instr)
750 livenessBlock blockmap (BasicBlock block_id instrs)
752 (regsLiveOnEntry, instrs1)
753 = livenessBack emptyUniqSet blockmap [] (reverse instrs)
754 blockmap' = extendBlockEnv blockmap block_id regsLiveOnEntry
756 instrs2 = livenessForward regsLiveOnEntry instrs1
758 output = BasicBlock block_id instrs2
760 in ( blockmap', output)
762 -- | Calculate liveness going forwards,
763 -- filling in when regs are born
767 => RegSet -- regs live on this instr
768 -> [LiveInstr instr] -> [LiveInstr instr]
770 livenessForward _ [] = []
771 livenessForward rsLiveEntry (li@(LiveInstr instr mLive) : lis)
773 = li : livenessForward rsLiveEntry lis
776 , RU _ written <- regUsageOfInstr instr
778 -- Regs that are written to but weren't live on entry to this instruction
779 -- are recorded as being born here.
781 $ filter (\r -> not $ elementOfUniqSet r rsLiveEntry) written
783 rsLiveNext = (rsLiveEntry `unionUniqSets` rsBorn)
784 `minusUniqSet` (liveDieRead live)
785 `minusUniqSet` (liveDieWrite live)
787 in LiveInstr instr (Just live { liveBorn = rsBorn })
788 : livenessForward rsLiveNext lis
790 livenessForward _ _ = panic "RegLiveness.livenessForward: no match"
793 -- | Calculate liveness going backwards,
794 -- filling in when regs die, and what regs are live across each instruction
798 => RegSet -- regs live on this instr
799 -> BlockMap RegSet -- regs live on entry to other BBs
800 -> [LiveInstr instr] -- instructions (accum)
801 -> [LiveInstr instr] -- instructions
802 -> (RegSet, [LiveInstr instr])
804 livenessBack liveregs _ done [] = (liveregs, done)
806 livenessBack liveregs blockmap acc (instr : instrs)
807 = let (liveregs', instr') = liveness1 liveregs blockmap instr
808 in livenessBack liveregs' blockmap (instr' : acc) instrs
811 -- don't bother tagging comments or deltas with liveness
817 -> (RegSet, LiveInstr instr)
819 liveness1 liveregs _ (LiveInstr instr _)
821 = (liveregs, LiveInstr instr Nothing)
823 liveness1 liveregs blockmap (LiveInstr instr _)
826 = (liveregs1, LiveInstr instr
828 { liveBorn = emptyUniqSet
829 , liveDieRead = mkUniqSet r_dying
830 , liveDieWrite = mkUniqSet w_dying }))
833 = (liveregs_br, LiveInstr instr
835 { liveBorn = emptyUniqSet
836 , liveDieRead = mkUniqSet r_dying_br
837 , liveDieWrite = mkUniqSet w_dying }))
840 RU read written = regUsageOfInstr instr
842 -- registers that were written here are dead going backwards.
843 -- registers that were read here are live going backwards.
844 liveregs1 = (liveregs `delListFromUniqSet` written)
845 `addListToUniqSet` read
847 -- registers that are not live beyond this point, are recorded
849 r_dying = [ reg | reg <- read, reg `notElem` written,
850 not (elementOfUniqSet reg liveregs) ]
852 w_dying = [ reg | reg <- written,
853 not (elementOfUniqSet reg liveregs) ]
855 -- union in the live regs from all the jump destinations of this
857 targets = jumpDestsOfInstr instr -- where we go from here
858 not_a_branch = null targets
860 targetLiveRegs target
861 = case lookupBlockEnv blockmap target of
863 Nothing -> emptyRegMap
865 live_from_branch = unionManyUniqSets (map targetLiveRegs targets)
867 liveregs_br = liveregs1 `unionUniqSets` live_from_branch
869 -- registers that are live only in the branch targets should
870 -- be listed as dying here.
871 live_branch_only = live_from_branch `minusUniqSet` liveregs
872 r_dying_br = uniqSetToList (mkUniqSet r_dying `unionUniqSets`