<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/65041>65041</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
unnecessary `mov` added in string concatination code with >= `-O2`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
firewave
</td>
</tr>
</table>
<pre>
```cpp
#include <string>
std::string cb();
std::string f1()
{
std::string s;
s += cb();
s += ' ';
return s;
}
```
Compiling this >= `-O2` introduces an additional `mov` within a label. There are bigger differences in the IR but it mainly seems to be a moved block.
```diff
.LBB0_7:
+ mov rcx, qword ptr [rbx]
mov r14, qword ptr [rbx + 8]
lea r12, [r14 + 1]
mov eax, 15
- cmp qword ptr [rbx], r15
+ cmp rcx, r15
je .LBB0_9
mov rax, qword ptr [rbx + 16]
```
```diff
@@ -55,51 +55,55 @@
%8 = load i64, ptr %0, align 8
%cond.i.i.i = select i1 %cmp.i.i.i.i14, i64 15, i64 %8
%cmp.i.i = icmp ugt i64 %add.i.i, %cond.i.i.i
- br i1 %cmp.i.i, label %if.then.i.i15, label %_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEpLEc.exit
+ br i1 %cmp.i.i, label %if.then.i.i15, label %nrvo.skipdtor
if.then.i.i15:
invoke void @std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_mutate(unsigned long, unsigned long, char const*, unsigned long)(ptr noundef nonnull align 8 dereferenceable(32) %agg.result, i64 noundef %6, i64 noundef 0, ptr noundef null, i64 noundef 1)
- to label %_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEpLEc.exit unwind label %lpad3
+ to label %.noexc16 unwind label %lpad3
-_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEpLEc.exit:
- %9 = load ptr, ptr %agg.result, align 8
- %arrayidx.i.i = getelementptr inbounds i8, ptr %9, i64 %6
- store i8 32, ptr %arrayidx.i.i, align 1
- store i64 %add.i.i, ptr %_M_string_length.i.i.i, align 8
- %10 = load ptr, ptr %agg.result, align 8
- %arrayidx.i.i.i = getelementptr inbounds i8, ptr %10, i64 %add.i.i
- store i8 0, ptr %arrayidx.i.i.i, align 1
- ret void
+.noexc16:
+ %.pre.i.i = load ptr, ptr %agg.result, align 8
+ br label %nrvo.skipdtor
lpad:
- %11 = landingpad { ptr, i32 }
+ %9 = landingpad { ptr, i32 }
cleanup
br label %ehcleanup
lpad1:
- %12 = landingpad { ptr, i32 }
+ %10 = landingpad { ptr, i32 }
cleanup
- %13 = load ptr, ptr %ref.tmp, align 8
- %14 = getelementptr inbounds %"class.std::__cxx11::basic_string", ptr %ref.tmp, i64 0, i32 2
- %cmp.i.i.i17 = icmp eq ptr %13, %14
+ %11 = load ptr, ptr %ref.tmp, align 8
+ %12 = getelementptr inbounds %"class.std::__cxx11::basic_string", ptr %ref.tmp, i64 0, i32 2
+ %cmp.i.i.i17 = icmp eq ptr %11, %12
br i1 %cmp.i.i.i17, label %ehcleanup, label %if.then.i.i18
if.then.i.i18:
- call void @operator delete(void*)(ptr noundef %13) #7
+ call void @operator delete(void*)(ptr noundef %11) #7
br label %ehcleanup
ehcleanup:
- %.pn = phi { ptr, i32 } [ %11, %lpad ], [ %12, %lpad1 ], [ %12, %if.then.i.i18 ]
+ %.pn = phi { ptr, i32 } [ %9, %lpad ], [ %10, %lpad1 ], [ %10, %if.then.i.i18 ]
call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %ref.tmp) #5
br label %ehcleanup6
lpad3:
- %15 = landingpad { ptr, i32 }
+ %13 = landingpad { ptr, i32 }
cleanup
br label %ehcleanup6
+nrvo.skipdtor:
+ %14 = phi ptr [ %.pre.i.i, %.noexc16 ], [ %7, %_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED2Ev.exit ]
+ %arrayidx.i.i = getelementptr inbounds i8, ptr %14, i64 %6
+ store i8 32, ptr %arrayidx.i.i, align 1
+ store i64 %add.i.i, ptr %_M_string_length.i.i.i, align 8
+ %15 = load ptr, ptr %agg.result, align 8
+ %arrayidx.i.i.i = getelementptr inbounds i8, ptr %15, i64 %add.i.i
+ store i8 0, ptr %arrayidx.i.i.i, align 1
+ ret void
+
ehcleanup6:
- %.pn9 = phi { ptr, i32 } [ %15, %lpad3 ], [ %.pn, %ehcleanup ]
+ %.pn9 = phi { ptr, i32 } [ %13, %lpad3 ], [ %.pn, %ehcleanup ]
%16 = load ptr, ptr %agg.result, align 8
%cmp.i.i.i20 = icmp eq ptr %16, %0
br i1 %cmp.i.i.i20, label %_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED2Ev.exit22, label %if.then.i.i21
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEWFmPozoW_jXOy1EQNmHJQx46tUgt3TsjTc_TvEQGnyS-7RiuMemqfz-yWQLZJlXV0lQvUJx98XcO8LqWO424IvGaxM8z3th9aVZbafAXP-IsL8X7iiRh-7eoKhI-k_AbYZHUhWoEAomeamuk3pHopSP6_2srSPSNRN9aKhQ5YRlhSxKt77FtacfW8qQdM5zz1YMeAIAaCFuT6PnSyoRKWOr-TYgGbWP0SB9Jn7ubPuyxv0_loZLKeWD3sgYXtFOchPN_MpKEILU1pWgKrIFr4EJIK0vNlWM5lEfH8kvavdTAQfEcVQD_3qNB4AYhl7sdGhByu0WD2imRGuwe4fu_IG8sSAsHLrV6hxrxUIMtIUfgcCiPKCBXZfEzGHs7hOBUdjEHf6zX4SZ12exquYbu51Ae_dUUb4Q9wd-_SiOgsgZIvDb5G4mfT3mbCtDFNQGXd8guxRRyMJQ5EcdIF56R3taP3DtE45Y-7-nFofLXa56yJzC9wCjGXqSLcWAZbP6F7bXNU9eIp0j51dS0ASRDBFd750o1yCIkixDmcUzYU0ydmvY2hpY0OEdYnIHrNVVyATLxCfcOsDh091zJnYZsLFCUWgTS_fGSNSosLEjqaYeqJQWyLZ5MFi7B3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