Skip to content

Commit 96d2dc7

Browse files
authored
[SCEVAA] Enhance SCEVAAResult::alias() to handle two pointers with different pointer bases (#91453)
This patch enhances the SCEVAAResult::alias() interface to handle two pointers with different pointer bases.  Before calling getMinusSCEV(), we firstly try to explicitly convert these two pointers into ptrtoint expressions to do that.  Either both pointers are used with ptrtoint or neither, so we can't end up with a ptr + int mix.
1 parent 30c10fd commit 96d2dc7

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

llvm/lib/Analysis/ScalarEvolutionAliasAnalysis.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ AliasResult SCEVAAResult::alias(const MemoryLocation &LocA,
6161
? static_cast<uint64_t>(LocB.Size.getValue())
6262
: MemoryLocation::UnknownSize);
6363

64+
// Firstly, try to convert the two pointers into ptrtoint expressions to
65+
// handle two pointers with different pointer bases.
66+
// Either both pointers are used with ptrtoint or neither, so we can't end
67+
// up with a ptr + int mix.
68+
const SCEV *AInt =
69+
SE.getPtrToIntExpr(AS, SE.getEffectiveSCEVType(AS->getType()));
70+
const SCEV *BInt =
71+
SE.getPtrToIntExpr(BS, SE.getEffectiveSCEVType(BS->getType()));
72+
if (!isa<SCEVCouldNotCompute>(AInt) && !isa<SCEVCouldNotCompute>(BInt)) {
73+
AS = AInt;
74+
BS = BInt;
75+
}
76+
6477
// Compute the difference between the two pointers.
6578
const SCEV *BA = SE.getMinusSCEV(BS, AS);
6679

llvm/test/Analysis/ScalarEvolution/scev-aa.ll

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,29 @@ for.latch:
340340
for.end:
341341
ret void
342342
}
343+
344+
; CHECK-LABEL: Function: test_different_pointer_bases_of_inttoptr: 2 pointers, 0 call sites
345+
; CHECK: NoAlias: <16 x i8>* %tmp5, <16 x i8>* %tmp7
346+
347+
define void @test_different_pointer_bases_of_inttoptr() {
348+
entry:
349+
br label %for.body
350+
351+
for.body:
352+
%tmp = phi i32 [ %next, %for.body ], [ 1, %entry ]
353+
%tmp1 = shl nsw i32 %tmp, 1
354+
%tmp2 = add nuw nsw i32 %tmp1, %tmp1
355+
%tmp3 = mul nsw i32 %tmp2, 1408
356+
%tmp4 = add nsw i32 %tmp3, 1408
357+
%tmp5 = getelementptr inbounds i8, ptr inttoptr (i32 1024 to ptr), i32 %tmp1
358+
%tmp6 = load <16 x i8>, ptr %tmp5, align 1
359+
%tmp7 = getelementptr inbounds i8, ptr inttoptr (i32 4096 to ptr), i32 %tmp4
360+
store <16 x i8> %tmp6, ptr %tmp7, align 1
361+
362+
%next = add i32 %tmp, 2
363+
%exitcond = icmp slt i32 %next, 10
364+
br i1 %exitcond, label %for.body, label %for.end
365+
366+
for.end:
367+
ret void
368+
}

0 commit comments

Comments
 (0)