May a compiler optimize a reference to constant parameter to constantvalue ?

G

Good Guy

Consider following function:

void func(const char & input){
//do something
}

Apparently it makes sense for the parameter to be constant value not
reference to constant, Now may a compiler optimize that to constant
value so that it'll be the same as following ?

void func(const char input){
//do something
}
 
S

Saeed Amrollahi

Consider following function:

void func(const char & input){
 //do something

}

Apparently it makes sense for the parameter to be constant value not
reference to constant, Now may a compiler optimize that to constant
value so that it'll be the same as following ?

void func(const char input){
 //do something

}

Hi
I don't get what you mean?
You can bind a const char and non-const char to const reference:
Example:

void f(const char& c)
{
}

int main()
{
const char c1 = 'c';
char c2 = 'd';

f(c1); // OK: bind const char to const reference to char
f(c2); // OK: bind char to const reference to char
}

Regards,
-- Saeed Amrollahi
 
G

Good Guy

Hi
I don't get what you mean?
You can bind a const char and non-const char to const reference:
Example:

void f(const char& c)
{

}

int main()
{
  const char c1 = 'c';
  char c2 = 'd';

  f(c1); // OK: bind const char to const reference to char
  f(c2); // OK: bind char to const reference to char

}

Regards,
  -- Saeed Amrollahi


سلام، در رابطه با بهینه سازی از نظر اسمبلی صحبت می کنم.

because overall size of a char variable is 1 bytes and less than 4
bytes that is size of a pointer on my platform, the sensible optimized
way to pass it is by constant value not passing address of it.
 
G

gwowen

Consider following function:

void func(const char & input){
 //do something
}

Now may a compiler optimize that to constant
value so that it'll be the same as following ?

void func(const char input){
 //do something
}

It may, as long as the compiler knows it can change all the call-sites
to match its redefinition. e.g. if the function is local to the
compilation unit (static function, or anonymous namespace), then no
external compilation unit can call that function and the compiler is
free to modify the contract.

If the function is visible from independent compilation units (e.g.
with a prototype exposed through a header) then that prototype acts as
a contract that the compiler must honour, subject to the platform ABI.

As with many of these micro-optimisations, its dubious (at best) that
they'll give any performance improvement, and may even hinder, so its
unlikely that any real compiler *would* do that.
 
S

Saeed Amrollahi

سلام، در رابطه با بهینه سازی از نظر اسمبلی صحبت می کنم.

because overall size of a char variable is 1 bytes and less than 4
bytes that is size of a pointer on my platform, the sensible optimized
way to pass it is by constant value not passing address of it.

salam
Finally, I see another Iranian on Comp.Lang.C++ discussion group.
I see, what you mean. You mean, because references are usually
implemented using const pointer and pointers are usually 4 bytes
and the size of char is 1 according to C++, So compiler optimizes it
and uses the character rather than pointer.
Of course I'm not compiler expert, but
Bjarne Stroustrup in 3rd (page 98) wrote:
"In some cases, the compiler can optimize away a
reference so that there is no object representing
that reference at run-time."

Good Luck
-- Saeed Amrollahi
 
G

Good Guy

salam
Finally, I see another Iranian on Comp.Lang.C++ discussion group.
I see, what you mean. You mean, because references are usually
implemented using const pointer and pointers are usually 4 bytes
and the size of char is 1 according to C++, So compiler optimizes it
and uses the character rather than pointer.
Of course I'm not compiler expert, but
Bjarne Stroustrup in 3rd (page 98) wrote:
"In some cases, the compiler can optimize away a
reference so that there is no object representing
that reference at run-time."

Good Luck
  -- Saeed Amrollahi

Interesting to see ya here too, I'll tell you what, one of cases that
this kinda optimization happens is when a function having a reference
as one of its parameters, is inline .
 
J

Johannes Schaub (litb)

Good said:
Consider following function:

void func(const char & input){
//do something
}

Apparently it makes sense for the parameter to be constant value not
reference to constant, Now may a compiler optimize that to constant
value so that it'll be the same as following ?

void func(const char input){
//do something
}

Being discussed on SO: Was asked on SO too:
http://stackoverflow.com/questions/3923417/may-a-compiler-optimize-a-
reference-to-constant-parameter-to-constant-value
 
K

Keith H Duggar

Consider following function:

void func(const char & input){
 //do something

}

Apparently it makes sense for the parameter to be constant value not
reference to constant, Now may a compiler optimize that to constant
value so that it'll be the same as following ?

void func(const char input){
 //do something
}

The situation is far more complicated than the previous posters
have implied. And this is true of optimization rules in general.
What can or cannot be optimized depends intimately on the actual
code bodies as a whole. Consider this simple example:

extern char TheChar ;
extern bool TheMode ;

void func (
char const & c
) {
TheMode = (&c == &TheChar) ;
}

The behavior for 'void func ( char const c )' would be entirely
different.

KHD
 
J

James Kanze

Consider following function:
void func(const char & input){
//do something
}
Apparently it makes sense for the parameter to be constant
value not reference to constant, Now may a compiler optimize
that to constant value so that it'll be the same as following
?
void func(const char input){
//do something
}

The compiler can do pretty much anything it likes, as long as
the observable behavior of the program doesn't change.

In practice, the compiler can only change the defined calling
interface from the standard form if it can find all of the call
sites, and change them as well (possibly inlining the function
and eliminating the argument entirely). This is generally
possible if the function is static, and so isn't visible outside
the translation unit, or if the function is inline, but modern
compilers are capable of optimizing accross translation unit
boundaries, and may be capable of finding all of the call sites
in the entire program. And as Keith Duggar has said, it also
depends on what the function does. Taking the address of
a reference returns the address of what is referred to, and if
this affects the observable behavior of the program, then the
compiler must take that into account.
 
G

gwowen

What can or cannot be optimized depends intimately on the actual
code bodies as a whole.

Thank you for that blindingly obvious point. No-one asked if it
*always* possible, merely if was allowed in circumstances where it was
possible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top