<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/146742>146742</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization of strdup to malloc + memcpy if argument is known at compile time
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
BreadTom
</td>
</tr>
</table>
<pre>
[Clang godbolt](https://godbolt.org/z/5GM85jT3G)
[GCC godbolt](https://godbolt.org/z/afqarMj9n)
Clang 20.1 and GCC 15.1 missed optimizing strdup("Hello") to malloc then memcpy "Hello" like f0_fast().
Maybe useful if the string came from -D:
```
#define HELLO_WORLD "Hello world"
char *f0_slow (void)
{
return __builtin_strdup (HELLO_WORLD);
}
```
C source code
```
char *f0_fast (void)
{
char *ret_val = __builtin_malloc (strlen ("Hello") + 1);
if (ret_val)
__builtin_memcpy (ret_val, "Hello", strlen ("Hello") + 1);
return ret_val;
}
char *f0_slow (void)
{
return __builtin_strdup ("Hello");
}
```
LLVM IR
```
@.str = private unnamed_addr constant [6 x i8] c"Hello\00", align 1
define dso_local noalias noundef ptr @f0_fast() local_unnamed_addr {
entry:
%call = tail call dereferenceable_or_null(6) ptr @malloc(i64 noundef 6) #5
%tobool.not = icmp eq ptr %call, null
br i1 %tobool.not, label %if.end, label %if.then
if.then:
tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(6) %call, ptr noundef nonnull align 1 dereferenceable(6) @.str, i64 6, i1 false)
br label %if.end
if.end:
ret ptr %call
}
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2
define dso_local noalias noundef ptr @f0_slow() local_unnamed_addr {
entry:
%call = tail call dereferenceable_or_null(6) ptr @strdup(ptr noundef nonnull @.str) #5
ret ptr %call
}
declare noalias ptr @strdup(ptr nocapture noundef readonly) local_unnamed_addr #4
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy8Vk1v2zgQ_TX0ZVBBoiRbPujgj3W6QIICRbF7FChxZDOlSJWkkk1__YL6sGU3KZI9bGAgksiZ9-bN4wezVhwVYk7SLUn3C9a5kzb51iDj33SzKDV_8WM7ydQRjpqXWjqS7gnNTs61lsQbQg-EHsahQJsjoYefhB7Su4csffwW3xG6JuGGpNu73e5jKVj9g5mHx7UaU4SbgQcNgwiY4uAzRmkQQSOsRQ66daIRP4U6gnWGdy2hGaH0M0qpCaWErsFpaJiUugJ3QgUNNlX7ArNJIMV3hDosamZdH78OSLh5YC8lQmex7iSI2kd7DA9VsQahNrqBT3tfTbghy3D8hRtCY461UAif_7i__1L8_eXr_f4MCM_aSO659fVVJ2aA0E0dFlbqZyA0e9KCj_WvtiTcAAAYdJ1RUBRlJ6QTqhiq9dNnID4q3vaB-1tS4WYHVnemQqg0x5vhGQ2vwls0pmkGXfHEJJB4P-M0ykxoZp2RqODXZhC6hehM08tKaDZmG8Bg_JulnTp2mbiDq7w7eC_gRcop11yv97XjN724Bv9NL-7v_3qAP7_eDiRhYJ3pZW2NeGIOoVOKNcgLxrmBSivrmHJA0u0S_gGRkXQP1Rk23YXhqAiT4qggGuBGP3KrC6krJkFpJgWzoHSnONbQetQkvFoE0E8trggMEqBy5mUwPgChacXk4AXHhIT-jaPBGg2qClkpsdCmUJ2UhGZLn3rEGxxDaCaWyZnLcuhbnE7pnS61loHSrgcRVdMC_hhyDOC-4D69jygNiOg6zo9LVqL0n0UdoOK3n_zmMIg1vYz1XWryTvCspXxqgsGVQRv6n1gmhGae0FSE0soTmtpwq8ekw4z_f4ke_OKjvYDL_iGCmkmL02oqzS-VT1X656FIg-5KztmK4FhJZvAtx7zWwbe8Q-PoOuf7BJ1wK9a6ziA8G-FQK_lyUe12ij_Nphl9ql4X0TTMHzW9u-iHV4bfEP63lXE-zV6zxbnv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