<div dir="ltr">Hi,<div><br></div><div>For the following small test case,</div><div><br></div><div><div>// RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm -O1 -o - %s | FileCheck %s</div><div><br></div><div>// Test code generation for the named return value optimization.<br>
</div></div><div>class X {<br></div><div><div>public:</div><div> X();</div><div>};</div><div><br></div><div>void f(const X& x);</div><div>void test10(bool b) {</div><div> f(X());</div><div> f(X());</div><div>}</div>
</div><div><br></div><div>we are generating the following LLVM IR with "</div><div><br></div><div><div>%class.X = type { i8 }</div><div><br></div><div>; Function Attrs: nounwind</div><div>define void @_Z6test10b(i1 zeroext %b) #0 {</div>
<div>entry:</div><div> %ref.tmp = alloca %class.X, align 1</div><div> %ref.tmp1 = alloca %class.X, align 1</div><div> call void @_ZN1XC1Ev(%class.X* %ref.tmp) #2</div><div> call void @_Z1fRK1X(%class.X* nonnull %ref.tmp) #2</div>
<div> call void @_ZN1XC1Ev(%class.X* %ref.tmp1) #2</div><div> call void @_Z1fRK1X(%class.X* nonnull %ref.tmp1) #2</div><div> ret void</div><div>}</div><div><br></div><div>declare void @_Z1fRK1X(%class.X* nonnull) #1</div>
<div>declare void @_ZN1XC1Ev(%class.X*) #1<br></div></div><div><br></div><div>So my questions is should NRVO be able to know ref.tmp and ref.tmp1 can be merged to be a single one? That is, I'm expecting the following LLVM IR code to be generated,</div>
<div><br></div><div><div>define void @_Z6test10b(i1 zeroext %b) #0 {</div><div>entry:</div><div> %ref.tmp = alloca %class.X, align 1</div><div> call void @_ZN1XC1Ev(%class.X* %ref.tmp) #2<br></div><div> call void @_Z1fRK1X(%class.X* nonnull %ref.tmp) #2</div>
<div> call void @_ZN1XC1Ev(%class.X* %ref.tmp) #2</div><div> call void @_Z1fRK1X(%class.X* nonnull %ref.tmp) #2</div><div> ret void</div><div>}</div></div><div><br></div><div>If we leave both ref.tmp and ref.tmp1 to LLVM IR, it seems to be hard for middle-end to combine them unless we demangle the function name _ZN1XC1Ev to know it is a C++ constructor and do more alias analysis.</div>
<div><br></div><div>Any idea?</div><div><br></div><div>Thanks,</div><div>-Jiangning</div></div>