<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/65741>65741</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Clang doesn't emit `@llvm.is.constant.i32` for function calls in `__builtin_constant_p`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang,
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Eisenwave
</td>
</tr>
</table>
<pre>
```cpp
int foo() { return 37; }
int bar() {
return __builtin_constant_p(foo());
}
```
The following IR is emitted for `bar` (https://godbolt.org/z/eWW1a9P4v):
```llvm
define dso_local noundef i32 @bar()() {
entry:
ret i32 0
}
```
I would expect something along the lines of
```llvm
define dso_local noundef i32 @zap(int)(i32 noundef %z) local_unnamed_addr {
entry:
%call = call noundef i32 @foo()() ; only emitted if foo() is has no side effects
%0 = tail call i1 @llvm.is.constant.i32(i32 %call)
%1 = zext i1 %0 to i32
ret i32 %1
}
```
To me, this looks like a missed optimization. `foo()` can easily be constant folded and the result of `foo()` should be known. If `@llvm.is.constant` was emitted, I'm sure that it would result in `1` as well. How come Clang doesn't emit it at all?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVE1v6zYQ_DXUZfEEirK-DjokcY3mVhQF3tGgxJXFhiINcRW_-NcXpPwRuGkOBQTYInZnZ2ZHlN7rg0VsWfHMim0iFxrd3P6mPdqTfMekc-qjZSVfn_54ZHzL-JO2BINzTNRMNMCqZ5iRltlCXrH8GVi1Xevu1Z2c79XrMQBc2_b7btGGtN33znqSlvZHJurbiPDkl7Y7-JXW-vrXiDA4Y9xJ2wO8_gnaA06aCBUMbgZW8kCi5MBEPRIdPcufmNgxsTs41TlDqZsPTOzOTOzw589MNn9s3uPkp4eBxrxP65HCQVsE5d3euF4asG6xCgfQuQC24TfZj-LR0vxxQ45GxB7-vchXOLnFKMBfR-wJvJuQxqBXGmcPQCOC0RY9uOF_cz7L4L22tNIOp9cKJopzkBH79ou1ckK1l0rN_62MiaKXxgDLtxD_PIz7tOTVovwZnDUft-Xp4VPWtIdRerAOvFYIOAzYk_80i8dBJLVZp-ksDAnqU-3Ta7xSnYuLtgu_QOCOkkWUM_6iCBBgyQXGj_sKtd-v7JJOBxMy8QI0ag_GuTcPRr8hSJi096jAHUlP-ixJO5uGtN6NKTn00gJKr80HdAhXGSHwChVIq-LuZ_SLIXDDv_r9GHPTIbxZd7IpvMaaL6wJ1Sd5-3YC51cmqgn8MiPQKAk0XWJ4madtwMpCp_RwQmNS-N2doHcTwouR9gDKobdMVBRxA4IkCLbnu0S1uWryRibYZmWzySpRbIpkbFWN1UYVssp5X4qykGWV9VJ0nczruqpVolvBRc4bXmeZaLhI-6FQKs-aqmxEr-oN23CcpDZpFOnmQ6K9X7Ati2qTJUZ2aHy8-4ToA00mBBMvTAiLJ4il4aTYJnMbEH50y8EHy7Qnf8ckTQbbL3V-bXFMX8njtTQstg8rj2n1Fyu_vA9LniyzaR-uLk3j0qW9m5jYxW98_flxnN3f2BMTu6jDM7GLqv8JAAD__xaHx-Y">