[llvm-commits] [llvm] r76345 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/bitcast-vec-canon.ll

Eli Friedman eli.friedman at gmail.com
Sat Jul 18 16:06:53 PDT 2009


Author: efriedma
Date: Sat Jul 18 18:06:53 2009
New Revision: 76345

URL: http://llvm.org/viewvc/llvm-project?rev=76345&view=rev
Log:
Canonicalize bitcasts between types like <1 x i64> and i64 to 
insertelement/extractelement.

I'm not entirely sure this is precisely what we want to do: should we 
prefer bitcast(insertelement) or insertelement(bitcast)?  Similarly. should we 
prefer extractelement(bitcast) or bitcast(extractelement)?


Added:
    llvm/trunk/test/Transforms/InstCombine/bitcast-vec-canon.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=76345&r1=76344&r2=76345&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Sat Jul 18 18:06:53 2009
@@ -9033,6 +9033,29 @@
     }
   }
 
+  if (const VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) {
+    if (DestVTy->getNumElements() == 1) {
+      if (!isa<VectorType>(SrcTy)) {
+        Value *Elem = InsertCastBefore(Instruction::BitCast, Src,
+                                       DestVTy->getElementType(), CI);
+        return InsertElementInst::Create(Context->getUndef(DestTy), Elem,
+                                         Context->getNullValue(Type::Int32Ty));
+      }
+      // FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast)
+    }
+  }
+
+  if (const VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy)) {
+    if (SrcVTy->getNumElements() == 1) {
+      if (!isa<VectorType>(DestTy)) {
+        Instruction *Elem =
+            new ExtractElementInst(Src, Context->getNullValue(Type::Int32Ty));
+        InsertNewInstBefore(Elem, CI);
+        return CastInst::Create(Instruction::BitCast, Elem, DestTy);
+      }
+    }
+  }
+
   if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) {
     if (SVI->hasOneUse()) {
       // Okay, we have (bitconvert (shuffle ..)).  Check to see if this is
@@ -12477,6 +12500,7 @@
                          Context->getConstantInt(Type::Int32Ty, SrcIdx, false));
       }
     }
+    // FIXME: Canonicalize extractelement(bitcast) -> bitcast(extractelement)
   }
   return 0;
 }

Added: llvm/trunk/test/Transforms/InstCombine/bitcast-vec-canon.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/bitcast-vec-canon.ll?rev=76345&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/bitcast-vec-canon.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/bitcast-vec-canon.ll Sat Jul 18 18:06:53 2009
@@ -0,0 +1,22 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep element | count 4
+
+define double @a(<1 x i64> %y) {
+  %c = bitcast <1 x i64> %y to double
+  ret double %c 
+}
+
+define i64 @b(<1 x i64> %y) {
+  %c = bitcast <1 x i64> %y to i64
+  ret i64 %c 
+}
+
+define <1 x i64> @c(double %y) {
+  %c = bitcast double %y to <1 x i64>
+  ret <1 x i64> %c
+}
+
+define <1 x i64> @d(i64 %y) {
+  %c = bitcast i64 %y to <1 x i64>
+  ret <1 x i64> %c
+}
+





More information about the llvm-commits mailing list