[cfe-commits] r40779 - /cfe/trunk/CodeGen/CGExpr.cpp

Chris Lattner sabre at nondot.org
Fri Aug 3 09:09:33 PDT 2007


Author: lattner
Date: Fri Aug  3 11:09:33 2007
New Revision: 40779

URL: http://llvm.org/viewvc/llvm-project?rev=40779&view=rev
Log:
In the common case where we are shuffling a vector, emit an
llvm vector shuffle instead of a bunch of insert/extract operations.
For:   vec4 = vec4.yyyy;  // splat

Emit:
        %tmp1 = shufflevector <4 x float> %tmp, <4 x float> undef, <4 x i32> < i32 1, i32 1, i32 1, i32 1 > 

instead of:

        %tmp1 = extractelement <4 x float> %tmp, i32 1          
        %tmp2 = insertelement <4 x float> undef, float %tmp1, i32 0             
        %tmp3 = extractelement <4 x float> %tmp, i32 1          
        %tmp4 = insertelement <4 x float> %tmp2, float %tmp3, i32 1             
        %tmp5 = extractelement <4 x float> %tmp, i32 1          
        %tmp6 = insertelement <4 x float> %tmp4, float %tmp5, i32 2             
        %tmp7 = extractelement <4 x float> %tmp, i32 1          
        %tmp8 = insertelement <4 x float> %tmp6, float %tmp7, i32 3             


Modified:
    cfe/trunk/CodeGen/CGExpr.cpp

Modified: cfe/trunk/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGExpr.cpp?rev=40779&r1=40778&r2=40779&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/CodeGen/CGExpr.cpp Fri Aug  3 11:09:33 2007
@@ -298,14 +298,31 @@
       return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
     }
     
-    
-    unsigned NumElts = cast<VectorType>(ExprType)->getNumElements();
+    // If the source and destination have the same number of elements, use a
+    // vector shuffle instead of insert/extracts.
+    unsigned NumResultElts = cast<VectorType>(ExprType)->getNumElements();
+    unsigned NumSourceElts =
+      cast<llvm::VectorType>(Vec->getType())->getNumElements();
+    
+    if (NumResultElts == NumSourceElts) {
+      llvm::SmallVector<llvm::Constant*, 4> Mask;
+      for (unsigned i = 0; i != NumResultElts; ++i) {
+        unsigned InIdx = OCUVectorComponent::getAccessedFieldNo(i, EncFields);
+        Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
+      }
+      
+      llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
+      Vec = Builder.CreateShuffleVector(Vec,
+                                        llvm::UndefValue::get(Vec->getType()),
+                                        MaskV, "tmp");
+      return RValue::get(Vec);
+    }
     
     // Start out with an undef of the result type.
     llvm::Value *Result = llvm::UndefValue::get(ConvertType(ExprType));
     
     // Extract/Insert each element of the result.
-    for (unsigned i = 0; i != NumElts; ++i) {
+    for (unsigned i = 0; i != NumResultElts; ++i) {
       unsigned InIdx = OCUVectorComponent::getAccessedFieldNo(i, EncFields);
       llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
       Elt = Builder.CreateExtractElement(Vec, Elt, "tmp");





More information about the cfe-commits mailing list