PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/carter/ghc
Haskell | 879 lines | 457 code | 153 blank | 269 comment | 9 complexity | 37a98b8757276669fc7ec66c7b5faeb1 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 OldCmm 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 [])
  126. = return ( CmmProc info lbl (ListGraph [])
  127. , Nothing
  128. , Nothing )
  129. regAlloc dflags (CmmProc static lbl 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 (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. ArchUnknown -> panic "linearRegAlloc ArchUnknown"
  174. linearRegAlloc'
  175. :: (FR freeRegs, Outputable instr, Instruction instr)
  176. => DynFlags
  177. -> freeRegs
  178. -> BlockId -- ^ the first block
  179. -> BlockMap RegSet -- ^ live regs on entry to each basic block
  180. -> [SCC (LiveBasicBlock instr)] -- ^ instructions annotated with "deaths"
  181. -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
  182. linearRegAlloc' dflags initFreeRegs first_id block_live sccs
  183. = do us <- getUs
  184. let (_, stack, stats, blocks) =
  185. runR dflags emptyBlockMap initFreeRegs emptyRegMap (emptyStackMap dflags) us
  186. $ linearRA_SCCs first_id block_live [] sccs
  187. return (blocks, stats, getStackUse stack)
  188. linearRA_SCCs :: (FR freeRegs, Instruction instr, Outputable instr)
  189. => BlockId
  190. -> BlockMap RegSet
  191. -> [NatBasicBlock instr]
  192. -> [SCC (LiveBasicBlock instr)]
  193. -> RegM freeRegs [NatBasicBlock instr]
  194. linearRA_SCCs _ _ blocksAcc []
  195. = return $ reverse blocksAcc
  196. linearRA_SCCs first_id block_live blocksAcc (AcyclicSCC block : sccs)
  197. = do blocks' <- processBlock block_live block
  198. linearRA_SCCs first_id block_live
  199. ((reverse blocks') ++ blocksAcc)
  200. sccs
  201. linearRA_SCCs first_id block_live blocksAcc (CyclicSCC blocks : sccs)
  202. = do
  203. blockss' <- process first_id block_live blocks [] (return []) False
  204. linearRA_SCCs first_id block_live
  205. (reverse (concat blockss') ++ blocksAcc)
  206. sccs
  207. {- from John Dias's patch 2008/10/16:
  208. The linear-scan allocator sometimes allocates a block
  209. before allocating one of its predecessors, which could lead to
  210. inconsistent allocations. Make it so a block is only allocated
  211. if a predecessor has set the "incoming" assignments for the block, or
  212. if it's the procedure's entry block.
  213. BL 2009/02: Careful. If the assignment for a block doesn't get set for
  214. some reason then this function will loop. We should probably do some
  215. more sanity checking to guard against this eventuality.
  216. -}
  217. process :: (FR freeRegs, Instruction instr, Outputable instr)
  218. => BlockId
  219. -> BlockMap RegSet
  220. -> [GenBasicBlock (LiveInstr instr)]
  221. -> [GenBasicBlock (LiveInstr instr)]
  222. -> [[NatBasicBlock instr]]
  223. -> Bool
  224. -> RegM freeRegs [[NatBasicBlock instr]]
  225. process _ _ [] [] accum _
  226. = return $ reverse accum
  227. process first_id block_live [] next_round accum madeProgress
  228. | not madeProgress
  229. {- BUGS: There are so many unreachable blocks in the code the warnings are overwhelming.
  230. pprTrace "RegAlloc.Linear.Main.process: no progress made, bailing out."
  231. ( text "Unreachable blocks:"
  232. $$ vcat (map ppr next_round)) -}
  233. = return $ reverse accum
  234. | otherwise
  235. = process first_id block_live
  236. next_round [] accum False
  237. process first_id block_live (b@(BasicBlock id _) : blocks)
  238. next_round accum madeProgress
  239. = do
  240. block_assig <- getBlockAssigR
  241. if isJust (mapLookup id block_assig)
  242. || id == first_id
  243. then do
  244. b' <- processBlock block_live b
  245. process first_id block_live blocks
  246. next_round (b' : accum) True
  247. else process first_id block_live blocks
  248. (b : next_round) accum madeProgress
  249. -- | Do register allocation on this basic block
  250. --
  251. processBlock
  252. :: (FR freeRegs, Outputable instr, Instruction instr)
  253. => BlockMap RegSet -- ^ live regs on entry to each basic block
  254. -> LiveBasicBlock instr -- ^ block to do register allocation on
  255. -> RegM freeRegs [NatBasicBlock instr] -- ^ block with registers allocated
  256. processBlock block_live (BasicBlock id instrs)
  257. = do initBlock id block_live
  258. (instrs', fixups)
  259. <- linearRA block_live [] [] id instrs
  260. return $ BasicBlock id instrs' : fixups
  261. -- | Load the freeregs and current reg assignment into the RegM state
  262. -- for the basic block with this BlockId.
  263. initBlock :: FR freeRegs
  264. => BlockId -> BlockMap RegSet -> RegM freeRegs ()
  265. initBlock id block_live
  266. = do dflags <- getDynFlags
  267. let platform = targetPlatform dflags
  268. block_assig <- getBlockAssigR
  269. case mapLookup id block_assig of
  270. -- no prior info about this block: we must consider
  271. -- any fixed regs to be allocated, but we can ignore
  272. -- virtual regs (presumably this is part of a loop,
  273. -- and we'll iterate again). The assignment begins
  274. -- empty.
  275. Nothing
  276. -> do -- pprTrace "initFreeRegs" (text $ show initFreeRegs) (return ())
  277. case mapLookup id block_live of
  278. Nothing ->
  279. setFreeRegsR (frInitFreeRegs platform)
  280. Just live ->
  281. setFreeRegsR $ foldr (frAllocateReg platform) (frInitFreeRegs platform) [ r | RegReal r <- uniqSetToList live ]
  282. setAssigR emptyRegMap
  283. -- load info about register assignments leading into this block.
  284. Just (freeregs, assig)
  285. -> do setFreeRegsR freeregs
  286. setAssigR assig
  287. -- | Do allocation for a sequence of instructions.
  288. linearRA
  289. :: (FR freeRegs, Outputable instr, Instruction instr)
  290. => BlockMap RegSet -- ^ map of what vregs are live on entry to each block.
  291. -> [instr] -- ^ accumulator for instructions already processed.
  292. -> [NatBasicBlock instr] -- ^ accumulator for blocks of fixup code.
  293. -> BlockId -- ^ id of the current block, for debugging.
  294. -> [LiveInstr instr] -- ^ liveness annotated instructions in this block.
  295. -> RegM freeRegs
  296. ( [instr] -- instructions after register allocation
  297. , [NatBasicBlock instr]) -- fresh blocks of fixup code.
  298. linearRA _ accInstr accFixup _ []
  299. = return
  300. ( reverse accInstr -- instrs need to be returned in the correct order.
  301. , accFixup) -- it doesn't matter what order the fixup blocks are returned in.
  302. linearRA block_live accInstr accFixups id (instr:instrs)
  303. = do
  304. (accInstr', new_fixups) <- raInsn block_live accInstr id instr
  305. linearRA block_live accInstr' (new_fixups ++ accFixups) id instrs
  306. -- | Do allocation for a single instruction.
  307. raInsn
  308. :: (FR freeRegs, Outputable instr, Instruction instr)
  309. => BlockMap RegSet -- ^ map of what vregs are love on entry to each block.
  310. -> [instr] -- ^ accumulator for instructions already processed.
  311. -> BlockId -- ^ the id of the current block, for debugging
  312. -> LiveInstr instr -- ^ the instr to have its regs allocated, with liveness info.
  313. -> RegM freeRegs
  314. ( [instr] -- new instructions
  315. , [NatBasicBlock instr]) -- extra fixup blocks
  316. raInsn _ new_instrs _ (LiveInstr ii Nothing)
  317. | Just n <- takeDeltaInstr ii
  318. = do setDeltaR n
  319. return (new_instrs, [])
  320. raInsn _ new_instrs _ (LiveInstr ii Nothing)
  321. | isMetaInstr ii
  322. = return (new_instrs, [])
  323. raInsn block_live new_instrs id (LiveInstr (Instr instr) (Just live))
  324. = do
  325. assig <- getAssigR
  326. -- If we have a reg->reg move between virtual registers, where the
  327. -- src register is not live after this instruction, and the dst
  328. -- register does not already have an assignment,
  329. -- and the source register is assigned to a register, not to a spill slot,
  330. -- then we can eliminate the instruction.
  331. -- (we can't eliminate it if the source register is on the stack, because
  332. -- we do not want to use one spill slot for different virtual registers)
  333. case takeRegRegMoveInstr instr of
  334. Just (src,dst) | src `elementOfUniqSet` (liveDieRead live),
  335. isVirtualReg dst,
  336. not (dst `elemUFM` assig),
  337. isRealReg src || isInReg src assig -> do
  338. case src of
  339. (RegReal rr) -> setAssigR (addToUFM assig dst (InReg rr))
  340. -- if src is a fixed reg, then we just map dest to this
  341. -- reg in the assignment. src must be an allocatable reg,
  342. -- otherwise it wouldn't be in r_dying.
  343. _virt -> case lookupUFM assig src of
  344. Nothing -> panic "raInsn"
  345. Just loc ->
  346. setAssigR (addToUFM (delFromUFM assig src) dst loc)
  347. -- we have eliminated this instruction
  348. {-
  349. freeregs <- getFreeRegsR
  350. assig <- getAssigR
  351. pprTrace "raInsn" (text "ELIMINATED: " <> docToSDoc (pprInstr instr)
  352. $$ ppr r_dying <+> ppr w_dying $$ text (show freeregs) $$ ppr assig) $ do
  353. -}
  354. return (new_instrs, [])
  355. _ -> genRaInsn block_live new_instrs id instr
  356. (uniqSetToList $ liveDieRead live)
  357. (uniqSetToList $ liveDieWrite live)
  358. raInsn _ _ _ instr
  359. = pprPanic "raInsn" (text "no match for:" <> ppr instr)
  360. -- ToDo: what can we do about
  361. --
  362. -- R1 = x
  363. -- jump I64[x] // [R1]
  364. --
  365. -- where x is mapped to the same reg as R1. We want to coalesce x and
  366. -- R1, but the register allocator doesn't know whether x will be
  367. -- assigned to again later, in which case x and R1 should be in
  368. -- different registers. Right now we assume the worst, and the
  369. -- assignment to R1 will clobber x, so we'll spill x into another reg,
  370. -- generating another reg->reg move.
  371. isInReg :: Reg -> RegMap Loc -> Bool
  372. isInReg src assig | Just (InReg _) <- lookupUFM assig src = True
  373. | otherwise = False
  374. genRaInsn :: (FR freeRegs, Instruction instr, Outputable instr)
  375. => BlockMap RegSet
  376. -> [instr]
  377. -> BlockId
  378. -> instr
  379. -> [Reg]
  380. -> [Reg]
  381. -> RegM freeRegs ([instr], [NatBasicBlock instr])
  382. genRaInsn block_live new_instrs block_id instr r_dying w_dying = do
  383. dflags <- getDynFlags
  384. let platform = targetPlatform dflags
  385. case regUsageOfInstr platform instr of { RU read written ->
  386. do
  387. let real_written = [ rr | (RegReal rr) <- written ]
  388. let virt_written = [ vr | (RegVirtual vr) <- written ]
  389. -- we don't need to do anything with real registers that are
  390. -- only read by this instr. (the list is typically ~2 elements,
  391. -- so using nub isn't a problem).
  392. let virt_read = nub [ vr | (RegVirtual vr) <- read ]
  393. -- debugging
  394. {- freeregs <- getFreeRegsR
  395. assig <- getAssigR
  396. pprDebugAndThen (defaultDynFlags Settings{ sTargetPlatform=platform }) trace "genRaInsn"
  397. (ppr instr
  398. $$ text "r_dying = " <+> ppr r_dying
  399. $$ text "w_dying = " <+> ppr w_dying
  400. $$ text "virt_read = " <+> ppr virt_read
  401. $$ text "virt_written = " <+> ppr virt_written
  402. $$ text "freeregs = " <+> text (show freeregs)
  403. $$ text "assig = " <+> ppr assig)
  404. $ do
  405. -}
  406. -- (a), (b) allocate real regs for all regs read by this instruction.
  407. (r_spills, r_allocd) <-
  408. allocateRegsAndSpill True{-reading-} virt_read [] [] virt_read
  409. -- (c) save any temporaries which will be clobbered by this instruction
  410. clobber_saves <- saveClobberedTemps real_written r_dying
  411. -- (d) Update block map for new destinations
  412. -- NB. do this before removing dead regs from the assignment, because
  413. -- these dead regs might in fact be live in the jump targets (they're
  414. -- only dead in the code that follows in the current basic block).
  415. (fixup_blocks, adjusted_instr)
  416. <- joinToTargets block_live block_id instr
  417. -- (e) Delete all register assignments for temps which are read
  418. -- (only) and die here. Update the free register list.
  419. releaseRegs r_dying
  420. -- (f) Mark regs which are clobbered as unallocatable
  421. clobberRegs real_written
  422. -- (g) Allocate registers for temporaries *written* (only)
  423. (w_spills, w_allocd) <-
  424. allocateRegsAndSpill False{-writing-} virt_written [] [] virt_written
  425. -- (h) Release registers for temps which are written here and not
  426. -- used again.
  427. releaseRegs w_dying
  428. let
  429. -- (i) Patch the instruction
  430. patch_map
  431. = listToUFM
  432. [ (t, RegReal r)
  433. | (t, r) <- zip virt_read r_allocd
  434. ++ zip virt_written w_allocd ]
  435. patched_instr
  436. = patchRegsOfInstr adjusted_instr patchLookup
  437. patchLookup x
  438. = case lookupUFM patch_map x of
  439. Nothing -> x
  440. Just y -> y
  441. -- (j) free up stack slots for dead spilled regs
  442. -- TODO (can't be bothered right now)
  443. -- erase reg->reg moves where the source and destination are the same.
  444. -- If the src temp didn't die in this instr but happened to be allocated
  445. -- to the same real reg as the destination, then we can erase the move anyway.
  446. let squashed_instr = case takeRegRegMoveInstr patched_instr of
  447. Just (src, dst)
  448. | src == dst -> []
  449. _ -> [patched_instr]
  450. let code = squashed_instr ++ w_spills ++ reverse r_spills
  451. ++ clobber_saves ++ new_instrs
  452. -- pprTrace "patched-code" ((vcat $ map (docToSDoc . pprInstr) code)) $ do
  453. -- pprTrace "pached-fixup" ((ppr fixup_blocks)) $ do
  454. return (code, fixup_blocks)
  455. }
  456. -- -----------------------------------------------------------------------------
  457. -- releaseRegs
  458. releaseRegs :: FR freeRegs => [Reg] -> RegM freeRegs ()
  459. releaseRegs regs = do
  460. dflags <- getDynFlags
  461. let platform = targetPlatform dflags
  462. assig <- getAssigR
  463. free <- getFreeRegsR
  464. let loop _ free _ | free `seq` False = undefined
  465. loop assig free [] = do setAssigR assig; setFreeRegsR free; return ()
  466. loop assig free (RegReal rr : rs) = loop assig (frReleaseReg platform rr free) rs
  467. loop assig free (r:rs) =
  468. case lookupUFM assig r of
  469. Just (InBoth real _) -> loop (delFromUFM assig r)
  470. (frReleaseReg platform real free) rs
  471. Just (InReg real) -> loop (delFromUFM assig r)
  472. (frReleaseReg platform real free) rs
  473. _ -> loop (delFromUFM assig r) free rs
  474. loop assig free regs
  475. -- -----------------------------------------------------------------------------
  476. -- Clobber real registers
  477. -- For each temp in a register that is going to be clobbered:
  478. -- - if the temp dies after this instruction, do nothing
  479. -- - otherwise, put it somewhere safe (another reg if possible,
  480. -- otherwise spill and record InBoth in the assignment).
  481. -- - for allocateRegs on the temps *read*,
  482. -- - clobbered regs are allocatable.
  483. --
  484. -- for allocateRegs on the temps *written*,
  485. -- - clobbered regs are not allocatable.
  486. --
  487. saveClobberedTemps
  488. :: (Outputable instr, Instruction instr, FR freeRegs)
  489. => [RealReg] -- real registers clobbered by this instruction
  490. -> [Reg] -- registers which are no longer live after this insn
  491. -> RegM freeRegs [instr] -- return: instructions to spill any temps that will
  492. -- be clobbered.
  493. saveClobberedTemps [] _
  494. = return []
  495. saveClobberedTemps clobbered dying
  496. = do
  497. assig <- getAssigR
  498. let to_spill
  499. = [ (temp,reg)
  500. | (temp, InReg reg) <- ufmToList assig
  501. , any (realRegsAlias reg) clobbered
  502. , temp `notElem` map getUnique dying ]
  503. (instrs,assig') <- clobber assig [] to_spill
  504. setAssigR assig'
  505. return instrs
  506. where
  507. clobber assig instrs []
  508. = return (instrs, assig)
  509. clobber assig instrs ((temp, reg) : rest)
  510. = do dflags <- getDynFlags
  511. let platform = targetPlatform dflags
  512. freeRegs <- getFreeRegsR
  513. let regclass = targetClassOfRealReg platform reg
  514. freeRegs_thisClass = frGetFreeRegs platform regclass freeRegs
  515. case filter (`notElem` clobbered) freeRegs_thisClass of
  516. -- (1) we have a free reg of the right class that isn't
  517. -- clobbered by this instruction; use it to save the
  518. -- clobbered value.
  519. (my_reg : _) -> do
  520. setFreeRegsR (frAllocateReg platform my_reg freeRegs)
  521. let new_assign = addToUFM assig temp (InReg my_reg)
  522. let instr = mkRegRegMoveInstr platform
  523. (RegReal reg) (RegReal my_reg)
  524. clobber new_assign (instr : instrs) rest
  525. -- (2) no free registers: spill the value
  526. [] -> do
  527. (spill, slot) <- spillR (RegReal reg) temp
  528. -- record why this reg was spilled for profiling
  529. recordSpill (SpillClobber temp)
  530. let new_assign = addToUFM assig temp (InBoth reg slot)
  531. clobber new_assign (spill : instrs) rest
  532. -- | Mark all these real regs as allocated,
  533. -- and kick out their vreg assignments.
  534. --
  535. clobberRegs :: FR freeRegs => [RealReg] -> RegM freeRegs ()
  536. clobberRegs []
  537. = return ()
  538. clobberRegs clobbered
  539. = do dflags <- getDynFlags
  540. let platform = targetPlatform dflags
  541. freeregs <- getFreeRegsR
  542. setFreeRegsR $! foldr (frAllocateReg platform) freeregs clobbered
  543. assig <- getAssigR
  544. setAssigR $! clobber assig (ufmToList assig)
  545. where
  546. -- if the temp was InReg and clobbered, then we will have
  547. -- saved it in saveClobberedTemps above. So the only case
  548. -- we have to worry about here is InBoth. Note that this
  549. -- also catches temps which were loaded up during allocation
  550. -- of read registers, not just those saved in saveClobberedTemps.
  551. clobber assig []
  552. = assig
  553. clobber assig ((temp, InBoth reg slot) : rest)
  554. | any (realRegsAlias reg) clobbered
  555. = clobber (addToUFM assig temp (InMem slot)) rest
  556. clobber assig (_:rest)
  557. = clobber assig rest
  558. -- -----------------------------------------------------------------------------
  559. -- allocateRegsAndSpill
  560. -- Why are we performing a spill?
  561. data SpillLoc = ReadMem StackSlot -- reading from register only in memory
  562. | WriteNew -- writing to a new variable
  563. | WriteMem -- writing to register only in memory
  564. -- Note that ReadNew is not valid, since you don't want to be reading
  565. -- from an uninitialized register. We also don't need the location of
  566. -- the register in memory, since that will be invalidated by the write.
  567. -- Technically, we could coalesce WriteNew and WriteMem into a single
  568. -- entry as well. -- EZY
  569. -- This function does several things:
  570. -- For each temporary referred to by this instruction,
  571. -- we allocate a real register (spilling another temporary if necessary).
  572. -- We load the temporary up from memory if necessary.
  573. -- We also update the register assignment in the process, and
  574. -- the list of free registers and free stack slots.
  575. allocateRegsAndSpill
  576. :: (FR freeRegs, Outputable instr, Instruction instr)
  577. => Bool -- True <=> reading (load up spilled regs)
  578. -> [VirtualReg] -- don't push these out
  579. -> [instr] -- spill insns
  580. -> [RealReg] -- real registers allocated (accum.)
  581. -> [VirtualReg] -- temps to allocate
  582. -> RegM freeRegs ( [instr] , [RealReg])
  583. allocateRegsAndSpill _ _ spills alloc []
  584. = return (spills, reverse alloc)
  585. allocateRegsAndSpill reading keep spills alloc (r:rs)
  586. = do assig <- getAssigR
  587. let doSpill = allocRegsAndSpill_spill reading keep spills alloc r rs assig
  588. case lookupUFM assig r of
  589. -- case (1a): already in a register
  590. Just (InReg my_reg) ->
  591. allocateRegsAndSpill reading keep spills (my_reg:alloc) rs
  592. -- case (1b): already in a register (and memory)
  593. -- NB1. if we're writing this register, update its assignment to be
  594. -- InReg, because the memory value is no longer valid.
  595. -- NB2. This is why we must process written registers here, even if they
  596. -- are also read by the same instruction.
  597. Just (InBoth my_reg _)
  598. -> do when (not reading) (setAssigR (addToUFM assig r (InReg my_reg)))
  599. allocateRegsAndSpill reading keep spills (my_reg:alloc) rs
  600. -- Not already in a register, so we need to find a free one...
  601. Just (InMem slot) | reading -> doSpill (ReadMem slot)
  602. | otherwise -> doSpill WriteMem
  603. Nothing | reading ->
  604. -- pprPanic "allocateRegsAndSpill: Cannot read from uninitialized register" (ppr r)
  605. -- ToDo: This case should be a panic, but we
  606. -- sometimes see an unreachable basic block which
  607. -- triggers this because the register allocator
  608. -- will start with an empty assignment.
  609. doSpill WriteNew
  610. | otherwise -> doSpill WriteNew
  611. -- reading is redundant with reason, but we keep it around because it's
  612. -- convenient and it maintains the recursive structure of the allocator. -- EZY
  613. allocRegsAndSpill_spill :: (FR freeRegs, Instruction instr, Outputable instr)
  614. => Bool
  615. -> [VirtualReg]
  616. -> [instr]
  617. -> [RealReg]
  618. -> VirtualReg
  619. -> [VirtualReg]
  620. -> UniqFM Loc
  621. -> SpillLoc
  622. -> RegM freeRegs ([instr], [RealReg])
  623. allocRegsAndSpill_spill reading keep spills alloc r rs assig spill_loc
  624. = do dflags <- getDynFlags
  625. let platform = targetPlatform dflags
  626. freeRegs <- getFreeRegsR
  627. let freeRegs_thisClass = frGetFreeRegs platform (classOfVirtualReg r) freeRegs
  628. case freeRegs_thisClass of
  629. -- case (2): we have a free register
  630. (my_reg : _) ->
  631. do spills' <- loadTemp r spill_loc my_reg spills
  632. setAssigR (addToUFM assig r $! newLocation spill_loc my_reg)
  633. setFreeRegsR $ frAllocateReg platform my_reg freeRegs
  634. allocateRegsAndSpill reading keep spills' (my_reg : alloc) rs
  635. -- case (3): we need to push something out to free up a register
  636. [] ->
  637. do let keep' = map getUnique keep
  638. -- the vregs we could kick out that are already in a slot
  639. let candidates_inBoth
  640. = [ (temp, reg, mem)
  641. | (temp, InBoth reg mem) <- ufmToList assig
  642. , temp `notElem` keep'
  643. , targetClassOfRealReg platform reg == classOfVirtualReg r ]
  644. -- the vregs we could kick out that are only in a reg
  645. -- this would require writing the reg to a new slot before using it.
  646. let candidates_inReg
  647. = [ (temp, reg)
  648. | (temp, InReg reg) <- ufmToList assig
  649. , temp `notElem` keep'
  650. , targetClassOfRealReg platform reg == classOfVirtualReg r ]
  651. let result
  652. -- we have a temporary that is in both register and mem,
  653. -- just free up its register for use.
  654. | (temp, my_reg, slot) : _ <- candidates_inBoth
  655. = do spills' <- loadTemp r spill_loc my_reg spills
  656. let assig1 = addToUFM assig temp (InMem slot)
  657. let assig2 = addToUFM assig1 r $! newLocation spill_loc my_reg
  658. setAssigR assig2
  659. allocateRegsAndSpill reading keep spills' (my_reg:alloc) rs
  660. -- otherwise, we need to spill a temporary that currently
  661. -- resides in a register.
  662. | (temp_to_push_out, (my_reg :: RealReg)) : _
  663. <- candidates_inReg
  664. = do
  665. (spill_insn, slot) <- spillR (RegReal my_reg) temp_to_push_out
  666. let spill_store = (if reading then id else reverse)
  667. [ -- COMMENT (fsLit "spill alloc")
  668. spill_insn ]
  669. -- record that this temp was spilled
  670. recordSpill (SpillAlloc temp_to_push_out)
  671. -- update the register assignment
  672. let assig1 = addToUFM assig temp_to_push_out (InMem slot)
  673. let assig2 = addToUFM assig1 r $! newLocation spill_loc my_reg
  674. setAssigR assig2
  675. -- if need be, load up a spilled temp into the reg we've just freed up.
  676. spills' <- loadTemp r spill_loc my_reg spills
  677. allocateRegsAndSpill reading keep
  678. (spill_store ++ spills')
  679. (my_reg:alloc) rs
  680. -- there wasn't anything to spill, so we're screwed.
  681. | otherwise
  682. = pprPanic ("RegAllocLinear.allocRegsAndSpill: no spill candidates\n")
  683. $ vcat
  684. [ text "allocating vreg: " <> text (show r)
  685. , text "assignment: " <> text (show $ ufmToList assig)
  686. , text "freeRegs: " <> text (show freeRegs)
  687. , text "initFreeRegs: " <> text (show (frInitFreeRegs platform `asTypeOf` freeRegs)) ]
  688. result
  689. -- | Calculate a new location after a register has been loaded.
  690. newLocation :: SpillLoc -> RealReg -> Loc
  691. -- if the tmp was read from a slot, then now its in a reg as well
  692. newLocation (ReadMem slot) my_reg = InBoth my_reg slot
  693. -- writes will always result in only the register being available
  694. newLocation _ my_reg = InReg my_reg
  695. -- | Load up a spilled temporary if we need to (read from memory).
  696. loadTemp
  697. :: (Outputable instr, Instruction instr)
  698. => VirtualReg -- the temp being loaded
  699. -> SpillLoc -- the current location of this temp
  700. -> RealReg -- the hreg to load the temp into
  701. -> [instr]
  702. -> RegM freeRegs [instr]
  703. loadTemp vreg (ReadMem slot) hreg spills
  704. = do
  705. insn <- loadR (RegReal hreg) slot
  706. recordSpill (SpillLoad $ getUnique vreg)
  707. return $ {- COMMENT (fsLit "spill load") : -} insn : spills
  708. loadTemp _ _ _ spills =
  709. return spills