<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/72152>72152</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Ineffisiscient double null/arhument matrialization.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
deadalnix
</td>
</tr>
</table>
<pre>
Consider the two following equivalent (as far as I can tell) IR:
```llvm
define ptr @foo(ptr readonly %arg.o) {
entry:
%0 = load ptr, ptr %arg.o, align 8
%1 = load i64, ptr %0, align 8
%cmp1 = icmp eq i64 %1, 0
br i1 %cmp1, label %exit, label %continue
continue:
%2 = getelementptr inbounds ptr, ptr %0, i64 1
%3 = load ptr, ptr %2, align 8
%4 = load ptr, ptr %3, align 8
%cmp2 = icmp eq ptr %4, null
%o = select i1 %cmp2, ptr %arg.o, ptr null
br label %exit
exit:
%ret = phi ptr [ null, %entry ], [ %o, %continue ]
ret ptr %ret
}
```
and
```llvm
define ptr @bar(ptr %arg.o) {
entry:
%0 = load ptr, ptr %arg.o, align 8
%1 = load i64, ptr %0, align 8
%cmp1 = icmp eq i64 %1, 0
br i1 %cmp1, label %fail, label %continue
continue:
%2 = getelementptr inbounds ptr, ptr %0, i64 1
%3 = load ptr, ptr %2, align 8
%4 = load ptr, ptr %3, align 8
%cmp2 = icmp eq ptr %4, null
br i1 %cmp2, label %exit, label %fail
fail:
br label %exit
exit:
%ret = phi ptr [ null, %fail ], [ %arg.o, %continue ]
ret ptr %ret
}
```
Nether are transformed into the other. From a canonicalization perspective, it's a bit of a miss. But more generally, the first one includes a phi of a select, which seems a bit redundant and leads to worse code generation. This example is a cleaned up version of a down cast in a language implementation, something that can potentially be run numerous times and is typically followed by an branch on the result. the select/phi combination prevent subsequent branches from being removed effectively, while the phi only implementation is able to simplify the control flow after inlining.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJz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