Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
authorChris Lattner <sabre@nondot.org>
Sun, 16 Apr 2006 00:03:56 +0000 (00:03 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 16 Apr 2006 00:03:56 +0000 (00:03 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27727 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/InstructionCombining.cpp

index a502bd25fefd6fbf6e9decb8b4adbbafe8de7ee8..7d1ddbf0983415a438206a598b6a940381b1f0eb 100644 (file)
@@ -7046,8 +7046,6 @@ Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
   if (isa<UndefValue>(Mask))
     return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
   
-  // TODO: Canonicalize shuffle(undef,x) -> shuffle(x, undef).
-
   // TODO: If we have shuffle(x, undef, mask) and any elements of mask refer to
   // the undef, change them to undefs.
   
@@ -7077,6 +7075,28 @@ Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
     MadeChange = true;
   }
   
+  // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
+  if (isa<UndefValue>(LHS)) {
+    // shuffle(undef,x,<0,0,0,0>) -> undef.
+    if (isa<ConstantAggregateZero>(Mask))
+      return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
+    
+    ConstantPacked *CPM = cast<ConstantPacked>(Mask);
+    std::vector<Constant*> Elts;
+    for (unsigned i = 0, e = CPM->getNumOperands(); i != e; ++i) {
+      if (isa<UndefValue>(CPM->getOperand(i)))
+        Elts.push_back(CPM->getOperand(i));
+      else {
+        unsigned EltNo = cast<ConstantUInt>(CPM->getOperand(i))->getRawValue();
+        if (EltNo >= e/2)
+          Elts.push_back(ConstantUInt::get(Type::UIntTy, EltNo-e/2));
+        else               // Referring to the undef.
+          Elts.push_back(UndefValue::get(Type::UIntTy));
+      }
+    }
+    return new ShuffleVectorInst(RHS, LHS, ConstantPacked::get(Elts));
+  }
+  
   if (ConstantPacked *CP = dyn_cast<ConstantPacked>(Mask)) {
     bool isLHSID = true, isRHSID = true;