Skip to content

Commit 1e1ef6c

Browse files
committed
[VectorCombine] Fix scalarizeExtExtract for big-endian
1 parent 827d775 commit 1e1ef6c

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,12 +2011,19 @@ bool VectorCombine::scalarizeExtExtract(Instruction &I) {
20112011
IntegerType::get(SrcTy->getContext(), DL->getTypeSizeInBits(SrcTy)));
20122012
uint64_t SrcEltSizeInBits = DL->getTypeSizeInBits(SrcTy->getElementType());
20132013
uint64_t EltBitMask = (1ull << SrcEltSizeInBits) - 1;
2014+
uint64_t TotalBits = DL->getTypeSizeInBits(SrcTy);
2015+
Type *PackedTy = IntegerType::get(SrcTy->getContext(), TotalBits);
2016+
Value *Mask = ConstantInt::get(PackedTy, EltBitMask);
20142017
for (User *U : Ext->users()) {
20152018
auto *Extract = cast<ExtractElementInst>(U);
20162019
uint64_t Idx =
20172020
cast<ConstantInt>(Extract->getIndexOperand())->getZExtValue();
2018-
Value *LShr = Builder.CreateLShr(ScalarV, Idx * SrcEltSizeInBits);
2019-
Value *And = Builder.CreateAnd(LShr, EltBitMask);
2021+
uint64_t ShiftAmt = DL->isBigEndian()
2022+
? (TotalBits - SrcEltSizeInBits - Idx * SrcEltSizeInBits)
2023+
: (Idx * SrcEltSizeInBits);
2024+
Value *ShAmtVal = ConstantInt::get(PackedTy, ShiftAmt);
2025+
Value *LShr = Builder.CreateLShr(ScalarV, ShAmtVal);
2026+
Value *And = Builder.CreateAnd(LShr, Mask);
20202027
U->replaceAllUsesWith(And);
20212028
}
20222029
return true;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; RUN: opt -passes='vector-combine,dce' -S -mtriple=aarch64-unknown-linux-gnu %s -o - | FileCheck %s --check-prefix=LE
2+
; RUN: opt -passes='vector-combine,dce' -S -mtriple=powerpc64-ibm-aix-xcoff %s -o - | FileCheck %s --check-prefix=BE
3+
4+
define i64 @g(<8 x i8> %v) {
5+
%z = zext <8 x i8> %v to <8 x i64>
6+
%e0 = extractelement <8 x i64> %z, i32 0
7+
%e7 = extractelement <8 x i64> %z, i32 7
8+
%sum = add i64 %e0, %e7
9+
ret i64 %sum
10+
}
11+
12+
; LE-LABEL: @g(
13+
; LE: bitcast <8 x i8> %{{.*}} to i64
14+
; LE: lshr i64 %{{.*}}, 56
15+
; LE: and i64 %{{.*}}, 255
16+
; LE-NOT: extractelement
17+
18+
; BE-LABEL: @g(
19+
; BE: bitcast <8 x i8> %{{.*}} to i64
20+
; BE: and i64 %{{.*}}, 255
21+
; BE: lshr i64 %{{.*}}, 56
22+
; BE-NOT: extractelement
23+

0 commit comments

Comments
 (0)