Skip to content

Commit 8965e3f

Browse files
committed
Compare with zero instead
1 parent 439ea0e commit 8965e3f

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3270,21 +3270,24 @@ void VPAliasLaneMaskRecipe::execute(VPTransformState &State) {
32703270

32713271
Value *Diff = Builder.CreateSub(SourceValue, SinkValue, "sub.diff");
32723272
auto *Type = Diff->getType();
3273+
Value *Zero = ConstantInt::get(Type, 0);
32733274
if (!WriteAfterRead)
32743275
Diff = Builder.CreateIntrinsic(
32753276
Intrinsic::abs, {Type},
3276-
{Diff, ConstantInt::getFalse(Builder.getInt1Ty())});
3277-
Value *MemEltSize = ConstantInt::get(Type, ElementSize);
3278-
Value *DiffDiv = Builder.CreateSDiv(Diff, MemEltSize, "diff");
3279-
// If the difference is negative then some elements may alias
3280-
Value *Cmp = Builder.CreateICmp(CmpInst::Predicate::ICMP_SLE, DiffDiv,
3281-
ConstantInt::get(Type, 0), "neg.compare");
3277+
{Diff, ConstantInt::getFalse(Builder.getInt1Ty())}, nullptr, "sub.abs");
3278+
3279+
Value *DiffDiv = Builder.CreateSDiv(Diff, Zero, "diff");
3280+
// If the difference is positive then some elements may alias
3281+
auto CmpCode = WriteAfterRead ? CmpInst::Predicate::ICMP_SLE
3282+
: CmpInst::Predicate::ICMP_EQ;
3283+
Value *Cmp = Builder.CreateICmp(CmpCode, DiffDiv, Zero, "neg.compare");
3284+
32823285
// Splat the compare result then OR it with a lane mask
32833286
Value *Splat = Builder.CreateVectorSplat(State.VF, Cmp);
32843287
Value *DiffMask = Builder.CreateIntrinsic(
32853288
Intrinsic::get_active_lane_mask,
3286-
{VectorType::get(Builder.getInt1Ty(), State.VF), Type},
3287-
{ConstantInt::get(Type, 0), DiffDiv}, nullptr, "ptr.diff.lane.mask");
3289+
{VectorType::get(Builder.getInt1Ty(), State.VF), Type}, {Zero, DiffDiv},
3290+
nullptr, "ptr.diff.lane.mask");
32883291
Value *Or = Builder.CreateBinOp(Instruction::BinaryOps::Or, DiffMask, Splat);
32893292
State.set(this, Or, /*IsScalar=*/false);
32903293
}

llvm/test/Transforms/LoopVectorize/AArch64/alias_mask.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ define dso_local void @alias_mask(ptr noalias %a, ptr %b, ptr %c, i32 %n) {
3030
; CHECK-NEXT: [[TMP6:%.*]] = call i64 @llvm.vscale.i64()
3131
; CHECK-NEXT: [[TMP7:%.*]] = mul i64 [[TMP6]], 16
3232
; CHECK-NEXT: [[SUB_DIFF:%.*]] = sub i64 [[B4]], [[C3]]
33-
; CHECK-NEXT: [[DIFF:%.*]] = sdiv i64 [[SUB_DIFF]], 1
33+
; CHECK-NEXT: [[DIFF:%.*]] = sdiv i64 [[SUB_DIFF]], 0
3434
; CHECK-NEXT: [[NEG_COMPARE:%.*]] = icmp sle i64 [[DIFF]], 0
3535
; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <vscale x 16 x i1> poison, i1 [[NEG_COMPARE]], i64 0
3636
; CHECK-NEXT: [[DOTSPLAT:%.*]] = shufflevector <vscale x 16 x i1> [[DOTSPLATINSERT]], <vscale x 16 x i1> poison, <vscale x 16 x i32> zeroinitializer

llvm/test/Transforms/LoopVectorize/AArch64/induction-costs-sve.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ define void @iv_casts(ptr %dst, ptr %src, i32 %x, i64 %N) #0 {
159159
; PRED-NEXT: [[TMP9:%.*]] = call i64 @llvm.vscale.i64()
160160
; PRED-NEXT: [[TMP10:%.*]] = mul i64 [[TMP9]], 8
161161
; PRED-NEXT: [[SUB_DIFF:%.*]] = sub i64 [[SRC2]], [[DST1]]
162-
; PRED-NEXT: [[DIFF:%.*]] = sdiv i64 [[SUB_DIFF]], 1
162+
; PRED-NEXT: [[DIFF:%.*]] = sdiv i64 [[SUB_DIFF]], 0
163163
; PRED-NEXT: [[NEG_COMPARE:%.*]] = icmp sle i64 [[DIFF]], 0
164164
; PRED-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <vscale x 8 x i1> poison, i1 [[NEG_COMPARE]], i64 0
165165
; PRED-NEXT: [[DOTSPLAT:%.*]] = shufflevector <vscale x 8 x i1> [[DOTSPLATINSERT]], <vscale x 8 x i1> poison, <vscale x 8 x i32> zeroinitializer

0 commit comments

Comments
 (0)