Files
clang-p2996/llvm/test/Analysis/BasicAA/gep-and-alias.ll
Nikita Popov 1301a8b473 [BasicAA] Don't unnecessarily extend pointer size
BasicAA GEP decomposition currently performs all calculation on the
maximum pointer size, but at least 64-bit, with an option to double
the size. The code comment claims that this improves analysis power
when working with uint64_t indices on 32-bit systems. However, I don't
see how this can be, at least while maintaining correctness:

When working on canonical code, the GEP indices will have GEP index
size. If the original code worked on uint64_t with a 32-bit size_t,
then there will be truncs inserted before use as a GEP index. Linear
expression decomposition does not look through truncs, so this will
be an opaque value as far as GEP decomposition is concerned. Working
on a wider pointer size does not help here (or have any effect at all).

When working on non-canonical code (before first InstCombine), the
GEP indices are implicitly truncated to GEP index size. The BasicAA
code currently just ignores this fact completely, and pretends that
this truncation doesn't happen. This is incorrect and will be
addressed by D110977.

I believe that for correctness reasons, it is important to work on
the actual GEP index size to properly model potential overflow.
BasicAA tries to patch over the fact that it uses the wrong size
(see adjustToPointerSize), but it only does that in limited cases
(only for constant values, and not all of them either). I'd like to
move this code towards always working on the correct size, and
dropping these artificial pointer size adjustments is the first step
towards that.

Differential Revision: https://reviews.llvm.org/D110657
2021-10-06 18:40:21 +02:00

44 lines
1.6 KiB
LLVM

; RUN: opt -S -basic-aa -gvn < %s | FileCheck %s
target datalayout = "e-m:o-p:32:32-f64:32:64-f80:128-n8:16:32-S128"
target triple = "i386-apple-macosx10.6.0"
; The load and store address in the loop body could alias so the load
; can't be hoisted above the store and out of the loop.
declare void @llvm.memset.p0i8.i32(i8* nocapture writeonly, i8, i32, i1)
define i32 @foo(i32 %x, i32 %z, i32 %n) {
entry:
%pool = alloca [59 x i32], align 4
%tmp = bitcast [59 x i32]* %pool to i8*
call void @llvm.memset.p0i8.i32(i8* align 4 nonnull %tmp, i8 0, i32 236, i1 false)
%cmp3 = icmp eq i32 %n, 0
br i1 %cmp3, label %for.end, label %for.body.lr.ph
for.body.lr.ph: ; preds = %entry
%add = add i32 %z, %x
%and = and i32 %add, 2147483647
%sub = add nsw i32 %and, -2137521902
%arrayidx = getelementptr inbounds [59 x i32], [59 x i32]* %pool, i32 0, i32 %sub
%arrayidx1 = getelementptr inbounds [59 x i32], [59 x i32]* %pool, i32 0, i32 42
br label %for.body
for.body: ; preds = %for.body.lr.ph, %for.body
%i.04 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.body ]
store i32 %i.04, i32* %arrayidx, align 4
%tmp1 = load i32, i32* %arrayidx1, align 4
%inc = add nuw i32 %i.04, 1
%exitcond = icmp ne i32 %inc, %n
br i1 %exitcond, label %for.body, label %for.end.loopexit
for.end.loopexit: ; preds = %for.body
%lcssa = phi i32 [ %tmp1, %for.body ]
br label %for.end
for.end: ; preds = %for.end.loopexit, %entry
%s = phi i32 [ 0, %entry ], [ %lcssa, %for.end.loopexit ]
; CHECK: ret i32 %s
ret i32 %s
}