llvm-project/llvm/test/Transforms/IndVarSimplify/lftr-reuse.ll

240 lines
7.0 KiB
LLVM
Raw Normal View History

; RUN: opt < %s -indvars -S | FileCheck %s
;
; Make sure that indvars can perform LFTR without a canonical IV.
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
; Perform LFTR using the original pointer-type IV.
; for(char* p = base; p < base + n; ++p) {
; *p = p-base;
; }
define void @ptriv(i8* %base, i32 %n) nounwind {
entry:
%idx.ext = sext i32 %n to i64
%add.ptr = getelementptr inbounds i8* %base, i64 %idx.ext
%cmp1 = icmp ult i8* %base, %add.ptr
br i1 %cmp1, label %for.body, label %for.end
; CHECK: for.body:
; CHECK: phi i8*
; CHECK-NOT: phi
; CHECK-NOT: add
; CHECK: icmp ne i8*
; CHECK: br i1
for.body:
%p.02 = phi i8* [ %base, %entry ], [ %incdec.ptr, %for.body ]
; cruft to make the IV useful
%sub.ptr.lhs.cast = ptrtoint i8* %p.02 to i64
%sub.ptr.rhs.cast = ptrtoint i8* %base to i64
%sub.ptr.sub = sub i64 %sub.ptr.lhs.cast, %sub.ptr.rhs.cast
%conv = trunc i64 %sub.ptr.sub to i8
store i8 %conv, i8* %p.02
%incdec.ptr = getelementptr inbounds i8* %p.02, i32 1
%cmp = icmp ult i8* %incdec.ptr, %add.ptr
br i1 %cmp, label %for.body, label %for.end
for.end:
ret void
}
[LPM] Make LoopSimplify no longer a LoopPass and instead both a utility function and a FunctionPass. This has many benefits. The motivating use case was to be able to compute function analysis passes *after* running LoopSimplify (to avoid invalidating them) and then to run other passes which require LoopSimplify. Specifically passes like unrolling and vectorization are critical to wire up to BranchProbabilityInfo and BlockFrequencyInfo so that they can be profile aware. For the LoopVectorize pass the only things in the way are LoopSimplify and LCSSA. This fixes LoopSimplify and LCSSA is next on my list. There are also a bunch of other benefits of doing this: - It is now very feasible to make more passes *preserve* LoopSimplify because they can simply run it after changing a loop. Because subsequence passes can assume LoopSimplify is preserved we can reduce the runs of this pass to the times when we actually mutate a loop structure. - The new pass manager should be able to more easily support loop passes factored in this way. - We can at long, long last observe that LoopSimplify is preserved across SCEV. This *halves* the number of times we run LoopSimplify!!! Now, getting here wasn't trivial. First off, the interfaces used by LoopSimplify are all over the map regarding how analysis are updated. We end up with weird "pass" parameters as a consequence. I'll try to clean at least some of this up later -- I'll have to have it all clean for the new pass manager. Next up I discovered a really frustrating bug. LoopUnroll *claims* to preserve LoopSimplify. That's actually a lie. But the way the LoopPassManager ends up running the passes, it always ran LoopSimplify on the unrolled-into loop, rectifying this oversight before any verification could kick in and point out that in fact nothing was preserved. So I've added code to the unroller to *actually* simplify the surrounding loop when it succeeds at unrolling. The only functional change in the test suite is that we now catch a case that was previously missed because SCEV and other loop transforms see their containing loops as simplified and thus don't miss some opportunities. One test case has been converted to check that we catch this case rather than checking that we miss it but at least don't get the wrong answer. Note that I have #if-ed out all of the verification logic in LoopSimplify! This is a temporary workaround while extracting these bits from the LoopPassManager. Currently, there is no way to have a pass in the LoopPassManager which preserves LoopSimplify along with one which does not. The LPM will try to verify on each loop in the nest that LoopSimplify holds but the now-Function-pass cannot distinguish what loop is being verified and so must try to verify all of them. The inner most loop is clearly no longer simplified as there is a pass which didn't even *attempt* to preserve it. =/ Once I get LCSSA out (and maybe LoopVectorize and some other fixes) I'll be able to re-enable this check and catch any places where we are still failing to preserve LoopSimplify. If this causes problems I can back this out and try to commit *all* of this at once, but so far this seems to work and allow much more incremental progress. llvm-svn: 199884
2014-01-23 19:23:19 +08:00
; This test checks that SCEVExpander can handle an outer loop that has been
; simplified, and as a result the inner loop's exit test will be rewritten.
define void @expandOuterRecurrence(i32 %arg) nounwind {
entry:
%sub1 = sub nsw i32 %arg, 1
%cmp1 = icmp slt i32 0, %sub1
br i1 %cmp1, label %outer, label %exit
[LPM] Make LoopSimplify no longer a LoopPass and instead both a utility function and a FunctionPass. This has many benefits. The motivating use case was to be able to compute function analysis passes *after* running LoopSimplify (to avoid invalidating them) and then to run other passes which require LoopSimplify. Specifically passes like unrolling and vectorization are critical to wire up to BranchProbabilityInfo and BlockFrequencyInfo so that they can be profile aware. For the LoopVectorize pass the only things in the way are LoopSimplify and LCSSA. This fixes LoopSimplify and LCSSA is next on my list. There are also a bunch of other benefits of doing this: - It is now very feasible to make more passes *preserve* LoopSimplify because they can simply run it after changing a loop. Because subsequence passes can assume LoopSimplify is preserved we can reduce the runs of this pass to the times when we actually mutate a loop structure. - The new pass manager should be able to more easily support loop passes factored in this way. - We can at long, long last observe that LoopSimplify is preserved across SCEV. This *halves* the number of times we run LoopSimplify!!! Now, getting here wasn't trivial. First off, the interfaces used by LoopSimplify are all over the map regarding how analysis are updated. We end up with weird "pass" parameters as a consequence. I'll try to clean at least some of this up later -- I'll have to have it all clean for the new pass manager. Next up I discovered a really frustrating bug. LoopUnroll *claims* to preserve LoopSimplify. That's actually a lie. But the way the LoopPassManager ends up running the passes, it always ran LoopSimplify on the unrolled-into loop, rectifying this oversight before any verification could kick in and point out that in fact nothing was preserved. So I've added code to the unroller to *actually* simplify the surrounding loop when it succeeds at unrolling. The only functional change in the test suite is that we now catch a case that was previously missed because SCEV and other loop transforms see their containing loops as simplified and thus don't miss some opportunities. One test case has been converted to check that we catch this case rather than checking that we miss it but at least don't get the wrong answer. Note that I have #if-ed out all of the verification logic in LoopSimplify! This is a temporary workaround while extracting these bits from the LoopPassManager. Currently, there is no way to have a pass in the LoopPassManager which preserves LoopSimplify along with one which does not. The LPM will try to verify on each loop in the nest that LoopSimplify holds but the now-Function-pass cannot distinguish what loop is being verified and so must try to verify all of them. The inner most loop is clearly no longer simplified as there is a pass which didn't even *attempt* to preserve it. =/ Once I get LCSSA out (and maybe LoopVectorize and some other fixes) I'll be able to re-enable this check and catch any places where we are still failing to preserve LoopSimplify. If this causes problems I can back this out and try to commit *all* of this at once, but so far this seems to work and allow much more incremental progress. llvm-svn: 199884
2014-01-23 19:23:19 +08:00
; CHECK: outer:
; CHECK: icmp slt
outer:
%i = phi i32 [ 0, %entry ], [ %i.inc, %outer.inc ]
%sub2 = sub nsw i32 %arg, %i
%sub3 = sub nsw i32 %sub2, 1
%cmp2 = icmp slt i32 0, %sub3
br i1 %cmp2, label %inner.ph, label %outer.inc
inner.ph:
br label %inner
; CHECK: inner:
; CHECK: br i1
inner:
%j = phi i32 [ 0, %inner.ph ], [ %j.inc, %inner ]
%j.inc = add nsw i32 %j, 1
%cmp3 = icmp slt i32 %j.inc, %sub3
br i1 %cmp3, label %inner, label %outer.inc
; CHECK: outer.inc:
; CHECK: icmp ne
; CHECK: br i1
outer.inc:
%i.inc = add nsw i32 %i, 1
%cmp4 = icmp slt i32 %i.inc, %sub1
br i1 %cmp4, label %outer, label %exit
exit:
ret void
}
; Force SCEVExpander to look for an existing well-formed phi.
; Perform LFTR without generating extra preheader code.
define void @guardedloop([0 x double]* %matrix, [0 x double]* %vector,
i32 %irow, i32 %ilead) nounwind {
; CHECK-LABEL: @guardedloop(
; CHECK-LABEL: entry:
; CHECK-NEXT: %[[cmp:.*]] = icmp slt i32 1, %irow
; CHECK-NEXT: br i1 %[[cmp]], label %[[loop_preheader:.*]], label %[[return:.*]]
; CHECK: [[loop_preheader]]:
; CHECK-NEXT: %[[sext:.*]] = sext i32 %ilead to i64
; CHECK-NEXT: %[[add:.*]] = add i32 %irow, -1
; CHECK-NEXT: br label %[[loop:.*]]
; CHECK: [[loop]]:
; CHECK-NEXT: %[[indvars_iv2:.*]] = phi i64
; CHECK-NEXT: phi i64
; CHECK-NOT: phi
; CHECK: %[[lftr_wideiv:.*]] = trunc i64 %[[indvars_iv2]] to i32
; CHECK-NEXT: %[[exitcond:.*]] = icmp ne i32 %[[lftr_wideiv]], %[[add]]
; CHECK-NEXT: br i1 %[[exitcond]], label %[[loop]], label
entry:
%cmp = icmp slt i32 1, %irow
br i1 %cmp, label %loop, label %return
loop:
%rowidx = phi i32 [ 0, %entry ], [ %row.inc, %loop ]
%i = phi i32 [ 0, %entry ], [ %i.inc, %loop ]
%diagidx = add nsw i32 %rowidx, %i
%diagidxw = sext i32 %diagidx to i64
%matrixp = getelementptr inbounds [0 x double]* %matrix, i32 0, i64 %diagidxw
%v1 = load double* %matrixp
%iw = sext i32 %i to i64
%vectorp = getelementptr inbounds [0 x double]* %vector, i32 0, i64 %iw
%v2 = load double* %vectorp
%row.inc = add nsw i32 %rowidx, %ilead
%i.inc = add nsw i32 %i, 1
%cmp196 = icmp slt i32 %i.inc, %irow
br i1 %cmp196, label %loop, label %return
return:
ret void
}
; Avoid generating extra code to materialize a trip count. Skip LFTR.
define void @unguardedloop([0 x double]* %matrix, [0 x double]* %vector,
i32 %irow, i32 %ilead) nounwind {
entry:
br label %loop
; CHECK: entry:
; CHECK-NOT: zext
; CHECK-NOT: add
; CHECK: loop:
; CHECK: phi i64
; CHECK: phi i64
; CHECK-NOT: phi
; CHECK: icmp slt
; CHECK: br i1
loop:
%rowidx = phi i32 [ 0, %entry ], [ %row.inc, %loop ]
%i = phi i32 [ 0, %entry ], [ %i.inc, %loop ]
%diagidx = add nsw i32 %rowidx, %i
%diagidxw = sext i32 %diagidx to i64
%matrixp = getelementptr inbounds [0 x double]* %matrix, i32 0, i64 %diagidxw
%v1 = load double* %matrixp
%iw = sext i32 %i to i64
%vectorp = getelementptr inbounds [0 x double]* %vector, i32 0, i64 %iw
%v2 = load double* %vectorp
%row.inc = add nsw i32 %rowidx, %ilead
%i.inc = add nsw i32 %i, 1
%cmp196 = icmp slt i32 %i.inc, %irow
br i1 %cmp196, label %loop, label %return
return:
ret void
}
; Remove %i which is only used by the exit test.
; Verify that SCEV can still compute a backedge count from the sign
; extended %n, used for pointer comparison by LFTR.
;
; TODO: Fix for PR13371 currently makes this impossible. See
; IndVarSimplify.cpp hasConcreteDef(). We may want to change to undef rules.
define void @geplftr(i8* %base, i32 %x, i32 %y, i32 %n) nounwind {
entry:
%x.ext = sext i32 %x to i64
%add.ptr = getelementptr inbounds i8* %base, i64 %x.ext
%y.ext = sext i32 %y to i64
%add.ptr10 = getelementptr inbounds i8* %add.ptr, i64 %y.ext
%lim = add i32 %x, %n
%cmp.ph = icmp ult i32 %x, %lim
br i1 %cmp.ph, label %loop, label %exit
; CHECK-LABEL: @geplftr(
; CHECK: loop:
; CHECK: phi i8*
; DISABLE-NOT: phi // This check is currently disabled
; CHECK: getelementptr
; CHECK: store
; DISABLE: icmp ne i8* // This check is currently disabled
; CHECK: br i1
loop:
%i = phi i32 [ %x, %entry ], [ %inc, %loop ]
%aptr = phi i8* [ %add.ptr10, %entry ], [ %incdec.ptr, %loop ]
%incdec.ptr = getelementptr inbounds i8* %aptr, i32 1
store i8 3, i8* %aptr
%inc = add i32 %i, 1
%cmp = icmp ult i32 %inc, %lim
br i1 %cmp, label %loop, label %exit
exit:
ret void
}
; Exercise backedge taken count verification with a never-taken loop.
define void @nevertaken() nounwind uwtable ssp {
entry:
br label %loop
; CHECK-LABEL: @nevertaken(
; CHECK: loop:
; CHECK-NOT: phi
; CHECK-NOT: add
; CHECK-NOT: icmp
; CHECK: exit:
loop:
%i = phi i32 [ 0, %entry ], [ %inc, %loop ]
%inc = add nsw i32 %i, 1
%cmp = icmp sle i32 %inc, 0
br i1 %cmp, label %loop, label %exit
exit:
ret void
}
; Test LFTR on an IV whose recurrence start is a non-unit pointer type.
define void @aryptriv([256 x i8]* %base, i32 %n) nounwind {
entry:
%ivstart = getelementptr inbounds [256 x i8]* %base, i32 0, i32 0
%ivend = getelementptr inbounds [256 x i8]* %base, i32 0, i32 %n
%cmp.ph = icmp ult i8* %ivstart, %ivend
br i1 %cmp.ph, label %loop, label %exit
; CHECK: loop:
; CHECK: phi i8*
; CHECK-NOT: phi
; CHECK: getelementptr
; CHECK: store
; CHECK: icmp ne i8*
; CHECK: br i1
loop:
%aptr = phi i8* [ %ivstart, %entry ], [ %incdec.ptr, %loop ]
%incdec.ptr = getelementptr inbounds i8* %aptr, i32 1
store i8 3, i8* %aptr
%cmp = icmp ult i8* %incdec.ptr, %ivend
br i1 %cmp, label %loop, label %exit
exit:
ret void
}