<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/80053>80053</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [WebAssembly] FastISel's lowering of comparisons is very suboptiomal
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          SingleAccretion
      </td>
    </tr>
</table>

<pre>
    See this godbolt: [link](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:llvm,selection:(endColumn:31,endLineNumber:3,positionColumn:31,positionLineNumber:3,selectionStartColumn:31,selectionStartLineNumber:3,startColumn:31,startLineNumber:3),source:'define+i1+@suboptimal_compare(ptr)+%230+%7B%0ABB01:%0A++++%25eq+%3D+icmp+eq+ptr+%250,+null%0A++++br+i1+%25eq,+label+%25BB02,+label+%25BB03%0ABB02:%0A++++ret+i1+2%0ABB03:%0A++++ret+i1+1%0A%7D%0A'),l:'5',n:'0',o:'LLVM+IR+source+%231',t:'0')),k:31.73908452413444,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:irclangtrunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'1',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:2,lang:llvm,libs:!(),options:'-target+wasm32+-mllvm+--print-after-all',overrides:!(),selection:(endColumn:21,endLineNumber:32,positionColumn:1,positionLineNumber:1,selectionStartColumn:21,selectionStartLineNumber:32,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+clang+(trunk)+(Editor+%231)',t:'0')),k:34.92758214253224,l:'4',m:100,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'clang+(trunk)',editorid:1,fontScale:14,fontUsePx:'0',j:2,wrap:'1'),l:'5',n:'0',o:'Output+of+clang+(trunk)+(Compiler+%232)',t:'0')),k:33.33333333333333,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4).
```llvm
define i1 @suboptimal_compare(ptr) #0 {
BB01:
    %eq = icmp eq ptr %0, null
    br i1 %eq, label %BB02, label %BB03
BB02:
    ret i1 2
BB03:
    ret i1 1
}
```
```wasm
suboptimal_compare: # @suboptimal_compare
        i32.const       0
 local.set       1
        local.get       0
        local.set 2
        local.get       1
        local.set       3
 local.get       2
        local.get       3
        i32.eq  
 local.set       4
        i32.const       1
        local.set 5
        local.get       4
        local.get       5
 i32.and 
        local.set       6
        block           
 local.get       6
        i32.eqz
        br_if           0 # 0: down to label0
        i32.const       0
 local.set       7
        local.get       7
        return
.LBB0_2: # %BB03
        end_block # label0:
        i32.const       1
        local.set 8
        local.get       8
        return
 end_function
```
Compare:
```llvm
define i1 @suboptimal_compare(i32) #0 {
BB01:
    %eq = icmp eq i32 %0, 0
 br i1 %eq, label %BB02, label %BB03
BB02:
    ret i1 2
BB03:
 ret i1 1
}
```
```wasm
suboptimal_compare:                     # @suboptimal_compare
        block           
        local.get 0
        br_if           0                               # 0: down to label0
        i32.const       0
        local.set       1
 local.get       1
        return
.LBB0_2: # %BB03
        end_block                               # label0:
 i32.const       1
        local.set       2
        local.get 2
        return
        end_function
```
To work around this issue, we had to do the following with null checks (which are very common in our case):
```llvm
    %p = ptrtoint ptr %0 to i32
    %eq = icmp eq int32 %p, 0
    br i1 %eq, label %BB02, label %BB03
```
Which yielded a 5% (!) debug code size improvement.

This issue exists for all types of `icmp`, where an `AND, 1` is (seemingly unnecessarily) inserted after the actual comparison.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0WF-P2yoW_zTkBU2EsR07D3mIkx3pSt12td2997HC9knCHQwu4ElzP_0KcDJx4nja3q41asP5x-Fwfj-wmTF8LwFWKC1Qup2x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