Skip to content

Commit c23e1b1

Browse files
committed
[Analysis] Bail out for negative offsets in isDereferenceableAndAlignedInLoop
This patch is effectively NFC because it doesn't actually affect any existing tests. It's not possible to even write a test for it, because by accident negative offsets are caught by a subsequent unsigned add overflow check. However, I think it's better to bail out explicitly for negative offsets so that it's more consistent with the unsigned remainder and add calculations.
1 parent d196736 commit c23e1b1

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

llvm/lib/Analysis/Loads.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,13 @@ bool llvm::isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L,
313313
const auto *Offset = dyn_cast<SCEVConstant>(StartS->getOperand(0));
314314
const auto *NewBase = dyn_cast<SCEVUnknown>(StartS->getOperand(1));
315315
if (StartS->getNumOperands() == 2 && Offset && NewBase) {
316+
// The following code below assumes the offset is unsigned, but GEP
317+
// offsets are treated as signed so we can end up with a signed value
318+
// here too. For example, suppose the initial PHI value is (i8 255),
319+
// the offset will be treated as (i8 -1) and sign-extended to (i64 -1).
320+
if (Offset->getAPInt().isNegative())
321+
return false;
322+
316323
// For the moment, restrict ourselves to the case where the offset is a
317324
// multiple of the requested alignment and the base is aligned.
318325
// TODO: generalize if a case found which warrants

llvm/test/Transforms/LoopVectorize/load-deref-pred-align.ll

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ define i8 @test_negative_off(i16 %len, ptr %test_base) {
211211
; CHECK: vector.ph:
212212
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
213213
; CHECK: vector.body:
214-
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
215-
; CHECK-NEXT: [[VEC_PHI:%.*]] = phi <2 x i8> [ zeroinitializer, [[VECTOR_PH]] ], [ [[TMP10:%.*]], [[VECTOR_BODY]] ]
214+
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[PRED_LOAD_CONTINUE2:%.*]] ]
215+
; CHECK-NEXT: [[VEC_PHI:%.*]] = phi <2 x i8> [ zeroinitializer, [[VECTOR_PH]] ], [ [[TMP18:%.*]], [[PRED_LOAD_CONTINUE2]] ]
216216
; CHECK-NEXT: [[DOTCAST:%.*]] = trunc i32 [[INDEX]] to i16
217217
; CHECK-NEXT: [[OFFSET_IDX:%.*]] = add i16 -1000, [[DOTCAST]]
218218
; CHECK-NEXT: [[TMP0:%.*]] = add i16 [[OFFSET_IDX]], 0
@@ -223,20 +223,35 @@ define i8 @test_negative_off(i16 %len, ptr %test_base) {
223223
; CHECK-NEXT: [[TMP5:%.*]] = load i1, ptr [[TMP3]], align 1
224224
; CHECK-NEXT: [[TMP6:%.*]] = insertelement <2 x i1> poison, i1 [[TMP4]], i32 0
225225
; CHECK-NEXT: [[TMP7:%.*]] = insertelement <2 x i1> [[TMP6]], i1 [[TMP5]], i32 1
226-
; CHECK-NEXT: [[TMP8:%.*]] = getelementptr i8, ptr [[ALLOCA]], i16 [[TMP0]]
227-
; CHECK-NEXT: [[TMP9:%.*]] = getelementptr i8, ptr [[TMP8]], i32 0
228-
; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <2 x i8>, ptr [[TMP9]], align 1
229-
; CHECK-NEXT: [[PREDPHI:%.*]] = select <2 x i1> [[TMP7]], <2 x i8> [[WIDE_LOAD]], <2 x i8> zeroinitializer
230-
; CHECK-NEXT: [[TMP10]] = add <2 x i8> [[VEC_PHI]], [[PREDPHI]]
226+
; CHECK-NEXT: [[TMP8:%.*]] = extractelement <2 x i1> [[TMP7]], i32 0
227+
; CHECK-NEXT: br i1 [[TMP8]], label [[PRED_LOAD_IF:%.*]], label [[PRED_LOAD_CONTINUE:%.*]]
228+
; CHECK: pred.load.if:
229+
; CHECK-NEXT: [[TMP9:%.*]] = getelementptr i8, ptr [[ALLOCA]], i16 [[TMP0]]
230+
; CHECK-NEXT: [[TMP10:%.*]] = load i8, ptr [[TMP9]], align 1
231+
; CHECK-NEXT: [[TMP11:%.*]] = insertelement <2 x i8> poison, i8 [[TMP10]], i32 0
232+
; CHECK-NEXT: br label [[PRED_LOAD_CONTINUE]]
233+
; CHECK: pred.load.continue:
234+
; CHECK-NEXT: [[TMP12:%.*]] = phi <2 x i8> [ poison, [[VECTOR_BODY]] ], [ [[TMP11]], [[PRED_LOAD_IF]] ]
235+
; CHECK-NEXT: [[TMP13:%.*]] = extractelement <2 x i1> [[TMP7]], i32 1
236+
; CHECK-NEXT: br i1 [[TMP13]], label [[PRED_LOAD_IF1:%.*]], label [[PRED_LOAD_CONTINUE2]]
237+
; CHECK: pred.load.if1:
238+
; CHECK-NEXT: [[TMP14:%.*]] = getelementptr i8, ptr [[ALLOCA]], i16 [[TMP1]]
239+
; CHECK-NEXT: [[TMP15:%.*]] = load i8, ptr [[TMP14]], align 1
240+
; CHECK-NEXT: [[TMP16:%.*]] = insertelement <2 x i8> [[TMP12]], i8 [[TMP15]], i32 1
241+
; CHECK-NEXT: br label [[PRED_LOAD_CONTINUE2]]
242+
; CHECK: pred.load.continue2:
243+
; CHECK-NEXT: [[TMP17:%.*]] = phi <2 x i8> [ [[TMP12]], [[PRED_LOAD_CONTINUE]] ], [ [[TMP16]], [[PRED_LOAD_IF1]] ]
244+
; CHECK-NEXT: [[PREDPHI:%.*]] = select <2 x i1> [[TMP7]], <2 x i8> [[TMP17]], <2 x i8> zeroinitializer
245+
; CHECK-NEXT: [[TMP18]] = add <2 x i8> [[VEC_PHI]], [[PREDPHI]]
231246
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 2
232-
; CHECK-NEXT: [[TMP11:%.*]] = icmp eq i32 [[INDEX_NEXT]], 12
233-
; CHECK-NEXT: br i1 [[TMP11]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
247+
; CHECK-NEXT: [[TMP19:%.*]] = icmp eq i32 [[INDEX_NEXT]], 12
248+
; CHECK-NEXT: br i1 [[TMP19]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
234249
; CHECK: middle.block:
235-
; CHECK-NEXT: [[TMP12:%.*]] = call i8 @llvm.vector.reduce.add.v2i8(<2 x i8> [[TMP10]])
250+
; CHECK-NEXT: [[TMP20:%.*]] = call i8 @llvm.vector.reduce.add.v2i8(<2 x i8> [[TMP18]])
236251
; CHECK-NEXT: br i1 true, label [[LOOP_EXIT:%.*]], label [[SCALAR_PH]]
237252
; CHECK: scalar.ph:
238253
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i16 [ -988, [[MIDDLE_BLOCK]] ], [ -1000, [[ENTRY:%.*]] ]
239-
; CHECK-NEXT: [[BC_MERGE_RDX:%.*]] = phi i8 [ [[TMP12]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY]] ]
254+
; CHECK-NEXT: [[BC_MERGE_RDX:%.*]] = phi i8 [ [[TMP20]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY]] ]
240255
; CHECK-NEXT: br label [[LOOP:%.*]]
241256
; CHECK: loop:
242257
; CHECK-NEXT: [[IV:%.*]] = phi i16 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], [[LATCH:%.*]] ]
@@ -255,7 +270,7 @@ define i8 @test_negative_off(i16 %len, ptr %test_base) {
255270
; CHECK-NEXT: [[EXIT:%.*]] = icmp ugt i16 [[IV]], -990
256271
; CHECK-NEXT: br i1 [[EXIT]], label [[LOOP_EXIT]], label [[LOOP]], !llvm.loop [[LOOP7:![0-9]+]]
257272
; CHECK: loop_exit:
258-
; CHECK-NEXT: [[ACCUM_NEXT_LCSSA:%.*]] = phi i8 [ [[ACCUM_NEXT]], [[LATCH]] ], [ [[TMP12]], [[MIDDLE_BLOCK]] ]
273+
; CHECK-NEXT: [[ACCUM_NEXT_LCSSA:%.*]] = phi i8 [ [[ACCUM_NEXT]], [[LATCH]] ], [ [[TMP20]], [[MIDDLE_BLOCK]] ]
259274
; CHECK-NEXT: ret i8 [[ACCUM_NEXT_LCSSA]]
260275
;
261276
entry:

0 commit comments

Comments
 (0)