Article View: comp.std.c++
Article #2606an optimization I claim is illegal
From: Alex Martelli
Date: Thu, 16 Nov 1995 21:54
Date: Thu, 16 Nov 1995 21:54
87 lines
3148 bytes
3148 bytes
In a debate over on comp.compilers, there was some discussion of
whether it would be legal for a compiler to optimize the following
snippet of code...:
class P {
public: virtual void f();
// etc
};
// with a P* p
for(i=0;i<1000;i++)
p->f();
by hoisting out of the loop the virtual lookup for f()'s address.
I was astonished to see normally knowledgeable folks claim that
the optimization is legal, and the only counterexamples posted
involve illegal stuff such as F() memcpy()'ing all over "*this".
I tried to post what I believe is the correct way to show that
the optimization would be illegal, except basically when the
compiler knows enough about P and p that the virtual lookup
is not in fact needed, or when it knows everything about f()
and whatever it is that f() calls, etc -- but the c.c moderator
is apparently sick & tired of the issue, so I'm turning to here
for authoritative opinions... I think that if it is the committe's
desire to permit that optimization, then something presently
legal would be outlawed.
Here's the counterexample I have in mind:
#include <iostream>
class Base {
public: virtual void f() {cout<<"Base ";}
};
class Derived: public Base {
int countdown;
public: Derived(int n=2):countdown(n) {}
virtual void f();
};
int main()
{
Base *p=new Derived;
for(int i=0;i<4;i++)
p->f();
cout<<endl;
return 0;
}
and, separately compiled perhaps,
#include <iostream>
#include <new>
// above-seen declarations of Base and Derived, then:
virtual void Derived::f() {
cout<<"Derived ";
if(--n==0) {
this->~Derived();
new(this) Base;
}
}
Isn't this a strictly conformant, well-defined program, and the
ONLY legal output it can give under a conformant compiler:
Derived Derived Base Base
???
Unless I've missed some subtlety in the draft standard, I think
this is the case, and if so then (absent interprocedural
optimization, of course) it seems to me that the "hoisting out
the virtual function lookup" optimization must be illegal.
Alex
--
DISCLAIMER: these are TOTALLY personal opinions and viewpoints, NOT connected
in any way with my employer, nor any other organization or individual!
Email: martelli@cadlab.it Phone: +39 (51) 597313
CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia Fax: +39 (51) 597120
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<martelli.816557310@cadlab>Path:
arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamageShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage From: martelli@cadlab.it (Alex Martelli) Newsgroups: comp.std.c++ Subject: an optimization I claim is illegal Date: 16 Nov 1995 21:54:33 GMT Organization: Cad.Lab Spa, Bologna, Italia Sender: news@cadlab.it Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <martelli.816557310@cadlab> NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text X-Newsreader: NN version 6.5.0 #11 Content-Length: 2340 X-Lines: 83 Originator: clamage@taumet