[Info-vax] yet another sys$qiow question
Johnny Billquist
bqt at softjar.se
Thu Aug 20 09:23:28 EDT 2015
On 2015-08-20 01:40, David Froble wrote:
> Johnny Billquist wrote:
>
> Some of this has caught my attention.
>
>
>> Exmaple:
>> x = iosb;
>> y = iosb;
>
>
>> The C compiler can easily compact this into just one read of the iosb
>> variable, even if your source code have two reads. And if the system
>> actaully changed the value between these two reads, then you could
>> argue that y got the wrong value.
>
> If this is what you get from optimization, then I got to ask, is
> optimized code accurate?
Yes, it is. Let's assume that iosb is a "normal" variable.
Now, assigning the contents of iosb to x, and then assigning it to y,
the iosb variable cannot change between the two statements. That should
be obvious. You can see the code with your own eyes, there is no
modification of the iosb between these two statements. So, why read the
iosb variable twice? Better reading it once, put it in a register, and
write that register to two cells. The end result is identical, but it
will be faster.
>> Even more fun:
>>
>> while (iosb == 0) ;
>
> In this case, I'm going to observe that it's poor coding practice. Why
> would you tie up the CPU in a loop? Wouldn't that have an adverse
> affect on all users?
I didn't claim that this was "good" code. But it was/is very good at
illustrating a problem. In real life, the problem often shows up in
other ways, but is caused by the same kind of optimizations.
Another, actually rather common, error is that you write to a variable
declared locally in a function, and then the function end. The compiler
will notice that nothing will be using the value you write to the
variable, so why do the write at all? Optimization will remove writes of
values that are never used.
If that write in reality was to a register mapped to I/O space, the
write itself might cause a side effect, such as the starting of an I/O
operation. So, obviously, the write actually do matter. But the compiler
cannot know this. Thus, you need volatile to tell that reading/writing
have effects the the compiler do not know, and thus the compiler are not
allowed to do optimizations of accesses to the variable.
Johnny
More information about the Info-vax
mailing list