Re: C++ virtual function calls

cliffc@ami.sps.mot.com (Cliff Click)
Sun, 5 Nov 1995 01:22:53 GMT

          From comp.compilers

Related articles
C++ virtual function calls tim@franck.Princeton.EDU (1995-09-29)
Re: C++ virtual function calls cliffc@ami.sps.mot.com (1995-10-05)
Re: C++ virtual function calls dlmoore@ix.netcom.com (1995-10-14)
Re: C++ virtual function calls genew@mindlink.bc.ca (1995-10-23)
Re: C++ virtual function calls cliffc@ami.sps.mot.com (1995-10-25)
Re: C++ virtual function calls joe@sanskrit.ho.att.com (1995-10-30)
Re: C++ virtual function calls jplevyak@violet-femmes.cs.uiuc.edu (John B. Plevyak) (1995-11-09)
Re: C++ virtual function calls cliffc@ami.sps.mot.com (1995-11-05)
Re: C++ virtual function calls martelli@cadlab.it (1995-11-05)
Re: C++ virtual function calls bothner@cygnus.com (Per Bothner) (1995-11-06)
C++ virtual function calls fjh@cs.mu.OZ.AU (1995-11-12)
Re: C++ virtual function calls jplevyak@pink-panther.cs.uiuc.edu (1995-11-16)
| List of all articles for this month |

Newsgroups: comp.compilers
From: cliffc@ami.sps.mot.com (Cliff Click)
Keywords: C++, optimize
Organization: none
References: 95-10-029 95-11-020
Date: Sun, 5 Nov 1995 01:22:53 GMT

joe@sanskrit.ho.att.com (Joe Orost) writes:


> I don't think it is legal to hoist the virtual function lookup
> out of the loop:
>
> for( ...) {
> p->virt_func();
> }
>
> because "p" is passed to virt_func as "this", and therefore the
> function could change the subtype of "p", causing a different
> function to be called next time around the loop!


The compiler _could_ know something about p that precluded this.
Such as: p is a local variable whose address has not "escaped".
This all becomes more obvious if we explode this HLL call into
some low-level instructions:


loop:
    ld r1 <- _p(stack) # r1 has type: ptr to object
    ld r2 <- vptr_off(r1) # r2 has type: const array of member fcn ptrs
    ld r3 <- virt_func(r2) # r3 has type: ptr to member fcn


    mov arg0 <- r1 # Move r1 into hidden "this" parameter
    call r3 # Make the call
    ...
    goto loop


If p's address is not taken then the virtual fcn can't change it,
r1 becomes loop invariant (as does r2 and r3). Various flavors of
interprocedural analysis might discover p is invariant dispite it's
address being exposed.


Cliff




--
Cliff Click Compiler Researcher & Designer
RISC Software, Motorola PowerPC Compilers
cliffc@risc.sps.mot.com (512) 891-7240
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.