Skip to content

Commit e987fbd

Browse files
committed
[BasicAA] Generalize recursive phi alias analysis
For recursive phis, we skip the recursive operands and check that the remaining operands are NoAlias with an unknown size. Currently, this is limited to inbounds GEPs with positive offsets, to guarantee that the recursion only ever increases the pointer. Make this more general by only requiring that the underlying object of the phi operand is the phi itself, i.e. it it based on itself in some way. To compensate, we need to use a beforeOrAfterPointer() location size, as we no longer have the guarantee that the pointer is strictly increasing. This allows us to handle some additional cases like negative geps, geps with dynamic offsets or geps that aren't inbounds. Differential Revision: https://reviews.llvm.org/D91914
1 parent d9da4c3 commit e987fbd

File tree

3 files changed

+16
-31
lines changed

3 files changed

+16
-31
lines changed

llvm/lib/Analysis/BasicAliasAnalysis.cpp

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,27 +1516,16 @@ AliasResult BasicAAResult::aliasPHI(const PHINode *PN, LocationSize PNSize,
15161516
}
15171517

15181518
SmallVector<Value *, 4> V1Srcs;
1519-
// For a recursive phi, that recurses through a contant gep, we can perform
1520-
// aliasing calculations using the other phi operands with an unknown size to
1521-
// specify that an unknown number of elements after the initial value are
1522-
// potentially accessed.
1519+
// If a phi operand recurses back to the phi, we can still determine NoAlias
1520+
// if we don't alias the underlying objects of the other phi operands, as we
1521+
// know that the recursive phi needs to be based on them in some way.
15231522
bool isRecursive = false;
15241523
auto CheckForRecPhi = [&](Value *PV) {
15251524
if (!EnableRecPhiAnalysis)
15261525
return false;
1527-
if (GEPOperator *PVGEP = dyn_cast<GEPOperator>(PV)) {
1528-
// Check whether the incoming value is a GEP that advances the pointer
1529-
// result of this PHI node (e.g. in a loop). If this is the case, we
1530-
// would recurse and always get a MayAlias. Handle this case specially
1531-
// below. We need to ensure that the phi is inbounds and has a constant
1532-
// positive operand so that we can check for alias with the initial value
1533-
// and an unknown but positive size.
1534-
if (PVGEP->getPointerOperand() == PN && PVGEP->isInBounds() &&
1535-
PVGEP->getNumIndices() == 1 && isa<ConstantInt>(PVGEP->idx_begin()) &&
1536-
!cast<ConstantInt>(PVGEP->idx_begin())->isNegative()) {
1537-
isRecursive = true;
1538-
return true;
1539-
}
1526+
if (getUnderlyingObject(PV) == PN) {
1527+
isRecursive = true;
1528+
return true;
15401529
}
15411530
return false;
15421531
};
@@ -1582,15 +1571,11 @@ AliasResult BasicAAResult::aliasPHI(const PHINode *PN, LocationSize PNSize,
15821571
if (V1Srcs.empty())
15831572
return MayAlias;
15841573

1585-
// If this PHI node is recursive, set the size of the accessed memory to
1586-
// unknown to represent all the possible values the GEP could advance the
1587-
// pointer to.
1574+
// If this PHI node is recursive, indicate that the pointer may be moved
1575+
// across iterations. We can only prove NoAlias if different underlying
1576+
// objects are involved.
15881577
if (isRecursive)
1589-
// TODO: We are checking above that the addrec GEP has a positive offset
1590-
// and can thus assume that all accesses happen after the base pointer.
1591-
// It might be better to drop the offset requirement and use
1592-
// beforeOrAfterPointer().
1593-
PNSize = LocationSize::afterPointer();
1578+
PNSize = LocationSize::beforeOrAfterPointer();
15941579

15951580
// In the recursive alias queries below, we may compare values from two
15961581
// different loop iterations. Keep track of visited phi blocks, which will

llvm/test/Analysis/BasicAA/recphi.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ if.end: ; preds = %f.exit
141141
; CHECK: NoAlias: [3 x i16]* %int_arr.10, i16** %argv.6.par
142142
; CHECK: NoAlias: i16* %_tmp1, i16** %argv.6.par
143143
; CHECK: PartialAlias: [3 x i16]* %int_arr.10, i16* %_tmp1
144-
; CHECK: MayAlias: i16* %ls1.9.0, i16** %argv.6.par
144+
; CHECK: NoAlias: i16* %ls1.9.0, i16** %argv.6.par
145145
; CHECK: MayAlias: [3 x i16]* %int_arr.10, i16* %ls1.9.0
146146
; CHECK: MayAlias: i16* %_tmp1, i16* %ls1.9.0
147-
; CHECK: MayAlias: i16* %_tmp7, i16** %argv.6.par
147+
; CHECK: NoAlias: i16* %_tmp7, i16** %argv.6.par
148148
; CHECK: MayAlias: [3 x i16]* %int_arr.10, i16* %_tmp7
149149
; CHECK: MayAlias: i16* %_tmp1, i16* %_tmp7
150150
; CHECK: NoAlias: i16* %_tmp7, i16* %ls1.9.0
@@ -191,9 +191,9 @@ bb5: ; preds = %bb3, %bb4
191191
; CHECK-LABEL: Function: dynamic_offset
192192
; CHECK: NoAlias: i8* %a, i8* %p.base
193193
; CHECK: MayAlias: i8* %p, i8* %p.base
194-
; CHECK: MayAlias: i8* %a, i8* %p
194+
; CHECK: NoAlias: i8* %a, i8* %p
195195
; CHECK: MayAlias: i8* %p.base, i8* %p.next
196-
; CHECK: MayAlias: i8* %a, i8* %p.next
196+
; CHECK: NoAlias: i8* %a, i8* %p.next
197197
; CHECK: MayAlias: i8* %p, i8* %p.next
198198
define void @dynamic_offset(i1 %c, i8* noalias %p.base) {
199199
entry:

llvm/test/CodeGen/Thumb2/mve-float32regloops.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,12 +1439,12 @@ define arm_aapcs_vfpcc void @arm_biquad_cascade_stereo_df2T_f32(%struct.arm_biqu
14391439
; CHECK-NEXT: @ => This Inner Loop Header: Depth=2
14401440
; CHECK-NEXT: vldrw.u32 q4, [r1, q0, uxtw #2]
14411441
; CHECK-NEXT: vldrw.u32 q5, [r4, q0, uxtw #2]
1442+
; CHECK-NEXT: vldrw.u32 q3, [sp, #8]
14421443
; CHECK-NEXT: adds r1, #8
14431444
; CHECK-NEXT: vfma.f32 q5, q4, r5
1445+
; CHECK-NEXT: vfma.f32 q3, q5, q2
14441446
; CHECK-NEXT: vstmia r7, {s20, s21}
14451447
; CHECK-NEXT: adds r7, #8
1446-
; CHECK-NEXT: vldrw.u32 q3, [sp, #8]
1447-
; CHECK-NEXT: vfma.f32 q3, q5, q2
14481448
; CHECK-NEXT: vfma.f32 q3, q4, q1
14491449
; CHECK-NEXT: vstrw.32 q3, [r4]
14501450
; CHECK-NEXT: le lr, .LBB17_3

0 commit comments

Comments
 (0)