[PATCH] D107964: [LoopIdiom] Don't transform loop into memmove when load from body has more than one use
Dawid Jurczak via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 13 07:28:33 PDT 2021
yurai007 updated this revision to Diff 366275.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D107964/new/
https://reviews.llvm.org/D107964
Files:
llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
llvm/test/Transforms/LoopIdiom/basic.ll
Index: llvm/test/Transforms/LoopIdiom/basic.ll
===================================================================
--- llvm/test/Transforms/LoopIdiom/basic.ll
+++ llvm/test/Transforms/LoopIdiom/basic.ll
@@ -1300,6 +1300,50 @@
ret void
}
+;; Do not form memmove when load has more than one use.
+define i32 @do_not_form_memmove5(i32* %p) {
+; CHECK-LABEL: @do_not_form_memmove5(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[FOR_BODY:%.*]]
+; CHECK: for.cond.cleanup:
+; CHECK-NEXT: [[ADD_LCSSA:%.*]] = phi i32 [ [[ADD:%.*]], [[FOR_BODY]] ]
+; CHECK-NEXT: ret i32 [[ADD_LCSSA]]
+; CHECK: for.body:
+; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 15, [[ENTRY:%.*]] ], [ [[SUB:%.*]], [[FOR_BODY]] ]
+; CHECK-NEXT: [[SUM:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[ADD]], [[FOR_BODY]] ]
+; CHECK-NEXT: [[SUB]] = add nsw i32 [[INDEX]], -1
+; CHECK-NEXT: [[TMP0:%.*]] = zext i32 [[SUB]] to i64
+; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[TMP0]]
+; CHECK-NEXT: [[TMP1:%.*]] = load i32, i32* [[ARRAYIDX]], align 4
+; CHECK-NEXT: [[IDXPROM:%.*]] = zext i32 [[INDEX]] to i64
+; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[IDXPROM]]
+; CHECK-NEXT: store i32 [[TMP1]], i32* [[ARRAYIDX2]], align 4
+; CHECK-NEXT: [[ADD]] = add nsw i32 [[TMP1]], [[SUM:%.*]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[INDEX]], 1
+; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_COND_CLEANUP]]
+;
+entry:
+ br label %for.body
+
+for.cond.cleanup: ; preds = %for.body
+ %add.lcssa = phi i32 [ %add, %for.body ]
+ ret i32 %add.lcssa
+
+for.body: ; preds = %entry, %for.body
+ %index = phi i32 [ 15, %entry ], [ %sub, %for.body ]
+ %sum = phi i32 [ 0, %entry ], [ %add, %for.body ]
+ %sub = add nsw i32 %index, -1
+ %0 = zext i32 %sub to i64
+ %arrayidx = getelementptr inbounds i32, i32* %p, i64 %0
+ %1 = load i32, i32* %arrayidx, align 4
+ %idxprom = zext i32 %index to i64
+ %arrayidx2 = getelementptr inbounds i32, i32* %p, i64 %idxprom
+ store i32 %1, i32* %arrayidx2, align 4
+ %add = add nsw i32 %1, %sum
+ %cmp = icmp sgt i32 %index, 1
+ br i1 %cmp, label %for.body, label %for.cond.cleanup
+}
+
;; Memcpy formation is still preferred over memmove.
define void @prefer_memcpy_over_memmove(i8* noalias %Src, i8* noalias %Dest, i64 %Size) {
; CHECK-LABEL: @prefer_memcpy_over_memmove(
Index: llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
+++ llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
@@ -1276,6 +1276,8 @@
mayLoopAccessLocation(StoreBasePtr, ModRefInfo::ModRef, CurLoop, BECount,
StoreSizeSCEV, *AA, Stores);
if (UseMemMove) {
+ if (!TheLoad->hasOneUse())
+ return Changed;
Stores.insert(TheLoad);
if (mayLoopAccessLocation(StoreBasePtr, ModRefInfo::ModRef, CurLoop,
BECount, StoreSizeSCEV, *AA, Stores)) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107964.366275.patch
Type: text/x-patch
Size: 3133 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210813/d3d1001f/attachment.bin>
More information about the llvm-commits
mailing list