[InstCombine] Canonicalize single element array store
authorDavid Majnemer <david.majnemer@gmail.com>
Mon, 11 May 2015 05:04:27 +0000 (05:04 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Mon, 11 May 2015 05:04:27 +0000 (05:04 +0000)
Use the element type instead of the aggregate type.

Differential Revision: http://reviews.llvm.org/D9591

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@236969 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
test/Transforms/InstCombine/unpack-fca.ll

index 1b0518654a012831636712bfa9eb9c780b6b1117..e74ab3c2afc93971b3ec5e925a000a0e9c81896b 100644 (file)
@@ -880,6 +880,15 @@ static bool unpackStoreToAggregate(InstCombiner &IC, StoreInst &SI) {
     }
   }
 
+  if (auto *AT = dyn_cast<ArrayType>(T)) {
+    // If the array only have one element, we unpack.
+    if (AT->getNumElements() == 1) {
+      V = IC.Builder->CreateExtractValue(V, 0);
+      combineStoreToNewValue(IC, SI, V);
+      return true;
+    }
+  }
+
   return false;
 }
 
index 759ffc694cfc9eb929216f5769352c7944c27ba5..fc04b24a8755bb51527c3f4b3feb53492ae32f01 100644 (file)
@@ -32,6 +32,26 @@ body:
   ret void
 }
 
+define void @storeArrayOfA() {
+body:
+  %0 = tail call i8* @allocmemory(i64 32)
+  %1 = bitcast i8* %0 to [1 x %A]*
+; CHECK-LABEL: storeStructOfA
+; CHECK: store %A__vtbl* @A__vtblZ
+  store [1 x %A] [%A { %A__vtbl* @A__vtblZ }], [1 x %A]* %1, align 8
+  ret void
+}
+
+define void @storeStructOfArrayOfA() {
+body:
+  %0 = tail call i8* @allocmemory(i64 32)
+  %1 = bitcast i8* %0 to { [1 x %A] }*
+; CHECK-LABEL: storeStructOfArrayOfA
+; CHECK: store %A__vtbl* @A__vtblZ
+  store { [1 x %A] } { [1 x %A] [%A { %A__vtbl* @A__vtblZ }] }, { [1 x %A] }* %1, align 8
+  ret void
+}
+
 define %A @loadA() {
 body:
   %0 = tail call i8* @allocmemory(i64 32)