F
Frederick Gotham
I'm going to be using an acronym a lot in this post:
IINM = If I'm not mistaken
Let's say we've got translation units which are going to be compiled to
object files, and that these object files will be supplied to people to link
with their own projects. Here's a sample function in one of the object files:
void Func(int const *const p)
{
int i = 0;
if(*p) ++i;
if(*p) ++i;
if(*p) ++i;
if(*p) ++i;
/* More code */
}
(Forget for the moment how stupid the function is.)
We can see that "*p" is accessed four times. If I'm not mistaken, the
compiler can optimise this to the following:
void Func(int const *const p)
{
int i = 0;
if(*p) i = 4;
}
Now, "*p" is only accessed once.
IINM, the only condition under which the compiler can't perform this
optimisation is if either:
(1) "p" is a pointer to volatile.
(2) "i" is volatile.
Could someone please confirm that?
Here's our next function:
void Func(int const *const p, int (*const OtherFunc)(void))
{
int i = 0;
if (*p) i += OtherFunc();
if (*p) i += OtherFunc();
}
It is my understanding that, even thought "p" is not a pointer to volatile,
the compiler can NOT optimise this to the following:
void Func(int const *const p, int (*const OtherFunc)(void))
{
int i = 0;
if (*p)
{
i = OtherFunc() + OtherFunc();
}
}
IINM, the second access to "*p" cannot be optimised away because we don't
know if the invocation of "Func" may have altered the data at that address.
Am I right?
If I understand correctly, this is why "restrict" was brought in. If we want
to be able to optimise away the second access of "*p", then we must write the
function as:
void Func(int const *const restrict p, int (*const OtherFunc)(void));
Am I right in thinking that, because the pointer is restrict, the compiler
can know turn that into:
void Func(int const *const restrict p, int (*const OtherFunc)(void))
{
int i = 0;
if (*p)
{
i = OtherFunc() + OtherFunc();
}
}
IINM = If I'm not mistaken
Let's say we've got translation units which are going to be compiled to
object files, and that these object files will be supplied to people to link
with their own projects. Here's a sample function in one of the object files:
void Func(int const *const p)
{
int i = 0;
if(*p) ++i;
if(*p) ++i;
if(*p) ++i;
if(*p) ++i;
/* More code */
}
(Forget for the moment how stupid the function is.)
We can see that "*p" is accessed four times. If I'm not mistaken, the
compiler can optimise this to the following:
void Func(int const *const p)
{
int i = 0;
if(*p) i = 4;
}
Now, "*p" is only accessed once.
IINM, the only condition under which the compiler can't perform this
optimisation is if either:
(1) "p" is a pointer to volatile.
(2) "i" is volatile.
Could someone please confirm that?
Here's our next function:
void Func(int const *const p, int (*const OtherFunc)(void))
{
int i = 0;
if (*p) i += OtherFunc();
if (*p) i += OtherFunc();
}
It is my understanding that, even thought "p" is not a pointer to volatile,
the compiler can NOT optimise this to the following:
void Func(int const *const p, int (*const OtherFunc)(void))
{
int i = 0;
if (*p)
{
i = OtherFunc() + OtherFunc();
}
}
IINM, the second access to "*p" cannot be optimised away because we don't
know if the invocation of "Func" may have altered the data at that address.
Am I right?
If I understand correctly, this is why "restrict" was brought in. If we want
to be able to optimise away the second access of "*p", then we must write the
function as:
void Func(int const *const restrict p, int (*const OtherFunc)(void));
Am I right in thinking that, because the pointer is restrict, the compiler
can know turn that into:
void Func(int const *const restrict p, int (*const OtherFunc)(void))
{
int i = 0;
if (*p)
{
i = OtherFunc() + OtherFunc();
}
}