-calculateOwnership :: UniqSet BlockId -> [BrokenBlock] -> BlockEnv (UniqSet BlockId)
-calculateOwnership proc_points blocks =
- fixedpoint dependants update (map brokenBlockId blocks) emptyUFM
- where
- blocks_ufm :: BlockEnv BrokenBlock
- blocks_ufm = blocksToBlockEnv blocks
-
- dependants :: BlockId -> [BlockId]
- dependants ident =
- brokenBlockTargets $ lookupWithDefaultUFM
- blocks_ufm unknown_block ident
-
- update :: BlockId -> Maybe BlockId
- -> BlockEnv (UniqSet BlockId) -> Maybe (BlockEnv (UniqSet BlockId))
- update ident cause owners =
- case (cause, ident `elementOfUniqSet` proc_points) of
- (Nothing, True) -> Just $ addToUFM owners ident (unitUniqSet ident)
- (Nothing, False) -> Nothing
- (Just cause', True) -> Nothing
- (Just cause', False) ->
- if (sizeUniqSet old) == (sizeUniqSet new)
- then Nothing
- else Just $ addToUFM owners ident new
- where
- old = lookupWithDefaultUFM owners emptyUniqSet ident
- new = old `unionUniqSets` lookupWithDefaultUFM owners emptyUniqSet cause'
-
- unknown_block = panic "unknown BlockId in selectStackFormat"
-
-calculateProcPoints :: [BrokenBlock] -> UniqSet BlockId
-calculateProcPoints blocks = calculateProcPoints' init_proc_points blocks
- where
- init_proc_points = mkUniqSet $
- map brokenBlockId $
- filter always_proc_point blocks
- always_proc_point BrokenBlock {
- brokenBlockEntry = FunctionEntry _ } = True
- always_proc_point BrokenBlock {
- brokenBlockEntry = ContinuationEntry _ } = True
- always_proc_point _ = False
-
-calculateProcPoints' :: UniqSet BlockId -> [BrokenBlock] -> UniqSet BlockId
-calculateProcPoints' old_proc_points blocks =
- if sizeUniqSet old_proc_points == sizeUniqSet new_proc_points
- then old_proc_points
- else calculateProcPoints' new_proc_points blocks
- where
- owners = calculateOwnership old_proc_points blocks
- new_proc_points = unionManyUniqSets (old_proc_points:(map (calculateProcPoints'' owners) blocks))
-
-calculateProcPoints'' :: BlockEnv (UniqSet BlockId) -> BrokenBlock -> UniqSet BlockId
-calculateProcPoints'' owners block =
- unionManyUniqSets (map (f parent_id) child_ids)
- where
- parent_id = brokenBlockId block
- child_ids = brokenBlockTargets block
- f parent_id child_id =
- if needs_proc_point
- then unitUniqSet child_id
- else emptyUniqSet
- where
- parent_owners = lookupWithDefaultUFM owners emptyUniqSet parent_id
- child_owners = lookupWithDefaultUFM owners emptyUniqSet child_id
- needs_proc_point = not $ isEmptyUniqSet $ child_owners `minusUniqSet` parent_owners
- --needs_proc_point = sizeUniqSet (parent_owners `unionUniqSets` child_owners) /= sizeUniqSet parent_owners
-
-cmmCondBranchTargets (CmmCondBranch _ target) = [target]
-cmmCondBranchTargets _ = []
-
-finalBranchOrSwitchTargets (FinalBranch target) = [target]
-finalBranchOrSwitchTargets (FinalSwitch _ targets) = mapCatMaybes id targets
-finalBranchOrSwitchTargets _ = []
-
-collectNonProcPointTargets ::
- UniqSet BlockId -> BlockEnv BrokenBlock
- -> UniqSet BlockId -> BlockId -> UniqSet BlockId
-collectNonProcPointTargets proc_points blocks current_targets block =
- if sizeUniqSet current_targets == sizeUniqSet new_targets
- then current_targets
- else foldl (collectNonProcPointTargets proc_points blocks) new_targets targets
- where
- block' = lookupWithDefaultUFM blocks (panic "TODO") block
- targets = -- We can't use the brokenBlockTargets b/c we don't want to follow the final goto after a call -- brokenBlockTargets block'
- --finalBranchOrSwitchTargets (brokenBlockExit block') ++ concatMap cmmCondBranchTargets (brokenBlockStmts block')
- uniqSetToList $ (mkUniqSet $ brokenBlockTargets block') `minusUniqSet` proc_points
- -- TODO: remove redundant uniqSetToList
- new_targets = current_targets `unionUniqSets` (mkUniqSet targets)
-
-buildContinuation ::
- UniqSet BlockId -> BlockEnv BrokenBlock
- -> BlockId -> Continuation
-buildContinuation proc_points blocks start =
- Continuation is_entry info_table clabel params body
- where
- children = (collectNonProcPointTargets proc_points blocks (unitUniqSet start) start) `delOneFromUniqSet` start
- start_block = lookupWithDefaultUFM blocks (panic "TODO") start
- children_blocks = map (lookupWithDefaultUFM blocks (panic "TODO")) (uniqSetToList children)
- body = start_block : children_blocks
- info_table = [] -- TODO
- is_entry = case start_block of
- BrokenBlock { brokenBlockEntry = FunctionEntry _ } -> True
- _ -> False
- clabel = mkReturnPtLabel $ getUnique start
- params = case start_block of
- BrokenBlock { brokenBlockEntry = FunctionEntry args } -> args
- BrokenBlock { brokenBlockEntry = ContinuationEntry args } -> args
- BrokenBlock { brokenBlockEntry = ControlEntry } -> [] -- TODO: it's a proc-point, we could pass lives in parameter registers
-
---------------------------------------------------------------------------------
--- For now just select the continuation orders in the order they are in the set with no gaps
-
-selectStackFormat :: BlockEnv CmmLive -> [BrokenBlock] -> BlockEnv StackFormat
-selectStackFormat live blocks =
- fixedpoint dependants update (map brokenBlockId blocks) emptyUFM
- where
- blocks_ufm :: BlockEnv BrokenBlock
- blocks_ufm = listToUFM $ map (\b -> (brokenBlockId b, b)) blocks
-
- dependants :: BlockId -> [BlockId]
- dependants ident =
- brokenBlockTargets $ lookupWithDefaultUFM
- blocks_ufm unknown_block ident
-
- update :: BlockId -> Maybe BlockId
- -> BlockEnv StackFormat -> Maybe (BlockEnv StackFormat)
- update ident cause formats =
- if ident `elemUFM` formats
- then Nothing -- Blocks only need to be updated once
- else case (cause,
- brokenBlockEntry $ lookupWithDefaultUFM blocks_ufm
- unknown_block ident) of
- -- Propagate only to blocks entered by branches
- -- (not function entry blocks or continuation entry blocks)
- (Just cause_name, ControlEntry) ->
- Just $ addToUFM formats ident cause_format
- where cause_format = lookupWithDefaultUFM
- formats unknown_block
- cause_name
- -- Do initial calculates for function blocks
- (Nothing, FunctionEntry _) ->
- Just $
- addToUFM formats ident $
- StackFormat ident 0 []
- -- Do initial calculates for continuation blocks
- (Nothing, ContinuationEntry _) ->
- Just $
- addToUFM formats ident $
- live_to_format ident $
- lookupWithDefaultUFM live unknown_block ident
- _ -> Nothing
-
- unknown_block = panic "unknown BlockId in selectStackFormat"
-
- live_to_format :: BlockId -> CmmLive -> StackFormat
- live_to_format label live =
- foldl extend_format
- (StackFormat label retAddrSizeW [])
- (uniqSetToList live)
-
- extend_format :: StackFormat -> LocalReg -> StackFormat
- extend_format (StackFormat block size offsets) reg =
- StackFormat block (slot_size reg + size) ((CmmLocal reg, size) : offsets)
-
-selectStackFormat2 :: BlockEnv CmmLive -> [Continuation] -> [(CLabel, StackFormat)]
-selectStackFormat2 live continuations =
- map (\c -> (continuationLabel c, selectStackFormat' c)) continuations
- where
- selectStackFormat' (Continuation True info_table label formals blocks) =
- let ident = brokenBlockId $ head blocks -- TODO: CLabel isn't a uniquable, but we need a better way than this
- in StackFormat ident 0 []
- selectStackFormat' (Continuation False info_table label formals blocks) =
- let ident = brokenBlockId $ head blocks -- TODO: CLabel isn't a uniquable, but we need a better way than this
- in live_to_format ident $ lookupWithDefaultUFM live unknown_block ident
-
- live_to_format :: BlockId -> CmmLive -> StackFormat
- live_to_format label live =
- foldl extend_format
- (StackFormat label retAddrSizeW [])
- (uniqSetToList live)
-
- extend_format :: StackFormat -> LocalReg -> StackFormat
- extend_format (StackFormat block size offsets) reg =
- StackFormat block (slot_size reg + size) ((CmmLocal reg, size) : offsets)
-
- unknown_block = panic "unknown BlockId in selectStackFormat"
-
-slot_size reg = ((machRepByteWidth (localRegRep reg) - 1) `div` wORD_SIZE) + 1
-
-constructContinuation :: [(CLabel, StackFormat)] -> Continuation -> CmmTop
-constructContinuation formats (Continuation is_entry info label formals blocks) =
- CmmProc info label formals (map (constructContinuation2' label formats) blocks)
-
-{-
- BasicBlock ident (prefix++stmts++postfix)
- where
-
- curr_format = lookupWithDefaultUFM formats unknown_block ident
- unknown_block = panic "unknown BlockId in constructContinuation"
- prefix = case entry of
- ControlEntry -> []
- FunctionEntry _ -> []
- ContinuationEntry formals ->
- unpack_continuation curr_format
- postfix = case exit of
- FinalBranch next -> [CmmBranch next]
- FinalSwitch expr targets -> [CmmSwitch expr targets]
- FinalReturn arguments ->
- exit_function curr_format
- (CmmLoad (CmmReg spReg) wordRep)
- arguments
- FinalJump target arguments ->
- exit_function curr_format target arguments
- -- TODO: do something about global saves
- FinalCall next (CmmForeignCall target CmmCallConv)
- results arguments saves ->
- pack_continuation curr_format cont_format ++
- [CmmJump target arguments]
- where
- cont_format = lookupWithDefaultUFM formats
- unknown_block next
- FinalCall next _ results arguments saves -> panic "unimplemented CmmCall"
--}
-
-constructContinuation2' :: CLabel -> [(CLabel, StackFormat)] -> BrokenBlock
- -> CmmBasicBlock
-constructContinuation2' curr_ident formats (BrokenBlock ident entry stmts _ exit) =
- BasicBlock ident (prefix++stmts++postfix)
- where
- curr_format = maybe unknown_block id $ lookup curr_ident formats
- unknown_block = panic "unknown BlockId in constructContinuation"
- prefix = case entry of
- ControlEntry -> []
- FunctionEntry _ -> []
- ContinuationEntry formals ->
- unpack_continuation curr_format
- postfix = case exit of
- FinalBranch next -> [CmmBranch next]
- FinalSwitch expr targets -> [CmmSwitch expr targets]
- FinalReturn arguments ->
- exit_function curr_format
- (CmmLoad (CmmReg spReg) wordRep)
- arguments
- FinalJump target arguments ->
- exit_function curr_format target arguments
- -- TODO: do something about global saves
- FinalCall next (CmmForeignCall target CmmCallConv)
- results arguments saves ->
- pack_continuation curr_format cont_format ++
- [CmmJump target arguments]
- where
- cont_format = maybe unknown_block id $
- lookup (mkReturnPtLabel $ getUnique next) formats
- FinalCall next _ results arguments saves -> panic "unimplemented CmmCall"
-
-constructContinuation2 :: BlockEnv StackFormat -> BrokenBlock
- -> CmmBasicBlock
-constructContinuation2 formats (BrokenBlock ident entry stmts _ exit) =
- BasicBlock ident (prefix++stmts++postfix)
- where
- curr_format = lookupWithDefaultUFM formats unknown_block ident
- unknown_block = panic "unknown BlockId in constructContinuation"
- prefix = case entry of
- ControlEntry -> []
- FunctionEntry _ -> []
- ContinuationEntry formals ->
- unpack_continuation curr_format
- postfix = case exit of
- FinalBranch next -> [CmmBranch next]
- FinalSwitch expr targets -> [CmmSwitch expr targets]
- FinalReturn arguments ->
- exit_function curr_format
- (CmmLoad (CmmReg spReg) wordRep)
- arguments
- FinalJump target arguments ->
- exit_function curr_format target arguments
- -- TODO: do something about global saves
- FinalCall next (CmmForeignCall target CmmCallConv)
- results arguments saves ->
- pack_continuation curr_format cont_format ++
- [CmmJump target arguments]
- where
- cont_format = lookupWithDefaultUFM formats
- unknown_block next
- FinalCall next _ results arguments saves -> panic "unimplemented CmmCall"
-
---------------------------------------------------------------------------------
--- Functions that generate CmmStmt sequences
--- for packing/unpacking continuations
--- and entering/exiting functions
-
-exit_function :: StackFormat -> CmmExpr -> CmmActuals -> [CmmStmt]
-exit_function (StackFormat curr_id curr_frame_size curr_offsets) target arguments
- = adjust_spReg ++ jump where
- adjust_spReg = [
- CmmAssign spReg
- (CmmRegOff spReg (curr_frame_size*wORD_SIZE))]
- jump = [CmmJump target arguments]
-
-enter_function :: WordOff -> [CmmStmt]
-enter_function max_frame_size
- = check_stack_limit where
- check_stack_limit = [
- CmmCondBranch
- (CmmMachOp (MO_U_Lt $ cmmRegRep spReg)
- [CmmRegOff spReg max_frame_size, CmmReg spLimReg])
- gc_block]
- gc_block = undefined -- TODO: get stack and heap checks to go to same
-
--- TODO: fix branches to proc point (we have to insert a new block to marshel the continuation)
-pack_continuation :: StackFormat -> StackFormat -> [CmmStmt]
-pack_continuation (StackFormat curr_id curr_frame_size curr_offsets)
- (StackFormat cont_id cont_frame_size cont_offsets)
- = save_live_values ++ set_stack_header ++ adjust_spReg where
- -- TODO: only save variables when actually needed
- save_live_values =
- [CmmStore
- (CmmRegOff
- spReg (wORD_SIZE*(curr_frame_size - cont_frame_size + offset)))
- (CmmReg reg)
- | (reg, offset) <- cont_offsets]
- set_stack_header = -- TODO: only set when needed
- [CmmStore (CmmRegOff spReg (wORD_SIZE*(curr_frame_size - cont_frame_size))) continuation_function]
- continuation_function = CmmLit $ CmmLabel $ mkReturnPtLabel {-TODO: use the correct function -} $ getUnique cont_id
- adjust_spReg =
- if curr_frame_size == cont_frame_size
- then []
- else [CmmAssign spReg (CmmRegOff spReg ((curr_frame_size - cont_frame_size)*wORD_SIZE))]
-
--- Lazy adjustment of stack headers assumes all blocks
--- that could branch to eachother (i.e. control blocks)
--- have the same stack format (this causes a problem
--- only for proc-point).
-unpack_continuation :: StackFormat -> [CmmStmt]
-unpack_continuation (StackFormat curr_id curr_frame_size curr_offsets)
- = load_live_values where
- -- TODO: only save variables when actually needed
- load_live_values =
- [CmmAssign
- reg
- (CmmLoad (CmmRegOff spReg (wORD_SIZE*offset)) (cmmRegRep reg))
- | (reg, offset) <- curr_offsets]
-
------------------------------------------------------------------------------
--- Breaking basic blocks on function calls
------------------------------------------------------------------------------
-