« first day    last day (291 days later) » 

12:42 AM
@bogdan: Thinking about it, maybe it would be simplest to start with two basic principles: 1. A use of a reference that is recognizably derived from another is simply a use of the latter reference, and does not constitute aliasing (though what counts as "recognizably derived" could sometimes be ambiguous) 2. For two references to alias during a particular execution of a function, both must be used during that execution (including functions called from it).
Most other complexity in the rules I'd like to see would revolve around when a quality compiler should be able to recognize that one reference is derived from another, but the principles, but together, allow for passing e.g. a pointer to a union member to a function that knows nothing about the union, provided that the object is accessed during the course of that function only through that pointer.
 
 
17 hours later…
5:22 PM
@supercat Yeah, the current rules are definitely not simple, but yours don't strike me as simple either. Yes, the basic principles are simple, but so are the current ones; once we get to practical rules, things get complicated, and I'm afraid that's inevitable if we keep looking at the problem from this angle - general rules that must fit all cases.
@supercat Yeah, use char8_t, which hopefully will be adopted into C++ really soon now (all fingers crossed in all patterns possible) and that can't alias anything :-D
Seriously, I would very much like to see an entirely different approach to these optimization problems.
What if the compiler warned you whenever it can't apply a possible optimization because the general rules get in the way, and allowed to you directly indicate, for that specific case, what the actual state of affairs is?
In this case, if you knew (without having to read assembly) that aliasing causes an optimization issue in that loop, and you could indicate simply and clearly that *dataptr will never alias datalen, the problem would be solved.
And you wouldn't even need to learn (almost) any kind of rules about aliasing: the compiler tells you what the problem is, you reply that it's not a problem in this case. done.
And all that would be visible in the source code for anybody else coming back to this later on, instead of maybe be mentioned in some comment.
 
 
2 hours later…
7:38 PM
@supercat Somewhat outside the scope of our discussion, but since you asked... for your example you could do the following in C++:
struct my_byte
{
    char m;
    my_byte(char c) : m(c) { }
    operator char() { return m; }
};

my_byte *dataptr;
my_byte can't alias anything as plain char does, so the code optimizes nicely.
 
8:04 PM
Also somewhat related both to this discussion and one of our previous ones regarding type punning and casting, this has been adopted into the draft for C++20: open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0476r2.html
(In case you didn't know already :-) )
It's not as powerful as I would like it, but it's a good step in the right direction.
 

« first day    last day (291 days later) »