{-# LANGUAGE BangPatterns, CPP, ScopedTypeVariables #-}
module RegAlloc.Linear.Main (
regAlloc,
module RegAlloc.Linear.Base,
module RegAlloc.Linear.Stats
) where
#include "HsVersions.h"
import GhcPrelude
import RegAlloc.Linear.State
import RegAlloc.Linear.Base
import RegAlloc.Linear.StackMap
import RegAlloc.Linear.FreeRegs
import RegAlloc.Linear.Stats
import RegAlloc.Linear.JoinToTargets
import qualified RegAlloc.Linear.PPC.FreeRegs as PPC
import qualified RegAlloc.Linear.SPARC.FreeRegs as SPARC
import qualified RegAlloc.Linear.X86.FreeRegs as X86
import qualified RegAlloc.Linear.X86_64.FreeRegs as X86_64
import TargetReg
import RegAlloc.Liveness
import Instruction
import Reg
import BlockId
import Hoopl.Collections
import Cmm hiding (RegSet)
import Digraph
import DynFlags
import Unique
import UniqSet
import UniqFM
import UniqSupply
import Outputable
import Platform
import Data.Maybe
import Data.List
import Control.Monad
regAlloc
:: (Outputable instr, Instruction instr)
=> DynFlags
-> LiveCmmDecl statics instr
-> UniqSM ( NatCmmDecl statics instr
, Maybe Int
, Maybe RegAllocStats
)
regAlloc :: DynFlags
-> LiveCmmDecl statics instr
-> UniqSM
(NatCmmDecl statics instr, Maybe Int, Maybe RegAllocStats)
regAlloc _ (CmmData sec :: Section
sec d :: statics
d)
= (NatCmmDecl statics instr, Maybe Int, Maybe RegAllocStats)
-> UniqSM
(NatCmmDecl statics instr, Maybe Int, Maybe RegAllocStats)
forall (m :: * -> *) a. Monad m => a -> m a
return
( Section -> statics -> NatCmmDecl statics instr
forall d h g. Section -> d -> GenCmmDecl d h g
CmmData Section
sec statics
d
, Maybe Int
forall a. Maybe a
Nothing
, Maybe RegAllocStats
forall a. Maybe a
Nothing )
regAlloc _ (CmmProc (LiveInfo info :: LabelMap CmmStatics
info _ _ _) lbl :: CLabel
lbl live :: [GlobalReg]
live [])
= (NatCmmDecl statics instr, Maybe Int, Maybe RegAllocStats)
-> UniqSM
(NatCmmDecl statics instr, Maybe Int, Maybe RegAllocStats)
forall (m :: * -> *) a. Monad m => a -> m a
return ( LabelMap CmmStatics
-> CLabel
-> [GlobalReg]
-> ListGraph instr
-> NatCmmDecl statics instr
forall d h g. h -> CLabel -> [GlobalReg] -> g -> GenCmmDecl d h g
CmmProc LabelMap CmmStatics
info CLabel
lbl [GlobalReg]
live ([GenBasicBlock instr] -> ListGraph instr
forall i. [GenBasicBlock i] -> ListGraph i
ListGraph [])
, Maybe Int
forall a. Maybe a
Nothing
, Maybe RegAllocStats
forall a. Maybe a
Nothing )
regAlloc dflags :: DynFlags
dflags (CmmProc static :: LiveInfo
static lbl :: CLabel
lbl live :: [GlobalReg]
live sccs :: [SCC (LiveBasicBlock instr)]
sccs)
| LiveInfo info :: LabelMap CmmStatics
info entry_ids :: [BlockId]
entry_ids@(first_id :: BlockId
first_id:_) (Just block_live :: BlockMap RegSet
block_live) _ <- LiveInfo
static
= do
(final_blocks :: [GenBasicBlock instr]
final_blocks, stats :: RegAllocStats
stats, stack_use :: Int
stack_use)
<- DynFlags
-> [BlockId]
-> BlockMap RegSet
-> [SCC (LiveBasicBlock instr)]
-> UniqSM ([GenBasicBlock instr], RegAllocStats, Int)
forall instr.
(Outputable instr, Instruction instr) =>
DynFlags
-> [BlockId]
-> BlockMap RegSet
-> [SCC (LiveBasicBlock instr)]
-> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
linearRegAlloc DynFlags
dflags [BlockId]
entry_ids BlockMap RegSet
block_live [SCC (LiveBasicBlock instr)]
sccs
let ((first' :: GenBasicBlock instr
first':_), rest' :: [GenBasicBlock instr]
rest')
= (GenBasicBlock instr -> Bool)
-> [GenBasicBlock instr]
-> ([GenBasicBlock instr], [GenBasicBlock instr])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition ((BlockId -> BlockId -> Bool
forall a. Eq a => a -> a -> Bool
== BlockId
first_id) (BlockId -> Bool)
-> (GenBasicBlock instr -> BlockId) -> GenBasicBlock instr -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenBasicBlock instr -> BlockId
forall i. GenBasicBlock i -> BlockId
blockId) [GenBasicBlock instr]
final_blocks
let max_spill_slots :: Int
max_spill_slots = DynFlags -> Int
maxSpillSlots DynFlags
dflags
extra_stack :: Maybe Int
extra_stack
| Int
stack_use Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
max_spill_slots
= Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
stack_use Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
max_spill_slots)
| Bool
otherwise
= Maybe Int
forall a. Maybe a
Nothing
(NatCmmDecl statics instr, Maybe Int, Maybe RegAllocStats)
-> UniqSM
(NatCmmDecl statics instr, Maybe Int, Maybe RegAllocStats)
forall (m :: * -> *) a. Monad m => a -> m a
return ( LabelMap CmmStatics
-> CLabel
-> [GlobalReg]
-> ListGraph instr
-> NatCmmDecl statics instr
forall d h g. h -> CLabel -> [GlobalReg] -> g -> GenCmmDecl d h g
CmmProc LabelMap CmmStatics
info CLabel
lbl [GlobalReg]
live ([GenBasicBlock instr] -> ListGraph instr
forall i. [GenBasicBlock i] -> ListGraph i
ListGraph (GenBasicBlock instr
first' GenBasicBlock instr
-> [GenBasicBlock instr] -> [GenBasicBlock instr]
forall a. a -> [a] -> [a]
: [GenBasicBlock instr]
rest'))
, Maybe Int
extra_stack
, RegAllocStats -> Maybe RegAllocStats
forall a. a -> Maybe a
Just RegAllocStats
stats)
regAlloc _ (CmmProc _ _ _ _)
= String
-> UniqSM
(NatCmmDecl statics instr, Maybe Int, Maybe RegAllocStats)
forall a. String -> a
panic "RegAllocLinear.regAlloc: no match"
linearRegAlloc
:: (Outputable instr, Instruction instr)
=> DynFlags
-> [BlockId]
-> BlockMap RegSet
-> [SCC (LiveBasicBlock instr)]
-> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
linearRegAlloc :: DynFlags
-> [BlockId]
-> BlockMap RegSet
-> [SCC (LiveBasicBlock instr)]
-> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
linearRegAlloc dflags :: DynFlags
dflags entry_ids :: [BlockId]
entry_ids block_live :: BlockMap RegSet
block_live sccs :: [SCC (LiveBasicBlock instr)]
sccs
= case Platform -> Arch
platformArch Platform
platform of
ArchX86 -> FreeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall freeRegs.
FR freeRegs =>
freeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
go (FreeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int))
-> FreeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall a b. (a -> b) -> a -> b
$ (Platform -> FreeRegs
forall freeRegs. FR freeRegs => Platform -> freeRegs
frInitFreeRegs Platform
platform :: X86.FreeRegs)
ArchX86_64 -> FreeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall freeRegs.
FR freeRegs =>
freeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
go (FreeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int))
-> FreeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall a b. (a -> b) -> a -> b
$ (Platform -> FreeRegs
forall freeRegs. FR freeRegs => Platform -> freeRegs
frInitFreeRegs Platform
platform :: X86_64.FreeRegs)
ArchSPARC -> FreeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall freeRegs.
FR freeRegs =>
freeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
go (FreeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int))
-> FreeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall a b. (a -> b) -> a -> b
$ (Platform -> FreeRegs
forall freeRegs. FR freeRegs => Platform -> freeRegs
frInitFreeRegs Platform
platform :: SPARC.FreeRegs)
ArchSPARC64 -> String -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall a. String -> a
panic "linearRegAlloc ArchSPARC64"
ArchPPC -> FreeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall freeRegs.
FR freeRegs =>
freeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
go (FreeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int))
-> FreeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall a b. (a -> b) -> a -> b
$ (Platform -> FreeRegs
forall freeRegs. FR freeRegs => Platform -> freeRegs
frInitFreeRegs Platform
platform :: PPC.FreeRegs)
ArchARM _ _ _ -> String -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall a. String -> a
panic "linearRegAlloc ArchARM"
ArchARM64 -> String -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall a. String -> a
panic "linearRegAlloc ArchARM64"
ArchPPC_64 _ -> FreeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall freeRegs.
FR freeRegs =>
freeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
go (FreeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int))
-> FreeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall a b. (a -> b) -> a -> b
$ (Platform -> FreeRegs
forall freeRegs. FR freeRegs => Platform -> freeRegs
frInitFreeRegs Platform
platform :: PPC.FreeRegs)
ArchAlpha -> String -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall a. String -> a
panic "linearRegAlloc ArchAlpha"
ArchMipseb -> String -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall a. String -> a
panic "linearRegAlloc ArchMipseb"
ArchMipsel -> String -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall a. String -> a
panic "linearRegAlloc ArchMipsel"
ArchJavaScript -> String -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall a. String -> a
panic "linearRegAlloc ArchJavaScript"
ArchUnknown -> String -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall a. String -> a
panic "linearRegAlloc ArchUnknown"
where
go :: freeRegs -> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
go f :: freeRegs
f = DynFlags
-> freeRegs
-> [BlockId]
-> BlockMap RegSet
-> [SCC (LiveBasicBlock instr)]
-> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall freeRegs instr.
(FR freeRegs, Outputable instr, Instruction instr) =>
DynFlags
-> freeRegs
-> [BlockId]
-> BlockMap RegSet
-> [SCC (LiveBasicBlock instr)]
-> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
linearRegAlloc' DynFlags
dflags freeRegs
f [BlockId]
entry_ids BlockMap RegSet
block_live [SCC (LiveBasicBlock instr)]
sccs
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
linearRegAlloc'
:: (FR freeRegs, Outputable instr, Instruction instr)
=> DynFlags
-> freeRegs
-> [BlockId]
-> BlockMap RegSet
-> [SCC (LiveBasicBlock instr)]
-> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
linearRegAlloc' :: DynFlags
-> freeRegs
-> [BlockId]
-> BlockMap RegSet
-> [SCC (LiveBasicBlock instr)]
-> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
linearRegAlloc' dflags :: DynFlags
dflags initFreeRegs :: freeRegs
initFreeRegs entry_ids :: [BlockId]
entry_ids block_live :: BlockMap RegSet
block_live sccs :: [SCC (LiveBasicBlock instr)]
sccs
= do UniqSupply
us <- UniqSM UniqSupply
forall (m :: * -> *). MonadUnique m => m UniqSupply
getUniqueSupplyM
let (_, stack :: StackMap
stack, stats :: RegAllocStats
stats, blocks :: [NatBasicBlock instr]
blocks) =
DynFlags
-> BlockAssignment freeRegs
-> freeRegs
-> RegMap Loc
-> StackMap
-> UniqSupply
-> RegM freeRegs [NatBasicBlock instr]
-> (BlockAssignment freeRegs, StackMap, RegAllocStats,
[NatBasicBlock instr])
forall freeRegs a.
DynFlags
-> BlockAssignment freeRegs
-> freeRegs
-> RegMap Loc
-> StackMap
-> UniqSupply
-> RegM freeRegs a
-> (BlockAssignment freeRegs, StackMap, RegAllocStats, a)
runR DynFlags
dflags BlockAssignment freeRegs
forall (map :: * -> *) a. IsMap map => map a
mapEmpty freeRegs
initFreeRegs RegMap Loc
forall a. UniqFM a
emptyRegMap (DynFlags -> StackMap
emptyStackMap DynFlags
dflags) UniqSupply
us
(RegM freeRegs [NatBasicBlock instr]
-> (BlockAssignment freeRegs, StackMap, RegAllocStats,
[NatBasicBlock instr]))
-> RegM freeRegs [NatBasicBlock instr]
-> (BlockAssignment freeRegs, StackMap, RegAllocStats,
[NatBasicBlock instr])
forall a b. (a -> b) -> a -> b
$ [BlockId]
-> BlockMap RegSet
-> [NatBasicBlock instr]
-> [SCC (LiveBasicBlock instr)]
-> RegM freeRegs [NatBasicBlock instr]
forall freeRegs instr.
(FR freeRegs, Instruction instr, Outputable instr) =>
[BlockId]
-> BlockMap RegSet
-> [NatBasicBlock instr]
-> [SCC (LiveBasicBlock instr)]
-> RegM freeRegs [NatBasicBlock instr]
linearRA_SCCs [BlockId]
entry_ids BlockMap RegSet
block_live [] [SCC (LiveBasicBlock instr)]
sccs
([NatBasicBlock instr], RegAllocStats, Int)
-> UniqSM ([NatBasicBlock instr], RegAllocStats, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return ([NatBasicBlock instr]
blocks, RegAllocStats
stats, StackMap -> Int
getStackUse StackMap
stack)
linearRA_SCCs :: (FR freeRegs, Instruction instr, Outputable instr)
=> [BlockId]
-> BlockMap RegSet
-> [NatBasicBlock instr]
-> [SCC (LiveBasicBlock instr)]
-> RegM freeRegs [NatBasicBlock instr]
linearRA_SCCs :: [BlockId]
-> BlockMap RegSet
-> [NatBasicBlock instr]
-> [SCC (LiveBasicBlock instr)]
-> RegM freeRegs [NatBasicBlock instr]
linearRA_SCCs _ _ blocksAcc :: [NatBasicBlock instr]
blocksAcc []
= [NatBasicBlock instr] -> RegM freeRegs [NatBasicBlock instr]
forall (m :: * -> *) a. Monad m => a -> m a
return ([NatBasicBlock instr] -> RegM freeRegs [NatBasicBlock instr])
-> [NatBasicBlock instr] -> RegM freeRegs [NatBasicBlock instr]
forall a b. (a -> b) -> a -> b
$ [NatBasicBlock instr] -> [NatBasicBlock instr]
forall a. [a] -> [a]
reverse [NatBasicBlock instr]
blocksAcc
linearRA_SCCs entry_ids :: [BlockId]
entry_ids block_live :: BlockMap RegSet
block_live blocksAcc :: [NatBasicBlock instr]
blocksAcc (AcyclicSCC block :: LiveBasicBlock instr
block : sccs :: [SCC (LiveBasicBlock instr)]
sccs)
= do [NatBasicBlock instr]
blocks' <- BlockMap RegSet
-> LiveBasicBlock instr -> RegM freeRegs [NatBasicBlock instr]
forall freeRegs instr.
(FR freeRegs, Outputable instr, Instruction instr) =>
BlockMap RegSet
-> LiveBasicBlock instr -> RegM freeRegs [NatBasicBlock instr]
processBlock BlockMap RegSet
block_live LiveBasicBlock instr
block
[BlockId]
-> BlockMap RegSet
-> [NatBasicBlock instr]
-> [SCC (LiveBasicBlock instr)]
-> RegM freeRegs [NatBasicBlock instr]
forall freeRegs instr.
(FR freeRegs, Instruction instr, Outputable instr) =>
[BlockId]
-> BlockMap RegSet
-> [NatBasicBlock instr]
-> [SCC (LiveBasicBlock instr)]
-> RegM freeRegs [NatBasicBlock instr]
linearRA_SCCs [BlockId]
entry_ids BlockMap RegSet
block_live
(([NatBasicBlock instr] -> [NatBasicBlock instr]
forall a. [a] -> [a]
reverse [NatBasicBlock instr]
blocks') [NatBasicBlock instr]
-> [NatBasicBlock instr] -> [NatBasicBlock instr]
forall a. [a] -> [a] -> [a]
++ [NatBasicBlock instr]
blocksAcc)
[SCC (LiveBasicBlock instr)]
sccs
linearRA_SCCs entry_ids :: [BlockId]
entry_ids block_live :: BlockMap RegSet
block_live blocksAcc :: [NatBasicBlock instr]
blocksAcc (CyclicSCC blocks :: [LiveBasicBlock instr]
blocks : sccs :: [SCC (LiveBasicBlock instr)]
sccs)
= do
[[NatBasicBlock instr]]
blockss' <- [BlockId]
-> BlockMap RegSet
-> [LiveBasicBlock instr]
-> [LiveBasicBlock instr]
-> [[NatBasicBlock instr]]
-> Bool
-> RegM freeRegs [[NatBasicBlock instr]]
forall freeRegs instr.
(FR freeRegs, Instruction instr, Outputable instr) =>
[BlockId]
-> BlockMap RegSet
-> [GenBasicBlock (LiveInstr instr)]
-> [GenBasicBlock (LiveInstr instr)]
-> [[NatBasicBlock instr]]
-> Bool
-> RegM freeRegs [[NatBasicBlock instr]]
process [BlockId]
entry_ids BlockMap RegSet
block_live [LiveBasicBlock instr]
blocks [] ([NatBasicBlock instr] -> [[NatBasicBlock instr]]
forall (m :: * -> *) a. Monad m => a -> m a
return []) Bool
False
[BlockId]
-> BlockMap RegSet
-> [NatBasicBlock instr]
-> [SCC (LiveBasicBlock instr)]
-> RegM freeRegs [NatBasicBlock instr]
forall freeRegs instr.
(FR freeRegs, Instruction instr, Outputable instr) =>
[BlockId]
-> BlockMap RegSet
-> [NatBasicBlock instr]
-> [SCC (LiveBasicBlock instr)]
-> RegM freeRegs [NatBasicBlock instr]
linearRA_SCCs [BlockId]
entry_ids BlockMap RegSet
block_live
([NatBasicBlock instr] -> [NatBasicBlock instr]
forall a. [a] -> [a]
reverse ([[NatBasicBlock instr]] -> [NatBasicBlock instr]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[NatBasicBlock instr]]
blockss') [NatBasicBlock instr]
-> [NatBasicBlock instr] -> [NatBasicBlock instr]
forall a. [a] -> [a] -> [a]
++ [NatBasicBlock instr]
blocksAcc)
[SCC (LiveBasicBlock instr)]
sccs
process :: (FR freeRegs, Instruction instr, Outputable instr)
=> [BlockId]
-> BlockMap RegSet
-> [GenBasicBlock (LiveInstr instr)]
-> [GenBasicBlock (LiveInstr instr)]
-> [[NatBasicBlock instr]]
-> Bool
-> RegM freeRegs [[NatBasicBlock instr]]
process :: [BlockId]
-> BlockMap RegSet
-> [GenBasicBlock (LiveInstr instr)]
-> [GenBasicBlock (LiveInstr instr)]
-> [[NatBasicBlock instr]]
-> Bool
-> RegM freeRegs [[NatBasicBlock instr]]
process _ _ [] [] accum :: [[NatBasicBlock instr]]
accum _
= [[NatBasicBlock instr]] -> RegM freeRegs [[NatBasicBlock instr]]
forall (m :: * -> *) a. Monad m => a -> m a
return ([[NatBasicBlock instr]] -> RegM freeRegs [[NatBasicBlock instr]])
-> [[NatBasicBlock instr]] -> RegM freeRegs [[NatBasicBlock instr]]
forall a b. (a -> b) -> a -> b
$ [[NatBasicBlock instr]] -> [[NatBasicBlock instr]]
forall a. [a] -> [a]
reverse [[NatBasicBlock instr]]
accum
process entry_ids :: [BlockId]
entry_ids block_live :: BlockMap RegSet
block_live [] next_round :: [GenBasicBlock (LiveInstr instr)]
next_round accum :: [[NatBasicBlock instr]]
accum madeProgress :: Bool
madeProgress
| Bool -> Bool
not Bool
madeProgress
= [[NatBasicBlock instr]] -> RegM freeRegs [[NatBasicBlock instr]]
forall (m :: * -> *) a. Monad m => a -> m a
return ([[NatBasicBlock instr]] -> RegM freeRegs [[NatBasicBlock instr]])
-> [[NatBasicBlock instr]] -> RegM freeRegs [[NatBasicBlock instr]]
forall a b. (a -> b) -> a -> b
$ [[NatBasicBlock instr]] -> [[NatBasicBlock instr]]
forall a. [a] -> [a]
reverse [[NatBasicBlock instr]]
accum
| Bool
otherwise
= [BlockId]
-> BlockMap RegSet
-> [GenBasicBlock (LiveInstr instr)]
-> [GenBasicBlock (LiveInstr instr)]
-> [[NatBasicBlock instr]]
-> Bool
-> RegM freeRegs [[NatBasicBlock instr]]
forall freeRegs instr.
(FR freeRegs, Instruction instr, Outputable instr) =>
[BlockId]
-> BlockMap RegSet
-> [GenBasicBlock (LiveInstr instr)]
-> [GenBasicBlock (LiveInstr instr)]
-> [[NatBasicBlock instr]]
-> Bool
-> RegM freeRegs [[NatBasicBlock instr]]
process [BlockId]
entry_ids BlockMap RegSet
block_live
[GenBasicBlock (LiveInstr instr)]
next_round [] [[NatBasicBlock instr]]
accum Bool
False
process entry_ids :: [BlockId]
entry_ids block_live :: BlockMap RegSet
block_live (b :: GenBasicBlock (LiveInstr instr)
b@(BasicBlock id :: BlockId
id _) : blocks :: [GenBasicBlock (LiveInstr instr)]
blocks)
next_round :: [GenBasicBlock (LiveInstr instr)]
next_round accum :: [[NatBasicBlock instr]]
accum madeProgress :: Bool
madeProgress
= do
BlockAssignment freeRegs
block_assig <- RegM freeRegs (BlockAssignment freeRegs)
forall freeRegs. RegM freeRegs (BlockAssignment freeRegs)
getBlockAssigR
if Maybe (freeRegs, RegMap Loc) -> Bool
forall a. Maybe a -> Bool
isJust (KeyOf LabelMap
-> BlockAssignment freeRegs -> Maybe (freeRegs, RegMap Loc)
forall (map :: * -> *) a.
IsMap map =>
KeyOf map -> map a -> Maybe a
mapLookup KeyOf LabelMap
BlockId
id BlockAssignment freeRegs
block_assig)
Bool -> Bool -> Bool
|| BlockId
id BlockId -> [BlockId] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [BlockId]
entry_ids
then do
[NatBasicBlock instr]
b' <- BlockMap RegSet
-> GenBasicBlock (LiveInstr instr)
-> RegM freeRegs [NatBasicBlock instr]
forall freeRegs instr.
(FR freeRegs, Outputable instr, Instruction instr) =>
BlockMap RegSet
-> LiveBasicBlock instr -> RegM freeRegs [NatBasicBlock instr]
processBlock BlockMap RegSet
block_live GenBasicBlock (LiveInstr instr)
b
[BlockId]
-> BlockMap RegSet
-> [GenBasicBlock (LiveInstr instr)]
-> [GenBasicBlock (LiveInstr instr)]
-> [[NatBasicBlock instr]]
-> Bool
-> RegM freeRegs [[NatBasicBlock instr]]
forall freeRegs instr.
(FR freeRegs, Instruction instr, Outputable instr) =>
[BlockId]
-> BlockMap RegSet
-> [GenBasicBlock (LiveInstr instr)]
-> [GenBasicBlock (LiveInstr instr)]
-> [[NatBasicBlock instr]]
-> Bool
-> RegM freeRegs [[NatBasicBlock instr]]
process [BlockId]
entry_ids BlockMap RegSet
block_live [GenBasicBlock (LiveInstr instr)]
blocks
[GenBasicBlock (LiveInstr instr)]
next_round ([NatBasicBlock instr]
b' [NatBasicBlock instr]
-> [[NatBasicBlock instr]] -> [[NatBasicBlock instr]]
forall a. a -> [a] -> [a]
: [[NatBasicBlock instr]]
accum) Bool
True
else [BlockId]
-> BlockMap RegSet
-> [GenBasicBlock (LiveInstr instr)]
-> [GenBasicBlock (LiveInstr instr)]
-> [[NatBasicBlock instr]]
-> Bool
-> RegM freeRegs [[NatBasicBlock instr]]
forall freeRegs instr.
(FR freeRegs, Instruction instr, Outputable instr) =>
[BlockId]
-> BlockMap RegSet
-> [GenBasicBlock (LiveInstr instr)]
-> [GenBasicBlock (LiveInstr instr)]
-> [[NatBasicBlock instr]]
-> Bool
-> RegM freeRegs [[NatBasicBlock instr]]
process [BlockId]
entry_ids BlockMap RegSet
block_live [GenBasicBlock (LiveInstr instr)]
blocks
(GenBasicBlock (LiveInstr instr)
b GenBasicBlock (LiveInstr instr)
-> [GenBasicBlock (LiveInstr instr)]
-> [GenBasicBlock (LiveInstr instr)]
forall a. a -> [a] -> [a]
: [GenBasicBlock (LiveInstr instr)]
next_round) [[NatBasicBlock instr]]
accum Bool
madeProgress
processBlock
:: (FR freeRegs, Outputable instr, Instruction instr)
=> BlockMap RegSet
-> LiveBasicBlock instr
-> RegM freeRegs [NatBasicBlock instr]
processBlock :: BlockMap RegSet
-> LiveBasicBlock instr -> RegM freeRegs [NatBasicBlock instr]
processBlock block_live :: BlockMap RegSet
block_live (BasicBlock id :: BlockId
id instrs :: [LiveInstr instr]
instrs)
= do BlockId -> BlockMap RegSet -> RegM freeRegs ()
forall freeRegs.
FR freeRegs =>
BlockId -> BlockMap RegSet -> RegM freeRegs ()
initBlock BlockId
id BlockMap RegSet
block_live
(instrs' :: [instr]
instrs', fixups :: [NatBasicBlock instr]
fixups)
<- BlockMap RegSet
-> [instr]
-> [NatBasicBlock instr]
-> BlockId
-> [LiveInstr instr]
-> RegM freeRegs ([instr], [NatBasicBlock instr])
forall freeRegs instr.
(FR freeRegs, Outputable instr, Instruction instr) =>
BlockMap RegSet
-> [instr]
-> [NatBasicBlock instr]
-> BlockId
-> [LiveInstr instr]
-> RegM freeRegs ([instr], [NatBasicBlock instr])
linearRA BlockMap RegSet
block_live [] [] BlockId
id [LiveInstr instr]
instrs
[NatBasicBlock instr] -> RegM freeRegs [NatBasicBlock instr]
forall (m :: * -> *) a. Monad m => a -> m a
return ([NatBasicBlock instr] -> RegM freeRegs [NatBasicBlock instr])
-> [NatBasicBlock instr] -> RegM freeRegs [NatBasicBlock instr]
forall a b. (a -> b) -> a -> b
$ BlockId -> [instr] -> NatBasicBlock instr
forall i. BlockId -> [i] -> GenBasicBlock i
BasicBlock BlockId
id [instr]
instrs' NatBasicBlock instr
-> [NatBasicBlock instr] -> [NatBasicBlock instr]
forall a. a -> [a] -> [a]
: [NatBasicBlock instr]
fixups
initBlock :: FR freeRegs
=> BlockId -> BlockMap RegSet -> RegM freeRegs ()
initBlock :: BlockId -> BlockMap RegSet -> RegM freeRegs ()
initBlock id :: BlockId
id block_live :: BlockMap RegSet
block_live
= do DynFlags
dflags <- RegM freeRegs DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
BlockAssignment freeRegs
block_assig <- RegM freeRegs (BlockAssignment freeRegs)
forall freeRegs. RegM freeRegs (BlockAssignment freeRegs)
getBlockAssigR
case KeyOf LabelMap
-> BlockAssignment freeRegs -> Maybe (freeRegs, RegMap Loc)
forall (map :: * -> *) a.
IsMap map =>
KeyOf map -> map a -> Maybe a
mapLookup KeyOf LabelMap
BlockId
id BlockAssignment freeRegs
block_assig of
Nothing
-> do
case KeyOf LabelMap -> BlockMap RegSet -> Maybe RegSet
forall (map :: * -> *) a.
IsMap map =>
KeyOf map -> map a -> Maybe a
mapLookup KeyOf LabelMap
BlockId
id BlockMap RegSet
block_live of
Nothing ->
freeRegs -> RegM freeRegs ()
forall freeRegs. freeRegs -> RegM freeRegs ()
setFreeRegsR (Platform -> freeRegs
forall freeRegs. FR freeRegs => Platform -> freeRegs
frInitFreeRegs Platform
platform)
Just live :: RegSet
live ->
freeRegs -> RegM freeRegs ()
forall freeRegs. freeRegs -> RegM freeRegs ()
setFreeRegsR (freeRegs -> RegM freeRegs ()) -> freeRegs -> RegM freeRegs ()
forall a b. (a -> b) -> a -> b
$ (freeRegs -> RealReg -> freeRegs)
-> freeRegs -> [RealReg] -> freeRegs
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' ((RealReg -> freeRegs -> freeRegs)
-> freeRegs -> RealReg -> freeRegs
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((RealReg -> freeRegs -> freeRegs)
-> freeRegs -> RealReg -> freeRegs)
-> (RealReg -> freeRegs -> freeRegs)
-> freeRegs
-> RealReg
-> freeRegs
forall a b. (a -> b) -> a -> b
$ Platform -> RealReg -> freeRegs -> freeRegs
forall freeRegs.
FR freeRegs =>
Platform -> RealReg -> freeRegs -> freeRegs
frAllocateReg Platform
platform) (Platform -> freeRegs
forall freeRegs. FR freeRegs => Platform -> freeRegs
frInitFreeRegs Platform
platform)
[ RealReg
r | RegReal r :: RealReg
r <- RegSet -> [Reg]
forall elt. UniqSet elt -> [elt]
nonDetEltsUniqSet RegSet
live ]
RegMap Loc -> RegM freeRegs ()
forall freeRegs. RegMap Loc -> RegM freeRegs ()
setAssigR RegMap Loc
forall a. UniqFM a
emptyRegMap
Just (freeregs :: freeRegs
freeregs, assig :: RegMap Loc
assig)
-> do freeRegs -> RegM freeRegs ()
forall freeRegs. freeRegs -> RegM freeRegs ()
setFreeRegsR freeRegs
freeregs
RegMap Loc -> RegM freeRegs ()
forall freeRegs. RegMap Loc -> RegM freeRegs ()
setAssigR RegMap Loc
assig
linearRA
:: (FR freeRegs, Outputable instr, Instruction instr)
=> BlockMap RegSet
-> [instr]
-> [NatBasicBlock instr]
-> BlockId
-> [LiveInstr instr]
-> RegM freeRegs
( [instr]
, [NatBasicBlock instr])
linearRA :: BlockMap RegSet
-> [instr]
-> [NatBasicBlock instr]
-> BlockId
-> [LiveInstr instr]
-> RegM freeRegs ([instr], [NatBasicBlock instr])
linearRA _ accInstr :: [instr]
accInstr accFixup :: [NatBasicBlock instr]
accFixup _ []
= ([instr], [NatBasicBlock instr])
-> RegM freeRegs ([instr], [NatBasicBlock instr])
forall (m :: * -> *) a. Monad m => a -> m a
return
( [instr] -> [instr]
forall a. [a] -> [a]
reverse [instr]
accInstr
, [NatBasicBlock instr]
accFixup)
linearRA block_live :: BlockMap RegSet
block_live accInstr :: [instr]
accInstr accFixups :: [NatBasicBlock instr]
accFixups id :: BlockId
id (instr :: LiveInstr instr
instr:instrs :: [LiveInstr instr]
instrs)
= do
(accInstr' :: [instr]
accInstr', new_fixups :: [NatBasicBlock instr]
new_fixups) <- BlockMap RegSet
-> [instr]
-> BlockId
-> LiveInstr instr
-> RegM freeRegs ([instr], [NatBasicBlock instr])
forall freeRegs instr.
(FR freeRegs, Outputable instr, Instruction instr) =>
BlockMap RegSet
-> [instr]
-> BlockId
-> LiveInstr instr
-> RegM freeRegs ([instr], [NatBasicBlock instr])
raInsn BlockMap RegSet
block_live [instr]
accInstr BlockId
id LiveInstr instr
instr
BlockMap RegSet
-> [instr]
-> [NatBasicBlock instr]
-> BlockId
-> [LiveInstr instr]
-> RegM freeRegs ([instr], [NatBasicBlock instr])
forall freeRegs instr.
(FR freeRegs, Outputable instr, Instruction instr) =>
BlockMap RegSet
-> [instr]
-> [NatBasicBlock instr]
-> BlockId
-> [LiveInstr instr]
-> RegM freeRegs ([instr], [NatBasicBlock instr])
linearRA BlockMap RegSet
block_live [instr]
accInstr' ([NatBasicBlock instr]
new_fixups [NatBasicBlock instr]
-> [NatBasicBlock instr] -> [NatBasicBlock instr]
forall a. [a] -> [a] -> [a]
++ [NatBasicBlock instr]
accFixups) BlockId
id [LiveInstr instr]
instrs
raInsn
:: (FR freeRegs, Outputable instr, Instruction instr)
=> BlockMap RegSet
-> [instr]
-> BlockId
-> LiveInstr instr
-> RegM freeRegs
( [instr]
, [NatBasicBlock instr])
raInsn :: BlockMap RegSet
-> [instr]
-> BlockId
-> LiveInstr instr
-> RegM freeRegs ([instr], [NatBasicBlock instr])
raInsn _ new_instrs :: [instr]
new_instrs _ (LiveInstr ii :: InstrSR instr
ii Nothing)
| Just n :: Int
n <- InstrSR instr -> Maybe Int
forall instr. Instruction instr => instr -> Maybe Int
takeDeltaInstr InstrSR instr
ii
= do Int -> RegM freeRegs ()
forall freeRegs. Int -> RegM freeRegs ()
setDeltaR Int
n
([instr], [NatBasicBlock instr])
-> RegM freeRegs ([instr], [NatBasicBlock instr])
forall (m :: * -> *) a. Monad m => a -> m a
return ([instr]
new_instrs, [])
raInsn _ new_instrs :: [instr]
new_instrs _ (LiveInstr ii :: InstrSR instr
ii@(Instr i :: instr
i) Nothing)
| InstrSR instr -> Bool
forall instr. Instruction instr => instr -> Bool
isMetaInstr InstrSR instr
ii
= ([instr], [NatBasicBlock instr])
-> RegM freeRegs ([instr], [NatBasicBlock instr])
forall (m :: * -> *) a. Monad m => a -> m a
return (instr
i instr -> [instr] -> [instr]
forall a. a -> [a] -> [a]
: [instr]
new_instrs, [])
raInsn block_live :: BlockMap RegSet
block_live new_instrs :: [instr]
new_instrs id :: BlockId
id (LiveInstr (Instr instr :: instr
instr) (Just live :: Liveness
live))
= do
RegMap Loc
assig <- RegM freeRegs (RegMap Loc)
forall freeRegs. RegM freeRegs (RegMap Loc)
getAssigR
case instr -> Maybe (Reg, Reg)
forall instr. Instruction instr => instr -> Maybe (Reg, Reg)
takeRegRegMoveInstr instr
instr of
Just (src :: Reg
src,dst :: Reg
dst) | Reg
src Reg -> RegSet -> Bool
forall a. Uniquable a => a -> UniqSet a -> Bool
`elementOfUniqSet` (Liveness -> RegSet
liveDieRead Liveness
live),
Reg -> Bool
isVirtualReg Reg
dst,
Bool -> Bool
not (Reg
dst Reg -> RegMap Loc -> Bool
forall key elt. Uniquable key => key -> UniqFM elt -> Bool
`elemUFM` RegMap Loc
assig),
Reg -> Bool
isRealReg Reg
src Bool -> Bool -> Bool
|| Reg -> RegMap Loc -> Bool
isInReg Reg
src RegMap Loc
assig -> do
case Reg
src of
(RegReal rr :: RealReg
rr) -> RegMap Loc -> RegM freeRegs ()
forall freeRegs. RegMap Loc -> RegM freeRegs ()
setAssigR (RegMap Loc -> Reg -> Loc -> RegMap Loc
forall key elt.
Uniquable key =>
UniqFM elt -> key -> elt -> UniqFM elt
addToUFM RegMap Loc
assig Reg
dst (RealReg -> Loc
InReg RealReg
rr))
_virt :: Reg
_virt -> case RegMap Loc -> Reg -> Maybe Loc
forall key elt. Uniquable key => UniqFM elt -> key -> Maybe elt
lookupUFM RegMap Loc
assig Reg
src of
Nothing -> String -> RegM freeRegs ()
forall a. String -> a
panic "raInsn"
Just loc :: Loc
loc ->
RegMap Loc -> RegM freeRegs ()
forall freeRegs. RegMap Loc -> RegM freeRegs ()
setAssigR (RegMap Loc -> Reg -> Loc -> RegMap Loc
forall key elt.
Uniquable key =>
UniqFM elt -> key -> elt -> UniqFM elt
addToUFM (RegMap Loc -> Reg -> RegMap Loc
forall key elt. Uniquable key => UniqFM elt -> key -> UniqFM elt
delFromUFM RegMap Loc
assig Reg
src) Reg
dst Loc
loc)
([instr], [NatBasicBlock instr])
-> RegM freeRegs ([instr], [NatBasicBlock instr])
forall (m :: * -> *) a. Monad m => a -> m a
return ([instr]
new_instrs, [])
_ -> BlockMap RegSet
-> [instr]
-> BlockId
-> instr
-> [Reg]
-> [Reg]
-> RegM freeRegs ([instr], [NatBasicBlock instr])
forall freeRegs instr.
(FR freeRegs, Instruction instr, Outputable instr) =>
BlockMap RegSet
-> [instr]
-> BlockId
-> instr
-> [Reg]
-> [Reg]
-> RegM freeRegs ([instr], [NatBasicBlock instr])
genRaInsn BlockMap RegSet
block_live [instr]
new_instrs BlockId
id instr
instr
(RegSet -> [Reg]
forall elt. UniqSet elt -> [elt]
nonDetEltsUniqSet (RegSet -> [Reg]) -> RegSet -> [Reg]
forall a b. (a -> b) -> a -> b
$ Liveness -> RegSet
liveDieRead Liveness
live)
(RegSet -> [Reg]
forall elt. UniqSet elt -> [elt]
nonDetEltsUniqSet (RegSet -> [Reg]) -> RegSet -> [Reg]
forall a b. (a -> b) -> a -> b
$ Liveness -> RegSet
liveDieWrite Liveness
live)
raInsn _ _ _ instr :: LiveInstr instr
instr
= String -> SDoc -> RegM freeRegs ([instr], [NatBasicBlock instr])
forall a. HasCallStack => String -> SDoc -> a
pprPanic "raInsn" (String -> SDoc
text "no match for:" SDoc -> SDoc -> SDoc
<> LiveInstr instr -> SDoc
forall a. Outputable a => a -> SDoc
ppr LiveInstr instr
instr)
isInReg :: Reg -> RegMap Loc -> Bool
isInReg :: Reg -> RegMap Loc -> Bool
isInReg src :: Reg
src assig :: RegMap Loc
assig | Just (InReg _) <- RegMap Loc -> Reg -> Maybe Loc
forall key elt. Uniquable key => UniqFM elt -> key -> Maybe elt
lookupUFM RegMap Loc
assig Reg
src = Bool
True
| Bool
otherwise = Bool
False
genRaInsn :: (FR freeRegs, Instruction instr, Outputable instr)
=> BlockMap RegSet
-> [instr]
-> BlockId
-> instr
-> [Reg]
-> [Reg]
-> RegM freeRegs ([instr], [NatBasicBlock instr])
genRaInsn :: BlockMap RegSet
-> [instr]
-> BlockId
-> instr
-> [Reg]
-> [Reg]
-> RegM freeRegs ([instr], [NatBasicBlock instr])
genRaInsn block_live :: BlockMap RegSet
block_live new_instrs :: [instr]
new_instrs block_id :: BlockId
block_id instr :: instr
instr r_dying :: [Reg]
r_dying w_dying :: [Reg]
w_dying = do
DynFlags
dflags <- RegM freeRegs DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
case Platform -> instr -> RegUsage
forall instr. Instruction instr => Platform -> instr -> RegUsage
regUsageOfInstr Platform
platform instr
instr of { RU read :: [Reg]
read written :: [Reg]
written ->
do
let real_written :: [RealReg]
real_written = [ RealReg
rr | (RegReal rr :: RealReg
rr) <- [Reg]
written ]
let virt_written :: [VirtualReg]
virt_written = [ VirtualReg
vr | (RegVirtual vr :: VirtualReg
vr) <- [Reg]
written ]
let virt_read :: [VirtualReg]
virt_read = [VirtualReg] -> [VirtualReg]
forall a. Eq a => [a] -> [a]
nub [ VirtualReg
vr | (RegVirtual vr :: VirtualReg
vr) <- [Reg]
read ]
(r_spills :: [instr]
r_spills, r_allocd :: [RealReg]
r_allocd) <-
Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> [VirtualReg]
-> RegM freeRegs ([instr], [RealReg])
forall freeRegs instr.
(FR freeRegs, Outputable instr, Instruction instr) =>
Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> [VirtualReg]
-> RegM freeRegs ([instr], [RealReg])
allocateRegsAndSpill Bool
True [VirtualReg]
virt_read [] [] [VirtualReg]
virt_read
[instr]
clobber_saves <- [RealReg] -> [Reg] -> RegM freeRegs [instr]
forall instr freeRegs.
(Instruction instr, FR freeRegs) =>
[RealReg] -> [Reg] -> RegM freeRegs [instr]
saveClobberedTemps [RealReg]
real_written [Reg]
r_dying
(fixup_blocks :: [NatBasicBlock instr]
fixup_blocks, adjusted_instr :: instr
adjusted_instr)
<- BlockMap RegSet
-> BlockId -> instr -> RegM freeRegs ([NatBasicBlock instr], instr)
forall freeRegs instr.
(FR freeRegs, Instruction instr, Outputable instr) =>
BlockMap RegSet
-> BlockId -> instr -> RegM freeRegs ([NatBasicBlock instr], instr)
joinToTargets BlockMap RegSet
block_live BlockId
block_id instr
instr
[Reg] -> RegM freeRegs ()
forall freeRegs. FR freeRegs => [Reg] -> RegM freeRegs ()
releaseRegs [Reg]
r_dying
[RealReg] -> RegM freeRegs ()
forall freeRegs. FR freeRegs => [RealReg] -> RegM freeRegs ()
clobberRegs [RealReg]
real_written
(w_spills :: [instr]
w_spills, w_allocd :: [RealReg]
w_allocd) <-
Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> [VirtualReg]
-> RegM freeRegs ([instr], [RealReg])
forall freeRegs instr.
(FR freeRegs, Outputable instr, Instruction instr) =>
Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> [VirtualReg]
-> RegM freeRegs ([instr], [RealReg])
allocateRegsAndSpill Bool
False [VirtualReg]
virt_written [] [] [VirtualReg]
virt_written
[Reg] -> RegM freeRegs ()
forall freeRegs. FR freeRegs => [Reg] -> RegM freeRegs ()
releaseRegs [Reg]
w_dying
let
patch_map :: UniqFM Reg
patch_map
= [(VirtualReg, Reg)] -> UniqFM Reg
forall key elt. Uniquable key => [(key, elt)] -> UniqFM elt
listToUFM
[ (VirtualReg
t, RealReg -> Reg
RegReal RealReg
r)
| (t :: VirtualReg
t, r :: RealReg
r) <- [VirtualReg] -> [RealReg] -> [(VirtualReg, RealReg)]
forall a b. [a] -> [b] -> [(a, b)]
zip [VirtualReg]
virt_read [RealReg]
r_allocd
[(VirtualReg, RealReg)]
-> [(VirtualReg, RealReg)] -> [(VirtualReg, RealReg)]
forall a. [a] -> [a] -> [a]
++ [VirtualReg] -> [RealReg] -> [(VirtualReg, RealReg)]
forall a b. [a] -> [b] -> [(a, b)]
zip [VirtualReg]
virt_written [RealReg]
w_allocd ]
patched_instr :: instr
patched_instr
= instr -> (Reg -> Reg) -> instr
forall instr. Instruction instr => instr -> (Reg -> Reg) -> instr
patchRegsOfInstr instr
adjusted_instr Reg -> Reg
patchLookup
patchLookup :: Reg -> Reg
patchLookup x :: Reg
x
= case UniqFM Reg -> Reg -> Maybe Reg
forall key elt. Uniquable key => UniqFM elt -> key -> Maybe elt
lookupUFM UniqFM Reg
patch_map Reg
x of
Nothing -> Reg
x
Just y :: Reg
y -> Reg
y
let squashed_instr :: [instr]
squashed_instr = case instr -> Maybe (Reg, Reg)
forall instr. Instruction instr => instr -> Maybe (Reg, Reg)
takeRegRegMoveInstr instr
patched_instr of
Just (src :: Reg
src, dst :: Reg
dst)
| Reg
src Reg -> Reg -> Bool
forall a. Eq a => a -> a -> Bool
== Reg
dst -> []
_ -> [instr
patched_instr]
let code :: [instr]
code = [instr]
squashed_instr [instr] -> [instr] -> [instr]
forall a. [a] -> [a] -> [a]
++ [instr]
w_spills [instr] -> [instr] -> [instr]
forall a. [a] -> [a] -> [a]
++ [instr] -> [instr]
forall a. [a] -> [a]
reverse [instr]
r_spills
[instr] -> [instr] -> [instr]
forall a. [a] -> [a] -> [a]
++ [instr]
clobber_saves [instr] -> [instr] -> [instr]
forall a. [a] -> [a] -> [a]
++ [instr]
new_instrs
([instr], [NatBasicBlock instr])
-> RegM freeRegs ([instr], [NatBasicBlock instr])
forall (m :: * -> *) a. Monad m => a -> m a
return ([instr]
code, [NatBasicBlock instr]
fixup_blocks)
}
releaseRegs :: FR freeRegs => [Reg] -> RegM freeRegs ()
releaseRegs :: [Reg] -> RegM freeRegs ()
releaseRegs regs :: [Reg]
regs = do
DynFlags
dflags <- RegM freeRegs DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
RegMap Loc
assig <- RegM freeRegs (RegMap Loc)
forall freeRegs. RegM freeRegs (RegMap Loc)
getAssigR
freeRegs
free <- RegM freeRegs freeRegs
forall freeRegs. RegM freeRegs freeRegs
getFreeRegsR
let loop :: RegMap Loc -> freeRegs -> [Reg] -> RegM freeRegs ()
loop assig :: RegMap Loc
assig !freeRegs
free [] = do RegMap Loc -> RegM freeRegs ()
forall freeRegs. RegMap Loc -> RegM freeRegs ()
setAssigR RegMap Loc
assig; freeRegs -> RegM freeRegs ()
forall freeRegs. freeRegs -> RegM freeRegs ()
setFreeRegsR freeRegs
free; () -> RegM freeRegs ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
loop assig :: RegMap Loc
assig !freeRegs
free (RegReal rr :: RealReg
rr : rs :: [Reg]
rs) = RegMap Loc -> freeRegs -> [Reg] -> RegM freeRegs ()
loop RegMap Loc
assig (Platform -> RealReg -> freeRegs -> freeRegs
forall freeRegs.
FR freeRegs =>
Platform -> RealReg -> freeRegs -> freeRegs
frReleaseReg Platform
platform RealReg
rr freeRegs
free) [Reg]
rs
loop assig :: RegMap Loc
assig !freeRegs
free (r :: Reg
r:rs :: [Reg]
rs) =
case RegMap Loc -> Reg -> Maybe Loc
forall key elt. Uniquable key => UniqFM elt -> key -> Maybe elt
lookupUFM RegMap Loc
assig Reg
r of
Just (InBoth real :: RealReg
real _) -> RegMap Loc -> freeRegs -> [Reg] -> RegM freeRegs ()
loop (RegMap Loc -> Reg -> RegMap Loc
forall key elt. Uniquable key => UniqFM elt -> key -> UniqFM elt
delFromUFM RegMap Loc
assig Reg
r)
(Platform -> RealReg -> freeRegs -> freeRegs
forall freeRegs.
FR freeRegs =>
Platform -> RealReg -> freeRegs -> freeRegs
frReleaseReg Platform
platform RealReg
real freeRegs
free) [Reg]
rs
Just (InReg real :: RealReg
real) -> RegMap Loc -> freeRegs -> [Reg] -> RegM freeRegs ()
loop (RegMap Loc -> Reg -> RegMap Loc
forall key elt. Uniquable key => UniqFM elt -> key -> UniqFM elt
delFromUFM RegMap Loc
assig Reg
r)
(Platform -> RealReg -> freeRegs -> freeRegs
forall freeRegs.
FR freeRegs =>
Platform -> RealReg -> freeRegs -> freeRegs
frReleaseReg Platform
platform RealReg
real freeRegs
free) [Reg]
rs
_ -> RegMap Loc -> freeRegs -> [Reg] -> RegM freeRegs ()
loop (RegMap Loc -> Reg -> RegMap Loc
forall key elt. Uniquable key => UniqFM elt -> key -> UniqFM elt
delFromUFM RegMap Loc
assig Reg
r) freeRegs
free [Reg]
rs
RegMap Loc -> freeRegs -> [Reg] -> RegM freeRegs ()
forall freeRegs.
FR freeRegs =>
RegMap Loc -> freeRegs -> [Reg] -> RegM freeRegs ()
loop RegMap Loc
assig freeRegs
free [Reg]
regs
saveClobberedTemps
:: (Instruction instr, FR freeRegs)
=> [RealReg]
-> [Reg]
-> RegM freeRegs [instr]
saveClobberedTemps :: [RealReg] -> [Reg] -> RegM freeRegs [instr]
saveClobberedTemps [] _
= [instr] -> RegM freeRegs [instr]
forall (m :: * -> *) a. Monad m => a -> m a
return []
saveClobberedTemps clobbered :: [RealReg]
clobbered dying :: [Reg]
dying
= do
RegMap Loc
assig <- RegM freeRegs (RegMap Loc)
forall freeRegs. RegM freeRegs (RegMap Loc)
getAssigR
let to_spill :: [(Unique, RealReg)]
to_spill
= [ (Unique
temp,RealReg
reg)
| (temp :: Unique
temp, InReg reg :: RealReg
reg) <- RegMap Loc -> [(Unique, Loc)]
forall elt. UniqFM elt -> [(Unique, elt)]
nonDetUFMToList RegMap Loc
assig
, (RealReg -> Bool) -> [RealReg] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (RealReg -> RealReg -> Bool
realRegsAlias RealReg
reg) [RealReg]
clobbered
, Unique
temp Unique -> [Unique] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` (Reg -> Unique) -> [Reg] -> [Unique]
forall a b. (a -> b) -> [a] -> [b]
map Reg -> Unique
forall a. Uniquable a => a -> Unique
getUnique [Reg]
dying ]
(instrs :: [instr]
instrs,assig' :: RegMap Loc
assig') <- RegMap Loc
-> [instr]
-> [(Unique, RealReg)]
-> RegM freeRegs ([instr], RegMap Loc)
forall freeRegs a.
(FR freeRegs, Instruction a) =>
RegMap Loc
-> [a] -> [(Unique, RealReg)] -> RegM freeRegs ([a], RegMap Loc)
clobber RegMap Loc
assig [] [(Unique, RealReg)]
to_spill
RegMap Loc -> RegM freeRegs ()
forall freeRegs. RegMap Loc -> RegM freeRegs ()
setAssigR RegMap Loc
assig'
[instr] -> RegM freeRegs [instr]
forall (m :: * -> *) a. Monad m => a -> m a
return [instr]
instrs
where
clobber :: RegMap Loc
-> [a] -> [(Unique, RealReg)] -> RegM freeRegs ([a], RegMap Loc)
clobber assig :: RegMap Loc
assig instrs :: [a]
instrs []
= ([a], RegMap Loc) -> RegM freeRegs ([a], RegMap Loc)
forall (m :: * -> *) a. Monad m => a -> m a
return ([a]
instrs, RegMap Loc
assig)
clobber assig :: RegMap Loc
assig instrs :: [a]
instrs ((temp :: Unique
temp, reg :: RealReg
reg) : rest :: [(Unique, RealReg)]
rest)
= do DynFlags
dflags <- RegM freeRegs DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
freeRegs
freeRegs <- RegM freeRegs freeRegs
forall freeRegs. RegM freeRegs freeRegs
getFreeRegsR
let regclass :: RegClass
regclass = Platform -> RealReg -> RegClass
targetClassOfRealReg Platform
platform RealReg
reg
freeRegs_thisClass :: [RealReg]
freeRegs_thisClass = Platform -> RegClass -> freeRegs -> [RealReg]
forall freeRegs.
FR freeRegs =>
Platform -> RegClass -> freeRegs -> [RealReg]
frGetFreeRegs Platform
platform RegClass
regclass freeRegs
freeRegs
case (RealReg -> Bool) -> [RealReg] -> [RealReg]
forall a. (a -> Bool) -> [a] -> [a]
filter (RealReg -> [RealReg] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [RealReg]
clobbered) [RealReg]
freeRegs_thisClass of
(my_reg :: RealReg
my_reg : _) -> do
freeRegs -> RegM freeRegs ()
forall freeRegs. freeRegs -> RegM freeRegs ()
setFreeRegsR (Platform -> RealReg -> freeRegs -> freeRegs
forall freeRegs.
FR freeRegs =>
Platform -> RealReg -> freeRegs -> freeRegs
frAllocateReg Platform
platform RealReg
my_reg freeRegs
freeRegs)
let new_assign :: RegMap Loc
new_assign = RegMap Loc -> Unique -> Loc -> RegMap Loc
forall key elt.
Uniquable key =>
UniqFM elt -> key -> elt -> UniqFM elt
addToUFM RegMap Loc
assig Unique
temp (RealReg -> Loc
InReg RealReg
my_reg)
let instr :: a
instr = Platform -> Reg -> Reg -> a
forall instr. Instruction instr => Platform -> Reg -> Reg -> instr
mkRegRegMoveInstr Platform
platform
(RealReg -> Reg
RegReal RealReg
reg) (RealReg -> Reg
RegReal RealReg
my_reg)
RegMap Loc
-> [a] -> [(Unique, RealReg)] -> RegM freeRegs ([a], RegMap Loc)
clobber RegMap Loc
new_assign (a
instr a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
instrs) [(Unique, RealReg)]
rest
[] -> do
(spill :: a
spill, slot :: Int
slot) <- Reg -> Unique -> RegM freeRegs (a, Int)
forall instr freeRegs.
Instruction instr =>
Reg -> Unique -> RegM freeRegs (instr, Int)
spillR (RealReg -> Reg
RegReal RealReg
reg) Unique
temp
SpillReason -> RegM freeRegs ()
forall freeRegs. SpillReason -> RegM freeRegs ()
recordSpill (Unique -> SpillReason
SpillClobber Unique
temp)
let new_assign :: RegMap Loc
new_assign = RegMap Loc -> Unique -> Loc -> RegMap Loc
forall key elt.
Uniquable key =>
UniqFM elt -> key -> elt -> UniqFM elt
addToUFM RegMap Loc
assig Unique
temp (RealReg -> Int -> Loc
InBoth RealReg
reg Int
slot)
RegMap Loc
-> [a] -> [(Unique, RealReg)] -> RegM freeRegs ([a], RegMap Loc)
clobber RegMap Loc
new_assign (a
spill a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
instrs) [(Unique, RealReg)]
rest
clobberRegs :: FR freeRegs => [RealReg] -> RegM freeRegs ()
clobberRegs :: [RealReg] -> RegM freeRegs ()
clobberRegs []
= () -> RegM freeRegs ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
clobberRegs clobbered :: [RealReg]
clobbered
= do DynFlags
dflags <- RegM freeRegs DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
freeRegs
freeregs <- RegM freeRegs freeRegs
forall freeRegs. RegM freeRegs freeRegs
getFreeRegsR
freeRegs -> RegM freeRegs ()
forall freeRegs. freeRegs -> RegM freeRegs ()
setFreeRegsR (freeRegs -> RegM freeRegs ()) -> freeRegs -> RegM freeRegs ()
forall a b. (a -> b) -> a -> b
$! (freeRegs -> RealReg -> freeRegs)
-> freeRegs -> [RealReg] -> freeRegs
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' ((RealReg -> freeRegs -> freeRegs)
-> freeRegs -> RealReg -> freeRegs
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((RealReg -> freeRegs -> freeRegs)
-> freeRegs -> RealReg -> freeRegs)
-> (RealReg -> freeRegs -> freeRegs)
-> freeRegs
-> RealReg
-> freeRegs
forall a b. (a -> b) -> a -> b
$ Platform -> RealReg -> freeRegs -> freeRegs
forall freeRegs.
FR freeRegs =>
Platform -> RealReg -> freeRegs -> freeRegs
frAllocateReg Platform
platform) freeRegs
freeregs [RealReg]
clobbered
RegMap Loc
assig <- RegM freeRegs (RegMap Loc)
forall freeRegs. RegM freeRegs (RegMap Loc)
getAssigR
RegMap Loc -> RegM freeRegs ()
forall freeRegs. RegMap Loc -> RegM freeRegs ()
setAssigR (RegMap Loc -> RegM freeRegs ()) -> RegMap Loc -> RegM freeRegs ()
forall a b. (a -> b) -> a -> b
$! RegMap Loc -> [(Unique, Loc)] -> RegMap Loc
forall key.
Uniquable key =>
RegMap Loc -> [(key, Loc)] -> RegMap Loc
clobber RegMap Loc
assig (RegMap Loc -> [(Unique, Loc)]
forall elt. UniqFM elt -> [(Unique, elt)]
nonDetUFMToList RegMap Loc
assig)
where
clobber :: RegMap Loc -> [(key, Loc)] -> RegMap Loc
clobber assig :: RegMap Loc
assig []
= RegMap Loc
assig
clobber assig :: RegMap Loc
assig ((temp :: key
temp, InBoth reg :: RealReg
reg slot :: Int
slot) : rest :: [(key, Loc)]
rest)
| (RealReg -> Bool) -> [RealReg] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (RealReg -> RealReg -> Bool
realRegsAlias RealReg
reg) [RealReg]
clobbered
= RegMap Loc -> [(key, Loc)] -> RegMap Loc
clobber (RegMap Loc -> key -> Loc -> RegMap Loc
forall key elt.
Uniquable key =>
UniqFM elt -> key -> elt -> UniqFM elt
addToUFM RegMap Loc
assig key
temp (Int -> Loc
InMem Int
slot)) [(key, Loc)]
rest
clobber assig :: RegMap Loc
assig (_:rest :: [(key, Loc)]
rest)
= RegMap Loc -> [(key, Loc)] -> RegMap Loc
clobber RegMap Loc
assig [(key, Loc)]
rest
data SpillLoc = ReadMem StackSlot
| WriteNew
| WriteMem
allocateRegsAndSpill
:: (FR freeRegs, Outputable instr, Instruction instr)
=> Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> [VirtualReg]
-> RegM freeRegs ( [instr] , [RealReg])
allocateRegsAndSpill :: Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> [VirtualReg]
-> RegM freeRegs ([instr], [RealReg])
allocateRegsAndSpill _ _ spills :: [instr]
spills alloc :: [RealReg]
alloc []
= ([instr], [RealReg]) -> RegM freeRegs ([instr], [RealReg])
forall (m :: * -> *) a. Monad m => a -> m a
return ([instr]
spills, [RealReg] -> [RealReg]
forall a. [a] -> [a]
reverse [RealReg]
alloc)
allocateRegsAndSpill reading :: Bool
reading keep :: [VirtualReg]
keep spills :: [instr]
spills alloc :: [RealReg]
alloc (r :: VirtualReg
r:rs :: [VirtualReg]
rs)
= do RegMap Loc
assig <- RegM freeRegs (RegMap Loc)
forall freeRegs. RegM freeRegs (RegMap Loc)
getAssigR
let doSpill :: SpillLoc -> RegM freeRegs ([instr], [RealReg])
doSpill = Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> VirtualReg
-> [VirtualReg]
-> RegMap Loc
-> SpillLoc
-> RegM freeRegs ([instr], [RealReg])
forall freeRegs instr.
(FR freeRegs, Instruction instr, Outputable instr) =>
Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> VirtualReg
-> [VirtualReg]
-> RegMap Loc
-> SpillLoc
-> RegM freeRegs ([instr], [RealReg])
allocRegsAndSpill_spill Bool
reading [VirtualReg]
keep [instr]
spills [RealReg]
alloc VirtualReg
r [VirtualReg]
rs RegMap Loc
assig
case RegMap Loc -> VirtualReg -> Maybe Loc
forall key elt. Uniquable key => UniqFM elt -> key -> Maybe elt
lookupUFM RegMap Loc
assig VirtualReg
r of
Just (InReg my_reg :: RealReg
my_reg) ->
Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> [VirtualReg]
-> RegM freeRegs ([instr], [RealReg])
forall freeRegs instr.
(FR freeRegs, Outputable instr, Instruction instr) =>
Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> [VirtualReg]
-> RegM freeRegs ([instr], [RealReg])
allocateRegsAndSpill Bool
reading [VirtualReg]
keep [instr]
spills (RealReg
my_regRealReg -> [RealReg] -> [RealReg]
forall a. a -> [a] -> [a]
:[RealReg]
alloc) [VirtualReg]
rs
Just (InBoth my_reg :: RealReg
my_reg _)
-> do Bool -> RegM freeRegs () -> RegM freeRegs ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not Bool
reading) (RegMap Loc -> RegM freeRegs ()
forall freeRegs. RegMap Loc -> RegM freeRegs ()
setAssigR (RegMap Loc -> VirtualReg -> Loc -> RegMap Loc
forall key elt.
Uniquable key =>
UniqFM elt -> key -> elt -> UniqFM elt
addToUFM RegMap Loc
assig VirtualReg
r (RealReg -> Loc
InReg RealReg
my_reg)))
Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> [VirtualReg]
-> RegM freeRegs ([instr], [RealReg])
forall freeRegs instr.
(FR freeRegs, Outputable instr, Instruction instr) =>
Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> [VirtualReg]
-> RegM freeRegs ([instr], [RealReg])
allocateRegsAndSpill Bool
reading [VirtualReg]
keep [instr]
spills (RealReg
my_regRealReg -> [RealReg] -> [RealReg]
forall a. a -> [a] -> [a]
:[RealReg]
alloc) [VirtualReg]
rs
Just (InMem slot :: Int
slot) | Bool
reading -> SpillLoc -> RegM freeRegs ([instr], [RealReg])
doSpill (Int -> SpillLoc
ReadMem Int
slot)
| Bool
otherwise -> SpillLoc -> RegM freeRegs ([instr], [RealReg])
doSpill SpillLoc
WriteMem
Nothing | Bool
reading ->
String -> SDoc -> RegM freeRegs ([instr], [RealReg])
forall a. HasCallStack => String -> SDoc -> a
pprPanic "allocateRegsAndSpill: Cannot read from uninitialized register" (VirtualReg -> SDoc
forall a. Outputable a => a -> SDoc
ppr VirtualReg
r)
| Bool
otherwise -> SpillLoc -> RegM freeRegs ([instr], [RealReg])
doSpill SpillLoc
WriteNew
allocRegsAndSpill_spill :: (FR freeRegs, Instruction instr, Outputable instr)
=> Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> VirtualReg
-> [VirtualReg]
-> UniqFM Loc
-> SpillLoc
-> RegM freeRegs ([instr], [RealReg])
allocRegsAndSpill_spill :: Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> VirtualReg
-> [VirtualReg]
-> RegMap Loc
-> SpillLoc
-> RegM freeRegs ([instr], [RealReg])
allocRegsAndSpill_spill reading :: Bool
reading keep :: [VirtualReg]
keep spills :: [instr]
spills alloc :: [RealReg]
alloc r :: VirtualReg
r rs :: [VirtualReg]
rs assig :: RegMap Loc
assig spill_loc :: SpillLoc
spill_loc
= do DynFlags
dflags <- RegM freeRegs DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
freeRegs
freeRegs <- RegM freeRegs freeRegs
forall freeRegs. RegM freeRegs freeRegs
getFreeRegsR
let freeRegs_thisClass :: [RealReg]
freeRegs_thisClass = Platform -> RegClass -> freeRegs -> [RealReg]
forall freeRegs.
FR freeRegs =>
Platform -> RegClass -> freeRegs -> [RealReg]
frGetFreeRegs Platform
platform (VirtualReg -> RegClass
classOfVirtualReg VirtualReg
r) freeRegs
freeRegs
case [RealReg]
freeRegs_thisClass of
(my_reg :: RealReg
my_reg : _) ->
do [instr]
spills' <- VirtualReg
-> SpillLoc -> RealReg -> [instr] -> RegM freeRegs [instr]
forall instr freeRegs.
Instruction instr =>
VirtualReg
-> SpillLoc -> RealReg -> [instr] -> RegM freeRegs [instr]
loadTemp VirtualReg
r SpillLoc
spill_loc RealReg
my_reg [instr]
spills
RegMap Loc -> RegM freeRegs ()
forall freeRegs. RegMap Loc -> RegM freeRegs ()
setAssigR (RegMap Loc -> VirtualReg -> Loc -> RegMap Loc
forall key elt.
Uniquable key =>
UniqFM elt -> key -> elt -> UniqFM elt
addToUFM RegMap Loc
assig VirtualReg
r (Loc -> RegMap Loc) -> Loc -> RegMap Loc
forall a b. (a -> b) -> a -> b
$! SpillLoc -> RealReg -> Loc
newLocation SpillLoc
spill_loc RealReg
my_reg)
freeRegs -> RegM freeRegs ()
forall freeRegs. freeRegs -> RegM freeRegs ()
setFreeRegsR (freeRegs -> RegM freeRegs ()) -> freeRegs -> RegM freeRegs ()
forall a b. (a -> b) -> a -> b
$ Platform -> RealReg -> freeRegs -> freeRegs
forall freeRegs.
FR freeRegs =>
Platform -> RealReg -> freeRegs -> freeRegs
frAllocateReg Platform
platform RealReg
my_reg freeRegs
freeRegs
Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> [VirtualReg]
-> RegM freeRegs ([instr], [RealReg])
forall freeRegs instr.
(FR freeRegs, Outputable instr, Instruction instr) =>
Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> [VirtualReg]
-> RegM freeRegs ([instr], [RealReg])
allocateRegsAndSpill Bool
reading [VirtualReg]
keep [instr]
spills' (RealReg
my_reg RealReg -> [RealReg] -> [RealReg]
forall a. a -> [a] -> [a]
: [RealReg]
alloc) [VirtualReg]
rs
[] ->
do let inRegOrBoth :: Loc -> Bool
inRegOrBoth (InReg _) = Bool
True
inRegOrBoth (InBoth _ _) = Bool
True
inRegOrBoth _ = Bool
False
let candidates' :: RegMap Loc
candidates' =
(RegMap Loc -> [VirtualReg] -> RegMap Loc)
-> [VirtualReg] -> RegMap Loc -> RegMap Loc
forall a b c. (a -> b -> c) -> b -> a -> c
flip RegMap Loc -> [VirtualReg] -> RegMap Loc
forall key elt. Uniquable key => UniqFM elt -> [key] -> UniqFM elt
delListFromUFM [VirtualReg]
keep (RegMap Loc -> RegMap Loc) -> RegMap Loc -> RegMap Loc
forall a b. (a -> b) -> a -> b
$
(Loc -> Bool) -> RegMap Loc -> RegMap Loc
forall elt. (elt -> Bool) -> UniqFM elt -> UniqFM elt
filterUFM Loc -> Bool
inRegOrBoth (RegMap Loc -> RegMap Loc) -> RegMap Loc -> RegMap Loc
forall a b. (a -> b) -> a -> b
$
RegMap Loc
assig
let candidates :: [(Unique, Loc)]
candidates = RegMap Loc -> [(Unique, Loc)]
forall elt. UniqFM elt -> [(Unique, elt)]
nonDetUFMToList RegMap Loc
candidates'
let candidates_inBoth :: [(Unique, RealReg, Int)]
candidates_inBoth
= [ (Unique
temp, RealReg
reg, Int
mem)
| (temp :: Unique
temp, InBoth reg :: RealReg
reg mem :: Int
mem) <- [(Unique, Loc)]
candidates
, Platform -> RealReg -> RegClass
targetClassOfRealReg Platform
platform RealReg
reg RegClass -> RegClass -> Bool
forall a. Eq a => a -> a -> Bool
== VirtualReg -> RegClass
classOfVirtualReg VirtualReg
r ]
let candidates_inReg :: [(Unique, RealReg)]
candidates_inReg
= [ (Unique
temp, RealReg
reg)
| (temp :: Unique
temp, InReg reg :: RealReg
reg) <- [(Unique, Loc)]
candidates
, Platform -> RealReg -> RegClass
targetClassOfRealReg Platform
platform RealReg
reg RegClass -> RegClass -> Bool
forall a. Eq a => a -> a -> Bool
== VirtualReg -> RegClass
classOfVirtualReg VirtualReg
r ]
let result :: RegM freeRegs ([instr], [RealReg])
result
| (temp :: Unique
temp, my_reg :: RealReg
my_reg, slot :: Int
slot) : _ <- [(Unique, RealReg, Int)]
candidates_inBoth
= do [instr]
spills' <- VirtualReg
-> SpillLoc -> RealReg -> [instr] -> RegM freeRegs [instr]
forall instr freeRegs.
Instruction instr =>
VirtualReg
-> SpillLoc -> RealReg -> [instr] -> RegM freeRegs [instr]
loadTemp VirtualReg
r SpillLoc
spill_loc RealReg
my_reg [instr]
spills
let assig1 :: RegMap Loc
assig1 = RegMap Loc -> Unique -> Loc -> RegMap Loc
forall key elt.
Uniquable key =>
UniqFM elt -> key -> elt -> UniqFM elt
addToUFM RegMap Loc
assig Unique
temp (Int -> Loc
InMem Int
slot)
let assig2 :: RegMap Loc
assig2 = RegMap Loc -> VirtualReg -> Loc -> RegMap Loc
forall key elt.
Uniquable key =>
UniqFM elt -> key -> elt -> UniqFM elt
addToUFM RegMap Loc
assig1 VirtualReg
r (Loc -> RegMap Loc) -> Loc -> RegMap Loc
forall a b. (a -> b) -> a -> b
$! SpillLoc -> RealReg -> Loc
newLocation SpillLoc
spill_loc RealReg
my_reg
RegMap Loc -> RegM freeRegs ()
forall freeRegs. RegMap Loc -> RegM freeRegs ()
setAssigR RegMap Loc
assig2
Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> [VirtualReg]
-> RegM freeRegs ([instr], [RealReg])
forall freeRegs instr.
(FR freeRegs, Outputable instr, Instruction instr) =>
Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> [VirtualReg]
-> RegM freeRegs ([instr], [RealReg])
allocateRegsAndSpill Bool
reading [VirtualReg]
keep [instr]
spills' (RealReg
my_regRealReg -> [RealReg] -> [RealReg]
forall a. a -> [a] -> [a]
:[RealReg]
alloc) [VirtualReg]
rs
| (temp_to_push_out :: Unique
temp_to_push_out, (RealReg
my_reg :: RealReg)) : _
<- [(Unique, RealReg)]
candidates_inReg
= do
(spill_insn :: instr
spill_insn, slot :: Int
slot) <- Reg -> Unique -> RegM freeRegs (instr, Int)
forall instr freeRegs.
Instruction instr =>
Reg -> Unique -> RegM freeRegs (instr, Int)
spillR (RealReg -> Reg
RegReal RealReg
my_reg) Unique
temp_to_push_out
let spill_store :: [instr]
spill_store = (if Bool
reading then [instr] -> [instr]
forall a. a -> a
id else [instr] -> [instr]
forall a. [a] -> [a]
reverse)
[
instr
spill_insn ]
SpillReason -> RegM freeRegs ()
forall freeRegs. SpillReason -> RegM freeRegs ()
recordSpill (Unique -> SpillReason
SpillAlloc Unique
temp_to_push_out)
let assig1 :: RegMap Loc
assig1 = RegMap Loc -> Unique -> Loc -> RegMap Loc
forall key elt.
Uniquable key =>
UniqFM elt -> key -> elt -> UniqFM elt
addToUFM RegMap Loc
assig Unique
temp_to_push_out (Int -> Loc
InMem Int
slot)
let assig2 :: RegMap Loc
assig2 = RegMap Loc -> VirtualReg -> Loc -> RegMap Loc
forall key elt.
Uniquable key =>
UniqFM elt -> key -> elt -> UniqFM elt
addToUFM RegMap Loc
assig1 VirtualReg
r (Loc -> RegMap Loc) -> Loc -> RegMap Loc
forall a b. (a -> b) -> a -> b
$! SpillLoc -> RealReg -> Loc
newLocation SpillLoc
spill_loc RealReg
my_reg
RegMap Loc -> RegM freeRegs ()
forall freeRegs. RegMap Loc -> RegM freeRegs ()
setAssigR RegMap Loc
assig2
[instr]
spills' <- VirtualReg
-> SpillLoc -> RealReg -> [instr] -> RegM freeRegs [instr]
forall instr freeRegs.
Instruction instr =>
VirtualReg
-> SpillLoc -> RealReg -> [instr] -> RegM freeRegs [instr]
loadTemp VirtualReg
r SpillLoc
spill_loc RealReg
my_reg [instr]
spills
Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> [VirtualReg]
-> RegM freeRegs ([instr], [RealReg])
forall freeRegs instr.
(FR freeRegs, Outputable instr, Instruction instr) =>
Bool
-> [VirtualReg]
-> [instr]
-> [RealReg]
-> [VirtualReg]
-> RegM freeRegs ([instr], [RealReg])
allocateRegsAndSpill Bool
reading [VirtualReg]
keep
([instr]
spill_store [instr] -> [instr] -> [instr]
forall a. [a] -> [a] -> [a]
++ [instr]
spills')
(RealReg
my_regRealReg -> [RealReg] -> [RealReg]
forall a. a -> [a] -> [a]
:[RealReg]
alloc) [VirtualReg]
rs
| Bool
otherwise
= String -> SDoc -> RegM freeRegs ([instr], [RealReg])
forall a. HasCallStack => String -> SDoc -> a
pprPanic ("RegAllocLinear.allocRegsAndSpill: no spill candidates\n")
(SDoc -> RegM freeRegs ([instr], [RealReg]))
-> SDoc -> RegM freeRegs ([instr], [RealReg])
forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
vcat
[ String -> SDoc
text "allocating vreg: " SDoc -> SDoc -> SDoc
<> String -> SDoc
text (VirtualReg -> String
forall a. Show a => a -> String
show VirtualReg
r)
, String -> SDoc
text "assignment: " SDoc -> SDoc -> SDoc
<> RegMap Loc -> SDoc
forall a. Outputable a => a -> SDoc
ppr RegMap Loc
assig
, String -> SDoc
text "freeRegs: " SDoc -> SDoc -> SDoc
<> String -> SDoc
text (freeRegs -> String
forall a. Show a => a -> String
show freeRegs
freeRegs)
, String -> SDoc
text "initFreeRegs: " SDoc -> SDoc -> SDoc
<> String -> SDoc
text (freeRegs -> String
forall a. Show a => a -> String
show (Platform -> freeRegs
forall freeRegs. FR freeRegs => Platform -> freeRegs
frInitFreeRegs Platform
platform freeRegs -> freeRegs -> freeRegs
forall a. a -> a -> a
`asTypeOf` freeRegs
freeRegs)) ]
RegM freeRegs ([instr], [RealReg])
result
newLocation :: SpillLoc -> RealReg -> Loc
newLocation :: SpillLoc -> RealReg -> Loc
newLocation (ReadMem slot :: Int
slot) my_reg :: RealReg
my_reg = RealReg -> Int -> Loc
InBoth RealReg
my_reg Int
slot
newLocation _ my_reg :: RealReg
my_reg = RealReg -> Loc
InReg RealReg
my_reg
loadTemp
:: (Instruction instr)
=> VirtualReg
-> SpillLoc
-> RealReg
-> [instr]
-> RegM freeRegs [instr]
loadTemp :: VirtualReg
-> SpillLoc -> RealReg -> [instr] -> RegM freeRegs [instr]
loadTemp vreg :: VirtualReg
vreg (ReadMem slot :: Int
slot) hreg :: RealReg
hreg spills :: [instr]
spills
= do
instr
insn <- Reg -> Int -> RegM freeRegs instr
forall instr freeRegs.
Instruction instr =>
Reg -> Int -> RegM freeRegs instr
loadR (RealReg -> Reg
RegReal RealReg
hreg) Int
slot
SpillReason -> RegM freeRegs ()
forall freeRegs. SpillReason -> RegM freeRegs ()
recordSpill (Unique -> SpillReason
SpillLoad (Unique -> SpillReason) -> Unique -> SpillReason
forall a b. (a -> b) -> a -> b
$ VirtualReg -> Unique
forall a. Uniquable a => a -> Unique
getUnique VirtualReg
vreg)
[instr] -> RegM freeRegs [instr]
forall (m :: * -> *) a. Monad m => a -> m a
return ([instr] -> RegM freeRegs [instr])
-> [instr] -> RegM freeRegs [instr]
forall a b. (a -> b) -> a -> b
$ instr
insn instr -> [instr] -> [instr]
forall a. a -> [a] -> [a]
: [instr]
spills
loadTemp _ _ _ spills :: [instr]
spills =
[instr] -> RegM freeRegs [instr]
forall (m :: * -> *) a. Monad m => a -> m a
return [instr]
spills