32 ms·
The compiler would know that the alignment is smaller than the type itself, which would disable the optimization mentioned in the post.
by eqvinox 7y ago
The compiler would know that the alignment is smaller than the type itself, which would disable the optimization mentioned in the post.
- ajross 7y agoIt literally cannot. The function in the linked article, that takes two pointers, must be compiled in isolation. If it needs to depend on alignment or overlapping, it needs to emit code to check it. And the standard does not require it to, nor do typical users want that. It's true that fancy tricks like LTO can make that kind of behavior visible to the optimizer, but the standard predates that kind of magic by several decades.
- eqvinox 7y agoThat's not what I meant. On an architecture where "long long" is 8 bytes, but only has a 4 byte alignment requirement, the compiler knows this, and knows that 2 pointers of type "long long * " may overlap. Therefore it would disable the optimization. (Except the pointers still aren't supposed to overlap, but...)
- bonzini 7y agoNo, it wouldn't disable the optimization, because a pointer 4 bytes into a live long long object isn't a valid pointer to another long long object that is live at the same time (even though it could abstractly be a valid pointer to long long over the life of the program). If you want to disable the optimization you can use a union (which would also be technically UB but in practice compilers document that they do the right thing).