[cfe-dev] About NRVO (named return value optimization)

Jiangning Liu liujiangning1 at gmail.com
Tue Jun 24 22:57:30 PDT 2014


Hi,

For the following small test case,

// RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm -O1 -o - %s |
FileCheck %s

// Test code generation for the named return value optimization.
class X {
public:
  X();
};

void f(const X& x);
void test10(bool b) {
  f(X());
  f(X());
}

we are generating the following LLVM IR with "

%class.X = type { i8 }

; Function Attrs: nounwind
define void @_Z6test10b(i1 zeroext %b) #0 {
entry:
  %ref.tmp = alloca %class.X, align 1
  %ref.tmp1 = alloca %class.X, align 1
  call void @_ZN1XC1Ev(%class.X* %ref.tmp) #2
  call void @_Z1fRK1X(%class.X* nonnull %ref.tmp) #2
  call void @_ZN1XC1Ev(%class.X* %ref.tmp1) #2
  call void @_Z1fRK1X(%class.X* nonnull %ref.tmp1) #2
  ret void
}

declare void @_Z1fRK1X(%class.X* nonnull) #1
declare void @_ZN1XC1Ev(%class.X*) #1

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,

define void @_Z6test10b(i1 zeroext %b) #0 {
entry:
  %ref.tmp = alloca %class.X, align 1
  call void @_ZN1XC1Ev(%class.X* %ref.tmp) #2
  call void @_Z1fRK1X(%class.X* nonnull %ref.tmp) #2
  call void @_ZN1XC1Ev(%class.X* %ref.tmp) #2
  call void @_Z1fRK1X(%class.X* nonnull %ref.tmp) #2
  ret void
}

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.

Any idea?

Thanks,
-Jiangning
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140625/b7fe07a6/attachment.html>


More information about the cfe-dev mailing list