Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 8 additions & 2 deletions llvm/lib/Transforms/Vectorize/VectorCombine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2011,12 +2011,18 @@ bool VectorCombine::scalarizeExtExtract(Instruction &I) {
IntegerType::get(SrcTy->getContext(), DL->getTypeSizeInBits(SrcTy)));
uint64_t SrcEltSizeInBits = DL->getTypeSizeInBits(SrcTy->getElementType());
uint64_t EltBitMask = (1ull << SrcEltSizeInBits) - 1;
uint64_t TotalBits = DL->getTypeSizeInBits(SrcTy);
Type *PackedTy = IntegerType::get(SrcTy->getContext(), TotalBits);
Value *Mask = ConstantInt::get(PackedTy, EltBitMask);
for (User *U : Ext->users()) {
auto *Extract = cast<ExtractElementInst>(U);
uint64_t Idx =
cast<ConstantInt>(Extract->getIndexOperand())->getZExtValue();
Value *LShr = Builder.CreateLShr(ScalarV, Idx * SrcEltSizeInBits);
Value *And = Builder.CreateAnd(LShr, EltBitMask);
uint64_t ShiftAmt = DL->isBigEndian()
? (TotalBits - SrcEltSizeInBits - Idx * SrcEltSizeInBits)
: (Idx * SrcEltSizeInBits);
Value *LShr = Builder.CreateLShr(ScalarV, ShiftAmt);
Value *And = Builder.CreateAnd(LShr, Mask);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-format this bit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done thanks

U->replaceAllUsesWith(And);
}
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
; RUN: opt -passes='vector-combine,dce' -S -mtriple=aarch64-unknown-linux-gnu %s -o - | FileCheck %s --check-prefix=LE
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need the dce pass?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not strictly needed but I figured that using dce would make the test output cleaner since the printed IR would only show the bitcast/lshr/and we care about. I don't mind removing it if you prefer to just have vector combine though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes please just show the effects of vectorcombe

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just made the change thanks

; RUN: opt -passes='vector-combine,dce' -S -mtriple=aarch64_be-unknown-linux-gnu %s -o - | FileCheck %s --check-prefix=BE

define i64 @g(<8 x i8> %v) {
%z = zext <8 x i8> %v to <8 x i64>
%e0 = extractelement <8 x i64> %z, i32 0
%e7 = extractelement <8 x i64> %z, i32 7
%sum = add i64 %e0, %e7
ret i64 %sum
}

; LE-LABEL: @g(
; LE: bitcast <8 x i8> %{{.*}} to i64
; LE: lshr i64 %{{.*}}, 56
; LE: and i64 %{{.*}}, 255
; LE-NOT: extractelement

; BE-LABEL: @g(
; BE: bitcast <8 x i8> %{{.*}} to i64
; BE: and i64 %{{.*}}, 255
; BE: lshr i64 %{{.*}}, 56
; BE-NOT: extractelement

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
; RUN: opt -passes='vector-combine,dce' -S -mtriple=aarch64-unknown-linux-gnu %s -o - | FileCheck %s --check-prefix=LE
; RUN: opt -passes='vector-combine,dce' -S -mtriple=powerpc64-ibm-aix-xcoff %s -o - | FileCheck %s --check-prefix=BE

define i64 @g(<8 x i8> %v) {
%z = zext <8 x i8> %v to <8 x i64>
%e0 = extractelement <8 x i64> %z, i32 0
%e7 = extractelement <8 x i64> %z, i32 7
%sum = add i64 %e0, %e7
ret i64 %sum
}

; LE-LABEL: @g(
; LE: bitcast <8 x i8> %{{.*}} to i64
; LE: lshr i64 %{{.*}}, 56
; LE: and i64 %{{.*}}, 255
; LE-NOT: extractelement

; BE-LABEL: @g(
; BE: bitcast <8 x i8> %{{.*}} to i64
; BE: and i64 %{{.*}}, 255
; BE: lshr i64 %{{.*}}, 56
; BE-NOT: extractelement

Loading