PageRenderTime 72ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/compiler/nativeGen/RegAlloc/Linear/Main.hs

https://github.com/crdueck/ghc
Haskell | 883 lines | 460 code | 153 blank | 270 comment | 9 complexity | f60fd856a5beaf429a8e3ce9e8dfa71b MD5 | raw file
  1. -----------------------------------------------------------------------------
  2. --
  3. -- The register allocator
  4. --
  5. -- (c) The University of Glasgow 2004
  6. --
  7. -----------------------------------------------------------------------------
  8. {-
  9. The algorithm is roughly:
  10. 1) Compute strongly connected components of the basic block list.
  11. 2) Compute liveness (mapping from pseudo register to
  12. point(s) of death?).
  13. 3) Walk instructions in each basic block. We keep track of
  14. (a) Free real registers (a bitmap?)
  15. (b) Current assignment of temporaries to machine registers and/or
  16. spill slots (call this the "assignment").
  17. (c) Partial mapping from basic block ids to a virt-to-loc mapping.
  18. When we first encounter a branch to a basic block,
  19. we fill in its entry in this table with the current mapping.
  20. For each instruction:
  21. (a) For each temporary *read* by the instruction:
  22. If the temporary does not have a real register allocation:
  23. - Allocate a real register from the free list. If
  24. the list is empty:
  25. - Find a temporary to spill. Pick one that is
  26. not used in this instruction (ToDo: not
  27. used for a while...)
  28. - generate a spill instruction
  29. - If the temporary was previously spilled,
  30. generate an instruction to read the temp from its spill loc.
  31. (optimisation: if we can see that a real register is going to
  32. be used soon, then don't use it for allocation).
  33. (b) For each real register clobbered by this instruction:
  34. If a temporary resides in it,
  35. If the temporary is live after this instruction,
  36. Move the temporary to another (non-clobbered & free) reg,
  37. or spill it to memory. Mark the temporary as residing
  38. in both memory and a register if it was spilled (it might
  39. need to be read by this instruction).
  40. (ToDo: this is wrong for jump instructions?)
  41. We do this after step (a), because if we start with
  42. movq v1, %rsi
  43. which is an instruction that clobbers %rsi, if v1 currently resides
  44. in %rsi we want to get
  45. movq %rsi, %freereg
  46. movq %rsi, %rsi -- will disappear
  47. instead of
  48. movq %rsi, %freereg
  49. movq %freereg, %rsi
  50. (c) Update the current assignment
  51. (d) If the instruction is a branch:
  52. if the destination block already has a register assignment,
  53. Generate a new block with fixup code and redirect the
  54. jump to the new block.
  55. else,
  56. Update the block id->assignment mapping with the current
  57. assignment.
  58. (e) Delete all register assignments for temps which are read
  59. (only) and die here. Update the free register list.
  60. (f) Mark all registers clobbered by this instruction as not free,
  61. and mark temporaries which have been spilled due to clobbering
  62. as in memory (step (a) marks then as in both mem & reg).
  63. (g) For each temporary *written* by this instruction:
  64. Allocate a real register as for (b), spilling something
  65. else if necessary.
  66. - except when updating the assignment, drop any memory
  67. locations that the temporary was previously in, since
  68. they will be no longer valid after this instruction.
  69. (h) Delete all register assignments for temps which are
  70. written and die here (there should rarely be any). Update
  71. the free register list.
  72. (i) Rewrite the instruction with the new mapping.
  73. (j) For each spilled reg known to be now dead, re-add its stack slot
  74. to the free list.
  75. -}
  76. module RegAlloc.Linear.Main (
  77. regAlloc,
  78. module RegAlloc.Linear.Base,
  79. module RegAlloc.Linear.Stats
  80. ) where
  81. #include "HsVersions.h"
  82. import RegAlloc.Linear.State
  83. import RegAlloc.Linear.Base
  84. import RegAlloc.Linear.StackMap
  85. import RegAlloc.Linear.FreeRegs
  86. import RegAlloc.Linear.Stats
  87. import RegAlloc.Linear.JoinToTargets
  88. import qualified RegAlloc.Linear.PPC.FreeRegs as PPC
  89. import qualified RegAlloc.Linear.SPARC.FreeRegs as SPARC
  90. import qualified RegAlloc.Linear.X86.FreeRegs as X86
  91. import qualified RegAlloc.Linear.X86_64.FreeRegs as X86_64
  92. import TargetReg
  93. import RegAlloc.Liveness
  94. import Instruction
  95. import Reg
  96. import BlockId
  97. import Cmm hiding (RegSet)
  98. import Digraph
  99. import DynFlags
  100. import Unique
  101. import UniqSet
  102. import UniqFM
  103. import UniqSupply
  104. import Outputable
  105. import Platform
  106. import Data.Maybe
  107. import Data.List
  108. import Control.Monad
  109. -- -----------------------------------------------------------------------------
  110. -- Top level of the register allocator
  111. -- Allocate registers
  112. regAlloc
  113. :: (Outputable instr, Instruction instr)
  114. => DynFlags
  115. -> LiveCmmDecl statics instr
  116. -> UniqSM ( NatCmmDecl statics instr
  117. , Maybe Int -- number of extra stack slots required,
  118. -- beyond maxSpillSlots
  119. , Maybe RegAllocStats)
  120. regAlloc _ (CmmData sec d)
  121. = return
  122. ( CmmData sec d
  123. , Nothing
  124. , Nothing )
  125. regAlloc _ (CmmProc (LiveInfo info _ _ _) lbl live [])
  126. = return ( CmmProc info lbl live (ListGraph [])
  127. , Nothing
  128. , Nothing )
  129. regAlloc dflags (CmmProc static lbl live sccs)
  130. | LiveInfo info (Just first_id) (Just block_live) _ <- static
  131. = do
  132. -- do register allocation on each component.
  133. (final_blocks, stats, stack_use)
  134. <- linearRegAlloc dflags first_id block_live sccs
  135. -- make sure the block that was first in the input list
  136. -- stays at the front of the output
  137. let ((first':_), rest')
  138. = partition ((== first_id) . blockId) final_blocks
  139. let max_spill_slots = maxSpillSlots dflags
  140. extra_stack
  141. | stack_use > max_spill_slots
  142. = Just (stack_use - max_spill_slots)
  143. | otherwise
  144. = Nothing
  145. return ( CmmProc info lbl live (ListGraph (first' : rest'))
  146. , extra_stack
  147. , Just stats)
  148. -- bogus. to make non-exhaustive match warning go away.
  149. regAlloc _ (CmmProc _ _ _ _)
  150. = panic "RegAllocLinear.regAlloc: no match"
  151. -- -----------------------------------------------------------------------------
  152. -- Linear sweep to allocate registers
  153. -- | Do register allocation on some basic blocks.
  154. -- But be careful to allocate a block in an SCC only if it has
  155. -- an entry in the block map or it is the first block.
  156. --
  157. linearRegAlloc
  158. :: (Outputable instr, Instruction instr)
  159. => DynFlags
  160. -> BlockId -- ^ the first block
  161. -> BlockMap RegSet -- ^ live regs on entry to each basic block
  162. -> [SCC (LiveBasicBlock instr)] -- ^ instructions annotated with "deaths"
  163. -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
  164. linearRegAlloc dflags first_id block_live sccs
  165. = let platform = targetPlatform dflags
  166. in case platformArch platform of
  167. ArchX86 -> linearRegAlloc' dflags (frInitFreeRegs platform :: X86.FreeRegs) first_id block_live sccs
  168. ArchX86_64 -> linearRegAlloc' dflags (frInitFreeRegs platform :: X86_64.FreeRegs) first_id block_live sccs
  169. ArchSPARC -> linearRegAlloc' dflags (frInitFreeRegs platform :: SPARC.FreeRegs) first_id block_live sccs
  170. ArchPPC -> linearRegAlloc' dflags (frInitFreeRegs platform :: PPC.FreeRegs) first_id block_live sccs
  171. ArchARM _ _ _ -> panic "linearRegAlloc ArchARM"
  172. ArchPPC_64 -> panic "linearRegAlloc ArchPPC_64"
  173. ArchAlpha -> panic "linearRegAlloc ArchAlpha"
  174. ArchMipseb -> panic "linearRegAlloc ArchMipseb"
  175. ArchMipsel -> panic "linearRegAlloc ArchMipsel"
  176. ArchUnknown -> panic "linearRegAlloc ArchUnknown"
  177. linearRegAlloc'
  178. :: (FR freeRegs, Outputable instr, Instruction instr)
  179. => DynFlags
  180. -> freeRegs
  181. -> BlockId -- ^ the first block
  182. -> BlockMap RegSet -- ^ live regs on entry to each basic block
  183. -> [SCC (LiveBasicBlock instr)] -- ^ instructions annotated with "deaths"
  184. -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
  185. linearRegAlloc' dflags initFreeRegs first_id block_live sccs
  186. = do us <- getUs
  187. let (_, stack, stats, blocks) =
  188. runR dflags emptyBlockMap initFreeRegs emptyRegMap (emptyStackMap dflags) us
  189. $ linearRA_SCCs first_id block_live [] sccs
  190. return (blocks, stats, getStackUse stack)
  191. linearRA_SCCs :: (FR freeRegs, Instruction instr, Outputable instr)
  192. => BlockId
  193. -> BlockMap RegSet
  194. -> [NatBasicBlock instr]
  195. -> [SCC (LiveBasicBlock instr)]
  196. -> RegM freeRegs [NatBasicBlock instr]
  197. linearRA_SCCs _ _ blocksAcc []
  198. = return $ reverse blocksAcc
  199. linearRA_SCCs first_id block_live blocksAcc (AcyclicSCC block : sccs)
  200. = do blocks' <- processBlock block_live block
  201. linearRA_SCCs first_id block_live
  202. ((reverse blocks') ++ blocksAcc)
  203. sccs
  204. linearRA_SCCs first_id block_live blocksAcc (CyclicSCC blocks : sccs)
  205. = do
  206. blockss' <- process first_id block_live blocks [] (return []) False
  207. linearRA_SCCs first_id block_live
  208. (reverse (concat blockss') ++ blocksAcc)
  209. sccs
  210. {- from John Dias's patch 2008/10/16:
  211. The linear-scan allocator sometimes allocates a block
  212. before allocating one of its predecessors, which could lead to
  213. inconsistent allocations. Make it so a block is only allocated
  214. if a predecessor has set the "incoming" assignments for the block, or
  215. if it's the procedure's entry block.
  216. BL 2009/02: Careful. If the assignment for a block doesn't get set for
  217. some reason then this function will loop. We should probably do some
  218. more sanity checking to guard against this eventuality.
  219. -}
  220. process :: (FR freeRegs, Instruction instr, Outputable instr)
  221. => BlockId
  222. -> BlockMap RegSet
  223. -> [GenBasicBlock (LiveInstr instr)]
  224. -> [GenBasicBlock (LiveInstr instr)]
  225. -> [[NatBasicBlock instr]]
  226. -> Bool
  227. -> RegM freeRegs [[NatBasicBlock instr]]
  228. process _ _ [] [] accum _
  229. = return $ reverse accum
  230. process first_id block_live [] next_round accum madeProgress
  231. | not madeProgress
  232. {- BUGS: There are so many unreachable blocks in the code the warnings are overwhelming.
  233. pprTrace "RegAlloc.Linear.Main.process: no progress made, bailing out."
  234. ( text "Unreachable blocks:"
  235. $$ vcat (map ppr next_round)) -}
  236. = return $ reverse accum
  237. | otherwise
  238. = process first_id block_live
  239. next_round [] accum False
  240. process first_id block_live (b@(BasicBlock id _) : blocks)
  241. next_round accum madeProgress
  242. = do
  243. block_assig <- getBlockAssigR
  244. if isJust (mapLookup id block_assig)
  245. || id == first_id
  246. then do
  247. b' <- processBlock block_live b
  248. process first_id block_live blocks
  249. next_round (b' : accum) True
  250. else process first_id block_live blocks
  251. (b : next_round) accum madeProgress
  252. -- | Do register allocation on this basic block
  253. --
  254. processBlock
  255. :: (FR freeRegs, Outputable instr, Instruction instr)
  256. => BlockMap RegSet -- ^ live regs on entry to each basic block
  257. -> LiveBasicBlock instr -- ^ block to do register allocation on
  258. -> RegM freeRegs [NatBasicBlock instr] -- ^ block with registers allocated
  259. processBlock block_live (BasicBlock id instrs)
  260. = do initBlock id block_live
  261. (instrs', fixups)
  262. <- linearRA block_live [] [] id instrs
  263. return $ BasicBlock id instrs' : fixups
  264. -- | Load the freeregs and current reg assignment into the RegM state
  265. -- for the basic block with this BlockId.
  266. initBlock :: FR freeRegs
  267. => BlockId -> BlockMap RegSet -> RegM freeRegs ()
  268. initBlock id block_live
  269. = do dflags <- getDynFlags
  270. let platform = targetPlatform dflags
  271. block_assig <- getBlockAssigR
  272. case mapLookup id block_assig of
  273. -- no prior info about this block: we must consider
  274. -- any fixed regs to be allocated, but we can ignore
  275. -- virtual regs (presumably this is part of a loop,
  276. -- and we'll iterate again). The assignment begins
  277. -- empty.
  278. Nothing
  279. -> do -- pprTrace "initFreeRegs" (text $ show initFreeRegs) (return ())
  280. case mapLookup id block_live of
  281. Nothing ->
  282. setFreeRegsR (frInitFreeRegs platform)
  283. Just live ->
  284. setFreeRegsR $ foldr (frAllocateReg platform) (frInitFreeRegs platform) [ r | RegReal r <- uniqSetToList live ]
  285. setAssigR emptyRegMap
  286. -- load info about register assignments leading into this block.
  287. Just (freeregs, assig)
  288. -> do setFreeRegsR freeregs
  289. setAssigR assig
  290. -- | Do allocation for a sequence of instructions.
  291. linearRA
  292. :: (FR freeRegs, Outputable instr, Instruction instr)
  293. => BlockMap RegSet -- ^ map of what vregs are live on entry to each block.
  294. -> [instr] -- ^ accumulator for instructions already processed.
  295. -> [NatBasicBlock instr] -- ^ accumulator for blocks of fixup code.
  296. -> BlockId -- ^ id of the current block, for debugging.
  297. -> [LiveInstr instr] -- ^ liveness annotated instructions in this block.
  298. -> RegM freeRegs
  299. ( [instr] -- instructions after register allocation
  300. , [NatBasicBlock instr]) -- fresh blocks of fixup code.
  301. linearRA _ accInstr accFixup _ []
  302. = return
  303. ( reverse accInstr -- instrs need to be returned in the correct order.
  304. , accFixup) -- it doesn't matter what order the fixup blocks are returned in.
  305. linearRA block_live accInstr accFixups id (instr:instrs)
  306. = do
  307. (accInstr', new_fixups) <- raInsn block_live accInstr id instr
  308. linearRA block_live accInstr' (new_fixups ++ accFixups) id instrs
  309. -- | Do allocation for a single instruction.
  310. raInsn
  311. :: (FR freeRegs, Outputable instr, Instruction instr)
  312. => BlockMap RegSet -- ^ map of what vregs are love on entry to each block.
  313. -> [instr] -- ^ accumulator for instructions already processed.
  314. -> BlockId -- ^ the id of the current block, for debugging
  315. -> LiveInstr instr -- ^ the instr to have its regs allocated, with liveness info.
  316. -> RegM freeRegs
  317. ( [instr] -- new instructions
  318. , [NatBasicBlock instr]) -- extra fixup blocks
  319. raInsn _ new_instrs _ (LiveInstr ii Nothing)
  320. | Just n <- takeDeltaInstr ii
  321. = do setDeltaR n
  322. return (new_instrs, [])
  323. raInsn _ new_instrs _ (LiveInstr ii Nothing)
  324. | isMetaInstr ii
  325. = return (new_instrs, [])
  326. raInsn block_live new_instrs id (LiveInstr (Instr instr) (Just live))
  327. = do
  328. assig <- getAssigR
  329. -- If we have a reg->reg move between virtual registers, where the
  330. -- src register is not live after this instruction, and the dst
  331. -- register does not already have an assignment,
  332. -- and the source register is assigned to a register, not to a spill slot,
  333. -- then we can eliminate the instruction.
  334. -- (we can't eliminate it if the source register is on the stack, because
  335. -- we do not want to use one spill slot for different virtual registers)
  336. case takeRegRegMoveInstr instr of
  337. Just (src,dst) | src `elementOfUniqSet` (liveDieRead live),
  338. isVirtualReg dst,
  339. not (dst `elemUFM` assig),
  340. isRealReg src || isInReg src assig -> do
  341. case src of
  342. (RegReal rr) -> setAssigR (addToUFM assig dst (InReg rr))
  343. -- if src is a fixed reg, then we just map dest to this
  344. -- reg in the assignment. src must be an allocatable reg,
  345. -- otherwise it wouldn't be in r_dying.
  346. _virt -> case lookupUFM assig src of
  347. Nothing -> panic "raInsn"
  348. Just loc ->
  349. setAssigR (addToUFM (delFromUFM assig src) dst loc)
  350. -- we have eliminated this instruction
  351. {-
  352. freeregs <- getFreeRegsR
  353. assig <- getAssigR
  354. pprTrace "raInsn" (text "ELIMINATED: " <> docToSDoc (pprInstr instr)
  355. $$ ppr r_dying <+> ppr w_dying $$ text (show freeregs) $$ ppr assig) $ do
  356. -}
  357. return (new_instrs, [])
  358. _ -> genRaInsn block_live new_instrs id instr
  359. (uniqSetToList $ liveDieRead live)
  360. (uniqSetToList $ liveDieWrite live)
  361. raInsn _ _ _ instr
  362. = pprPanic "raInsn" (text "no match for:" <> ppr instr)
  363. -- ToDo: what can we do about
  364. --
  365. -- R1 = x
  366. -- jump I64[x] // [R1]
  367. --
  368. -- where x is mapped to the same reg as R1. We want to coalesce x and
  369. -- R1, but the register allocator doesn't know whether x will be
  370. -- assigned to again later, in which case x and R1 should be in
  371. -- different registers. Right now we assume the worst, and the
  372. -- assignment to R1 will clobber x, so we'll spill x into another reg,
  373. -- generating another reg->reg move.
  374. isInReg :: Reg -> RegMap Loc -> Bool
  375. isInReg src assig | Just (InReg _) <- lookupUFM assig src = True
  376. | otherwise = False
  377. genRaInsn :: (FR freeRegs, Instruction instr, Outputable instr)
  378. => BlockMap RegSet
  379. -> [instr]
  380. -> BlockId
  381. -> instr
  382. -> [Reg]
  383. -> [Reg]
  384. -> RegM freeRegs ([instr], [NatBasicBlock instr])
  385. genRaInsn block_live new_instrs block_id instr r_dying w_dying = do
  386. dflags <- getDynFlags
  387. let platform = targetPlatform dflags
  388. case regUsageOfInstr platform instr of { RU read written ->
  389. do
  390. let real_written = [ rr | (RegReal rr) <- written ]
  391. let virt_written = [ vr | (RegVirtual vr) <- written ]
  392. -- we don't need to do anything with real registers that are
  393. -- only read by this instr. (the list is typically ~2 elements,
  394. -- so using nub isn't a problem).
  395. let virt_read = nub [ vr | (RegVirtual vr) <- read ]
  396. -- debugging
  397. {- freeregs <- getFreeRegsR
  398. assig <- getAssigR
  399. pprDebugAndThen (defaultDynFlags Settings{ sTargetPlatform=platform }) trace "genRaInsn"
  400. (ppr instr
  401. $$ text "r_dying = " <+> ppr r_dying
  402. $$ text "w_dying = " <+> ppr w_dying
  403. $$ text "virt_read = " <+> ppr virt_read
  404. $$ text "virt_written = " <+> ppr virt_written
  405. $$ text "freeregs = " <+> text (show freeregs)
  406. $$ text "assig = " <+> ppr assig)
  407. $ do
  408. -}
  409. -- (a), (b) allocate real regs for all regs read by this instruction.
  410. (r_spills, r_allocd) <-
  411. allocateRegsAndSpill True{-reading-} virt_read [] [] virt_read
  412. -- (c) save any temporaries which will be clobbered by this instruction
  413. clobber_saves <- saveClobberedTemps real_written r_dying
  414. -- (d) Update block map for new destinations
  415. -- NB. do this before removing dead regs from the assignment, because
  416. -- these dead regs might in fact be live in the jump targets (they're
  417. -- only dead in the code that follows in the current basic block).
  418. (fixup_blocks, adjusted_instr)
  419. <- joinToTargets block_live block_id instr
  420. -- (e) Delete all register assignments for temps which are read
  421. -- (only) and die here. Update the free register list.
  422. releaseRegs r_dying
  423. -- (f) Mark regs which are clobbered as unallocatable
  424. clobberRegs real_written
  425. -- (g) Allocate registers for temporaries *written* (only)
  426. (w_spills, w_allocd) <-
  427. allocateRegsAndSpill False{-writing-} virt_written [] [] virt_written
  428. -- (h) Release registers for temps which are written here and not
  429. -- used again.
  430. releaseRegs w_dying
  431. let
  432. -- (i) Patch the instruction
  433. patch_map
  434. = listToUFM
  435. [ (t, RegReal r)
  436. | (t, r) <- zip virt_read r_allocd
  437. ++ zip virt_written w_allocd ]
  438. patched_instr
  439. = patchRegsOfInstr adjusted_instr patchLookup
  440. patchLookup x
  441. = case lookupUFM patch_map x of
  442. Nothing -> x
  443. Just y -> y
  444. -- (j) free up stack slots for dead spilled regs
  445. -- TODO (can't be bothered right now)
  446. -- erase reg->reg moves where the source and destination are the same.
  447. -- If the src temp didn't die in this instr but happened to be allocated
  448. -- to the same real reg as the destination, then we can erase the move anyway.
  449. let squashed_instr = case takeRegRegMoveInstr patched_instr of
  450. Just (src, dst)
  451. | src == dst -> []
  452. _ -> [patched_instr]
  453. let code = squashed_instr ++ w_spills ++ reverse r_spills
  454. ++ clobber_saves ++ new_instrs
  455. -- pprTrace "patched-code" ((vcat $ map (docToSDoc . pprInstr) code)) $ do
  456. -- pprTrace "pached-fixup" ((ppr fixup_blocks)) $ do
  457. return (code, fixup_blocks)
  458. }
  459. -- -----------------------------------------------------------------------------
  460. -- releaseRegs
  461. releaseRegs :: FR freeRegs => [Reg] -> RegM freeRegs ()
  462. releaseRegs regs = do
  463. dflags <- getDynFlags
  464. let platform = targetPlatform dflags
  465. assig <- getAssigR
  466. free <- getFreeRegsR
  467. let loop _ free _ | free `seq` False = undefined
  468. loop assig free [] = do setAssigR assig; setFreeRegsR free; return ()
  469. loop assig free (RegReal rr : rs) = loop assig (frReleaseReg platform rr free) rs
  470. loop assig free (r:rs) =
  471. case lookupUFM assig r of
  472. Just (InBoth real _) -> loop (delFromUFM assig r)
  473. (frReleaseReg platform real free) rs
  474. Just (InReg real) -> loop (delFromUFM assig r)
  475. (frReleaseReg platform real free) rs
  476. _ -> loop (delFromUFM assig r) free rs
  477. loop assig free regs
  478. -- -----------------------------------------------------------------------------
  479. -- Clobber real registers
  480. -- For each temp in a register that is going to be clobbered:
  481. -- - if the temp dies after this instruction, do nothing
  482. -- - otherwise, put it somewhere safe (another reg if possible,
  483. -- otherwise spill and record InBoth in the assignment).
  484. -- - for allocateRegs on the temps *read*,
  485. -- - clobbered regs are allocatable.
  486. --
  487. -- for allocateRegs on the temps *written*,
  488. -- - clobbered regs are not allocatable.
  489. --
  490. saveClobberedTemps
  491. :: (Outputable instr, Instruction instr, FR freeRegs)
  492. => [RealReg] -- real registers clobbered by this instruction
  493. -> [Reg] -- registers which are no longer live after this insn
  494. -> RegM freeRegs [instr] -- return: instructions to spill any temps that will
  495. -- be clobbered.
  496. saveClobberedTemps [] _
  497. = return []
  498. saveClobberedTemps clobbered dying
  499. = do
  500. assig <- getAssigR
  501. let to_spill
  502. = [ (temp,reg)
  503. | (temp, InReg reg) <- ufmToList assig
  504. , any (realRegsAlias reg) clobbered
  505. , temp `notElem` map getUnique dying ]
  506. (instrs,assig') <- clobber assig [] to_spill
  507. setAssigR assig'
  508. return instrs
  509. where
  510. clobber assig instrs []
  511. = return (instrs, assig)
  512. clobber assig instrs ((temp, reg) : rest)
  513. = do dflags <- getDynFlags
  514. let platform = targetPlatform dflags
  515. freeRegs <- getFreeRegsR
  516. let regclass = targetClassOfRealReg platform reg
  517. freeRegs_thisClass = frGetFreeRegs platform regclass freeRegs
  518. case filter (`notElem` clobbered) freeRegs_thisClass of
  519. -- (1) we have a free reg of the right class that isn't
  520. -- clobbered by this instruction; use it to save the
  521. -- clobbered value.
  522. (my_reg : _) -> do
  523. setFreeRegsR (frAllocateReg platform my_reg freeRegs)
  524. let new_assign = addToUFM assig temp (InReg my_reg)
  525. let instr = mkRegRegMoveInstr platform
  526. (RegReal reg) (RegReal my_reg)
  527. clobber new_assign (instr : instrs) rest
  528. -- (2) no free registers: spill the value
  529. [] -> do
  530. (spill, slot) <- spillR (RegReal reg) temp
  531. -- record why this reg was spilled for profiling
  532. recordSpill (SpillClobber temp)
  533. let new_assign = addToUFM assig temp (InBoth reg slot)
  534. clobber new_assign (spill : instrs) rest
  535. -- | Mark all these real regs as allocated,
  536. -- and kick out their vreg assignments.
  537. --
  538. clobberRegs :: FR freeRegs => [RealReg] -> RegM freeRegs ()
  539. clobberRegs []
  540. = return ()
  541. clobberRegs clobbered
  542. = do dflags <- getDynFlags
  543. let platform = targetPlatform dflags
  544. freeregs <- getFreeRegsR
  545. setFreeRegsR $! foldr (frAllocateReg platform) freeregs clobbered
  546. assig <- getAssigR
  547. setAssigR $! clobber assig (ufmToList assig)
  548. where
  549. -- if the temp was InReg and clobbered, then we will have
  550. -- saved it in saveClobberedTemps above. So the only case
  551. -- we have to worry about here is InBoth. Note that this
  552. -- also catches temps which were loaded up during allocation
  553. -- of read registers, not just those saved in saveClobberedTemps.
  554. clobber assig []
  555. = assig
  556. clobber assig ((temp, InBoth reg slot) : rest)
  557. | any (realRegsAlias reg) clobbered
  558. = clobber (addToUFM assig temp (InMem slot)) rest
  559. clobber assig (_:rest)
  560. = clobber assig rest
  561. -- -----------------------------------------------------------------------------
  562. -- allocateRegsAndSpill
  563. -- Why are we performing a spill?
  564. data SpillLoc = ReadMem StackSlot -- reading from register only in memory
  565. | WriteNew -- writing to a new variable
  566. | WriteMem -- writing to register only in memory
  567. -- Note that ReadNew is not valid, since you don't want to be reading
  568. -- from an uninitialized register. We also don't need the location of
  569. -- the register in memory, since that will be invalidated by the write.
  570. -- Technically, we could coalesce WriteNew and WriteMem into a single
  571. -- entry as well. -- EZY
  572. -- This function does several things:
  573. -- For each temporary referred to by this instruction,
  574. -- we allocate a real register (spilling another temporary if necessary).
  575. -- We load the temporary up from memory if necessary.
  576. -- We also update the register assignment in the process, and
  577. -- the list of free registers and free stack slots.
  578. allocateRegsAndSpill
  579. :: (FR freeRegs, Outputable instr, Instruction instr)
  580. => Bool -- True <=> reading (load up spilled regs)
  581. -> [VirtualReg] -- don't push these out
  582. -> [instr] -- spill insns
  583. -> [RealReg] -- real registers allocated (accum.)
  584. -> [VirtualReg] -- temps to allocate
  585. -> RegM freeRegs ( [instr] , [RealReg])
  586. allocateRegsAndSpill _ _ spills alloc []
  587. = return (spills, reverse alloc)
  588. allocateRegsAndSpill reading keep spills alloc (r:rs)
  589. = do assig <- getAssigR
  590. let doSpill = allocRegsAndSpill_spill reading keep spills alloc r rs assig
  591. case lookupUFM assig r of
  592. -- case (1a): already in a register
  593. Just (InReg my_reg) ->
  594. allocateRegsAndSpill reading keep spills (my_reg:alloc) rs
  595. -- case (1b): already in a register (and memory)
  596. -- NB1. if we're writing this register, update its assignment to be
  597. -- InReg, because the memory value is no longer valid.
  598. -- NB2. This is why we must process written registers here, even if they
  599. -- are also read by the same instruction.
  600. Just (InBoth my_reg _)
  601. -> do when (not reading) (setAssigR (addToUFM assig r (InReg my_reg)))
  602. allocateRegsAndSpill reading keep spills (my_reg:alloc) rs
  603. -- Not already in a register, so we need to find a free one...
  604. Just (InMem slot) | reading -> doSpill (ReadMem slot)
  605. | otherwise -> doSpill WriteMem
  606. Nothing | reading ->
  607. pprPanic "allocateRegsAndSpill: Cannot read from uninitialized register" (ppr r)
  608. -- NOTE: if the input to the NCG contains some
  609. -- unreachable blocks with junk code, this panic
  610. -- might be triggered. Make sure you only feed
  611. -- sensible code into the NCG. In CmmPipeline we
  612. -- call removeUnreachableBlocks at the end for this
  613. -- reason.
  614. | otherwise -> doSpill WriteNew
  615. -- reading is redundant with reason, but we keep it around because it's
  616. -- convenient and it maintains the recursive structure of the allocator. -- EZY
  617. allocRegsAndSpill_spill :: (FR freeRegs, Instruction instr, Outputable instr)
  618. => Bool
  619. -> [VirtualReg]
  620. -> [instr]
  621. -> [RealReg]
  622. -> VirtualReg
  623. -> [VirtualReg]
  624. -> UniqFM Loc
  625. -> SpillLoc
  626. -> RegM freeRegs ([instr], [RealReg])
  627. allocRegsAndSpill_spill reading keep spills alloc r rs assig spill_loc
  628. = do dflags <- getDynFlags
  629. let platform = targetPlatform dflags
  630. freeRegs <- getFreeRegsR
  631. let freeRegs_thisClass = frGetFreeRegs platform (classOfVirtualReg r) freeRegs
  632. case freeRegs_thisClass of
  633. -- case (2): we have a free register
  634. (my_reg : _) ->
  635. do spills' <- loadTemp r spill_loc my_reg spills
  636. setAssigR (addToUFM assig r $! newLocation spill_loc my_reg)
  637. setFreeRegsR $ frAllocateReg platform my_reg freeRegs
  638. allocateRegsAndSpill reading keep spills' (my_reg : alloc) rs
  639. -- case (3): we need to push something out to free up a register
  640. [] ->
  641. do let keep' = map getUnique keep
  642. -- the vregs we could kick out that are already in a slot
  643. let candidates_inBoth
  644. = [ (temp, reg, mem)
  645. | (temp, InBoth reg mem) <- ufmToList assig
  646. , temp `notElem` keep'
  647. , targetClassOfRealReg platform reg == classOfVirtualReg r ]
  648. -- the vregs we could kick out that are only in a reg
  649. -- this would require writing the reg to a new slot before using it.
  650. let candidates_inReg
  651. = [ (temp, reg)
  652. | (temp, InReg reg) <- ufmToList assig
  653. , temp `notElem` keep'
  654. , targetClassOfRealReg platform reg == classOfVirtualReg r ]
  655. let result
  656. -- we have a temporary that is in both register and mem,
  657. -- just free up its register for use.
  658. | (temp, my_reg, slot) : _ <- candidates_inBoth
  659. = do spills' <- loadTemp r spill_loc my_reg spills
  660. let assig1 = addToUFM assig temp (InMem slot)
  661. let assig2 = addToUFM assig1 r $! newLocation spill_loc my_reg
  662. setAssigR assig2
  663. allocateRegsAndSpill reading keep spills' (my_reg:alloc) rs
  664. -- otherwise, we need to spill a temporary that currently
  665. -- resides in a register.
  666. | (temp_to_push_out, (my_reg :: RealReg)) : _
  667. <- candidates_inReg
  668. = do
  669. (spill_insn, slot) <- spillR (RegReal my_reg) temp_to_push_out
  670. let spill_store = (if reading then id else reverse)
  671. [ -- COMMENT (fsLit "spill alloc")
  672. spill_insn ]
  673. -- record that this temp was spilled
  674. recordSpill (SpillAlloc temp_to_push_out)
  675. -- update the register assignment
  676. let assig1 = addToUFM assig temp_to_push_out (InMem slot)
  677. let assig2 = addToUFM assig1 r $! newLocation spill_loc my_reg
  678. setAssigR assig2
  679. -- if need be, load up a spilled temp into the reg we've just freed up.
  680. spills' <- loadTemp r spill_loc my_reg spills
  681. allocateRegsAndSpill reading keep
  682. (spill_store ++ spills')
  683. (my_reg:alloc) rs
  684. -- there wasn't anything to spill, so we're screwed.
  685. | otherwise
  686. = pprPanic ("RegAllocLinear.allocRegsAndSpill: no spill candidates\n")
  687. $ vcat
  688. [ text "allocating vreg: " <> text (show r)
  689. , text "assignment: " <> text (show $ ufmToList assig)
  690. , text "freeRegs: " <> text (show freeRegs)
  691. , text "initFreeRegs: " <> text (show (frInitFreeRegs platform `asTypeOf` freeRegs)) ]
  692. result
  693. -- | Calculate a new location after a register has been loaded.
  694. newLocation :: SpillLoc -> RealReg -> Loc
  695. -- if the tmp was read from a slot, then now its in a reg as well
  696. newLocation (ReadMem slot) my_reg = InBoth my_reg slot
  697. -- writes will always result in only the register being available
  698. newLocation _ my_reg = InReg my_reg
  699. -- | Load up a spilled temporary if we need to (read from memory).
  700. loadTemp
  701. :: (Outputable instr, Instruction instr)
  702. => VirtualReg -- the temp being loaded
  703. -> SpillLoc -- the current location of this temp
  704. -> RealReg -- the hreg to load the temp into
  705. -> [instr]
  706. -> RegM freeRegs [instr]
  707. loadTemp vreg (ReadMem slot) hreg spills
  708. = do
  709. insn <- loadR (RegReal hreg) slot
  710. recordSpill (SpillLoad $ getUnique vreg)
  711. return $ {- COMMENT (fsLit "spill load") : -} insn : spills
  712. loadTemp _ _ _ spills =
  713. return spills