Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
GHC
GHC
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 4,322
    • Issues 4,322
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
    • Iterations
  • Merge Requests 368
    • Merge Requests 368
  • Requirements
    • Requirements
    • List
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
  • Security & Compliance
    • Security & Compliance
    • Dependency List
    • License Compliance
  • Operations
    • Operations
    • Incidents
    • Environments
  • Analytics
    • Analytics
    • CI / CD
    • Code Review
    • Insights
    • Issue
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Glasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #12984

Closed
Open
Opened Dec 15, 2016 by Sylvain Henry@hsyl20Developer

Missed constant folding oportunities (associativity)

Consider this simple example:

import System.Environment

main :: IO ()
main = do
   args <- getArgs
   print ((length args + 10) - 10)

Compiled with -O2, we get the following Core:

case GHC.Show.$wshowSignedInt
              0#
              (GHC.Prim.-# (GHC.Prim.+# ww2_a6Pd 10#) 10#)
              (GHC.Types.[] @ Char)
       of

I would expect GHC to perform constant folding there to remove the unnecessary operation.

Basically, for any expression composed of Int#/Word# +/-/* primops, I think we should use associativity and distributivity laws to push the constants in the outer operation. It would allow the "scrutinee constant folding" optimization to be applied more often too.

I have a patch that does this for case scrutinees, but where should I put it in GHC so that the optimization gets applied more generally?

Trac metadata
Trac field Value
Version 8.0.1
Type FeatureRequest
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
Assignee
Assign to
None
Milestone
None
Assign milestone
Time tracking
None
Due date
None
Reference: ghc/ghc#12984