Index: llvm/trunk/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp =================================================================== --- llvm/trunk/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ llvm/trunk/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -518,6 +518,16 @@ } } + if (auto *AT = dyn_cast(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: llvm/trunk/test/Transforms/InstCombine/unpack-fca.ll =================================================================== --- llvm/trunk/test/Transforms/InstCombine/unpack-fca.ll +++ llvm/trunk/test/Transforms/InstCombine/unpack-fca.ll @@ -55,6 +55,31 @@ 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)