Skip to content

Commit 9a1e478

Browse files
committed
[Loads] Apply loop guards to maximum pointer difference.
Applying loop guards to MaxPtrDiff can improve results in some cases.
1 parent a65aa32 commit 9a1e478

File tree

2 files changed

+9
-24
lines changed

2 files changed

+9
-24
lines changed

llvm/lib/Analysis/Loads.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,10 @@ bool llvm::isDereferenceableAndAlignedInLoop(
355355
const SCEV *PtrDiff = SE.getMinusSCEV(AccessEnd, AccessStart);
356356
if (isa<SCEVCouldNotCompute>(PtrDiff))
357357
return false;
358-
APInt MaxPtrDiff = SE.getUnsignedRangeMax(PtrDiff);
358+
ScalarEvolution::LoopGuards LoopGuards =
359+
ScalarEvolution::LoopGuards::collect(AddRec->getLoop(), SE);
360+
APInt MaxPtrDiff =
361+
SE.getUnsignedRangeMax(SE.applyLoopGuards(PtrDiff, LoopGuards));
359362

360363
Value *Base = nullptr;
361364
APInt AccessSize;

llvm/test/Transforms/LoopVectorize/dereferenceable-info-from-assumption-constant-size-needs-loop-guards.ll

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -167,31 +167,13 @@ define void @loop_guard_on_trip_count_needed_to_prove_dereferenceable(i32 %x, pt
167167
; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 [[N_EXT]], [[N_MOD_VF]]
168168
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
169169
; CHECK: [[VECTOR_BODY]]:
170-
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[PRED_LOAD_CONTINUE2:.*]] ]
170+
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
171171
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr i32, ptr [[B]], i64 [[INDEX]]
172172
; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <2 x i32>, ptr [[TMP0]], align 4
173-
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne <2 x i32> [[WIDE_LOAD]], zeroinitializer
174-
; CHECK-NEXT: [[TMP2:%.*]] = extractelement <2 x i1> [[TMP1]], i32 0
175-
; CHECK-NEXT: br i1 [[TMP2]], label %[[PRED_LOAD_IF:.*]], label %[[PRED_LOAD_CONTINUE:.*]]
176-
; CHECK: [[PRED_LOAD_IF]]:
177-
; CHECK-NEXT: [[TMP3:%.*]] = add i64 [[INDEX]], 0
178-
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr i32, ptr [[A]], i64 [[TMP3]]
179-
; CHECK-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP4]], align 4
180-
; CHECK-NEXT: [[TMP6:%.*]] = insertelement <2 x i32> poison, i32 [[TMP5]], i32 0
181-
; CHECK-NEXT: br label %[[PRED_LOAD_CONTINUE]]
182-
; CHECK: [[PRED_LOAD_CONTINUE]]:
183-
; CHECK-NEXT: [[TMP7:%.*]] = phi <2 x i32> [ poison, %[[VECTOR_BODY]] ], [ [[TMP6]], %[[PRED_LOAD_IF]] ]
184-
; CHECK-NEXT: [[TMP8:%.*]] = extractelement <2 x i1> [[TMP1]], i32 1
185-
; CHECK-NEXT: br i1 [[TMP8]], label %[[PRED_LOAD_IF1:.*]], label %[[PRED_LOAD_CONTINUE2]]
186-
; CHECK: [[PRED_LOAD_IF1]]:
187-
; CHECK-NEXT: [[TMP9:%.*]] = add i64 [[INDEX]], 1
188-
; CHECK-NEXT: [[TMP10:%.*]] = getelementptr i32, ptr [[A]], i64 [[TMP9]]
189-
; CHECK-NEXT: [[TMP11:%.*]] = load i32, ptr [[TMP10]], align 4
190-
; CHECK-NEXT: [[TMP12:%.*]] = insertelement <2 x i32> [[TMP7]], i32 [[TMP11]], i32 1
191-
; CHECK-NEXT: br label %[[PRED_LOAD_CONTINUE2]]
192-
; CHECK: [[PRED_LOAD_CONTINUE2]]:
193-
; CHECK-NEXT: [[TMP13:%.*]] = phi <2 x i32> [ [[TMP7]], %[[PRED_LOAD_CONTINUE]] ], [ [[TMP12]], %[[PRED_LOAD_IF1]] ]
194-
; CHECK-NEXT: [[PREDPHI:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[TMP13]], <2 x i32> [[WIDE_LOAD]]
173+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i32> [[WIDE_LOAD]], zeroinitializer
174+
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i32, ptr [[A]], i64 [[INDEX]]
175+
; CHECK-NEXT: [[WIDE_LOAD1:%.*]] = load <2 x i32>, ptr [[TMP2]], align 4
176+
; CHECK-NEXT: [[PREDPHI:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[WIDE_LOAD]], <2 x i32> [[WIDE_LOAD1]]
195177
; CHECK-NEXT: [[TMP14:%.*]] = getelementptr inbounds i32, ptr [[C]], i64 [[INDEX]]
196178
; CHECK-NEXT: store <2 x i32> [[PREDPHI]], ptr [[TMP14]], align 4
197179
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 2

0 commit comments

Comments
 (0)