2 module RegAlloc.Graph.SpillCost (
14 lifeMapFromSpillCostInfo
19 import RegAlloc.Liveness
34 import Data.List (nub, minimumBy)
39 = ( VirtualReg -- register name
40 , Int -- number of writes to this reg
41 , Int -- number of reads from this reg
42 , Int) -- number of instrs this reg was live on entry to
45 = UniqFM SpillCostRecord
48 zeroSpillCostInfo :: SpillCostInfo
49 zeroSpillCostInfo = emptyUFM
51 -- | Add two spillCostInfos
52 plusSpillCostInfo :: SpillCostInfo -> SpillCostInfo -> SpillCostInfo
53 plusSpillCostInfo sc1 sc2
54 = plusUFM_C plusSpillCostRecord sc1 sc2
56 plusSpillCostRecord :: SpillCostRecord -> SpillCostRecord -> SpillCostRecord
57 plusSpillCostRecord (r1, a1, b1, c1) (r2, a2, b2, c2)
58 | r1 == r2 = (r1, a1 + a2, b1 + b2, c1 + c2)
59 | otherwise = error "RegSpillCost.plusRegInt: regs don't match"
62 -- | Slurp out information used for determining spill costs
63 -- for each vreg, the number of times it was written to, read from,
64 -- and the number of instructions it was live on entry to (lifetime)
67 :: (Outputable instr, Instruction instr)
71 slurpSpillCostInfo cmm
72 = execState (countCmm cmm) zeroSpillCostInfo
74 countCmm CmmData{} = return ()
75 countCmm (CmmProc info _ _ (ListGraph blocks))
76 = mapM_ (countComp info) blocks
78 countComp info (BasicBlock _ blocks)
79 = mapM_ (countBlock info) blocks
81 -- lookup the regs that are live on entry to this block in
82 -- the info table from the CmmProc
83 countBlock info (BasicBlock blockId instrs)
84 | LiveInfo _ _ blockLive <- info
85 , Just rsLiveEntry <- lookupBlockEnv blockLive blockId
87 , rsLiveEntry_virt <- mapUniqSet (\(RegVirtual vr) -> vr)
88 $ filterUniqSet isVirtualReg rsLiveEntry
90 = countLIs rsLiveEntry_virt instrs
93 = error "RegAlloc.SpillCost.slurpSpillCostInfo: bad block"
98 -- skip over comment and delta pseudo instrs
99 countLIs rsLive (SPILL{} : lis)
100 = countLIs rsLive lis
102 countLIs rsLive (RELOAD{} : lis)
103 = countLIs rsLive lis
105 countLIs rsLive (Instr instr Nothing : lis)
107 = countLIs rsLive lis
110 = pprPanic "RegSpillCost.slurpSpillCostInfo"
111 (text "no liveness information on instruction " <> ppr instr)
113 countLIs rsLiveEntry (Instr instr (Just live) : lis)
115 -- increment the lifetime counts for regs live on entry to this instr
116 mapM_ incLifetime $ uniqSetToList rsLiveEntry
118 -- increment counts for what regs were read/written from
119 let (RU read written) = regUsageOfInstr instr
120 mapM_ incUses $ catMaybes $ map takeVirtualReg $ nub read
121 mapM_ incDefs $ catMaybes $ map takeVirtualReg $ nub written
123 -- compute liveness for entry to next instruction.
125 = mapUniqSet (\(RegVirtual vr) -> vr)
126 $ filterUniqSet isVirtualReg set
128 let liveDieRead_virt = takeVirtuals (liveDieRead live)
129 let liveDieWrite_virt = takeVirtuals (liveDieWrite live)
130 let liveBorn_virt = takeVirtuals (liveBorn live)
133 = rsLiveEntry `minusUniqSet` liveDieRead_virt
136 = (rsLiveAcross `unionUniqSets` liveBorn_virt)
137 `minusUniqSet` liveDieWrite_virt
139 countLIs rsLiveNext lis
141 incDefs reg = modify $ \s -> addToUFM_C plusSpillCostRecord s reg (reg, 1, 0, 0)
142 incUses reg = modify $ \s -> addToUFM_C plusSpillCostRecord s reg (reg, 0, 1, 0)
143 incLifetime reg = modify $ \s -> addToUFM_C plusSpillCostRecord s reg (reg, 0, 0, 1)
146 -- | Choose a node to spill from this graph
150 -> Graph VirtualReg RegClass RealReg
153 chooseSpill info graph
154 = let cost = spillCost_length info graph
155 node = minimumBy (\n1 n2 -> compare (cost $ nodeId n1) (cost $ nodeId n2))
156 $ eltsUFM $ graphMap graph
162 -- | Chaitins spill cost function is:
164 -- cost = sum loadCost * freq (u) + sum storeCost * freq (d)
165 -- u <- uses (v) d <- defs (v)
167 -- There are no loops in our code at the momemnt, so we can set the freq's to 1
168 -- We divide this by the degree if t
171 -- If we don't have live range splitting then Chaitins function performs badly if we have
172 -- lots of nested live ranges and very few registers.
185 -- defs uses degree cost
190 -- v3 has the lowest cost, but if we only have 2 hardregs and we insert spill code for v3
191 -- then this isn't going to improve the colorability of the graph.
193 -- When compiling SHA1, which as very long basic blocks and some vregs with very long live ranges
194 -- the allocator seems to try and spill from the inside out and eventually run out of stack slots.
196 -- Without live range splitting, its's better to spill from the outside in so set the cost of very
197 -- long live ranges to zero
202 -> Graph Reg RegClass Reg
206 spillCost_chaitin info graph reg
207 -- Spilling a live range that only lives for 1 instruction isn't going to help
208 -- us at all - and we definately want to avoid trying to re-spill previously
209 -- inserted spill code.
210 | lifetime <= 1 = 1/0
212 -- It's unlikely that we'll find a reg for a live range this long
213 -- better to spill it straight up and not risk trying to keep it around
214 -- and have to go through the build/color cycle again.
215 | lifetime > allocatableRegsInClass (regClass reg) * 10
218 -- otherwise revert to chaitin's regular cost function.
219 | otherwise = fromIntegral (uses + defs) / fromIntegral (nodeDegree graph reg)
220 where (_, defs, uses, lifetime)
221 = fromMaybe (reg, 0, 0, 0) $ lookupUFM info reg
224 -- Just spill the longest live range.
227 -> Graph VirtualReg RegClass RealReg
231 spillCost_length info _ reg
232 | lifetime <= 1 = 1/0
233 | otherwise = 1 / fromIntegral lifetime
234 where (_, _, _, lifetime)
235 = fromMaybe (reg, 0, 0, 0)
240 lifeMapFromSpillCostInfo :: SpillCostInfo -> UniqFM (VirtualReg, Int)
241 lifeMapFromSpillCostInfo info
243 $ map (\(r, _, _, life) -> (r, (r, life)))
247 -- | Work out the degree (number of neighbors) of this node which have the same class.
249 :: (VirtualReg -> RegClass)
250 -> Graph VirtualReg RegClass RealReg
254 nodeDegree classOfVirtualReg graph reg
255 | Just node <- lookupUFM (graphMap graph) reg
257 , virtConflicts <- length
258 $ filter (\r -> classOfVirtualReg r == classOfVirtualReg reg)
262 = virtConflicts + sizeUniqSet (nodeExclusions node)
268 -- | Show a spill cost record, including the degree from the graph and final calulated spill cos
270 :: (VirtualReg -> RegClass)
272 -> Graph VirtualReg RegClass RealReg
276 pprSpillCostRecord regClass pprReg graph (reg, uses, defs, life)
278 [ pprReg (RegVirtual reg)
282 , ppr $ nodeDegree regClass graph reg
283 , text $ show $ (fromIntegral (uses + defs)
284 / fromIntegral (nodeDegree regClass graph reg) :: Float) ]