2 module RegAlloc.Graph.SpillCost (
14 lifeMapFromSpillCostInfo
19 import RegAlloc.Liveness
30 import Digraph (flattenSCCs)
34 import Data.List (nub, minimumBy)
38 = ( VirtualReg -- register name
39 , Int -- number of writes to this reg
40 , Int -- number of reads from this reg
41 , Int) -- number of instrs this reg was live on entry to
44 = UniqFM SpillCostRecord
47 zeroSpillCostInfo :: SpillCostInfo
48 zeroSpillCostInfo = emptyUFM
50 -- | Add two spillCostInfos
51 plusSpillCostInfo :: SpillCostInfo -> SpillCostInfo -> SpillCostInfo
52 plusSpillCostInfo sc1 sc2
53 = plusUFM_C plusSpillCostRecord sc1 sc2
55 plusSpillCostRecord :: SpillCostRecord -> SpillCostRecord -> SpillCostRecord
56 plusSpillCostRecord (r1, a1, b1, c1) (r2, a2, b2, c2)
57 | r1 == r2 = (r1, a1 + a2, b1 + b2, c1 + c2)
58 | otherwise = error "RegSpillCost.plusRegInt: regs don't match"
61 -- | Slurp out information used for determining spill costs
62 -- for each vreg, the number of times it was written to, read from,
63 -- and the number of instructions it was live on entry to (lifetime)
66 :: (Outputable instr, Instruction instr)
70 slurpSpillCostInfo cmm
71 = execState (countCmm cmm) zeroSpillCostInfo
73 countCmm CmmData{} = return ()
74 countCmm (CmmProc info _ sccs)
75 = mapM_ (countBlock info)
78 -- lookup the regs that are live on entry to this block in
79 -- the info table from the CmmProc
80 countBlock info (BasicBlock blockId instrs)
81 | LiveInfo _ _ (Just blockLive) _ <- info
82 , Just rsLiveEntry <- mapLookup blockId blockLive
83 , rsLiveEntry_virt <- takeVirtuals rsLiveEntry
84 = countLIs rsLiveEntry_virt instrs
87 = error "RegAlloc.SpillCost.slurpSpillCostInfo: bad block"
92 -- skip over comment and delta pseudo instrs
93 countLIs rsLive (LiveInstr instr Nothing : lis)
98 = pprPanic "RegSpillCost.slurpSpillCostInfo"
99 (text "no liveness information on instruction " <> ppr instr)
101 countLIs rsLiveEntry (LiveInstr instr (Just live) : lis)
103 -- increment the lifetime counts for regs live on entry to this instr
104 mapM_ incLifetime $ uniqSetToList rsLiveEntry
106 -- increment counts for what regs were read/written from
107 let (RU read written) = regUsageOfInstr instr
108 mapM_ incUses $ catMaybes $ map takeVirtualReg $ nub read
109 mapM_ incDefs $ catMaybes $ map takeVirtualReg $ nub written
111 -- compute liveness for entry to next instruction.
112 let liveDieRead_virt = takeVirtuals (liveDieRead live)
113 let liveDieWrite_virt = takeVirtuals (liveDieWrite live)
114 let liveBorn_virt = takeVirtuals (liveBorn live)
117 = rsLiveEntry `minusUniqSet` liveDieRead_virt
120 = (rsLiveAcross `unionUniqSets` liveBorn_virt)
121 `minusUniqSet` liveDieWrite_virt
123 countLIs rsLiveNext lis
125 incDefs reg = modify $ \s -> addToUFM_C plusSpillCostRecord s reg (reg, 1, 0, 0)
126 incUses reg = modify $ \s -> addToUFM_C plusSpillCostRecord s reg (reg, 0, 1, 0)
127 incLifetime reg = modify $ \s -> addToUFM_C plusSpillCostRecord s reg (reg, 0, 0, 1)
130 takeVirtuals :: UniqSet Reg -> UniqSet VirtualReg
131 takeVirtuals set = mapUniqSet get_virtual
132 $ filterUniqSet isVirtualReg set
134 get_virtual (RegVirtual vr) = vr
135 get_virtual _ = panic "getVirt"
137 -- | Choose a node to spill from this graph
141 -> Graph VirtualReg RegClass RealReg
144 chooseSpill info graph
145 = let cost = spillCost_length info graph
146 node = minimumBy (\n1 n2 -> compare (cost $ nodeId n1) (cost $ nodeId n2))
147 $ eltsUFM $ graphMap graph
153 -- | Chaitins spill cost function is:
155 -- cost = sum loadCost * freq (u) + sum storeCost * freq (d)
156 -- u <- uses (v) d <- defs (v)
158 -- There are no loops in our code at the momemnt, so we can set the freq's to 1
159 -- We divide this by the degree if t
162 -- If we don't have live range splitting then Chaitins function performs badly if we have
163 -- lots of nested live ranges and very few registers.
176 -- defs uses degree cost
181 -- v3 has the lowest cost, but if we only have 2 hardregs and we insert spill code for v3
182 -- then this isn't going to improve the colorability of the graph.
184 -- When compiling SHA1, which as very long basic blocks and some vregs with very long live ranges
185 -- the allocator seems to try and spill from the inside out and eventually run out of stack slots.
187 -- Without live range splitting, its's better to spill from the outside in so set the cost of very
188 -- long live ranges to zero
193 -> Graph Reg RegClass Reg
197 spillCost_chaitin info graph reg
198 -- Spilling a live range that only lives for 1 instruction isn't going to help
199 -- us at all - and we definately want to avoid trying to re-spill previously
200 -- inserted spill code.
201 | lifetime <= 1 = 1/0
203 -- It's unlikely that we'll find a reg for a live range this long
204 -- better to spill it straight up and not risk trying to keep it around
205 -- and have to go through the build/color cycle again.
206 | lifetime > allocatableRegsInClass (regClass reg) * 10
209 -- otherwise revert to chaitin's regular cost function.
210 | otherwise = fromIntegral (uses + defs) / fromIntegral (nodeDegree graph reg)
211 where (_, defs, uses, lifetime)
212 = fromMaybe (reg, 0, 0, 0) $ lookupUFM info reg
215 -- Just spill the longest live range.
218 -> Graph VirtualReg RegClass RealReg
222 spillCost_length info _ reg
223 | lifetime <= 1 = 1/0
224 | otherwise = 1 / fromIntegral lifetime
225 where (_, _, _, lifetime)
226 = fromMaybe (reg, 0, 0, 0)
231 lifeMapFromSpillCostInfo :: SpillCostInfo -> UniqFM (VirtualReg, Int)
232 lifeMapFromSpillCostInfo info
234 $ map (\(r, _, _, life) -> (r, (r, life)))
238 -- | Work out the degree (number of neighbors) of this node which have the same class.
240 :: (VirtualReg -> RegClass)
241 -> Graph VirtualReg RegClass RealReg
245 nodeDegree classOfVirtualReg graph reg
246 | Just node <- lookupUFM (graphMap graph) reg
248 , virtConflicts <- length
249 $ filter (\r -> classOfVirtualReg r == classOfVirtualReg reg)
253 = virtConflicts + sizeUniqSet (nodeExclusions node)
259 -- | Show a spill cost record, including the degree from the graph and final calulated spill cos
261 :: (VirtualReg -> RegClass)
263 -> Graph VirtualReg RegClass RealReg
267 pprSpillCostRecord regClass pprReg graph (reg, uses, defs, life)
269 [ pprReg (RegVirtual reg)
273 , ppr $ nodeDegree regClass graph reg
274 , text $ show $ (fromIntegral (uses + defs)
275 / fromIntegral (nodeDegree regClass graph reg) :: Float) ]