<commit_before>{-# LANGUAGE ScopedTypeVariables #-}
module OPT where

import Data.List
import Data.Maybe
import qualified Data.Map as HashMap
import qualified Data.Set as Set
import qualified LLIR
import LLIR
import Text.Printf
import Data.Word

-- NEED TO TO MEM2REG AS REQUISITE
mem2reg :: Builder -> Builder
mem2reg builder =
  let pm = pmod builder
      fxs :: HashMap.Map String VFunction = functions pm
      (pm2, dbg) :: (PModule, [IO()]) = HashMap.fold (
        \fx (newPm, dbgs) -> let (retPm, retDbgs) = mem2reg_function newPm fx
            in (retPm, dbgs ++ retDbgs)) (pm, []) fxs
      in builder{pmod=pm2,debugs=( (debugs builder) ++ dbg ) }

cse :: Builder -> Builder
cse builder =
  let pm = pmod builder
      fxs :: HashMap.Map String VFunction = functions pm
      fxs2 = HashMap.map cse_function fxs
      pm2 = pm{functions=fxs2}
      in builder{pmod=pm2}

<<<<<<< HEAD

cfold :: Builder -> Builder
cfold builder =
  let pm = pmod builder
      fxs :: HashMap.Map String VFunction = functions pm
      fxs2 = HashMap.map cfold_function fxs
      pm2 = pm{functions=fxs2}
      in builder{pmod=pm2}

isConstInt :: ValueRef -> Bool
isConstInt (ConstInt a) = True
isConstInt _ = False

getConstInt :: ValueRef -> Integer
getConstInt (ConstInt a) = a
getConstInt _ = error "not constInt"

isConstBool :: ValueRef -> Bool
isConstBool (ConstBool a) = True
isConstBool _ = False

getConstBool :: ValueRef -> Bool
getConstBool (ConstBool a) = a
getConstBool _ = error "not constBool"


cfold_function :: VFunction -> VFunction
cfold_function func =
  let insts = functionInstructions func
      foldf = \(func, changed) inst ->
        case inst of
          VBinOp name op op1 op2 ->
            if (isConstInt op1) && (isConstInt op2) then
               let x1 = getConstInt op1
                   x2 = getConstInt op2
                   u1 :: Word64 = fromIntegral x1
                   u2 :: Word64 = fromIntegral x2
                   rval = case op of
                      "==" -> ConstBool $ x1 == x2
                      "!=" -> ConstBool $ x1 /= x2
                      "<=" -> ConstBool $ x1 <= x2
                      "<" -> ConstBool $ x1 < x2
                      ">=" -> ConstBool $ x1 >= x2
                      ">" -> ConstBool $ x1 > x2
                      "u<=" -> ConstBool $ u1 <= u2
                      "u<" -> ConstBool $ u1 < u2
                      "u>=" -> ConstBool $ u1 >= u2
                      "u>" -> ConstBool $ u1 > u2
                      "+" -> ConstInt $ x1 + x2
                      "-" -> ConstInt $ x1 - x2
                      "*" -> ConstInt $ x1 * x2
                      "/" -> ConstInt $ x1 `div` x2
                      "%" -> ConstInt $ x1 `mod` x2
                   f1 = replaceAllUses func inst rval
                   f2 = deleteInstruction inst f1
                   in (f2,True)
            else if (isConstBool op1) && (isConstBool op2) then
               let x1 = getConstBool op1
                   x2 = getConstBool op2
                   rval = case op of
                      "==" -> ConstBool $ x1 == x2
                      "!=" -> ConstBool $ x1 /= x2
                      "&" -> ConstBool $ x1 && x2
                      "|" -> ConstBool $ x1 || x2
                   f1 = replaceAllUses func inst rval
                   f2 = deleteInstruction inst f1
                   in (f2,True)
            else (func,changed)
          _ -> (func,changed)
      (nfunc, changed) = foldl foldf (func, False) insts
      in if changed then
         --error (show func)
         cfold_function nfunc
         else func
=======
dce :: Builder -> Builder
dce builder =
    let pm = pmod builder
        fxs :: HashMap.Map String VFunction = functions pm
        fxs2 = HashMap.map dce_function fxs
        pm2 = pm{functions=fxs2}
        in builder{pmod=pm2}
>>>>>>> daea786d8f3b17682ae63f17547beaca06d84c04

cse_function :: VFunction -> VFunction
cse_function func = func
<commit_msg>fix merge issue<commit_after>{-# LANGUAGE ScopedTypeVariables #-}
module OPT where

import Data.List
import Data.Maybe
import qualified Data.Map as HashMap
import qualified Data.Set as Set
import qualified LLIR
import LLIR
import Text.Printf
import Data.Word

-- NEED TO TO MEM2REG AS REQUISITE
mem2reg :: Builder -> Builder
mem2reg builder =
  let pm = pmod builder
      fxs :: HashMap.Map String VFunction = functions pm
      (pm2, dbg) :: (PModule, [IO()]) = HashMap.fold (
        \fx (newPm, dbgs) -> let (retPm, retDbgs) = mem2reg_function newPm fx
            in (retPm, dbgs ++ retDbgs)) (pm, []) fxs
      in builder{pmod=pm2,debugs=( (debugs builder) ++ dbg ) }

cse :: Builder -> Builder
cse builder =
  let pm = pmod builder
      fxs :: HashMap.Map String VFunction = functions pm
      fxs2 = HashMap.map cse_function fxs
      pm2 = pm{functions=fxs2}
      in builder{pmod=pm2}

cfold :: Builder -> Builder
cfold builder =
  let pm = pmod builder
      fxs :: HashMap.Map String VFunction = functions pm
      fxs2 = HashMap.map cfold_function fxs
      pm2 = pm{functions=fxs2}
      in builder{pmod=pm2}

isConstInt :: ValueRef -> Bool
isConstInt (ConstInt a) = True
isConstInt _ = False

getConstInt :: ValueRef -> Integer
getConstInt (ConstInt a) = a
getConstInt _ = error "not constInt"

isConstBool :: ValueRef -> Bool
isConstBool (ConstBool a) = True
isConstBool _ = False

getConstBool :: ValueRef -> Bool
getConstBool (ConstBool a) = a
getConstBool _ = error "not constBool"


cfold_function :: VFunction -> VFunction
cfold_function func =
  let insts = functionInstructions func
      foldf = \(func, changed) inst ->
        case inst of
          VBinOp name op op1 op2 ->
            if (isConstInt op1) && (isConstInt op2) then
               let x1 = getConstInt op1
                   x2 = getConstInt op2
                   u1 :: Word64 = fromIntegral x1
                   u2 :: Word64 = fromIntegral x2
                   rval = case op of
                      "==" -> ConstBool $ x1 == x2
                      "!=" -> ConstBool $ x1 /= x2
                      "<=" -> ConstBool $ x1 <= x2
                      "<" -> ConstBool $ x1 < x2
                      ">=" -> ConstBool $ x1 >= x2
                      ">" -> ConstBool $ x1 > x2
                      "u<=" -> ConstBool $ u1 <= u2
                      "u<" -> ConstBool $ u1 < u2
                      "u>=" -> ConstBool $ u1 >= u2
                      "u>" -> ConstBool $ u1 > u2
                      "+" -> ConstInt $ x1 + x2
                      "-" -> ConstInt $ x1 - x2
                      "*" -> ConstInt $ x1 * x2
                      "/" -> ConstInt $ x1 `div` x2
                      "%" -> ConstInt $ x1 `mod` x2
                   f1 = replaceAllUses func inst rval
                   f2 = deleteInstruction inst f1
                   in (f2,True)
            else if (isConstBool op1) && (isConstBool op2) then
               let x1 = getConstBool op1
                   x2 = getConstBool op2
                   rval = case op of
                      "==" -> ConstBool $ x1 == x2
                      "!=" -> ConstBool $ x1 /= x2
                      "&" -> ConstBool $ x1 && x2
                      "|" -> ConstBool $ x1 || x2
                   f1 = replaceAllUses func inst rval
                   f2 = deleteInstruction inst f1
                   in (f2,True)
            else (func,changed)
          _ -> (func,changed)
      (nfunc, changed) = foldl foldf (func, False) insts
      in if changed then
         --error (show func)
         cfold_function nfunc
         else func

cse_function :: VFunction -> VFunction
cse_function func = func

{'fraction_non_alphanumeric': 0.10300653594771242, 'fraction_numerical': 0.024967320261437907, 'mean_word_length': 1.5909244835760243, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 91, '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': '1585019', 'n_tokens_mistral': 2382, 'n_tokens_neox': 2287, 'n_words': 701}