2 -- | Register coalescing.
25 -- | Do register coalescing on this top level thing
26 -- For Reg -> Reg moves, if the first reg dies at the same time the second reg is born
27 -- then the mov only serves to join live ranges. The two regs can be renamed to be
28 -- the same and the move instruction safely erased.
30 regCoalesce :: [LiveCmmTop] -> UniqSM [LiveCmmTop]
33 let joins = foldl' unionBags emptyBag
34 $ map slurpJoinMovs code
36 let alloc = foldl' buildAlloc emptyUFM
39 let patched = map (patchEraseLive (sinkReg alloc)) code
44 buildAlloc :: UniqFM Reg -> (Reg, Reg) -> UniqFM Reg
45 buildAlloc fm (r1, r2)
46 = let rmin = min r1 r2
48 in addToUFM fm rmax rmin
50 sinkReg :: UniqFM Reg -> Reg -> Reg
52 = case lookupUFM fm r of
54 Just r' -> sinkReg fm r'
57 -- | Slurp out mov instructions that only serve to join live ranges.
58 -- During a mov, if the source reg dies and the destiation reg is born
59 -- then we can rename the two regs to the same thing and eliminate the move.
61 slurpJoinMovs :: LiveCmmTop -> Bag (Reg, Reg)
63 = slurpCmm emptyBag live
65 slurpCmm rs CmmData{} = rs
66 slurpCmm rs (CmmProc _ _ _ blocks) = foldl' slurpComp rs blocks
67 slurpComp rs (BasicBlock i blocks) = foldl' slurpBlock rs blocks
68 slurpBlock rs (BasicBlock i instrs) = foldl' slurpLI rs instrs
70 slurpLI rs (Instr _ Nothing) = rs
71 slurpLI rs (Instr instr (Just live))
72 | Just (r1, r2) <- isRegRegMove instr
73 , elementOfUniqSet r1 $ liveDieRead live
74 , elementOfUniqSet r2 $ liveBorn live
76 -- only coalesce movs between two virtuals for now, else we end up with
77 -- allocatable regs in the live regs list..
78 , isVirtualReg r1 && isVirtualReg r2