The previous implementation misses an opportunity to apply NRVO (Named Return Value
Optimization) below. That discourages user to write early return code.
struct Foo {}; Foo f(bool b) { if (b) return Foo(); Foo oo; return oo; }
That is, we can/should apply RVO for a local variable if:
- It's directly returned by at least one return statement.
- And, all reachable return statements in its scope returns the variable directly.
While, the previous implementation disables the RVO in a scope if there are multiple return
statements that refers different variables.
On the new algorithm, local variables are in NRVO_Candidate state at first, and a return
statement changes it to NRVO_Disabled for all visible variables but the return statement refers.
Then, at the end of the function AST traversal, NRVO is enabled for variables in NRVO_Candidate
state and refers from at least one return statement.
* on the right, please. Also auto -> Decl would be clearer and no longer. Is dyn_cast_or_null really necessary? (Can DeclsInScope contain nullptr?) I would expect that just dyn_cast would suffice.