https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128945
Bug ID: 128945
Summary: Accesses through pointers derived from one base with
disjoint offset ranges not disambiguated without TBAA
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: pmenzel+gcc at molgen dot mpg.de
Target Milestone: ---
Created attachment 66254
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66254&action=edit
Reproducer
[This report was drafted with an LLM (Claude Opus 5.5 in Claude Code) and
reviewed by me; the analysis and the measurements were also done with it.]
With -fno-strict-aliasing, GCC does not disambiguate two memory accesses based
on different pointers derived from the same base pointer, even when their
offset ranges cannot overlap. Clang does.
$ cat alias.c
struct rc { unsigned range, code; };
struct s { struct rc rc; unsigned short probs[16][256]; };
unsigned
f1 (struct s *s, unsigned k, unsigned i)
{
unsigned short *p = s->probs[k];
unsigned r = s->rc.range;
p[i] = 1;
return r - s->rc.range; /* always 0 */
}
void
f2 (struct s *s, unsigned k)
{
unsigned short *probs = s->probs[k];
unsigned i = 1;
do
{
unsigned short *p = &probs[i];
unsigned bound = (s->rc.range >> 11) * *p;
s->rc.range = bound;
*p += (2048 - *p) >> 5;
i = 2 * i + (bound & 1);
}
while (i < 256);
}
GCC trunk (r17-6554-g54805df7d2f), -O2 -fno-strict-aliasing, powerpc64le:
f1:
sldi 5,5,1
sldi 4,4,9
lwz 9,0(3)
add 4,4,5
li 10,1
add 4,3,4
sth 10,8(4)
lwz 3,0(3) <- reload of s->rc.range
subf 3,3,9
rldicl 3,3,0,32
blr
The optimized dump still has
r_9 = s_5(D)->rc.range;
...
_3 = s_5(D) + _16;
*_3 = 1;
_4 = s_5(D)->rc.range;
_12 = r_9 - _4;
return _12;
Clang 23.1.3, same options:
f1:
sldi 4, 4, 9
add 3, 3, 4
sldi 4, 5, 1
add 3, 3, 4
li 4, 1
sth 4, 8(3)
li 3, 0
blr
In f2, GCC stores s->rc.range and reloads it in every iteration (and loads *p
twice), while Clang keeps it in a register and stores it once after the loop.
x86_64 shows the same (checked with GCC 12.2 to 16.1 and Clang 23.1.3). With
-fstrict-aliasing, GCC folds f1 to "return 0" because TBAA separates unsigned
short from unsigned int.
Why the transformation is valid: p[i] is at byte offset 8 + 512 * k + 2 * i
from s, with k and i unsigned int, so the offset is in [8, 8 + 512 * 0xffffffff
+ 2 * 0xffffffff], far below 2^63. The store can never overlap s->rc.range at
[0, 4), not even if pointer arithmetic is assumed to wrap (-fno-strict-overflow
/ -fwrapv-pointer, as the Linux kernel uses). With a signed index (int i),
p[i] can reach s->rc (i = -4), so that case must stay as it is.
As far as I can see, indirect_refs_may_alias_p (tree-ssa-alias.cc) compares
offsets only when both references use the same pointer SSA name. For different
pointers, points-to analysis finds that both point to the same object, and
without TBAA nothing else separates them. LLVM’s BasicAA decomposes both
addresses into a common base plus constant and scaled variable offsets and uses
the known ranges of the variable parts.
Impact: this is the hot loop of the xz-embedded LZMA2 decoder
(linux/lib/xz/xz_dec_lzma2.c [1], rc_bit/rc_bittree), where the range coder
state (rc.range, rc.code) and the probability arrays live in one structure, and
which the Linux kernel builds with -fno-strict-aliasing. xz-embedded master
(52f2742), userspace benchmark decompressing i915.ko.xz, -O2
-fno-strict-aliasing -static, minimum of 5 iterations of 3 interleaved rounds,
pinned to one CPU of an idle POWER8 (3.49 GHz):
GCC 11 (separate earlier run) 283.7 ms
GCC 16.2.0 285.5 ms
GCC 17.0.0 (54805df7d2f) 285.6 ms
GCC 17.0.0 -fstrict-aliasing 248.3 ms
Clang 23.1.3 264.3 ms
[1]:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/lib/xz/xz_dec_lzma2.c