[llvm] ad9ae6e - MemCpyOpt cannot use ABI alignment even if it was not given
Juneyoung Lee via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 5 13:22:03 PST 2020
Author: Juneyoung Lee
Date: 2020-02-06T06:21:55+09:00
New Revision: ad9ae6ee2b3c697a953321eeedb3ab0d0c922864
URL: https://github.com/llvm/llvm-project/commit/ad9ae6ee2b3c697a953321eeedb3ab0d0c922864
DIFF: https://github.com/llvm/llvm-project/commit/ad9ae6ee2b3c697a953321eeedb3ab0d0c922864.diff
LOG: MemCpyOpt cannot use ABI alignment even if it was not given
Summary: This patch fixes https://bugs.llvm.org/show_bug.cgi?id=44388 which incorrectly assigns an ABI alignment to memset when there was no explicit alignment given.
Reviewers: gchatelet, lenary, nikic
Reviewed By: nikic
Subscribers: hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74083
Added:
Modified:
llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
llvm/test/Transforms/MemCpyOpt/form-memset.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
index c24fa40860eb..fe7596f3bbe0 100644
--- a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -144,6 +144,24 @@ bool MemsetRange::isProfitableToUseMemset(const DataLayout &DL) const {
return TheStores.size() > NumPointerStores+NumByteStores;
}
+
+static Align findStoreAlignment(const DataLayout &DL, const StoreInst *SI) {
+ return DL.getValueOrABITypeAlignment(MaybeAlign(SI->getAlignment()),
+ SI->getOperand(0)->getType());
+}
+
+static Align findLoadAlignment(const DataLayout &DL, const LoadInst *LI) {
+ return DL.getValueOrABITypeAlignment(MaybeAlign(LI->getAlignment()),
+ LI->getType());
+}
+
+static Align findCommonAlignment(const DataLayout &DL, const StoreInst *SI,
+ const LoadInst *LI) {
+ Align StoreAlign = findStoreAlignment(DL, SI);
+ Align LoadAlign = findLoadAlignment(DL, LI);
+ return commonAlignment(StoreAlign, LoadAlign);
+}
+
namespace {
class MemsetRanges {
@@ -173,8 +191,8 @@ class MemsetRanges {
void addStore(int64_t OffsetFromFirst, StoreInst *SI) {
int64_t StoreSize = DL.getTypeStoreSize(SI->getOperand(0)->getType());
- addRange(OffsetFromFirst, StoreSize,
- SI->getPointerOperand(), SI->getAlignment(), SI);
+ addRange(OffsetFromFirst, StoreSize, SI->getPointerOperand(),
+ findStoreAlignment(DL, SI).value(), SI);
}
void addMemSet(int64_t OffsetFromFirst, MemSetInst *MSI) {
@@ -387,13 +405,8 @@ Instruction *MemCpyOptPass::tryMergingIntoMemset(Instruction *StartInst,
// Get the starting pointer of the block.
StartPtr = Range.StartPtr;
- // Determine alignment
- const Align Alignment = DL.getValueOrABITypeAlignment(
- MaybeAlign(Range.Alignment),
- cast<PointerType>(StartPtr->getType())->getElementType());
-
AMemSet = Builder.CreateMemSet(StartPtr, ByteVal, Range.End - Range.Start,
- Alignment);
+ MaybeAlign(Range.Alignment));
LLVM_DEBUG(dbgs() << "Replace stores:\n"; for (Instruction *SI
: Range.TheStores) dbgs()
<< *SI << '\n';
@@ -413,23 +426,6 @@ Instruction *MemCpyOptPass::tryMergingIntoMemset(Instruction *StartInst,
return AMemSet;
}
-static Align findStoreAlignment(const DataLayout &DL, const StoreInst *SI) {
- return DL.getValueOrABITypeAlignment(MaybeAlign(SI->getAlignment()),
- SI->getOperand(0)->getType());
-}
-
-static Align findLoadAlignment(const DataLayout &DL, const LoadInst *LI) {
- return DL.getValueOrABITypeAlignment(MaybeAlign(LI->getAlignment()),
- LI->getType());
-}
-
-static Align findCommonAlignment(const DataLayout &DL, const StoreInst *SI,
- const LoadInst *LI) {
- Align StoreAlign = findStoreAlignment(DL, SI);
- Align LoadAlign = findLoadAlignment(DL, LI);
- return commonAlignment(StoreAlign, LoadAlign);
-}
-
// This method try to lift a store instruction before position P.
// It will lift the store and its argument + that anything that
// may alias with these.
diff --git a/llvm/test/Transforms/MemCpyOpt/form-memset.ll b/llvm/test/Transforms/MemCpyOpt/form-memset.ll
index 836a6107d249..dde025dac926 100644
--- a/llvm/test/Transforms/MemCpyOpt/form-memset.ll
+++ b/llvm/test/Transforms/MemCpyOpt/form-memset.ll
@@ -217,7 +217,7 @@ entry:
tail call void @llvm.memset.p0i8.i64(i8* %1, i8 0, i64 12, i1 false)
ret void
; CHECK-LABEL: @test6(
-; CHECK: call void @llvm.memset.p0i8.i64(i8* align 4 %2, i8 0, i64 24, i1 false)
+; CHECK: call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 24, i1 false)
}
; More aggressive heuristic
@@ -280,7 +280,7 @@ define void @test10(i8* nocapture %P) nounwind {
ret void
; CHECK-LABEL: @test10(
; CHECK-NOT: memset
-; CHECK: call void @llvm.memset.p0i8.i64(i8* align 1 %P, i8 0, i64 42, i1 false)
+; CHECK: call void @llvm.memset.p0i8.i64(i8* %P, i8 0, i64 42, i1 false)
; CHECK-NOT: memset
; CHECK: ret void
}
@@ -299,3 +299,16 @@ entry:
; CHECK-NOT: store
; CHECK: call void @llvm.memset.p0i8.i64(i8* align 4 %1, i8 1, i64 23, i1 false)
}
+
+; Alignment should be preserved when there is a store with default align
+define void @test12(i32* nocapture %P) nounwind ssp {
+entry:
+ store i32 0, i32* %P
+ %add.ptr = getelementptr inbounds i32, i32* %P, i64 1
+ %0 = bitcast i32* %add.ptr to i8*
+ tail call void @llvm.memset.p0i8.i64(i8* %0, i8 0, i64 11, i1 false)
+ ret void
+; CHECK-LABEL: @test12(
+; CHECK-NOT: store
+; CHECK: call void @llvm.memset.p0i8.i64(i8* align 4 %1, i8 0, i64 15, i1 false)
+}
More information about the llvm-commits
mailing list