Skip to content

Commit acb339d

Browse files
committed
[LLE] Don't hoist conditionally executed loads
If the load is conditional we can't hoist its 0-iteration instance to the preheader because that would make it unconditional. Thus we would access a memory location that the original loop did not access. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273991 91177308-0d34-0410-b5e6-96231b3b80d8 (cherry picked from commit b168c25)
1 parent deab518 commit acb339d

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lib/Transforms/Scalar/LoopLoadElimination.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ bool doesStoreDominatesAllLatches(BasicBlock *StoreBlock, Loop *L,
119119
});
120120
}
121121

122+
/// \brief Return true if the load is not executed on all paths in the loop.
123+
static bool isLoadConditional(LoadInst *Load, Loop *L) {
124+
return Load->getParent() != L->getHeader();
125+
}
126+
122127
/// \brief The per-loop class that does most of the work.
123128
class LoadEliminationForLoop {
124129
public:
@@ -450,6 +455,12 @@ class LoadEliminationForLoop {
450455
if (!doesStoreDominatesAllLatches(Cand.Store->getParent(), L, DT))
451456
continue;
452457

458+
// If the load is conditional we can't hoist its 0-iteration instance to
459+
// the preheader because that would make it unconditional. Thus we would
460+
// access a memory location that the original loop did not access.
461+
if (isLoadConditional(Cand.Load, L))
462+
continue;
463+
453464
// Check whether the SCEV difference is the same as the induction step,
454465
// thus we load the value in the next iteration.
455466
if (!Cand.isDependenceDistanceOfOne(PSE, L))
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
; RUN: opt -S -loop-load-elim < %s | FileCheck %s
2+
3+
; We can't hoist conditional loads to the preheader for the initial value.
4+
; E.g. in the loop below we'd access array[-1] if we did:
5+
;
6+
; for(int i = 0 ; i < n ; i++ )
7+
; array[i] = ( i > 0 ? array[i - 1] : 0 ) + 4;
8+
9+
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
10+
target triple = "x86_64-apple-macosx10.11.0"
11+
12+
define void @f(i32* %array, i32 %n) {
13+
entry:
14+
%cmp10 = icmp sgt i32 %n, 0
15+
br i1 %cmp10, label %for.body, label %for.cond.cleanup
16+
17+
for.cond.cleanup: ; preds = %cond.end, %entry
18+
ret void
19+
20+
for.body: ; preds = %entry, %cond.end
21+
%indvars.iv = phi i64 [ %indvars.iv.next, %cond.end ], [ 0, %entry ]
22+
; CHECK-NOT: %store_forwarded = phi
23+
%cmp1 = icmp sgt i64 %indvars.iv, 0
24+
br i1 %cmp1, label %cond.true, label %cond.end
25+
26+
cond.true: ; preds = %for.body
27+
%0 = add nsw i64 %indvars.iv, -1
28+
%arrayidx = getelementptr inbounds i32, i32* %array, i64 %0
29+
%1 = load i32, i32* %arrayidx, align 4
30+
br label %cond.end
31+
32+
cond.end: ; preds = %for.body, %cond.true
33+
%cond = phi i32 [ %1, %cond.true ], [ 0, %for.body ]
34+
; CHECK: %cond = phi i32 [ %1, %cond.true ], [ 0, %for.body ]
35+
%add = add nsw i32 %cond, 4
36+
%arrayidx3 = getelementptr inbounds i32, i32* %array, i64 %indvars.iv
37+
store i32 %add, i32* %arrayidx3, align 4
38+
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
39+
%lftr.wideiv = trunc i64 %indvars.iv.next to i32
40+
%exitcond = icmp eq i32 %lftr.wideiv, %n
41+
br i1 %exitcond, label %for.cond.cleanup, label %for.body
42+
}

0 commit comments

Comments
 (0)