Optimize tagToEnum# at Core level
Old comparison primops that returned Bool were implemented by inserting call to `tagToEnum#` in the code generator. This call to `tagToEnum#` was later optimized away by a special case in the code geenrator (see \[\[GhcFile(compiler/codeGen/StgCmmExpr.hs)\]\], Note \[case on bool\])). Now that we have new comparison primops (see #6135) we no longer insert `tagToEnum#` in the code generator - all uses of `tagToEnum#` come from explicit calls in a source program. But we still have that special case in the code generator to optimize away `tagToEnum#`. We should drop that special case in the code generator and handle scrutinizing of `tagToEnum#` at the Core level by:
1. removing call to `tagToEnum#` in the scrutinee
1. adding calls to `dataToTag#` in case branches
1. constant-folding inserted `dataToTag#`
Here is an example. This Haskell code:
```
if tagToEnum# (a ># b)
then E1
else E2
```
will be translated to this Core:
```
case tagToEnum# (a ># b) of
True -> E1
False -> E2
```
and optimized like this:
```
case a ># b of
dataToTag# True -> E1
dataToTag# False -> E2
```
#### \>
```
case a ># b of
1 -> E1
0 -> E2
```
<details><summary>Trac metadata</summary>
| Trac field | Value |
| ---------------------- | ------------ |
| Version | 7.7 |
| Type | Task |
| TypeOfFailure | OtherFailure |
| Priority | normal |
| Resolution | Unresolved |
| Component | Compiler |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | |
| Operating system | |
| Architecture | |
</details>
<!-- {"blocked_by":[],"summary":"Optimize tagToEnum# at Core level","status":"New","operating_system":"","component":"Compiler","related":[],"milestone":"","resolution":"Unresolved","owner":{"tag":"Unowned"},"version":"7.7","keywords":[],"differentials":[],"test_case":"","architecture":"","cc":[""],"type":"Task","description":"Old comparison primops that returned Bool were implemented by inserting call to `tagToEnum#` in the code generator. This call to `tagToEnum#` was later optimized away by a special case in the code geenrator (see [[GhcFile(compiler/codeGen/StgCmmExpr.hs)]], Note [case on bool])). Now that we have new comparison primops (see #6135) we no longer insert `tagToEnum#` in the code generator - all uses of `tagToEnum#` come from explicit calls in a source program. But we still have that special case in the code generator to optimize away `tagToEnum#`. We should drop that special case in the code generator and handle scrutinizing of `tagToEnum#` at the Core level by:\r\n\r\n 1. removing call to `tagToEnum#` in the scrutinee\r\n 2. adding calls to `dataToTag#` in case branches\r\n 3. constant-folding inserted `dataToTag#`\r\n\r\nHere is an example. This Haskell code:\r\n{{{\r\nif tagToEnum# (a ># b)\r\nthen E1\r\nelse E2\r\n}}}\r\nwill be translated to this Core:\r\n{{{\r\ncase tagToEnum# (a ># b) of\r\n True -> E1\r\n False -> E2\r\n}}}\r\nand optimized like this:\r\n{{{\r\ncase a ># b of\r\n dataToTag# True -> E1\r\n dataToTag# False -> E2\r\n}}}\r\n====>\r\n{{{\r\ncase a ># b of\r\n 1 -> E1\r\n 0 -> E2\r\n}}}","type_of_failure":"OtherFailure","blocking":[]} -->
issue