Languages that give "hints" to the compiler

ssimmons@convex.com (Steve Simmons)
Mon, 1 Aug 1994 12:30:08 GMT

          From comp.compilers

Related articles
Languages that give "hints" to the compiler jhummel@cy4.ICS.UCI.EDU (Joe Hummel) (1994-07-18)
Re: Languages that give "hints" to the compiler wisej@acf4.nyu.edu (1994-07-29)
Re: Languages that give "hints" to the compiler tgl@netcom.com (1994-07-19)
Re: Languages that give "hints" to the compiler jfisher@hplabsz.hpl.hp.com (1994-07-21)
Re: Languages that give "hints" to the compiler rfg@netcom.com (1994-07-31)
Languages that give "hints" to the compiler ssimmons@convex.com (1994-08-01)
Re: Languages that give "hints" to the compiler jls@summit.novell.com (1994-08-02)
Re: Languages that give "hints" to the compiler rfg@netcom.com (1994-08-03)
| List of all articles for this month |
Newsgroups: comp.compilers
From: ssimmons@convex.com (Steve Simmons)
Keywords: C, C++, optimize
Organization: CONVEX News Network, Engineering (cnn.eng), Richardson, Tx USA
References: 94-07-066 94-07-104
Date: Mon, 1 Aug 1994 12:30:08 GMT
Status: RO

> Now to tell the truth, I know of no C or C++ compilers that currently make
> use of const qualification in this way, but I keep hoping. (If anyone
> else knows of compilers that *do* use const qualification in the manner
> I've described, please let me know.) Evidence of my hope may be seen
> frequently in my own (recent) code, which is littered with stuff with
> looks roughly like the following (for example):


The const qualifier is great; however, it would be a bad optimizer that
would key purely off the const qualifier. Why???? Const can be coerced/cast
away. Look at the following C++ code...


int foo::getValue() const
{
        if (!valueInCache) ((foo*) this)->value = calculateValue();
        return value;
}


void example()
{
      const foo bar;


        .... load all of bar's fields


      int i = bar.getValue();


      .... the value and valueInCache fields would be stale here.
}




Here, the "this" pointer is to a const class of foo. In this case, the
field "value" needs to be updated on first reference because it is a
cached value. If all of the values for bar's fields were loaded before the
call, it would be stale after the call.


You may argue the coercing constness away is a bad practice; however, the
language permits it and it is really good code like in the above example.
Here, the interface says that it is retrieving a value. The caller merely
perceives that it is reading a value and not writing into the object. It
is merely a coincidence of implementation that the body requires a
calculation for the value in cache.


Therefore, a good optimizer should probably rely on data flow analysis to
see if a variable is really constant.


Thank you.




Steve Simmons
--


Post a followup to this message

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