<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Apr 9, 2015 at 2:48 PM, Benjamin Kramer <span dir="ltr"><<a href="mailto:benny.kra@gmail.com" target="_blank">benny.kra@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><br>
> On 09.04.2015, at 23:05, Richard Smith <<a href="mailto:richard@metafoo.co.uk">richard@metafoo.co.uk</a>> wrote:<br>
><br>
> On Thu, Apr 9, 2015 at 9:09 AM, Benjamin Kramer <<a href="mailto:benny.kra@googlemail.com">benny.kra@googlemail.com</a>> wrote:<br>
> Author: d0k<br>
> Date: Thu Apr  9 11:09:29 2015<br>
> New Revision: 234499<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=234499&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=234499&view=rev</a><br>
> Log:<br>
> [CodeGen] When promoting a reference temporary to a global use the inner type to fold it.<br>
><br>
> The MaterializeTemporaryExpr can have a different type than the inner<br>
> expression, miscompiling the constant. PR23165.<br>
><br>
> Modified:<br>
>     cfe/trunk/lib/CodeGen/CGExpr.cpp<br>
>     cfe/trunk/test/CodeGenCXX/cxx0x-initializer-references.cpp<br>
><br>
> Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=234499&r1=234498&r2=234499&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=234499&r1=234498&r2=234499&view=diff</a><br>
> ==============================================================================<br>
> --- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)<br>
> +++ cfe/trunk/lib/CodeGen/CGExpr.cpp Thu Apr  9 11:09:29 2015<br>
> @@ -309,12 +309,13 @@ createReferenceTemporary(CodeGenFunction<br>
>          (M->getType()->isArrayType() || M->getType()->isRecordType()) &&<br>
>          CGF.CGM.isTypeConstant(M->getType(), true))<br>
><br>
> This reference to M->getType() should be changed to Inner->getType() too. Consider a case like:<br>
><br>
>   struct S { struct T { int a; } t; mutable int b; };<br>
>   void f() { const S::T &r = S().t; }<br>
><br>
> Here, we shouldn't emit the temporary as a global constant due to the mutable member.<br>
><br>
> The same is probably true of the array type / record type check -- if the reference temporary itself is of array or reference type, and we're binding a reference to a non-array/non-reference subobject, we should probably still promote to a global.<br>
<br>
</div></div>You're right. Changing this is tricky though without destroying the optimization. If I remove the mutable from your test case the AST looks like this:<br>
<br>
        `-MaterializeTemporaryExpr 0x7faab985f560 <col:30, col:34> 'const struct S::T':'const struct S::T' lvalue extended by Var 0x7faab985eda0 'r' 'const struct S::T &'<br>
          `-ImplicitCastExpr 0x7faab985f548 <col:30, col:34> 'const struct S::T':'const struct S::T' <NoOp><br>
            `-MemberExpr 0x7faab985f248 <col:30, col:34> 'struct T':'struct S::T' .t 0x7faab985eaa0<br>
              `-CXXTemporaryObjectExpr 0x7faab985f108 <col:30, col:32> 'struct S' 'void (void)' zeroing<br>
<br>
Note the lack of const on the CXXTemporaryObjectExpr :(<br></blockquote><div><br></div><div>Hmm, right, the optimization is not correct with or without the 'mutable' because the temporary is not actually constant; the 'const' on M can be removed by a const_cast without UB. "Destroying the optimization" is the right thing to do here. We can still optimize in the case where Inner has a constant type.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
- Ben<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
><br>
>        if (llvm::Constant *Init =<br>
> -              CGF.CGM.EmitConstantExpr(Inner, M->getType(), &CGF)) {<br>
> +              CGF.CGM.EmitConstantExpr(Inner, Inner->getType(), &CGF)) {<br>
>          auto *GV = new llvm::GlobalVariable(<br>
>              CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true,<br>
>              llvm::GlobalValue::PrivateLinkage, Init, ".ref.tmp");<br>
> -        GV->setAlignment(<br>
> -            CGF.getContext().getTypeAlignInChars(M->getType()).getQuantity());<br>
> +        GV->setAlignment(CGF.getContext()<br>
> +                             .getTypeAlignInChars(Inner->getType())<br>
> +                             .getQuantity());<br>
>          // FIXME: Should we put the new global into a COMDAT?<br>
>          return GV;<br>
>        }<br>
><br>
> Modified: cfe/trunk/test/CodeGenCXX/cxx0x-initializer-references.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/cxx0x-initializer-references.cpp?rev=234499&r1=234498&r2=234499&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/cxx0x-initializer-references.cpp?rev=234499&r1=234498&r2=234499&view=diff</a><br>
> ==============================================================================<br>
> --- cfe/trunk/test/CodeGenCXX/cxx0x-initializer-references.cpp (original)<br>
> +++ cfe/trunk/test/CodeGenCXX/cxx0x-initializer-references.cpp Thu Apr  9 11:09:29 2015<br>
> @@ -1,5 +1,7 @@<br>
>  // RUN: %clang_cc1 -std=c++11 -S -triple armv7-none-eabi -emit-llvm -o - %s | FileCheck %s<br>
><br>
> +// CHECK: private constant { i8** } { i8** getelementptr inbounds ([3 x i8*], [3 x i8*]* @_ZTVN7PR2316510ChildClassE, i64 0, i64 2) }, align 4<br>
> +<br>
>  namespace reference {<br>
>    struct A {<br>
>      int i1, i2;<br>
> @@ -79,3 +81,22 @@ namespace reference {<br>
>    }<br>
><br>
>  }<br>
> +<br>
> +namespace PR23165 {<br>
> +struct AbstractClass {<br>
> +  virtual void foo() const = 0;<br>
> +};<br>
> +<br>
> +struct ChildClass : public AbstractClass {<br>
> +  virtual void foo() const {}<br>
> +};<br>
> +<br>
> +void helper(const AbstractClass &param) {<br>
> +  param.foo();<br>
> +}<br>
> +<br>
> +void foo() {<br>
> +// CHECK: call void @_ZN7PR231656helperERKNS_13AbstractClassE(%{{.*}} bitcast ({ i8** }* @{{.*}} to %{{.*}}*))<br>
> +  helper(ChildClass());<br>
> +}<br>
> +}<br>
><br>
><br>
> _______________________________________________<br>
> cfe-commits mailing list<br>
> <a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
><br>
<br>
</div></div></blockquote></div><br></div></div>