<commit_before>module Optimizer.RegisterAllocation(optimize) where

import Datatypes
import Data.List
import Data.Maybe
import qualified Data.Map as Map
import Control.Monad.State
import Control.Monad.Writer
import Optimizer.Dataflow(getLiveVariables)

data CG =
  CG {
    registers :: [Int],
    tempMap :: Map.Map Int Int,
    permaMap :: Map.Map Int Int
  }
  deriving (Show)

optimize :: [IR] -> Writer [String] [IR]
optimize ir = do
  let liveVariables = getLiveVariables ir
      registersUsed = maximum $ map length liveVariables
      CG _ _ registerMappings = execState
        (mapM_ allocateRegisters liveVariables)
        (CG [7..] Map.empty Map.empty)

  tell [ "Program mapped using " ++ show registersUsed ++ " of 9 available registers:" ++ show liveVariables ]
  return $ map (rewriteRegisters registerMappings) ir<commit_msg>Reduce compiler output from register allocation stage<commit_after>module Optimizer.RegisterAllocation(optimize) where

import Datatypes
import Data.List
import Data.Maybe
import qualified Data.Map as Map
import Control.Monad.State
import Control.Monad.Writer
import Optimizer.Dataflow(getLiveVariables)

data CG =
  CG {
    registers :: [Int],
    tempMap :: Map.Map Int Int,
    permaMap :: Map.Map Int Int
  }
  deriving (Show)

optimize :: [IR] -> Writer [String] [IR]
optimize ir = do
  let liveVariables = getLiveVariables ir
      registersUsed = maximum $ map length liveVariables
      CG _ _ registerMappings = execState
        (mapM_ allocateRegisters liveVariables)
        (CG [7..] Map.empty Map.empty)

  tell [ "Program mapped using " ++ show registersUsed ++ " of 9 available registers" ]
  return $ map (rewriteRegisters registerMappings) ir
{'fraction_non_alphanumeric': 0.08504398826979472, 'fraction_numerical': 0.002346041055718475, 'mean_word_length': 3.874285714285714, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5611096', 'n_tokens_mistral': 496, 'n_tokens_neox': 491, 'n_words': 161}