1 -----------------------------------------------------------------------------
3 -- The register liveness determinator
5 -- (c) The University of Glasgow 2004
7 -----------------------------------------------------------------------------
8 {-# OPTIONS -fno-warn-missing-signatures #-}
13 BlockMap, emptyBlockMap,
20 mapBlockTop, mapBlockTopM,
21 mapGenBlockTop, mapGenBlockTopM,
33 #include "HsVersions.h"
53 -----------------------------------------------------------------------------
54 type RegSet = UniqSet Reg
56 type RegMap a = UniqFM a
58 emptyRegMap :: UniqFM a
59 emptyRegMap = emptyUFM
61 type BlockMap a = UniqFM a
63 emptyBlockMap :: UniqFM a
64 emptyBlockMap = emptyUFM
67 -- | A top level thing which carries liveness information.
72 (ListGraph (GenBasicBlock LiveInstr))
73 -- the "instructions" here are actually more blocks,
74 -- single blocks are acyclic
75 -- multiple blocks are taken to be cyclic.
77 -- | An instruction with liveness information.
79 = Instr Instr (Maybe Liveness)
81 -- | Liveness information.
82 -- The regs which die are ones which are no longer live in the *next* instruction
84 -- (NB. if the instruction is a jump, these registers might still be live
85 -- at the jump target(s) - you have to check the liveness at the destination
86 -- block to find out).
90 { liveBorn :: RegSet -- ^ registers born in this instruction (written to for first time).
91 , liveDieRead :: RegSet -- ^ registers that died because they were read for the last time.
92 , liveDieWrite :: RegSet } -- ^ registers that died because they were clobbered by something.
95 -- | Stash regs live on entry to each basic block in the info part of the cmm code.
98 [CmmStatic] -- cmm static stuff
99 (Maybe BlockId) -- id of the first block
100 (BlockMap RegSet) -- argument locals live on entry to this block
102 -- | A basic block with liveness information.
104 = GenBasicBlock LiveInstr
107 instance Outputable LiveInstr where
108 ppr (Instr instr Nothing)
111 ppr (Instr instr (Just live))
115 [ pprRegs (ptext SLIT("# born: ")) (liveBorn live)
116 , pprRegs (ptext SLIT("# r_dying: ")) (liveDieRead live)
117 , pprRegs (ptext SLIT("# w_dying: ")) (liveDieWrite live) ]
120 where pprRegs :: SDoc -> RegSet -> SDoc
122 | isEmptyUniqSet regs = empty
123 | otherwise = name <> (hcat $ punctuate space $ map (docToSDoc . pprUserReg) $ uniqSetToList regs)
126 instance Outputable LiveInfo where
127 ppr (LiveInfo static firstId liveOnEntry)
128 = (vcat $ map ppr static)
129 $$ text "# firstId = " <> ppr firstId
130 $$ text "# liveOnEntry = " <> ppr liveOnEntry
133 -- | map a function across all the basic blocks in this code
136 :: (LiveBasicBlock -> LiveBasicBlock)
137 -> LiveCmmTop -> LiveCmmTop
140 = evalState (mapBlockTopM (\x -> return $ f x) cmm) ()
143 -- | map a function across all the basic blocks in this code (monadic version)
147 => (LiveBasicBlock -> m LiveBasicBlock)
148 -> LiveCmmTop -> m LiveCmmTop
150 mapBlockTopM _ cmm@(CmmData{})
153 mapBlockTopM f (CmmProc header label params (ListGraph comps))
154 = do comps' <- mapM (mapBlockCompM f) comps
155 return $ CmmProc header label params (ListGraph comps')
157 mapBlockCompM f (BasicBlock i blocks)
158 = do blocks' <- mapM f blocks
159 return $ BasicBlock i blocks'
162 -- map a function across all the basic blocks in this code
164 :: (GenBasicBlock i -> GenBasicBlock i)
165 -> (GenCmmTop d h (ListGraph i) -> GenCmmTop d h (ListGraph i))
168 = evalState (mapGenBlockTopM (\x -> return $ f x) cmm) ()
171 -- | map a function across all the basic blocks in this code (monadic version)
174 => (GenBasicBlock i -> m (GenBasicBlock i))
175 -> (GenCmmTop d h (ListGraph i) -> m (GenCmmTop d h (ListGraph i)))
177 mapGenBlockTopM _ cmm@(CmmData{})
180 mapGenBlockTopM f (CmmProc header label params (ListGraph blocks))
181 = do blocks' <- mapM f blocks
182 return $ CmmProc header label params (ListGraph blocks')
185 -- | Slurp out the list of register conflicts and reg-reg moves from this top level thing.
186 -- Slurping of conflicts and moves is wrapped up together so we don't have
187 -- to make two passes over the same code when we want to build the graph.
189 slurpConflicts :: LiveCmmTop -> (Bag (UniqSet Reg), Bag (Reg, Reg))
191 = slurpCmm (emptyBag, emptyBag) live
193 where slurpCmm rs CmmData{} = rs
194 slurpCmm rs (CmmProc info _ _ (ListGraph blocks))
195 = foldl' (slurpComp info) rs blocks
197 slurpComp info rs (BasicBlock _ blocks)
198 = foldl' (slurpBlock info) rs blocks
200 slurpBlock info rs (BasicBlock blockId instrs)
201 | LiveInfo _ _ blockLive <- info
202 , Just rsLiveEntry <- lookupUFM blockLive blockId
203 , (conflicts, moves) <- slurpLIs rsLiveEntry rs instrs
204 = (consBag rsLiveEntry conflicts, moves)
207 = error "RegLiveness.slurpBlock: bad block"
209 slurpLIs rsLive (conflicts, moves) []
210 = (consBag rsLive conflicts, moves)
212 slurpLIs rsLive rs (Instr _ Nothing : lis) = slurpLIs rsLive rs lis
214 slurpLIs rsLiveEntry (conflicts, moves) (Instr instr (Just live) : lis)
216 -- regs that die because they are read for the last time at the start of an instruction
217 -- are not live across it.
218 rsLiveAcross = rsLiveEntry `minusUniqSet` (liveDieRead live)
220 -- regs live on entry to the next instruction.
221 -- be careful of orphans, make sure to delete dying regs _after_ unioning
222 -- in the ones that are born here.
223 rsLiveNext = (rsLiveAcross `unionUniqSets` (liveBorn live))
224 `minusUniqSet` (liveDieWrite live)
226 -- orphan vregs are the ones that die in the same instruction they are born in.
227 -- these are likely to be results that are never used, but we still
228 -- need to assign a hreg to them..
229 rsOrphans = intersectUniqSets
231 (unionUniqSets (liveDieWrite live) (liveDieRead live))
234 rsConflicts = unionUniqSets rsLiveNext rsOrphans
236 in case isRegRegMove instr of
237 Just rr -> slurpLIs rsLiveNext
238 ( consBag rsConflicts conflicts
239 , consBag rr moves) lis
241 Nothing -> slurpLIs rsLiveNext
242 ( consBag rsConflicts conflicts
246 -- | Strip away liveness information, yielding NatCmmTop
248 stripLive :: LiveCmmTop -> NatCmmTop
252 where stripCmm (CmmData sec ds) = CmmData sec ds
253 stripCmm (CmmProc (LiveInfo info _ _) label params (ListGraph comps))
254 = CmmProc info label params (ListGraph $ concatMap stripComp comps)
256 stripComp (BasicBlock _ blocks) = map stripBlock blocks
257 stripBlock (BasicBlock i instrs) = BasicBlock i (map stripLI instrs)
258 stripLI (Instr instr _) = instr
261 -- | Make real spill instructions out of SPILL, RELOAD pseudos
263 spillNatBlock :: NatBasicBlock -> NatBasicBlock
264 spillNatBlock (BasicBlock i is)
265 = BasicBlock i instrs'
267 = runState (spillNat [] is) 0
270 = return (reverse acc)
272 spillNat acc (DELTA i : instrs)
276 spillNat acc (SPILL reg slot : instrs)
278 spillNat (mkSpillInstr reg delta slot : acc) instrs
280 spillNat acc (RELOAD slot reg : instrs)
282 spillNat (mkLoadInstr reg delta slot : acc) instrs
284 spillNat acc (instr : instrs)
285 = spillNat (instr : acc) instrs
288 -- | Slurp out a map of how many times each register was live upon entry to an instruction.
292 -> UniqFM (Reg, Int) -- ^ reg -> (reg, count)
295 = countCmm emptyUFM cmm
297 countCmm fm CmmData{} = fm
298 countCmm fm (CmmProc info _ _ (ListGraph blocks))
299 = foldl' (countComp info) fm blocks
301 countComp info fm (BasicBlock _ blocks)
302 = foldl' (countBlock info) fm blocks
304 countBlock info fm (BasicBlock blockId instrs)
305 | LiveInfo _ _ blockLive <- info
306 , Just rsLiveEntry <- lookupUFM blockLive blockId
307 = countLIs rsLiveEntry fm instrs
310 = error "RegLiveness.countBlock: bad block"
312 countLIs _ fm [] = fm
313 countLIs rsLive fm (Instr _ Nothing : lis) = countLIs rsLive fm lis
315 countLIs rsLiveEntry fm (Instr _ (Just live) : lis)
317 rsLiveAcross = rsLiveEntry `minusUniqSet` (liveDieRead live)
319 rsLiveNext = (rsLiveAcross `unionUniqSets` (liveBorn live))
320 `minusUniqSet` (liveDieWrite live)
322 add r fm = addToUFM_C
323 (\(r1, l1) (_, l2) -> (r1, l1 + l2))
326 fm' = foldUniqSet add fm rsLiveEntry
327 in countLIs rsLiveNext fm' lis
330 -- | Erase Delta instructions.
332 eraseDeltasLive :: LiveCmmTop -> LiveCmmTop
334 = mapBlockTop eraseBlock cmm
336 isDelta (DELTA _) = True
339 eraseBlock (BasicBlock id lis)
341 $ filter (\(Instr i _) -> not $ isDelta i)
345 -- | Patch the registers in this code according to this register mapping.
346 -- also erase reg -> reg moves when the reg is the same.
347 -- also erase reg -> reg moves when the destination dies in this instr.
351 -> LiveCmmTop -> LiveCmmTop
353 patchEraseLive patchF cmm
356 patchCmm cmm@CmmData{} = cmm
358 patchCmm (CmmProc info label params (ListGraph comps))
359 | LiveInfo static id blockMap <- info
360 = let patchRegSet set = mkUniqSet $ map patchF $ uniqSetToList set
361 blockMap' = mapUFM patchRegSet blockMap
363 info' = LiveInfo static id blockMap'
364 in CmmProc info' label params $ ListGraph $ map patchComp comps
366 patchComp (BasicBlock id blocks)
367 = BasicBlock id $ map patchBlock blocks
369 patchBlock (BasicBlock id lis)
370 = BasicBlock id $ patchInstrs lis
373 patchInstrs (li : lis)
375 | Instr i (Just live) <- li'
376 , Just (r1, r2) <- isRegRegMove i
381 = li' : patchInstrs lis
383 where li' = patchRegsLiveInstr patchF li
386 -- source and destination regs are the same
389 -- desination reg is never used
390 | elementOfUniqSet r2 (liveBorn live)
391 , elementOfUniqSet r2 (liveDieRead live) || elementOfUniqSet r2 (liveDieWrite live)
397 -- | Patch registers in this LiveInstr, including the liveness information.
401 -> LiveInstr -> LiveInstr
403 patchRegsLiveInstr patchF li
406 -> Instr (patchRegs instr patchF) Nothing
408 Instr instr (Just live)
410 (patchRegs instr patchF)
412 { -- WARNING: have to go via lists here because patchF changes the uniq in the Reg
413 liveBorn = mkUniqSet $ map patchF $ uniqSetToList $ liveBorn live
414 , liveDieRead = mkUniqSet $ map patchF $ uniqSetToList $ liveDieRead live
415 , liveDieWrite = mkUniqSet $ map patchF $ uniqSetToList $ liveDieWrite live })
418 ---------------------------------------------------------------------------------
419 -- Annotate code with register liveness information
425 regLiveness (CmmData i d)
426 = returnUs $ CmmData i d
428 regLiveness (CmmProc info lbl params (ListGraph []))
430 (LiveInfo info Nothing emptyUFM)
431 lbl params (ListGraph [])
433 regLiveness (CmmProc info lbl params (ListGraph blocks@(first : _)))
434 = let first_id = blockId first
435 sccs = sccBlocks blocks
436 (ann_sccs, block_live) = computeLiveness sccs
439 = map (\scc -> case scc of
440 AcyclicSCC b@(BasicBlock l _) -> BasicBlock l [b]
441 CyclicSCC bs@(BasicBlock l _ : _) -> BasicBlock l bs
443 -> panic "RegLiveness.regLiveness: no blocks in scc list")
446 in returnUs $ CmmProc
447 (LiveInfo info (Just first_id) block_live)
448 lbl params (ListGraph liveBlocks)
451 sccBlocks :: [NatBasicBlock] -> [SCC NatBasicBlock]
452 sccBlocks blocks = stronglyConnComp graph
454 getOutEdges :: [Instr] -> [BlockId]
455 getOutEdges instrs = foldl' (\a x -> jumpDests x a) [] instrs
457 graph = [ (block, getUnique id, map getUnique (getOutEdges instrs))
458 | block@(BasicBlock id instrs) <- blocks ]
461 -- -----------------------------------------------------------------------------
462 -- Computing liveness
465 :: [SCC NatBasicBlock]
466 -> ([SCC LiveBasicBlock], -- instructions annotated with list of registers
467 -- which are "dead after this instruction".
468 BlockMap RegSet) -- blocks annontated with set of live registers
469 -- on entry to the block.
471 -- NOTE: on entry, the SCCs are in "reverse" order: later blocks may transfer
472 -- control to earlier ones only. The SCCs returned are in the *opposite*
473 -- order, which is exactly what we want for the next pass.
476 = livenessSCCs emptyBlockMap [] sccs
481 -> [SCC LiveBasicBlock] -- accum
482 -> [SCC NatBasicBlock]
483 -> ([SCC LiveBasicBlock], BlockMap RegSet)
485 livenessSCCs blockmap done [] = (done, blockmap)
487 livenessSCCs blockmap done (AcyclicSCC block : sccs)
488 = let (blockmap', block') = livenessBlock blockmap block
489 in livenessSCCs blockmap' (AcyclicSCC block' : done) sccs
491 livenessSCCs blockmap done
492 (CyclicSCC blocks : sccs) =
493 livenessSCCs blockmap' (CyclicSCC blocks':done) sccs
494 where (blockmap', blocks')
495 = iterateUntilUnchanged linearLiveness equalBlockMaps
498 iterateUntilUnchanged
499 :: (a -> b -> (a,c)) -> (a -> a -> Bool)
503 iterateUntilUnchanged f eq a b
506 groupBy (\(a1, _) (a2, _) -> eq a1 a2) $
507 iterate (\(a, _) -> f a b) $
508 (a, error "RegisterAlloc.livenessSCCs")
511 linearLiveness :: BlockMap RegSet -> [NatBasicBlock]
512 -> (BlockMap RegSet, [LiveBasicBlock])
513 linearLiveness = mapAccumL livenessBlock
515 -- probably the least efficient way to compare two
516 -- BlockMaps for equality.
519 where a' = map f $ ufmToList a
520 b' = map f $ ufmToList b
521 f (key,elt) = (key, uniqSetToList elt)
525 -- | Annotate a basic block with register liveness information.
530 -> (BlockMap RegSet, LiveBasicBlock)
532 livenessBlock blockmap (BasicBlock block_id instrs)
534 (regsLiveOnEntry, instrs1)
535 = livenessBack emptyUniqSet blockmap [] (reverse instrs)
536 blockmap' = addToUFM blockmap block_id regsLiveOnEntry
538 instrs2 = livenessForward regsLiveOnEntry instrs1
540 output = BasicBlock block_id instrs2
542 in ( blockmap', output)
544 -- | Calculate liveness going forwards,
545 -- filling in when regs are born
548 :: RegSet -- regs live on this instr
549 -> [LiveInstr] -> [LiveInstr]
551 livenessForward _ [] = []
552 livenessForward rsLiveEntry (li@(Instr instr mLive) : lis)
554 = li : livenessForward rsLiveEntry lis
557 , RU _ written <- regUsage instr
559 -- Regs that are written to but weren't live on entry to this instruction
560 -- are recorded as being born here.
562 $ filter (\r -> not $ elementOfUniqSet r rsLiveEntry) written
564 rsLiveNext = (rsLiveEntry `unionUniqSets` rsBorn)
565 `minusUniqSet` (liveDieRead live)
566 `minusUniqSet` (liveDieWrite live)
568 in Instr instr (Just live { liveBorn = rsBorn })
569 : livenessForward rsLiveNext lis
571 livenessForward _ _ = panic "RegLiveness.livenessForward: no match"
574 -- | Calculate liveness going backwards,
575 -- filling in when regs die, and what regs are live across each instruction
578 :: RegSet -- regs live on this instr
579 -> BlockMap RegSet -- regs live on entry to other BBs
580 -> [LiveInstr] -- instructions (accum)
581 -> [Instr] -- instructions
582 -> (RegSet, [LiveInstr])
584 livenessBack liveregs _ done [] = (liveregs, done)
586 livenessBack liveregs blockmap acc (instr : instrs)
587 = let (liveregs', instr') = liveness1 liveregs blockmap instr
588 in livenessBack liveregs' blockmap (instr' : acc) instrs
590 -- don't bother tagging comments or deltas with liveness
591 liveness1 liveregs _ (instr@COMMENT{})
592 = (liveregs, Instr instr Nothing)
594 liveness1 liveregs _ (instr@DELTA{})
595 = (liveregs, Instr instr Nothing)
597 liveness1 liveregs blockmap instr
600 = (liveregs1, Instr instr
602 { liveBorn = emptyUniqSet
603 , liveDieRead = mkUniqSet r_dying
604 , liveDieWrite = mkUniqSet w_dying }))
607 = (liveregs_br, Instr instr
609 { liveBorn = emptyUniqSet
610 , liveDieRead = mkUniqSet r_dying_br
611 , liveDieWrite = mkUniqSet w_dying }))
614 RU read written = regUsage instr
616 -- registers that were written here are dead going backwards.
617 -- registers that were read here are live going backwards.
618 liveregs1 = (liveregs `delListFromUniqSet` written)
619 `addListToUniqSet` read
621 -- registers that are not live beyond this point, are recorded
623 r_dying = [ reg | reg <- read, reg `notElem` written,
624 not (elementOfUniqSet reg liveregs) ]
626 w_dying = [ reg | reg <- written,
627 not (elementOfUniqSet reg liveregs) ]
629 -- union in the live regs from all the jump destinations of this
631 targets = jumpDests instr [] -- where we go from here
632 not_a_branch = null targets
634 targetLiveRegs target
635 = case lookupUFM blockmap target of
637 Nothing -> emptyBlockMap
639 live_from_branch = unionManyUniqSets (map targetLiveRegs targets)
641 liveregs_br = liveregs1 `unionUniqSets` live_from_branch
643 -- registers that are live only in the branch targets should
644 -- be listed as dying here.
645 live_branch_only = live_from_branch `minusUniqSet` liveregs
646 r_dying_br = uniqSetToList (mkUniqSet r_dying `unionUniqSets`