diff options
author | Florian Hahn <flo@fhahn.com> | 2022-04-07 23:36:17 +0200 |
---|---|---|
committer | Florian Hahn <flo@fhahn.com> | 2022-04-25 13:18:22 +0100 |
commit | 0d2efbb8b82c13ea6e4aef727c5360eea6679605 (patch) | |
tree | 9fafe9de22501095c0cd386f6a61c8c98d91a0a0 | |
parent | e7a9fd4f57d6c51c2c97a545da6552c698eea347 (diff) | |
download | llvm-0d2efbb8b82c13ea6e4aef727c5360eea6679605.tar.gz |
[LV] Always use add to add scalar iv and (startidx + step) for ints.
In the integer case, step will be negative and InductionOpCode will be
Sub for inductions counting down.
By using the InductionOpCode for integers, we would incorrectly subtract
a negative value, when it should be added instead.
This fixes #54427 on the 14.x branch.
-rw-r--r-- | llvm/lib/Transforms/Vectorize/LoopVectorize.cpp | 16 | ||||
-rw-r--r-- | llvm/test/Transforms/LoopVectorize/induction-unroll-novec.ll | 4 |
2 files changed, 10 insertions, 10 deletions
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp index e1cc7946073e..93eaed655130 100644 --- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -2544,19 +2544,19 @@ void InnerLoopVectorizer::widenIntOrFpInduction( Type *ScalarTy = IntegerType::get(ScalarIV->getContext(), Step->getType()->getScalarSizeInBits()); - Instruction::BinaryOps IncOp = ID.getInductionOpcode(); - if (IncOp == Instruction::BinaryOpsEnd) - IncOp = Instruction::Add; for (unsigned Part = 0; Part < UF; ++Part) { Value *StartIdx = ConstantInt::get(ScalarTy, Part); - Instruction::BinaryOps MulOp = Instruction::Mul; + Value *EntryPart; if (Step->getType()->isFloatingPointTy()) { StartIdx = Builder.CreateUIToFP(StartIdx, Step->getType()); - MulOp = Instruction::FMul; + Value *MulOp = Builder.CreateFMul(StartIdx, Step); + EntryPart = Builder.CreateBinOp(ID.getInductionOpcode(), ScalarIV, + MulOp, "induction"); + } else { + EntryPart = Builder.CreateAdd( + ScalarIV, Builder.CreateMul(StartIdx, Step), "induction"); + EntryPart->dump(); } - - Value *Mul = Builder.CreateBinOp(MulOp, StartIdx, Step); - Value *EntryPart = Builder.CreateBinOp(IncOp, ScalarIV, Mul, "induction"); State.set(Def, EntryPart, Part); if (Trunc) { assert(!Step->getType()->isFloatingPointTy() && diff --git a/llvm/test/Transforms/LoopVectorize/induction-unroll-novec.ll b/llvm/test/Transforms/LoopVectorize/induction-unroll-novec.ll index a4e595e98fb7..5266b3f9eaec 100644 --- a/llvm/test/Transforms/LoopVectorize/induction-unroll-novec.ll +++ b/llvm/test/Transforms/LoopVectorize/induction-unroll-novec.ll @@ -15,9 +15,9 @@ define void @test_nonconst_start_and_step(i32* %dst, i32 %start, i32 %step, i64 ; CHECK-NEXT: [[TMP3:%.*]] = mul i32 [[TMP2]], [[NEG_STEP]] ; CHECK-NEXT: [[OFFSET_IDX:%.*]] = add i32 %start, [[TMP3]] ; CHECK-NEXT: [[TMP4:%.*]] = mul i32 0, [[NEG_STEP]] -; CHECK-NEXT: [[INDUCTION:%.*]] = sub i32 [[OFFSET_IDX]], [[TMP4]] +; CHECK-NEXT: [[INDUCTION:%.*]] = add i32 [[OFFSET_IDX]], [[TMP4]] ; CHECK-NEXT: [[TMP5:%.*]] = mul i32 1, [[NEG_STEP]] -; CHECK-NEXT: [[INDUCTION2:%.*]] = sub i32 [[OFFSET_IDX]], [[TMP5]] +; CHECK-NEXT: [[INDUCTION2:%.*]] = add i32 [[OFFSET_IDX]], [[TMP5]] ; CHECK-NEXT: [[TMP6:%.*]] = sub nsw i32 [[INDUCTION]], %step ; CHECK-NEXT: [[TMP7:%.*]] = sub nsw i32 [[INDUCTION2]], %step ; CHECK-NEXT: [[TMP8:%.*]] = getelementptr inbounds i32, i32* [[DST:%.*]], i64 [[INDUCTION3]] |