teach SROA to handle promoting vector allocas with a memset into them into
[oota-llvm.git] / lib / Transforms / Scalar / ScalarReplAggregates.cpp
index 98d5a027013fe940caa14cf11bbb527ee7d660cb..78730b6eb7230d35d7f5e4c52638a26210ae5f53 100644 (file)
@@ -1350,8 +1350,6 @@ bool SROA::CanConvertToScalar(Value *V, bool &IsNotTrivial, const Type *&VecTy,
       // Store of constant value and constant size.
       if (isa<ConstantInt>(MSI->getValue()) &&
           isa<ConstantInt>(MSI->getLength())) {
-        // FIXME (!): Why reset VecTy?
-        VecTy = Type::VoidTy;
         IsNotTrivial = true;
         continue;
       }
@@ -1628,21 +1626,25 @@ Value *SROA::ConvertScalar_InsertValue(Value *SV, Value *Old,
   const Type *AllocaType = Old->getType();
 
   if (const VectorType *VTy = dyn_cast<VectorType>(AllocaType)) {
-    // If the result alloca is a vector type, this is either an element
-    // access or a bitcast to another vector type.
-    if (isa<VectorType>(SV->getType())) {
-      SV = Builder.CreateBitCast(SV, AllocaType, "tmp");
-    } else {
-      // Must be an element insertion.
-      unsigned Elt = Offset/TD->getTypePaddedSizeInBits(VTy->getElementType());
-      
-      if (SV->getType() != VTy->getElementType())
-        SV = Builder.CreateBitCast(SV, VTy->getElementType(), "tmp");
-      
-      SV = Builder.CreateInsertElement(Old, SV, 
-                                       ConstantInt::get(Type::Int32Ty, Elt), 
-                                       "tmp");
-    }
+    uint64_t VecSize = TD->getTypePaddedSizeInBits(VTy);
+    uint64_t ValSize = TD->getTypePaddedSizeInBits(SV->getType());
+    
+    // Changing the whole vector with memset or with an access of a different
+    // vector type?
+    if (ValSize == VecSize)
+      return Builder.CreateBitCast(SV, AllocaType, "tmp");
+
+    uint64_t EltSize = TD->getTypePaddedSizeInBits(VTy->getElementType());
+
+    // Must be an element insertion.
+    unsigned Elt = Offset/EltSize;
+    
+    if (SV->getType() != VTy->getElementType())
+      SV = Builder.CreateBitCast(SV, VTy->getElementType(), "tmp");
+    
+    SV = Builder.CreateInsertElement(Old, SV, 
+                                     ConstantInt::get(Type::Int32Ty, Elt),
+                                     "tmp");
     return SV;
   }