[llvm-commits] [llvm] r111318 - in /llvm/trunk: lib/Transforms/InstCombine/InstCombineVectorOps.cpp test/Transforms/InstCombine/vec_shuffle.ll

Eric Christopher echristo at apple.com
Tue Aug 17 15:55:27 PDT 2010


Author: echristo
Date: Tue Aug 17 17:55:27 2010
New Revision: 111318

URL: http://llvm.org/viewvc/llvm-project?rev=111318&view=rev
Log:
Temporarily revert r110987 as it's causing some miscompares in
vector heavy code.  I'll re-enable when we've tracked down the problem.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
    llvm/trunk/test/Transforms/InstCombine/vec_shuffle.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp?rev=111318&r1=111317&r2=111318&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp Tue Aug 17 17:55:27 2010
@@ -448,8 +448,10 @@
   if (isa<UndefValue>(SVI.getOperand(2)))
     return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
   
-  unsigned VWidth = Mask.size();
-  unsigned LHSWidth = cast<VectorType>(LHS->getType())->getNumElements();
+  unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements();
+  
+  if (VWidth != cast<VectorType>(LHS->getType())->getNumElements())
+    return 0;
   
   APInt UndefElts(VWidth, 0);
   APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
@@ -462,12 +464,14 @@
   // Canonicalize shuffle(x    ,x,mask) -> shuffle(x, undef,mask')
   // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
   if (LHS == RHS || isa<UndefValue>(LHS)) {
-    if (isa<UndefValue>(LHS) && LHS == RHS)
-      return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
+    if (isa<UndefValue>(LHS) && LHS == RHS) {
+      // shuffle(undef,undef,mask) -> undef.
+      return ReplaceInstUsesWith(SVI, LHS);
+    }
     
     // Remap any references to RHS to use LHS.
     std::vector<Constant*> Elts;
-    for (unsigned i = 0, e = LHSWidth; i != VWidth; ++i) {
+    for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
       if (Mask[i] >= 2*e)
         Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
       else {
@@ -491,130 +495,67 @@
   }
   
   // Analyze the shuffle, are the LHS or RHS and identity shuffles?
-  if (VWidth == LHSWidth) {
-    bool isLHSID = true, isRHSID = true;
-    
-    for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
-      if (Mask[i] >= e*2) continue;  // Ignore undef values.
-      // Is this an identity shuffle of the LHS value?
-      isLHSID &= (Mask[i] == i);
-      
-      // Is this an identity shuffle of the RHS value?
-      isRHSID &= (Mask[i]-e == i);
-    }
-  
-    // Eliminate identity shuffles.
-    if (isLHSID) return ReplaceInstUsesWith(SVI, LHS);
-    if (isRHSID) return ReplaceInstUsesWith(SVI, RHS);
-  }
+  bool isLHSID = true, isRHSID = true;
   
-  // Check for a handful of important shuffle(shuffle()) combinations.
-  ShuffleVectorInst *LSVI = dyn_cast<ShuffleVectorInst>(LHS);
-  if (!LSVI)
-    return MadeChange ? &SVI : 0;
-
-  LHS = LSVI->getOperand(0);
-  std::vector<unsigned> LHSMask = getShuffleMask(LSVI);
-  unsigned LHSInNElts = cast<VectorType>(LHS->getType())->getNumElements();
-  
-  // If the LHS is an identity shuffle, or if SVI + LHS form a full unpack 
-  // operation, merge the LHS and SVI shuffles. This allows llvm to emit 
-  // efficient code for matrix transposes written with generic vector ops.
-  bool isLHSLoExtract = true, isLHSHiExtract = true;
-  bool isUnpackLo = isPowerOf2_32(VWidth);
-  bool isUnpackHi = isPowerOf2_32(VWidth);
-  for (unsigned i = 0, e = LHSMask.size(); i != e; ++i) {
-    if (LHSMask[i] >= LHSInNElts*2) continue; // Ignore undef values;
-    isLHSLoExtract &= (LHSMask[i] == i);
-    isLHSHiExtract &= (LHSMask[i] == i+(LHSInNElts/2));
-    isUnpackLo &= (LHSMask[i] == (i/2));
-    isUnpackHi &= (LHSMask[i] == (i/2) + (e/2));
-  }
-  for (unsigned i = 0, e = Mask.size(); i != e && (isUnpackLo || isUnpackHi);
-       i += 2) {
-    isUnpackLo &= (Mask[i] == i) && (Mask[i+1] == (i/2)+e);
-    isUnpackHi &= (Mask[i] == i) && (Mask[i+1] == (i/2)+e+(e/2));
-  }
-  if ((isLHSLoExtract || isLHSHiExtract || isUnpackLo || isUnpackHi) && 
-      (isa<UndefValue>(RHS) || (LHSWidth == LHSInNElts))) {
-    std::vector<Constant*> Elts;
-    for (unsigned i = 0, e = VWidth; i != e; ++i) {
-      if (Mask[i] >= 2*LHSWidth)
-        Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
-      else if (Mask[i] >= e)
-        Elts.push_back(ConstantInt::get(Type::getInt32Ty(SVI.getContext()),
-                                        Mask[i]));
-      else
-        Elts.push_back(ConstantInt::get(Type::getInt32Ty(SVI.getContext()),
-                                        LHSMask[Mask[i]]));
-    }
-    if (isa<UndefValue>(RHS))
-      RHS = UndefValue::get(LHS->getType());
-    return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Elts));
-  }
-
-  // If rhs is shuffle + identity, propagate.
-  if (ShuffleVectorInst *RSVI = dyn_cast<ShuffleVectorInst>(RHS)) {
-    std::vector<unsigned> RHSMask = getShuffleMask(RSVI);
-    unsigned RHSInNElts = 
-      cast<VectorType>(RSVI->getOperand(0)->getType())->getNumElements();
-
-    // If rhs is identity, propagate
-    bool isRHSLoExtract = true, isRHSHiExtract = true;
-    for (unsigned i = 0, e = RHSMask.size(); i != e; ++i) {
-      if (RHSMask[i] >= RHSInNElts*2) continue; // Ignore undef values;
-      isRHSLoExtract &= (RHSMask[i] == i);
-      isRHSHiExtract &= (RHSMask[i] == i+(RHSInNElts/2));
-    }
-    if ((isRHSLoExtract || isRHSHiExtract) && (LHSWidth == RHSInNElts)) {
-      std::vector<Constant*> Elts;
-      for (unsigned i = 0, e = VWidth; i != e; ++i) {
-        if (Mask[i] >= 2*LHSWidth)
-          Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
-        else if (Mask[i] < LHSWidth)
-          Elts.push_back(ConstantInt::get(Type::getInt32Ty(SVI.getContext()),
-                                          Mask[i]));
-        else
-          Elts.push_back(ConstantInt::get(Type::getInt32Ty(SVI.getContext()),
-                                          RHSMask[Mask[i]-LHSWidth]+LHSWidth));
-      }
-      SVI.setOperand(1, RSVI->getOperand(0));
-      SVI.setOperand(2, ConstantVector::get(Elts));
-      return &SVI;
-    }
+  for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
+    if (Mask[i] >= e*2) continue;  // Ignore undef values.
+    // Is this an identity shuffle of the LHS value?
+    isLHSID &= (Mask[i] == i);
+    
+    // Is this an identity shuffle of the RHS value?
+    isRHSID &= (Mask[i]-e == i);
   }
   
-  // Be extremely conservative when merging shufflevector instructions.  It is 
-  // difficult for the code generator to recognize a merged shuffle, which 
-  // usually leads to worse code from merging a shuffle.
-  if (!isa<UndefValue>(RHS))
-    return MadeChange ? &SVI : 0;
-  
-  // If the merged shuffle mask is one of the two input shuffle masks, which
-  // just removes one instruction.  This should handle splat(splat) -> splat.
-  if (LHSMask.size() == Mask.size()) {
-    std::vector<unsigned> NewMask;
-    for (unsigned i = 0, e = Mask.size(); i != e; ++i)
-      if (Mask[i] >= e)
-        NewMask.push_back(2*e);
-      else
-        NewMask.push_back(LHSMask[Mask[i]]);
-    
-    // If the result mask is equal to the src shuffle or this shuffle mask,
-    // do the replacement.
-    if (NewMask == LHSMask || NewMask == Mask) {
-      std::vector<Constant*> Elts;
-      for (unsigned i = 0, e = NewMask.size(); i != e; ++i) {
-        if (NewMask[i] >= LHSInNElts*2) {
-          Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
-        } else {
-          Elts.push_back(ConstantInt::get(Type::getInt32Ty(SVI.getContext()),
-                                          NewMask[i]));
+  // Eliminate identity shuffles.
+  if (isLHSID) return ReplaceInstUsesWith(SVI, LHS);
+  if (isRHSID) return ReplaceInstUsesWith(SVI, RHS);
+  
+  // If the LHS is a shufflevector itself, see if we can combine it with this
+  // one without producing an unusual shuffle.  Here we are really conservative:
+  // we are absolutely afraid of producing a shuffle mask not in the input
+  // program, because the code gen may not be smart enough to turn a merged
+  // shuffle into two specific shuffles: it may produce worse code.  As such,
+  // we only merge two shuffles if the result is one of the two input shuffle
+  // masks.  In this case, merging the shuffles just removes one instruction,
+  // which we know is safe.  This is good for things like turning:
+  // (splat(splat)) -> splat.
+  if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) {
+    if (isa<UndefValue>(RHS)) {
+      std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI);
+      
+      if (LHSMask.size() == Mask.size()) {
+        std::vector<unsigned> NewMask;
+        for (unsigned i = 0, e = Mask.size(); i != e; ++i)
+          if (Mask[i] >= e)
+            NewMask.push_back(2*e);
+          else
+            NewMask.push_back(LHSMask[Mask[i]]);
+        
+        // If the result mask is equal to the src shuffle or this
+        // shuffle mask, do the replacement.
+        if (NewMask == LHSMask || NewMask == Mask) {
+          unsigned LHSInNElts =
+          cast<VectorType>(LHSSVI->getOperand(0)->getType())->
+          getNumElements();
+          std::vector<Constant*> Elts;
+          for (unsigned i = 0, e = NewMask.size(); i != e; ++i) {
+            if (NewMask[i] >= LHSInNElts*2) {
+              Elts.push_back(UndefValue::get(
+                                             Type::getInt32Ty(SVI.getContext())));
+            } else {
+              Elts.push_back(ConstantInt::get(
+                                              Type::getInt32Ty(SVI.getContext()),
+                                              NewMask[i]));
+            }
+          }
+          return new ShuffleVectorInst(LHSSVI->getOperand(0),
+                                       LHSSVI->getOperand(1),
+                                       ConstantVector::get(Elts));
         }
       }
-      return new ShuffleVectorInst(LHS, LSVI->getOperand(1),
-                                   ConstantVector::get(Elts));
     }
   }
+  
   return MadeChange ? &SVI : 0;
 }
+

Modified: llvm/trunk/test/Transforms/InstCombine/vec_shuffle.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/vec_shuffle.ll?rev=111318&r1=111317&r2=111318&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/vec_shuffle.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/vec_shuffle.ll Tue Aug 17 17:55:27 2010
@@ -87,32 +87,3 @@
 	%tmp9 = shufflevector <4 x i8> %tmp7, <4 x i8> undef, <4 x i32> < i32 3, i32 1, i32 2, i32 0 >		; <<4 x i8>> [#uses=1]
 	ret <4 x i8> %tmp9
 }
-
-; Test fold of hi/lo vector halves
-; Test fold of unpack operation
-define void @test10(<16 x i8>* %out, <16 x i8> %r, <16 x i8> %g, <16 x i8> %b, <16 x i8> %a) nounwind ssp {
-; CHECK: @test10
-; CHECK-NEXT: shufflevector
-; CHECK-NEXT: shufflevector
-; CHECK-NEXT: store
-; CHECK-NEXT: getelementptr
-; CHECK-NEXT: store
-; CHECK-NEXT: ret
-  %tmp1 = shufflevector <16 x i8> %r, <16 x i8> undef, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7> ; <<8 x i8>> [#uses=1]
-  %tmp3 = shufflevector <8 x i8> %tmp1, <8 x i8> undef, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef> ; <<16 x i8>> [#uses=1]
-  %tmp4 = shufflevector <16 x i8> undef, <16 x i8> %tmp3, <16 x i32> <i32 16, i32 1, i32 17, i32 3, i32 18, i32 5, i32 19, i32 7, i32 20, i32 9, i32 21, i32 11, i32 22, i32 13, i32 23, i32 15> ; <<16 x i8>> [#uses=1]
-  %tmp6 = shufflevector <16 x i8> %b, <16 x i8> undef, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7> ; <<8 x i8>> [#uses=1]
-  %tmp8 = shufflevector <8 x i8> %tmp6, <8 x i8> undef, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef> ; <<16 x i8>> [#uses=1]
-  %tmp9 = shufflevector <16 x i8> %tmp4, <16 x i8> %tmp8, <16 x i32> <i32 0, i32 16, i32 2, i32 17, i32 4, i32 18, i32 6, i32 19, i32 8, i32 20, i32 10, i32 21, i32 12, i32 22, i32 14, i32 23> ; <<16 x i8>> [#uses=1]
-  %tmp11 = shufflevector <16 x i8> %r, <16 x i8> undef, <8 x i32> <i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15> ; <<8 x i8>> [#uses=1]
-  %tmp13 = shufflevector <8 x i8> %tmp11, <8 x i8> undef, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef> ; <<16 x i8>> [#uses=1]
-  %tmp14 = shufflevector <16 x i8> undef, <16 x i8> %tmp13, <16 x i32> <i32 16, i32 1, i32 17, i32 3, i32 18, i32 5, i32 19, i32 7, i32 20, i32 9, i32 21, i32 11, i32 22, i32 13, i32 23, i32 15> ; <<16 x i8>> [#uses=1]
-  %tmp16 = shufflevector <16 x i8> %b, <16 x i8> undef, <8 x i32> <i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15> ; <<8 x i8>> [#uses=1]
-  %tmp18 = shufflevector <8 x i8> %tmp16, <8 x i8> undef, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef> ; <<16 x i8>> [#uses=1]
-  %tmp19 = shufflevector <16 x i8> %tmp14, <16 x i8> %tmp18, <16 x i32> <i32 0, i32 16, i32 2, i32 17, i32 4, i32 18, i32 6, i32 19, i32 8, i32 20, i32 10, i32 21, i32 12, i32 22, i32 14, i32 23> ; <<16 x i8>> [#uses=1]
-  %arrayidx = getelementptr inbounds <16 x i8>* %out, i64 0 ; <<16 x i8>*> [#uses=1]
-  store <16 x i8> %tmp9, <16 x i8>* %arrayidx
-  %arrayidx24 = getelementptr inbounds <16 x i8>* %out, i64 1 ; <<16 x i8>*> [#uses=1]
-  store <16 x i8> %tmp19, <16 x i8>* %arrayidx24
-  ret void
-}





More information about the llvm-commits mailing list