PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/compiler/nativeGen/RegAlloc/Linear/JoinToTargets.hs

https://bitbucket.org/carter/ghc
Haskell | 358 lines | 175 code | 65 blank | 118 comment | 9 complexity | 5b17ea8df97c046440b58b5e3742a598 MD5 | raw file
  1. -- | Handles joining of a jump instruction to its targets.
  2. -- The first time we encounter a jump to a particular basic block, we
  3. -- record the assignment of temporaries. The next time we encounter a
  4. -- jump to the same block, we compare our current assignment to the
  5. -- stored one. They might be different if spilling has occrred in one
  6. -- branch; so some fixup code will be required to match up the assignments.
  7. --
  8. module RegAlloc.Linear.JoinToTargets (joinToTargets) where
  9. import RegAlloc.Linear.State
  10. import RegAlloc.Linear.Base
  11. import RegAlloc.Linear.FreeRegs
  12. import RegAlloc.Liveness
  13. import Instruction
  14. import Reg
  15. import BlockId
  16. import OldCmm hiding (RegSet)
  17. import Digraph
  18. import DynFlags
  19. import Outputable
  20. import Unique
  21. import UniqFM
  22. import UniqSet
  23. -- | For a jump instruction at the end of a block, generate fixup code so its
  24. -- vregs are in the correct regs for its destination.
  25. --
  26. joinToTargets
  27. :: (FR freeRegs, Instruction instr)
  28. => BlockMap RegSet -- ^ maps the unique of the blockid to the set of vregs
  29. -- that are known to be live on the entry to each block.
  30. -> BlockId -- ^ id of the current block
  31. -> instr -- ^ branch instr on the end of the source block.
  32. -> RegM freeRegs ([NatBasicBlock instr] -- fresh blocks of fixup code.
  33. , instr) -- the original branch
  34. -- instruction, but maybe
  35. -- patched to jump
  36. -- to a fixup block first.
  37. joinToTargets block_live id instr
  38. -- we only need to worry about jump instructions.
  39. | not $ isJumpishInstr instr
  40. = return ([], instr)
  41. | otherwise
  42. = joinToTargets' block_live [] id instr (jumpDestsOfInstr instr)
  43. -----
  44. joinToTargets'
  45. :: (FR freeRegs, Instruction instr)
  46. => BlockMap RegSet -- ^ maps the unique of the blockid to the set of vregs
  47. -- that are known to be live on the entry to each block.
  48. -> [NatBasicBlock instr] -- ^ acc blocks of fixup code.
  49. -> BlockId -- ^ id of the current block
  50. -> instr -- ^ branch instr on the end of the source block.
  51. -> [BlockId] -- ^ branch destinations still to consider.
  52. -> RegM freeRegs ([NatBasicBlock instr], instr)
  53. -- no more targets to consider. all done.
  54. joinToTargets' _ new_blocks _ instr []
  55. = return (new_blocks, instr)
  56. -- handle a branch target.
  57. joinToTargets' block_live new_blocks block_id instr (dest:dests)
  58. = do
  59. -- get the map of where the vregs are stored on entry to each basic block.
  60. block_assig <- getBlockAssigR
  61. -- get the assignment on entry to the branch instruction.
  62. assig <- getAssigR
  63. -- adjust the current assignment to remove any vregs that are not live
  64. -- on entry to the destination block.
  65. let Just live_set = mapLookup dest block_live
  66. let still_live uniq _ = uniq `elemUniqSet_Directly` live_set
  67. let adjusted_assig = filterUFM_Directly still_live assig
  68. -- and free up those registers which are now free.
  69. let to_free =
  70. [ r | (reg, loc) <- ufmToList assig
  71. , not (elemUniqSet_Directly reg live_set)
  72. , r <- regsOfLoc loc ]
  73. case mapLookup dest block_assig of
  74. Nothing
  75. -> joinToTargets_first
  76. block_live new_blocks block_id instr dest dests
  77. block_assig adjusted_assig to_free
  78. Just (_, dest_assig)
  79. -> joinToTargets_again
  80. block_live new_blocks block_id instr dest dests
  81. adjusted_assig dest_assig
  82. -- this is the first time we jumped to this block.
  83. joinToTargets_first :: (FR freeRegs, Instruction instr)
  84. => BlockMap RegSet
  85. -> [NatBasicBlock instr]
  86. -> BlockId
  87. -> instr
  88. -> BlockId
  89. -> [BlockId]
  90. -> BlockAssignment freeRegs
  91. -> RegMap Loc
  92. -> [RealReg]
  93. -> RegM freeRegs ([NatBasicBlock instr], instr)
  94. joinToTargets_first block_live new_blocks block_id instr dest dests
  95. block_assig src_assig
  96. to_free
  97. = do dflags <- getDynFlags
  98. let platform = targetPlatform dflags
  99. -- free up the regs that are not live on entry to this block.
  100. freeregs <- getFreeRegsR
  101. let freeregs' = foldr (frReleaseReg platform) freeregs to_free
  102. -- remember the current assignment on entry to this block.
  103. setBlockAssigR (mapInsert dest (freeregs', src_assig) block_assig)
  104. joinToTargets' block_live new_blocks block_id instr dests
  105. -- we've jumped to this block before
  106. joinToTargets_again :: (Instruction instr, FR freeRegs)
  107. => BlockMap RegSet
  108. -> [NatBasicBlock instr]
  109. -> BlockId
  110. -> instr
  111. -> BlockId
  112. -> [BlockId]
  113. -> UniqFM Loc
  114. -> UniqFM Loc
  115. -> RegM freeRegs ([NatBasicBlock instr], instr)
  116. joinToTargets_again
  117. block_live new_blocks block_id instr dest dests
  118. src_assig dest_assig
  119. -- the assignments already match, no problem.
  120. | ufmToList dest_assig == ufmToList src_assig
  121. = joinToTargets' block_live new_blocks block_id instr dests
  122. -- assignments don't match, need fixup code
  123. | otherwise
  124. = do
  125. -- make a graph of what things need to be moved where.
  126. let graph = makeRegMovementGraph src_assig dest_assig
  127. -- look for cycles in the graph. This can happen if regs need to be swapped.
  128. -- Note that we depend on the fact that this function does a
  129. -- bottom up traversal of the tree-like portions of the graph.
  130. --
  131. -- eg, if we have
  132. -- R1 -> R2 -> R3
  133. --
  134. -- ie move value in R1 to R2 and value in R2 to R3.
  135. --
  136. -- We need to do the R2 -> R3 move before R1 -> R2.
  137. --
  138. let sccs = stronglyConnCompFromEdgedVerticesR graph
  139. {- -- debugging
  140. pprTrace
  141. ("joinToTargets: making fixup code")
  142. (vcat [ text " in block: " <> ppr block_id
  143. , text " jmp instruction: " <> ppr instr
  144. , text " src assignment: " <> ppr src_assig
  145. , text " dest assignment: " <> ppr dest_assig
  146. , text " movement graph: " <> ppr graph
  147. , text " sccs of graph: " <> ppr sccs
  148. , text ""])
  149. (return ())
  150. -}
  151. delta <- getDeltaR
  152. fixUpInstrs_ <- mapM (handleComponent delta instr) sccs
  153. let fixUpInstrs = concat fixUpInstrs_
  154. -- make a new basic block containing the fixup code.
  155. -- A the end of the current block we will jump to the fixup one,
  156. -- then that will jump to our original destination.
  157. fixup_block_id <- getUniqueR
  158. let block = BasicBlock (mkBlockId fixup_block_id)
  159. $ fixUpInstrs ++ mkJumpInstr dest
  160. {- pprTrace
  161. ("joinToTargets: fixup code is:")
  162. (vcat [ ppr block
  163. , text ""])
  164. (return ())
  165. -}
  166. -- if we didn't need any fixups, then don't include the block
  167. case fixUpInstrs of
  168. [] -> joinToTargets' block_live new_blocks block_id instr dests
  169. -- patch the original branch instruction so it goes to our
  170. -- fixup block instead.
  171. _ -> let instr' = patchJumpInstr instr
  172. (\bid -> if bid == dest
  173. then mkBlockId fixup_block_id
  174. else bid) -- no change!
  175. in joinToTargets' block_live (block : new_blocks) block_id instr' dests
  176. -- | Construct a graph of register\/spill movements.
  177. --
  178. -- Cyclic components seem to occur only very rarely.
  179. --
  180. -- We cut some corners by not handling memory-to-memory moves.
  181. -- This shouldn't happen because every temporary gets its own stack slot.
  182. --
  183. makeRegMovementGraph :: RegMap Loc -> RegMap Loc -> [(Unique, Loc, [Loc])]
  184. makeRegMovementGraph adjusted_assig dest_assig
  185. = [ node | (vreg, src) <- ufmToList adjusted_assig
  186. -- source reg might not be needed at the dest:
  187. , Just loc <- [lookupUFM_Directly dest_assig vreg]
  188. , node <- expandNode vreg src loc ]
  189. -- | Expand out the destination, so InBoth destinations turn into
  190. -- a combination of InReg and InMem.
  191. -- The InBoth handling is a little tricky here. If the destination is
  192. -- InBoth, then we must ensure that the value ends up in both locations.
  193. -- An InBoth destination must conflict with an InReg or InMem source, so
  194. -- we expand an InBoth destination as necessary.
  195. --
  196. -- An InBoth source is slightly different: we only care about the register
  197. -- that the source value is in, so that we can move it to the destinations.
  198. --
  199. expandNode
  200. :: a
  201. -> Loc -- ^ source of move
  202. -> Loc -- ^ destination of move
  203. -> [(a, Loc, [Loc])]
  204. expandNode vreg loc@(InReg src) (InBoth dst mem)
  205. | src == dst = [(vreg, loc, [InMem mem])]
  206. | otherwise = [(vreg, loc, [InReg dst, InMem mem])]
  207. expandNode vreg loc@(InMem src) (InBoth dst mem)
  208. | src == mem = [(vreg, loc, [InReg dst])]
  209. | otherwise = [(vreg, loc, [InReg dst, InMem mem])]
  210. expandNode _ (InBoth _ src) (InMem dst)
  211. | src == dst = [] -- guaranteed to be true
  212. expandNode _ (InBoth src _) (InReg dst)
  213. | src == dst = []
  214. expandNode vreg (InBoth src _) dst
  215. = expandNode vreg (InReg src) dst
  216. expandNode vreg src dst
  217. | src == dst = []
  218. | otherwise = [(vreg, src, [dst])]
  219. -- | Generate fixup code for a particular component in the move graph
  220. -- This component tells us what values need to be moved to what
  221. -- destinations. We have eliminated any possibility of single-node
  222. -- cycles in expandNode above.
  223. --
  224. handleComponent
  225. :: Instruction instr
  226. => Int -> instr -> SCC (Unique, Loc, [Loc])
  227. -> RegM freeRegs [instr]
  228. -- If the graph is acyclic then we won't get the swapping problem below.
  229. -- In this case we can just do the moves directly, and avoid having to
  230. -- go via a spill slot.
  231. --
  232. handleComponent delta _ (AcyclicSCC (vreg, src, dsts))
  233. = mapM (makeMove delta vreg src) dsts
  234. -- Handle some cyclic moves.
  235. -- This can happen if we have two regs that need to be swapped.
  236. -- eg:
  237. -- vreg source loc dest loc
  238. -- (vreg1, InReg r1, [InReg r2])
  239. -- (vreg2, InReg r2, [InReg r1])
  240. --
  241. -- To avoid needing temp register, we just spill all the source regs, then
  242. -- reaload them into their destination regs.
  243. --
  244. -- Note that we can not have cycles that involve memory locations as
  245. -- sources as single destination because memory locations (stack slots)
  246. -- are allocated exclusively for a virtual register and therefore can not
  247. -- require a fixup.
  248. --
  249. handleComponent delta instr
  250. (CyclicSCC ((vreg, InReg sreg, (InReg dreg: _)) : rest))
  251. -- dest list may have more than one element, if the reg is also InMem.
  252. = do
  253. -- spill the source into its slot
  254. (instrSpill, slot)
  255. <- spillR (RegReal sreg) vreg
  256. -- reload into destination reg
  257. instrLoad <- loadR (RegReal dreg) slot
  258. remainingFixUps <- mapM (handleComponent delta instr)
  259. (stronglyConnCompFromEdgedVerticesR rest)
  260. -- make sure to do all the reloads after all the spills,
  261. -- so we don't end up clobbering the source values.
  262. return ([instrSpill] ++ concat remainingFixUps ++ [instrLoad])
  263. handleComponent _ _ (CyclicSCC _)
  264. = panic "Register Allocator: handleComponent cyclic"
  265. -- | Move a vreg between these two locations.
  266. --
  267. makeMove
  268. :: Instruction instr
  269. => Int -- ^ current C stack delta.
  270. -> Unique -- ^ unique of the vreg that we're moving.
  271. -> Loc -- ^ source location.
  272. -> Loc -- ^ destination location.
  273. -> RegM freeRegs instr -- ^ move instruction.
  274. makeMove delta vreg src dst
  275. = do dflags <- getDynFlags
  276. let platform = targetPlatform dflags
  277. case (src, dst) of
  278. (InReg s, InReg d) ->
  279. do recordSpill (SpillJoinRR vreg)
  280. return $ mkRegRegMoveInstr platform (RegReal s) (RegReal d)
  281. (InMem s, InReg d) ->
  282. do recordSpill (SpillJoinRM vreg)
  283. return $ mkLoadInstr dflags (RegReal d) delta s
  284. (InReg s, InMem d) ->
  285. do recordSpill (SpillJoinRM vreg)
  286. return $ mkSpillInstr dflags (RegReal s) delta d
  287. _ ->
  288. -- we don't handle memory to memory moves.
  289. -- they shouldn't happen because we don't share
  290. -- stack slots between vregs.
  291. panic ("makeMove " ++ show vreg ++ " (" ++ show src ++ ") ("
  292. ++ show dst ++ ")"
  293. ++ " we don't handle mem->mem moves.")