Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 89881d2

Browse files
committedMar 21, 2024
[VectorCombine] foldBitcastShuffle - peek through any residual bitcasts before creating a new bitcast on top
Encountered while working on #67803, this helps prevents cases where the bitcast chains aren't cleared out and we can't perform further combines until after InstCombine/InstSimplify has run. I'm assuming we can't safely put this inside IRBuilderBase.CreateBitCast?
1 parent 15eba9c commit 89881d2

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed
 

‎llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,18 @@ class VectorCombine {
135135
};
136136
} // namespace
137137

138+
/// Return the source operand of a potentially bitcasted value while
139+
/// optionally checking if it has one use. If there is no bitcast or the one
140+
/// use check is not met, return the input value itself.
141+
static Value *peekThroughBitcast(Value *V, bool OneUseOnly = false) {
142+
if (auto *BitCast = dyn_cast<BitCastInst>(V))
143+
if (!OneUseOnly || BitCast->hasOneUse())
144+
return BitCast->getOperand(0);
145+
146+
// V is not a bitcast or V has more than one use and OneUseOnly is true.
147+
return V;
148+
}
149+
138150
static bool canWidenLoad(LoadInst *Load, const TargetTransformInfo &TTI) {
139151
// Do not widen load if atomic/volatile or under asan/hwasan/memtag/tsan.
140152
// The widened load may load data from dirty regions or create data races
@@ -751,8 +763,8 @@ bool VectorCombine::foldBitcastShuffle(Instruction &I) {
751763

752764
// bitcast (shuf V0, V1, MaskC) --> shuf (bitcast V0), (bitcast V1), MaskC'
753765
++NumShufOfBitcast;
754-
Value *CastV0 = Builder.CreateBitCast(V0, NewShuffleTy);
755-
Value *CastV1 = Builder.CreateBitCast(V1, NewShuffleTy);
766+
Value *CastV0 = Builder.CreateBitCast(peekThroughBitcast(V0), NewShuffleTy);
767+
Value *CastV1 = Builder.CreateBitCast(peekThroughBitcast(V1), NewShuffleTy);
756768
Value *Shuf = Builder.CreateShuffleVector(CastV0, CastV1, NewMask);
757769
replaceValue(I, *Shuf);
758770
return true;

‎llvm/test/Transforms/VectorCombine/X86/shuffle-inseltpoison.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ define <2 x i64> @PR35454_1(<2 x i64> %v) {
133133
; SSE-NEXT: ret <2 x i64> [[BC3]]
134134
;
135135
; AVX-LABEL: @PR35454_1(
136-
; AVX-NEXT: [[BC:%.*]] = bitcast <2 x i64> [[V:%.*]] to <4 x i32>
137-
; AVX-NEXT: [[TMP1:%.*]] = bitcast <4 x i32> [[BC]] to <16 x i8>
136+
; AVX-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> [[V:%.*]] to <16 x i8>
138137
; AVX-NEXT: [[BC1:%.*]] = shufflevector <16 x i8> [[TMP1]], <16 x i8> poison, <16 x i32> <i32 12, i32 13, i32 14, i32 15, i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6, i32 7, i32 0, i32 1, i32 2, i32 3>
139138
; AVX-NEXT: [[ADD:%.*]] = shl <16 x i8> [[BC1]], <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
140139
; AVX-NEXT: [[BC2:%.*]] = bitcast <16 x i8> [[ADD]] to <4 x i32>
@@ -164,8 +163,7 @@ define <2 x i64> @PR35454_2(<2 x i64> %v) {
164163
; SSE-NEXT: ret <2 x i64> [[BC3]]
165164
;
166165
; AVX-LABEL: @PR35454_2(
167-
; AVX-NEXT: [[BC:%.*]] = bitcast <2 x i64> [[V:%.*]] to <4 x i32>
168-
; AVX-NEXT: [[TMP1:%.*]] = bitcast <4 x i32> [[BC]] to <8 x i16>
166+
; AVX-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> [[V:%.*]] to <8 x i16>
169167
; AVX-NEXT: [[BC1:%.*]] = shufflevector <8 x i16> [[TMP1]], <8 x i16> poison, <8 x i32> <i32 6, i32 7, i32 4, i32 5, i32 2, i32 3, i32 0, i32 1>
170168
; AVX-NEXT: [[ADD:%.*]] = shl <8 x i16> [[BC1]], <i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1>
171169
; AVX-NEXT: [[BC2:%.*]] = bitcast <8 x i16> [[ADD]] to <4 x i32>

‎llvm/test/Transforms/VectorCombine/X86/shuffle.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ define <2 x i64> @PR35454_1(<2 x i64> %v) {
133133
; SSE-NEXT: ret <2 x i64> [[BC3]]
134134
;
135135
; AVX-LABEL: @PR35454_1(
136-
; AVX-NEXT: [[BC:%.*]] = bitcast <2 x i64> [[V:%.*]] to <4 x i32>
137-
; AVX-NEXT: [[TMP1:%.*]] = bitcast <4 x i32> [[BC]] to <16 x i8>
136+
; AVX-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> [[V:%.*]] to <16 x i8>
138137
; AVX-NEXT: [[BC1:%.*]] = shufflevector <16 x i8> [[TMP1]], <16 x i8> poison, <16 x i32> <i32 12, i32 13, i32 14, i32 15, i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6, i32 7, i32 0, i32 1, i32 2, i32 3>
139138
; AVX-NEXT: [[ADD:%.*]] = shl <16 x i8> [[BC1]], <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
140139
; AVX-NEXT: [[BC2:%.*]] = bitcast <16 x i8> [[ADD]] to <4 x i32>
@@ -164,8 +163,7 @@ define <2 x i64> @PR35454_2(<2 x i64> %v) {
164163
; SSE-NEXT: ret <2 x i64> [[BC3]]
165164
;
166165
; AVX-LABEL: @PR35454_2(
167-
; AVX-NEXT: [[BC:%.*]] = bitcast <2 x i64> [[V:%.*]] to <4 x i32>
168-
; AVX-NEXT: [[TMP1:%.*]] = bitcast <4 x i32> [[BC]] to <8 x i16>
166+
; AVX-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> [[V:%.*]] to <8 x i16>
169167
; AVX-NEXT: [[BC1:%.*]] = shufflevector <8 x i16> [[TMP1]], <8 x i16> poison, <8 x i32> <i32 6, i32 7, i32 4, i32 5, i32 2, i32 3, i32 0, i32 1>
170168
; AVX-NEXT: [[ADD:%.*]] = shl <8 x i16> [[BC1]], <i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1>
171169
; AVX-NEXT: [[BC2:%.*]] = bitcast <8 x i16> [[ADD]] to <4 x i32>

0 commit comments

Comments
 (0)
Please sign in to comment.