summaryrefslogtreecommitdiff
path: root/test/Transforms/LoopReroll/reduction.ll
diff options
context:
space:
mode:
authorHal Finkel <hfinkel@anl.gov>2013-11-16 23:59:05 +0000
committerHal Finkel <hfinkel@anl.gov>2013-11-16 23:59:05 +0000
commitbebe48dbfe00078329341945bfb11f778ace6d12 (patch)
tree5efe3810ad3db10985cd43351ed4a7bd83727908 /test/Transforms/LoopReroll/reduction.ll
parent0ccb37a7339883e1fd090beadc2deb1ce40ea7d4 (diff)
downloadllvm-bebe48dbfe00078329341945bfb11f778ace6d12.tar.gz
llvm-bebe48dbfe00078329341945bfb11f778ace6d12.tar.bz2
llvm-bebe48dbfe00078329341945bfb11f778ace6d12.tar.xz
Add a loop rerolling pass
This adds a loop rerolling pass: the opposite of (partial) loop unrolling. The transformation aims to take loops like this: for (int i = 0; i < 3200; i += 5) { a[i] += alpha * b[i]; a[i + 1] += alpha * b[i + 1]; a[i + 2] += alpha * b[i + 2]; a[i + 3] += alpha * b[i + 3]; a[i + 4] += alpha * b[i + 4]; } and turn them into this: for (int i = 0; i < 3200; ++i) { a[i] += alpha * b[i]; } and loops like this: for (int i = 0; i < 500; ++i) { x[3*i] = foo(0); x[3*i+1] = foo(0); x[3*i+2] = foo(0); } and turn them into this: for (int i = 0; i < 1500; ++i) { x[i] = foo(0); } There are two motivations for this transformation: 1. Code-size reduction (especially relevant, obviously, when compiling for code size). 2. Providing greater choice to the loop vectorizer (and generic unroller) to choose the unrolling factor (and a better ability to vectorize). The loop vectorizer can take vector lengths and register pressure into account when choosing an unrolling factor, for example, and a pre-unrolled loop limits that choice. This is especially problematic if the manual unrolling was optimized for a machine different from the current target. The current implementation is limited to single basic-block loops only. The rerolling recognition should work regardless of how the loop iterations are intermixed within the loop body (subject to dependency and side-effect constraints), but the significant restriction is that the order of the instructions in each iteration must be identical. This seems sufficient to capture all current use cases. This pass is not currently enabled by default at any optimization level. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@194939 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/Transforms/LoopReroll/reduction.ll')
-rw-r--r--test/Transforms/LoopReroll/reduction.ll96
1 files changed, 96 insertions, 0 deletions
diff --git a/test/Transforms/LoopReroll/reduction.ll b/test/Transforms/LoopReroll/reduction.ll
new file mode 100644
index 0000000000..aed7670b66
--- /dev/null
+++ b/test/Transforms/LoopReroll/reduction.ll
@@ -0,0 +1,96 @@
+; RUN: opt < %s -loop-reroll -S | FileCheck %s
+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-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define i32 @foo(i32* nocapture readonly %x) #0 {
+entry:
+ br label %for.body
+
+for.body: ; preds = %entry, %for.body
+ %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
+ %r.029 = phi i32 [ 0, %entry ], [ %add12, %for.body ]
+ %arrayidx = getelementptr inbounds i32* %x, i64 %indvars.iv
+ %0 = load i32* %arrayidx, align 4
+ %add = add nsw i32 %0, %r.029
+ %1 = or i64 %indvars.iv, 1
+ %arrayidx3 = getelementptr inbounds i32* %x, i64 %1
+ %2 = load i32* %arrayidx3, align 4
+ %add4 = add nsw i32 %add, %2
+ %3 = or i64 %indvars.iv, 2
+ %arrayidx7 = getelementptr inbounds i32* %x, i64 %3
+ %4 = load i32* %arrayidx7, align 4
+ %add8 = add nsw i32 %add4, %4
+ %5 = or i64 %indvars.iv, 3
+ %arrayidx11 = getelementptr inbounds i32* %x, i64 %5
+ %6 = load i32* %arrayidx11, align 4
+ %add12 = add nsw i32 %add8, %6
+ %indvars.iv.next = add nuw nsw i64 %indvars.iv, 4
+ %7 = trunc i64 %indvars.iv.next to i32
+ %cmp = icmp slt i32 %7, 400
+ br i1 %cmp, label %for.body, label %for.end
+
+; CHECK-LABEL: @foo
+
+; CHECK: for.body:
+; CHECK: %indvar = phi i64 [ %indvar.next, %for.body ], [ 0, %entry ]
+; CHECK: %r.029 = phi i32 [ 0, %entry ], [ %add, %for.body ]
+; CHECK: %arrayidx = getelementptr inbounds i32* %x, i64 %indvar
+; CHECK: %0 = load i32* %arrayidx, align 4
+; CHECK: %add = add nsw i32 %0, %r.029
+; CHECK: %indvar.next = add i64 %indvar, 1
+; CHECK: %exitcond = icmp eq i64 %indvar.next, 400
+; CHECK: br i1 %exitcond, label %for.end, label %for.body
+
+; CHECK: ret
+
+for.end: ; preds = %for.body
+ ret i32 %add12
+}
+
+define float @bar(float* nocapture readonly %x) #0 {
+entry:
+ br label %for.body
+
+for.body: ; preds = %entry, %for.body
+ %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
+ %r.029 = phi float [ 0.0, %entry ], [ %add12, %for.body ]
+ %arrayidx = getelementptr inbounds float* %x, i64 %indvars.iv
+ %0 = load float* %arrayidx, align 4
+ %add = fadd float %0, %r.029
+ %1 = or i64 %indvars.iv, 1
+ %arrayidx3 = getelementptr inbounds float* %x, i64 %1
+ %2 = load float* %arrayidx3, align 4
+ %add4 = fadd float %add, %2
+ %3 = or i64 %indvars.iv, 2
+ %arrayidx7 = getelementptr inbounds float* %x, i64 %3
+ %4 = load float* %arrayidx7, align 4
+ %add8 = fadd float %add4, %4
+ %5 = or i64 %indvars.iv, 3
+ %arrayidx11 = getelementptr inbounds float* %x, i64 %5
+ %6 = load float* %arrayidx11, align 4
+ %add12 = fadd float %add8, %6
+ %indvars.iv.next = add nuw nsw i64 %indvars.iv, 4
+ %7 = trunc i64 %indvars.iv.next to i32
+ %cmp = icmp slt i32 %7, 400
+ br i1 %cmp, label %for.body, label %for.end
+
+; CHECK-LABEL: @bar
+
+; CHECK: for.body:
+; CHECK: %indvar = phi i64 [ %indvar.next, %for.body ], [ 0, %entry ]
+; CHECK: %r.029 = phi float [ 0.000000e+00, %entry ], [ %add, %for.body ]
+; CHECK: %arrayidx = getelementptr inbounds float* %x, i64 %indvar
+; CHECK: %0 = load float* %arrayidx, align 4
+; CHECK: %add = fadd float %0, %r.029
+; CHECK: %indvar.next = add i64 %indvar, 1
+; CHECK: %exitcond = icmp eq i64 %indvar.next, 400
+; CHECK: br i1 %exitcond, label %for.end, label %for.body
+
+; CHECK: ret
+
+for.end: ; preds = %for.body
+ ret float %add12
+}
+
+attributes #0 = { nounwind readonly uwtable }
+