[cfe-dev] CFG temporary objects destructors

John McCall rjmccall at apple.com
Fri Oct 22 16:17:45 PDT 2010


On Oct 22, 2010, at 3:49 PM, Ted Kremenek wrote:

> 
> On Oct 22, 2010, at 10:59 AM, Marcin Świderski wrote:
> 
>> W dniu 22 października 2010 11:22 użytkownik Zhongxing Xu <xuzhongxing at gmail.com> napisał:
>> When the binary operator is logical operator, why we need to replicate the control flow when adding the dtors in LHS and RHS? Could we just add the dtor right after where the LHS and RHS are evaluated?
>> 
>> That is, for code
>> 
>> A && B
>> 
>> we generate CFG like this:
>> 
>> A && B
>> ~A()
>> |      \
>> |       B
>> |       ~B()
>> |        |
>> A && B
>> 
>> Since in logical binary operator, we only need the boolean value. The temporary object can be destroyed right after it is evaluated.
>> 
>> This depends on how much we want to simulate real control flow of expression. C++ standard states that destructors of temporaries should be called at the end of full expression and in reverse order of their construction. Your example does not satisfy this. It only guaranties that destructor for temporary will be called.
> 
> 
> I think Zhongxing is right.  Here is what the compiler does:

Marcin is right;  the temporary is destroyed conditionally at the end of the full expression.  It just happens to be the case that that immediately follows the call to 'B::operator bool()' in your example.  You can see that easily in the following modification to your example:

void baz(bool);
int test() {
  foo(foo() || bar());
}

test():                              ## @_Z4testv
Leh_func_begin0:
## BB#0:                                ## %entry
	pushq	%rbp
Ltmp0:
	movq	%rsp, %rbp
Ltmp1:
	pushq	%r14
	pushq	%rbx
	subq	$16, %rsp
Ltmp2:
	leaq	-24(%rbp), %rbx
	movq	%rbx, %rdi
	callq	foo()
	movq	%rbx, %rdi
	callq	A::operator bool()
	cmpb	$1, %al
	jne	LBB0_2
## BB#1:                                ## %lor.end.thread7
	movl	$1, %edi
	callq	baz(bool)
	jmp	LBB0_3
LBB0_2:                                 ## %temp.cond-dtor.call
	leaq	-32(%rbp), %rbx
	movq	%rbx, %rdi
	callq	bar()
	movq	%rbx, %rdi
	callq	B::operator bool()
	movzbl	%al, %edi
	callq	baz(bool)
	movl	%eax, %r14d
	movq	%rbx, %rdi
	callq	B::~B()
	movl	%r14d, %eax
LBB0_3:                                 ## %temp.cond-dtor.cont
	movl	%eax, %ebx
	leaq	-24(%rbp), %rdi
	callq	A::~A()
	movl	%ebx, %eax
	addq	$16, %rsp
	popq	%rbx
	popq	%r14
	popq	%rbp
	ret

Er, actually, the optimizer has helpfully cloned the call to 'baz' so as to obscure my point, but I think you get it.

John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20101022/9e4200e5/attachment.html>


More information about the cfe-dev mailing list