Take more advantage of STG representation invariance (follows up #9291)
case alternatives at the STG stage (i.e. untyped) :
case scrut of False ->  Just x -> Right x  -> Nothing
The common theme of all these is that the scrutinee's memory layout and the result's memory layout coincide. So operationally no allocation needs to take place, and the whole
case expression is simply a (strict) identity at the STG stage.
I propose to add a small STG analysis to:
- for each
casealternative check whether the assigned tag between scrutinee and result matches, and if so
- check whether both have the underlying memory layout and contents.
If these conditions are met, the case alternative can be replaced with the identity. When all alternatives simplify to the identity (also considering the DEFAULT alternative), then the entire
case expression reduces to a single identity DEFAULT alternative (i.e. all other alternatives in the
case can be dropped).
Many of the code examples in the join points paper (https://www.microsoft.com/en-us/research/wp-content/uploads/2016/11/join-points-pldi17.pdf) exhibit these optimisation opportunities.
The already implemented suggestion in #9291 (closed) comes with the restriction that it only operates in the scope of the same type (see last comment there), but STG is untyped, so why not take advantage of this fact?