Skip to content

Commit 23ac9f0

Browse files
committed
[Loads] Support dereference for non-constant offset
Improve isDereferenceableAndAlignedInLoop API to consider non-constant offset feeding into the first access in the loop.
1 parent 5923111 commit 23ac9f0

File tree

2 files changed

+25
-45
lines changed

2 files changed

+25
-45
lines changed

llvm/lib/Analysis/Loads.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -361,29 +361,29 @@ bool llvm::isDereferenceableAndAlignedInLoop(
361361
AccessSize = MaxPtrDiff;
362362
AccessSizeSCEV = PtrDiff;
363363
} else if (auto *MinAdd = dyn_cast<SCEVAddExpr>(AccessStart)) {
364-
if (MinAdd->getNumOperands() != 2)
365-
return false;
364+
const auto *NewBase = dyn_cast<SCEVUnknown>(SE.getPointerBase(MinAdd));
365+
const auto *OffsetSCEV = SE.removePointerBase(MinAdd);
366366

367-
const auto *Offset = dyn_cast<SCEVConstant>(MinAdd->getOperand(0));
368-
const auto *NewBase = dyn_cast<SCEVUnknown>(MinAdd->getOperand(1));
369-
if (!Offset || !NewBase)
367+
if (!OffsetSCEV || !NewBase)
370368
return false;
371369

372-
// The following code below assumes the offset is unsigned, but GEP
373-
// offsets are treated as signed so we can end up with a signed value
374-
// here too. For example, suppose the initial PHI value is (i8 255),
375-
// the offset will be treated as (i8 -1) and sign-extended to (i64 -1).
376-
if (Offset->getAPInt().isNegative())
370+
if (!SE.isKnownNonNegative(OffsetSCEV))
377371
return false;
378372

379373
// For the moment, restrict ourselves to the case where the offset is a
380374
// multiple of the requested alignment and the base is aligned.
381375
// TODO: generalize if a case found which warrants
382-
if (Offset->getAPInt().urem(Alignment.value()) != 0)
376+
auto *OffsetSCEVTy = OffsetSCEV->getType();
377+
if (!SE.isKnownPredicate(
378+
ICmpInst::ICMP_EQ,
379+
SE.getURemExpr(OffsetSCEV,
380+
SE.getConstant(OffsetSCEVTy, Alignment.value())),
381+
SE.getZero(OffsetSCEVTy)))
383382
return false;
384-
385-
AccessSize = MaxPtrDiff + Offset->getAPInt();
386-
AccessSizeSCEV = SE.getAddExpr(PtrDiff, Offset);
383+
AccessSizeSCEV = SE.getAddExpr(PtrDiff, OffsetSCEV);
384+
const auto *Offset = dyn_cast<SCEVConstant>(OffsetSCEV);
385+
AccessSize = MaxPtrDiff + (Offset ? Offset->getAPInt()
386+
: SE.getUnsignedRangeMax(OffsetSCEV));
387387
Base = NewBase->getValue();
388388
} else
389389
return false;

llvm/test/Transforms/LoopVectorize/dereferenceable-info-from-assumption-variable-size.ll

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -562,38 +562,19 @@ define void @deref_assumption_loop_access_start_variable(i8 %v, ptr noundef %P,
562562
; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[IV_START]], [[N_VEC]]
563563
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
564564
; CHECK: [[VECTOR_BODY]]:
565-
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[PRED_LOAD_CONTINUE2:.*]] ]
565+
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
566566
; CHECK-NEXT: [[OFFSET_IDX:%.*]] = add i64 [[IV_START]], [[INDEX]]
567-
; CHECK-NEXT: [[TMP6:%.*]] = getelementptr inbounds i32, ptr [[B]], i64 [[OFFSET_IDX]]
568-
; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <2 x i32>, ptr [[TMP6]], align 1
569-
; CHECK-NEXT: [[TMP8:%.*]] = icmp sge <2 x i32> [[WIDE_LOAD]], zeroinitializer
570-
; CHECK-NEXT: [[TMP4:%.*]] = xor <2 x i1> [[TMP8]], splat (i1 true)
571-
; CHECK-NEXT: [[TMP5:%.*]] = extractelement <2 x i1> [[TMP4]], i32 0
572-
; CHECK-NEXT: br i1 [[TMP5]], label %[[PRED_LOAD_IF:.*]], label %[[PRED_LOAD_CONTINUE:.*]]
573-
; CHECK: [[PRED_LOAD_IF]]:
574-
; CHECK-NEXT: [[TMP16:%.*]] = add i64 [[OFFSET_IDX]], 0
575-
; CHECK-NEXT: [[TMP7:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 [[TMP16]]
576-
; CHECK-NEXT: [[TMP19:%.*]] = load i32, ptr [[TMP7]], align 1
577-
; CHECK-NEXT: [[TMP9:%.*]] = insertelement <2 x i32> poison, i32 [[TMP19]], i32 0
578-
; CHECK-NEXT: br label %[[PRED_LOAD_CONTINUE]]
579-
; CHECK: [[PRED_LOAD_CONTINUE]]:
580-
; CHECK-NEXT: [[TMP10:%.*]] = phi <2 x i32> [ poison, %[[VECTOR_BODY]] ], [ [[TMP9]], %[[PRED_LOAD_IF]] ]
581-
; CHECK-NEXT: [[TMP11:%.*]] = extractelement <2 x i1> [[TMP4]], i32 1
582-
; CHECK-NEXT: br i1 [[TMP11]], label %[[PRED_LOAD_IF1:.*]], label %[[PRED_LOAD_CONTINUE2]]
583-
; CHECK: [[PRED_LOAD_IF1]]:
584-
; CHECK-NEXT: [[TMP12:%.*]] = add i64 [[OFFSET_IDX]], 1
585-
; CHECK-NEXT: [[TMP13:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 [[TMP12]]
586-
; CHECK-NEXT: [[TMP14:%.*]] = load i32, ptr [[TMP13]], align 1
587-
; CHECK-NEXT: [[TMP15:%.*]] = insertelement <2 x i32> [[TMP10]], i32 [[TMP14]], i32 1
588-
; CHECK-NEXT: br label %[[PRED_LOAD_CONTINUE2]]
589-
; CHECK: [[PRED_LOAD_CONTINUE2]]:
590-
; CHECK-NEXT: [[WIDE_LOAD1:%.*]] = phi <2 x i32> [ [[TMP10]], %[[PRED_LOAD_CONTINUE]] ], [ [[TMP15]], %[[PRED_LOAD_IF1]] ]
591-
; CHECK-NEXT: [[PREDPHI:%.*]] = select <2 x i1> [[TMP8]], <2 x i32> [[WIDE_LOAD]], <2 x i32> [[WIDE_LOAD1]]
592-
; CHECK-NEXT: [[TMP17:%.*]] = getelementptr inbounds i32, ptr [[C]], i64 [[OFFSET_IDX]]
593-
; CHECK-NEXT: store <2 x i32> [[PREDPHI]], ptr [[TMP17]], align 1
567+
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i32, ptr [[A]], i64 [[OFFSET_IDX]]
568+
; CHECK-NEXT: [[TMP7:%.*]] = getelementptr inbounds i32, ptr [[B]], i64 [[OFFSET_IDX]]
569+
; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <2 x i32>, ptr [[TMP7]], align 1
570+
; CHECK-NEXT: [[TMP4:%.*]] = icmp sge <2 x i32> [[WIDE_LOAD]], zeroinitializer
571+
; CHECK-NEXT: [[WIDE_LOAD1:%.*]] = load <2 x i32>, ptr [[TMP2]], align 1
572+
; CHECK-NEXT: [[PREDPHI:%.*]] = select <2 x i1> [[TMP4]], <2 x i32> [[WIDE_LOAD]], <2 x i32> [[WIDE_LOAD1]]
573+
; CHECK-NEXT: [[TMP5:%.*]] = getelementptr inbounds i32, ptr [[C]], i64 [[OFFSET_IDX]]
574+
; CHECK-NEXT: store <2 x i32> [[PREDPHI]], ptr [[TMP5]], align 1
594575
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 2
595-
; CHECK-NEXT: [[TMP18:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
596-
; CHECK-NEXT: br i1 [[TMP18]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP14:![0-9]+]]
576+
; CHECK-NEXT: [[TMP6:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
577+
; CHECK-NEXT: br i1 [[TMP6]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP14:![0-9]+]]
597578
; CHECK: [[MIDDLE_BLOCK]]:
598579
; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[TMP3]], [[N_VEC]]
599580
; CHECK-NEXT: br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
@@ -620,7 +601,6 @@ define void @deref_assumption_loop_access_start_variable(i8 %v, ptr noundef %P,
620601
; CHECK: [[EXIT]]:
621602
; CHECK-NEXT: ret void
622603
;
623-
624604
entry:
625605
%a = getelementptr i8, ptr %P, i64 16
626606
%cmp = icmp slt i64 %iv.start, %N

0 commit comments

Comments
 (0)