<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Jul 31, 2014 at 12:36 PM, Jordan Rose <span dir="ltr"><<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div class=""><br><div><div>On Jul 31, 2014, at 1:20 , Manuel Klimek <<a href="mailto:klimek@google.com" target="_blank">klimek@google.com</a>> wrote:</div>
<br><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Jul 31, 2014 at 5:19 AM, Jordan Rose <span dir="ltr"><<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><br>
<div><div>On Jul 30, 2014, at 8:14 , Manuel Klimek <<a href="mailto:klimek@google.com" target="_blank">klimek@google.com</a>> wrote:</div><br><blockquote type="cite"><div dir="ltr">Hi Jordan,<div><br></div><div>I made some progress on inlining temporary destructors. My main problem currently is that somehow the initializer values in my constructor get lost if I don't also reference 'this' via some other way.</div>
<div><br></div><div>An examples shows that best. Consider:</div><div>struct A {</div><div> A(int* y) : z(y) {}</div><div> ~A() { *z = 23; }</div><div> int *z;</div><div>};</div><div>void f() {</div><div> int x = 0;</div>
<div> {(A(&x));}</div><div>}</div><div><br></div><div>This leads to a "Dereference of undefined pointer value" warning on *z = 23.</div><div>Funnily enough this warning goes away (and all values get correctly propagated) if change the constructor to be:</div>
<div>A(int* y) : z(y) { (void)this; }</div><div><br></div><div>Obviously this also works fine if I use a local variable instead of a temporary.</div><div><br></div><div>Any ideas what might be going wrong here would be greatly appreciated.</div>
</div></blockquote><br></div></div></div><div>My guess is that the temporary is an rvalue, and in the analyzer a NonLoc, and that the only reason this even appears to work is because we needed to be able to call <i>methods</i> on temporaries and so will create a temporary region for them on demand to serve as the 'this' pointer. However, I don't think we expect that to happen for <i>constructors,</i> and so we don't keep the region around after the constructor has finished executing. (That is, when it's just a method being called—and neither the constructor nor destructor are inlined—it doesn't actually matter what you do to the contents of the region, because nothing else can access it.)</div>
<div><br></div></div></blockquote><div><br></div><div>So, funnily enough if I remove this code:</div><div><div> if (isTemporaryPRValue(CCE, ThisV))</div><div><span style="white-space:pre-wrap"> </span>ThisV = state->getSVal(ThisV.castAs<Loc>());</div>
</div><div>which puts the temp region into a lazycompoundregion, the memory seems to be correctly handled. I have not yet looked what else this breaks though.</div></div></div></div></blockquote><br></div></div><div>Okay, analyzer design time. :-)</div>
<div><br></div><div>C++ has a distinction between lvalues and rvalues (actually glvalues and prvalues). There are a number of things that go into this distinction, but the part the analyzer cares about is that glvalues refer to true objects and prvalues do not. (In C++ parlance, "object" refers to anything with storage, a type, and lifetime, not just class instances. In the analyzer, we call such things "regions".)</div>
<div><br></div><div>If you were to build a very literal C++ compiler, every glvalue would be represented by an address. PRValues*, on the other hand, don't really need addresses; they only exist to be used transiently.</div>
<div><br></div><div><i>* How do you capitalize "prvalue"?</i></div><div><br></div><div>The analyzer pretty much always believes the compiler when it comes to glvalues vs. prvalues. That means that the value of a glvalue expression is <i>always</i> a Loc. A prvalue expression, on the other hand, will only be a Loc if it's a pointer type*. If you're talking about a struct prvalue, what you're really talking about is the bindings in its memory region—it has no identity. This is represented by a CompoundVal or LazyCompoundVal. This model worked great for C, but it's starting to show strain in C++.</div>
<div><br></div><div><i>* or one of the other types mentioned in Loc::isLocType. But not all of those can be expression values.</i></div><div><br></div><div>Besides "regular" temporaries, this is also a problem for prvalue function arguments. When the analyzer inlines a function, it just blithely binds the arguments to the proper VarRegions created for each ParmVarDecl in the current stack frame. That doesn't really fly if we're supposed to be calling the copy constructor. Worse, the copy constructor call happens <i>outside</i> of the function call, which means it happens before we've even decided whether or not to inline, which means there are no VarRegions yet to construct into. We've/I've been putting off dealing with this one for a while; we get around it mostly because we <i>don't inline constructors for temporaries.</i></div>
<div><br></div><div>The general temporary problem goes to what Richard was saying: a temporary expression is an rvalue, but if we have to run a constructor then clearly there <i>was</i> a region at some point.</div></div>
</blockquote><div><br></div><div>Here's how I think of this:</div><div><br></div><div>A glvalue expression is a function from the environment to a region.</div><div>A prvalue expression is a function of (environment, region) that produces no output.</div>
<div><br></div><div>That is, glvalue expressions tell you how to find a region, and prvalue expressions tell you how to initialize a region.</div><div><br></div><div>As a special case, you probably shouldn't bother to create a region when evaluating prvalues of non-class, non-array types (neither IRGen nor the constant evaluator do so). The standard blesses this with its "value that is not associated with an object" wording, but you can't actually observe the difference.</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>The C++11 standard (at least the version I have) is not wonderful about classifying temporaries ([basic.lval]p1):</div>
<div><br></div><div><div><blockquote type="cite">- An <i>lvalue</i>...designates a function or an object.</blockquote><blockquote type="cite">- An <i>xvalue</i> (an “eXpiring” value) also refers to an object, usually near the end of its lifetime.</blockquote>
<div><blockquote type="cite">- A <i>glvalue</i> (“generalized” lvalue) is an lvalue or an xvalue.</blockquote></div><div><blockquote type="cite">- An <i>rvalue...</i>is an xvalue, a temporary object (12.2) or subobject thereof, or a value that is not associated with an object.</blockquote>
<blockquote type="cite"><font color="#0f61c8">- A <i>prvalue</i> (“pure” rvalue) is an rvalue that is not an xvalue.</font></blockquote></div></div><div><br></div><div>So temporary objects are prvalues. Even though they are modifiable (defined later in the same section). Even though with construction and destruction they end up needing a fixed region, or at least consistency in region contents.</div>
<div><br></div><div>I don't have a great answer here. My guess is we'll probably end up turning on <i>some</i> inlining for temporaries to start, but not all.</div><div><br></div><div>Oh, right, so what's that 'isTemporaryPRValue' doing? That's taking the few cases where the AST wants a prvalue but the analyzer really needs a valid Loc, and conjuring a region on the fly. That's important for things that need to have referential identity, like the 'this' pointer in a method called on a temporary, or (say) a temporary that's been lifetime-extended.</div>
<div><br></div><div>Yuck, right? Just making all temporaries into xvalues would most likely make 60% of these problems go away.</div><span class="HOEnZb"><font color="#888888"><div>Jordan</div></font></span></div></div><br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div></div>