PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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