Skip to content
GitLab
Projects Groups Topics Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Register
  • Sign in
  • GHC GHC
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributor statistics
    • Graph
    • Compare revisions
    • Locked files
  • Issues 5.5k
    • Issues 5.5k
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 633
    • Merge requests 633
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Artifacts
    • Schedules
    • Test cases
  • Deployments
    • Deployments
    • Releases
  • Analytics
    • Analytics
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Glasgow Haskell CompilerGlasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #22152
Closed
Open
Issue created Sep 08, 2022 by Teo Camarasu@teoDeveloper

Constant folding for division operations (`quotRem` and `divMod`)

Motivation

I recently noticed that with GHC-9.2 and GHC-9.4 x `quot` 60 `quot` 60 doesn't get constant folded to x `quot` 3600.

I realised this as I wanted to write a module like the following and expected the function toHours to be optimised to just a single division, but we get two.

{-# OPTIONS_GHC -O2 -ddump-simpl -ddump-to-file -ddump-stg-final #-}
module Time where

{-# INLINE toHoursMinutesSeconds #-}
toHoursMinutesSeconds :: Int -> (Int, Int, Int)
toHoursMinutesSeconds t = (h, m', s)
  where
    (h, m') = m `quotRem` 60
    (m, s) = toMinutesSeconds t

toMinutesSeconds :: Int -> (Int, Int)
toMinutesSeconds t = t `quotRem` 60

toHours t = h
  where
    (h, _, _) = toHoursMinutesSeconds t

Proposal

Add constant folding rules for quotRem and divMod for repeated division where the remainder is not used, ie, x `quot` y `quot` z = x `quot` (y * z) and that multiplication is done at compile time.

Edited Sep 08, 2022 by Teo Camarasu
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking