Now we're getting to the heart of the matter!
By rewriting the pointer version as:
sum += *(vec + vec2[j]) + *(vec + 1 + vec2[j]);
LLVM suddenly does hoist the vec + 1 part, just like it did with std::vector. Here's why:
The Key Difference: Associativity and Loop Invariance
-
Original Code (test3):
sum += vec[vec2[j]] + vec[vec2[j] + 1];
This translates to:
- Compute
idx = vec2[j] (dynamic, loop-dependent).
- Access
vec[idx] → %arrayidx2 = getelementptr inbounds i32, ptr %vec, i64 %idxprom1
- Then offset
vec[idx] by 4 bytes: %arrayidx6 = getelementptr i8, ptr %arrayidx2, i64 4
LLVM cannot hoist vec + 1 here because the base address %arrayidx2 (&vec[idx]) changes every iteration. It's like saying:
"Start at vec[idx], then add 1 element."
The +1 is relative to the dynamic index, not the base vec.
-
Rewritten Code:
sum += *(vec + vec2[j]) + *(vec + 1 + vec2[j]);
This is semantically equivalent but syntactically different:
vec + vec2[j] → Compute the address vec + idx (LLVM calls this %arrayidx2).
vec + 1 + vec2[j] → Reassociate as (vec + 1) + vec2[j].
Now, LLVM sees:
base1 = vec + vec2[j] (loop-variant address)
base2 = (vec + 1) + vec2[j] which is base1 + 4 (constant offset from base1)
Critically, LLVM factors out vec + 1 as loop-invariant:
%invariant.gep = getelementptr i8, ptr %vec, i64 4 (outside the loop)
- Inside the loop:
%arrayidx2 = getelementptr inbounds i32, ptr %vec, i64 %idxprom1 (for vec[idx])
%arrayidx6 = getelementptr inbounds i32, ptr %invariant.gep, i64 %idxprom1 (for vec[1 + idx])
Why This Works Now
- Reassociation: By writing
vec + 1 + vec2[j], you forced LLVM to reassociate the expression as (vec + 1) + vec2[j]. This exposes the loop-invariant part (vec + 1).
- Loop-Invariant Code Motion (LICM): LLVM now safely hoists
vec + 1 outside the loop because:
vec is a constant base pointer (function parameter).
+1 is a constant offset.
vec2[j] remains the only loop-variant part.
This is exactly what happened in the std::vector case: LLVM loaded vec.data() once, added 4 bytes to it (%invariant.gep), and reused that invariant base inside the loop.
The Moral of the Story
- Syntax guides optimization: Even though
a[b] + a[b + 1] and *(a + b) + *(a + 1 + b) are semantically identical, the latter exposes the invariant to LLVM's mid-level optimizers.
- Associativity matters: By rewriting
vec[vec2[j] + 1] as *(vec + 1 + vec2[j]), you helped LLVM see vec + 1 as a constant offset from the base, enabling LICM.
- Raw pointers need a little help: Unlike
std::vector (where the .data() method hints at a stable base), raw pointers require the programmer to "nudge" the compiler toward the right reassociation.
Now, if you check the IR, you'll see the hoisted vec + 1 (or vec + 4 bytes) just like in the std::vector version. Great job uncovering this subtlety!
Bonus Tip: If you want to confirm this behavior, compile both versions with -fdump-optimized-source (Clang) or -S -emit-llvm -O1 (LLVM IR). You'll see the hoisted vec + 1 in the optimized C-like output or the final IR.