From 7059e2959d61c5b97916e1a02c610d68aed9a1fe Mon Sep 17 00:00:00 2001 From: Sanjoy Das Date: Thu, 15 Jan 2015 20:45:46 +0000 Subject: Add a new pass "inductive range check elimination" IRCE eliminates range checks of the form 0 <= A * I + B < Length by splitting a loop's iteration space into three segments in a way that the check is completely redundant in the middle segment. As an example, IRCE will convert len = < known positive > for (i = 0; i < n; i++) { if (0 <= i && i < len) { do_something(); } else { throw_out_of_bounds(); } } to len = < known positive > limit = smin(n, len) // no first segment for (i = 0; i < limit; i++) { if (0 <= i && i < len) { // this check is fully redundant do_something(); } else { throw_out_of_bounds(); } } for (i = limit; i < n; i++) { if (0 <= i && i < len) { do_something(); } else { throw_out_of_bounds(); } } IRCE can deal with multiple range checks in the same loop (it takes the intersection of the ranges that will make each of them redundant individually). Currently IRCE does not do any profitability analysis. That is a TODO. Please note that the status of this pass is *experimental*, and it is not part of any default pass pipeline. Having said that, I will love to get feedback and general input from people interested in trying this out. Differential Revision: http://reviews.llvm.org/D6693 llvm-svn: 226201 --- .../Transforms/IRCE/multiple-access-no-preloop.ll | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 llvm/test/Transforms/IRCE/multiple-access-no-preloop.ll (limited to 'llvm/test/Transforms/IRCE/multiple-access-no-preloop.ll') diff --git a/llvm/test/Transforms/IRCE/multiple-access-no-preloop.ll b/llvm/test/Transforms/IRCE/multiple-access-no-preloop.ll new file mode 100644 index 00000000000..56b7b7b6c65 --- /dev/null +++ b/llvm/test/Transforms/IRCE/multiple-access-no-preloop.ll @@ -0,0 +1,59 @@ +; RUN: opt -irce -S < %s | FileCheck %s + +define void @multiple_access_no_preloop( + i32* %arr_a, i32* %a_len_ptr, i32* %arr_b, i32* %b_len_ptr, i32 %n) { + + entry: + %len.a = load i32* %a_len_ptr, !range !0 + %len.b = load i32* %b_len_ptr, !range !0 + %first.itr.check = icmp sgt i32 %n, 0 + br i1 %first.itr.check, label %loop, label %exit + + loop: + %idx = phi i32 [ 0, %entry ] , [ %idx.next, %in.bounds.b ] + %idx.next = add i32 %idx, 1 + %abc.a = icmp slt i32 %idx, %len.a + br i1 %abc.a, label %in.bounds.a, label %out.of.bounds + + in.bounds.a: + %addr.a = getelementptr i32* %arr_a, i32 %idx + store i32 0, i32* %addr.a + %abc.b = icmp slt i32 %idx, %len.b + br i1 %abc.b, label %in.bounds.b, label %out.of.bounds + + in.bounds.b: + %addr.b = getelementptr i32* %arr_b, i32 %idx + store i32 -1, i32* %addr.b + %next = icmp slt i32 %idx.next, %n + br i1 %next, label %loop, label %exit + + out.of.bounds: + ret void + + exit: + ret void +} + +; CHECK-LABEL: multiple_access_no_preloop + +; CHECK-LABEL: loop.preheader: +; CHECK: [[smaller_len_cmp:[^ ]+]] = icmp slt i32 %len.a, %len.b +; CHECK: [[smaller_len:[^ ]+]] = select i1 [[smaller_len_cmp]], i32 %len.a, i32 %len.b +; CHECK: [[upper_bound_cmp:[^ ]+]] = icmp slt i32 %n, %3 +; CHECK: [[upper_bound:[^ ]+]] = select i1 %5, i32 %n, i32 %3 + +; CHECK-LABEL: loop: +; CHECK: br i1 true, label %in.bounds.a, label %out.of.bounds + +; CHECK-LABEL: in.bounds.a: +; CHECK: br i1 true, label %in.bounds.b, label %out.of.bounds + +; CHECK-LABEL: in.bounds.b: +; CHECK: [[main_loop_cond:[^ ]+]] = icmp slt i32 %idx.next, [[upper_bound]] +; CHECK: br i1 [[main_loop_cond]], label %loop, label %main.exit.selector + +; CHECK-LABEL: in.bounds.b.postloop: +; CHECK: %next.postloop = icmp slt i32 %idx.next.postloop, %n +; CHECK: br i1 %next.postloop, label %loop.postloop, label %exit.loopexit + +!0 = !{i32 0, i32 2147483647} -- cgit v1.2.3