Skip to content

Commit 4e692c9

Browse files
committed
[LoopIdiom] Don't transform loop into memmove when load from body has more than one use
This change fixes issue found by Markus: https://reviews.llvm.org/rG11338e998df1 Before this patch following code was transformed to memmove: for (int i = 15; i >= 1; i--) { p[i] = p[i-1]; sum += p[i-1]; } However load from p[i-1] is used not only by store to p[i] but also by sum computation. Therefore we cannot emit memmove in loop header. Differential Revision: https://reviews.llvm.org/D107964 (cherry picked from commit bdcf042)
1 parent c22eb9a commit 4e692c9

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,11 @@ bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(
12471247
mayLoopAccessLocation(StoreBasePtr, ModRefInfo::ModRef, CurLoop, BECount,
12481248
StoreSize, *AA, Stores);
12491249
if (UseMemMove) {
1250+
// For memmove case it's not enough to guarantee that loop doesn't access
1251+
// TheStore and TheLoad. Additionally we need to make sure that TheStore is
1252+
// the only user of TheLoad.
1253+
if (!TheLoad->hasOneUse())
1254+
return Changed;
12501255
Stores.insert(TheLoad);
12511256
if (mayLoopAccessLocation(StoreBasePtr, ModRefInfo::ModRef, CurLoop,
12521257
BECount, StoreSize, *AA, Stores)) {

llvm/test/Transforms/LoopIdiom/basic.ll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,14 +1300,10 @@ for.end: ; preds = %for.body, %entry
13001300
ret void
13011301
}
13021302

1303-
;; FIXME: Do not form memmove when load has more than one use.
1303+
;; Do not form memmove when load has more than one use.
13041304
define i32 @do_not_form_memmove5(i32* %p) {
13051305
; CHECK-LABEL: @do_not_form_memmove5(
13061306
; CHECK-NEXT: entry:
1307-
; CHECK-NEXT: [[P2:%.*]] = bitcast i32* [[P:%.*]] to i8*
1308-
; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i32, i32* [[P:%.*]], i64 1
1309-
; CHECK-NEXT: [[SCEVGEP1:%.*]] = bitcast i32* [[SCEVGEP]] to i8*
1310-
; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i64(i8* align 4 [[SCEVGEP1]], i8* align 4 [[P2]], i64 60, i1 false)
13111307
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
13121308
; CHECK: for.cond.cleanup:
13131309
; CHECK-NEXT: [[ADD_LCSSA:%.*]] = phi i32 [ [[ADD:%.*]], [[FOR_BODY]] ]
@@ -1321,6 +1317,7 @@ define i32 @do_not_form_memmove5(i32* %p) {
13211317
; CHECK-NEXT: [[TMP1:%.*]] = load i32, i32* [[ARRAYIDX]], align 4
13221318
; CHECK-NEXT: [[IDXPROM:%.*]] = zext i32 [[INDEX]] to i64
13231319
; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[IDXPROM]]
1320+
; CHECK-NEXT: store i32 [[TMP1]], i32* [[ARRAYIDX2]], align 4
13241321
; CHECK-NEXT: [[ADD]] = add nsw i32 [[TMP1]], [[SUM:%.*]]
13251322
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[INDEX]], 1
13261323
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_COND_CLEANUP]]

0 commit comments

Comments
 (0)