Skip to content

[SCEVAA] Enhance SCEVAAResult::alias() to handle two pointers with different pointer bases #91453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions llvm/lib/Analysis/ScalarEvolutionAliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ AliasResult SCEVAAResult::alias(const MemoryLocation &LocA,
? static_cast<uint64_t>(LocB.Size.getValue())
: MemoryLocation::UnknownSize);

// Firstly, try to convert the two pointers into ptrtoint expressions to
// handle two pointers with different pointer bases.
// Either both pointers are used with ptrtoint or neither, so we can't end
// up with a ptr + int mix.
const SCEV *AInt =
SE.getPtrToIntExpr(AS, SE.getEffectiveSCEVType(AS->getType()));
const SCEV *BInt =
SE.getPtrToIntExpr(BS, SE.getEffectiveSCEVType(BS->getType()));
if (!isa<SCEVCouldNotCompute>(AInt) && !isa<SCEVCouldNotCompute>(BInt)) {
AS = AInt;
BS = BInt;
}

// Compute the difference between the two pointers.
const SCEV *BA = SE.getMinusSCEV(BS, AS);

Expand Down
26 changes: 26 additions & 0 deletions llvm/test/Analysis/ScalarEvolution/scev-aa.ll
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,29 @@ for.latch:
for.end:
ret void
}

; CHECK-LABEL: Function: test_different_pointer_bases_of_inttoptr: 2 pointers, 0 call sites
; CHECK: NoAlias: <16 x i8>* %tmp5, <16 x i8>* %tmp7

define void @test_different_pointer_bases_of_inttoptr() {
entry:
br label %for.body

for.body:
%tmp = phi i32 [ %next, %for.body ], [ 1, %entry ]
%tmp1 = shl nsw i32 %tmp, 1
%tmp2 = add nuw nsw i32 %tmp1, %tmp1
%tmp3 = mul nsw i32 %tmp2, 1408
%tmp4 = add nsw i32 %tmp3, 1408
%tmp5 = getelementptr inbounds i8, ptr inttoptr (i32 1024 to ptr), i32 %tmp1
%tmp6 = load <16 x i8>, ptr %tmp5, align 1
%tmp7 = getelementptr inbounds i8, ptr inttoptr (i32 4096 to ptr), i32 %tmp4
store <16 x i8> %tmp6, ptr %tmp7, align 1

%next = add i32 %tmp, 2
%exitcond = icmp slt i32 %next, 10
br i1 %exitcond, label %for.body, label %for.end

for.end:
ret void
}
Loading