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

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

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

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

index 4ce5ad3554971a005d7e6094579766a5c076b0a6..1b0518654a012831636712bfa9eb9c780b6b1117 100644 (file)
@@ -518,6 +518,16 @@ static Instruction *unpackLoadToAggregate(InstCombiner &IC, LoadInst &LI) {
     }
   }
 
+  if (auto *AT = dyn_cast<ArrayType>(T)) {
+    // If the array only have one element, we unpack.
+    if (AT->getNumElements() == 1) {
+      LoadInst *NewLoad = combineLoadToNewType(IC, LI, AT->getElementType(),
+                                               ".unpack");
+      return IC.ReplaceInstUsesWith(LI, IC.Builder->CreateInsertValue(
+        UndefValue::get(T), NewLoad, 0, LI.getName()));
+    }
+  }
+
   return nullptr;
 }
 
index 58dfbe55162fad4f17d727ff72cb881ad93534f7..759ffc694cfc9eb929216f5769352c7944c27ba5 100644 (file)
@@ -55,6 +55,31 @@ body:
   ret { %A } %2
 }
 
+define [1 x %A] @loadArrayOfA() {
+body:
+  %0 = tail call i8* @allocmemory(i64 32)
+  %1 = bitcast i8* %0 to [1 x %A]*
+; CHECK-LABEL: loadArrayOfA
+; CHECK: load %A__vtbl*,
+; CHECK: insertvalue %A undef, %A__vtbl* {{.*}}, 0
+; CHECK: insertvalue [1 x %A] undef, %A {{.*}}, 0
+  %2 = load [1 x %A], [1 x %A]* %1, align 8
+  ret [1 x %A] %2
+}
+
+define { [1 x %A] } @loadStructOfArrayOfA() {
+body:
+  %0 = tail call i8* @allocmemory(i64 32)
+  %1 = bitcast i8* %0 to { [1 x %A] }*
+; CHECK-LABEL: loadStructOfArrayOfA
+; CHECK: load %A__vtbl*,
+; CHECK: insertvalue %A undef, %A__vtbl* {{.*}}, 0
+; CHECK: insertvalue [1 x %A] undef, %A {{.*}}, 0
+; CHECK: insertvalue { [1 x %A] } undef, [1 x %A] {{.*}}, 0
+  %2 = load { [1 x %A] }, { [1 x %A] }* %1, align 8
+  ret { [1 x %A] } %2
+}
+
 define { %A } @structOfA() {
 body:
   %0 = tail call i8* @allocmemory(i64 32)