Skip to content

[VectorCombine] Don't shrink lshr if the shamt is not less than bitwidth #108705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions llvm/lib/Transforms/Vectorize/VectorCombine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2597,11 +2597,19 @@ bool VectorCombine::shrinkType(llvm::Instruction &I) {
auto *SmallTy = cast<FixedVectorType>(ZExted->getType());
unsigned BW = SmallTy->getElementType()->getPrimitiveSizeInBits();

// Check that the expression overall uses at most the same number of bits as
// ZExted
KnownBits KB = computeKnownBits(&I, *DL);
if (KB.countMaxActiveBits() > BW)
return false;
if (I.getOpcode() == Instruction::LShr) {
// Check that the shift amount is less than the number of bits in the
// smaller type. Otherwise, the smaller lshr will return a poison value.
KnownBits ShAmtKB = computeKnownBits(I.getOperand(1), *DL);
if (ShAmtKB.getMaxValue().uge(BW))
return false;
} else {
// Check that the expression overall uses at most the same number of bits as
// ZExted
KnownBits KB = computeKnownBits(&I, *DL);
if (KB.countMaxActiveBits() > BW)
return false;
}

// Calculate costs of leaving current IR as it is and moving ZExt operation
// later, along with adding truncates if needed
Expand All @@ -2628,7 +2636,7 @@ bool VectorCombine::shrinkType(llvm::Instruction &I) {
return false;

// Check if we can propagate ZExt through its other users
KB = computeKnownBits(UI, *DL);
KnownBits KB = computeKnownBits(UI, *DL);
if (KB.countMaxActiveBits() > BW)
return false;

Expand Down
13 changes: 13 additions & 0 deletions llvm/test/Transforms/VectorCombine/AArch64/shrink-types.ll
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,17 @@ vector.body:
ret i32 %2
}

define <2 x i32> @pr108698(<2 x i64> %x, <2 x i32> %y) {
; CHECK-LABEL: @pr108698(
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i64> [[X:%.*]], zeroinitializer
; CHECK-NEXT: [[EXT:%.*]] = zext <2 x i1> [[CMP]] to <2 x i32>
; CHECK-NEXT: [[LSHR:%.*]] = lshr <2 x i32> [[EXT]], [[Y:%.*]]
; CHECK-NEXT: ret <2 x i32> [[LSHR]]
;
%cmp = icmp eq <2 x i64> %x, zeroinitializer
%ext = zext <2 x i1> %cmp to <2 x i32>
%lshr = lshr <2 x i32> %ext, %y
ret <2 x i32> %lshr
}

declare i32 @llvm.vector.reduce.add.v16i32(<16 x i32>)
Loading