<div dir="ltr">Hi Simon,<div><br></div><div>This commit caused regression for a small test case as described in ticket <a href="https://llvm.org/bugs/show_bug.cgi?id=23065">https://llvm.org/bugs/show_bug.cgi?id=23065</a> .</div><div><br></div><div>Thanks,</div><div>-Jiangning</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">2015-03-06 1:14 GMT+08:00 Simon Pilgrim <span dir="ltr"><<a href="mailto:llvm-dev@redking.me.uk" target="_blank">llvm-dev@redking.me.uk</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: rksimon<br>
Date: Thu Mar 5 11:14:04 2015<br>
New Revision: 231380<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=231380&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=231380&view=rev</a><br>
Log:<br>
[DagCombiner] Allow shuffles to merge through bitcasts<br>
<br>
Currently shuffles may only be combined if they are of the same type, despite the fact that bitcasts are often introduced in between shuffle nodes (e.g. x86 shuffle type widening).<br>
<br>
This patch allows a single input shuffle to peek through bitcasts and if the input is another shuffle will merge them, shuffling using the smallest sized type, and re-applying the bitcasts at the inputs and output instead.<br>
<br>
Dropped old ShuffleToZext test - this patch removes the use of the zext and vector-zext.ll covers these anyhow.<br>
<br>
Differential Revision: <a href="http://reviews.llvm.org/D7939" target="_blank">http://reviews.llvm.org/D7939</a><br>
<br>
Removed:<br>
llvm/trunk/test/CodeGen/X86/2013-02-12-ShuffleToZext.ll<br>
Modified:<br>
llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp<br>
llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v16.ll<br>
llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v2.ll<br>
llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v4.ll<br>
llvm/trunk/test/CodeGen/X86/vector-shuffle-256-v4.ll<br>
llvm/trunk/test/CodeGen/X86/vector-shuffle-mmx.ll<br>
<br>
Modified: llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp?rev=231380&r1=231379&r2=231380&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp?rev=231380&r1=231379&r2=231380&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (original)<br>
+++ llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp Thu Mar 5 11:14:04 2015<br>
@@ -11877,6 +11877,89 @@ SDValue DAGCombiner::visitVECTOR_SHUFFLE<br>
return V;<br>
}<br>
<br>
+ // If this shuffle only has a single input that is a bitcasted shuffle,<br>
+ // attempt to merge the 2 shuffles and suitably bitcast the inputs/output<br>
+ // back to their original types.<br>
+ if (N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() &&<br>
+ N1.getOpcode() == ISD::UNDEF && Level < AfterLegalizeVectorOps &&<br>
+ TLI.isTypeLegal(VT)) {<br>
+<br>
+ // Peek through the bitcast only if there is one user.<br>
+ SDValue BC0 = N0;<br>
+ while (BC0.getOpcode() == ISD::BITCAST) {<br>
+ if (!BC0.hasOneUse())<br>
+ break;<br>
+ BC0 = BC0.getOperand(0);<br>
+ }<br>
+<br>
+ auto ScaleShuffleMask = [](ArrayRef<int> Mask, int Scale) {<br>
+ if (Scale == 1)<br>
+ return SmallVector<int, 8>(Mask.begin(), Mask.end());<br>
+<br>
+ SmallVector<int, 8> NewMask;<br>
+ for (int M : Mask)<br>
+ for (int s = 0; s != Scale; ++s)<br>
+ NewMask.push_back(M < 0 ? -1 : Scale * M + s);<br>
+ return NewMask;<br>
+ };<br>
+<br>
+ if (BC0.getOpcode() == ISD::VECTOR_SHUFFLE && BC0.hasOneUse()) {<br>
+ EVT SVT = VT.getScalarType();<br>
+ EVT InnerVT = BC0->getValueType(0);<br>
+ EVT InnerSVT = InnerVT.getScalarType();<br>
+<br>
+ // Determine which shuffle works with the smaller scalar type.<br>
+ EVT ScaleVT = SVT.bitsLT(InnerSVT) ? VT : InnerVT;<br>
+ EVT ScaleSVT = ScaleVT.getScalarType();<br>
+<br>
+ if (TLI.isTypeLegal(ScaleVT) &&<br>
+ 0 == (InnerSVT.getSizeInBits() % ScaleSVT.getSizeInBits()) &&<br>
+ 0 == (SVT.getSizeInBits() % ScaleSVT.getSizeInBits())) {<br>
+<br>
+ int InnerScale = InnerSVT.getSizeInBits() / ScaleSVT.getSizeInBits();<br>
+ int OuterScale = SVT.getSizeInBits() / ScaleSVT.getSizeInBits();<br>
+<br>
+ // Scale the shuffle masks to the smaller scalar type.<br>
+ ShuffleVectorSDNode *InnerSVN = cast<ShuffleVectorSDNode>(BC0);<br>
+ SmallVector<int, 8> InnerMask =<br>
+ ScaleShuffleMask(InnerSVN->getMask(), InnerScale);<br>
+ SmallVector<int, 8> OuterMask =<br>
+ ScaleShuffleMask(SVN->getMask(), OuterScale);<br>
+<br>
+ // Merge the shuffle masks.<br>
+ SmallVector<int, 8> NewMask;<br>
+ for (int M : OuterMask)<br>
+ NewMask.push_back(M < 0 ? -1 : InnerMask[M]);<br>
+<br>
+ // Test for shuffle mask legality over both commutations.<br>
+ SDValue SV0 = BC0->getOperand(0);<br>
+ SDValue SV1 = BC0->getOperand(1);<br>
+ bool LegalMask = TLI.isShuffleMaskLegal(NewMask, ScaleVT);<br>
+ if (!LegalMask) {<br>
+ for (int i = 0, e = (int)NewMask.size(); i != e; ++i) {<br>
+ int idx = NewMask[i];<br>
+ if (idx < 0)<br>
+ continue;<br>
+ else if (idx < e)<br>
+ NewMask[i] = idx + e;<br>
+ else<br>
+ NewMask[i] = idx - e;<br>
+ }<br>
+ std::swap(SV0, SV1);<br>
+ LegalMask = TLI.isShuffleMaskLegal(NewMask, ScaleVT);<br>
+ }<br>
+<br>
+ if (LegalMask) {<br>
+ SV0 = DAG.getNode(ISD::BITCAST, SDLoc(N), ScaleVT, SV0);<br>
+ SV1 = DAG.getNode(ISD::BITCAST, SDLoc(N), ScaleVT, SV1);<br>
+ return DAG.getNode(<br>
+ ISD::BITCAST, SDLoc(N), VT,<br>
+ DAG.getVectorShuffle(ScaleVT, SDLoc(N), SV0, SV1, NewMask));<br>
+ }<br>
+ }<br>
+ }<br>
+ }<br>
+<br>
// Canonicalize shuffles according to rules:<br>
// shuffle(A, shuffle(A, B)) -> shuffle(shuffle(A,B), A)<br>
// shuffle(B, shuffle(A, B)) -> shuffle(shuffle(A,B), B)<br>
<br>
Removed: llvm/trunk/test/CodeGen/X86/2013-02-12-ShuffleToZext.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/2013-02-12-ShuffleToZext.ll?rev=231379&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/2013-02-12-ShuffleToZext.ll?rev=231379&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/CodeGen/X86/2013-02-12-ShuffleToZext.ll (original)<br>
+++ llvm/trunk/test/CodeGen/X86/2013-02-12-ShuffleToZext.ll (removed)<br>
@@ -1,14 +0,0 @@<br>
-; RUN: llc < %s -march=x86-64 -mcpu=corei7-avx -mtriple=x86_64-pc-win32 | FileCheck %s<br>
-<br>
-; CHECK: test<br>
-; CHECK: vpmovzxwd<br>
-; CHECK: vpmovzxwd<br>
-define void @test(<4 x i64> %a, <4 x i16>* %buf) {<br>
- %ex1 = extractelement <4 x i64> %a, i32 0<br>
- %ex2 = extractelement <4 x i64> %a, i32 1<br>
- %x1 = bitcast i64 %ex1 to <4 x i16><br>
- %x2 = bitcast i64 %ex2 to <4 x i16><br>
- %Sh = shufflevector <4 x i16> %x1, <4 x i16> %x2, <4 x i32> <i32 0, i32 1, i32 4, i32 5><br>
- store <4 x i16> %Sh, <4 x i16>* %buf, align 1<br>
- ret void<br>
-}<br>
<br>
Modified: llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v16.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v16.ll?rev=231380&r1=231379&r2=231380&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v16.ll?rev=231380&r1=231379&r2=231380&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v16.ll (original)<br>
+++ llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v16.ll Thu Mar 5 11:14:04 2015<br>
@@ -1336,3 +1336,22 @@ define <16 x i8> @shuffle_v16i8_uu_02_03<br>
%shuffle = shufflevector <16 x i8> %a, <16 x i8> zeroinitializer, <16 x i32> <i32 undef, i32 2, i32 3, i32 16, i32 undef, i32 6, i32 7, i32 16, i32 undef, i32 10, i32 11, i32 16, i32 undef, i32 14, i32 15, i32 16><br>
ret <16 x i8> %shuffle<br>
}<br>
+<br>
+define <16 x i8> @shuffle_v16i8_bitcast_unpack(<16 x i8> %a, <16 x i8> %b) {<br>
+; SSE-LABEL: shuffle_v16i8_bitcast_unpack:<br>
+; SSE: # BB#0:<br>
+; SSE-NEXT: punpcklbw {{.*#+}} xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]<br>
+; SSE-NEXT: retq<br>
+;<br>
+; AVX-LABEL: shuffle_v16i8_bitcast_unpack:<br>
+; AVX: # BB#0:<br>
+; AVX-NEXT: punpcklbw {{.*#+}} xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]<br>
+; AVX-NEXT: retq<br>
+ %shuffle8 = shufflevector <16 x i8> %a, <16 x i8> %b, <16 x i32> <i32 7, i32 23, i32 6, i32 22, i32 5, i32 21, i32 4, i32 20, i32 3, i32 19, i32 2, i32 18, i32 1, i32 17, i32 0, i32 16><br>
+ %bitcast32 = bitcast <16 x i8> %shuffle8 to <4 x float><br>
+ %shuffle32 = shufflevector <4 x float> %bitcast32, <4 x float> undef, <4 x i32> <i32 3, i32 2, i32 1, i32 0><br>
+ %bitcast16 = bitcast <4 x float> %shuffle32 to <8 x i16><br>
+ %shuffle16 = shufflevector <8 x i16> %bitcast16, <8 x i16> undef, <8 x i32> <i32 1, i32 0, i32 3, i32 2, i32 5, i32 4, i32 7, i32 6><br>
+ %bitcast8 = bitcast <8 x i16> %shuffle16 to <16 x i8><br>
+ ret <16 x i8> %bitcast8<br>
+}<br>
<br>
Modified: llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v2.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v2.ll?rev=231380&r1=231379&r2=231380&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v2.ll?rev=231380&r1=231379&r2=231380&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v2.ll (original)<br>
+++ llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v2.ll Thu Mar 5 11:14:04 2015<br>
@@ -810,6 +810,25 @@ define <2 x double> @shuffle_v2f64_z1(<2<br>
ret <2 x double> %shuffle<br>
}<br>
<br>
+define <2 x double> @shuffle_v2f64_bitcast_1z(<2 x double> %a) {<br>
+; SSE-LABEL: shuffle_v2f64_bitcast_1z:<br>
+; SSE: # BB#0:<br>
+; SSE-NEXT: xorpd %xmm1, %xmm1<br>
+; SSE-NEXT: shufpd {{.*#+}} xmm0 = xmm0[1],xmm1[0]<br>
+; SSE-NEXT: retq<br>
+;<br>
+; AVX-LABEL: shuffle_v2f64_bitcast_1z:<br>
+; AVX: # BB#0:<br>
+; AVX-NEXT: vxorpd %xmm1, %xmm1, %xmm1<br>
+; AVX-NEXT: vshufpd {{.*#+}} xmm0 = xmm0[1],xmm1[0]<br>
+; AVX-NEXT: retq<br>
+ %shuffle64 = shufflevector <2 x double> %a, <2 x double> zeroinitializer, <2 x i32> <i32 2, i32 1><br>
+ %bitcast32 = bitcast <2 x double> %shuffle64 to <4 x float><br>
+ %shuffle32 = shufflevector <4 x float> %bitcast32, <4 x float> undef, <4 x i32> <i32 2, i32 3, i32 0, i32 1><br>
+ %bitcast64 = bitcast <4 x float> %shuffle32 to <2 x double><br>
+ ret <2 x double> %bitcast64<br>
+}<br>
+<br>
define <2 x i64> @insert_reg_and_zero_v2i64(i64 %a) {<br>
; SSE-LABEL: insert_reg_and_zero_v2i64:<br>
; SSE: # BB#0:<br>
<br>
Modified: llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v4.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v4.ll?rev=231380&r1=231379&r2=231380&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v4.ll?rev=231380&r1=231379&r2=231380&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v4.ll (original)<br>
+++ llvm/trunk/test/CodeGen/X86/vector-shuffle-128-v4.ll Thu Mar 5 11:14:04 2015<br>
@@ -1574,6 +1574,23 @@ define <4 x i32> @shuffle_v4i32_0zz3(<4<br>
ret <4 x i32> %shuffle<br>
}<br>
<br>
+define <4 x i32> @shuffle_v4i32_bitcast_0415(<4 x i32> %a, <4 x i32> %b) {<br>
+; SSE-LABEL: shuffle_v4i32_bitcast_0415:<br>
+; SSE: # BB#0:<br>
+; SSE-NEXT: punpckldq {{.*#+}} xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]<br>
+; SSE-NEXT: retq<br>
+;<br>
+; AVX-LABEL: shuffle_v4i32_bitcast_0415:<br>
+; AVX: # BB#0:<br>
+; AVX-NEXT: vpunpckldq {{.*#+}} xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]<br>
+; AVX-NEXT: retq<br>
+ %shuffle32 = shufflevector <4 x i32> %a, <4 x i32> %b, <4 x i32> <i32 1, i32 5, i32 0, i32 4><br>
+ %bitcast64 = bitcast <4 x i32> %shuffle32 to <2 x double><br>
+ %shuffle64 = shufflevector <2 x double> %bitcast64, <2 x double> undef, <2 x i32> <i32 1, i32 0><br>
+ %bitcast32 = bitcast <2 x double> %shuffle64 to <4 x i32><br>
+ ret <4 x i32> %bitcast32<br>
+}<br>
+<br>
define <4 x i32> @insert_reg_and_zero_v4i32(i32 %a) {<br>
; SSE-LABEL: insert_reg_and_zero_v4i32:<br>
; SSE: # BB#0:<br>
<br>
Modified: llvm/trunk/test/CodeGen/X86/vector-shuffle-256-v4.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/vector-shuffle-256-v4.ll?rev=231380&r1=231379&r2=231380&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/vector-shuffle-256-v4.ll?rev=231380&r1=231379&r2=231380&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/CodeGen/X86/vector-shuffle-256-v4.ll (original)<br>
+++ llvm/trunk/test/CodeGen/X86/vector-shuffle-256-v4.ll Thu Mar 5 11:14:04 2015<br>
@@ -922,3 +922,22 @@ define <4 x double> @splat_v4f64(<2 x do<br>
%1 = shufflevector <2 x double> %r, <2 x double> undef, <4 x i32> zeroinitializer<br>
ret <4 x double> %1<br>
}<br>
+<br>
+define <4 x double> @bitcast_v4f64_0426(<4 x double> %a, <4 x double> %b) {<br>
+; AVX1-LABEL: bitcast_v4f64_0426:<br>
+; AVX1: # BB#0:<br>
+; AVX1-NEXT: vunpcklpd {{.*#+}} ymm0 = ymm0[0],ymm1[0],ymm0[2],ymm1[2]<br>
+; AVX1-NEXT: retq<br>
+;<br>
+; AVX2-LABEL: bitcast_v4f64_0426:<br>
+; AVX2: # BB#0:<br>
+; AVX2-NEXT: vpunpcklqdq {{.*#+}} ymm0 = ymm0[0],ymm1[0],ymm0[2],ymm1[2]<br>
+; AVX2-NEXT: retq<br>
+ %shuffle64 = shufflevector <4 x double> %a, <4 x double> %b, <4 x i32> <i32 4, i32 0, i32 6, i32 2><br>
+ %bitcast32 = bitcast <4 x double> %shuffle64 to <8 x float><br>
+ %shuffle32 = shufflevector <8 x float> %bitcast32, <8 x float> undef, <8 x i32> <i32 3, i32 2, i32 1, i32 0, i32 7, i32 6, i32 5, i32 4><br>
+ %bitcast16 = bitcast <8 x float> %shuffle32 to <16 x i16><br>
+ %shuffle16 = shufflevector <16 x i16> %bitcast16, <16 x i16> undef, <16 x i32> <i32 2, i32 3, i32 0, i32 1, i32 6, i32 7, i32 4, i32 5, i32 10, i32 11, i32 8, i32 9, i32 14, i32 15, i32 12, i32 13><br>
+ %bitcast64 = bitcast <16 x i16> %shuffle16 to <4 x double><br>
+ ret <4 x double> %bitcast64<br>
+}<br>
<br>
Modified: llvm/trunk/test/CodeGen/X86/vector-shuffle-mmx.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/vector-shuffle-mmx.ll?rev=231380&r1=231379&r2=231380&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/vector-shuffle-mmx.ll?rev=231380&r1=231379&r2=231380&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/CodeGen/X86/vector-shuffle-mmx.ll (original)<br>
+++ llvm/trunk/test/CodeGen/X86/vector-shuffle-mmx.ll Thu Mar 5 11:14:04 2015<br>
@@ -8,14 +8,14 @@ define void @test0(<1 x i64>* %x) {<br>
; X32: ## BB#0: ## %entry<br>
; X32-NEXT: movl {{[0-9]+}}(%esp), %eax<br>
; X32-NEXT: movsd {{.*#+}} xmm0 = mem[0],zero<br>
-; X32-NEXT: pshufd {{.*#+}} xmm0 = xmm0[1,1,1,3]<br>
+; X32-NEXT: pshufd {{.*#+}} xmm0 = xmm0[1,1,2,3]<br>
; X32-NEXT: movlpd %xmm0, (%eax)<br>
; X32-NEXT: retl<br>
;<br>
; X64-LABEL: test0:<br>
; X64: ## BB#0: ## %entry<br>
; X64-NEXT: movq {{.*#+}} xmm0 = mem[0],zero<br>
-; X64-NEXT: pshufd {{.*#+}} xmm0 = xmm0[1,1,1,3]<br>
+; X64-NEXT: pshufd {{.*#+}} xmm0 = xmm0[1,1,2,3]<br>
; X64-NEXT: movq %xmm0, (%rdi)<br>
; X64-NEXT: retq<br>
entry:<br>
@@ -84,16 +84,15 @@ define void @test2() nounwind {<br>
; X32: ## BB#0: ## %entry<br>
; X32-NEXT: movl L_tmp_V2i$non_lazy_ptr, %eax<br>
; X32-NEXT: movsd {{.*#+}} xmm0 = mem[0],zero<br>
-; X32-NEXT: movlhps {{.*#+}} xmm0 = xmm0[0,0]<br>
-; X32-NEXT: pshufd {{.*#+}} xmm0 = xmm0[0,2,2,3]<br>
-; X32-NEXT: movlpd %xmm0, (%eax)<br>
+; X32-NEXT: unpcklps {{.*#+}} xmm0 = xmm0[0,0,1,1]<br>
+; X32-NEXT: movlps %xmm0, (%eax)<br>
; X32-NEXT: retl<br>
;<br>
; X64-LABEL: test2:<br>
; X64: ## BB#0: ## %entry<br>
; X64-NEXT: movq _tmp_V2i@{{.*}}(%rip), %rax<br>
; X64-NEXT: movq {{.*#+}} xmm0 = mem[0],zero<br>
-; X64-NEXT: pshufd {{.*#+}} xmm0 = xmm0[0,0,0,1]<br>
+; X64-NEXT: pshufd {{.*#+}} xmm0 = xmm0[0,0,1,1]<br>
; X64-NEXT: movq %xmm0, (%rax)<br>
; X64-NEXT: retq<br>
entry:<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>