diff options
| author | Amaury Sechet <deadalnix@gmail.com> | 2016-03-02 22:36:45 +0000 |
|---|---|---|
| committer | Amaury Sechet <deadalnix@gmail.com> | 2016-03-02 22:36:45 +0000 |
| commit | 3b8b2ea2e1887e927a89e197980e4e381dd44153 (patch) | |
| tree | 6d517f95ce07abca75991c59d59a4c04cd126fe5 /llvm | |
| parent | 98be3f2be2ae8ccb79116de4c4353c7b93cf3484 (diff) | |
| download | bcm5719-llvm-3b8b2ea2e1887e927a89e197980e4e381dd44153.tar.gz bcm5719-llvm-3b8b2ea2e1887e927a89e197980e4e381dd44153.zip | |
Explode store of arrays in instcombine
Summary: This is the last step toward supporting aggregate memory access in instcombine. This explodes stores of arrays into a serie of stores for each element, allowing them to be optimized.
Reviewers: joker.eph, reames, hfinkel, majnemer, mgrang
Subscribers: llvm-commits
Differential Revision: http://reviews.llvm.org/D17828
llvm-svn: 262530
Diffstat (limited to 'llvm')
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp | 34 | ||||
| -rw-r--r-- | llvm/test/Transforms/InstCombine/unpack-fca.ll | 21 |
2 files changed, 54 insertions, 1 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp index c559d93e3f7..e0a3f36355d 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -1017,11 +1017,43 @@ 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) { + auto NumElements = AT->getNumElements(); + if (NumElements == 1) { V = IC.Builder->CreateExtractValue(V, 0); combineStoreToNewValue(IC, SI, V); return true; } + + const DataLayout &DL = IC.getDataLayout(); + auto EltSize = DL.getTypeAllocSize(AT->getElementType()); + auto Align = SI.getAlignment(); + if (!Align) + Align = DL.getABITypeAlignment(T); + + SmallString<16> EltName = V->getName(); + EltName += ".elt"; + auto *Addr = SI.getPointerOperand(); + SmallString<16> AddrName = Addr->getName(); + AddrName += ".repack"; + + auto *IdxType = Type::getInt64Ty(T->getContext()); + auto *Zero = ConstantInt::get(IdxType, 0); + + uint64_t Offset = 0; + for (uint64_t i = 0; i < NumElements; i++) { + Value *Indices[2] = { + Zero, + ConstantInt::get(IdxType, i), + }; + auto *Ptr = IC.Builder->CreateInBoundsGEP(AT, Addr, makeArrayRef(Indices), + AddrName); + auto *Val = IC.Builder->CreateExtractValue(V, i, EltName); + auto EltAlign = MinAlign(Align, Offset); + IC.Builder->CreateAlignedStore(Val, Ptr, EltAlign); + Offset += EltSize; + } + + return true; } return false; diff --git a/llvm/test/Transforms/InstCombine/unpack-fca.ll b/llvm/test/Transforms/InstCombine/unpack-fca.ll index cd97ce63621..47e747ccc46 100644 --- a/llvm/test/Transforms/InstCombine/unpack-fca.ll +++ b/llvm/test/Transforms/InstCombine/unpack-fca.ll @@ -58,6 +58,27 @@ define void @storeStructOfArrayOfA({ [1 x %A] }* %saa.ptr) { ret void } +define void @storeArrayOfB([2 x %B]* %ab.ptr, [2 x %B] %ab) { +; CHECK-LABEL: storeArrayOfB +; CHECK-NEXT: [[EVB0:%[a-z0-9\.]+]] = extractvalue [2 x %B] %ab, 0 +; CHECK-NEXT: [[GEP0:%[a-z0-9\.]+]] = getelementptr inbounds [2 x %B], [2 x %B]* %ab.ptr, i64 0, i64 0, i32 0 +; CHECK-NEXT: [[EV0:%[a-z0-9\.]+]] = extractvalue %B [[EVB0]], 0 +; CHECK-NEXT: store i8* [[EV0]], i8** [[GEP0]], align 8 +; CHECK-NEXT: [[GEP1:%[a-z0-9\.]+]] = getelementptr inbounds [2 x %B], [2 x %B]* %ab.ptr, i64 0, i64 0, i32 1 +; CHECK-NEXT: [[EV1:%[a-z0-9\.]+]] = extractvalue %B [[EVB0]], 1 +; CHECK-NEXT: store i64 [[EV1]], i64* [[GEP1]], align 8 +; CHECK-NEXT: [[EVB1:%[a-z0-9\.]+]] = extractvalue [2 x %B] %ab, 1 +; CHECK-NEXT: [[GEP2:%[a-z0-9\.]+]] = getelementptr inbounds [2 x %B], [2 x %B]* %ab.ptr, i64 0, i64 1, i32 0 +; CHECK-NEXT: [[EV2:%[a-z0-9\.]+]] = extractvalue %B [[EVB1]], 0 +; CHECK-NEXT: store i8* [[EV2]], i8** [[GEP2]], align 8 +; CHECK-NEXT: [[GEP3:%[a-z0-9\.]+]] = getelementptr inbounds [2 x %B], [2 x %B]* %ab.ptr, i64 0, i64 1, i32 1 +; CHECK-NEXT: [[EV3:%[a-z0-9\.]+]] = extractvalue %B [[EVB1]], 1 +; CHECK-NEXT: store i64 [[EV3]], i64* [[GEP3]], align 8 +; CHECK-NEXT: ret void + store [2 x %B] %ab, [2 x %B]* %ab.ptr, align 8 + ret void +} + define %A @loadA(%A* %a.ptr) { ; CHECK-LABEL: loadA ; CHECK-NEXT: [[GEP:%[a-z0-9\.]+]] = getelementptr inbounds %A, %A* %a.ptr, i64 0, i32 0 |

